| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
trait LanguageFallbackTestTrait { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
abstract protected function getCallee( array $options = [] ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
abstract protected function getMessagesKey(); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
abstract protected function getStrictKey(); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function callMethod( $callee, $method, ...$params ) { |
| 68 |
return [ $callee, $method ]( ...$params ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 147 |
|
| 148 |
$this->callMethod( $callee, 'getAll', 'en', 5 ); |
| 149 |
$this->callMethod( $callee, 'getAll', '!!!', 6 ); |
| 150 |
|
| 151 |
|
| 152 |
$this->callMethod( $callee, 'getAll', 'fr', 7 ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 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 |
} |