1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Tests; |
5
|
|
|
|
6
|
|
|
use CircleCITestIndex; |
7
|
|
|
use Firesphere\SolrSearch\Extensions\DataObjectExtension; |
8
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
9
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
10
|
|
|
use Firesphere\SolrSearch\Tasks\SolrConfigureTask; |
11
|
|
|
use Firesphere\SolrSearch\Tasks\SolrIndexTask; |
12
|
|
|
use GuzzleHttp\Handler\MockHandler; |
13
|
|
|
use GuzzleHttp\HandlerStack; |
14
|
|
|
use GuzzleHttp\Psr7\Response; |
15
|
|
|
use Page; |
16
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
17
|
|
|
use SilverStripe\Control\NullHTTPRequest; |
18
|
|
|
use SilverStripe\Core\Config\Config; |
19
|
|
|
use SilverStripe\Core\Injector\Injector; |
20
|
|
|
use SilverStripe\Dev\SapphireTest; |
21
|
|
|
use SilverStripe\ORM\ArrayList; |
22
|
|
|
use Solarium\Core\Client\Client; |
23
|
|
|
|
24
|
|
|
class SolrCoreServiceTest extends SapphireTest |
25
|
|
|
{ |
26
|
|
|
protected static $fixture_file = 'tests/fixtures/DataResolver.yml'; |
27
|
|
|
protected static $extra_dataobjects = [ |
28
|
|
|
TestObject::class, |
29
|
|
|
TestPage::class, |
30
|
|
|
TestRelationObject::class, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var SolrCoreService |
35
|
|
|
*/ |
36
|
|
|
protected $service; |
37
|
|
|
|
38
|
|
|
public function testIndexes() |
39
|
|
|
{ |
40
|
|
|
$service = new SolrCoreService(); |
41
|
|
|
|
42
|
|
|
$expected = [ |
43
|
|
|
CircleCITestIndex::class, |
44
|
|
|
TestIndex::class, |
45
|
|
|
TestIndexTwo::class, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$this->assertEquals($expected, $service->getValidIndexes()); |
49
|
|
|
$this->assertEquals([CircleCITestIndex::class], $service->getValidIndexes(CircleCITestIndex::class)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testUpdateItems() |
53
|
|
|
{ |
54
|
|
|
$index = new CircleCITestIndex(); |
55
|
|
|
$query = new BaseQuery(); |
56
|
|
|
$query->addTerm('*:*'); |
57
|
|
|
$items = SiteTree::get(); |
58
|
|
|
|
59
|
|
|
$result = $this->service->updateItems($items, SolrCoreService::UPDATE_TYPE, CircleCITestIndex::class); |
60
|
|
|
$this->assertEquals(200, $result->getResponse()->getStatusCode()); |
61
|
|
|
|
62
|
|
|
$this->service->updateItems($items, SolrCoreService::DELETE_TYPE, CircleCITestIndex::class); |
63
|
|
|
$this->assertEquals(0, $index->doSearch($query)->getTotalItems()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testUpdateItemsEmptyArray() |
67
|
|
|
{ |
68
|
|
|
$index = new CircleCITestIndex(); |
69
|
|
|
$query = new BaseQuery(); |
70
|
|
|
$this->service->doManipulate(ArrayList::create(), SolrCoreService::DELETE_TYPE_ALL, $index); |
71
|
|
|
$this->assertEquals(0, $index->doSearch($query)->getTotalItems()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @expectedException \LogicException |
76
|
|
|
*/ |
77
|
|
|
public function testWrongUpdateItems() |
78
|
|
|
{ |
79
|
|
|
$items = SiteTree::get(); |
80
|
|
|
|
81
|
|
|
$this->service->updateItems($items, SolrCoreService::UPDATE_TYPE, 'NonExisting'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testCoreStatus() |
85
|
|
|
{ |
86
|
|
|
$status = $this->service->coreStatus(CircleCITestIndex::class); |
87
|
|
|
$deprecatedStatus = $this->service->coreIsActive(CircleCITestIndex::class); |
88
|
|
|
$this->assertEquals($status->getVersion(), $deprecatedStatus->getVersion()); |
89
|
|
|
$this->assertEquals($status->getNumberOfDocuments(), $deprecatedStatus->getNumberOfDocuments()); |
90
|
|
|
$this->assertEquals($status->getCoreName(), $deprecatedStatus->getCoreName()); |
91
|
|
|
|
92
|
|
|
$this->assertEquals('CircleCITestIndex', $status->getCoreName()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testCoreUnload() |
96
|
|
|
{ |
97
|
|
|
$status1 = $this->service->coreStatus(CircleCITestIndex::class); |
98
|
|
|
$this->service->coreUnload(CircleCITestIndex::class); |
99
|
|
|
$status2 = $this->service->coreStatus(CircleCITestIndex::class); |
100
|
|
|
$this->assertEquals(0, $status2->getUptime()); |
101
|
|
|
$this->assertNotEquals($status1->getUptime(), $status2->getUptime()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGetSetClient() |
105
|
|
|
{ |
106
|
|
|
$client = $this->service->getClient(); |
107
|
|
|
$this->assertInstanceOf(Client::class, $client); |
108
|
|
|
$service = $this->service->setClient($client); |
109
|
|
|
$this->assertInstanceOf(SolrCoreService::class, $service); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGetSolrVersion() |
113
|
|
|
{ |
114
|
|
|
$this->assertEquals(5, $this->service->getSolrVersion()); |
115
|
|
|
$version4 = [ |
116
|
|
|
'responseHeader' => |
117
|
|
|
[ |
118
|
|
|
'status' => 0, |
119
|
|
|
'QTime' => 10, |
120
|
|
|
], |
121
|
|
|
'mode' => 'std', |
122
|
|
|
'solr_home' => '/var/solr/data', |
123
|
|
|
'lucene' => |
124
|
|
|
[ |
125
|
|
|
'solr-spec-version' => '4.3.2', |
126
|
|
|
] |
127
|
|
|
]; |
128
|
|
|
$mock = new MockHandler([ |
129
|
|
|
new Response(200, ['X-Foo' => 'Bar'], json_encode($version4)), |
130
|
|
|
]); |
131
|
|
|
$handler = HandlerStack::create($mock); |
132
|
|
|
|
133
|
|
|
$this->assertEquals(4, $this->service->getSolrVersion($handler)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function setUp() |
137
|
|
|
{ |
138
|
|
|
parent::setUp(); |
139
|
|
|
Injector::inst()->get(Page::class)->requireDefaultRecords(); |
140
|
|
|
foreach (self::$extra_dataobjects as $className) { |
141
|
|
|
Config::modify()->merge($className, 'extensions', [DataObjectExtension::class]); |
142
|
|
|
} |
143
|
|
|
$this->service = new SolrCoreService(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function tearDown() |
147
|
|
|
{ |
148
|
|
|
/** @var SolrConfigureTask $task */ |
149
|
|
|
$task = Injector::inst()->get(SolrConfigureTask::class); |
150
|
|
|
$task->run(new NullHTTPRequest()); |
151
|
|
|
/** @var SolrIndexTask $task */ |
152
|
|
|
$task = Injector::inst()->get(SolrIndexTask::class); |
153
|
|
|
$task->run(new NullHTTPRequest()); |
154
|
|
|
parent::tearDown(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|