1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Tests; |
5
|
|
|
|
6
|
|
|
use CircleCITestIndex; |
7
|
|
|
use Firesphere\SolrSearch\Helpers\Synonyms; |
8
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
9
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
10
|
|
|
use Firesphere\SolrSearch\Results\SearchResult; |
11
|
|
|
use Firesphere\SolrSearch\Stores\FileConfigStore; |
12
|
|
|
use Page; |
13
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
14
|
|
|
use SilverStripe\Control\Director; |
15
|
|
|
use SilverStripe\Control\HTTPRequestBuilder; |
16
|
|
|
use SilverStripe\Control\NullHTTPRequest; |
17
|
|
|
use SilverStripe\Core\Injector\Injector; |
18
|
|
|
use SilverStripe\Dev\SapphireTest; |
19
|
|
|
use SilverStripe\ORM\ArrayList; |
20
|
|
|
use SilverStripe\ORM\DatabaseAdmin; |
21
|
|
|
use SilverStripe\ORM\DB; |
22
|
|
|
use SilverStripe\ORM\PaginatedList; |
23
|
|
|
use SilverStripe\Security\DefaultAdminService; |
24
|
|
|
use SilverStripe\View\ArrayData; |
25
|
|
|
use Solarium\Component\Result\Highlighting\Highlighting; |
26
|
|
|
use Solarium\Core\Client\Client; |
27
|
|
|
|
28
|
|
|
class BaseIndexTest extends SapphireTest |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var BaseIndex |
32
|
|
|
*/ |
33
|
|
|
protected $index; |
34
|
|
|
|
35
|
|
|
public static function setUpBeforeClass() |
36
|
|
|
{ |
37
|
|
|
parent::setUpBeforeClass(); |
38
|
|
|
// This is a hack on my local dev to make fixtures populate temporary database |
39
|
|
|
$conn = DB::get_conn(); |
40
|
|
|
$dbName = self::tempDB()->build(); |
41
|
|
|
$conn->selectDatabase($dbName); |
42
|
|
|
$dbAdmin = new DatabaseAdmin(); |
43
|
|
|
$dbAdmin->setRequest(HTTPRequestBuilder::createFromEnvironment()); |
44
|
|
|
$dbAdmin->doBuild(true, true, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testConstruct() |
48
|
|
|
{ |
49
|
|
|
$this->assertInstanceOf(Client::class, $this->index->getClient()); |
50
|
|
|
$this->assertCount(1, $this->index->getClasses()); |
51
|
|
|
$this->assertCount(1, $this->index->getFulltextFields()); |
52
|
|
|
$this->assertContains(SiteTree::class, $this->index->getClasses()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testInit() |
56
|
|
|
{ |
57
|
|
|
$this->assertNull($this->index->init()); |
58
|
|
|
$this->assertNotEmpty($this->index->getFulltextFields()); |
59
|
|
|
$this->assertNotEmpty($this->index->getFieldsForIndexing()); |
60
|
|
|
$expected = [ |
61
|
|
|
'Title', |
62
|
|
|
'Content', |
63
|
|
|
'Created', |
64
|
|
|
'SubsiteID' |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->assertEquals($expected, array_values($this->index->getFieldsForIndexing())); |
68
|
|
|
|
69
|
|
|
$index = new TestIndexTwo(); |
70
|
|
|
|
71
|
|
|
$this->assertCount(3, $index->getFulltextFields()); |
72
|
|
|
$this->assertCount(1, $index->getFacetFields()); |
73
|
|
|
$facets = $index->getFacetFields(); |
74
|
|
|
$this->assertEquals(['Field' => 'SiteTree_TestObjectID', 'Title' => 'TestObject'], $facets[TestObject::class]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGetSynonyms() |
78
|
|
|
{ |
79
|
|
|
$this->assertEquals(Synonyms::getSynonymsAsString(), $this->index->getSynonyms()); |
80
|
|
|
|
81
|
|
|
$this->assertEmpty($this->index->getSynonyms(false)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testIndexName() |
85
|
|
|
{ |
86
|
|
|
$this->assertEquals('TestIndex', $this->index->getIndexName()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testUploadConfig() |
90
|
|
|
{ |
91
|
|
|
$config = [ |
92
|
|
|
'mode' => 'file', |
93
|
|
|
'path' => '.solr' |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
/** @var FileConfigStore $configStore */ |
97
|
|
|
$configStore = Injector::inst()->create(FileConfigStore::class, $config); |
98
|
|
|
|
99
|
|
|
$this->index->uploadConfig($configStore); |
100
|
|
|
|
101
|
|
|
$this->assertFileExists(Director::baseFolder() . '/.solr/TestIndex/conf/schema.xml'); |
102
|
|
|
$this->assertFileExists(Director::baseFolder() . '/.solr/TestIndex/conf/synonyms.txt'); |
103
|
|
|
$this->assertFileExists(Director::baseFolder() . '/.solr/TestIndex/conf/stopwords.txt'); |
104
|
|
|
|
105
|
|
|
$xml = file_get_contents(Director::baseFolder() . '/.solr/TestIndex/conf/schema.xml'); |
106
|
|
|
$this->assertContains( |
107
|
|
|
'<field name=\'SiteTree_Title\' type=\'string\' indexed=\'true\' stored=\'true\' multiValued=\'false\'/>', |
108
|
|
|
$xml |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$original = $configStore->getConfig(); |
112
|
|
|
$configStore->setConfig([]); |
113
|
|
|
$this->assertEquals([], $configStore->getConfig()); |
114
|
|
|
// Unhappy path, the config is not updated |
115
|
|
|
$this->assertNotEquals($original, $configStore->getConfig()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGetFieldsForIndexing() |
119
|
|
|
{ |
120
|
|
|
$expected = [ |
121
|
|
|
'Title', |
122
|
|
|
'Content', |
123
|
|
|
'Created', |
124
|
|
|
'SubsiteID' |
125
|
|
|
]; |
126
|
|
|
$this->assertEquals($expected, array_values($this->index->getFieldsForIndexing())); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testGetSetClient() |
130
|
|
|
{ |
131
|
|
|
$client = $this->index->getClient(); |
132
|
|
|
// set client to something stupid |
133
|
|
|
$this->index->setClient('test'); |
134
|
|
|
$this->assertEquals('test', $this->index->getClient()); |
135
|
|
|
$this->index->setClient($client); |
136
|
|
|
$this->assertInstanceOf(Client::class, $this->index->getClient()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testDoSearch() |
140
|
|
|
{ |
141
|
|
|
$index = new CircleCITestIndex(); |
142
|
|
|
|
143
|
|
|
$query = new BaseQuery(); |
144
|
|
|
$query->addTerm('Home'); |
145
|
|
|
|
146
|
|
|
$result = $index->doSearch($query); |
147
|
|
|
$this->assertInstanceOf(SearchResult::class, $result); |
148
|
|
|
$this->assertEquals(1, $result->getTotalItems()); |
149
|
|
|
|
150
|
|
|
$admin = singleton(DefaultAdminService::class)->findOrCreateDefaultAdmin(); |
151
|
|
|
$this->loginAs($admin); |
152
|
|
|
// Result should be the same for now |
153
|
|
|
$result2 = $index->doSearch($query); |
154
|
|
|
$this->assertEquals($result, $result2); |
155
|
|
|
|
156
|
|
|
$query->addClass(SiteTree::class); |
157
|
|
|
|
158
|
|
|
$result3 = $index->doSearch($query); |
159
|
|
|
$request = new NullHTTPRequest(); |
160
|
|
|
$this->assertInstanceOf(PaginatedList::class, $result3->getPaginatedMatches($request)); |
161
|
|
|
$this->assertEquals($result3->getTotalItems(), $result3->getPaginatedMatches($request)->getTotalItems()); |
162
|
|
|
$this->assertInstanceOf(ArrayData::class, $result3->getFacets()); |
163
|
|
|
$this->assertInstanceOf(ArrayList::class, $result3->getSpellcheck()); |
164
|
|
|
$this->assertInstanceOf(Highlighting::class, $result3->getHighlight()); |
165
|
|
|
|
166
|
|
|
$result3->setCustomisedMatches([]); |
167
|
|
|
$this->assertInstanceOf(ArrayList::class, $result3->getMatches()); |
168
|
|
|
$this->assertCount(0, $result3->getMatches()); |
169
|
|
|
|
170
|
|
|
$index = new CircleCITestIndex(); |
171
|
|
|
$query = new BaseQuery(); |
172
|
|
|
$query->addTerm('Home', ['SiteTree_Title'], 5); |
173
|
|
|
$result4 = $index->doSearch($query); |
174
|
|
|
|
175
|
|
|
$this->assertContains('SiteTree_Title:Home^5.0', $index->getQueryFactory()->getBoostTerms()); |
176
|
|
|
$this->assertContains('Home', $index->getQueryTerms()); |
177
|
|
|
$this->assertEquals(1, $result4->getTotalItems()); |
178
|
|
|
|
179
|
|
|
$index = new CircleCITestIndex(); |
180
|
|
|
$query = new BaseQuery(); |
181
|
|
|
$query->addTerm('Home', ['SiteTree.Title'], 3); |
182
|
|
|
$result4 = $index->doSearch($query); |
183
|
|
|
|
184
|
|
|
$this->assertContains('SiteTree_Title:Home^3.0', $index->getQueryFactory()->getBoostTerms()); |
185
|
|
|
$this->assertContains('Home', $index->getQueryTerms()); |
186
|
|
|
$this->assertEquals(1, $result4->getTotalItems()); |
187
|
|
|
|
188
|
|
|
$index = new CircleCITestIndex(); |
189
|
|
|
$query = new BaseQuery(); |
190
|
|
|
$query->addTerm('Home', [], 0, true); |
191
|
|
|
$index->doSearch($query); |
192
|
|
|
|
193
|
|
|
$this->assertContains('Home~', $index->getQueryTerms()); |
194
|
|
|
|
195
|
|
|
$index = new CircleCITestIndex(); |
196
|
|
|
$query = new BaseQuery(); |
197
|
|
|
$query->addTerm('Home', [], 0, 2); |
198
|
|
|
$index->doSearch($query); |
199
|
|
|
|
200
|
|
|
$this->assertContains('Home~2', $index->getQueryTerms()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testGetFieldsForSubsites() |
204
|
|
|
{ |
205
|
|
|
$this->assertContains('SubsiteID', $this->index->getFilterFields()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testSetFacets() |
209
|
|
|
{ |
210
|
|
|
$this->index->addFacetField(Page::class, ['Title' => 'Title', 'Field' => 'Content']); |
211
|
|
|
|
212
|
|
|
$expected = [ |
213
|
|
|
'Page' => [ |
214
|
|
|
'Title' => 'Title', |
215
|
|
|
'Field' => 'Content' |
216
|
|
|
] |
217
|
|
|
]; |
218
|
|
|
$this->assertEquals($expected, $this->index->getFacetFields()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testAddCopyField() |
222
|
|
|
{ |
223
|
|
|
$this->index->addCopyField('myfield', ['Content']); |
224
|
|
|
$expected = [ |
225
|
|
|
'_text' => ['*'], |
226
|
|
|
'myfield' => ['Content'] |
227
|
|
|
]; |
228
|
|
|
$this->assertEquals($expected, $this->index->getCopyFields()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
protected function setUp() |
232
|
|
|
{ |
233
|
|
|
$this->index = Injector::inst()->get(TestIndex::class); |
234
|
|
|
|
235
|
|
|
return parent::setUp(); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|