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