Debian Codegraph @

tests/phpunit/unit/includes/language/LanguageFallbackTestTrait.php

raw symbols SCIP
1 <?php
2
3 // phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
4 // phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintReturn
5
6 /**
7 * Code to test the getFallbackFor, getFallbacksFor, and getFallbacksIncludingSiteLanguage methods
8 * that have historically been static methods of the Language class. It can be used to test any
9 * class or object that implements those three methods.
10 *
11 * @internal For LanguageFallbackTest and LanguageFallbackIntegrationTest
12 */
13 trait LanguageFallbackTestTrait {
14 /**
15 * @param array $options Valid keys:
16 * * expectedGets: How many times we expect to hit the localisation cache. (This can be
17 * ignored in integration tests -- it's enough to test in unit tests.)
18 * * fallbackMap: A map of language codes to fallback sequences to use.
19 * * siteLangCode
20 * @return string|object Name of class or object with the three methods getFirst, getAll, and
21 * getAllIncludingSiteLanguage (or getFallbackFor, getFallbacksFor, and
22 * getFallbacksIncludingSiteLanguage if callMethod() is suitably overridden).
23 */
24 abstract protected function getCallee( array $options = [] );
25
26 /**
27 * @return int Value from LanguageFallback:MESSAGES
28 */
29 abstract protected function getMessagesKey();
30
31 /**
32 * @return int Value from LanguageFallback::STRICT
33 */
34 abstract protected function getStrictKey();
35
36 /**
37 * @param int $expectedGets How many times it's expected that 'getItem' will be called
38 * @param array $map Map language codes to fallback arrays to return
39 * @return LocalisationCache
40 */
41 protected function getMockLocalisationCache( $expectedGets, $map ) {
42 $mockLocCache = $this->createNoOpMock( LocalisationCache::class, [ 'getItem' ] );
43 $mockLocCache->expects( $this->exactly( $expectedGets ) )->method( 'getItem' )
44 ->with( $this->anything(),
45 $this->logicalOr( 'fallbackSequence', 'originalFallbackSequence' ) )
46 ->willReturnCallback( static function ( $code, $key ) use ( $map ) {
47 if ( $key === 'originalFallbackSequence' || $code === 'en' ) {
48 return $map[$code];
49 }
50 $fallbacks = $map[$code];
51 if ( !$fallbacks || $fallbacks[count( $fallbacks ) - 1] !== 'en' ) {
52 $fallbacks[] = 'en';
53 }
54 return $fallbacks;
55 } );
56 return $mockLocCache;
57 }
58
59 /**
60 * Convenience/readability wrapper to call a method on a class or object.
61 *
62 * @param string|object $callee As in return value of getCallee()
63 * @param string $method Name of method to call
64 * @param mixed ...$params To pass to method
65 * @return mixed Return value of method
66 */
67 public function callMethod( $callee, $method, ...$params ) {
68 return [ $callee, $method ]( ...$params );
69 }
70
71 /**
72 * @param string $code
73 * @param array $expected
74 * @param array $options
75 * @dataProvider provideGetAll
76 */
77 public function testGetFirst( $code, array $expected, array $options = [] ) {
78 $callee = $this->getCallee( $options );
79 $this->assertSame( $expected[0] ?? null,
80 $this->callMethod( $callee, 'getFirst', $code ) );
81 }
82
83 /**
84 * @param string $code
85 * @param array $expected
86 * @param array $options
87 * @dataProvider provideGetAll
88 */
89 public function testGetAll( $code, array $expected, array $options = [] ) {
90 $this->assertSame( $expected,
91 $this->callMethod( $this->getCallee( $options ), 'getAll', $code ) );
92 }
93
94 /**
95 * @param string $code
96 * @param array $expected
97 * @param array $options
98 * @dataProvider provideGetAll
99 */
100 public function testGetAll_messages( $code, array $expected, array $options = [] ) {
101 $this->assertSame( $expected,
102 $this->callMethod( $this->getCallee( $options ), 'getAll',
103 $code, $this->getMessagesKey() ) );
104 }
105
106 public static function provideGetAll() {
107 return [
108 'en' => [ 'en', [], [ 'expectedGets' => 0 ] ],
109 'fr' => [ 'fr', [ 'en' ] ],
110 'sco' => [ 'sco', [ 'en' ] ],
111 'yi' => [ 'yi', [ 'he', 'en' ] ],
112 'ruq' => [ 'ruq', [ 'ruq-latn', 'ro', 'en' ] ],
113 'sh' => [ 'sh', [ 'sh-latn', 'sh-cyrl', 'bs', 'sr-el', 'sr-latn', 'hr', 'en' ] ],
114 ];
115 }
116
117 /**
118 * @param string $code
119 * @param array $expected
120 * @param array $options
121 * @dataProvider provideGetAll_strict
122 */
123 public function testGetAll_strict( $code, array $expected, array $options = [] ) {
124 $this->assertSame( $expected,
125 $this->callMethod( $this->getCallee( $options ), 'getAll',
126 $code, $this->getStrictKey() ) );
127 }
128
129 public static function provideGetAll_strict() {
130 return [
131 'en' => [ 'en', [], [ 'expectedGets' => 0 ] ],
132 'fr' => [ 'fr', [] ],
133 'sco' => [ 'sco', [ 'en' ] ],
134 'yi' => [ 'yi', [ 'he' ] ],
135 'ruq' => [ 'ruq', [ 'ruq-latn', 'ro' ] ],
136 'sh' => [ 'sh', [ 'sh-latn', 'sh-cyrl', 'bs', 'sr-el', 'sr-latn', 'hr' ] ],
137 ];
138 }
139
140 public function testGetAll_invalidMode() {
141 $this->expectException( InvalidArgumentException::class );
142 $this->expectExceptionMessage( 'Invalid fallback mode "7"' );
143
144 $callee = $this->getCallee( [ 'expectedGets' => 0 ] );
145
146 // These should not throw, because of short-circuiting. If they do, it will fail the test,
147 // because we pass 5 and 6 instead of 7.
148 $this->callMethod( $callee, 'getAll', 'en', 5 );
149 $this->callMethod( $callee, 'getAll', '!!!', 6 );
150
151 // This is the one that should throw.
152 $this->callMethod( $callee, 'getAll', 'fr', 7 );
153 }
154
155 /**
156 * @param string $code
157 * @param string $siteLangCode
158 * @param array $expected
159 * @param int $expectedGets
160 * @dataProvider provideGetAllIncludingSiteLanguage
161 */
162 public function testGetAllIncludingSiteLanguage(
163 $code, $siteLangCode, array $expected, $expectedGets = 1
164 ) {
165 $callee = $this->getCallee(
166 [ 'siteLangCode' => $siteLangCode, 'expectedGets' => $expectedGets ] );
167 $this->assertSame( $expected,
168 $this->callMethod( $callee, 'getAllIncludingSiteLanguage', $code ) );
169
170 // Call again to make sure we don't call LocalisationCache again
171 $this->callMethod( $callee, 'getAllIncludingSiteLanguage', $code );
172 }
173
174 public static function provideGetAllIncludingSiteLanguage() {
175 return [
176 'en on en' => [ 'en', 'en', [ [], [ 'en' ] ], 0 ],
177 'fr on en' => [ 'fr', 'en', [ [ 'en' ], [] ] ],
178 'en on fr' => [ 'en', 'fr', [ [], [ 'fr', 'en' ] ] ],
179 'fr on fr' => [ 'fr', 'fr', [ [ 'en' ], [ 'fr' ] ] ],
180
181 'sco on en' => [ 'sco', 'en', [ [ 'en' ], [] ] ],
182 'en on sco' => [ 'en', 'sco', [ [], [ 'sco', 'en' ] ] ],
183 'sco on sco' => [ 'sco', 'sco', [ [ 'en' ], [ 'sco' ] ] ],
184
185 'fr on sco' => [ 'fr', 'sco', [ [ 'en' ], [ 'sco' ] ], 2 ],
186 'sco on fr' => [ 'sco', 'fr', [ [ 'en' ], [ 'fr' ] ], 2 ],
187
188 'fr on yi' => [ 'fr', 'yi', [ [ 'en' ], [ 'yi', 'he' ] ], 2 ],
189 'yi on fr' => [ 'yi', 'fr', [ [ 'he', 'en' ], [ 'fr' ] ], 2 ],
190 'yi on yi' => [ 'yi', 'yi', [ [ 'he', 'en' ], [ 'yi' ] ] ],
191
192 'sh on ruq' => [ 'sh', 'ruq',
193 [ [ 'sh-latn', 'sh-cyrl', 'bs', 'sr-el', 'sr-latn', 'hr', 'en' ], [ 'ruq', 'ruq-latn', 'ro' ] ], 2 ],
194 'ruq on sh' => [ 'ruq', 'sh',
195 [ [ 'ruq-latn', 'ro', 'en' ], [ 'sh', 'sh-latn', 'sh-cyrl', 'bs', 'sr-el', 'sr-latn', 'hr' ] ], 2 ],
196 ];
197 }
198 }