aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/cache/LocalisationCacheTest.php
blob: 163e0a99d8cc25056fc4ad306dd24cfe5da9bcc8 (plain) (blame)
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
<?php

use MediaWiki\Config\ServiceOptions;
use MediaWiki\Languages\LanguageNameUtils;
use Psr\Log\NullLogger;

/**
 * @group Database
 * @group Cache
 * @covers LocalisationCache
 * @author Niklas Laxström
 */
class LocalisationCacheTest extends MediaWikiTestCase {
	protected function setUp() : void {
		parent::setUp();
		$this->setMwGlobals( [
			'wgExtensionMessagesFiles' => [],
			'wgHooks' => [],
		] );
	}

	/**
	 * @return LocalisationCache
	 */
	protected function getMockLocalisationCache() {
		global $IP;

		$mockLangNameUtils = $this->createMock( LanguageNameUtils::class );
		$mockLangNameUtils->method( 'isValidBuiltInCode' )->will( $this->returnCallback(
			function ( $code ) {
				// Copy-paste, but it's only one line
				return (bool)preg_match( '/^[a-z0-9-]{2,}$/', $code );
			}
		) );
		$mockLangNameUtils->method( 'isSupportedLanguage' )->will( $this->returnCallback(
			function ( $code ) {
				return in_array( $code, [
					'ar',
					'arz',
					'ba',
					'de',
					'en',
					'ksh',
					'ru',
				] );
			}
		) );
		$mockLangNameUtils->method( 'getMessagesFileName' )->will( $this->returnCallback(
			function ( $code ) {
				global $IP;
				$code = str_replace( '-', '_', ucfirst( $code ) );
				return "$IP/languages/messages/Messages$code.php";
			}
		) );
		$mockLangNameUtils->expects( $this->never() )->method( $this->anythingBut(
			'isValidBuiltInCode', 'isSupportedLanguage', 'getMessagesFileName'
		) );

		$lc = $this->getMockBuilder( LocalisationCache::class )
			->setConstructorArgs( [
				new ServiceOptions( LocalisationCache::CONSTRUCTOR_OPTIONS, [
					'forceRecache' => false,
					'manualRecache' => false,
					'ExtensionMessagesFiles' => [],
					'MessagesDirs' => [],
				] ),
				new LCStoreDB( [] ),
				new NullLogger,
				[],
				$mockLangNameUtils
			] )
			->setMethods( [ 'getMessagesDirs' ] )
			->getMock();
		$lc->expects( $this->any() )->method( 'getMessagesDirs' )
			->will( $this->returnValue(
				[ "$IP/tests/phpunit/data/localisationcache" ]
			) );

		return $lc;
	}

	public function testPluralRulesFallback() {
		$cache = $this->getMockLocalisationCache();

		$this->assertEquals(
			$cache->getItem( 'ar', 'pluralRules' ),
			$cache->getItem( 'arz', 'pluralRules' ),
			'arz plural rules (undefined) fallback to ar (defined)'
		);

		$this->assertEquals(
			$cache->getItem( 'ar', 'compiledPluralRules' ),
			$cache->getItem( 'arz', 'compiledPluralRules' ),
			'arz compiled plural rules (undefined) fallback to ar (defined)'
		);

		$this->assertNotEquals(
			$cache->getItem( 'ksh', 'pluralRules' ),
			$cache->getItem( 'de', 'pluralRules' ),
			'ksh plural rules (defined) dont fallback to de (defined)'
		);

		$this->assertNotEquals(
			$cache->getItem( 'ksh', 'compiledPluralRules' ),
			$cache->getItem( 'de', 'compiledPluralRules' ),
			'ksh compiled plural rules (defined) dont fallback to de (defined)'
		);
	}

	public function testRecacheFallbacks() {
		$lc = $this->getMockLocalisationCache();
		$lc->recache( 'ba' );
		$this->assertEquals(
			[
				'present-ba' => 'ba',
				'present-ru' => 'ru',
				'present-en' => 'en',
			],
			$lc->getItem( 'ba', 'messages' ),
			'Fallbacks are only used to fill missing data'
		);
	}

	public function testRecacheFallbacksWithHooks() {
		// Use hook to provide updates for messages. This is what the
		// LocalisationUpdate extension does. See T70781.
		$this->mergeMwGlobalArrayValue( 'wgHooks', [
			'LocalisationCacheRecacheFallback' => [
				function (
					LocalisationCache $lc,
					$code,
					array &$cache
				) {
					if ( $code === 'ru' ) {
						$cache['messages']['present-ba'] = 'ru-override';
						$cache['messages']['present-ru'] = 'ru-override';
						$cache['messages']['present-en'] = 'ru-override';
					}
				}
			]
		] );

		$lc = $this->getMockLocalisationCache();
		$lc->recache( 'ba' );
		$this->assertEquals(
			[
				'present-ba' => 'ba',
				'present-ru' => 'ru-override',
				'present-en' => 'ru-override',
			],
			$lc->getItem( 'ba', 'messages' ),
			'Updates provided by hooks follow the normal fallback order.'
		);
	}
}