| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\Content; |
| 4 |
use MediaWiki\Content\FallbackContent; |
| 5 |
use MediaWiki\Content\FallbackContentHandler; |
| 6 |
use MediaWiki\Content\JavaScriptContent; |
| 7 |
use MediaWiki\Content\WikitextContent; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class FallbackContentTest extends MediaWikiLangTestCase { |
| 15 |
|
| 16 |
private const CONTENT_MODEL = 'xyzzy'; |
| 17 |
|
| 18 |
protected function setUp(): void { |
| 19 |
parent::setUp(); |
| 20 |
$this->mergeMwGlobalArrayValue( |
| 21 |
'wgContentHandlers', |
| 22 |
[ self::CONTENT_MODEL => FallbackContentHandler::class ] |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function newContent( $data, $type = self::CONTENT_MODEL ) { |
| 33 |
return new FallbackContent( $data, $type ); |
| 34 |
} |
| 35 |
|
| 36 |
public function testGetRedirectTarget() { |
| 37 |
$content = $this->newContent( '#REDIRECT [[Horkyporky]]' ); |
| 38 |
$this->assertNull( $content->getRedirectTarget() ); |
| 39 |
} |
| 40 |
|
| 41 |
public function testIsRedirect() { |
| 42 |
$content = $this->newContent( '#REDIRECT [[Horkyporky]]' ); |
| 43 |
$this->assertFalse( $content->isRedirect() ); |
| 44 |
} |
| 45 |
|
| 46 |
public function testIsCountable() { |
| 47 |
$content = $this->newContent( '[[Horkyporky]]' ); |
| 48 |
$this->assertFalse( $content->isCountable( true ) ); |
| 49 |
} |
| 50 |
|
| 51 |
public function testGetTextForSummary() { |
| 52 |
$content = $this->newContent( 'Horkyporky' ); |
| 53 |
$this->assertSame( '', $content->getTextForSummary() ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testGetTextForSearchIndex() { |
| 57 |
$content = $this->newContent( 'Horkyporky' ); |
| 58 |
$this->assertSame( '', $content->getTextForSearchIndex() ); |
| 59 |
} |
| 60 |
|
| 61 |
public function testCopy() { |
| 62 |
$content = $this->newContent( 'hello world.' ); |
| 63 |
$copy = $content->copy(); |
| 64 |
|
| 65 |
$this->assertSame( $content, $copy ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testGetSize() { |
| 69 |
$content = $this->newContent( 'hello world.' ); |
| 70 |
|
| 71 |
$this->assertEquals( 12, $content->getSize() ); |
| 72 |
} |
| 73 |
|
| 74 |
public function testGetData() { |
| 75 |
$content = $this->newContent( 'hello world.' ); |
| 76 |
|
| 77 |
$this->assertEquals( 'hello world.', $content->getData() ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testGetNativeData() { |
| 81 |
$content = $this->newContent( 'hello world.' ); |
| 82 |
|
| 83 |
$this->assertEquals( 'hello world.', $content->getNativeData() ); |
| 84 |
} |
| 85 |
|
| 86 |
public function testGetWikitextForTransclusion() { |
| 87 |
$content = $this->newContent( 'hello world.' ); |
| 88 |
|
| 89 |
$this->assertFalse( $content->getWikitextForTransclusion() ); |
| 90 |
} |
| 91 |
|
| 92 |
public function testGetModel() { |
| 93 |
$content = $this->newContent( "hello world.", 'horkyporky' ); |
| 94 |
|
| 95 |
$this->assertEquals( 'horkyporky', $content->getModel() ); |
| 96 |
} |
| 97 |
|
| 98 |
public function testGetContentHandler() { |
| 99 |
$this->mergeMwGlobalArrayValue( |
| 100 |
'wgContentHandlers', |
| 101 |
[ 'horkyporky' => FallbackContentHandler::class ] |
| 102 |
); |
| 103 |
|
| 104 |
$content = $this->newContent( "hello world.", 'horkyporky' ); |
| 105 |
|
| 106 |
$this->assertInstanceOf( FallbackContentHandler::class, $content->getContentHandler() ); |
| 107 |
$this->assertEquals( 'horkyporky', $content->getContentHandler()->getModelID() ); |
| 108 |
} |
| 109 |
|
| 110 |
public static function dataIsEmpty() { |
| 111 |
return [ |
| 112 |
[ '', true ], |
| 113 |
[ ' ', false ], |
| 114 |
[ '0', false ], |
| 115 |
[ 'hallo welt.', false ], |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function testIsEmpty( $text, $empty ) { |
| 123 |
$content = $this->newContent( $text ); |
| 124 |
|
| 125 |
$this->assertEquals( $empty, $content->isEmpty() ); |
| 126 |
} |
| 127 |
|
| 128 |
public static function provideEquals() { |
| 129 |
return [ |
| 130 |
[ new FallbackContent( "hallo", 'horky' ), null, false ], |
| 131 |
[ new FallbackContent( "hallo", 'horky' ), new FallbackContent( "hallo", 'horky' ), true ], |
| 132 |
[ new FallbackContent( "hallo", 'horky' ), new FallbackContent( "hallo", 'xyzzy' ), false ], |
| 133 |
[ new FallbackContent( "hallo", 'horky' ), new JavaScriptContent( "hallo" ), false ], |
| 134 |
[ new FallbackContent( "hallo", 'horky' ), new WikitextContent( "hallo" ), false ], |
| 135 |
]; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function testEquals( Content $a, ?Content $b = null, $equal = false ) { |
| 142 |
$this->assertEquals( $equal, $a->equals( $b ) ); |
| 143 |
} |
| 144 |
|
| 145 |
public static function provideConvert() { |
| 146 |
return [ |
| 147 |
[ |
| 148 |
'Hallo Welt', |
| 149 |
CONTENT_MODEL_WIKITEXT, |
| 150 |
'lossless', |
| 151 |
'Hallo Welt' |
| 152 |
], |
| 153 |
[ |
| 154 |
'Hallo Welt', |
| 155 |
CONTENT_MODEL_WIKITEXT, |
| 156 |
'lossless', |
| 157 |
'Hallo Welt' |
| 158 |
], |
| 159 |
[ |
| 160 |
'Hallo Welt', |
| 161 |
CONTENT_MODEL_CSS, |
| 162 |
'lossless', |
| 163 |
'Hallo Welt' |
| 164 |
], |
| 165 |
[ |
| 166 |
'Hallo Welt', |
| 167 |
CONTENT_MODEL_JAVASCRIPT, |
| 168 |
'lossless', |
| 169 |
'Hallo Welt' |
| 170 |
], |
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
public function testConvert() { |
| 175 |
$content = $this->newContent( 'More horkyporky?' ); |
| 176 |
|
| 177 |
$this->assertFalse( $content->convert( CONTENT_MODEL_TEXT ) ); |
| 178 |
} |
| 179 |
|
| 180 |
public function testSerialize() { |
| 181 |
$content = $this->newContent( 'Hörkypörky', 'horkyporky' ); |
| 182 |
|
| 183 |
$this->assertSame( 'Hörkypörky', $content->serialize() ); |
| 184 |
} |
| 185 |
|
| 186 |
} |