summaryrefslogtreecommitdiff
blob: fd439254fa8892e6f52797d25d3b2fbfb6b6e636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

namespace MediaWiki\Extension\Translate\TranslatorInterface\Insertable;

use MediaWikiUnitTestCase;

/** @covers \MediaWiki\Extension\Translate\TranslatorInterface\Insertable\HtmlTagInsertablesSuggester */
class HtmlTagInsertablesSuggesterTest extends MediaWikiUnitTestCase {
	/** @dataProvider getTestHtmlTagInsertablesSuggesterProvider */
	public function testHtmlTagInsertablesSuggester( $text, $expected, $comment = '' ) {
		$suggester = new HtmlTagInsertablesSuggester();
		$actual = $suggester->getInsertables( $text );

		$this->assertCount(
			count( $expected ),
			$actual,
			'should return correct number of insertables ' . $comment
		);

		foreach ( $expected as $i => $values ) {
			$this->assertEquals(
				$values['display'],
				$actual[$i]->getDisplayText(),
				'should return the correct display text ' . $comment
			);
			$this->assertEquals(
				$values['pre'],
				$actual[$i]->getPreText(),
				'should return the correct pre text ' . $comment
			);
			$this->assertEquals(
				$values['post'],
				$actual[$i]->getPostText(),
				'should return the correct post text ' . $comment
			);
		}
	}

	public function getTestHtmlTagInsertablesSuggesterProvider() {
		yield [
			'Hello <b>World</b>',
			[
				[ 'display' => '<b></b>', 'pre' => '<b>', 'post' => '</b>' ],
			],
			'for plain tag'
		];

		yield [
			'<html> <rocks>',
			[],
			'for unclosed tags'
		];

		yield [
			'Hello <b class="shaking">World</b>',
			[
				[ 'display' => '<b class="shaking"></b>', 'pre' => '<b class="shaking">', 'post'
				=> '</b>' ],
			],
			'for tag with attributes'
		];

		// TODO: support nested tags
		yield [
			'Hello <b><i>World</i></b>',
			[
				[ 'display' => '<b></b>', 'pre' => '<b>', 'post' => '</b>' ],
			],
			'for nested tags (currently not supported)'
		];

		// TODO: avoid duplicate insertables. Not a big issue as frontend de-duplicates
		yield [
			'Hello <b>a</b><i>b</i><b>c</b>',
			[
				[ 'display' => '<b></b>', 'pre' => '<b>', 'post' => '</b>' ],
				[ 'display' => '<i></i>', 'pre' => '<i>', 'post' => '</i>' ],
				[ 'display' => '<b></b>', 'pre' => '<b>', 'post' => '</b>' ],
			],
			'for multiple tags'
		];
	}
}