Passed
Push — master ( 631cc3...8704f4 )
by
unknown
04:11
created

ElementTest::testGetFormattedTerm_3()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the MediaWiki extension Lingo.
4
 *
5
 * @copyright 2011 - 2018, Stephan Gambke
6
 * @license   GNU General Public License, version 2 (or any later version)
7
 *
8
 * The Lingo extension is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by the Free
10
 * Software Foundation; either version 2 of the License, or (at your option) any
11
 * later version.
12
 *
13
 * The Lingo extension is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
 * details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program. If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 * @author Stephan Gambke
22
 * @since 2.0
23
 * @file
24
 * @ingroup Lingo
25
 */
26
27
namespace Lingo\Tests\Unit;
28
use Lingo\Element;
29
30
/**
31
 * @group extensions-lingo
32
 * @group extensions-lingo-unit
33
 * @group mediawiki-databaseless
34
 *
35
 * @coversDefaultClass \Lingo\Element
36
 *
37
 * @ingroup Lingo
38
 * @ingroup Test
39
 */
40
class ElementTest extends \PHPUnit\Framework\TestCase {
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
42
	/** @var Element */
43
	protected $element;
44
	protected $doc;
45
46
	protected function setUp() {
47
		$this->doc = new \DOMDocument();
48
	}
49
50
	/**
51
	 * @covers ::__construct
52
	 */
53
	public function testCanConstruct() {
54
55
		$term = 'someTerm';
56
		$definition = [];
57
		$element = new Element( $term, $definition );
58
59
		$this->assertInstanceOf( '\Lingo\Element', $element );
60
	}
61
62
	/**
63
	 * Tests
64
	 * - if $wgexLingoDisplayOnce = false, the first and second occurrence of a term is correctly marked up as tooltip anchor
65
	 */
66
	public function testGetFormattedTerm_1() {
67
68
		// Setup
69
		$term = 'someTerm';
70
		$definition = [
71
			Element::ELEMENT_TERM       => $term,
72
			Element::ELEMENT_DEFINITION => 'someDefinition',
73
			Element::ELEMENT_LINK       => uniqid(), // just some fake page name that does not exist on the wiki
74
			Element::ELEMENT_SOURCE     => null,
75
			Element::ELEMENT_STYLE      => null,
76
		];
77
		$element = new Element( $term, $definition );
78
79
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
80
81
		$expectedAttributes = ['class' => [ 'mw-lingo-term' ], 'data-lingo-term-id' => '8ade40e10f35a32fbb1e06a4b54751d0' ];
82
83
		// Run
84
		$node = $element->getFormattedTerm( $this->doc );
85
86
		// Check
87
		$this->checkTermIsDomElement( $node, 'span', $term, $expectedAttributes );
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type DOMText; however, parameter $node of Lingo\Tests\Unit\Element...checkTermIsDomElement() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
		$this->checkTermIsDomElement( /** @scrutinizer ignore-type */ $node, 'span', $term, $expectedAttributes );
Loading history...
Bug introduced by
$expectedAttributes of type array<string,array<integer,string>|string> is incompatible with the type string[] expected by parameter $expectedAttributes of Lingo\Tests\Unit\Element...checkTermIsDomElement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
		$this->checkTermIsDomElement( $node, 'span', $term, /** @scrutinizer ignore-type */ $expectedAttributes );
Loading history...
88
89
		// Run
90
		$node = $element->getFormattedTerm( $this->doc );
91
92
		// Check
93
		$this->checkTermIsDomElement( $node, 'span', $term, $expectedAttributes );
94
	}
95
96
	/**
97
	 * Tests
98
	 * - if $wgexLingoDisplayOnce = true, the first occurrence of a term is correctly marked up as tooltip anchor
99
	 * - if $wgexLingoDisplayOnce = true, the second occurrence of a term is not marked up
100
	 */
101
	public function testGetFormattedTerm_2() {
102
103
		// Setup
104
		$term = 'someTerm';
105
		$definition = [];
106
		$element = new Element( $term, $definition );
107
108
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = true;
109
110
		$expectedAttributes = ['class' => 'mw-lingo-term', 'data-lingo-term-id' => '8ade40e10f35a32fbb1e06a4b54751d0' ];
111
112
		// Run
113
		$node = $element->getFormattedTerm( $this->doc );
114
115
		// Check
116
		$this->checkTermIsDomElement( $node, 'span', $term, $expectedAttributes );
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type DOMText; however, parameter $node of Lingo\Tests\Unit\Element...checkTermIsDomElement() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
		$this->checkTermIsDomElement( /** @scrutinizer ignore-type */ $node, 'span', $term, $expectedAttributes );
Loading history...
117
118
		// Run
119
		$node = $element->getFormattedTerm( $this->doc );
120
121
		// Check
122
		$this->assertInstanceOf( 'DOMText', $node );
123
		$this->assertEquals( $term, $node->wholeText );
0 ignored issues
show
Bug introduced by
The property wholeText does not seem to exist on DOMElement.
Loading history...
124
125
	}
126
127
	/**
128
	 * Tests
129
	 * - if there is only one definition and its text is empty and it has a link, the term is marked up as link
130
	 * - if the link is not a URL and does not point to an existing page, the term is marked up as "new" link
131
	 * - if $wgexLingoDisplayOnce = false, the first and second occurrence of of term are marked up as link
132
	 */
133
	public function testGetFormattedTerm_3() {
134
135
		// Setup
136
		$term = 'someTerm';
137
		$title = uniqid();
138
139
		$definition = [
140
			Element::ELEMENT_TERM       => $term,
141
			Element::ELEMENT_DEFINITION => null,
142
			Element::ELEMENT_LINK       => $title, // just some fake page name that does not exist on the wiki
143
			Element::ELEMENT_SOURCE     => null,
144
			Element::ELEMENT_STYLE      => null,
145
		];
146
147
		$element = new Element( $term, $definition );
148
149
		$expectedAttributes = [ 'class' => [ 'mw-lingo-term', 'new' ],  'title' => wfMessage( 'red-link-title', $title )->text()];
0 ignored issues
show
Bug introduced by
The function wfMessage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
		$expectedAttributes = [ 'class' => [ 'mw-lingo-term', 'new' ],  'title' => /** @scrutinizer ignore-call */ wfMessage( 'red-link-title', $title )->text()];
Loading history...
150
151
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
152
153
		// Run
154
		$node = $element->getFormattedTerm( $this->doc );
155
156
		// Check
157
		$this->checkTermIsDomElement( $node, 'a', $term, $expectedAttributes );
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type DOMText; however, parameter $node of Lingo\Tests\Unit\Element...checkTermIsDomElement() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
		$this->checkTermIsDomElement( /** @scrutinizer ignore-type */ $node, 'a', $term, $expectedAttributes );
Loading history...
158
159
		// Run
160
		$node = $element->getFormattedTerm( $this->doc );
161
162
		// Check
163
		$this->checkTermIsDomElement( $node, 'a', $term, $expectedAttributes );
164
	}
165
166
	/**
167
	 * Tests
168
	 * - if the link is not a URL and points to an existing page, the term is marked up with that title
169
	 */
170
	public function testGetFormattedTerm_4() {
171
172
		// Setup
173
		$term = 'someTerm';
174
		$title = 'Main Page';
175
176
		$definition = [
177
			Element::ELEMENT_TERM       => $term,
178
			Element::ELEMENT_DEFINITION => null,
179
			Element::ELEMENT_LINK       => $title, // just some fake page name that does not exist on the wiki
180
			Element::ELEMENT_SOURCE     => null,
181
			Element::ELEMENT_STYLE      => null,
182
		];
183
184
		$element = new Element( $term, $definition );
185
186
		$expectedAttributes = [ 'class' => [ 'mw-lingo-term' ],  'title' => $title ];
187
188
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
189
190
		// Run
191
		$node = $element->getFormattedTerm( $this->doc );
192
193
		// Check
194
		$this->checkTermIsDomElement( $node, 'a', $term, $expectedAttributes );
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type DOMText; however, parameter $node of Lingo\Tests\Unit\Element...checkTermIsDomElement() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
		$this->checkTermIsDomElement( /** @scrutinizer ignore-type */ $node, 'a', $term, $expectedAttributes );
Loading history...
Bug introduced by
$expectedAttributes of type array<string,array<integer,string>|string> is incompatible with the type string[] expected by parameter $expectedAttributes of Lingo\Tests\Unit\Element...checkTermIsDomElement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
		$this->checkTermIsDomElement( $node, 'a', $term, /** @scrutinizer ignore-type */ $expectedAttributes );
Loading history...
195
	}
196
197
	/**
198
	 * Tests
199
	 * - if there is only one definition and its text is empty and it has a link and $wgexLingoDisplayOnce = true, the first occurrence of a term is correctly marked up as link
200
	 * - if there is only one definition and its text is empty and it has a link and $wgexLingoDisplayOnce = true, the second occurrence of a term is not marked up
201
	 * - if a style is set in the definition, the link is marked up with that style
202
	 * - if the link is a valid URL, the term is marked up as external link
203
	 */
204
	public function testGetFormattedTerm_5() {
205
206
		// Setup
207
		$term = 'someTerm';
208
209
		$definition = [
210
			Element::ELEMENT_TERM       => $term,
211
			Element::ELEMENT_DEFINITION => null,
212
			Element::ELEMENT_LINK       => 'http://foo.com',
213
			Element::ELEMENT_SOURCE     => null,
214
			Element::ELEMENT_STYLE      => 'some-style',
215
		];
216
217
		$element = new Element( $term, $definition );
218
219
		$expectedAttributes = [ 'class' => [ 'mw-lingo-term', 'ext', 'some-style' ], 'title' => $term ];
220
221
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = true;
222
223
		// Run
224
		$node = $element->getFormattedTerm( $this->doc );
225
226
		// Check
227
		$this->checkTermIsDomElement( $node, 'a', $term, $expectedAttributes );
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type DOMText; however, parameter $node of Lingo\Tests\Unit\Element...checkTermIsDomElement() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
		$this->checkTermIsDomElement( /** @scrutinizer ignore-type */ $node, 'a', $term, $expectedAttributes );
Loading history...
Bug introduced by
$expectedAttributes of type array<string,string|array<integer,string>> is incompatible with the type string[] expected by parameter $expectedAttributes of Lingo\Tests\Unit\Element...checkTermIsDomElement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
		$this->checkTermIsDomElement( $node, 'a', $term, /** @scrutinizer ignore-type */ $expectedAttributes );
Loading history...
228
229
		// Run
230
		$node = $element->getFormattedTerm( $this->doc );
231
232
		// Check
233
		$this->assertInstanceOf( 'DOMText', $node );
234
		$this->assertEquals( $term, $node->wholeText );
0 ignored issues
show
Bug introduced by
The property wholeText does not seem to exist on DOMElement.
Loading history...
235
	}
236
237
	/**
238
	 * Tests
239
	 * - if there is only one definition and its text is empty and it has an invalid link, the term is marked up as tooltip
240
	 * - if the term contains HTML-special characters, it is handled without raising an exception
241
	 */
242
	public function testGetFormattedTerm_6() {
243
244
		// Setup
245
		$term = 'some&Term';
246
247
		$definition = [
248
			Element::ELEMENT_TERM       => $term,
249
			Element::ELEMENT_DEFINITION => null,
250
			Element::ELEMENT_LINK       => 'foo[]bar',
251
			Element::ELEMENT_SOURCE     => null,
252
			Element::ELEMENT_STYLE      => null,
253
		];
254
255
		$element = new Element( $term, $definition );
256
257
		$expectedAttributes = [ 'class' => 'mw-lingo-term', 'data-lingo-term-id' => 'a8057b0494da505d2f7ac2e96e17083f' ];
258
259
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
260
261
		// Run
262
		$node = $element->getFormattedTerm( $this->doc );
263
264
		// Check
265
		$this->checkTermIsDomElement( $node, 'span', $term, $expectedAttributes );
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type DOMText; however, parameter $node of Lingo\Tests\Unit\Element...checkTermIsDomElement() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

265
		$this->checkTermIsDomElement( /** @scrutinizer ignore-type */ $node, 'span', $term, $expectedAttributes );
Loading history...
266
267
	}
268
269
	/**
270
	 * Tests
271
	 * - if there is only one definition and its text is empty and it has an anchor link, the term is marked up as link without title attribute
272
	 */
273
	public function testGetFormattedTerm_7() {
274
275
		// Setup
276
		$term = 'some&Term';
277
278
		$definition = [
279
			Element::ELEMENT_TERM       => $term,
280
			Element::ELEMENT_DEFINITION => null,
281
			Element::ELEMENT_LINK       => '#someAnchor',
282
			Element::ELEMENT_SOURCE     => null,
283
			Element::ELEMENT_STYLE      => null,
284
		];
285
286
		$element = new Element( $term, $definition );
287
288
		$expectedAttributes = [ 'class' => 'mw-lingo-term' ];
289
		$unexpectedAttributes = [ 'title' ];
290
291
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
292
293
		// Run
294
		$node = $element->getFormattedTerm( $this->doc );
295
296
		// Check
297
		$this->checkTermIsDomElement( $node, 'a', $term, $expectedAttributes, $unexpectedAttributes );
0 ignored issues
show
Bug introduced by
It seems like $node can also be of type DOMText; however, parameter $node of Lingo\Tests\Unit\Element...checkTermIsDomElement() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

297
		$this->checkTermIsDomElement( /** @scrutinizer ignore-type */ $node, 'a', $term, $expectedAttributes, $unexpectedAttributes );
Loading history...
298
299
	}
300
301
	/**
302
	 * Tests
303
	 * - correct html is produced
304
	 * - correct order of definitions
305
	 * - user-defined class is applied to definition
306
	 */
307
	public function testGetFormattedDefinitions_1() {
308
309
		// Setup
310
		$term = 'some&Term';
311
312
		$definition1 = [
313
			Element::ELEMENT_TERM       => $term,
314
			Element::ELEMENT_DEFINITION => 'someDefinition1',
315
			Element::ELEMENT_LINK       => 'someInternalLink1',
316
			Element::ELEMENT_SOURCE     => null,
317
			Element::ELEMENT_STYLE      => null,
318
		];
319
320
		$url1 = \Title::newFromText( $definition1[ Element::ELEMENT_LINK ] )->getFullURL();
0 ignored issues
show
Bug introduced by
The type Title was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
321
322
		$definition2 = [
323
			Element::ELEMENT_TERM       => $term,
324
			Element::ELEMENT_DEFINITION => 'someDefinition2',
325
			Element::ELEMENT_LINK       => 'someInternalLink2',
326
			Element::ELEMENT_SOURCE     => null,
327
			Element::ELEMENT_STYLE      => 'some-style-2',
328
		];
329
330
		$url2 = \Title::newFromText( $definition2[ Element::ELEMENT_LINK ] )->getFullURL();
331
332
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
333
334
		$element = new Element( $term, $definition1 );
335
		$element->addDefinition( $definition2 );
336
		$node = $element->getFormattedTerm( $this->doc );
0 ignored issues
show
Unused Code introduced by
The assignment to $node is dead and can be removed.
Loading history...
337
338
		// Run
339
		$definitions = $element->getFormattedDefinitions();
340
341
		$this->assertEquals(
342
			"<div class='mw-lingo-tooltip' id='a8057b0494da505d2f7ac2e96e17083f'>" .
343
			"<div class='mw-lingo-definition '>" .
344
			"<div class='mw-lingo-definition-text'>\n" .
345
			"someDefinition1\n" .
346
			"</div>" .
347
			"<div class='mw-lingo-definition-link'>" .
348
			"[" . $url1 . " <nowiki/>]" .
349
			"</div></div>" .
350
			"<div class='mw-lingo-definition some-style-2'>" .
351
			"<div class='mw-lingo-definition-text'>\n" .
352
			"someDefinition2\n" .
353
			"</div>" .
354
			"<div class='mw-lingo-definition-link'>" .
355
			"[" . $url2 . " <nowiki/>]" .
356
			"</div></div>\n" .
357
			"</div>",
358
			$definitions
359
		);
360
	}
361
362
	/**
363
	 * Tests
364
	 * - if there is no link defined, no link is added to the list of definitions
365
	 * - if there is an invalid link, an error message is attached to the list of definitions and the link is omitted
366
	 */
367
	public function testGetFormattedDefinitions_2() {
368
369
		// Setup
370
		$term = 'some&Term';
371
372
		$definition1 = [
373
			Element::ELEMENT_TERM       => $term,
374
			Element::ELEMENT_DEFINITION => 'someDefinition1',
375
			Element::ELEMENT_LINK       => null,
376
			Element::ELEMENT_SOURCE     => null,
377
			Element::ELEMENT_STYLE      => null,
378
		];
379
380
		//$url1 = \Title::newFromText( $definition1[ Element::ELEMENT_LINK ] )->getFullURL();
381
382
		$definition2 = [
383
			Element::ELEMENT_TERM       => $term,
384
			Element::ELEMENT_DEFINITION => 'someDefinition2',
385
			Element::ELEMENT_LINK       => 'some[]InvalidLink2',
386
			Element::ELEMENT_SOURCE     => null,
387
			Element::ELEMENT_STYLE      => 'some-style-2',
388
		];
389
390
		//$url2 = \Title::newFromText( $definition2[ Element::ELEMENT_LINK ] )->getFullURL();
391
392
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
393
394
		$element = new Element( $term, $definition1 );
395
		$element->addDefinition( $definition2 );
396
		$node = $element->getFormattedTerm( $this->doc );
0 ignored issues
show
Unused Code introduced by
The assignment to $node is dead and can be removed.
Loading history...
397
398
		// Run
399
		$definitions = $element->getFormattedDefinitions();
400
401
		$this->assertEquals(
402
			"<div class='mw-lingo-tooltip' id='a8057b0494da505d2f7ac2e96e17083f'>" .
403
			"<div class='mw-lingo-definition '>" .
404
			"<div class='mw-lingo-definition-text'>\n" .
405
			"someDefinition1\n" .
406
			"</div>" .
407
			"</div>" .
408
			"<div class='mw-lingo-definition some-style-2'>" .
409
			"<div class='mw-lingo-definition-text'>\n" .
410
			"someDefinition2\n" .
411
			"</div></div>" .
412
			"<div class='mw-lingo-definition invalid-link-target'>" .
413
			"<div class='mw-lingo-definition-text'>\n" .
414
			"Invalid link target for term \"some&Term\": some[]InvalidLink2\n" .
415
			"</div></div>\n" .
416
			"</div>",
417
			$definitions
418
		);
419
	}
420
421
	/**
422
	 * Tests
423
	 * - if there is only one definition and its text is empty and it has a link, no definitions are produced
424
	 */
425
	public function testGetFormattedDefinitions_3() {
426
427
		// Setup
428
		$term = 'someTerm';
429
430
		$definition = [
431
			Element::ELEMENT_TERM       => $term,
432
			Element::ELEMENT_DEFINITION => null,
433
			Element::ELEMENT_LINK       => 'someLink',
434
			Element::ELEMENT_SOURCE     => null,
435
			Element::ELEMENT_STYLE      => null,
436
		];
437
438
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
439
440
		$element = new Element( $term, $definition );
441
		$node = $element->getFormattedTerm( $this->doc );
0 ignored issues
show
Unused Code introduced by
The assignment to $node is dead and can be removed.
Loading history...
442
443
		// Run
444
		$definitions = $element->getFormattedDefinitions();
445
446
		$this->assertEquals( '', $definitions );
447
	}
448
449
	/**
450
	 * Tests
451
	 * - if there is only one definition and its text is empty and it has an invalid link, the error message shows as tooltip
452
	 * - class 'invalid-link-target' is correctly applied to error message
453
	 * - if the term contains HTML-special characters, it is handled without raising an exception
454
	 */
455
	public function testGetFormattedDefinitions_4() {
456
457
		// Setup
458
		$term = 'some&Term';
459
460
		$definition = [
461
			Element::ELEMENT_TERM       => $term,
462
			Element::ELEMENT_DEFINITION => null,
463
			Element::ELEMENT_LINK       => 'foo[]bar',
464
			Element::ELEMENT_SOURCE     => null,
465
			Element::ELEMENT_STYLE      => null,
466
		];
467
468
		$GLOBALS[ 'wgexLingoDisplayOnce' ] = false;
469
470
		$element = new Element( $term, $definition );
471
		$node = $element->getFormattedTerm( $this->doc );
0 ignored issues
show
Unused Code introduced by
The assignment to $node is dead and can be removed.
Loading history...
472
473
		// Run
474
		$definitions = $element->getFormattedDefinitions();
475
476
		$this->assertEquals(
477
			"<div class='mw-lingo-tooltip' id='a8057b0494da505d2f7ac2e96e17083f'>" .
478
			"<div class='mw-lingo-definition invalid-link-target'>" .
479
			"<div class='mw-lingo-definition-text'>\n" .
480
			"Invalid link target for term \"some&Term\": foo[]bar\n" .
481
			"</div></div>\n</div>",
482
			$definitions
483
		);
484
	}
485
486
	/**
487
	 * @param \DOMElement $node
488
	 * @param string $tagName
489
	 * @param string $text
490
	 * @param string[] $expectedAttributes
491
	 * @param array $unexpectedAttributes
492
	 */
493
	protected function checkTermIsDomElement( $node, $tagName, $text, $expectedAttributes = [], $unexpectedAttributes = [] ) {
494
		$nodeText = $this->doc->saveHTML( $node );
0 ignored issues
show
Unused Code introduced by
The assignment to $nodeText is dead and can be removed.
Loading history...
495
496
		$this->assertInstanceOf( 'DOMElement', $node );
497
		$this->assertEquals( $tagName, $node->tagName );
498
		$this->assertEquals( $text, $node->textContent );
499
500
		if ( array_key_exists( 'class', $expectedAttributes ) ) {
501
502
			$classes = array_flip( array_filter( explode( ' ', $node->getAttribute( 'class' ) ) ) );
503
504
			foreach ( (array) $expectedAttributes[ 'class' ] as $expectedClass ) {
505
				$this->assertTrue( array_key_exists( $expectedClass , $classes ) );
506
			}
507
508
			unset( $expectedAttributes[ 'class' ] );
509
		}
510
511
		foreach ( $expectedAttributes as $attribute => $value ) {
512
			$this->assertEquals( $value, $node->getAttribute( $attribute ) );
513
		}
514
515
		foreach ( $unexpectedAttributes as $attribute ) {
516
			$this->assertFalse( $node->hasAttribute( $attribute ) );
517
		}
518
519
	}
520
}
521