1
|
|
|
<?php // phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols -- see evil hack below |
2
|
|
|
|
3
|
|
|
namespace Wikibase\TermStore\MediaWiki\Tests\Unit\MediaWikiDependent; |
4
|
|
|
|
5
|
|
|
use MediaWikiTestCase; |
6
|
|
|
use Wikibase\TermStore\MediaWiki\PackagePrivate\DatabaseTermCleaner; |
7
|
|
|
use Wikibase\TermStore\MediaWiki\TermStoreSchemaUpdater; |
8
|
|
|
use Wikibase\TermStore\MediaWiki\Tests\Util\FakeLoadBalancer; |
9
|
|
|
use Wikimedia\Rdbms\ILoadBalancer; |
10
|
|
|
use Wikimedia\Rdbms\IMaintainableDatabase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers \Wikibase\TermStore\MediaWiki\PackagePrivate\DatabaseTermCleaner |
14
|
|
|
* |
15
|
|
|
* @group Database |
16
|
|
|
* |
17
|
|
|
* @license GPL-2.0-or-later |
18
|
|
|
*/ |
19
|
|
|
class DatabaseTermCleanerTest extends MediaWikiTestCase { |
20
|
|
|
|
21
|
|
|
protected function setUp() { |
22
|
|
|
parent::setUp(); |
23
|
|
|
$this->tablesUsed[] = 'wbt_type'; |
24
|
|
|
$this->tablesUsed[] = 'wbt_text'; |
25
|
|
|
$this->tablesUsed[] = 'wbt_text_in_lang'; |
26
|
|
|
$this->tablesUsed[] = 'wbt_term_in_lang'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function getSchemaOverrides( IMaintainableDatabase $db ) { |
30
|
|
|
return [ |
31
|
|
|
'scripts' => [ TermStoreSchemaUpdater::getSqlFileAbsolutePath() ], |
32
|
|
|
'create' => [ |
33
|
|
|
'wbt_type', |
34
|
|
|
'wbt_text', |
35
|
|
|
'wbt_text_in_lang', |
36
|
|
|
'wbt_term_in_lang', |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function getCleaner(): DatabaseTermCleaner { |
42
|
|
|
return new DatabaseTermCleaner( new FakeLoadBalancer( [ |
43
|
|
|
'dbr' => $this->db, |
44
|
|
|
] ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCleanupEverything() { |
48
|
|
|
$this->db->insert( 'wbt_type', |
49
|
|
|
[ 'wby_name' => 'label' ] ); |
50
|
|
|
$typeId = $this->db->insertId(); |
51
|
|
|
$this->db->insert( 'wbt_text', |
52
|
|
|
[ 'wbx_text' => 'a label' ] ); |
53
|
|
|
$text1Id = $this->db->insertId(); |
54
|
|
|
$this->db->insert( 'wbt_text', |
55
|
|
|
[ 'wbx_text' => 'eine Bezeichnung' ] ); |
56
|
|
|
$text2Id = $this->db->insertId(); |
57
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
58
|
|
|
[ 'wbxl_language' => 'en', 'wbxl_text_id' => $text1Id ] ); |
59
|
|
|
$textInLang1Id = $this->db->insertId(); |
60
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
61
|
|
|
[ 'wbxl_language' => 'de', 'wbxl_text_id' => $text2Id ] ); |
62
|
|
|
$textInLang2Id = $this->db->insertId(); |
63
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
64
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
65
|
|
|
$termInLang1Id = $this->db->insertId(); |
66
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
67
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
68
|
|
|
$termInLang2Id = $this->db->insertId(); |
69
|
|
|
|
70
|
|
|
$this->getCleaner()->cleanTerms( [ $termInLang1Id, $termInLang2Id ] ); |
71
|
|
|
|
72
|
|
|
$this->assertSelect( 'wbt_text', 'wbx_id', '*', [] ); |
73
|
|
|
$this->assertSelect( 'wbt_text_in_lang', 'wbxl_id', '*', [] ); |
74
|
|
|
$this->assertSelect( 'wbt_term_in_lang', 'wbtl_id', '*', [] ); |
75
|
|
|
$this->assertSelect( 'wbt_type', 'wby_name', '*', [ [ 'label' ] ] ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testCleanupTermInLangButNoTextInLang() { |
79
|
|
|
$this->db->insert( 'wbt_type', |
80
|
|
|
[ 'wby_name' => 'label' ] ); |
81
|
|
|
$type1Id = $this->db->insertId(); |
82
|
|
|
$this->db->insert( 'wbt_type', |
83
|
|
|
[ 'wby_name' => 'description' ] ); |
84
|
|
|
$type2Id = $this->db->insertId(); |
85
|
|
|
$this->db->insert( 'wbt_text', |
86
|
|
|
[ 'wbx_text' => 'some text' ] ); |
87
|
|
|
$text1Id = $this->db->insertId(); |
88
|
|
|
$this->db->insert( 'wbt_text', |
89
|
|
|
[ 'wbx_text' => 'etwas Text' ] ); |
90
|
|
|
$text2Id = $this->db->insertId(); |
91
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
92
|
|
|
[ 'wbxl_language' => 'en', 'wbxl_text_id' => $text1Id ] ); |
93
|
|
|
$textInLang1Id = $this->db->insertId(); |
94
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
95
|
|
|
[ 'wbxl_language' => 'de', 'wbxl_text_id' => $text2Id ] ); |
96
|
|
|
$textInLang2Id = $this->db->insertId(); |
97
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
98
|
|
|
[ 'wbtl_type_id' => $type1Id, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
99
|
|
|
$termInLang1Id = $this->db->insertId(); |
100
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
101
|
|
|
[ 'wbtl_type_id' => $type2Id, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
102
|
|
|
$termInLang2Id = $this->db->insertId(); |
103
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
104
|
|
|
[ 'wbtl_type_id' => $type1Id, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
105
|
|
|
$termInLang3Id = $this->db->insertId(); |
106
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
107
|
|
|
[ 'wbtl_type_id' => $type2Id, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
108
|
|
|
$termInLang4Id = $this->db->insertId(); |
109
|
|
|
|
110
|
|
|
$this->getCleaner()->cleanTerms( [ $termInLang1Id, $termInLang4Id ] ); |
111
|
|
|
|
112
|
|
|
$this->assertSelect( |
113
|
|
|
'wbt_text', |
114
|
|
|
'wbx_id', |
115
|
|
|
'*', |
116
|
|
|
[ [ $text1Id ], [ $text2Id ] ], |
117
|
|
|
[ 'ORDER BY' => 'wbx_id' ] |
118
|
|
|
); |
119
|
|
|
$this->assertSelect( |
120
|
|
|
'wbt_text_in_lang', |
121
|
|
|
'wbxl_id', |
122
|
|
|
'*', |
123
|
|
|
[ [ $textInLang1Id ], [ $textInLang2Id ] ], |
124
|
|
|
[ 'ORDER BY' => 'wbxl_id' ] |
125
|
|
|
); |
126
|
|
|
$this->assertSelect( |
127
|
|
|
'wbt_term_in_lang', |
128
|
|
|
'wbtl_id', |
129
|
|
|
'*', |
130
|
|
|
[ [ $termInLang2Id ], [ $termInLang3Id ] ], |
131
|
|
|
[ 'ORDER BY' => 'wbtl_id' ] |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testCleanupOneTextInLangButNoText() { |
136
|
|
|
$this->db->insert( 'wbt_type', |
137
|
|
|
[ 'wby_name' => 'label' ] ); |
138
|
|
|
$typeId = $this->db->insertId(); |
139
|
|
|
$this->db->insert( 'wbt_text', |
140
|
|
|
[ 'wbx_text' => 'text' ] ); |
141
|
|
|
$text1Id = $this->db->insertId(); |
142
|
|
|
$this->db->insert( 'wbt_text', |
143
|
|
|
[ 'wbx_text' => 'Text' ] ); |
144
|
|
|
$text2Id = $this->db->insertId(); |
145
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
146
|
|
|
[ 'wbxl_language' => 'en', 'wbxl_text_id' => $text1Id ] ); |
147
|
|
|
$textInLang1Id = $this->db->insertId(); |
148
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
149
|
|
|
[ 'wbxl_language' => 'de', 'wbxl_text_id' => $text2Id ] ); |
150
|
|
|
$textInLang2Id = $this->db->insertId(); |
151
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
152
|
|
|
[ 'wbxl_language' => 'fr', 'wbxl_text_id' => $text1Id ] ); |
153
|
|
|
$textInLang3Id = $this->db->insertId(); |
154
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
155
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
156
|
|
|
$termInLang1Id = $this->db->insertId(); |
157
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
158
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
159
|
|
|
$termInLang2Id = $this->db->insertId(); |
160
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
161
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang3Id ] ); |
162
|
|
|
$termInLang3Id = $this->db->insertId(); |
163
|
|
|
|
164
|
|
|
$this->getCleaner()->cleanTerms( [ $termInLang1Id ] ); |
165
|
|
|
|
166
|
|
|
// $textInLang1Id and $termInLang1Id gone, |
167
|
|
|
// but $text1Id still there because referenced by $termInLang3Id |
168
|
|
|
$this->assertSelect( |
169
|
|
|
'wbt_text', |
170
|
|
|
'wbx_id', |
171
|
|
|
'*', |
172
|
|
|
[ [ $text1Id ], [ $text2Id ] ], |
173
|
|
|
[ 'ORDER BY' => 'wbx_id' ] |
174
|
|
|
); |
175
|
|
|
$this->assertSelect( |
176
|
|
|
'wbt_text_in_lang', |
177
|
|
|
'wbxl_id', |
178
|
|
|
'*', |
179
|
|
|
[ [ $textInLang2Id ], [ $textInLang3Id ] ], |
180
|
|
|
[ 'ORDER BY' => 'wbxl_id' ] |
181
|
|
|
); |
182
|
|
|
$this->assertSelect( |
183
|
|
|
'wbt_term_in_lang', |
184
|
|
|
'wbtl_id', |
185
|
|
|
'*', |
186
|
|
|
[ [ $termInLang2Id ], [ $termInLang3Id ] ], |
187
|
|
|
[ 'ORDER BY' => 'wbtl_id' ] |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testCleanupOneText() { |
192
|
|
|
$this->db->insert( 'wbt_type', |
193
|
|
|
[ 'wby_name' => 'label' ] ); |
194
|
|
|
$typeId = $this->db->insertId(); |
195
|
|
|
$this->db->insert( 'wbt_text', |
196
|
|
|
[ 'wbx_text' => 'text' ] ); |
197
|
|
|
$text1Id = $this->db->insertId(); |
198
|
|
|
$this->db->insert( 'wbt_text', |
199
|
|
|
[ 'wbx_text' => 'Text' ] ); |
200
|
|
|
$text2Id = $this->db->insertId(); |
201
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
202
|
|
|
[ 'wbxl_language' => 'en', 'wbxl_text_id' => $text1Id ] ); |
203
|
|
|
$textInLang1Id = $this->db->insertId(); |
204
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
205
|
|
|
[ 'wbxl_language' => 'de', 'wbxl_text_id' => $text2Id ] ); |
206
|
|
|
$textInLang2Id = $this->db->insertId(); |
207
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
208
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
209
|
|
|
$termInLang1Id = $this->db->insertId(); |
210
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
211
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
212
|
|
|
$termInLang2Id = $this->db->insertId(); |
213
|
|
|
|
214
|
|
|
$this->getCleaner()->cleanTerms( [ $termInLang1Id ] ); |
215
|
|
|
|
216
|
|
|
// $textId1, $textInLang1Id and $termInLang1Id gone |
217
|
|
|
$this->assertSelect( |
218
|
|
|
'wbt_text', |
219
|
|
|
'wbx_id', |
220
|
|
|
'*', |
221
|
|
|
[ [ $text2Id ] ] |
222
|
|
|
); |
223
|
|
|
$this->assertSelect( |
224
|
|
|
'wbt_text_in_lang', |
225
|
|
|
'wbxl_id', |
226
|
|
|
'*', |
227
|
|
|
[ [ $textInLang2Id ] ] |
228
|
|
|
); |
229
|
|
|
$this->assertSelect( |
230
|
|
|
'wbt_term_in_lang', |
231
|
|
|
'wbtl_id', |
232
|
|
|
'*', |
233
|
|
|
[ [ $termInLang2Id ] ] |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testCleanupLeavesUnrelatedTextsUntouched() { |
238
|
|
|
$this->db->insert( 'wbt_type', |
239
|
|
|
[ 'wby_name' => 'label' ] ); |
240
|
|
|
$typeId = $this->db->insertId(); |
241
|
|
|
$this->db->insert( 'wbt_text', |
242
|
|
|
[ 'wbx_text' => 'a label' ] ); |
243
|
|
|
$text1Id = $this->db->insertId(); |
244
|
|
|
$this->db->insert( 'wbt_text', |
245
|
|
|
[ 'wbx_text' => 'eine Bezeichnung' ] ); |
246
|
|
|
$text2Id = $this->db->insertId(); |
247
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
248
|
|
|
[ 'wbxl_language' => 'en', 'wbxl_text_id' => $text1Id ] ); |
249
|
|
|
$textInLang1Id = $this->db->insertId(); |
250
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
251
|
|
|
[ 'wbxl_language' => 'de', 'wbxl_text_id' => $text2Id ] ); |
252
|
|
|
$textInLang2Id = $this->db->insertId(); |
253
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
254
|
|
|
[ 'wbtl_type_id' => $typeId, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
255
|
|
|
$termInLang1Id = $this->db->insertId(); |
256
|
|
|
|
257
|
|
|
$this->getCleaner()->cleanTerms( [ $termInLang1Id ] ); |
258
|
|
|
|
259
|
|
|
// $text2Id and $textInLang2Id are not used by any term_in_lang, |
260
|
|
|
// but we should not attempt to clean them up |
261
|
|
|
$this->assertSelect( |
262
|
|
|
'wbt_text', |
263
|
|
|
'wbx_id', |
264
|
|
|
'*', |
265
|
|
|
[ [ $text2Id ] ] |
266
|
|
|
); |
267
|
|
|
$this->assertSelect( |
268
|
|
|
'wbt_text_in_lang', |
269
|
|
|
'wbxl_id', |
270
|
|
|
'*', |
271
|
|
|
[ [ $textInLang2Id ] ] |
272
|
|
|
); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
} |
276
|
|
|
|