getUser(); $status = $page->doEditContent( $content, 'Test for TemplateStyles', 0, false, $user ); if ( !$status->isOk() ) { $this->fail( "Failed to create $title: " . $status->getWikiText( false, false, 'en' ) ); } } public function addDBDataOnce() { $this->addPage( 'wikitext', '.foo { color: red; }', CONTENT_MODEL_WIKITEXT ); $this->addPage( 'nonsanitized.css', '.foo { color: red; }', CONTENT_MODEL_CSS ); $this->addPage( 'styles1.css', '.foo { color: blue; }', 'sanitized-css' ); $this->addPage( 'styles2.css', '.bar { color: green; }', 'sanitized-css' ); } /** * @dataProvider provideOnRegistration * @param array $textModelsToParse * @param bool $autoParseContent * @param array $expect */ public function testOnRegistration( $textModelsToParse, $autoParseContent, $expect ) { $this->setMwGlobals( [ 'wgTextModelsToParse' => $textModelsToParse, 'wgTemplateStylesAutoParseContent' => $autoParseContent, ] ); global $wgTextModelsToParse; TemplateStylesHooks::onRegistration(); $this->assertSame( $expect, $wgTextModelsToParse ); } public static function provideOnRegistration() { return [ [ [ CONTENT_MODEL_WIKITEXT ], true, [ CONTENT_MODEL_WIKITEXT ] ], [ [ CONTENT_MODEL_WIKITEXT, CONTENT_MODEL_CSS ], true, [ CONTENT_MODEL_WIKITEXT, CONTENT_MODEL_CSS, 'sanitized-css' ], ], [ [ CONTENT_MODEL_WIKITEXT, CONTENT_MODEL_CSS ], false, [ CONTENT_MODEL_WIKITEXT, CONTENT_MODEL_CSS ], ], ]; } /** * @dataProvider provideOnParserAfterTidy */ public function testOnParserAfterTidy( $text, $expect ) { $p = new Parser(); TemplateStylesHooks::onParserAfterTidy( $p, $text ); $this->assertSame( $expect, $text ); } public static function provideOnParserAfterTidy() { return [ [ "", "", ], [ "", "", ], [ "", "", ], [ "", "", ], [ "", "", ], [ "", "", ], ]; } /** * @dataProvider provideOnContentHandlerDefaultModelFor */ public function testOnContentHandlerDefaultModelFor( $ns, $title, $expect ) { $this->setMwGlobals( [ 'wgTemplateStylesNamespaces' => [ 10 => true, 2 => false, 3000 => true, 3002 => true ], 'wgNamespacesWithSubpages' => [ 10 => true, 2 => true, 3000 => true, 3002 => false ], ] ); $model = 'unchanged'; $ret = TemplateStylesHooks::onContentHandlerDefaultModelFor( Title::makeTitle( $ns, $title ), $model ); $this->assertSame( !$expect, $ret ); $this->assertSame( $expect ? 'sanitized-css' : 'unchanged', $model ); } public static function provideOnContentHandlerDefaultModelFor() { return [ [ 10, 'Test/test.css', true ], [ 10, 'Test.css', false ], [ 10, 'Test/test.xss', false ], [ 10, 'Test/test.CSS', false ], [ 3000, 'Test/test.css', true ], [ 3002, 'Test/test.css', false ], [ 2, 'Test/test.css', false ], ]; } /** * Unfortunately we can't just use a parserTests.txt file because our * tag's output depends on the revision IDs of the input pages. * @dataProvider provideTag */ public function testTag( $popt, $wikitext, $expect ) { global $wgParserConf; $this->setMwGlobals( [ 'wgScriptPath' => '', 'wgScript' => '/index.php', 'wgArticlePath' => '/wiki/$1', ] ); $oldCurrentRevisionCallback = $popt->setCurrentRevisionCallback( function ( $title, $parser = false ) use ( &$oldCurrentRevisionCallback ) { if ( $title->getPrefixedText() === 'Template:Test replacement' ) { $user = RequestContext::getMain()->getUser(); return new Revision( [ 'page' => $title->getArticleID(), 'user_text' => $user->getName(), 'user' => $user->getId(), 'parent_id' => $title->getLatestRevId(), 'title' => $title, 'content' => new TemplateStylesContent( '.baz { color:orange; bogus:bogus; }' ) ] ); } return call_user_func( $oldCurrentRevisionCallback, $title, $parser ); } ); $class = $wgParserConf['class']; $parser = new $class( $wgParserConf ); $parser->firstCallInit(); if ( !isset( $parser->mTagHooks['templatestyles'] ) ) { $this->markTestSkipped( 'templatestyles tag hook is not in the parser' ); } $out = $parser->parse( $wikitext, Title::newFromText( 'Test' ), $popt ); $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle $this->assertEquals( $expect, $out->getText() ); } public static function provideTag() { $popt = ParserOptions::newFromContext( RequestContext::getMain() ); $popt->setWrapOutputClass( 'templatestyles-test' ); $popt2 = ParserOptions::newFromContext( RequestContext::getMain() ); $popt2->setWrapOutputClass( false ); return [ 'Tag without src' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

TemplateStyles' src attribute must not be empty.\n

", // @codingStandardsIgnoreEnd ], 'Tag with invalid src' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

Invalid title for TemplateStyles' src attribute.\n

", // @codingStandardsIgnoreEnd ], 'Tag with valid but nonexistent title' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

Page Template:ThisDoes'''Not'''Exist has no content.\n

", // @codingStandardsIgnoreEnd ], 'Tag with valid but nonexistent title, main namespace' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

Page ThisDoes'''Not'''Exist has no content.\n

", // @codingStandardsIgnoreEnd ], 'Tag with wikitext page' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

Page Template:TemplateStyles test/wikitext must have content model \"Sanitized CSS\" for TemplateStyles (current model is \"wikitext\").\n

", // @codingStandardsIgnoreEnd ], 'Tag with CSS (not sanitized-css) page' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

Page Template:TemplateStyles test/nonsanitized.css must have content model \"Sanitized CSS\" for TemplateStyles (current model is \"CSS\").\n

", // @codingStandardsIgnoreEnd ], 'Working tag' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

\n

", // @codingStandardsIgnoreEnd ], 'Replaced content (which includes sanitization errors)' => [ $popt, '', // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong "

\n

", // @codingStandardsIgnoreEnd ], 'Still prefixed despite no wrapper' => [ $popt2, '', "

\n

", ], 'Not yet deduplicated tags' => [ $popt, trim( ' ' ), trim( '

' ), ], ]; } }