MediaWiki  1.31.0
MagicVariableTest.php
Go to the documentation of this file.
1 <?php
22  private $testParser = null;
23 
31  private $expectedAsInteger = [
32  'revisionday',
33  'revisionmonth1',
34  ];
35 
37  protected function setUp() {
38  parent::setUp();
39 
40  $contLang = Language::factory( 'en' );
41  $this->setMwGlobals( [
42  'wgLanguageCode' => 'en',
43  'wgContLang' => $contLang,
44  ] );
45 
46  $this->testParser = new Parser();
47  $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
48 
49  # initialize parser output
50  $this->testParser->clearState();
51 
52  # Needs a title to do magic word stuff
53  $title = Title::newFromText( 'Tests' );
54  # Else it needs a db connection just to check if it's a redirect
55  # (when deciding the page language).
56  $title->mRedirect = false;
57 
58  $this->testParser->setTitle( $title );
59  }
60 
65  private static function createProviderUpTo( $num ) {
66  $ret = [];
67  for ( $i = 1; $i <= $num; $i++ ) {
68  $ret[] = [ $i ];
69  }
70 
71  return $ret;
72  }
73 
77  public static function provideMonths() {
78  return self::createProviderUpTo( 12 );
79  }
80 
84  public static function provideDays() {
85  return self::createProviderUpTo( 31 );
86  }
87 
88  # ############## TESTS #############################################
89  # @todo FIXME:
90  # - those got copy pasted, we can probably make them cleaner
91  # - tests are lacking useful messages
92 
93  # day
94 
96  public function testCurrentdayIsUnPadded( $day ) {
97  $this->assertUnPadded( 'currentday', $day );
98  }
99 
101  public function testCurrentdaytwoIsZeroPadded( $day ) {
102  $this->assertZeroPadded( 'currentday2', $day );
103  }
104 
106  public function testLocaldayIsUnPadded( $day ) {
107  $this->assertUnPadded( 'localday', $day );
108  }
109 
111  public function testLocaldaytwoIsZeroPadded( $day ) {
112  $this->assertZeroPadded( 'localday2', $day );
113  }
114 
115  # month
116 
118  public function testCurrentmonthIsZeroPadded( $month ) {
119  $this->assertZeroPadded( 'currentmonth', $month );
120  }
121 
123  public function testCurrentmonthoneIsUnPadded( $month ) {
124  $this->assertUnPadded( 'currentmonth1', $month );
125  }
126 
128  public function testLocalmonthIsZeroPadded( $month ) {
129  $this->assertZeroPadded( 'localmonth', $month );
130  }
131 
133  public function testLocalmonthoneIsUnPadded( $month ) {
134  $this->assertUnPadded( 'localmonth1', $month );
135  }
136 
137  # revision day
138 
140  public function testRevisiondayIsUnPadded( $day ) {
141  $this->assertUnPadded( 'revisionday', $day );
142  }
143 
145  public function testRevisiondaytwoIsZeroPadded( $day ) {
146  $this->assertZeroPadded( 'revisionday2', $day );
147  }
148 
149  # revision month
150 
152  public function testRevisionmonthIsZeroPadded( $month ) {
153  $this->assertZeroPadded( 'revisionmonth', $month );
154  }
155 
157  public function testRevisionmonthoneIsUnPadded( $month ) {
158  $this->assertUnPadded( 'revisionmonth1', $month );
159  }
160 
161  # ############## HELPERS ############################################
162 
164  public function assertZeroPadded( $magic, $value ) {
165  $this->assertMagicPadding( $magic, $value, '%02d' );
166  }
167 
169  public function assertUnPadded( $magic, $value ) {
170  $this->assertMagicPadding( $magic, $value, '%d' );
171  }
172 
179  private function assertMagicPadding( $magic, $value, $format ) {
180  # Initialize parser timestamp as year 2010 at 12h34 56s.
181  # month and day are given by the caller ($value). Month < 12!
182  if ( $value > 12 ) {
183  $month = $value % 12;
184  } else {
185  $month = $value;
186  }
187 
188  $this->setParserTS(
189  sprintf( '2010%02d%02d123456', $month, $value )
190  );
191 
192  # please keep the following commented line of code. It helps debugging.
193  // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
194 
195  # format expectation and test it
196  $expected = sprintf( $format, $value );
197  $this->assertMagic( $expected, $magic );
198  }
199 
204  private function setParserTS( $ts ) {
205  $this->testParser->Options()->setTimestamp( $ts );
206  $this->testParser->mRevisionTimestamp = $ts;
207  }
208 
214  private function assertMagic( $expected, $magic ) {
215  if ( in_array( $magic, $this->expectedAsInteger ) ) {
216  $expected = (int)$expected;
217  }
218 
219  # Generate a message for the assertion
220  $msg = sprintf( "Magic %s should be <%s:%s>",
221  $magic,
222  $expected,
223  gettype( $expected )
224  );
225 
226  $this->assertSame(
227  $expected,
228  $this->testParser->getVariableValue( $magic ),
229  $msg
230  );
231  }
232 }
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:273
MagicVariableTest\assertZeroPadded
assertZeroPadded( $magic, $value)
assertion helper expecting a magic output which is zero padded
Definition: MagicVariableTest.php:164
MagicVariableTest\testCurrentdaytwoIsZeroPadded
testCurrentdaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:101
MagicVariableTest\setParserTS
setParserTS( $ts)
helper to set the parser timestamp and revision timestamp
Definition: MagicVariableTest.php:204
MagicVariableTest\$testParser
Parser $testParser
Definition: MagicVariableTest.php:22
MagicVariableTest\testLocalmonthIsZeroPadded
testLocalmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:128
MagicVariableTest\testRevisionmonthoneIsUnPadded
testRevisionmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:157
MagicVariableTest\provideMonths
static provideMonths()
Definition: MagicVariableTest.php:77
MagicVariableTest\testLocaldayIsUnPadded
testLocaldayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:106
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:31
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
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:214
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:678
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MagicVariableTest\testCurrentmonthIsZeroPadded
testCurrentmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:118
MagicVariableTest\setUp
setUp()
setup a basic parser object
Definition: MagicVariableTest.php:37
MagicVariableTest\testRevisionmonthIsZeroPadded
testRevisionmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:152
MagicVariableTest\testCurrentmonthoneIsUnPadded
testCurrentmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:123
MagicVariableTest\testRevisiondaytwoIsZeroPadded
testRevisiondaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:145
MagicVariableTest\testLocaldaytwoIsZeroPadded
testLocaldaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:111
MagicVariableTest\testCurrentdayIsUnPadded
testCurrentdayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:96
$value
$value
Definition: styleTest.css.php:45
MagicVariableTest
Database Parser::getVariableValue.
Definition: MagicVariableTest.php:18
MagicVariableTest\provideDays
static provideDays()
Definition: MagicVariableTest.php:84
$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:1987
MagicVariableTest\assertMagicPadding
assertMagicPadding( $magic, $value, $format)
Main assertion helper for magic variables padding.
Definition: MagicVariableTest.php:179
MagicVariableTest\createProviderUpTo
static createProviderUpTo( $num)
Definition: MagicVariableTest.php:65
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:169
MagicVariableTest\testRevisiondayIsUnPadded
testRevisiondayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:140
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
MagicVariableTest\testLocalmonthoneIsUnPadded
testLocalmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:133