-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSearch20Test.php
More file actions
314 lines (277 loc) · 13.2 KB
/
Copy pathSearch20Test.php
File metadata and controls
314 lines (277 loc) · 13.2 KB
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
<?php declare(strict_types=1);
namespace Keyman\Site\com\keyman\api\tests {
require_once(__DIR__ . '/../tools/base.inc.php');
require_once(__DIR__ . '/../script/search/2.0/search.inc.php');
require_once(__DIR__ . '/TestUtils.inc.php');
require_once(__DIR__ . '/TestDBBuild.inc.php');
use PHPUnit\Framework\TestCase;
final class Search20Test extends TestCase
{
private const SchemaFilename = "/search/2.0/search.json";
private $schema, $mssql, $s;
static function setUpBeforeClass(): void
{
TestDBBuild::Build();
}
public function setUp(): void
{
$this->schema = TestUtils::LoadJSONSchema(Search20Test::SchemaFilename);
$this->mssql = \Keyman\Site\com\keyman\api\Tools\DB\DBConnect::Connect();
$this->s = new \KeyboardSearch($this->mssql);
}
public function testSimpleSearchResultValidatesAgainstSchema(): void
{
$json = $this->s->GetSearchMatches(null, 'thai', 1, 1);
// Whoa, PHP does *not* round-trip JSON cleanly. This however takes our output and transforms it
// to something that passes our schema validation
// TODO(lowpri): find a way to skip this by emitting clean JSON object from WriteSearchResults()
$json = json_decode(json_encode($json));
// This will throw an exception if it does not pass
$this->schema->in($json);
// Once we get here we know this test has passed so make PHPUnit happy
$this->assertTrue(true);
}
public function testSimpleSearchResultContentsConsistent()
{
$json = $this->s->GetSearchMatches(null, 'khmer', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/fixtures/Search.2.0.khmer.json', json_encode($json), "Search for 'khmer' gives same results as Search.2.0.khmer.json");
}
public function testPhraseSearchResult()
{
$json = $this->s->GetSearchMatches(null, 'khmer angkor', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/fixtures/Search.2.0.khmer-angkor.json', json_encode($json), "Search for 'khmer angkor' gives same results as Search.2.0.khmer-angkor.json");
}
public function testUnicodeSearchResult()
{
$json = $this->s->GetSearchMatches(null, 'ት', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/fixtures/Search.2.0.ethiopic.json', json_encode($json), "Search for 'ት' gives same results as Search.2.0.ethiopic.json");
}
public function testKeyboardIdSearchResult()
{
$json = $this->s->GetSearchMatches(null, 'khmer_', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('khmer_angkor', $json->keyboards[0]->id);
}
public function testDinkaSearchResult() {
$json = $this->s->GetSearchMatches(null, 'Thuɔŋjäŋ', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(3, $json->context->totalRows);
$this->assertEquals('el_dinka', $json->keyboards[0]->id);
$this->assertEquals('dlia25bas', $json->keyboards[1]->id);
$this->assertEquals('dinkaweb11', $json->keyboards[2]->id);
}
// Searches with qualifiers, e.g. k: l: c: s: id: legacy:
public function testSearchByKeyboard()
{
$json = $this->s->GetSearchMatches(null, 'k:khmer', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertJsonStringEqualsJsonFile(__DIR__ . '/fixtures/Search.2.0.khmer-keyboards.json', json_encode($json), "Search for 'k:khmer' gives same results as Search.2.0.khmer-keyboards.json");
}
public function testSearchByKeyboardId()
{
$json = $this->s->GetSearchMatches(null, 'id:basic_kbdkhmr', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('basic_kbdkhmr', $json->keyboards[0]->id);
$this->assertEquals('keyboard_id', $json->keyboards[0]->match->type);
$json = $this->s->GetSearchMatches(null, 'k:id:basic_kbdkhmr', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('basic_kbdkhmr', $json->keyboards[0]->id);
$this->assertEquals('keyboard_id', $json->keyboards[0]->match->type);
}
public function testSearchByLanguageBcp47Tag()
{
$json = $this->s->GetSearchMatches(null, 'l:id:ach', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('acoli', $json->keyboards[0]->id);
$this->assertEquals('language_bcp47_tag', $json->keyboards[0]->match->type);
$json = $this->s->GetSearchMatches(null, 'l:id:km-Khmr-KH', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(6, $json->context->totalRows);
$this->assertEquals('km', $json->keyboards[0]->match->name);
$this->assertEquals('language_bcp47_tag', $json->keyboards[0]->match->type);
$this->assertEquals('khmer_angkor', $json->keyboards[0]->id);
$this->assertEquals('basic_kbdkni', $json->keyboards[1]->id);
$this->assertEquals('sil_khmer', $json->keyboards[2]->id);
$this->assertEquals('basic_kbdkhmr', $json->keyboards[3]->id);
$this->assertEquals('kbdkhmr', $json->keyboards[4]->id);
$this->assertEquals('khmer10', $json->keyboards[5]->id);
}
public function testSearchResultForKeyboardsBcp47Tag()
{
// We should return the tag from the keyboard in the match->tag field, not the normalised tag
$json = $this->s->GetSearchMatches(null, 'l:id:str-latn', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(3, $json->context->totalRows);
$this->assertEquals('fv_sencoten', $json->keyboards[0]->id);
$this->assertEquals('language_bcp47_tag', $json->keyboards[0]->match->type);
$this->assertEquals('str-latn', $json->keyboards[0]->match->tag);
$json = $this->s->GetSearchMatches(null, 'sencoten', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(2, $json->context->totalRows);
$this->assertEquals('fv_sencoten', $json->keyboards[0]->id);
// is now "languages": {"str": {}}
//$this->assertEquals('language', $json->keyboards[0]->match->type);
//$this->assertEquals('str-latn', $json->keyboards[0]->match->tag);
}
public function testSearchByLanguageName()
{
$json = $this->s->GetSearchMatches(null, 'l:Blang', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('basic_kbdtaile', $json->keyboards[0]->id);
}
public function testSearchByCountryIso3166Code()
{
$json = $this->s->GetSearchMatches(null, 'c:id:nz', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(5, $json->context->totalRows);
$this->assertEquals('el_pasifika', $json->keyboards[0]->id);
$this->assertEquals('country_iso3166_code', $json->keyboards[0]->match->type);
}
public function testSearchByCountryName()
{
$json = $this->s->GetSearchMatches(null, 'c:Tanzania', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(6, $json->context->totalRows);
$this->assertEquals('sil_uganda_tanzania', $json->keyboards[0]->id);
}
public function testSearchByScriptIso15924Code()
{
$json = $this->s->GetSearchMatches(null, 's:id:bali', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('aksarabali_panlex', $json->keyboards[0]->id);
$this->assertEquals('script_iso15924_code', $json->keyboards[0]->match->type);
}
public function testSearchByScriptName()
{
$json = $this->s->GetSearchMatches(null, 's:Ethiopic', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(42, $json->context->totalRows);
$this->assertEquals('sil_ethiopic', $json->keyboards[0]->id);
}
/**
* The langtags database includes 'Central Bontoc' but sil_philippines adds an alternative
* spelling of 'Central Bontok'
*/
public function testSearchByCustomLanguageName()
{
$json = $this->s->GetSearchMatches(null, 'Central Bontok', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('Central Bontok', $json->context->text);
$this->assertEquals('sil_philippines', $json->keyboards[0]->id);
}
/**
* The langtags database includes 'khw[-Arab]' but not 'khw-Latn'.
* Keyboard burushaski_girmanas targets a Latin script variant of this.
* Test the direct search for the language tag.
*/
public function testSearchByCustomLanguageTag()
{
$json = $this->s->GetSearchMatches(null, 'l:id:khw-latn', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(1, $json->context->totalRows);
$this->assertEquals('khw-latn', $json->context->text);
$this->assertEquals('burushaski_girminas', $json->keyboards[0]->id);
}
/**
* The langtags database includes 'khw[-Arab]' but not 'khw-Latn'.
* Keyboard burushaski_girmanas targets a Latin script variant of this.
* Validate that the search for 'Khowar' finds both Arabic and Latin script results
*/
public function testSearchByLanguageFindsCustomTag()
{
$json = $this->s->GetSearchMatches(null, 'l:Khowar', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(4, $json->context->totalRows);
$this->assertEquals('rac_khowar', $json->keyboards[0]->id);
$this->assertEquals('sil_khowar', $json->keyboards[1]->id);
$this->assertEquals('burushaski_girminas', $json->keyboards[2]->id);
$this->assertEquals('khowar', $json->keyboards[3]->id);
}
/**
* The langtags database does not include 'pi' (Pali) without a script tag.
* (This is not a helpful tag, but it's good for testing at this time; it is likely that
* an update to the keyboard will correct the tag at which point we should perhaps switch
* to a qa? tag.)
*/
public function testSearchByCustomLanguageTag2()
{
$json = $this->s->GetSearchMatches(null, 'l:id:pi', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(2, $json->context->totalRows);
$this->assertEquals('heidelberginputsolution', $json->keyboards[0]->id);
$this->assertEquals('isis', $json->keyboards[1]->id);
}
public function testSearchByPopularity()
{
$json = $this->s->GetSearchMatches(null, 'p:popularity', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(100, $json->context->totalRows);
$this->assertEquals('gff_amharic', $json->keyboards[0]->id);
}
public function testSearchAlphabetically()
{
$json = $this->s->GetSearchMatches(null, 'p:alphab', 1, 1);
$json = json_decode(json_encode($json));
// test disabled: author email in our current dataset is invalid because
// it contains a space. Schema is valid apart from that.
//$this->schema->in($json);
$this->assertEquals(662, $json->context->totalRows);
$this->assertEquals('acoli', $json->keyboards[0]->id);
}
public function testSearchExcludeObsoleteKeyboards() {
$json = $this->s->GetSearchMatches(null, 'khmer', 0, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(5, $json->context->totalRows);
$this->assertEquals('khmer_angkor', $json->keyboards[0]->id);
$this->assertEquals('sil_khmer', $json->keyboards[1]->id);
$this->assertEquals('basic_kbdkni', $json->keyboards[2]->id);
$this->assertEquals('basic_kbdkhmr', $json->keyboards[3]->id);
$this->assertEquals('krung', $json->keyboards[4]->id);
$json = $this->s->GetSearchMatches(null, 'khmer', 1, 1);
$json = json_decode(json_encode($json));
$this->schema->in($json);
$this->assertEquals(7, $json->context->totalRows);
$this->assertEquals('khmer_angkor', $json->keyboards[0]->id);
$this->assertEquals('sil_khmer', $json->keyboards[1]->id);
$this->assertEquals('basic_kbdkni', $json->keyboards[2]->id);
$this->assertEquals('basic_kbdkhmr', $json->keyboards[3]->id);
$this->assertEquals('krung', $json->keyboards[4]->id);
// Following two are obsolete
$this->assertEquals('khmer10', $json->keyboards[5]->id);
$this->assertEquals('kbdkhmr', $json->keyboards[6]->id);
}
}
}