summaryrefslogtreecommitdiff
blob: 150f6b8b4086aebed3a590b95897c791357cac10 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare( strict_types = 1 );

/**
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @covers \AndroidXmlFFS
 */
class AndroidXmlFFSTest extends MediaWikiIntegrationTestCase {
	private const DOCLANG = 'qqq';

	protected function setUp(): void {
		$this->setMwGlobals( 'wgTranslateDocumentationLanguageCode', self::DOCLANG );
	}

	protected $groupConfiguration = [
		'BASIC' => [
			'class' => FileBasedMessageGroup::class,
			'id' => 'test-id',
			'label' => 'Test Label',
			'namespace' => 'NS_MEDIAWIKI',
			'description' => 'Test description',
		],
		'FILES' => [
			'class' => AndroidXmlFFS::class,
			'sourcePattern' => '',
		],
	];

	public function testParsing() {
		$file =
<<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<!-- Authors:
* Imaginary translator
-->
<resources>
	<string name="wpt_voicerec">Voice recording</string>
	<string name="wpt_stillimage" fuzzy="true">Picture</string>
	<plurals name="alot">
		<item quantity="one">bunny</item>
		<item quantity="other">bunnies</item>
	</plurals>
	<string name="has_quotes">Go to \"Wikipedia\"</string>
	<string name="starts_with_at">\@Wikipedia</string>
	<string name="has_ampersand">1&amp;nbsp;000</string>
	<string name="has_newline">first\nsecond</string>
	<string name="has_slashes">first \\ second</string>
	<string name="utf8_symbols">Hello World: \\u1234 \u1234 \\\u1234</string>
	<string name="quote_double_slash">Hello World: \' \\\'</string>
</resources>
XML;

		/** @var FileBasedMessageGroup $group */
		$group = MessageGroupBase::factory( $this->groupConfiguration );
		$ffs = new AndroidXmlFFS( $group );
		$parsed = $ffs->readFromVariable( $file );
		$expected = [
			'MESSAGES' => [
				'wpt_voicerec' => 'Voice recording',
				'wpt_stillimage' => '!!FUZZY!!Picture',
				'alot' => '{{PLURAL|one=bunny|bunnies}}',
				'has_quotes' => 'Go to "Wikipedia"',
				'starts_with_at' => '@Wikipedia',
				'has_ampersand' => '1&nbsp;000',
				'has_newline' => "first\nsecond",
				'has_slashes' => 'first \\ second',
				'utf8_symbols' => "Hello World: \u1234 ሴ \ሴ",
				'quote_double_slash' => 'Hello World: \' \\\''
			],
			'AUTHORS' => [
				'Imaginary translator',
			]
		];

		$this->assertEquals( $expected, $parsed );
	}

	public function testWrite() {
		/** @var FileBasedMessageGroup $group */
		$group = MessageGroupBase::factory( $this->groupConfiguration );
		$ffs = new AndroidXmlFFS( $group );

		$messages = [
			'ko=26ra' => 'wawe',
			'foobar' => '!!FUZZY!!Kissa kala <koira> "a\'b',
			'amuch' => '{{PLURAL|one=bunny|bunnies}}',
			'ampersand' => '&nbsp; &foo',
			'newlines' => "first\nsecond",
			'slashes' => 'has \\ slash'
		];
		$authors = [
			'1 Hyphen-Fan',
			'2 Hyphen--Lover',
			'3 Hyphen---Fanatic-',
		];

		$collection = new MockMessageCollection( $messages );
		$collection->addCollectionAuthors( $authors, 'set' );

		$xml = $ffs->writeIntoVariable( $collection );
		$parsed = $ffs->readFromVariable( $xml );
		$expected = [
			'MESSAGES' => $messages,
			'AUTHORS' => $authors,
		];
		$this->assertEquals( $expected, $parsed );
	}

	public function testWriteDoc() {
		/** @var FileBasedMessageGroup $group */
		$group = MessageGroupBase::factory( $this->groupConfiguration );
		$ffs = new AndroidXmlFFS( $group );

		$messages = [
			'a' => 'b',
		];

		$collection = new MockMessageCollection( $messages, self::DOCLANG );

		$actual = $ffs->writeIntoVariable( $collection );
		$expected = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="all">
  <string name="a">b</string>
</resources>

XML;
		$this->assertEquals( $expected, $actual );
	}
}

class MockMessageCollection extends MessageCollection {
	public function __construct( array $messages, string $code = 'en' ) {
		$this->code = $code;
		$keys = array_keys( $messages );
		$this->keys = array_combine( $keys, $keys );
		foreach ( $messages as $key => $value ) {
			$m = new FatMessage( $key, $value );
			$m->setTranslation( $value );
			if ( $key === 'foobar' ) {
				$m->addTag( 'fuzzy' );
			}
			$this->messages[$key] = $m;
		}
	}

	public function filter( $type, $condition = true, $value = null ) {
	}

	public function loadTranslations() {
	}
}