MediaWiki  1.30.0
MagicVariableTest.php
Go to the documentation of this file.
1 <?php
21  private $testParser = null;
22 
30  private $expectedAsInteger = [
31  'revisionday',
32  'revisionmonth1',
33  ];
34 
36  protected function setUp() {
37  parent::setUp();
38 
39  $contLang = Language::factory( 'en' );
40  $this->setMwGlobals( [
41  'wgLanguageCode' => 'en',
42  'wgContLang' => $contLang,
43  ] );
44 
45  $this->testParser = new Parser();
46  $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
47 
48  # initialize parser output
49  $this->testParser->clearState();
50 
51  # Needs a title to do magic word stuff
52  $title = Title::newFromText( 'Tests' );
53  # Else it needs a db connection just to check if it's a redirect
54  # (when deciding the page language).
55  $title->mRedirect = false;
56 
57  $this->testParser->setTitle( $title );
58  }
59 
64  private static function createProviderUpTo( $num ) {
65  $ret = [];
66  for ( $i = 1; $i <= $num; $i++ ) {
67  $ret[] = [ $i ];
68  }
69 
70  return $ret;
71  }
72 
76  public static function provideMonths() {
77  return self::createProviderUpTo( 12 );
78  }
79 
83  public static function provideDays() {
84  return self::createProviderUpTo( 31 );
85  }
86 
87  # ############## TESTS #############################################
88  # @todo FIXME:
89  # - those got copy pasted, we can probably make them cleaner
90  # - tests are lacking useful messages
91 
92  # day
93 
95  public function testCurrentdayIsUnPadded( $day ) {
96  $this->assertUnPadded( 'currentday', $day );
97  }
98 
100  public function testCurrentdaytwoIsZeroPadded( $day ) {
101  $this->assertZeroPadded( 'currentday2', $day );
102  }
103 
105  public function testLocaldayIsUnPadded( $day ) {
106  $this->assertUnPadded( 'localday', $day );
107  }
108 
110  public function testLocaldaytwoIsZeroPadded( $day ) {
111  $this->assertZeroPadded( 'localday2', $day );
112  }
113 
114  # month
115 
117  public function testCurrentmonthIsZeroPadded( $month ) {
118  $this->assertZeroPadded( 'currentmonth', $month );
119  }
120 
122  public function testCurrentmonthoneIsUnPadded( $month ) {
123  $this->assertUnPadded( 'currentmonth1', $month );
124  }
125 
127  public function testLocalmonthIsZeroPadded( $month ) {
128  $this->assertZeroPadded( 'localmonth', $month );
129  }
130 
132  public function testLocalmonthoneIsUnPadded( $month ) {
133  $this->assertUnPadded( 'localmonth1', $month );
134  }
135 
136  # revision day
137 
139  public function testRevisiondayIsUnPadded( $day ) {
140  $this->assertUnPadded( 'revisionday', $day );
141  }
142 
144  public function testRevisiondaytwoIsZeroPadded( $day ) {
145  $this->assertZeroPadded( 'revisionday2', $day );
146  }
147 
148  # revision month
149 
151  public function testRevisionmonthIsZeroPadded( $month ) {
152  $this->assertZeroPadded( 'revisionmonth', $month );
153  }
154 
156  public function testRevisionmonthoneIsUnPadded( $month ) {
157  $this->assertUnPadded( 'revisionmonth1', $month );
158  }
159 
160  # ############## HELPERS ############################################
161 
163  public function assertZeroPadded( $magic, $value ) {
164  $this->assertMagicPadding( $magic, $value, '%02d' );
165  }
166 
168  public function assertUnPadded( $magic, $value ) {
169  $this->assertMagicPadding( $magic, $value, '%d' );
170  }
171 
178  private function assertMagicPadding( $magic, $value, $format ) {
179  # Initialize parser timestamp as year 2010 at 12h34 56s.
180  # month and day are given by the caller ($value). Month < 12!
181  if ( $value > 12 ) {
182  $month = $value % 12;
183  } else {
184  $month = $value;
185  }
186 
187  $this->setParserTS(
188  sprintf( '2010%02d%02d123456', $month, $value )
189  );
190 
191  # please keep the following commented line of code. It helps debugging.
192  // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
193 
194  # format expectation and test it
195  $expected = sprintf( $format, $value );
196  $this->assertMagic( $expected, $magic );
197  }
198 
203  private function setParserTS( $ts ) {
204  $this->testParser->Options()->setTimestamp( $ts );
205  $this->testParser->mRevisionTimestamp = $ts;
206  }
207 
213  private function assertMagic( $expected, $magic ) {
214  if ( in_array( $magic, $this->expectedAsInteger ) ) {
215  $expected = (int)$expected;
216  }
217 
218  # Generate a message for the assertion
219  $msg = sprintf( "Magic %s should be <%s:%s>",
220  $magic,
221  $expected,
222  gettype( $expected )
223  );
224 
225  $this->assertSame(
226  $expected,
227  $this->testParser->getVariableValue( $magic ),
228  $msg
229  );
230  }
231 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
MagicVariableTest\assertZeroPadded
assertZeroPadded( $magic, $value)
assertion helper expecting a magic output which is zero padded
Definition: MagicVariableTest.php:163
MagicVariableTest\testCurrentdaytwoIsZeroPadded
testCurrentdaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:100
MagicVariableTest\setParserTS
setParserTS( $ts)
helper to set the parser timestamp and revision timestamp
Definition: MagicVariableTest.php:203
MagicVariableTest\$testParser
Parser $testParser
Definition: MagicVariableTest.php:21
MagicVariableTest\testLocalmonthIsZeroPadded
testLocalmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:127
MagicVariableTest\testRevisionmonthoneIsUnPadded
testRevisionmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:156
MagicVariableTest\provideMonths
static provideMonths()
Definition: MagicVariableTest.php:76
MagicVariableTest\testLocaldayIsUnPadded
testLocaldayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:105
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MagicVariableTest\$expectedAsInteger
$expectedAsInteger
An array of magicword returned as type integer by the parser They are usually returned as a string fo...
Definition: MagicVariableTest.php:30
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
ParserOptions\newFromUserAndLang
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
Definition: ParserOptions.php:992
MagicVariableTest\assertMagic
assertMagic( $expected, $magic)
Assertion helper to test a magic variable output.
Definition: MagicVariableTest.php:213
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
MagicVariableTest\testCurrentmonthIsZeroPadded
testCurrentmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:117
MagicVariableTest\setUp
setUp()
setup a basic parser object
Definition: MagicVariableTest.php:36
MagicVariableTest\testRevisionmonthIsZeroPadded
testRevisionmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:151
MagicVariableTest\testCurrentmonthoneIsUnPadded
testCurrentmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:122
MagicVariableTest\testRevisiondaytwoIsZeroPadded
testRevisiondaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:144
MagicVariableTest\testLocaldaytwoIsZeroPadded
testLocaldaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:110
MagicVariableTest\testCurrentdayIsUnPadded
testCurrentdayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:95
$value
$value
Definition: styleTest.css.php:45
MagicVariableTest
Definition: MagicVariableTest.php:17
MagicVariableTest\provideDays
static provideDays()
Definition: MagicVariableTest.php:83
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1965
MagicVariableTest\assertMagicPadding
assertMagicPadding( $magic, $value, $format)
Main assertion helper for magic variables padding.
Definition: MagicVariableTest.php:178
MagicVariableTest\createProviderUpTo
static createProviderUpTo( $num)
Definition: MagicVariableTest.php:64
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
MagicVariableTest\assertUnPadded
assertUnPadded( $magic, $value)
assertion helper expecting a magic output which is unpadded
Definition: MagicVariableTest.php:168
MagicVariableTest\testRevisiondayIsUnPadded
testRevisiondayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:139
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
MagicVariableTest\testLocalmonthoneIsUnPadded
testLocalmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:132