* Port tests from t/inc/
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 3 Feb 2010 17:35:59 +0000 (17:35 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 3 Feb 2010 17:35:59 +0000 (17:35 +0000)
* Added new tests to XmlTest

tests/LicensesTest.php [new file with mode: 0644]
tests/SanitizerTest.php [new file with mode: 0644]
tests/TimeAdjustTest.php [new file with mode: 0644]
tests/TitleTest.php [new file with mode: 0644]
tests/XmlTest.php [new file with mode: 0644]

diff --git a/tests/LicensesTest.php b/tests/LicensesTest.php
new file mode 100644 (file)
index 0000000..c5357f8
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @group Broken
+ */
+class LicensesTest extends PHPUnit_Framework_TestCase {
+
+       function testLicenses() {
+               $str = "
+* Free licenses:
+** GFLD|Debian disagrees
+";
+
+               $lc = new Licenses( $str );
+               $this->assertTrue( is_a( $lc, 'Licenses' ), 'Correct class' );
+       }
+}
\ No newline at end of file
diff --git a/tests/SanitizerTest.php b/tests/SanitizerTest.php
new file mode 100644 (file)
index 0000000..6fb9d5a
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+
+global $IP;
+require_once( "$IP/includes/Sanitizer.php" );
+
+class SanitizerTest extends PHPUnit_Framework_TestCase {
+
+       function testDecodeNamedEntities() {
+               $this->assertEquals(
+                       "\xc3\xa9cole",
+                       Sanitizer::decodeCharReferences( '&eacute;cole' ),
+                       'decode named entities'
+               );
+       }
+
+       function testDecodeNumericEntities() {
+               $this->assertEquals(
+                       "\xc4\x88io bonas dans l'\xc3\xa9cole!",
+                       Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&#233;cole!" ),
+                       'decode numeric entities'
+               );
+       }
+
+       function testDecodeMixedEntities() {
+               $this->assertEquals(
+                       "\xc4\x88io bonas dans l'\xc3\xa9cole!",
+                       Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&eacute;cole!" ),
+                       'decode mixed numeric/named entities'
+               );
+       }
+
+       function testDecodeMixedComplexEntities() {
+               $this->assertEquals(
+                       "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas &#x108;io dans l'&eacute;cole)",
+                       Sanitizer::decodeCharReferences(
+                               "&#x108;io bonas dans l'&eacute;cole! (mais pas &amp;#x108;io dans l'&#38;eacute;cole)"
+                       ),
+                       'decode mixed complex entities'
+               );
+       }
+
+       function testInvalidAmpersand() {
+               $this->assertEquals(
+                       'a & b',
+                       Sanitizer::decodeCharReferences( 'a & b' ),
+                       'Invalid ampersand'
+               );
+       }
+
+       function testInvalidEntities() {
+               $this->assertEquals(
+                       '&foo;',
+                       Sanitizer::decodeCharReferences( '&foo;' ),
+                       'Invalid named entity'
+               );
+       }
+
+       function testInvalidNumberedEntities() {
+               $this->assertEquals( UTF8_REPLACEMENT, Sanitizer::decodeCharReferences( "&#88888888888888;" ), 'Invalid numbered entity' );
+       }
+
+       function testSelfClosingTag() {
+               $GLOBALS['wgUseTidy'] = false;
+               $this->assertEquals(
+                       '<div>Hello world</div>',
+                       Sanitizer::removeHTMLtags( '<div>Hello world</div />' ),
+                       'Self-closing closing div'
+               );
+       }
+}
+
diff --git a/tests/TimeAdjustTest.php b/tests/TimeAdjustTest.php
new file mode 100644 (file)
index 0000000..bbd697b
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+class TimeAdjustTest extends PHPUnit_Framework_TestCase {
+
+       public function setUp() {
+               $this->iniSet( 'precision', 15 );
+       }
+
+       # Test offset usage for a given language::userAdjust
+       function testUserAdjust() {
+               global $wgLocalTZoffset, $wgContLang, $wgUser;
+
+               $wgContLang = $en = Language::factory( 'en' );
+
+               # Collection of parameters for Language_t_Offset.
+               # Format: date to be formatted, localTZoffset value, expected date
+               $userAdjust_tests = array(
+                       array( 20061231235959,   0, 20061231235959 ),
+                       array( 20061231235959,   5, 20070101000459 ),
+                       array( 20061231235959,  15, 20070101001459 ),
+                       array( 20061231235959,  60, 20070101005959 ),
+                       array( 20061231235959,  90, 20070101012959 ),
+                       array( 20061231235959, 120, 20070101015959 ),
+                       array( 20061231235959, 540, 20070101085959 ),
+                       array( 20061231235959,  -5, 20061231235459 ),
+                       array( 20061231235959, -30, 20061231232959 ),
+                       array( 20061231235959, -60, 20061231225959 ),
+               );
+
+               foreach( $userAdjust_tests as $data ) {
+                       $wgLocalTZoffset = $data[1];
+
+                       $this->assertEquals(
+                               strval( $data[2] ),
+                               strval( $en->userAdjust( $data[0], '' ) ),
+                               "User adjust {$data[0]} by {$data[1]} minutes should give {$data[2]}"
+                       );
+               }
+       }
+}
diff --git a/tests/TitleTest.php b/tests/TitleTest.php
new file mode 100644 (file)
index 0000000..5b42c1c
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+class TitleTest extends PHPUnit_Framework_TestCase {
+
+       function testLegalChars() {
+               $titlechars = Title::legalChars();
+
+               foreach ( range( 1, 255 ) as $num ) {
+                       $chr = chr( $num );
+                       if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
+                               $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
+                       } else {
+                               $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
+                       }
+               }
+       }
+}
diff --git a/tests/XmlTest.php b/tests/XmlTest.php
new file mode 100644 (file)
index 0000000..330e60c
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+
+class XmlTest extends PHPUnit_Framework_TestCase {
+
+       function testElementOpen() {
+               $this->assertEquals(
+                       '<element>',
+                       Xml::element( 'element', null, null ),
+                       'Opening element with no attributes'
+               );
+       }
+
+       function testElementEmpty() {
+               $this->assertEquals(
+                       '<element />',
+                       Xml::element( 'element', null, '' ),
+                       'Terminated empty element'
+               );
+       }
+
+       function testElementEscaping() {
+               $this->assertEquals(
+                       '<element>hello &lt;there&gt; you &amp; you</element>',
+                       Xml::element( 'element', null, 'hello <there> you & you' ),
+                       'Element with no attributes and content that needs escaping'
+               );
+       }
+
+       function testElementAttributes() {
+               $this->assertEquals(
+                       '<element key="value" <>="&lt;&gt;">',
+                       Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ),
+                       'Element attributes, keys are not escaped'
+               );
+       }
+
+       function testOpenElement() {
+               $this->assertEquals(
+                       '<element k="v">',
+                       Xml::openElement( 'element', array( 'k' => 'v' ) ),
+                       'openElement() shortcut'
+               );
+       }
+
+       function testCloseElement() {
+               $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' );
+       }
+
+       #
+       # textarea
+       #
+       function testTextareaNoContent() {
+               $this->assertEquals(
+                       '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
+                       Xml::textarea( 'name', '' ),
+                       'textarea() with not content'
+               );
+       }
+
+       function testTextareaAttribs() {
+               $this->assertEquals(
+                       '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
+                       Xml::textarea( 'name', '<txt>', 20, 10 ),
+                       'textarea() with custom attribs'
+               );
+       }
+
+       #
+       # JS
+       #
+       function testEscapeJsStringSpecialChars() {
+               $this->assertEquals(
+                       '\\\\\r\n',
+                       Xml::escapeJsString( "\\\r\n" ),
+                       'escapeJsString() with special characters'
+               );
+       }
+
+       function testEncodeJsVarBoolean() {
+               $this->assertEquals(
+                       'true',
+                       Xml::encodeJsVar( true ),
+                       'encodeJsVar() with boolean'
+               );
+       }
+
+       function testEncodeJsVarNull() {
+               $this->assertEquals(
+                       'null',
+                       Xml::encodeJsVar( null ),
+                       'encodeJsVar() with null'
+               );
+       }
+
+       function testEncodeJsVarArray() {
+               $this->assertEquals(
+                       '["a", 1]',
+                       Xml::encodeJsVar( array( 'a', 1 ) ),
+                       'encodeJsVar() with array'
+               );
+               $this->assertEquals(
+                       '{"a": "a", "b": 1}',
+                       Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
+                       'encodeJsVar() with associative array'
+               );
+       }
+
+       function testEncodeJsVarObject() {
+               $this->assertEquals(
+                       '{"a": "a", "b": 1}',
+                       Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
+                       'encodeJsVar() with object'
+               );
+       }
+}