assertRepositorySettingsEqual()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Wikibase\Tests;
4
5
use MediaWikiIntegrationTestCase;
6
use MWNamespace;
7
use Wikibase\Lib\SettingsArray;
8
use Wikibase\Lib\WikibaseSettings;
9
10
/**
11
 * @group Wikibase
12
 * @group WikibaseClient
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Daniel Kinzler
16
 */
17
class ClientDefaultsTest extends MediaWikiIntegrationTestCase {
18
19
	public function settingsProvider() {
20
		$cases = [
21
			[ // #0: no local repo, all values set
22
				[ // $settings
23
					'repoUrl' => 'http://acme.com',
24
					'repoArticlePath' => '/wiki',
25
					'repoScriptPath' => '/w',
26
					'siteGlobalID' => 'mywiki',
27
					'repoDatabase' => 'foo',
28
					'changesDatabase' => 'doo',
29
					'sharedCacheKeyPrefix' => 'wikibase_shared/',
30
				],
31
				[ // $wg
32
					'wgServer' => 'http://www.acme.com',
33
					'wgArticlePath' => '/mywiki',
34
					'wgScriptPath' => '/mediawiki',
35
					'wgDBname' => 'mw_mywiki',
36
				],
37
				false, // $repoIsLocal
38
				[ // $expected
39
					'repoUrl' => 'http://acme.com',
40
					'repoArticlePath' => '/wiki',
41
					'repoScriptPath' => '/w',
42
					'siteGlobalID' => 'mywiki',
43
					'repoDatabase' => 'foo',
44
					'changesDatabase' => 'doo',
45
					'sharedCacheKeyPrefix' => 'wikibase_shared/',
46
				]
47
			],
48
49
			[ // #1: no local repo, no values set
50
				[ // $settings
51
				],
52
				[ // $wg
53
					'wgServer' => 'http://www.acme.com',
54
					'wgArticlePath' => '/mywiki',
55
					'wgScriptPath' => '/mediawiki',
56
					'wgDBname' => 'mw_mywiki',
57
				],
58
				false, // $repoIsLocal
59
				[ // $expected
60
					'repoUrl' => '//www.wikidata.org',   // hardcoded default
61
					'repoArticlePath' => '/wiki/$1', // hardcoded default
62
					'repoScriptPath' => '/w', // hardcoded default
63
					'siteGlobalID' => 'mw_mywiki',
64
					'repositories' => [
65
						'' => [
66
							'repoDatabase' => null,
67
							'baseUri' => '//www.wikidata.org/entity/',
68
							'entityNamespaces' => [
69
								'item' => 0,
70
								'property' => 120,
71
							],
72
						],
73
					],
74
					'changesDatabase' => null,
75
					'sharedCacheKeyPrefix' => 'wikibase_shared/mw_mywiki',
76
				]
77
			],
78
79
			[ // #2: local repo, all values set
80
				[ // $settings
81
					'repoUrl' => 'http://acme.com',
82
					'repoArticlePath' => '/wiki',
83
					'repoScriptPath' => '/w',
84
					'siteGlobalID' => 'mywiki',
85
					'repoDatabase' => 'foo',
86
					'changesDatabase' => 'doo',
87
					'sharedCacheKeyPrefix' => 'foo:WBL/',
88
				],
89
				[ // $wg
90
					'wgServer' => 'http://www.acme.com',
91
					'wgArticlePath' => '/mywiki',
92
					'wgScriptPath' => '/mediawiki',
93
					'wgDBname' => 'mw_mywiki',
94
				],
95
				true, // $repoIsLocal
96
				[ // $expected
97
					'repoUrl' => 'http://acme.com',
98
					'repoArticlePath' => '/wiki',
99
					'repoScriptPath' => '/w',
100
					'siteGlobalID' => 'mywiki',
101
					'repoDatabase' => 'foo',
102
					'changesDatabase' => 'doo',
103
					'sharedCacheKeyPrefix' => 'foo:WBL/',
104
				]
105
			],
106
		];
107
108
		if ( WikibaseSettings::isRepoEnabled() ) {
109
			$cases[] = [ // #3: local repo, no values set
110
				[ // $settings
111
				],
112
				[ // $wg
113
					'wgServer' => 'http://www.acme.com',
114
					'wgArticlePath' => '/mywiki',
115
					'wgScriptPath' => '/mediawiki',
116
					'wgDBname' => 'mw_mywiki',
117
					'wgWBRepoSettings' => [
118
						'entityNamespaces' => [ 'item' => 303 ],
119
					],
120
				],
121
				true, // $repoIsLocal
122
				[ // $expected
123
					'repoUrl' => 'http://www.acme.com',
124
					'repoArticlePath' => '/mywiki',
125
					'repoScriptPath' => '/mediawiki',
126
					'siteGlobalID' => 'mw_mywiki',
127
					'repositories' => [
128
						'' => [
129
							'repoDatabase' => false,
130
							'baseUri' => 'http://www.acme.com/entity/',
131
						],
132
					],
133
					'changesDatabase' => false,
134
					'sharedCacheKeyPrefix' => 'wikibase_shared/mw_mywiki',
135
				]
136
			];
137
		}
138
139
		$cases[] = [ // #4: derive changesDatabase
140
			[ // $settings
141
				'repositories' => [
142
					'' => [
143
						'repoDatabase' => 'mw_foowiki'
144
					],
145
				],
146
			],
147
			[ // $wg
148
			],
149
			false, // $repoIsLocal
150
			[ // $expected
151
				'changesDatabase' => 'mw_foowiki',
152
			]
153
		];
154
155
		if ( WikibaseSettings::isRepoEnabled() ) {
156
			$cases[] = [ // #5: sharedCacheKeyPrefix explicitly set
157
				[ // $settings
158
					'sharedCacheKeyPrefix' => 'wikibase_shared/wikidata_1_25wmf24'
159
				],
160
				[ // $wg
161
					'wgServer' => 'http://www.acme.com',
162
					'wgArticlePath' => '/mywiki',
163
					'wgScriptPath' => '/mediawiki',
164
					'wgDBname' => 'mw_mywiki',
165
					'wgWBRepoSettings' => [ 'entityNamespaces' => [ 'item' => 303 ] ],
166
				],
167
				true, // $repoIsLocal
168
				[ // $expected
169
					'repoUrl' => 'http://www.acme.com',
170
					'repoArticlePath' => '/mywiki',
171
					'repoScriptPath' => '/mediawiki',
172
					'siteGlobalID' => 'mw_mywiki',
173
					'changesDatabase' => false,
174
					'sharedCacheKeyPrefix' => 'wikibase_shared/wikidata_1_25wmf24',
175
				]
176
			];
177
		}
178
179
		$cases[] = [ // #6: derive repoNamespaces and entityNamespaces
180
			[ // $settings
181
			],
182
			[ // $wg
183
			],
184
			false, // $repoIsLocal
185
			[ // $expected
186
				'repoNamespaces' => [ 'item' => '', 'property' => 'Property' ],
187
				'repositories' => [
188
					'' => [
189
						'entityNamespaces' => [ 'item' => 0, 'property' => 120 ],
190
					],
191
				],
192
			]
193
		];
194
195
		if ( WikibaseSettings::isRepoEnabled() ) {
196
			$repoSettings = WikibaseSettings::getRepoSettings();
197
			$entityNamespaces = $repoSettings->getSetting( 'entityNamespaces' );
198
			$namespaceNames = array_map( [ MWNamespace::class, 'getCanonicalName' ], $entityNamespaces );
199
200
			$cases[] = [ // #7: default repoNamespaces and entityNamespaces
201
				[], // $settings
202
				[], // $wg
203
				true, // $repoIsLocal
204
				[ // $expected
205
					'repoNamespaces' => $namespaceNames,
206
					'repositories' => [
207
						'' => [
208
							'entityNamespaces' => $entityNamespaces,
209
						],
210
					],
211
				]
212
			];
213
		}
214
215
		return $cases;
216
	}
217
218
	/**
219
	 * @dataProvider settingsProvider
220
	 */
221
	public function testDefaults( array $settings, array $wg, $repoIsLocal, $expected ) {
222
		$this->markTestSkipped( 'flaky, see T214761' );
223
		$this->setMwGlobals( $wg );
224
225
		$defaults = require __DIR__ . '/../../config/WikibaseClient.default.php';
226
227
		$settings = array_merge( $defaults, $settings );
228
		$settings = new SettingsArray( $settings );
229
230
		//NOTE: thisWikiIsTheRepo is used by some "magic" (dynamic) defaults
231
		//      to decide how to behave. Normally, this is true if and only if
232
		//      the WikibaseRepo extension is loaded.
233
		$settings->setSetting( 'thisWikiIsTheRepo', $repoIsLocal );
0 ignored issues
show
Unused Code introduced by
The call to the method Wikibase\Lib\SettingsArray::setSetting() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
234
235
		foreach ( $expected as $key => $exp ) {
236
			$actual = $settings->getSetting( $key );
237
238
			if ( $key === 'repositories' ) {
239
				$this->assertRepositorySettingsEqual( $exp, $actual );
240
				continue;
241
			}
242
243
			$this->assertSame( $exp, $actual, "Setting $key" );
244
		}
245
	}
246
247
	private function assertRepositorySettingsEqual( $expected, $actual ) {
248
		foreach ( $expected as $repoName => $expectedRepoSettings ) {
249
			$actualToCompare = array_intersect_key( $actual[$repoName], $expectedRepoSettings );
250
			$this->assertSame( $expectedRepoSettings, $actualToCompare );
251
		}
252
	}
253
254
}
255