MediaWiki  1.33.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->setContentLang( $contLang );
42 
43  $this->testParser = new Parser();
44  $this->testParser->Options( ParserOptions::newFromUserAndLang( new User, $contLang ) );
45 
46  # initialize parser output
47  $this->testParser->clearState();
48 
49  # Needs a title to do magic word stuff
50  $title = Title::newFromText( 'Tests' );
51  # Else it needs a db connection just to check if it's a redirect
52  # (when deciding the page language).
53  $title->mRedirect = false;
54 
55  $this->testParser->setTitle( $title );
56  }
57 
62  private static function createProviderUpTo( $num ) {
63  $ret = [];
64  for ( $i = 1; $i <= $num; $i++ ) {
65  $ret[] = [ $i ];
66  }
67 
68  return $ret;
69  }
70 
74  public static function provideMonths() {
75  return self::createProviderUpTo( 12 );
76  }
77 
81  public static function provideDays() {
82  return self::createProviderUpTo( 31 );
83  }
84 
85  # ############## TESTS #############################################
86  # @todo FIXME:
87  # - those got copy pasted, we can probably make them cleaner
88  # - tests are lacking useful messages
89 
90  # day
91 
93  public function testCurrentdayIsUnPadded( $day ) {
94  $this->assertUnPadded( 'currentday', $day );
95  }
96 
98  public function testCurrentdaytwoIsZeroPadded( $day ) {
99  $this->assertZeroPadded( 'currentday2', $day );
100  }
101 
103  public function testLocaldayIsUnPadded( $day ) {
104  $this->assertUnPadded( 'localday', $day );
105  }
106 
108  public function testLocaldaytwoIsZeroPadded( $day ) {
109  $this->assertZeroPadded( 'localday2', $day );
110  }
111 
112  # month
113 
115  public function testCurrentmonthIsZeroPadded( $month ) {
116  $this->assertZeroPadded( 'currentmonth', $month );
117  }
118 
120  public function testCurrentmonthoneIsUnPadded( $month ) {
121  $this->assertUnPadded( 'currentmonth1', $month );
122  }
123 
125  public function testLocalmonthIsZeroPadded( $month ) {
126  $this->assertZeroPadded( 'localmonth', $month );
127  }
128 
130  public function testLocalmonthoneIsUnPadded( $month ) {
131  $this->assertUnPadded( 'localmonth1', $month );
132  }
133 
134  # revision day
135 
137  public function testRevisiondayIsUnPadded( $day ) {
138  $this->assertUnPadded( 'revisionday', $day );
139  }
140 
142  public function testRevisiondaytwoIsZeroPadded( $day ) {
143  $this->assertZeroPadded( 'revisionday2', $day );
144  }
145 
146  # revision month
147 
149  public function testRevisionmonthIsZeroPadded( $month ) {
150  $this->assertZeroPadded( 'revisionmonth', $month );
151  }
152 
154  public function testRevisionmonthoneIsUnPadded( $month ) {
155  $this->assertUnPadded( 'revisionmonth1', $month );
156  }
157 
158  # ############## HELPERS ############################################
159 
161  public function assertZeroPadded( $magic, $value ) {
162  $this->assertMagicPadding( $magic, $value, '%02d' );
163  }
164 
166  public function assertUnPadded( $magic, $value ) {
167  $this->assertMagicPadding( $magic, $value, '%d' );
168  }
169 
176  private function assertMagicPadding( $magic, $value, $format ) {
177  # Initialize parser timestamp as year 2010 at 12h34 56s.
178  # month and day are given by the caller ($value). Month < 12!
179  if ( $value > 12 ) {
180  $month = $value % 12;
181  } else {
182  $month = $value;
183  }
184 
185  $this->setParserTS(
186  sprintf( '2010%02d%02d123456', $month, $value )
187  );
188 
189  # please keep the following commented line of code. It helps debugging.
190  // print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
191 
192  # format expectation and test it
193  $expected = sprintf( $format, $value );
194  $this->assertMagic( $expected, $magic );
195  }
196 
201  private function setParserTS( $ts ) {
202  $this->testParser->Options()->setTimestamp( $ts );
203  $this->testParser->mRevisionTimestamp = $ts;
204  }
205 
211  private function assertMagic( $expected, $magic ) {
212  if ( in_array( $magic, $this->expectedAsInteger ) ) {
213  $expected = (int)$expected;
214  }
215 
216  # Generate a message for the assertion
217  $msg = sprintf( "Magic %s should be <%s:%s>",
218  $magic,
219  $expected,
220  gettype( $expected )
221  );
222 
223  $this->assertSame(
224  $expected,
225  $this->testParser->getVariableValue( $magic ),
226  $msg
227  );
228  }
229 }
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:306
MagicVariableTest\assertZeroPadded
assertZeroPadded( $magic, $value)
assertion helper expecting a magic output which is zero padded
Definition: MagicVariableTest.php:161
MagicVariableTest\testCurrentdaytwoIsZeroPadded
testCurrentdaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:98
MagicVariableTest\setParserTS
setParserTS( $ts)
helper to set the parser timestamp and revision timestamp
Definition: MagicVariableTest.php:201
MagicVariableTest\$testParser
Parser $testParser
Definition: MagicVariableTest.php:22
MagicVariableTest\testLocalmonthIsZeroPadded
testLocalmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:125
MagicVariableTest\testRevisionmonthoneIsUnPadded
testRevisionmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:154
MagicVariableTest\provideMonths
static provideMonths()
Definition: MagicVariableTest.php:74
MagicVariableTest\testLocaldayIsUnPadded
testLocaldayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:103
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:925
ParserOptions\newFromUserAndLang
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
Definition: ParserOptions.php:1031
MagicVariableTest\assertMagic
assertMagic( $expected, $magic)
Assertion helper to test a magic variable output.
Definition: MagicVariableTest.php:211
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MagicVariableTest\testCurrentmonthIsZeroPadded
testCurrentmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:115
MagicVariableTest\setUp
setUp()
setup a basic parser object
Definition: MagicVariableTest.php:37
MagicVariableTest\testRevisionmonthIsZeroPadded
testRevisionmonthIsZeroPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:149
MagicVariableTest\testCurrentmonthoneIsUnPadded
testCurrentmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:120
MagicVariableTest\testRevisiondaytwoIsZeroPadded
testRevisiondaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:142
MediaWikiTestCase\setContentLang
setContentLang( $lang)
Definition: MediaWikiTestCase.php:1066
MagicVariableTest\testLocaldaytwoIsZeroPadded
testLocaldaytwoIsZeroPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:108
MagicVariableTest\testCurrentdayIsUnPadded
testCurrentdayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:93
$value
$value
Definition: styleTest.css.php:49
MagicVariableTest
Database Parser::getVariableValue.
Definition: MagicVariableTest.php:18
MagicVariableTest\provideDays
static provideDays()
Definition: MagicVariableTest.php:81
$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:1985
MagicVariableTest\assertMagicPadding
assertMagicPadding( $magic, $value, $format)
Main assertion helper for magic variables padding.
Definition: MagicVariableTest.php:176
MagicVariableTest\createProviderUpTo
static createProviderUpTo( $num)
Definition: MagicVariableTest.php:62
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:215
MagicVariableTest\assertUnPadded
assertUnPadded( $magic, $value)
assertion helper expecting a magic output which is unpadded
Definition: MagicVariableTest.php:166
MagicVariableTest\testRevisiondayIsUnPadded
testRevisiondayIsUnPadded( $day)
@dataProvider provideDays
Definition: MagicVariableTest.php:137
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
MagicVariableTest\testLocalmonthoneIsUnPadded
testLocalmonthoneIsUnPadded( $month)
@dataProvider provideMonths
Definition: MagicVariableTest.php:130