From 545f1d3a731016b26aa1bb018b88516bbff6b66b Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Fri, 5 Sep 2014 10:52:31 -0700 Subject: [PATCH] TitleTest: Break secure and split test into two tests with providers Change-Id: Ib33819c3e8828c154951ab76db2a283e3a549ea0 --- tests/phpunit/includes/TitleTest.php | 205 ++++++++++++++------------- 1 file changed, 110 insertions(+), 95 deletions(-) diff --git a/tests/phpunit/includes/TitleTest.php b/tests/phpunit/includes/TitleTest.php index b2b0d34bc0..97f61466fb 100644 --- a/tests/phpunit/includes/TitleTest.php +++ b/tests/phpunit/includes/TitleTest.php @@ -39,84 +39,61 @@ class TitleTest extends MediaWikiTestCase { } } - /** - * See also mediawiki.Title.test.js - * @covers Title::secureAndSplit - * @todo This method should be split into 2 separate tests each with a provider - * @note This mainly tests MediaWikiTitleCodec::parseTitle(). - */ - public function testSecureAndSplit() { - $this->setMwGlobals( array( - 'wgLocalInterwikis' => array( 'localtestiw' ), - 'wgHooks' => array( - 'InterwikiLoadPrefix' => array( - function ( $prefix, &$data ) { - if ( $prefix === 'localtestiw' ) { - $data = array( 'iw_url' => 'localtestiw' ); - } elseif ( $prefix === 'remotetestiw' ) { - $data = array( 'iw_url' => 'remotetestiw' ); - } - return false; - } - ) - ) - )); - // Valid - foreach ( array( - 'Sandbox', - 'A "B"', - 'A \'B\'', - '.com', - '~', - '#', - '"', - '\'', - 'Talk:Sandbox', - 'Talk:Foo:Sandbox', - 'File:Example.svg', - 'File_talk:Example.svg', - 'Foo/.../Sandbox', - 'Sandbox/...', - 'A~~', - ':A', + public function provideValidSecureAndSplit() { + return array( + array( 'Sandbox' ), + array( 'A "B"' ), + array( 'A \'B\'' ), + array( '.com' ), + array( '~' ), + array( '#' ), + array( '"' ), + array( '\'' ), + array( 'Talk:Sandbox' ), + array( 'Talk:Foo:Sandbox' ), + array( 'File:Example.svg' ), + array( 'File_talk:Example.svg' ), + array( 'Foo/.../Sandbox' ), + array( 'Sandbox/...' ), + array( 'A~~' ), + array( ':A' ), // Length is 256 total, but only title part matters - 'Category:' . str_repeat( 'x', 248 ), - str_repeat( 'x', 252 ), + array( 'Category:' . str_repeat( 'x', 248 ) ), + array( str_repeat( 'x', 252 ) ), // interwiki prefix - 'localtestiw: #anchor', - 'localtestiw:', - 'localtestiw:foo', - 'localtestiw: foo # anchor', - 'localtestiw: Talk: Sandbox # anchor', - 'remotetestiw:', - 'remotetestiw: Talk: # anchor', - 'remotetestiw: #bar', - 'remotetestiw: Talk:', - 'remotetestiw: Talk: Foo', - 'localtestiw:remotetestiw:', - 'localtestiw:remotetestiw:foo' - ) as $text ) { - $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" ); - } + array( 'localtestiw: #anchor' ), + array( 'localtestiw:' ), + array( 'localtestiw:foo' ), + array( 'localtestiw: foo # anchor' ), + array( 'localtestiw: Talk: Sandbox # anchor' ), + array( 'remotetestiw:' ), + array( 'remotetestiw: Talk: # anchor' ), + array( 'remotetestiw: #bar' ), + array( 'remotetestiw: Talk:' ), + array( 'remotetestiw: Talk: Foo' ), + array( 'localtestiw:remotetestiw:' ), + array( 'localtestiw:remotetestiw:foo' ) + ); + } - // Invalid - foreach ( array( - '', - ':', - '__ __', - ' __ ', + public function provideInvalidSecureAndSplit() { + return array( + array( '' ), + array( ':' ), + array( '__ __' ), + array( ' __ ' ), // Bad characters forbidden regardless of wgLegalTitleChars - 'A [ B', - 'A ] B', - 'A { B', - 'A } B', - 'A < B', - 'A > B', - 'A | B', + array( 'A [ B' ), + array( 'A ] B' ), + array( 'A { B' ), + array( 'A } B' ), + array( 'A < B' ), + array( 'A > B' ), + array( 'A | B' ), // URL encoding - 'A%20B', - 'A%23B', - 'A%2523B', + array( 'A%20B' ), + array( 'A%23B' ), + array( 'A%2523B' ), // XML/HTML character entity references // Note: Commented out because they are not marked invalid by the PHP test as // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first. @@ -124,32 +101,70 @@ class TitleTest extends MediaWikiTestCase { //'A é B', //'A é B', // Subject of NS_TALK does not roundtrip to NS_MAIN - 'Talk:File:Example.svg', + array( 'Talk:File:Example.svg' ), // Directory navigation - '.', - '..', - './Sandbox', - '../Sandbox', - 'Foo/./Sandbox', - 'Foo/../Sandbox', - 'Sandbox/.', - 'Sandbox/..', + array( '.' ), + array( '..' ), + array( './Sandbox' ), + array( '../Sandbox' ), + array( 'Foo/./Sandbox' ), + array( 'Foo/../Sandbox' ), + array( 'Sandbox/.' ), + array( 'Sandbox/..' ), // Tilde - 'A ~~~ Name', - 'A ~~~~ Signature', - 'A ~~~~~ Timestamp', - str_repeat( 'x', 256 ), + array( 'A ~~~ Name' ), + array( 'A ~~~~ Signature' ), + array( 'A ~~~~~ Timestamp' ), + array( str_repeat( 'x', 256 ) ), // Namespace prefix without actual title - 'Talk:', - 'Talk:#', - 'Category: ', - 'Category: #bar', + array( 'Talk:' ), + array( 'Talk:#' ), + array( 'Category: ' ), + array( 'Category: #bar' ), // interwiki prefix - 'localtestiw: Talk: # anchor', - 'localtestiw: Talk:' - ) as $text ) { - $this->assertNull( Title::newFromText( $text ), "Invalid: $text" ); - } + array( 'localtestiw: Talk: # anchor' ), + array( 'localtestiw: Talk:' ) + ); + } + + private function secureAndSplitGlobals() { + $this->setMwGlobals( array( + 'wgLocalInterwikis' => array( 'localtestiw' ), + 'wgHooks' => array( + 'InterwikiLoadPrefix' => array( + function ( $prefix, &$data ) { + if ( $prefix === 'localtestiw' ) { + $data = array( 'iw_url' => 'localtestiw' ); + } elseif ( $prefix === 'remotetestiw' ) { + $data = array( 'iw_url' => 'remotetestiw' ); + } + return false; + } + ) + ) + )); + } + + /** + * See also mediawiki.Title.test.js + * @covers Title::secureAndSplit + * @dataProvider provideValidSecureAndSplit + * @note This mainly tests MediaWikiTitleCodec::parseTitle(). + */ + public function testSecureAndSplitValid( $text ) { + $this->secureAndSplitGlobals(); + $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" ); + } + + /** + * See also mediawiki.Title.test.js + * @covers Title::secureAndSplit + * @dataProvider provideInvalidSecureAndSplit + * @note This mainly tests MediaWikiTitleCodec::parseTitle(). + */ + public function testSecureAndSplitInvalid( $text ) { + $this->secureAndSplitGlobals(); + $this->assertNull( Title::newFromText( $text ), "Invalid: $text" ); } public static function provideConvertByteClassToUnicodeClass() { -- 2.20.1