1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\SearchTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Repository\Tests\Service\Mock; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
12
|
|
|
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; |
13
|
|
|
use eZ\Publish\Core\Repository\ContentService; |
14
|
|
|
use eZ\Publish\Core\Repository\Helper\DomainMapper; |
15
|
|
|
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest; |
16
|
|
|
use eZ\Publish\Core\Repository\SearchService; |
17
|
|
|
use eZ\Publish\Core\Repository\Permission\PermissionCriterionResolver; |
18
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
19
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationQuery; |
20
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
21
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\SortClause; |
22
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; |
23
|
|
|
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit; |
24
|
|
|
use eZ\Publish\Core\Search\Common\BackgroundIndexer; |
25
|
|
|
use eZ\Publish\Core\Search\Common\BackgroundIndexer\NullIndexer; |
26
|
|
|
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo; |
27
|
|
|
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation; |
28
|
|
|
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException; |
29
|
|
|
use Exception; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Mock test case for Search service. |
33
|
|
|
*/ |
34
|
|
|
class SearchTest extends BaseServiceMockTest |
35
|
|
|
{ |
36
|
|
|
protected $repositoryMock; |
37
|
|
|
|
38
|
|
|
protected $domainMapperMock; |
39
|
|
|
|
40
|
|
|
protected $permissionsCriterionResolverMock; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Test for the __construct() method. |
44
|
|
|
* |
45
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::__construct |
46
|
|
|
*/ |
47
|
|
|
public function testConstructor() |
48
|
|
|
{ |
49
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
50
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
51
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
52
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
53
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
54
|
|
|
$settings = array('teh setting'); |
55
|
|
|
|
56
|
|
|
$service = new SearchService( |
57
|
|
|
$repositoryMock, |
|
|
|
|
58
|
|
|
$searchHandlerMock, |
59
|
|
|
$domainMapperMock, |
|
|
|
|
60
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
61
|
|
|
new NullIndexer(), |
62
|
|
|
$settings |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$this->assertAttributeSame( |
66
|
|
|
$repositoryMock, |
67
|
|
|
'repository', |
68
|
|
|
$service |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->assertAttributeSame( |
72
|
|
|
$searchHandlerMock, |
73
|
|
|
'searchHandler', |
74
|
|
|
$service |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->assertAttributeSame( |
78
|
|
|
$domainMapperMock, |
79
|
|
|
'domainMapper', |
80
|
|
|
$service |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->assertAttributeSame( |
84
|
|
|
$permissionsCriterionResolverMock, |
85
|
|
|
'permissionCriterionResolver', |
86
|
|
|
$service |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->assertAttributeSame( |
90
|
|
|
$settings, |
91
|
|
|
'settings', |
92
|
|
|
$service |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function providerForFindContentValidatesLocationCriteriaAndSortClauses() |
97
|
|
|
{ |
98
|
|
|
return array( |
99
|
|
|
array( |
100
|
|
|
new Query(array('filter' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))), |
101
|
|
|
"Argument '\$query' is invalid: Location criterions cannot be used in Content search", |
102
|
|
|
), |
103
|
|
|
array( |
104
|
|
|
new Query(array('query' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))), |
105
|
|
|
"Argument '\$query' is invalid: Location criterions cannot be used in Content search", |
106
|
|
|
), |
107
|
|
|
array( |
108
|
|
|
new Query( |
109
|
|
|
array( |
110
|
|
|
'query' => new Criterion\LogicalAnd( |
111
|
|
|
array( |
112
|
|
|
new Criterion\Location\Depth(Criterion\Operator::LT, 2), |
113
|
|
|
) |
114
|
|
|
), |
115
|
|
|
) |
116
|
|
|
), |
117
|
|
|
"Argument '\$query' is invalid: Location criterions cannot be used in Content search", |
118
|
|
|
), |
119
|
|
|
array( |
120
|
|
|
new Query(array('sortClauses' => array(new SortClause\Location\Id()))), |
121
|
|
|
"Argument '\$query' is invalid: Location sort clauses cannot be used in Content search", |
122
|
|
|
), |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @dataProvider providerForFindContentValidatesLocationCriteriaAndSortClauses |
128
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function testFindContentValidatesLocationCriteriaAndSortClauses($query, $exceptionMessage) |
131
|
|
|
{ |
132
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
133
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
134
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
135
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
136
|
|
|
|
137
|
|
|
$service = new SearchService( |
138
|
|
|
$repositoryMock, |
|
|
|
|
139
|
|
|
$searchHandlerMock, |
140
|
|
|
$this->getDomainMapperMock(), |
|
|
|
|
141
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
142
|
|
|
new NullIndexer(), |
143
|
|
|
array() |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
try { |
147
|
|
|
$service->findContent($query); |
148
|
|
|
} catch (InvalidArgumentException $e) { |
149
|
|
|
$this->assertEquals($exceptionMessage, $e->getMessage()); |
150
|
|
|
throw $e; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$this->fail('Expected exception was not thrown'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function providerForFindSingleValidatesLocationCriteria() |
157
|
|
|
{ |
158
|
|
|
return array( |
159
|
|
|
array( |
160
|
|
|
new Criterion\Location\Depth(Criterion\Operator::LT, 2), |
161
|
|
|
"Argument '\$filter' is invalid: Location criterions cannot be used in Content search", |
162
|
|
|
), |
163
|
|
|
array( |
164
|
|
|
new Criterion\LogicalAnd( |
165
|
|
|
array( |
166
|
|
|
new Criterion\Location\Depth(Criterion\Operator::LT, 2), |
167
|
|
|
) |
168
|
|
|
), |
169
|
|
|
"Argument '\$filter' is invalid: Location criterions cannot be used in Content search", |
170
|
|
|
), |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @dataProvider providerForFindSingleValidatesLocationCriteria |
176
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
public function testFindSingleValidatesLocationCriteria($criterion, $exceptionMessage) |
179
|
|
|
{ |
180
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
181
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
182
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
183
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
184
|
|
|
$service = new SearchService( |
185
|
|
|
$repositoryMock, |
|
|
|
|
186
|
|
|
$searchHandlerMock, |
187
|
|
|
$this->getDomainMapperMock(), |
|
|
|
|
188
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
189
|
|
|
new NullIndexer(), |
190
|
|
|
array() |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
try { |
194
|
|
|
$service->findSingle($criterion); |
195
|
|
|
} catch (InvalidArgumentException $e) { |
196
|
|
|
$this->assertEquals($exceptionMessage, $e->getMessage()); |
197
|
|
|
throw $e; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->fail('Expected exception was not thrown'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Test for the findContent() method. |
205
|
|
|
* |
206
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion |
207
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findContent |
208
|
|
|
* @expectedException \Exception |
209
|
|
|
* @expectedExceptionMessage Handler threw an exception |
210
|
|
|
*/ |
211
|
|
View Code Duplication |
public function testFindContentThrowsHandlerException() |
212
|
|
|
{ |
213
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
214
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
215
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
216
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
217
|
|
|
|
218
|
|
|
$service = new SearchService( |
219
|
|
|
$repositoryMock, |
|
|
|
|
220
|
|
|
$searchHandlerMock, |
221
|
|
|
$this->getDomainMapperMock(), |
|
|
|
|
222
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
223
|
|
|
new NullIndexer(), |
224
|
|
|
array() |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */ |
228
|
|
|
$criterionMock = $this |
229
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
230
|
|
|
->disableOriginalConstructor() |
231
|
|
|
->getMock(); |
232
|
|
|
$query = new Query(array('filter' => $criterionMock)); |
233
|
|
|
|
234
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
235
|
|
|
->method('getPermissionsCriterion') |
236
|
|
|
->with('content', 'read') |
237
|
|
|
->will($this->throwException(new Exception('Handler threw an exception'))); |
238
|
|
|
|
239
|
|
|
$service->findContent($query, array(), true); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
View Code Duplication |
public function providerForFindContentWhenContentLoadThrowsException() |
243
|
|
|
{ |
244
|
|
|
return [ |
245
|
|
|
[ |
246
|
|
|
new NotFoundException('content', 'id = 33'), |
247
|
|
|
true, |
248
|
|
|
], |
249
|
|
|
[ |
250
|
|
|
new UnauthorizedException('content', 'read', ['id' => 33]), |
251
|
|
|
false, |
252
|
|
|
], |
253
|
|
|
]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Test for the findContent() method when search is out of sync with persistence. |
258
|
|
|
* |
259
|
|
|
* @dataProvider providerForFindContentWhenContentLoadThrowsException |
260
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findContent |
261
|
|
|
*/ |
262
|
|
|
public function testFindContentWhenContentLoadThrowsException($e, $index = true) |
263
|
|
|
{ |
264
|
|
|
$indexer = $this->createMock(BackgroundIndexer::class); |
265
|
|
View Code Duplication |
if ($index) { |
|
|
|
|
266
|
|
|
$indexer->expects($this->once()) |
267
|
|
|
->method('registerContent') |
268
|
|
|
->with($this->isInstanceOf(SPIContentInfo::class)); |
269
|
|
|
} else { |
270
|
|
|
$indexer->expects($this->never())->method($this->anything()); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$service = $this->getMockBuilder(SearchService::class) |
274
|
|
|
->setConstructorArgs([ |
275
|
|
|
$repo = $this->getRepositoryMock(), |
276
|
|
|
$this->getSPIMockHandler('Search\\Handler'), |
277
|
|
|
$this->getDomainMapperMock(), |
278
|
|
|
$this->getPermissionCriterionResolverMock(), |
279
|
|
|
$indexer, |
280
|
|
|
])->setMethods(['internalFindContentInfo']) |
281
|
|
|
->getMock(); |
282
|
|
|
|
283
|
|
|
$info = new SPIContentInfo(['id' => 33]); |
284
|
|
|
$result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $info])], 'totalCount' => 2]); |
285
|
|
|
$service->expects($this->once()) |
286
|
|
|
->method('internalFindContentInfo') |
287
|
|
|
->with($this->isInstanceOf(Query::class)) |
288
|
|
|
->willReturn($result); |
289
|
|
|
|
290
|
|
|
$contentService = $this->createMock(ContentService::class); |
291
|
|
|
$contentService->expects($this->once()) |
292
|
|
|
->method('internalLoadContent') |
293
|
|
|
->with(33) |
294
|
|
|
->willThrowException($e); |
295
|
|
|
|
296
|
|
|
$repo->expects($this->once()) |
297
|
|
|
->method('getContentService') |
298
|
|
|
->willReturn($contentService); |
299
|
|
|
|
300
|
|
|
$finalResult = $service->findContent(new Query()); |
301
|
|
|
|
302
|
|
|
$this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty'); |
303
|
|
|
$this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Test for the findContent() method. |
308
|
|
|
* |
309
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion |
310
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findContent |
311
|
|
|
*/ |
312
|
|
|
public function testFindContentNoPermissionsFilter() |
313
|
|
|
{ |
314
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
315
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
316
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
317
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
318
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
319
|
|
|
$service = new SearchService( |
320
|
|
|
$repositoryMock, |
|
|
|
|
321
|
|
|
$searchHandlerMock, |
322
|
|
|
$domainMapperMock, |
|
|
|
|
323
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
324
|
|
|
new NullIndexer(), |
325
|
|
|
array() |
326
|
|
|
); |
327
|
|
|
|
328
|
|
|
$repositoryMock->expects($this->never())->method('hasAccess'); |
329
|
|
|
|
330
|
|
|
$repositoryMock |
331
|
|
|
->expects($this->once()) |
332
|
|
|
->method('getContentService') |
333
|
|
|
->will( |
334
|
|
|
$this->returnValue( |
335
|
|
|
$contentServiceMock = $this |
336
|
|
|
->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService') |
337
|
|
|
->disableOriginalConstructor() |
338
|
|
|
->getMock() |
339
|
|
|
) |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
$serviceQuery = new Query(); |
343
|
|
|
$handlerQuery = new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25)); |
344
|
|
|
$languageFilter = array(); |
345
|
|
|
$spiContentInfo = new SPIContentInfo(); |
346
|
|
|
$contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); |
347
|
|
|
|
348
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
349
|
|
|
$searchHandlerMock->expects($this->once()) |
350
|
|
|
->method('findContent') |
351
|
|
|
->with($this->equalTo($handlerQuery), $this->equalTo($languageFilter)) |
352
|
|
|
->will( |
353
|
|
|
$this->returnValue( |
354
|
|
|
new SearchResult( |
355
|
|
|
array( |
356
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))), |
357
|
|
|
'totalCount' => 1, |
358
|
|
|
) |
359
|
|
|
) |
360
|
|
|
) |
361
|
|
|
); |
362
|
|
|
|
363
|
|
|
$contentServiceMock |
364
|
|
|
->expects($this->once()) |
365
|
|
|
->method('internalLoadContent') |
366
|
|
|
->will($this->returnValue($contentMock)); |
367
|
|
|
|
368
|
|
|
$result = $service->findContent($serviceQuery, $languageFilter, false); |
369
|
|
|
|
370
|
|
|
$this->assertEquals( |
371
|
|
|
new SearchResult( |
372
|
|
|
array( |
373
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))), |
374
|
|
|
'totalCount' => 1, |
375
|
|
|
) |
376
|
|
|
), |
377
|
|
|
$result |
378
|
|
|
); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Test for the findContent() method. |
383
|
|
|
* |
384
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion |
385
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findContent |
386
|
|
|
*/ |
387
|
|
|
public function testFindContentWithPermission() |
388
|
|
|
{ |
389
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
390
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
391
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
392
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
393
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
394
|
|
|
$service = new SearchService( |
395
|
|
|
$repositoryMock, |
|
|
|
|
396
|
|
|
$searchHandlerMock, |
397
|
|
|
$domainMapperMock, |
|
|
|
|
398
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
399
|
|
|
new NullIndexer(), |
400
|
|
|
array() |
401
|
|
|
); |
402
|
|
|
|
403
|
|
|
$repositoryMock |
404
|
|
|
->expects($this->once()) |
405
|
|
|
->method('getContentService') |
406
|
|
|
->will( |
407
|
|
|
$this->returnValue( |
408
|
|
|
$contentServiceMock = $this |
409
|
|
|
->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService') |
410
|
|
|
->disableOriginalConstructor() |
411
|
|
|
->getMock() |
412
|
|
|
) |
413
|
|
|
); |
414
|
|
|
|
415
|
|
|
$criterionMock = $this |
416
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
417
|
|
|
->disableOriginalConstructor() |
418
|
|
|
->getMock(); |
419
|
|
|
$query = new Query(array('filter' => $criterionMock, 'limit' => 10)); |
420
|
|
|
$languageFilter = array(); |
421
|
|
|
$spiContentInfo = new SPIContentInfo(); |
422
|
|
|
$contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); |
423
|
|
|
|
424
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
425
|
|
|
$searchHandlerMock->expects($this->once()) |
426
|
|
|
->method('findContent') |
427
|
|
|
->with($this->equalTo($query), $this->equalTo($languageFilter)) |
428
|
|
|
->will( |
429
|
|
|
$this->returnValue( |
430
|
|
|
new SearchResult( |
431
|
|
|
array( |
432
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))), |
433
|
|
|
'totalCount' => 1, |
434
|
|
|
) |
435
|
|
|
) |
436
|
|
|
) |
437
|
|
|
); |
438
|
|
|
|
439
|
|
|
$domainMapperMock->expects($this->never()) |
440
|
|
|
->method($this->anything()); |
441
|
|
|
|
442
|
|
|
$contentServiceMock |
443
|
|
|
->expects($this->once()) |
444
|
|
|
->method('internalLoadContent') |
445
|
|
|
->will($this->returnValue($contentMock)); |
446
|
|
|
|
447
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
448
|
|
|
->method('getPermissionsCriterion') |
449
|
|
|
->with('content', 'read') |
450
|
|
|
->will($this->returnValue(true)); |
451
|
|
|
|
452
|
|
|
$result = $service->findContent($query, $languageFilter, true); |
453
|
|
|
|
454
|
|
|
$this->assertEquals( |
455
|
|
|
new SearchResult( |
456
|
|
|
array( |
457
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))), |
458
|
|
|
'totalCount' => 1, |
459
|
|
|
) |
460
|
|
|
), |
461
|
|
|
$result |
462
|
|
|
); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Test for the findContent() method. |
467
|
|
|
* |
468
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion |
469
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findContent |
470
|
|
|
*/ |
471
|
|
|
public function testFindContentWithNoPermission() |
472
|
|
|
{ |
473
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
474
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
475
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
476
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
477
|
|
|
$service = new SearchService( |
478
|
|
|
$repositoryMock, |
|
|
|
|
479
|
|
|
$searchHandlerMock, |
480
|
|
|
$this->getDomainMapperMock(), |
|
|
|
|
481
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
482
|
|
|
new NullIndexer(), |
483
|
|
|
array() |
484
|
|
|
); |
485
|
|
|
|
486
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
487
|
|
|
$searchHandlerMock->expects($this->never())->method('findContent'); |
488
|
|
|
|
489
|
|
|
$criterionMock = $this |
490
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
491
|
|
|
->disableOriginalConstructor() |
492
|
|
|
->getMock(); |
493
|
|
|
$query = new Query(array('filter' => $criterionMock)); |
494
|
|
|
|
495
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
496
|
|
|
->method('getPermissionsCriterion') |
497
|
|
|
->with('content', 'read') |
498
|
|
|
->will($this->returnValue(false)); |
499
|
|
|
|
500
|
|
|
$result = $service->findContent($query, array(), true); |
501
|
|
|
|
502
|
|
|
$this->assertEquals( |
503
|
|
|
new SearchResult(array('time' => 0, 'totalCount' => 0)), |
504
|
|
|
$result |
505
|
|
|
); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Test for the findContent() method. |
510
|
|
|
*/ |
511
|
|
|
public function testFindContentWithDefaultQueryValues() |
512
|
|
|
{ |
513
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
514
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
515
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
516
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
517
|
|
|
$service = new SearchService( |
518
|
|
|
$repositoryMock, |
|
|
|
|
519
|
|
|
$searchHandlerMock, |
520
|
|
|
$domainMapperMock, |
|
|
|
|
521
|
|
|
$this->getPermissionCriterionResolverMock(), |
|
|
|
|
522
|
|
|
new NullIndexer(), |
523
|
|
|
array() |
524
|
|
|
); |
525
|
|
|
|
526
|
|
|
$repositoryMock |
527
|
|
|
->expects($this->once()) |
528
|
|
|
->method('getContentService') |
529
|
|
|
->will( |
530
|
|
|
$this->returnValue( |
531
|
|
|
$contentServiceMock = $this |
532
|
|
|
->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService') |
533
|
|
|
->disableOriginalConstructor() |
534
|
|
|
->getMock() |
535
|
|
|
) |
536
|
|
|
); |
537
|
|
|
|
538
|
|
|
$languageFilter = array(); |
539
|
|
|
$spiContentInfo = new SPIContentInfo(); |
540
|
|
|
$contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); |
541
|
|
|
$domainMapperMock->expects($this->never()) |
542
|
|
|
->method($this->anything()); |
543
|
|
|
|
544
|
|
|
$contentServiceMock |
545
|
|
|
->expects($this->once()) |
546
|
|
|
->method('internalLoadContent') |
547
|
|
|
->will($this->returnValue($contentMock)); |
548
|
|
|
|
549
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
550
|
|
|
$searchHandlerMock |
551
|
|
|
->expects($this->once()) |
552
|
|
|
->method('findContent') |
553
|
|
|
->with( |
554
|
|
|
new Query( |
555
|
|
|
array( |
556
|
|
|
'filter' => new Criterion\MatchAll(), |
557
|
|
|
'limit' => 25, |
558
|
|
|
) |
559
|
|
|
), |
560
|
|
|
array() |
561
|
|
|
) |
562
|
|
|
->will( |
563
|
|
|
$this->returnValue( |
564
|
|
|
new SearchResult( |
565
|
|
|
array( |
566
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))), |
567
|
|
|
'totalCount' => 1, |
568
|
|
|
) |
569
|
|
|
) |
570
|
|
|
) |
571
|
|
|
); |
572
|
|
|
|
573
|
|
|
$result = $service->findContent(new Query(), $languageFilter, false); |
574
|
|
|
|
575
|
|
|
$this->assertEquals( |
576
|
|
|
new SearchResult( |
577
|
|
|
array( |
578
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))), |
579
|
|
|
'totalCount' => 1, |
580
|
|
|
) |
581
|
|
|
), |
582
|
|
|
$result |
583
|
|
|
); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Test for the findSingle() method. |
588
|
|
|
* |
589
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion |
590
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findSingle |
591
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
592
|
|
|
*/ |
593
|
|
View Code Duplication |
public function testFindSingleThrowsNotFoundException() |
594
|
|
|
{ |
595
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
596
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
597
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
598
|
|
|
$service = new SearchService( |
599
|
|
|
$repositoryMock, |
|
|
|
|
600
|
|
|
$searchHandlerMock, |
601
|
|
|
$this->getDomainMapperMock(), |
|
|
|
|
602
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(), |
603
|
|
|
new NullIndexer(), |
604
|
|
|
array() |
605
|
|
|
); |
606
|
|
|
|
607
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */ |
608
|
|
|
$criterionMock = $this |
609
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
610
|
|
|
->disableOriginalConstructor() |
611
|
|
|
->getMock(); |
612
|
|
|
|
613
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
614
|
|
|
->method('getPermissionsCriterion') |
615
|
|
|
->with('content', 'read') |
616
|
|
|
->willReturn(false); |
617
|
|
|
|
618
|
|
|
$service->findSingle($criterionMock, array(), true); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Test for the findSingle() method. |
623
|
|
|
* |
624
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion |
625
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findSingle |
626
|
|
|
* @expectedException \Exception |
627
|
|
|
* @expectedExceptionMessage Handler threw an exception |
628
|
|
|
*/ |
629
|
|
View Code Duplication |
public function testFindSingleThrowsHandlerException() |
630
|
|
|
{ |
631
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
632
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
633
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
634
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
635
|
|
|
$service = new SearchService( |
636
|
|
|
$repositoryMock, |
|
|
|
|
637
|
|
|
$searchHandlerMock, |
638
|
|
|
$this->getDomainMapperMock(), |
|
|
|
|
639
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
640
|
|
|
new NullIndexer(), |
641
|
|
|
array() |
642
|
|
|
); |
643
|
|
|
|
644
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */ |
645
|
|
|
$criterionMock = $this |
646
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
647
|
|
|
->disableOriginalConstructor() |
648
|
|
|
->getMock(); |
649
|
|
|
|
650
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
651
|
|
|
->method('getPermissionsCriterion') |
652
|
|
|
->with('content', 'read') |
653
|
|
|
->will($this->throwException(new Exception('Handler threw an exception'))); |
654
|
|
|
|
655
|
|
|
$service->findSingle($criterionMock, array(), true); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Test for the findSingle() method. |
660
|
|
|
* |
661
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion |
662
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findSingle |
663
|
|
|
*/ |
664
|
|
|
public function testFindSingle() |
665
|
|
|
{ |
666
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
667
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
668
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
669
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
670
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
671
|
|
|
$service = new SearchService( |
672
|
|
|
$repositoryMock, |
|
|
|
|
673
|
|
|
$searchHandlerMock, |
674
|
|
|
$domainMapperMock, |
|
|
|
|
675
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
676
|
|
|
new NullIndexer(), |
677
|
|
|
array() |
678
|
|
|
); |
679
|
|
|
|
680
|
|
|
$repositoryMock |
681
|
|
|
->expects($this->once()) |
682
|
|
|
->method('getContentService') |
683
|
|
|
->will( |
684
|
|
|
$this->returnValue( |
685
|
|
|
$contentServiceMock = $this |
686
|
|
|
->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService') |
687
|
|
|
->disableOriginalConstructor() |
688
|
|
|
->getMock() |
689
|
|
|
) |
690
|
|
|
); |
691
|
|
|
|
692
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */ |
693
|
|
|
$criterionMock = $this |
694
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
695
|
|
|
->disableOriginalConstructor() |
696
|
|
|
->getMock(); |
697
|
|
|
|
698
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
699
|
|
|
->method('getPermissionsCriterion') |
700
|
|
|
->with('content', 'read') |
701
|
|
|
->will($this->returnValue(true)); |
702
|
|
|
|
703
|
|
|
$languageFilter = array(); |
704
|
|
|
$spiContentInfo = new SPIContentInfo(); |
705
|
|
|
$contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); |
706
|
|
|
|
707
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
708
|
|
|
$searchHandlerMock->expects($this->once()) |
709
|
|
|
->method('findSingle') |
710
|
|
|
->with($this->equalTo($criterionMock), $this->equalTo($languageFilter)) |
711
|
|
|
->will($this->returnValue($spiContentInfo)); |
712
|
|
|
|
713
|
|
|
$domainMapperMock->expects($this->never()) |
714
|
|
|
->method($this->anything()); |
715
|
|
|
|
716
|
|
|
$contentServiceMock |
717
|
|
|
->expects($this->once()) |
718
|
|
|
->method('internalLoadContent') |
719
|
|
|
->will($this->returnValue($contentMock)); |
720
|
|
|
|
721
|
|
|
$result = $service->findSingle($criterionMock, $languageFilter, true); |
722
|
|
|
|
723
|
|
|
$this->assertEquals($contentMock, $result); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Test for the findLocations() method. |
728
|
|
|
*/ |
729
|
|
|
public function testFindLocationsWithPermission() |
730
|
|
|
{ |
731
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
732
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
733
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
734
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
735
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
736
|
|
|
$service = new SearchService( |
737
|
|
|
$repositoryMock, |
|
|
|
|
738
|
|
|
$searchHandlerMock, |
739
|
|
|
$domainMapperMock, |
|
|
|
|
740
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
741
|
|
|
new NullIndexer(), |
742
|
|
|
array() |
743
|
|
|
); |
744
|
|
|
|
745
|
|
|
$criterionMock = $this |
746
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
747
|
|
|
->disableOriginalConstructor() |
748
|
|
|
->getMock(); |
749
|
|
|
$query = new LocationQuery(array('filter' => $criterionMock, 'limit' => 10)); |
750
|
|
|
$spiLocation = new SPILocation(); |
751
|
|
|
$locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location'); |
752
|
|
|
|
753
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
754
|
|
|
$searchHandlerMock->expects($this->once()) |
755
|
|
|
->method('findLocations') |
756
|
|
|
->with($this->equalTo($query)) |
757
|
|
|
->will( |
758
|
|
|
$this->returnValue( |
759
|
|
|
new SearchResult( |
760
|
|
|
array( |
761
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))), |
762
|
|
|
'totalCount' => 1, |
763
|
|
|
) |
764
|
|
|
) |
765
|
|
|
) |
766
|
|
|
); |
767
|
|
|
|
768
|
|
|
$domainMapperMock->expects($this->once()) |
769
|
|
|
->method('buildLocationDomainObject') |
770
|
|
|
->with($this->equalTo($spiLocation)) |
771
|
|
|
->will($this->returnValue($locationMock)); |
772
|
|
|
|
773
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
774
|
|
|
->method('getPermissionsCriterion') |
775
|
|
|
->with('content', 'read') |
776
|
|
|
->will($this->returnValue(true)); |
777
|
|
|
|
778
|
|
|
$result = $service->findLocations($query, array(), true); |
779
|
|
|
|
780
|
|
|
$this->assertEquals( |
781
|
|
|
new SearchResult( |
782
|
|
|
array( |
783
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))), |
784
|
|
|
'totalCount' => 1, |
785
|
|
|
) |
786
|
|
|
), |
787
|
|
|
$result |
788
|
|
|
); |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
/** |
792
|
|
|
* Test for the findLocations() method. |
793
|
|
|
*/ |
794
|
|
|
public function testFindLocationsWithNoPermissionsFilter() |
795
|
|
|
{ |
796
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
797
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
798
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
799
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
800
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
801
|
|
|
$service = new SearchService( |
802
|
|
|
$repositoryMock, |
|
|
|
|
803
|
|
|
$searchHandlerMock, |
804
|
|
|
$domainMapperMock, |
|
|
|
|
805
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
806
|
|
|
new NullIndexer(), |
807
|
|
|
array() |
808
|
|
|
); |
809
|
|
|
|
810
|
|
|
$repositoryMock->expects($this->never())->method('hasAccess'); |
811
|
|
|
|
812
|
|
|
$serviceQuery = new LocationQuery(); |
813
|
|
|
$handlerQuery = new LocationQuery(array('filter' => new Criterion\MatchAll(), 'limit' => 25)); |
814
|
|
|
$spiLocation = new SPILocation(); |
815
|
|
|
$locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location'); |
816
|
|
|
|
817
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
818
|
|
|
$searchHandlerMock->expects($this->once()) |
819
|
|
|
->method('findLocations') |
820
|
|
|
->with($this->equalTo($handlerQuery)) |
821
|
|
|
->will( |
822
|
|
|
$this->returnValue( |
823
|
|
|
new SearchResult( |
824
|
|
|
array( |
825
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))), |
826
|
|
|
'totalCount' => 1, |
827
|
|
|
) |
828
|
|
|
) |
829
|
|
|
) |
830
|
|
|
); |
831
|
|
|
|
832
|
|
|
$domainMapperMock->expects($this->once()) |
833
|
|
|
->method('buildLocationDomainObject') |
834
|
|
|
->with($this->equalTo($spiLocation)) |
835
|
|
|
->will($this->returnValue($locationMock)); |
836
|
|
|
|
837
|
|
|
$result = $service->findLocations($serviceQuery, array(), false); |
838
|
|
|
|
839
|
|
|
$this->assertEquals( |
840
|
|
|
new SearchResult( |
841
|
|
|
array( |
842
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))), |
843
|
|
|
'totalCount' => 1, |
844
|
|
|
) |
845
|
|
|
), |
846
|
|
|
$result |
847
|
|
|
); |
848
|
|
|
} |
849
|
|
|
|
850
|
|
View Code Duplication |
public function providerForFindLocationsWhenDomainMapperThrowsException() |
851
|
|
|
{ |
852
|
|
|
return [ |
853
|
|
|
[ |
854
|
|
|
new NotFoundException('content', 'id = 33'), |
855
|
|
|
true, |
856
|
|
|
], |
857
|
|
|
[ |
858
|
|
|
new UnauthorizedException('content', 'read', ['id' => 33]), |
859
|
|
|
false, |
860
|
|
|
], |
861
|
|
|
]; |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
/** |
865
|
|
|
* Test for the findLocations() method when search is out of sync with persistence. |
866
|
|
|
* |
867
|
|
|
* @dataProvider providerForFindLocationsWhenDomainMapperThrowsException |
868
|
|
|
* @covers \eZ\Publish\Core\Repository\SearchService::findLocations |
869
|
|
|
*/ |
870
|
|
|
public function testFindLocationsBackgroundIndexerWhenDomainMapperThrowsException($e, $index = true) |
871
|
|
|
{ |
872
|
|
|
$indexer = $this->createMock(BackgroundIndexer::class); |
873
|
|
View Code Duplication |
if ($index) { |
|
|
|
|
874
|
|
|
$indexer->expects($this->once()) |
875
|
|
|
->method('registerLocation') |
876
|
|
|
->with($this->isInstanceOf(SPILocation::class)); |
877
|
|
|
} else { |
878
|
|
|
$indexer->expects($this->never())->method($this->anything()); |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
$service = $this->getMockBuilder(SearchService::class) |
882
|
|
|
->setConstructorArgs([ |
883
|
|
|
$this->getRepositoryMock(), |
884
|
|
|
$searchHandler = $this->getSPIMockHandler('Search\\Handler'), |
885
|
|
|
$mapper = $this->getDomainMapperMock(), |
886
|
|
|
$this->getPermissionCriterionResolverMock(), |
887
|
|
|
$indexer, |
888
|
|
|
])->setMethods(['addPermissionsCriterion']) |
889
|
|
|
->getMock(); |
890
|
|
|
|
891
|
|
|
$location = new SPILocation(['id' => 44]); |
892
|
|
|
$service->expects($this->once()) |
893
|
|
|
->method('addPermissionsCriterion') |
894
|
|
|
->with($this->isInstanceOf(Criterion::class)) |
895
|
|
|
->willReturn(true); |
896
|
|
|
|
897
|
|
|
$result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $location])], 'totalCount' => 2]); |
898
|
|
|
$searchHandler->expects($this->once()) |
899
|
|
|
->method('findLocations') |
900
|
|
|
->with($this->isInstanceOf(LocationQuery::class), $this->isType('array')) |
901
|
|
|
->willReturn($result); |
902
|
|
|
|
903
|
|
|
$mapper->expects($this->once()) |
904
|
|
|
->method('buildLocationDomainObject') |
905
|
|
|
->with($this->isInstanceOf(SPILocation::class)) |
906
|
|
|
->willThrowException($e); |
907
|
|
|
|
908
|
|
|
$finalResult = $service->findLocations(new LocationQuery()); |
909
|
|
|
|
910
|
|
|
$this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty'); |
911
|
|
|
$this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1'); |
912
|
|
|
} |
913
|
|
|
|
914
|
|
|
/** |
915
|
|
|
* Test for the findLocations() method. |
916
|
|
|
* |
917
|
|
|
* @expectedException \Exception |
918
|
|
|
* @expectedExceptionMessage Handler threw an exception |
919
|
|
|
*/ |
920
|
|
View Code Duplication |
public function testFindLocationsThrowsHandlerException() |
921
|
|
|
{ |
922
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
923
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
924
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
925
|
|
|
$permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(); |
926
|
|
|
|
927
|
|
|
$service = new SearchService( |
928
|
|
|
$repositoryMock, |
|
|
|
|
929
|
|
|
$searchHandlerMock, |
930
|
|
|
$this->getDomainMapperMock(), |
|
|
|
|
931
|
|
|
$permissionsCriterionResolverMock, |
|
|
|
|
932
|
|
|
new NullIndexer(), |
933
|
|
|
array() |
934
|
|
|
); |
935
|
|
|
|
936
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */ |
937
|
|
|
$criterionMock = $this |
938
|
|
|
->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion') |
939
|
|
|
->disableOriginalConstructor() |
940
|
|
|
->getMock(); |
941
|
|
|
$query = new LocationQuery(array('filter' => $criterionMock)); |
942
|
|
|
|
943
|
|
|
$permissionsCriterionResolverMock->expects($this->once()) |
944
|
|
|
->method('getPermissionsCriterion') |
945
|
|
|
->with('content', 'read') |
946
|
|
|
->will($this->throwException(new Exception('Handler threw an exception'))); |
947
|
|
|
|
948
|
|
|
$service->findLocations($query, array(), true); |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* Test for the findLocations() method. |
953
|
|
|
*/ |
954
|
|
|
public function testFindLocationsWithDefaultQueryValues() |
955
|
|
|
{ |
956
|
|
|
$repositoryMock = $this->getRepositoryMock(); |
957
|
|
|
/** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */ |
958
|
|
|
$searchHandlerMock = $this->getSPIMockHandler('Search\\Handler'); |
959
|
|
|
$domainMapperMock = $this->getDomainMapperMock(); |
960
|
|
|
$service = new SearchService( |
961
|
|
|
$repositoryMock, |
|
|
|
|
962
|
|
|
$searchHandlerMock, |
963
|
|
|
$domainMapperMock, |
|
|
|
|
964
|
|
|
$this->getPermissionCriterionResolverMock(), |
|
|
|
|
965
|
|
|
new NullIndexer(), |
966
|
|
|
array() |
967
|
|
|
); |
968
|
|
|
|
969
|
|
|
$spiLocation = new SPILocation(); |
970
|
|
|
$locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location'); |
971
|
|
|
$domainMapperMock->expects($this->once()) |
972
|
|
|
->method('buildLocationDomainObject') |
973
|
|
|
->with($this->equalTo($spiLocation)) |
974
|
|
|
->will($this->returnValue($locationMock)); |
975
|
|
|
|
976
|
|
|
/* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */ |
977
|
|
|
$searchHandlerMock |
978
|
|
|
->expects($this->once()) |
979
|
|
|
->method('findLocations') |
980
|
|
|
->with( |
981
|
|
|
new LocationQuery( |
982
|
|
|
array( |
983
|
|
|
'filter' => new Criterion\MatchAll(), |
984
|
|
|
'limit' => 25, |
985
|
|
|
) |
986
|
|
|
) |
987
|
|
|
) |
988
|
|
|
->will( |
989
|
|
|
$this->returnValue( |
990
|
|
|
new SearchResult( |
991
|
|
|
array( |
992
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))), |
993
|
|
|
'totalCount' => 1, |
994
|
|
|
) |
995
|
|
|
) |
996
|
|
|
) |
997
|
|
|
); |
998
|
|
|
|
999
|
|
|
$result = $service->findLocations(new LocationQuery(), array(), false); |
1000
|
|
|
|
1001
|
|
|
$this->assertEquals( |
1002
|
|
|
new SearchResult( |
1003
|
|
|
array( |
1004
|
|
|
'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))), |
1005
|
|
|
'totalCount' => 1, |
1006
|
|
|
) |
1007
|
|
|
), |
1008
|
|
|
$result |
1009
|
|
|
); |
1010
|
|
|
} |
1011
|
|
|
|
1012
|
|
|
/** |
1013
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper |
1014
|
|
|
*/ |
1015
|
|
|
protected function getDomainMapperMock() |
1016
|
|
|
{ |
1017
|
|
|
if (!isset($this->domainMapperMock)) { |
1018
|
|
|
$this->domainMapperMock = $this |
1019
|
|
|
->getMockBuilder(DomainMapper::class) |
1020
|
|
|
->disableOriginalConstructor() |
1021
|
|
|
->getMock(); |
1022
|
|
|
} |
1023
|
|
|
|
1024
|
|
|
return $this->domainMapperMock; |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
|
|
/** |
1028
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\PermissionCriterionResolver |
1029
|
|
|
*/ |
1030
|
|
|
protected function getPermissionCriterionResolverMock() |
1031
|
|
|
{ |
1032
|
|
|
if (!isset($this->permissionsCriterionResolverMock)) { |
1033
|
|
|
$this->permissionsCriterionResolverMock = $this |
1034
|
|
|
->getMockBuilder(PermissionCriterionResolver::class) |
1035
|
|
|
->disableOriginalConstructor() |
1036
|
|
|
->getMock(); |
1037
|
|
|
} |
1038
|
|
|
|
1039
|
|
|
return $this->permissionsCriterionResolverMock; |
1040
|
|
|
} |
1041
|
|
|
} |
1042
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.