1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\TermStore\MediaWiki\Tests\Integration\PackagePrivate; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Wikibase\TermStore\MediaWiki\PackagePrivate\DatabaseTermIdsResolver; |
7
|
|
|
use Wikibase\TermStore\MediaWiki\PackagePrivate\StaticTypeIdsStore; |
8
|
|
|
use Wikibase\TermStore\MediaWiki\PackagePrivate\TypeIdsResolver; |
9
|
|
|
use Wikibase\TermStore\MediaWiki\TermStoreSchemaUpdater; |
10
|
|
|
use Wikibase\TermStore\MediaWiki\Tests\Util\FakeLoadBalancer; |
11
|
|
|
use Wikimedia\Rdbms\Database; |
12
|
|
|
use Wikimedia\Rdbms\DatabaseSqlite; |
13
|
|
|
use Wikimedia\Rdbms\IDatabase; |
14
|
|
|
use Wikimedia\Rdbms\ILoadBalancer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers \Wikibase\TermStore\MediaWiki\PackagePrivate\DatabaseTermIdsResolver |
18
|
|
|
*/ |
19
|
|
|
class DatabaseTermIdsResolverTest extends TestCase { |
20
|
|
|
|
21
|
|
|
const TYPE_LABEL = 1; |
22
|
|
|
const TYPE_DESCRIPTION = 2; |
23
|
|
|
const TYPE_ALIAS = 3; |
24
|
|
|
|
25
|
|
|
/** @var TypeIdsResolver */ |
26
|
|
|
private $typeIdsResolver; |
27
|
|
|
|
28
|
|
|
/** @var IDatabase */ |
29
|
|
|
private $db; |
30
|
|
|
|
31
|
|
|
/** @var ILoadBalancer */ |
32
|
|
|
private $loadBalancer; |
33
|
|
|
|
34
|
|
|
public function setUp() { |
35
|
|
|
$this->typeIdsResolver = new StaticTypeIdsStore( [ |
36
|
|
|
'label' => self::TYPE_LABEL, |
37
|
|
|
'description' => self::TYPE_DESCRIPTION, |
38
|
|
|
'alias' => self::TYPE_ALIAS, |
39
|
|
|
] ); |
40
|
|
|
$this->db = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
41
|
|
|
$this->db->sourceFile( TermStoreSchemaUpdater::getSqlFileAbsolutePath() ); |
42
|
|
|
$this->loadBalancer = new FakeLoadBalancer( [ |
43
|
|
|
'dbr' => $this->db, |
44
|
|
|
] ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCanResolveEverything() { |
48
|
|
|
$this->db->insert( 'wbt_text', |
49
|
|
|
[ 'wbx_text' => 'text' ] ); |
50
|
|
|
$text1Id = $this->db->insertId(); |
51
|
|
|
$this->db->insert( 'wbt_text', |
52
|
|
|
[ 'wbx_text' => 'Text' ] ); |
53
|
|
|
$text2Id = $this->db->insertId(); |
54
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
55
|
|
|
[ 'wbxl_language' => 'en', 'wbxl_text_id' => $text1Id ] ); |
56
|
|
|
$textInLang1Id = $this->db->insertId(); |
57
|
|
|
$this->db->insert( 'wbt_text_in_lang', |
58
|
|
|
[ 'wbxl_language' => 'de', 'wbxl_text_id' => $text2Id ] ); |
59
|
|
|
$textInLang2Id = $this->db->insertId(); |
60
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
61
|
|
|
[ 'wbtl_type_id' => self::TYPE_LABEL, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
62
|
|
|
$termInLang1Id = $this->db->insertId(); |
63
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
64
|
|
|
[ 'wbtl_type_id' => self::TYPE_DESCRIPTION, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
65
|
|
|
$termInLang2Id = $this->db->insertId(); |
66
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
67
|
|
|
[ 'wbtl_type_id' => self::TYPE_LABEL, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
68
|
|
|
$termInLang3Id = $this->db->insertId(); |
69
|
|
|
|
70
|
|
|
$resolver = new DatabaseTermIdsResolver( $this->typeIdsResolver, $this->loadBalancer ); |
71
|
|
|
$terms = $resolver->resolveTermIds( [ $termInLang1Id, $termInLang2Id, $termInLang3Id ] ); |
72
|
|
|
|
73
|
|
|
$this->assertSame( [ |
74
|
|
|
'label' => [ |
75
|
|
|
'en' => [ 'text' ], |
76
|
|
|
'de' => [ 'Text' ], |
77
|
|
|
], |
78
|
|
|
'description' => [ |
79
|
|
|
'en' => [ 'text' ], |
80
|
|
|
], |
81
|
|
|
], $terms ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testReadsEverythingFromReplicaIfPossible() { |
85
|
|
|
$this->db->insert( 'wbt_text', |
86
|
|
|
[ 'wbx_text' => 'text' ] ); |
87
|
|
|
$text1Id = $this->db->insertId(); |
88
|
|
|
$this->db->insert( 'wbt_text', |
89
|
|
|
[ 'wbx_text' => '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' => self::TYPE_LABEL, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
99
|
|
|
$termInLang1Id = $this->db->insertId(); |
100
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
101
|
|
|
[ 'wbtl_type_id' => self::TYPE_DESCRIPTION, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
102
|
|
|
$termInLang2Id = $this->db->insertId(); |
103
|
|
|
$this->db->insert( 'wbt_term_in_lang', |
104
|
|
|
[ 'wbtl_type_id' => self::TYPE_LABEL, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
105
|
|
|
$termInLang3Id = $this->db->insertId(); |
106
|
|
|
|
107
|
|
|
$dbw = $this->createMock( Database::class ); |
108
|
|
|
$dbw->expects( $this->never() )->method( 'query' ); |
109
|
|
|
|
110
|
|
|
$resolver = new DatabaseTermIdsResolver( |
111
|
|
|
$this->typeIdsResolver, |
112
|
|
|
new FakeLoadBalancer( [ |
113
|
|
|
'dbr' => $this->db, |
114
|
|
|
'dbw' => $dbw, |
115
|
|
|
] ) |
116
|
|
|
); |
117
|
|
|
$resolver->resolveTermIds( [ $termInLang1Id, $termInLang2Id, $termInLang3Id ] ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testFallsBackToMasterIfNecessary() { |
121
|
|
|
$dbr = $this->db; |
122
|
|
|
$dbw = DatabaseSqlite::newStandaloneInstance( ':memory:' ); |
123
|
|
|
$dbw->sourceFile( TermStoreSchemaUpdater::getSqlFileAbsolutePath() ); |
124
|
|
|
// both master and replica have most of the data |
125
|
|
|
foreach ( [ $dbr, $dbw ] as $db ) { |
126
|
|
|
// note: we assume that both DBs get the same insert IDs |
127
|
|
|
$db->insert( 'wbt_text', |
128
|
|
|
[ 'wbx_text' => 'text' ] ); |
129
|
|
|
$text1Id = $db->insertId(); |
130
|
|
|
$db->insert( 'wbt_text', |
131
|
|
|
[ 'wbx_text' => 'Text' ] ); |
132
|
|
|
$text2Id = $db->insertId(); |
133
|
|
|
$db->insert( 'wbt_text_in_lang', |
134
|
|
|
[ 'wbxl_language' => 'en', 'wbxl_text_id' => $text1Id ] ); |
135
|
|
|
$textInLang1Id = $db->insertId(); |
136
|
|
|
$db->insert( 'wbt_text_in_lang', |
137
|
|
|
[ 'wbxl_language' => 'de', 'wbxl_text_id' => $text2Id ] ); |
138
|
|
|
$textInLang2Id = $db->insertId(); |
139
|
|
|
$db->insert( 'wbt_term_in_lang', |
140
|
|
|
[ 'wbtl_type_id' => self::TYPE_LABEL, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
141
|
|
|
$termInLang1Id = $db->insertId(); |
142
|
|
|
$db->insert( 'wbt_term_in_lang', |
143
|
|
|
[ 'wbtl_type_id' => self::TYPE_DESCRIPTION, 'wbtl_text_in_lang_id' => $textInLang1Id ] ); |
144
|
|
|
$termInLang2Id = $db->insertId(); |
145
|
|
|
} |
146
|
|
|
// only master has the last term_in_lang row |
147
|
|
|
$db->insert( 'wbt_term_in_lang', |
|
|
|
|
148
|
|
|
[ 'wbtl_type_id' => self::TYPE_LABEL, 'wbtl_text_in_lang_id' => $textInLang2Id ] ); |
|
|
|
|
149
|
|
|
$termInLang3Id = $db->insertId(); |
150
|
|
|
|
151
|
|
|
$resolver = new DatabaseTermIdsResolver( |
152
|
|
|
$this->typeIdsResolver, |
153
|
|
|
new FakeLoadBalancer( [ |
154
|
|
|
'dbr' => $dbr, |
155
|
|
|
'dbw' => $dbw, |
156
|
|
|
] ) |
157
|
|
|
); |
158
|
|
|
$terms = $resolver->resolveTermIds( [ $termInLang1Id, $termInLang2Id, $termInLang3Id ] ); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$this->assertSame( [ |
161
|
|
|
'label' => [ |
162
|
|
|
'en' => [ 'text' ], |
163
|
|
|
'de' => [ 'Text' ], |
164
|
|
|
], |
165
|
|
|
'description' => [ |
166
|
|
|
'en' => [ 'text' ], |
167
|
|
|
], |
168
|
|
|
], $terms ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
It seems like you are relying on a variable being defined by an iteration: