summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/tests/phpunit/TranslatablePageTest.php
blob: c492690b5f35c0c0a3ffe9eeab29f86db4d6c83f (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

/**
 * @ingroup PageTranslation
 */
class TranslatablePageTest extends PHPUnit\Framework\TestCase {
	/**
	 * @dataProvider provideTestSectionise
	 */
	public function testSectionise( $input, $pattern, $comment ) {
		$result = TranslatablePage::sectionise( $input );
		$pattern = addcslashes( $pattern, '~' );
		$this->assertRegExp( "~^$pattern$~", $result['template'], $comment );
	}

	public static function provideTestSectionise() {
		// Ugly implicit assumption
		$ph = "\x7fUNIQ[a-z0-9]{8,16}-\d+";

		$cases = [];

		$cases[] = [
			'Hello',
			"$ph",
			'No surrounding whitespace',
		];

		$cases[] = [
			"\nHello",
			"\n$ph",
			'With surrounding whitespace',
		];

		$cases[] = [
			"\nHello world\n\nBunny\n",
			"\n$ph\n\n$ph\n",
			'Splitting at one empty line',
		];

		$cases[] = [
			"First\n\n\n\n\nSecond\n\nThird",
			"$ph\n\n\n\n\n$ph\n\n$ph",
			'Splitting with multiple empty lines',
		];

		return $cases;
	}

	/**
	 * @dataProvider provideTestCleanupTags
	 */
	public function testCleanupTags( $input, $expected, $comment ) {
		$output = TranslatablePage::cleanupTags( $input );
		$this->assertEquals( $expected, $output, $comment );
	}

	public static function provideTestCleanupTags() {
		$cases = [];

		$cases[] = [
			"== Hello ==\n</translate>",
			'== Hello ==',
			'Unbalanced tag in a section preview',
		];

		$cases[] = [
			"</translate><translate>",
			'',
			'Unbalanced tags, no whitespace',
		];

		$cases[] = [
			"1\n2<translate>3\n4</translate>5\n6",
			"1\n23\n45\n6",
			'Unbalanced tags, non-removable whitespace',
		];

		$cases[] = [
			"1<translate>\n\n</translate>2",
			'12',
			'Unbalanced tags, removable whitespace',
		];

		$cases[] = [
			'[[<tvar|wmf>Special:MyLanguage/Wikimedia Foundation</>|Wikimedia Foundation]].',
			'[[Special:MyLanguage/Wikimedia Foundation|Wikimedia Foundation]].',
			'TVAR tag is collapsed',
		];

		$cases[] = [
			'You can use the <nowiki><translate></nowiki> tag.',
			'You can use the <nowiki><translate></nowiki> tag.',
			'Tag inside a nowiki is retained',
		];

		$cases[] = [
			'What if I <translate and </translate>.',
			'What if I <translate and .',
			'Broken tag is retained',
		];

		return $cases;
	}
}