File: LanguageFallbackTestTrait.php

package info (click to toggle)
mediawiki 1%3A1.35.13-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 274,932 kB
  • sloc: php: 677,563; javascript: 572,709; sql: 11,565; python: 4,447; xml: 3,145; sh: 892; perl: 788; ruby: 496; pascal: 365; makefile: 128
file content (237 lines) | stat: -rw-r--r-- 8,540 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php

use Wikimedia\Assert\PostconditionException;

/**
 * Code to test the getFallbackFor, getFallbacksFor, and getFallbacksIncludingSiteLanguage methods
 * that have historically been static methods of the Language class. It can be used to test any
 * class or object that implements those three methods.
 */
trait LanguageFallbackTestTrait {
	/**
	 * @param array $options Valid keys:
	 *   * expectedGets: How many times we expect to hit the localisation cache. (This can be
	 *   ignored in integration tests -- it's enough to test in unit tests.)
	 *   * fallbackMap: A map of language codes to fallback sequences to use.
	 *   * siteLangCode
	 * @return string|object Name of class or object with the three methods getFirst, getAll, and
	 *   getAllIncludingSiteLanguage (or getFallbackFor, getFallbacksFor, and
	 *   getFallbacksIncludingSiteLanguage if callMethod() is suitably overridden).
	 */
	abstract protected function getCallee( array $options = [] );

	/**
	 * @return int Value that was historically in Language::MESSAGES_FALLBACKS
	 */
	abstract protected function getMessagesKey();

	/**
	 * @return int Value that was historically in Language::STRICT_FALLBACKS
	 */
	abstract protected function getStrictKey();

	/**
	 * @param int $expectedGets How many times it's expected that 'getItem' will be called
	 * @param array $map Map language codes to fallback arrays to return
	 * @return LocalisationCache
	 */
	protected function getMockLocalisationCache( $expectedGets, $map ) {
		$mockLocCache = $this->createMock( LocalisationCache::class );
		$mockLocCache->expects( $this->exactly( $expectedGets ) )->method( 'getItem' )
			->with( $this->anything(),
				$this->logicalOr( 'fallbackSequence', 'originalFallbackSequence' ) )
			->will( $this->returnCallback( function ( $code, $key ) use ( $map ) {
				if ( $key === 'originalFallbackSequence' || $code === 'en' ) {
					return $map[$code];
				}
				$fallbacks = $map[$code];
				if ( !$fallbacks || $fallbacks[count( $fallbacks ) - 1] !== 'en' ) {
					$fallbacks[] = 'en';
				}
				return $fallbacks;
			} ) );
		$mockLocCache->expects( $this->never() )->method( $this->anythingBut( 'getItem' ) );
		return $mockLocCache;
	}

	/**
	 * Convenience/readability wrapper to call a method on a class or object.
	 *
	 * @param string|object $callee As in return value of getCallee()
	 * @param string $method Name of method to call
	 * @param mixed ...$params To pass to method
	 * @return mixed Return value of method
	 */
	public function callMethod( $callee, $method, ...$params ) {
		return [ $callee, $method ]( ...$params );
	}

	/**
	 * @param string $code
	 * @param array $expected
	 * @param array $options
	 * @dataProvider provideGetAll
	 * @covers MediaWiki\Languages\LanguageFallback::getFirst
	 * @covers Language::getFallbackFor
	 */
	public function testGetFirst( $code, array $expected, array $options = [] ) {
		$callee = $this->getCallee( $options );
		// One behavior difference between the old static methods and the new instance methods:
		// returning null instead of false.
		$defaultExpected = is_object( $callee ) ? null : false;
		$this->assertSame( $expected[0] ?? $defaultExpected,
			$this->callMethod( $callee, 'getFirst', $code ) );
	}

	/**
	 * @param string $code
	 * @param array $expected
	 * @param array $options
	 * @dataProvider provideGetAll
	 * @covers MediaWiki\Languages\LanguageFallback::getAll
	 * @covers Language::getFallbacksFor
	 */
	public function testGetAll( $code, array $expected, array $options = [] ) {
		$this->assertSame( $expected,
			$this->callMethod( $this->getCallee( $options ), 'getAll', $code ) );
	}

	/**
	 * @param string $code
	 * @param array $expected
	 * @param array $options
	 * @dataProvider provideGetAll
	 * @covers MediaWiki\Languages\LanguageFallback::getAll
	 * @covers Language::getFallbacksFor
	 */
	public function testGetAll_messages( $code, array $expected, array $options = [] ) {
		$this->assertSame( $expected,
			$this->callMethod( $this->getCallee( $options ), 'getAll',
				$code, $this->getMessagesKey() ) );
	}

	public static function provideGetAll() {
		return [
			'en' => [ 'en', [], [ 'expectedGets' => 0 ] ],
			'fr' => [ 'fr', [ 'en' ] ],
			'sco' => [ 'sco', [ 'en' ] ],
			'yi' => [ 'yi', [ 'he', 'en' ] ],
			'ruq' => [ 'ruq', [ 'ruq-latn', 'ro', 'en' ] ],
			'sh' => [ 'sh', [ 'bs', 'sr-el', 'hr', 'en' ] ],
		];
	}

	/**
	 * @param string $code
	 * @param array $expected
	 * @param array $options
	 * @dataProvider provideGetAll_strict
	 * @covers MediaWiki\Languages\LanguageFallback::getAll
	 * @covers Language::getFallbacksFor
	 */
	public function testGetAll_strict( $code, array $expected, array $options = [] ) {
		$this->assertSame( $expected,
			$this->callMethod( $this->getCallee( $options ), 'getAll',
				$code, $this->getStrictKey() ) );
	}

	public static function provideGetAll_strict() {
		return [
			'en' => [ 'en', [], [ 'expectedGets' => 0 ] ],
			'fr' => [ 'fr', [] ],
			'sco' => [ 'sco', [ 'en' ] ],
			'yi' => [ 'yi', [ 'he' ] ],
			'ruq' => [ 'ruq', [ 'ruq-latn', 'ro' ] ],
			'sh' => [ 'sh', [ 'bs', 'sr-el', 'hr' ] ],
		];
	}

	/**
	 * @covers MediaWiki\Languages\LanguageFallback::getAll
	 * @covers Language::getFallbacksFor
	 */
	public function testGetAll_invalidMode() {
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage( 'Invalid fallback mode "7"' );

		$callee = $this->getCallee( [ 'expectedGets' => 0 ] );

		// These should not throw, because of short-circuiting. If they do, it will fail the test,
		// because we pass 5 and 6 instead of 7.
		$this->callMethod( $callee, 'getAll', 'en', 5 );
		$this->callMethod( $callee, 'getAll', '!!!', 6 );

		// This is the one that should throw.
		$this->callMethod( $callee, 'getAll', 'fr', 7 );
	}

	/**
	 * @covers MediaWiki\Languages\LanguageFallback::getAll
	 * @covers Language::getFallbacksFor
	 */
	public function testGetAll_invalidFallback() {
		$callee = $this->getCallee( [ 'fallbackMap' => [ 'qqz' => [ 'fr', 'de', '!!!', 'hi' ] ] ] );

		$this->expectException( PostconditionException::class );
		$this->expectExceptionMessage( "Invalid fallback code '!!!' in fallback sequence for 'qqz'" );
		$this->callMethod( $callee, 'getAll', 'qqz' );
	}

	/**
	 * @covers MediaWiki\Languages\LanguageFallback::getAll
	 * @covers Language::getFallbacksFor
	 */
	public function testGetAll_invalidFallback_strict() {
		$callee = $this->getCallee( [ 'fallbackMap' => [ 'qqz' => [ 'fr', 'de', '!!!', 'hi' ] ] ] );

		$this->expectException( PostconditionException::class );
		$this->expectExceptionMessage( "Invalid fallback code '!!!' in fallback sequence for 'qqz'" );
		$this->callMethod( $callee, 'getAll', 'qqz', $this->getStrictKey() );
	}

	/**
	 * @param string $code
	 * @param string $siteLangCode
	 * @param array $expected
	 * @param int $expectedGets
	 * @dataProvider provideGetAllIncludingSiteLanguage
	 * @covers MediaWiki\Languages\LanguageFallback::getAllIncludingSiteLanguage
	 * @covers Language::getFallbacksIncludingSiteLanguage
	 */
	public function testGetAllIncludingSiteLanguage(
		$code, $siteLangCode, array $expected, $expectedGets = 1
	) {
		$callee = $this->getCallee(
			[ 'siteLangCode' => $siteLangCode, 'expectedGets' => $expectedGets ] );
		$this->assertSame( $expected,
			$this->callMethod( $callee, 'getAllIncludingSiteLanguage', $code ) );

		// Call again to make sure we don't call LocalisationCache again
		$this->callMethod( $callee, 'getAllIncludingSiteLanguage', $code );
	}

	public static function provideGetAllIncludingSiteLanguage() {
		return [
			'en on en' => [ 'en', 'en', [ [], [ 'en' ] ], 0 ],
			'fr on en' => [ 'fr', 'en', [ [ 'en' ], [] ] ],
			'en on fr' => [ 'en', 'fr', [ [], [ 'fr', 'en' ] ] ],
			'fr on fr' => [ 'fr', 'fr', [ [ 'en' ], [ 'fr' ] ] ],

			'sco on en' => [ 'sco', 'en', [ [ 'en' ], [] ] ],
			'en on sco' => [ 'en', 'sco', [ [], [ 'sco', 'en' ] ] ],
			'sco on sco' => [ 'sco', 'sco', [ [ 'en' ], [ 'sco' ] ] ],

			'fr on sco' => [ 'fr', 'sco', [ [ 'en' ], [ 'sco' ] ], 2 ],
			'sco on fr' => [ 'sco', 'fr', [ [ 'en' ], [ 'fr' ] ], 2 ],

			'fr on yi' => [ 'fr', 'yi', [ [ 'en' ], [ 'yi', 'he' ] ], 2 ],
			'yi on fr' => [ 'yi', 'fr', [ [ 'he', 'en' ], [ 'fr' ] ], 2 ],
			'yi on yi' => [ 'yi', 'yi', [ [ 'he', 'en' ], [ 'yi' ] ] ],

			'sh on ruq' => [ 'sh', 'ruq',
				[ [ 'bs', 'sr-el', 'hr', 'en' ], [ 'ruq', 'ruq-latn', 'ro' ] ], 2 ],
			'ruq on sh' => [ 'ruq', 'sh',
				[ [ 'ruq-latn', 'ro', 'en' ], [ 'sh', 'bs', 'sr-el', 'hr' ] ], 2 ],
		];
	}
}