Debian Codegraph @

tests/phpunit/includes/content/CssContentTest.php

raw symbols SCIP
1 <?php
2 declare( strict_types = 1 );
3
4 namespace MediaWiki\Tests\Content;
5
6 use MediaWiki\Content\Content;
7 use MediaWiki\Content\CssContent;
8 use MediaWiki\Content\WikitextContent;
9 use MediaWiki\MainConfigNames;
10
11 /**
12 * @group ContentHandler
13 * @group Database
14 * ^--- needed, because we do need the database to test link updates
15 * @covers \MediaWiki\Content\CssContent
16 */
17 class CssContentTest extends TextContentTest {
18 use ContentSerializationTestTrait;
19
20 protected function setUp(): void {
21 parent::setUp();
22
23 $this->overrideConfigValue(
24 MainConfigNames::TextModelsToParse,
25 [
26 CONTENT_MODEL_CSS,
27 ]
28 );
29 }
30
31 public function newContent( $text ) {
32 return new CssContent( $text );
33 }
34
35 // XXX: currently, preSaveTransform is applied to styles. this may change or become optional.
36 public static function dataPreSaveTransform() {
37 return [
38 [ 'hello this is ~~~',
39 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
40 ],
41 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
42 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
43 ],
44 [ " Foo \n ",
45 " Foo",
46 ],
47 ];
48 }
49
50 public function testGetModel() {
51 $content = $this->newContent( 'hello world.' );
52
53 $this->assertEquals( CONTENT_MODEL_CSS, $content->getModel() );
54 }
55
56 public function testGetContentHandler() {
57 $content = $this->newContent( 'hello world.' );
58
59 $this->assertEquals( CONTENT_MODEL_CSS, $content->getContentHandler()->getModelID() );
60 }
61
62 /**
63 * Redirects aren't supported
64 */
65 public static function provideUpdateRedirect() {
66 return [
67 [
68 '#REDIRECT [[Someplace]]',
69 '#REDIRECT [[Someplace]]',
70 ],
71 ];
72 }
73
74 /**
75 * @dataProvider provideGetRedirectTarget
76 */
77 public function testGetRedirectTarget( $title, $text ) {
78 $this->overrideConfigValues( [
79 MainConfigNames::Server => '//example.org',
80 MainConfigNames::ScriptPath => '/w',
81 MainConfigNames::Script => '/w/index.php',
82 ] );
83 $content = new CssContent( $text );
84 $target = $content->getRedirectTarget();
85 $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
86 }
87
88 /**
89 * Keep this in sync with CssContentHandlerTest::provideMakeRedirectContent()
90 */
91 public static function provideGetRedirectTarget() {
92 return [
93 [ 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ],
94 [ 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ],
95 [
96 'User:😂/unicode.css',
97 '/* #REDIRECT */@import url(//example.org/w/index.php?title=User:%F0%9F%98%82/unicode.css&action=raw&ctype=text/css);'
98 ],
99 # No #REDIRECT comment
100 [ null, "@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ],
101 # Wrong domain
102 [ null, "/* #REDIRECT */@import url(//example.com/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ],
103 ];
104 // phpcs:enable
105 }
106
107 public static function dataEquals() {
108 return [
109 [ new CssContent( 'hallo' ), null, false ],
110 [ new CssContent( 'hallo' ), new CssContent( 'hallo' ), true ],
111 [ new CssContent( 'hallo' ), new WikitextContent( 'hallo' ), false ],
112 [ new CssContent( 'hallo' ), new CssContent( 'HALLO' ), false ],
113 ];
114 }
115
116 /**
117 * @dataProvider dataEquals
118 */
119 public function testEquals( Content $a, ?Content $b = null, $equal = false ) {
120 $this->assertEquals( $equal, $a->equals( $b ) );
121 }
122
123 public static function getClassToTest(): string {
124 return CssContent::class;
125 }
126
127 public static function getTestInstancesAndAssertions(): array {
128 $redirects = self::provideGetRedirectTarget();
129 [ $redirectTitle, $redirectBlob ] = $redirects[0];
130 return [
131 'basic' => [
132 'instance' => new CssContent( '/* hello */' ),
133 'assertions' => static function ( $testCase, $obj ) {
134 $testCase->assertInstanceof( CssContent::class, $obj );
135 $testCase->assertSame( '/* hello */', $obj->getText() );
136 $testCase->assertNull( $obj->getRedirectTarget() );
137 },
138 ],
139 'redirect' => [
140 'instance' => new CssContent( $redirectBlob ),
141 'assertions' => static function ( $testCase, $obj ) use ( $redirectTitle, $redirectBlob ) {
142 $testCase->overrideConfigValues( [
143 MainConfigNames::Server => '//example.org',
144 MainConfigNames::ScriptPath => '/w',
145 MainConfigNames::Script => '/w/index.php',
146 ] );
147 $testCase->assertInstanceof( CssContent::class, $obj );
148 $testCase->assertSame( $redirectBlob, $obj->getText() );
149 $testCase->assertSame( $redirectTitle, $obj->getRedirectTarget()->getPrefixedText() );
150 },
151 ],
152 ];
153 }
154 }