r81414 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81413‎ | r81414 | r81415 >
Date:21:04, 2 February 2011
Author:hashar
Status:ok (Comments)
Tags:
Comment:
Test suite for revisions and date related magic variables.

Tests should be carefully reviewed, please note there is are exceptions
for 'revisionday' and 'revisionmonth1' magics which are hardcoded in
the parser to return an integer.

Follow up r66200

TESTS:

$ php phpunit.php -c suite.xml --filter MagicVariable --testdox
PHPUnit 3.5.10 by Sebastian Bergmann.

MagicVariable
[x] Currentday is un padded
[x] Currentdaytwo is zero padded
[x] Localday is un padded
[x] Localdaytwo is zero padded
[x] Currentmonth is zero padded
[x] Currentmonthone is un padded
[x] Localmonth is zero padded
[x] Localmonthone is un padded
[x] Revisionday is un padded
[x] Revisiondaytwo is zero padded
[x] Revisionmonth is zero padded
[x] Revisionmonthone is un padded
$
Modified paths:
  • /trunk/phase3/tests/phpunit/includes/parser/MagicVariableTest.php (added) (history)

Diff [purge]

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

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r66200(bug 23426) The {{REVISIONMONTH}} variable is now zero-padded and added new v...mgrabovsky16:24, 11 May 2010

Comments

#Comment by Hashar (talk | contribs)   21:06, 2 February 2011

Not sure if we want this in 1.17 or not. Might help with the new magic variables added by r66200 (fixme as of now).

#Comment by 😂 (talk | contribs)   21:07, 2 February 2011

I killed the phpunit suite in 1.17.

#Comment by Catrope (talk | contribs)   21:07, 2 February 2011

The phpunit framework isn't in 1.17, so this can't go in.

#Comment by Hashar (talk | contribs)   21:09, 2 February 2011

Ok :)

#Comment by Krinkle (talk | contribs)   13:16, 7 May 2011

Removing 1.18 (which is the tag to MFT to branches/REL1_18). See CodeReview Tags

Status & tagging log