From e02f9b170655732b1051a38fd0cf31ceaabce0c8 Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 18 Apr 2012 15:02:21 +0200 Subject: [PATCH] first test cases. found and fixed a bug in Title::isCssJsSubpage --- includes/Title.php | 5 +- tests/phpunit/includes/ContentHandlerTest.php | 33 ++++++ tests/phpunit/includes/TitleMethodsTest.php | 100 +++++++++++++++++- 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/includes/ContentHandlerTest.php diff --git a/includes/Title.php b/includes/Title.php index 6152f1cf1e..2145590c69 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -974,6 +974,8 @@ class Title { * This is generally true for pages in the MediaWiki namespace having CONTENT_MODEL_CSS * or CONTENT_MODEL_JAVASCRIPT. * + * This method does *not* return true for per-user JS/CSS. Use isCssJsSubpage() for that! + * * Note that this method should not return true for pages that contain and show "inactive" CSS or JS. * * @return Bool @@ -997,7 +999,8 @@ class Title { */ public function isCssJsSubpage() { return ( NS_USER == $this->mNamespace && $this->isSubpage() - && $this->isCssOrJsPage() ); + && ( $this->hasContentModel( CONTENT_MODEL_CSS ) + || $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT ) ) ); } /** diff --git a/tests/phpunit/includes/ContentHandlerTest.php b/tests/phpunit/includes/ContentHandlerTest.php new file mode 100644 index 0000000000..3bd9fb9838 --- /dev/null +++ b/tests/phpunit/includes/ContentHandlerTest.php @@ -0,0 +1,33 @@ +assertEquals( $expectedModelName, ContentHandler::getDefaultModelFor( $title ) ); + } + +} diff --git a/tests/phpunit/includes/TitleMethodsTest.php b/tests/phpunit/includes/TitleMethodsTest.php index 2f1103e8cb..11c11ff2a5 100644 --- a/tests/phpunit/includes/TitleMethodsTest.php +++ b/tests/phpunit/includes/TitleMethodsTest.php @@ -21,8 +21,8 @@ class TitleMethodsTest extends MediaWikiTestCase { $titleA = Title::newFromText( $titleA ); $titleB = Title::newFromText( $titleB ); - $this->assertEquals( $titleA->equals( $titleB ), $expectedBool ); - $this->assertEquals( $titleB->equals( $titleA ), $expectedBool ); + $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) ); + $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) ); } public function dataInNamespace() { @@ -43,7 +43,7 @@ class TitleMethodsTest extends MediaWikiTestCase { */ public function testInNamespace( $title, $ns, $expectedBool ) { $title = Title::newFromText( $title ); - $this->assertEquals( $title->inNamespace( $ns ), $expectedBool ); + $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) ); } public function testInNamespaces() { @@ -72,7 +72,99 @@ class TitleMethodsTest extends MediaWikiTestCase { */ public function testHasSubjectNamespace( $title, $ns, $expectedBool ) { $title = Title::newFromText( $title ); - $this->assertEquals( $title->hasSubjectNamespace( $ns ), $expectedBool ); + $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) ); } + + public function dataGetContentModelName() { + return array( + array( 'Foo', CONTENT_MODEL_WIKITEXT ), + array( 'Foo.js', CONTENT_MODEL_WIKITEXT ), + array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT ), + array( 'User:Foo', CONTENT_MODEL_WIKITEXT ), + array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ), + array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ), + array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ), + array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ), + array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ), + array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ), + array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ), + array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ), + array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ), + array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ), + array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ), + ); + } + + /** + * @dataProvider dataGetContentModelName + */ + public function testGetContentModelName( $title, $expectedModelName ) { + $title = Title::newFromText( $title ); + $this->assertEquals( $expectedModelName, $title->getContentModelName() ); + } + + /** + * @dataProvider dataGetContentModelName + */ + public function testHasContentModel( $title, $expectedModelName ) { + $title = Title::newFromText( $title ); + $this->assertTrue( $title->hasContentModel( $expectedModelName ) ); + } + + public function dataIsCssOrJsPage() { + return array( + array( 'Foo', false ), + array( 'Foo.js', false ), + array( 'Foo/bar.js', false ), + array( 'User:Foo', false ), + array( 'User:Foo.js', false ), + array( 'User:Foo/bar.js', false ), + array( 'User:Foo/bar.css', false ), + array( 'User talk:Foo/bar.css', false ), + array( 'User:Foo/bar.js.xxx', false ), + array( 'User:Foo/bar.xxx', false ), + array( 'MediaWiki:Foo.js', true ), + array( 'MediaWiki:Foo.css', true ), + array( 'MediaWiki:Foo.JS', false ), + array( 'MediaWiki:Foo.CSS', false ), + array( 'MediaWiki:Foo.css.xxx', false ), + ); + } + + /** + * @dataProvider dataIsCssOrJsPage + */ + public function testIsCssOrJsPage( $title, $expectedBool ) { + $title = Title::newFromText( $title ); + $this->assertEquals( $expectedBool, $title->isCssOrJsPage() ); + } + + + public function dataIsCssJsSubpage() { + return array( + array( 'Foo', false ), + array( 'Foo.js', false ), + array( 'Foo/bar.js', false ), + array( 'User:Foo', false ), + array( 'User:Foo.js', false ), + array( 'User:Foo/bar.js', true ), + array( 'User:Foo/bar.css', true ), + array( 'User talk:Foo/bar.css', false ), + array( 'User:Foo/bar.js.xxx', false ), + array( 'User:Foo/bar.xxx', false ), + array( 'MediaWiki:Foo.js', false ), + array( 'User:Foo/bar.JS', false ), + array( 'User:Foo/bar.CSS', false ), + ); + } + + /** + * @dataProvider dataIsCssJsSubpage + */ + public function testIsCssJsSubpage( $title, $expectedBool ) { + $title = Title::newFromText( $title ); + $this->assertEquals( $expectedBool, $title->isCssJsSubpage() ); + } + } -- 2.20.1