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
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\Translate\PageTranslation;
use FatMessage;
use Language;
use MediaWikiTestCase;
use WikiPageMessageGroup;
/**
* @author Niklas Laxström
* @license GPL-2.0-or-later
* @covers \MediaWiki\Extension\Translate\PageTranslation\TranslationPage
*/
class TranslationPageTest extends MediaWikiTestCase {
/** @dataProvider provideTestGenerateSourceFromTranslations */
public function testGenerateSourceFromTranslations(
bool $inline,
bool $canWrap,
bool $wrapUntranslated,
array $messages,
string $expected
) {
// This test skips all the message loading from database
$template = '<S>';
$unitMap = [];
foreach ( $messages as $id => $m ) {
/** @var FatMessage $m */
$unit = new TranslationUnit();
$unit->id = $id;
$unit->text = $m->definition();
$unit->setIsInline( $inline );
$unit->setCanWrap( $canWrap );
$unitMap[$unit->id] = $unit;
}
// Then create appropriate units in the section. We are using the array keys, which
// works as long as there are less than ten units.
$sectionMap = [ '<S>' => new Section( '', implode( ' | ', array_keys( $unitMap ) ), '' ) ];
$output = new ParserOutput( $template, $sectionMap, $unitMap );
$translationPage = new TranslationPage(
$output,
$this->createMock( WikiPageMessageGroup::class ),
Language::factory( 'ar' ),
Language::factory( 'en' ),
true /*$showOutdated*/,
$wrapUntranslated,
'' /*$prefix*/
);
$actual = $translationPage->generateSourceFromTranslations( $messages );
$this->assertSame( $expected, $actual );
}
public function provideTestGenerateSourceFromTranslations() {
$inline = true;
$block = false;
$wrap = true;
$nowrap = false;
$wrapUntranslated = true;
$sectionText = 'Hello';
$okMessage = new FatMessage( 'ignoredKey', $sectionText );
$okMessage->setTranslation( 'Hallo' );
$fuzzyMessage = new FatMessage( 'ignoredKey', $sectionText );
$fuzzyMessage->setTranslation( 'hallo' );
$fuzzyMessage->addTag( 'fuzzy' );
$untranslatedMessage = new FatMessage( 'ignoredKey', $sectionText );
$identicalMessage = new FatMessage( 'ignoredKey', $sectionText );
$identicalMessage->setTranslation( $sectionText );
$inlineWrappedOutdated = '<span class="mw-translate-fuzzy">hallo</span>';
$inlineWrappedUntranslated = '<span lang="en" dir="ltr" class="mw-content-ltr">Hello</span>';
$blockWrappedOutdated = "<div class=\"mw-translate-fuzzy\">\nhallo\n</div>";
$blockWrappedUntranslated = "<div lang=\"en\" dir=\"ltr\" class=\"mw-content-ltr\">\nHello\n</div>";
// Matrix of (inline|block) * (no)wrap * (no)wrapUntranslated
yield [
$inline,
$wrap,
$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
"Hallo | $inlineWrappedOutdated | Hello | $inlineWrappedUntranslated"
];
yield [
$inline,
$wrap,
!$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
"Hallo | $inlineWrappedOutdated | Hello | Hello"
];
yield [
$inline,
$nowrap,
$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
'Hallo | hallo | Hello | Hello',
];
yield [
$inline,
$nowrap,
!$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
'Hallo | hallo | Hello | Hello',
];
yield [
$block,
$wrap,
$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
"Hallo | $blockWrappedOutdated | Hello | $blockWrappedUntranslated"
];
yield [
$block,
$wrap,
!$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
"Hallo | $blockWrappedOutdated | Hello | Hello"
];
yield [
$block,
$nowrap,
$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
'Hallo | hallo | Hello | Hello',
];
yield [
$block,
$nowrap,
!$wrapUntranslated,
[ $okMessage, $fuzzyMessage, $identicalMessage, $untranslatedMessage ],
'Hallo | hallo | Hello | Hello',
];
}
}
|