Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Utils/TokenUtilsTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Utils;
4
5 use Wikimedia\Parsoid\Tokens\KV;
6 use Wikimedia\Parsoid\Tokens\KVSourceRange;
7 use Wikimedia\Parsoid\Tokens\NlTk;
8 use Wikimedia\Parsoid\Tokens\SelfclosingTagTk;
9 use Wikimedia\Parsoid\Tokens\TagTk;
10 use Wikimedia\Parsoid\Tokens\Token;
11 use Wikimedia\Parsoid\Utils\TokenUtils;
12
13 /**
14 * @coversDefaultClass \Wikimedia\Parsoid\Utils\TokenUtils
15 */
16 class TokenUtilsTest extends \PHPUnit\Framework\TestCase {
17
18 private const TOKEN_TEST_DATA = [
19 [
20 'token' => 'string',
21 'getTokenType' => 'string',
22 'tokensToString' => 'string',
23 ],
24 [
25 'token' => [ 'type' => 'NlTk' ],
26 'getTokenType' => 'NlTk',
27 'tokenTrimTransparent' => true,
28 ],
29 [
30 'name' => '<div>',
31 'token' => [
32 'type' => 'TagTk',
33 'name' => 'div',
34 'attribs' => [],
35 ],
36 'getTokenType' => 'TagTk',
37 'tagClosesBlockScope' => true,
38 ],
39 [
40 'name' => '<p>',
41 'token' => [
42 'type' => 'TagTk',
43 'name' => 'p',
44 'attribs' => [],
45 ],
46 'getTokenType' => 'TagTk',
47 'tagOpensBlockScope' => true,
48 ],
49 [
50 'name' => '<td>',
51 'token' => [
52 'type' => 'TagTk',
53 'name' => 'td',
54 'attribs' => [],
55 ],
56 'getTokenType' => 'TagTk',
57 'tagClosesBlockScope' => true,
58 'isTableTag' => true,
59 ],
60 [
61 'name' => 'template token',
62 'token' => [
63 'type' => 'SelfclosingTagTk',
64 'name' => 'template',
65 'attribs' => [],
66 ],
67 'getTokenType' => 'SelfclosingTagTk',
68 'isTemplateToken' => true,
69 ],
70 [
71 'name' => 'html tag token',
72 'token' => [
73 'type' => 'TagTk',
74 'name' => 'div',
75 'attribs' => [
76 [ 'k' => 'role', 'v' => 'note' ],
77 [ 'k' => 'class', 'v' => 'hatnote navigation-not-searchable' ],
78 ],
79 'dataParsoid' => [
80 'stx' => 'html',
81 ],
82 ],
83 'getTokenType' => 'TagTk',
84 'tagClosesBlockScope' => true,
85 'isHTMLTag' => true,
86 ],
87 [
88 'name' => 'DOMFragment',
89 'token' => [
90 'type' => 'TagTk',
91 'name' => 'span',
92 'attribs' => [
93 [ 'k' => 'data-parsoid', 'v' => '{}' ],
94 [ 'k' => 'typeof', 'v' => 'mw:DOMFragment' ],
95 ],
96 'dataParsoid' => [
97 'tmp' => [ 'setDSR' => true ],
98 'html' => 'mwf13',
99 'tagWidths' => [ 8, 9 ]
100 ],
101 ],
102 'getTokenType' => 'TagTk',
103 'hasDOMFragmentType' => true,
104 ],
105 [
106 'name' => 'SOL-transparent <link>',
107 'token' => [
108 'type' => 'SelfclosingTagTk',
109 'name' => 'link',
110 'attribs' => [
111 [ 'k' => 'rel', 'v' => 'mw:PageProp/Category' ],
112 [ 'k' => 'href', 'v' => './Category:Articles_with_short_description' ],
113 ],
114 'dataParsoid' => [
115 'stx' => 'simple',
116 'a' => [
117 'href' => './Category:Articles_with_short_description',
118 ],
119 'sa' => [
120 'href' => 'Category:articles with short description'
121 ],
122 ],
123 ],
124 'getTokenType' => 'SelfclosingTagTk',
125 'isSolTransparentLinkTag' => true,
126 ],
127 [
128 'name' => 'comment',
129 'token' => [
130 'type' => 'CommentTk',
131 'value' => ' THIS IS A COMMENT ',
132 'dataParsoid' => [
133 'tsr' => [ 2104, 2147 ],
134 ],
135 ],
136 'getTokenType' => 'CommentTk',
137 ],
138 [
139 'name' => 'empty line meta token',
140 'token' => [
141 'type' => 'SelfclosingTagTk',
142 'name' => 'meta',
143 'attribs' => [
144 [ 'k' => 'typeof', 'v' => 'mw:EmptyLine' ],
145 ],
146 'dataParsoid' => [
147 'tokens' => [
148 [ 'type' => 'NlTk' ],
149 ],
150 ],
151 ],
152 'getTokenType' => 'SelfclosingTagTk',
153 'isEmptyLineMetaToken' => true,
154 ],
155 ];
156
157 public function provideTokens() {
158 foreach ( self::TOKEN_TEST_DATA as $k => $t ) {
159 $t['name'] = $t['name'] ?? "Token Test #$k";
160 $t['token'] = Token::getToken( $t['token'] );
161 yield $t['name'] => [ $t ];
162 }
163 }
164
165 /**
166 * @covers ::getTokenType
167 * @dataProvider provideTokens
168 */
169 public function testGetTokenType( array $testCase ) {
170 $this->assertEquals(
171 $testCase['getTokenType'] ?? 'unknown',
172 TokenUtils::getTokenType( $testCase['token'] )
173 );
174 }
175
176 /**
177 * @covers ::tagOpensBlockScope
178 * @dataProvider provideTokens
179 */
180 public function testTagOpensBlockScope( array $testCase ) {
181 $token = $testCase['token'];
182 $this->assertEquals(
183 $testCase['tagOpensBlockScope'] ?? false,
184 is_string( $token ) ? false :
185 TokenUtils::tagOpensBlockScope( $token->getName() )
186 );
187 }
188
189 /**
190 * @covers ::tagClosesBlockScope
191 * @dataProvider provideTokens
192 */
193 public function testTagClosesBlockScope( array $testCase ) {
194 $token = $testCase['token'];
195 $this->assertEquals(
196 $testCase['tagClosesBlockScope'] ?? false,
197 is_string( $token ) ? false :
198 TokenUtils::tagClosesBlockScope( $token->getName() )
199 );
200 }
201
202 /**
203 * @covers ::isTemplateToken
204 * @dataProvider provideTokens
205 */
206 public function testIsTemplateToken( array $testCase ) {
207 $token = $testCase['token'];
208 $this->assertEquals(
209 $testCase['isTemplateToken'] ?? false,
210 TokenUtils::isTemplateToken( $token )
211 );
212 }
213
214 /**
215 * @covers ::isHTMLTag
216 * @dataProvider provideTokens
217 */
218 public function testIsHTMLTag( array $testCase ) {
219 $token = $testCase['token'];
220 $this->assertEquals(
221 $testCase['isHTMLTag'] ?? false,
222 TokenUtils::isHTMLTag( $token )
223 );
224 }
225
226 /**
227 * @covers ::hasDOMFragmentType
228 * @dataProvider provideTokens
229 */
230 public function testIsDOMFragmentType( array $testCase ) {
231 $token = $testCase['token'];
232 $this->assertEquals(
233 $testCase['hasDOMFragmentType'] ?? false,
234 ( $token instanceof TagTk || $token instanceof SelfclosingTagTk ) ?
235 TokenUtils::hasDOMFragmentType(
236 $token
237 ) : false
238 );
239 }
240
241 /**
242 * @covers ::isTableTag
243 * @dataProvider provideTokens
244 */
245 public function testIsTableTag( array $testCase ) {
246 $token = $testCase['token'];
247 $this->assertEquals(
248 $testCase['isTableTag'] ?? false,
249 TokenUtils::isTableTag( $token )
250 );
251 }
252
253 /**
254 * @covers ::isSolTransparentLinkTag
255 * @dataProvider provideTokens
256 */
257 public function testIsSolTransparentLinkTag( array $testCase ) {
258 $token = $testCase['token'];
259 $this->assertEquals(
260 $testCase['isSolTransparentLinkTag'] ?? false,
261 TokenUtils::isSolTransparentLinkTag( $token )
262 );
263 }
264
265 /**
266 * @covers ::isEntitySpanToken
267 * @dataProvider provideTokens
268 */
269 public function testIsEntitySpanToken( array $testCase ) {
270 $token = $testCase['token'];
271 $this->assertEquals(
272 $testCase['isEntitySpanToken'] ?? false,
273 TokenUtils::isEntitySpanToken( $token )
274 );
275 }
276
277 /**
278 * @covers ::isEmptyLineMetaToken
279 * @dataProvider provideTokens
280 */
281 public function testIsEmptyLineMetaToken( array $testCase ) {
282 $token = $testCase['token'];
283 $this->assertEquals(
284 $testCase['isEmptyLineMetaToken'] ?? false,
285 TokenUtils::isEmptyLineMetaToken( $token )
286 );
287 }
288
289 /**
290 * @covers ::tokensToString
291 * @dataProvider provideTokens
292 */
293 public function testTokensToString( array $testCase ) {
294 $tokens = [ 'abc', $testCase['token'], 'def', new NlTk( null ) ];
295 $this->assertEquals(
296 'abc' . ( $testCase['tokensToString'] ?? '' ) . 'def',
297 TokenUtils::tokensToString( $tokens )
298 );
299 }
300
301 /**
302 * @covers ::kvToHash
303 * @covers ::tokenTrim
304 * @dataProvider provideTokens
305 */
306 public function testKvToHash( array $testCase ) {
307 $k = [ ' key', $testCase['token'], 'ABC ' ];
308 $v = [ ' vaLUE', $testCase['token'], new NlTk( null ) ];
309 $kExpect = 'key' . ( $testCase['tokensToString'] ?? '' ) . 'abc';
310 $vExpect = 'vaLUE' . ( $testCase['tokensToString'] ?? '' );
311 $srcOffsets = new KVSourceRange( 0, 10, 11, 20 );
312 $expected = [];
313 $expected[$kExpect] = $vExpect;
314 $this->assertEquals(
315 $expected,
316 TokenUtils::kvToHash( [ new KV( $k, $v, $srcOffsets ) ] )
317 );
318 }
319
320 /**
321 * @covers ::convertOffsets()
322 * @dataProvider provideConvertOffsets
323 */
324 public function testConvertOffsets( $str, $from, $to, $input, $expect ) {
325 $offsets = [];
326 foreach ( $input as &$v ) {
327 $offsets[] = &$v;
328 }
329 unset( $v );
330
331 TokenUtils::convertOffsets( $str, $from, $to, $offsets );
332 $this->assertSame( $expect, $offsets, "$from β†’ $to" );
333 }
334
335 public static function provideConvertOffsets() {
336 # Ensure that we have char from each UTF-8 class here.
337 #
338 # "foo bΓ‘r πŸ’©πŸ’© baz AΤ±δΊΊπŸ’©"
339 # char 012345678 9 01234567 8
340 # ucs 012345678 0 23456789 0
341 # byte 012345789 3 78901235 8
342 #
343 $str = "foo bΓ‘r \u{1F4A9}\u{1F4A9} baz A\u{0531}\u{4EBA}\u{1F4A9}";
344 $offsets = [
345 # 0th offset must be zero, 1st should be length of string
346 'byte' => [ 0, 32, 4, 13, 9, 18, 21, 22, 23, 25, 28 ],
347 'char' => [ 0, 19, 4, 9, 8, 11, 14, 15, 16, 17, 18 ],
348 'ucs2' => [ 0, 22, 4, 10, 8, 13, 16, 17, 18, 19, 20 ],
349 ];
350 foreach ( $offsets as $from => $input ) {
351 foreach ( $offsets as $to => $expect ) {
352 yield "$from β†’ $to" => [ $str, $from, $to, $input, $expect ];
353 }
354 }
355
356 yield "Passing 0 offsets doesn't error" => [ $str, 'byte', 'char', [], [] ];
357
358 yield "No error if we run out of offsets before EOS"
359 => [ $str, 'byte', 'char', [ 0, 9 ], [ 0, 8 ] ];
360
361 foreach ( $offsets as $from => $input ) {
362 foreach ( $offsets as $to => $expect ) {
363 yield "Out of bounds offsets, $from β†’ $to"
364 => [ $str, $from, $to, [ -10, 500 ], [ $expect[0], $expect[1] ] ];
365 }
366 }
367
368 yield "Rounding bytes"
369 => [ "πŸ’©πŸ’©πŸ’©", 'byte', 'byte', [ 0, 1, 2, 3, 4, 5 ], [ 0, 4, 4, 4, 4, 8 ] ];
370 yield "Rounding ucs2"
371 => [ "πŸ’©πŸ’©πŸ’©", 'ucs2', 'ucs2', [ 0, 1, 2, 3, 4 ], [ 0, 2, 2, 4, 4 ] ];
372 }
373
374 }