wiki.techinc.nl/tests/phpunit/includes/content/CssContentTest.php
Daimona Eaytoy 485e47ff10 tests: Avoid database usage when possible
We would like to remove DB access in non-database PHPUnit tests. As a
first step, avoid database usage in tested code when possible. In
particular:
- In NameTableStoreFactory, avoid domain ID normalization if the
  provided ID is already false.
- In SpecialDoubleRedirects, do not acquire a DB connection until it's
  needed (which is just one place).
- Use editPage() in TitleDefTest instead of a DIY implementation, and
  add `@group Database` accordingly.
- Avoid parsing titles in ContentHandler tests that don't need to parse
  titles. Among the many dependencies of parsing titles is the interwiki
  lookup, which requires DB access.
- Also remove test cases that used the "Gadget" namespace; it doesn't
  exist in core, so these pages were actually in the mainspace.
- Mock the database in CategoriesRdfTest. The only two methods that use
  the database were already being mocked.
- Add `@group Database` to test classes that are intentionally using the
  Database, mainly via getTestUser().

Bug: T155147
Change-Id: I9385fe14cfeb6b7b7378cc322d510034c4ee0711
2023-07-31 00:46:13 +00:00

122 lines
3.3 KiB
PHP

<?php
use MediaWiki\MainConfigNames;
/**
* @group ContentHandler
* @group Database
* ^--- needed, because we do need the database to test link updates
*/
class CssContentTest extends TextContentTest {
protected function setUp(): void {
parent::setUp();
$this->overrideConfigValue(
MainConfigNames::TextModelsToParse,
[
CONTENT_MODEL_CSS,
]
);
}
public function newContent( $text ) {
return new CssContent( $text );
}
// XXX: currently, preSaveTransform is applied to styles. this may change or become optional.
public static function dataPreSaveTransform() {
return [
[ 'hello this is ~~~',
"hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
],
[ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
],
[ " Foo \n ",
" Foo",
],
];
}
/**
* @covers CssContent::getModel
*/
public function testGetModel() {
$content = $this->newContent( 'hello world.' );
$this->assertEquals( CONTENT_MODEL_CSS, $content->getModel() );
}
/**
* @covers CssContent::getContentHandler
*/
public function testGetContentHandler() {
$content = $this->newContent( 'hello world.' );
$this->assertEquals( CONTENT_MODEL_CSS, $content->getContentHandler()->getModelID() );
}
/**
* Redirects aren't supported
*/
public static function provideUpdateRedirect() {
return [
[
'#REDIRECT [[Someplace]]',
'#REDIRECT [[Someplace]]',
],
];
}
/**
* @covers CssContent::getRedirectTarget
* @dataProvider provideGetRedirectTarget
*/
public function testGetRedirectTarget( $title, $text ) {
$this->overrideConfigValues( [
MainConfigNames::Server => '//example.org',
MainConfigNames::ScriptPath => '/w',
MainConfigNames::Script => '/w/index.php',
] );
$content = new CssContent( $text );
$target = $content->getRedirectTarget();
$this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
}
/**
* Keep this in sync with CssContentHandlerTest::provideMakeRedirectContent()
*/
public static function provideGetRedirectTarget() {
return [
[ 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ],
[ 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ],
[
'User:😂/unicode.css',
'/* #REDIRECT */@import url(//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.css&action=raw&ctype=text/css);'
],
# No #REDIRECT comment
[ null, "@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ],
# Wrong domain
[ null, "/* #REDIRECT */@import url(//example.com/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ],
];
// phpcs:enable
}
public static function dataEquals() {
return [
[ new CssContent( 'hallo' ), null, false ],
[ new CssContent( 'hallo' ), new CssContent( 'hallo' ), true ],
[ new CssContent( 'hallo' ), new WikitextContent( 'hallo' ), false ],
[ new CssContent( 'hallo' ), new CssContent( 'HALLO' ), false ],
];
}
/**
* @dataProvider dataEquals
* @covers CssContent::equals
*/
public function testEquals( Content $a, Content $b = null, $equal = false ) {
$this->assertEquals( $equal, $a->equals( $b ) );
}
}