-
Notifications
You must be signed in to change notification settings - Fork 59
/
DatabaseInnerTermStoreCleanerTest.php
341 lines (286 loc) 路 11.2 KB
/
DatabaseInnerTermStoreCleanerTest.php
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
declare( strict_types=1 );
namespace Wikibase\Lib\Tests\Store\Sql\Terms;
use MediaWikiIntegrationTestCase;
use Wikibase\Lib\Store\Sql\Terms\DatabaseInnerTermStoreCleaner;
use Wikibase\Lib\WikibaseSettings;
/**
* @covers \Wikibase\Lib\Store\Sql\Terms\DatabaseInnerTermStoreCleaner
*
* @group Wikibase
* @group Database
*
* @license GPL-2.0-or-later
*/
class DatabaseInnerTermStoreCleanerTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
if ( !WikibaseSettings::isRepoEnabled() ) {
$this->markTestSkipped( "Skipping because WikibaseClient doesn't have local term store tables." );
}
parent::setUp();
}
private function getCleaner(): DatabaseInnerTermStoreCleaner {
return new DatabaseInnerTermStoreCleaner();
}
public function testCleanupEverything() {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'label' ] )
->caller( __METHOD__ )
->execute();
$typeId = $this->db->insertId();
[ $text1Id, $text2Id ] = $this->insertTexts( [ 'a label', 'eine Bezeichnung' ] );
[ $textInLang1Id, $textInLang2Id ] = $this->insertTextsInLang( [ $text1Id => 'en', $text2Id => 'de' ] );
[ $termInLang1Id, $termInLang2Id ] = $this->insertTermsInLang( [ $textInLang1Id => $typeId, $textInLang2Id => $typeId ] );
$this->getCleaner()->cleanTermInLangIds( $this->db, $this->db, [ $termInLang1Id, $termInLang2Id ] );
$this->newSelectQueryBuilder()
->select( 'wbx_id' )
->from( 'wbt_text' )
->assertEmptyResult();
$this->newSelectQueryBuilder()
->select( 'wbxl_id' )
->from( 'wbt_text_in_lang' )
->assertEmptyResult();
$this->newSelectQueryBuilder()
->select( 'wbtl_id' )
->from( 'wbt_term_in_lang' )
->assertEmptyResult();
$this->newSelectQueryBuilder()
->select( 'wby_name' )
->from( 'wbt_type' )
->assertFieldValue( 'label' );
}
public function testCleanupTermInLangButNoTextInLang() {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'label' ] )
->caller( __METHOD__ )
->execute();
$type1Id = $this->db->insertId();
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'description' ] )
->caller( __METHOD__ )
->execute();
$type2Id = $this->db->insertId();
// insert two texts into wbt_text
[ $text1Id, $text2Id ] = $this->insertTexts( [ 'some text', 'etwas Text' ] );
// insert into wbt_text_in_lang
[ $textInLang1Id, $textInLang2Id ] = $this->insertTextsInLang( [ $text1Id => 'en', $text2Id => 'de' ] );
// both texts are label & description in wbt_term_in_lang
[ $termInLang1Id, $termInLang2Id, $termInLang3Id, $termInLang4Id ] = $this->insertTermsInLang(
[
$textInLang1Id => [ $type1Id, $type2Id ],
$textInLang2Id => [ $type1Id, $type2Id ],
]
);
// remove the first and the last one
$this->getCleaner()->cleanTermInLangIds( $this->db, $this->db, [ $termInLang1Id, $termInLang4Id ] );
// The two initial inserts remain
$this->assertTextTableReturns( [ $text1Id, $text2Id ] );
$this->assertTextInLangTableReturns( [ $textInLang1Id, $textInLang2Id ] );
// the first and the last is removed from wbt_term_in_lang
$this->assertTermInLangTableReturns( [ $termInLang2Id, $termInLang3Id ] );
}
public function testCleanupOneTextInLangButNoText() {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'label' ] )
->caller( __METHOD__ )
->execute();
$typeId = $this->db->insertId();
// insert two texts into wbt_text
[ $text1Id, $text2Id ] = $this->insertTexts( [ 'text', 'Text' ] );
// insert into wbt_text_in_lang
[ $textInLang1Id, $textInLang2Id ] = $this->insertTextsInLang( [ $text1Id => 'en', $text2Id => 'de' ] );
// text1 has one additional
[ $textInLang3Id ] = $this->insertTextsInLang( [ $text1Id => 'fr' ] );
// all terms are the same type
[ $termInLang1Id, $termInLang2Id, $termInLang3Id ] = $this->insertTermsInLang(
[
$textInLang1Id => $typeId,
$textInLang2Id => $typeId,
$textInLang3Id => $typeId,
]
);
// remove term_in_lang with 'en' language
$this->getCleaner()->cleanTermInLangIds( $this->db, $this->db, [ $termInLang1Id ] );
// $textInLang1Id and $termInLang1Id gone,
$this->assertTextInLangTableReturns( [ $textInLang2Id, $textInLang3Id ] );
$this->assertTermInLangTableReturns( [ $termInLang2Id, $termInLang3Id ] );
// but $text1Id is still there because referenced by $termInLang3Id
$this->assertTextTableReturns( [ $text1Id, $text2Id ] );
}
public function testCleanupOneText() {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'label' ] )
->caller( __METHOD__ )
->execute();
$typeId = $this->db->insertId();
// insert two texts into wbt_text
[ $text1Id, $text2Id ] = $this->insertTexts( [ 'text', 'Text' ] );
// insert into wbt_text_in_lang and term_in_lang
[ $textInLang1Id, $textInLang2Id ] = $this->insertTextsInLang( [ $text1Id => 'en', $text2Id => 'de' ] );
[ $termInLang1Id, $termInLang2Id ] = $this->insertTermsInLang( [ $textInLang1Id => $typeId, $textInLang2Id => $typeId ] );
$this->getCleaner()->cleanTermInLangIds( $this->db, $this->db, [ $termInLang1Id ] );
// $textId1, $textInLang1Id and $termInLang1Id gone
$this->assertTextTableReturns( [ $text2Id ] );
$this->assertTextInLangTableReturns( [ $textInLang2Id ] );
$this->assertTermInLangTableReturns( [ $termInLang2Id ] );
}
public function testCleanupLeavesUnrelatedTextsUntouched() {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'label' ] )
->caller( __METHOD__ )
->execute();
$typeId = $this->db->insertId();
[ $text1Id, $text2Id ] = $this->insertTexts( [ 'a label', 'eine Bezeichnung' ] );
[ $textInLang1Id, $textInLang2Id ] = $this->insertTextsInLang( [ $text1Id => 'en', $text2Id => 'de' ] );
[ $termInLang1Id ] = $this->insertTermsInLang( [ $textInLang1Id => $typeId ] );
// remove the first
$this->getCleaner()->cleanTermInLangIds( $this->db, $this->db, [ $termInLang1Id ] );
// $text2Id and $textInLang2Id are not used by any term_in_lang,
// but we should not attempt to clean them up
$this->assertTextTableReturns( [ $text2Id ] );
$this->assertTextInLangTableReturns( [ $textInLang2Id ] );
$this->assertTermInLangTableReturns( [] );
}
public function testT237984_sharedTextInLangIdsAreNotDeleted() {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'label' ] )
->caller( __METHOD__ )
->execute();
$typeIdLabel = $this->db->insertId();
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'description' ] )
->caller( __METHOD__ )
->execute();
$typeIdDescription = $this->db->insertId();
[ $textId ] = $this->insertTexts( [ 'someText' ] );
[ $textInLangIdSingleUse1 ] = $this->insertTextsInLang( [ $textId => 'en' ] );
[ $textInLangIdSingleUse2 ] = $this->insertTextsInLang( [ $textId => 'de' ] );
[ $textInLangIdShared ] = $this->insertTextsInLang( [ $textId => 'fr' ] );
[ $termInLangIdToDelete1, $termInLangIdToDelete2, $termInLangIdToDelete3, $termInLangIdToRemain ] = $this->insertTermsInLang(
[
$textInLangIdSingleUse1 => $typeIdLabel,
$textInLangIdSingleUse2 => $typeIdLabel,
$textInLangIdShared => [ $typeIdLabel, $typeIdDescription ],
]
);
$this->getCleaner()->cleanTermInLangIds( $this->db, $this->db, [
$termInLangIdToDelete1,
$termInLangIdToDelete2,
$termInLangIdToDelete3,
] );
$this->assertTextTableReturns( [ $textId ] );
$this->assertTermInLangTableReturns( [ $termInLangIdToRemain ] );
// This row should not be deleted, as it is still used by $termInLangIdToRemain
$this->assertTextInLangTableReturns( [ $textInLangIdShared ] );
}
public function testT237984_sharedTextIdsAreNotDeleted() {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_type' )
->row( [ 'wby_name' => 'label' ] )
->caller( __METHOD__ )
->execute();
$typeIdLabel = $this->db->insertId();
[ $textIdSingleUse, $textIdShared ] = $this->insertTexts( [ 'someText1', 'someText2' ] );
// insert a language for each, and one additional for the shared
[ $textInLangIdToDelete1, $textInLangIdToDelete2 ] = $this->insertTextsInLang(
[
$textIdSingleUse => 'en',
$textIdShared => 'de',
]
);
[ $textInLangIdToRemain3 ] = $this->insertTextsInLang( [ $textIdShared => 'fr' ] );
[ $termInLangIdToDelete1, $termInLangIdToDelete2, $termInLangIdToRemain3 ] = $this->insertTermsInLang(
[
$textInLangIdToDelete1 => $typeIdLabel,
$textInLangIdToDelete2 => $typeIdLabel,
$textInLangIdToRemain3 => $typeIdLabel,
]
);
$this->getCleaner()->cleanTermInLangIds( $this->db, $this->db, [ $termInLangIdToDelete1, $termInLangIdToDelete2 ] );
// This row should not be deleted, as it is still used by $textIdShared
$this->assertTermInLangTableReturns( [ $termInLangIdToRemain3 ] );
$this->assertTextTableReturns( [ $textIdShared ] );
}
private function assertTableReturns( array $elements, string $table, string $field ) {
$this->newSelectQueryBuilder()
->select( $field )
->from( $table )
->assertResultSet( array_map(
function( $element ) {
return [ $element ];
},
$elements
) );
}
private function assertTextTableReturns( array $elements ) {
$this->assertTableReturns( $elements, 'wbt_text', 'wbx_id' );
}
private function assertTextInLangTableReturns( array $elements ) {
$this->assertTableReturns( $elements, 'wbt_text_in_lang', 'wbxl_id' );
}
private function assertTermInLangTableReturns( array $elements ) {
$this->assertTableReturns( $elements, 'wbt_term_in_lang', 'wbtl_id' );
}
/**
* @param string[] $texts list of texts
* @return int[] text IDs in the same order
*/
private function insertTexts( array $texts ): array {
$ids = [];
foreach ( $texts as $text ) {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_text' )
->row( [ 'wbx_text' => $text ] )
->caller( __METHOD__ )
->execute();
$ids[] = $this->db->insertId();
}
return $ids;
}
/**
* @param string[] $textIds mapping from text ID to language code
* @return int[] text_in_lang IDs in the same order
*/
private function insertTextsInLang( array $textIds ): array {
$ids = [];
foreach ( $textIds as $textId => $language ) {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_text_in_lang' )
->row( [ 'wbxl_language' => $language, 'wbxl_text_id' => $textId ] )
->caller( __METHOD__ )
->execute();
$ids[] = $this->db->insertId();
}
return $ids;
}
/**
* @param (int|int[])[] $textInLangIds mapping from text_in_lang ID to type ID(s)
* (to add several terms with the same text_in_lang ID, call the method multiple times)
* @return int[] term_in_lang IDs in the same order
*/
private function insertTermsInLang( array $textInLangIds ): array {
$ids = [];
foreach ( $textInLangIds as $textInLangId => $typeId ) {
if ( !is_array( $typeId ) ) {
$typeId = [ $typeId ];
}
foreach ( $typeId as $id ) {
$this->db->newInsertQueryBuilder()
->insertInto( 'wbt_term_in_lang' )
->row( [ 'wbtl_type_id' => $id, 'wbtl_text_in_lang_id' => $textInLangId ] )
->caller( __METHOD__ )
->execute();
$ids[] = $this->db->insertId();
}
}
return $ids;
}
}