Use consistent notation for "@todo FIXME". Should update http://svn.wikimedia.org...
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / MagicVariableTest.php
1 <?php
2 /**
3 * This file is intended to test magic variables in the parser
4 * It was inspired by Raymond & Matěj Grabovský commenting about r66200
5 *
6 * As of february 2011, it only tests some revisions and date related
7 * magic variables.
8 *
9 * @author Ashar Voultoiz
10 * @copyright Copyright © 2011, Ashar Voultoiz
11 * @file
12 */
13
14 /** */
15 class MagicVariableTest extends PHPUnit_Framework_TestCase {
16 /** Will contains a parser object*/
17 private $testParser = null;
18
19 /**
20 * An array of magicword returned as type integer by the parser
21 * They are usually returned as a string for i18n since we support
22 * persan numbers for example, but some magic explicitly return
23 * them as integer.
24 * @see MagicVariableTest::assertMagic()
25 */
26 private $expectedAsInteger = array(
27 'revisionday',
28 'revisionmonth1',
29 );
30
31 /** setup a basic parser object */
32 function setUp() {
33 global $wgContLang;
34 $wgContLang = Language::factory( 'en' );
35
36 $this->testParser = new Parser();
37 $this->testParser->Options( new ParserOptions() );
38
39 # initialize parser output
40 $this->testParser->clearState();
41 }
42
43 /** destroy parser (TODO: is it really neded?)*/
44 function tearDown() {
45 unset( $this->testParser );
46 }
47
48 ############### TESTS #############################################
49 # @todo FIXME:
50 # - those got copy pasted, we can probably make them cleaner
51 # - tests are lacking useful messages
52
53 # day
54
55 /** @dataProvider MediaWikiProvide::Days */
56 function testCurrentdayIsUnPadded( $day ) {
57 $this->assertUnPadded( 'currentday', $day );
58 }
59 /** @dataProvider MediaWikiProvide::Days */
60 function testCurrentdaytwoIsZeroPadded( $day ) {
61 $this->assertZeroPadded( 'currentday2', $day );
62 }
63 /** @dataProvider MediaWikiProvide::Days */
64 function testLocaldayIsUnPadded( $day ) {
65 $this->assertUnPadded( 'localday', $day );
66 }
67 /** @dataProvider MediaWikiProvide::Days */
68 function testLocaldaytwoIsZeroPadded( $day ) {
69 $this->assertZeroPadded( 'localday2', $day );
70 }
71
72 # month
73
74 /** @dataProvider MediaWikiProvide::Months */
75 function testCurrentmonthIsZeroPadded( $month ) {
76 $this->assertZeroPadded( 'currentmonth', $month );
77 }
78 /** @dataProvider MediaWikiProvide::Months */
79 function testCurrentmonthoneIsUnPadded( $month ) {
80 $this->assertUnPadded( 'currentmonth1', $month );
81 }
82 /** @dataProvider MediaWikiProvide::Months */
83 function testLocalmonthIsZeroPadded( $month ) {
84 $this->assertZeroPadded( 'localmonth', $month );
85 }
86 /** @dataProvider MediaWikiProvide::Months */
87 function testLocalmonthoneIsUnPadded( $month ) {
88 $this->assertUnPadded( 'localmonth1', $month );
89 }
90
91
92 # revision day
93
94 /** @dataProvider MediaWikiProvide::Days */
95 function testRevisiondayIsUnPadded( $day ) {
96 $this->assertUnPadded( 'revisionday', $day );
97 }
98 /** @dataProvider MediaWikiProvide::Days */
99 function testRevisiondaytwoIsZeroPadded( $day ) {
100 $this->assertZeroPadded( 'revisionday2', $day );
101 }
102
103 # revision month
104
105 /** @dataProvider MediaWikiProvide::Months */
106 function testRevisionmonthIsZeroPadded( $month ) {
107 $this->assertZeroPadded( 'revisionmonth', $month );
108 }
109 /** @dataProvider MediaWikiProvide::Months */
110 function testRevisionmonthoneIsUnPadded( $month ) {
111 $this->assertUnPadded( 'revisionmonth1', $month );
112 }
113
114 ############### HELPERS ############################################
115
116 /** assertion helper expecting a magic output which is zero padded */
117 PUBLIC function assertZeroPadded( $magic, $value ) {
118 $this->assertMagicPadding( $magic, $value, '%02d' );
119 }
120
121 /** assertion helper expecting a magic output which is unpadded */
122 PUBLIC function assertUnPadded( $magic, $value ) {
123 $this->assertMagicPadding( $magic, $value, '%d' );
124 }
125
126 /**
127 * Main assertion helper for magic variables padding
128 * @param $magic string Magic variable name
129 * @param $value mixed Month or day
130 * @param $format string sprintf format for $value
131 */
132 private function assertMagicPadding( $magic, $value, $format ) {
133 # Initialize parser timestamp as year 2010 at 12h34 56s.
134 # month and day are given by the caller ($value). Month < 12!
135 if( $value > 12 ) { $month = $value % 12; }
136 else { $month = $value; }
137
138 $this->setParserTS(
139 sprintf( '2010%02d%02d123456', $month, $value )
140 );
141
142 # please keep the following commented line of code. It helps debugging.
143 //print "\nDEBUG (value $value):" . sprintf( '2010%02d%02d123456', $value, $value ) . "\n";
144
145 # format expectation and test it
146 $expected = sprintf( $format, $value );
147 $this->assertMagic( $expected, $magic );
148 }
149
150 /** helper to set the parser timestamp and revision timestamp */
151 private function setParserTS( $ts ) {
152 $this->testParser->Options()->setTimestamp( $ts );
153 $this->testParser->mRevisionTimestamp = $ts;
154 }
155
156 /**
157 * Assertion helper to test a magic variable output
158 */
159 private function assertMagic( $expected, $magic ) {
160 if( in_array( $magic, $this->expectedAsInteger ) ) {
161 $expected = (int) $expected;
162 }
163
164 # Generate a message for the assertion
165 $msg = sprintf( "Magic %s should be <%s:%s>",
166 $magic,
167 $expected,
168 gettype( $expected )
169 );
170
171 $this->assertSame(
172 $expected,
173 $this->testParser->getVariableValue( $magic ),
174 $msg
175 );
176 }
177 }