1
|
|
|
<?php |
2
|
|
|
use SilverStripe\Elastica\ElasticSearcher; |
3
|
|
|
use Elastica\Type; |
4
|
|
|
use SilverStripe\Elastica\ReindexTask; |
5
|
|
|
|
6
|
|
|
class TranslatableUnitTest extends ElasticsearchBaseTest { |
7
|
|
|
public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; |
8
|
|
|
|
9
|
|
|
public static $ignoreFixtureFileFor = array('testResultsForEmptySearch'); |
10
|
|
|
|
11
|
|
|
public function setUpOnce() { |
12
|
|
|
//Add translatable if it exists |
13
|
|
|
if(class_exists('Translatable')) { |
14
|
|
|
SiteTree::add_extension('Translatable'); |
15
|
|
|
} |
16
|
|
|
parent::setUpOnce(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testElasticSearchForm() { |
20
|
|
|
if(!class_exists('Translatable')) { |
21
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$form = new \ElasticSearchForm(new \Controller(), 'TestForm'); |
25
|
|
|
$fields = $form->Fields(); |
26
|
|
|
$result = array(); |
27
|
|
|
foreach($fields as $field) { |
|
|
|
|
28
|
|
|
$result[$field->getName()] = $field->Value(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$expected = array('q' => '', 'searchlocale' => 'en_US'); |
32
|
|
|
$this->assertEquals($expected, $result); |
33
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
public function testHighlightPassingFields() { |
38
|
|
|
if(!class_exists('Translatable')) { |
39
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$es = new ElasticSearcher(); |
43
|
|
|
$es->setClasses('FlickrPhotoTO'); |
44
|
|
|
|
45
|
|
|
$es->setHighlightedFields(array('Title', 'Title.standard', 'Description')); |
46
|
|
|
|
47
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
48
|
|
|
$query = 'New Zealand'; |
49
|
|
|
$paginated = $es->search($query, $fields); |
50
|
|
|
$ctr = 0; |
51
|
|
|
|
52
|
|
|
foreach($paginated->getList()->toArray() as $result) { |
53
|
|
|
$ctr++; |
54
|
|
|
|
55
|
|
|
foreach($result->SearchHighlightsByField->Description->getIterator() as $highlight) { |
56
|
|
|
$snippet = $highlight->Snippet; |
57
|
|
|
$snippet = strtolower($snippet); |
58
|
|
|
$wordFound = false; |
59
|
|
|
$lcquery = explode(' ', strtolower($query)); |
60
|
|
|
foreach($lcquery as $part) { |
61
|
|
|
$bracketed = '<strong class="hl">' . $part . '</strong>'; |
62
|
|
|
if(strpos($snippet, $bracketed) > 0) { |
63
|
|
|
$wordFound = true; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$this->assertTrue($wordFound, 'Highlight should have been found'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
public function testAutoCompleteGood() { |
73
|
|
|
if(!class_exists('Translatable')) { |
74
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$es = new ElasticSearcher(); |
78
|
|
|
$es->setClasses('FlickrPhotoTO'); |
79
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
|
|
|
|
80
|
|
|
$query = 'Lond'; |
81
|
|
|
$results = $es->autocomplete_search($query, 'Title'); |
82
|
|
|
$this->assertEquals(7, $results->getTotalItems()); |
83
|
|
|
foreach($results->toArray() as $result) { |
84
|
|
|
$this->assertTrue(strpos($result->Title, $query) > 0); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
// --------------------- |
92
|
|
|
|
93
|
|
|
// FIXME - this test is shardy unfortunately |
94
|
|
|
public function testMoreLikeThisSinglePhoto() { |
95
|
|
|
if(!class_exists('Translatable')) { |
96
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
100
|
|
|
$es = new ElasticSearcher(); |
101
|
|
|
$locale = \i18n::default_locale(); |
102
|
|
|
$es->setLocale($locale); |
103
|
|
|
$es->setClasses('FlickrPhotoTO'); |
104
|
|
|
|
105
|
|
|
$fields = array('Description.standard' => 1,'Title.standard' => 1); |
106
|
|
|
$results = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$terms = $results->getList()->MoreLikeThisTerms; |
|
|
|
|
109
|
|
|
|
110
|
|
|
$fieldNamesReturned = array_keys($terms); |
111
|
|
|
$fieldNames = array_keys($fields); |
112
|
|
|
sort($fieldNames); |
113
|
|
|
sort($fieldNamesReturned); |
114
|
|
|
|
115
|
|
|
$this->assertEquals($fieldNames, $fieldNamesReturned); |
116
|
|
|
|
117
|
|
|
//FIXME - this seems anomolyous, check in more detail |
118
|
|
|
$expected = array('texas'); |
119
|
|
|
$this->assertEquals($expected, $terms['Title.standard']); |
120
|
|
|
|
121
|
|
|
$expected = array('new', 'see', 'photographs', 'information', 'resolution', 'company', 'view', |
|
|
|
|
122
|
|
|
'high', 'collection', 'pacific', 'orleans', 'degolyer', 'southern', 'everett', |
123
|
|
|
'railroad', 'texas'); |
124
|
|
|
|
125
|
|
|
$expected = array('collection', 'company', 'degolyer', 'everett', 'file', 'high', |
126
|
|
|
'information', 'new', 'orleans', 'pacific', 'photographs', 'railroad', 'resolution', |
127
|
|
|
'see', 'southern', 'texas', 'view'); |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
$actual = $terms['Description.standard']; |
132
|
|
|
sort($expected); |
133
|
|
|
sort($actual); |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
$this->assertEquals($expected, $actual); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/* |
143
|
|
|
test blank fields |
144
|
|
|
test fields with no weighting (ie not associative) |
145
|
|
|
|
146
|
|
|
*/ |
147
|
|
|
|
148
|
|
|
public function testSimilarGood() { |
149
|
|
|
if(!class_exists('Translatable')) { |
150
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
154
|
|
|
$es = new ElasticSearcher(); |
155
|
|
|
$es->setClasses('FlickrPhotoTO'); |
156
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
157
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
158
|
|
|
|
159
|
|
|
$this->assertEquals(32, $paginated->getTotalItems()); |
160
|
|
|
$results = $paginated->getList()->toArray(); |
161
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Stockdale, Texas]", $results[0]->Title); |
162
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Taft, Texas]", $results[1]->Title); |
163
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]", $results[2]->Title); |
164
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Freight Station, Waxahachie, Texas]", $results[3]->Title); |
165
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]", $results[4]->Title); |
166
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]", $results[5]->Title); |
167
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Eakin Street Yard Office, Dallas, Texas]", $results[6]->Title); |
168
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Locomotive Scrap Line, Englewood Yards, Houston, Texas]", $results[7]->Title); |
169
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Switchman's Tower, San Antonio, Texas]", $results[8]->Title); |
170
|
|
|
$this->assertEquals("Flash Light view in new Subterranean", $results[9]->Title); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/* |
175
|
|
|
FIXME - this is not working, not sure why. Trying to complete coverage of ReindexTask |
176
|
|
|
*/ |
177
|
|
|
public function testBulkIndexing() { |
178
|
|
|
if(!class_exists('Translatable')) { |
179
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
//Reset the index, so that nothing has been indexed |
183
|
|
|
$this->service->reset(); |
184
|
|
|
|
185
|
|
|
//Number of requests indexing wise made to Elasticsearch server |
186
|
|
|
$reqs = $this->service->getIndexingRequestCtr(); |
187
|
|
|
|
188
|
|
|
$task = new ReindexTask($this->service); |
189
|
|
|
|
190
|
|
|
// null request is fine as no parameters used |
191
|
|
|
$task->run(null); |
192
|
|
|
|
193
|
|
|
//Check that the number of indexing requests has increased by 2 |
194
|
|
|
$deltaReqs = $this->service->getIndexingRequestCtr() - $reqs; |
195
|
|
|
//One call is made for each of Page and FlickrPhotoTO |
196
|
|
|
$this->assertEquals(2, $deltaReqs); |
197
|
|
|
|
198
|
|
|
// default installed pages plus 100 FlickrPhotoTOs |
199
|
|
|
$this->checkNumberOfIndexedDocuments(103); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
// if this is not set to unbounded, zero, a conditional is triggered to add max doc freq to the request |
204
|
|
|
public function testSimilarChangeMaxDocFreq() { |
205
|
|
|
if(!class_exists('Translatable')) { |
206
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
210
|
|
|
$es = new ElasticSearcher(); |
211
|
|
|
$es->setMaxDocFreq(4); |
212
|
|
|
$es->setClasses('FlickrPhotoTO'); |
213
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
214
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
215
|
|
|
|
216
|
|
|
$this->assertEquals(14, $paginated->getTotalItems()); |
217
|
|
|
$results = $paginated->getList()->toArray(); |
|
|
|
|
218
|
|
|
$this->makeCode($paginated); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
public function testSimilarNullFields() { |
223
|
|
|
if(!class_exists('Translatable')) { |
224
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
228
|
|
|
$es = new ElasticSearcher(); |
229
|
|
|
$es->setClasses('FlickrPhotoTO'); |
230
|
|
|
try { |
231
|
|
|
$paginated = $es->moreLikeThis($fp, null, true); |
|
|
|
|
232
|
|
|
} catch (InvalidArgumentException $e) { |
233
|
|
|
$this->assertEquals('Fields cannot be null', $e->getMessage()); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
public function testSimilarNullItem() { |
239
|
|
|
if(!class_exists('Translatable')) { |
240
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$es = new ElasticSearcher(); |
244
|
|
|
$es->setClasses('FlickrPhotoTO'); |
245
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
246
|
|
|
|
247
|
|
|
try { |
248
|
|
|
$paginated = $es->moreLikeThis(null, $fields, true); |
|
|
|
|
249
|
|
|
} catch (InvalidArgumentException $e) { |
250
|
|
|
$this->assertEquals('A searchable item cannot be null', $e->getMessage()); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
View Code Duplication |
private function makeCode($paginated) { |
|
|
|
|
255
|
|
|
$results = $paginated->getList()->toArray(); |
256
|
|
|
$ctr = 0; |
257
|
|
|
echo '$result = $paginated->getList()->toArray();' . "\n"; |
258
|
|
|
foreach($results as $result) { |
259
|
|
|
echo '$this->assertEquals("' . $result->Title . '", $results[' . $ctr . ']->Title);' . "\n"; |
260
|
|
|
$ctr++; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.