Completed
Push — master ( 727c99...0dc4f6 )
by André
21:06
created

testFindWithOffsetToNonexistent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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\Search\Legacy\Tests\Content;
10
11
use eZ\Publish\Core\Persistence;
12
use eZ\Publish\Core\Search\Legacy\Content;
13
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter;
14
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseConverter;
15
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
16
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
17
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
18
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
19
use eZ\Publish\Core\Search\Legacy\Content\Location\Gateway\CriterionHandler as LocationCriterionHandler;
20
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler as CommonCriterionHandler;
21
use eZ\Publish\Core\Search\Legacy\Content\Location\Gateway\SortClauseHandler as LocationSortClauseHandler;
22
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseHandler as CommonSortClauseHandler;
23
use eZ\Publish\Core\Search\Legacy\Content\Gateway as ContentGateway;
24
use eZ\Publish\Core\Persistence\Legacy\Content\Mapper as ContentMapper;
25
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper;
26
27
/**
28
 * Location Search test case for ContentSearchHandler.
29
 */
30
class HandlerLocationTest extends AbstractTestCase
31
{
32
    /**
33
     * Returns the location search handler to test.
34
     *
35
     * This method returns a fully functional search handler to perform tests on.
36
     *
37
     * @param array $fullTextSearchConfiguration
38
     *
39
     * @return \eZ\Publish\Core\Search\Legacy\Content\Handler
40
     */
41
    protected function getContentSearchHandler(array $fullTextSearchConfiguration = array())
42
    {
43
        $transformationProcessor = new Persistence\TransformationProcessor\DefinitionBased(
44
            new Persistence\TransformationProcessor\DefinitionBased\Parser(),
45
            new Persistence\TransformationProcessor\PcreCompiler(
46
                new Persistence\Utf8Converter()
47
            ),
48
            glob(__DIR__ . '/../../../../Persistence/Tests/TransformationProcessor/_fixtures/transformations/*.tr')
49
        );
50
        $commaSeparatedCollectionValueHandler = new CommonCriterionHandler\FieldValue\Handler\Collection(
51
            $this->getDatabaseHandler(),
52
            $transformationProcessor,
53
            ','
54
        );
55
        $hyphenSeparatedCollectionValueHandler = new CommonCriterionHandler\FieldValue\Handler\Collection(
56
            $this->getDatabaseHandler(),
57
            $transformationProcessor,
58
            '-'
59
        );
60
        $simpleValueHandler = new CommonCriterionHandler\FieldValue\Handler\Simple(
61
            $this->getDatabaseHandler(),
62
            $transformationProcessor
63
        );
64
        $compositeValueHandler = new CommonCriterionHandler\FieldValue\Handler\Composite(
65
            $this->getDatabaseHandler(),
66
            $transformationProcessor
67
        );
68
69
        return new Content\Handler(
70
            $this->createMock(ContentGateway::class),
71
            new Content\Location\Gateway\DoctrineDatabase(
72
                $this->getDatabaseHandler(),
73
                new CriteriaConverter(
74
                    array(
75
                        new LocationCriterionHandler\LocationId($this->getDatabaseHandler()),
76
                        new LocationCriterionHandler\ParentLocationId($this->getDatabaseHandler()),
77
                        new LocationCriterionHandler\LocationRemoteId($this->getDatabaseHandler()),
78
                        new LocationCriterionHandler\Subtree($this->getDatabaseHandler()),
79
                        new LocationCriterionHandler\Visibility($this->getDatabaseHandler()),
80
                        new LocationCriterionHandler\Location\Depth($this->getDatabaseHandler()),
81
                        new LocationCriterionHandler\Location\Priority($this->getDatabaseHandler()),
82
                        new LocationCriterionHandler\Location\IsMainLocation($this->getDatabaseHandler()),
83
                        new CommonCriterionHandler\ContentId($this->getDatabaseHandler()),
84
                        new CommonCriterionHandler\ContentTypeGroupId($this->getDatabaseHandler()),
85
                        new CommonCriterionHandler\ContentTypeId($this->getDatabaseHandler()),
86
                        new CommonCriterionHandler\ContentTypeIdentifier(
87
                            $this->getDatabaseHandler(),
88
                            $this->getContentTypeHandler()
89
                        ),
90
                        new CommonCriterionHandler\DateMetadata($this->getDatabaseHandler()),
91
                        new CommonCriterionHandler\Field(
92
                            $this->getDatabaseHandler(),
93
                            $this->getContentTypeHandler(),
94
                            $this->getLanguageHandler(),
95
                            $this->getConverterRegistry(),
96
                            new CommonCriterionHandler\FieldValue\Converter(
97
                                new CommonCriterionHandler\FieldValue\HandlerRegistry(
98
                                    array(
99
                                        'ezboolean' => $simpleValueHandler,
100
                                        'ezcountry' => $commaSeparatedCollectionValueHandler,
101
                                        'ezdate' => $simpleValueHandler,
102
                                        'ezdatetime' => $simpleValueHandler,
103
                                        'ezemail' => $simpleValueHandler,
104
                                        'ezinteger' => $simpleValueHandler,
105
                                        'ezobjectrelation' => $simpleValueHandler,
106
                                        'ezobjectrelationlist' => $commaSeparatedCollectionValueHandler,
107
                                        'ezselection' => $hyphenSeparatedCollectionValueHandler,
108
                                        'eztime' => $simpleValueHandler,
109
                                    )
110
                                ),
111
                                $compositeValueHandler
112
                            ),
113
                            $transformationProcessor
114
                        ),
115
                        new CommonCriterionHandler\FullText(
116
                            $this->getDatabaseHandler(),
117
                            $transformationProcessor,
118
                            $fullTextSearchConfiguration
119
                        ),
120
                        new CommonCriterionHandler\LanguageCode(
121
                            $this->getDatabaseHandler(),
122
                            $this->getLanguageMaskGenerator()
123
                        ),
124
                        new CommonCriterionHandler\LogicalAnd($this->getDatabaseHandler()),
125
                        new CommonCriterionHandler\LogicalNot($this->getDatabaseHandler()),
126
                        new CommonCriterionHandler\LogicalOr($this->getDatabaseHandler()),
127
                        new CommonCriterionHandler\MapLocationDistance(
128
                            $this->getDatabaseHandler(),
129
                            $this->getContentTypeHandler(),
130
                            $this->getLanguageHandler()
131
                        ),
132
                        new CommonCriterionHandler\MatchAll($this->getDatabaseHandler()),
133
                        new CommonCriterionHandler\ObjectStateId($this->getDatabaseHandler()),
134
                        new CommonCriterionHandler\FieldRelation(
135
                            $this->getDatabaseHandler(),
136
                            $this->getContentTypeHandler(),
137
                            $this->getLanguageHandler()
138
                        ),
139
                        new CommonCriterionHandler\RemoteId($this->getDatabaseHandler()),
140
                        new CommonCriterionHandler\SectionId($this->getDatabaseHandler()),
141
                        new CommonCriterionHandler\UserMetadata($this->getDatabaseHandler()),
142
                    )
143
                ),
144
                new SortClauseConverter(
145
                    array(
146
                        new LocationSortClauseHandler\Location\Id($this->getDatabaseHandler()),
147
                        new CommonSortClauseHandler\ContentId($this->getDatabaseHandler()),
148
                    )
149
                ),
150
                $this->getLanguageHandler()
151
            ),
152
            new Content\WordIndexer\Gateway\DoctrineDatabase(
153
                $this->getDatabaseHandler(),
154
                $this->getContentTypeHandler(),
155
                $transformationProcessor,
156
                new Content\WordIndexer\Repository\SearchIndex($this->getDatabaseHandler()),
157
                $this->getFullTextSearchConfiguration()
158
            ),
159
            $this->createMock(ContentMapper::class),
160
            $this->getLocationMapperMock(),
161
            $this->getLanguageHandler(),
162
            $this->getFullTextMapper($this->getContentTypeHandler())
0 ignored issues
show
Compatibility introduced by
$this->getContentTypeHandler() of type object<eZ\Publish\SPI\Pe...e\Content\Type\Handler> is not a sub-type of object<eZ\Publish\Core\P...y\Content\Type\Handler>. It seems like you assume a concrete implementation of the interface eZ\Publish\SPI\Persistence\Content\Type\Handler to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
163
        );
164
    }
165
166
    /**
167
     * Returns a location mapper mock.
168
     *
169
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper
170
     */
171 View Code Duplication
    protected function getLocationMapperMock()
172
    {
173
        $mapperMock = $this->getMockBuilder(LocationMapper::class)
174
            ->setMethods(array('createLocationsFromRows'))
175
            ->getMock();
176
        $mapperMock
177
            ->expects($this->any())
178
            ->method('createLocationsFromRows')
179
            ->with($this->isType('array'))
180
            ->will(
181
                $this->returnCallback(
182
                    function ($rows) {
183
                        $locations = array();
184
                        foreach ($rows as $row) {
185
                            $locationId = (int)$row['node_id'];
186
                            if (!isset($locations[$locationId])) {
187
                                $locations[$locationId] = new SPILocation();
188
                                $locations[$locationId]->id = $locationId;
189
                            }
190
                        }
191
192
                        return array_values($locations);
193
                    }
194
                )
195
            );
196
197
        return $mapperMock;
198
    }
199
200
    public function testFindWithoutOffsetLimit()
201
    {
202
        $handler = $this->getContentSearchHandler();
203
204
        $searchResult = $handler->findLocations(
205
            new LocationQuery(
206
                array(
207
                    'filter' => new Criterion\LocationId(2),
208
                )
209
            )
210
        );
211
212
        $this->assertEquals(1, $searchResult->totalCount);
213
        $this->assertCount(1, $searchResult->searchHits);
214
    }
215
216
    public function testFindWithZeroLimit()
217
    {
218
        $handler = $this->getContentSearchHandler();
219
220
        $searchResult = $handler->findLocations(
221
            new LocationQuery(
222
                array(
223
                    'filter' => new Criterion\LocationId(2),
224
                    'offset' => 0,
225
                    'limit' => 0,
226
                )
227
            )
228
        );
229
230
        $this->assertEquals(1, $searchResult->totalCount);
231
        $this->assertEquals(array(), $searchResult->searchHits);
232
    }
233
234
    /**
235
     * Issue with PHP_MAX_INT limit overflow in databases.
236
     */
237
    public function testFindWithNullLimit()
238
    {
239
        $handler = $this->getContentSearchHandler();
240
241
        $searchResult = $handler->findLocations(
242
            new LocationQuery(
243
                array(
244
                    'filter' => new Criterion\LocationId(2),
245
                    'offset' => 0,
246
                    'limit' => null,
247
                )
248
            )
249
        );
250
251
        $this->assertEquals(1, $searchResult->totalCount);
252
        $this->assertCount(1, $searchResult->searchHits);
253
    }
254
255
    /**
256
     * Issue with offsetting to the nonexistent results produces \ezcQueryInvalidParameterException exception.
257
     */
258
    public function testFindWithOffsetToNonexistent()
259
    {
260
        $handler = $this->getContentSearchHandler();
261
262
        $searchResult = $handler->findLocations(
263
            new LocationQuery(
264
                array(
265
                    'filter' => new Criterion\LocationId(2),
266
                    'offset' => 1000,
267
                    'limit' => null,
268
                )
269
            )
270
        );
271
272
        $this->assertEquals(1, $searchResult->totalCount);
273
        $this->assertEquals(array(), $searchResult->searchHits);
274
    }
275
276
    public function testLocationIdFilter()
277
    {
278
        $this->assertSearchResults(
279
            array(12, 13),
280
            $this->getContentSearchHandler()->findLocations(
281
                new LocationQuery(
282
                    array(
283
                        'filter' => new Criterion\LocationId(
284
                            array(4, 12, 13)
285
                        ),
286
                        'limit' => 10,
287
                    )
288
                )
289
            )
290
        );
291
    }
292
293
    public function testParentLocationIdFilter()
294
    {
295
        $this->assertSearchResults(
296
            array(12, 13, 14, 44, 227),
297
            $this->getContentSearchHandler()->findLocations(
298
                new LocationQuery(
299
                    array(
300
                        'filter' => new Criterion\ParentLocationId(5),
301
                        'limit' => 10,
302
                    )
303
                )
304
            )
305
        );
306
    }
307
308 View Code Duplication
    public function testLocationIdAndCombinatorFilter()
309
    {
310
        $this->assertSearchResults(
311
            array(13),
312
            $this->getContentSearchHandler()->findLocations(
313
                new LocationQuery(
314
                    array(
315
                        'filter' => new Criterion\LogicalAnd(
316
                            array(
317
                                new Criterion\LocationId(
318
                                    array(4, 12, 13)
319
                                ),
320
                                new Criterion\LocationId(
321
                                    array(13, 44)
322
                                ),
323
                            )
324
                        ),
325
                        'limit' => 10,
326
                    )
327
                )
328
            )
329
        );
330
    }
331
332 View Code Duplication
    public function testLocationIdParentLocationIdAndCombinatorFilter()
333
    {
334
        $this->assertSearchResults(
335
            array(44, 160),
336
            $this->getContentSearchHandler()->findLocations(
337
                new LocationQuery(
338
                    array(
339
                        'filter' => new Criterion\LogicalAnd(
340
                            array(
341
                                new Criterion\LocationId(
342
                                    array(2, 44, 160, 166)
343
                                ),
344
                                new Criterion\ParentLocationId(
345
                                    array(5, 156)
346
                                ),
347
                            )
348
                        ),
349
                        'limit' => 10,
350
                    )
351
                )
352
            )
353
        );
354
    }
355
356
    public function testContentDepthFilterEq()
357
    {
358
        $this->assertSearchResults(
359
            array(2, 5, 43, 48, 58),
360
            $this->getContentSearchHandler()->findLocations(
361
                new LocationQuery(
362
                    array(
363
                        'filter' => new Criterion\Location\Depth(Criterion\Operator::EQ, 1),
364
                        'limit' => 10,
365
                    )
366
                )
367
            )
368
        );
369
    }
370
371 View Code Duplication
    public function testContentDepthFilterIn()
372
    {
373
        $this->assertSearchResults(
374
            array(2, 5, 12, 13, 14, 43, 44, 48, 51, 52, 53, 54, 56, 58, 59, 69, 77, 86, 96, 107, 153, 156, 167, 190, 227),
375
            $this->getContentSearchHandler()->findLocations(
376
                new LocationQuery(
377
                    array(
378
                        'filter' => new Criterion\Location\Depth(Criterion\Operator::IN, array(1, 2)),
379
                        'limit' => 50,
380
                    )
381
                )
382
            )
383
        );
384
    }
385
386
    public function testContentDepthFilterBetween()
387
    {
388
        $this->assertSearchResults(
389
            array(2, 5, 43, 48, 58),
390
            $this->getContentSearchHandler()->findLocations(
391
                new LocationQuery(
392
                    array(
393
                        'filter' => new Criterion\Location\Depth(Criterion\Operator::BETWEEN, array(0, 1)),
394
                    )
395
                )
396
            )
397
        );
398
    }
399
400 View Code Duplication
    public function testContentDepthFilterGreaterThan()
401
    {
402
        $this->assertSearchResults(
403
            array(99, 102, 135, 136, 137, 139, 140, 142, 143, 144, 145, 148, 151, 174, 175, 177, 194, 196, 197, 198, 199, 200, 201, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 214, 215),
404
            $this->getContentSearchHandler()->findLocations(
405
                new LocationQuery(
406
                    array(
407
                        'filter' => new Criterion\Location\Depth(Criterion\Operator::GT, 4),
408
                        'limit' => 50,
409
                    )
410
                )
411
            )
412
        );
413
    }
414
415 View Code Duplication
    public function testContentDepthFilterGreaterThanOrEqual()
416
    {
417
        $this->assertSearchResults(
418
            array(99, 102, 135, 136, 137, 139, 140, 142, 143, 144, 145, 148, 151, 174, 175, 177, 194, 196, 197, 198, 199, 200, 201, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 214, 215),
419
            $this->getContentSearchHandler()->findLocations(
420
                new LocationQuery(
421
                    array(
422
                        'filter' => new Criterion\Location\Depth(Criterion\Operator::GTE, 5),
423
                        'limit' => 50,
424
                    )
425
                )
426
            )
427
        );
428
    }
429
430
    public function testContentDepthFilterLessThan()
431
    {
432
        $this->assertSearchResults(
433
            array(2, 5, 43, 48, 58),
434
            $this->getContentSearchHandler()->findLocations(
435
                new LocationQuery(
436
                    array(
437
                        'filter' => new Criterion\Location\Depth(Criterion\Operator::LT, 2),
438
                        'limit' => 10,
439
                    )
440
                )
441
            )
442
        );
443
    }
444
445 View Code Duplication
    public function testContentDepthFilterLessThanOrEqual()
446
    {
447
        $this->assertSearchResults(
448
            array(2, 5, 12, 13, 14, 43, 44, 48, 51, 52, 53, 54, 56, 58, 59, 69, 77, 86, 96, 107, 153, 156, 167, 190, 227),
449
            $this->getContentSearchHandler()->findLocations(
450
                new LocationQuery(
451
                    array(
452
                        'filter' => new Criterion\Location\Depth(Criterion\Operator::LTE, 2),
453
                        'limit' => 50,
454
                    )
455
                )
456
            )
457
        );
458
    }
459
460
    public function testLocationPriorityFilter()
461
    {
462
        $this->assertSearchResults(
463
            array(156, 167, 190),
464
            $this->getContentSearchHandler()->findLocations(
465
                new LocationQuery(
466
                    array(
467
                        'filter' => new Criterion\Location\Priority(
468
                            Criterion\Operator::BETWEEN,
469
                            array(1, 10)
470
                        ),
471
                        'limit' => 10,
472
                    )
473
                )
474
            )
475
        );
476
    }
477
478
    public function testLocationRemoteIdFilter()
479
    {
480
        $this->assertSearchResults(
481
            array(2, 5),
482
            $this->getContentSearchHandler()->findLocations(
483
                new LocationQuery(
484
                    array(
485
                        'filter' => new Criterion\LocationRemoteId(
486
                            array('3f6d92f8044aed134f32153517850f5a', 'f3e90596361e31d496d4026eb624c983')
487
                        ),
488
                        'limit' => 10,
489
                    )
490
                )
491
            )
492
        );
493
    }
494
495 View Code Duplication
    public function testVisibilityFilterVisible()
496
    {
497
        $this->assertSearchResults(
498
            array(2, 5, 12, 13, 14),
499
            $this->getContentSearchHandler()->findLocations(
500
                new LocationQuery(
501
                    array(
502
                        'filter' => new Criterion\Visibility(
503
                            Criterion\Visibility::VISIBLE
504
                        ),
505
                        'limit' => 5,
506
                        'sortClauses' => array(new SortClause\Location\Id()),
507
                    )
508
                )
509
            )
510
        );
511
    }
512
513
    public function testVisibilityFilterHidden()
514
    {
515
        $this->assertSearchResults(
516
            array(228),
517
            $this->getContentSearchHandler()->findLocations(
518
                new LocationQuery(
519
                    array(
520
                        'filter' => new Criterion\Visibility(
521
                            Criterion\Visibility::HIDDEN
522
                        ),
523
                    )
524
                )
525
            )
526
        );
527
    }
528
529 View Code Duplication
    public function testLocationNotCombinatorFilter()
530
    {
531
        $this->assertSearchResults(
532
            array(2, 5),
533
            $this->getContentSearchHandler()->findLocations(
534
                new LocationQuery(
535
                    array(
536
                        'filter' => new Criterion\LogicalAnd(
537
                            array(
538
                                new Criterion\LocationId(
539
                                    array(2, 5, 12, 356)
540
                                ),
541
                                new Criterion\LogicalNot(
542
                                    new Criterion\LocationId(
543
                                        array(12, 13, 14)
544
                                    )
545
                                ),
546
                            )
547
                        ),
548
                        'limit' => 10,
549
                    )
550
                )
551
            )
552
        );
553
    }
554
555
    public function testLocationOrCombinatorFilter()
556
    {
557
        $this->assertSearchResults(
558
            array(2, 5, 12, 13, 14),
559
            $this->getContentSearchHandler()->findLocations(
560
                new LocationQuery(
561
                    array(
562
                        'filter' => new Criterion\LogicalOr(
563
                            array(
564
                                new Criterion\LocationId(
565
                                    array(2, 5, 12)
566
                                ),
567
                                new Criterion\LocationId(
568
                                    array(12, 13, 14)
569
                                ),
570
                            )
571
                        ),
572
                        'limit' => 10,
573
                    )
574
                )
575
            )
576
        );
577
    }
578
579
    public function testContentIdFilterEquals()
580
    {
581
        $this->assertSearchResults(
582
            array(225),
583
            $this->getContentSearchHandler()->findLocations(
584
                new LocationQuery(
585
                    array(
586
                        'filter' => new Criterion\ContentId(223),
587
                    )
588
                )
589
            )
590
        );
591
    }
592
593
    public function testContentIdFilterIn()
594
    {
595
        $this->assertSearchResults(
596
            array(225, 226, 227),
597
            $this->getContentSearchHandler()->findLocations(
598
                new LocationQuery(
599
                    array(
600
                        'filter' => new Criterion\ContentId(
601
                            array(223, 224, 225)
602
                        ),
603
                    )
604
                )
605
            )
606
        );
607
    }
608
609 View Code Duplication
    public function testContentTypeGroupFilter()
610
    {
611
        $this->assertSearchResults(
612
            array(5, 12, 13, 14, 15, 44, 45, 227, 228),
613
            $this->getContentSearchHandler()->findLocations(
614
                new LocationQuery(
615
                    array(
616
                        'filter' => new Criterion\ContentTypeGroupId(2),
617
                        'limit' => 10,
618
                    )
619
                )
620
            )
621
        );
622
    }
623
624
    public function testContentTypeIdFilter()
625
    {
626
        $this->assertSearchResults(
627
            array(15, 45, 228),
628
            $this->getContentSearchHandler()->findLocations(
629
                new LocationQuery(
630
                    array(
631
                        'filter' => new Criterion\ContentTypeId(4),
632
                        'limit' => 10,
633
                    )
634
                )
635
            )
636
        );
637
    }
638
639 View Code Duplication
    public function testContentTypeIdentifierFilter()
640
    {
641
        $this->assertSearchResults(
642
            array(43, 48, 51, 52, 53),
643
            $this->getContentSearchHandler()->findLocations(
644
                new LocationQuery(
645
                    array(
646
                        'filter' => new Criterion\ContentTypeIdentifier('folder'),
647
                        'limit' => 5,
648
                        'sortClauses' => array(new SortClause\Location\Id()),
649
                    )
650
                )
651
            )
652
        );
653
    }
654
655 View Code Duplication
    public function testObjectStateIdFilter()
656
    {
657
        $this->assertSearchResults(
658
            array(5, 12, 13, 14, 15, 43, 44, 45, 48, 51),
659
            $this->getContentSearchHandler()->findLocations(
660
                new LocationQuery(
661
                    array(
662
                        'filter' => new Criterion\ObjectStateId(1),
663
                        'limit' => 10,
664
                        'sortClauses' => array(new SortClause\ContentId()),
665
                    )
666
                )
667
            )
668
        );
669
    }
670
671
    public function testObjectStateIdFilterIn()
672
    {
673
        $this->assertSearchResults(
674
            array(2, 5, 12, 13, 14, 15, 43, 44, 45, 48),
675
            $this->getContentSearchHandler()->findLocations(
676
                new LocationQuery(
677
                    array(
678
                        'filter' => new Criterion\ObjectStateId(array(1, 2)),
679
                        'limit' => 10,
680
                        'sortClauses' => array(new SortClause\Location\Id()),
681
                    )
682
                )
683
            )
684
        );
685
    }
686
687
    public function testRemoteIdFilter()
688
    {
689
        $this->assertSearchResults(
690
            array(5, 45),
691
            $this->getContentSearchHandler()->findLocations(
692
                new LocationQuery(
693
                    array(
694
                        'filter' => new Criterion\RemoteId(
695
                            array('f5c88a2209584891056f987fd965b0ba', 'faaeb9be3bd98ed09f606fc16d144eca')
696
                        ),
697
                        'limit' => 10,
698
                    )
699
                )
700
            )
701
        );
702
    }
703
704 View Code Duplication
    public function testSectionFilter()
705
    {
706
        $this->assertSearchResults(
707
            array(5, 12, 13, 14, 15, 44, 45, 228),
708
            $this->getContentSearchHandler()->findLocations(
709
                new LocationQuery(
710
                    array(
711
                        'filter' => new Criterion\SectionId(array(2)),
712
                        'limit' => 10,
713
                    )
714
                )
715
            )
716
        );
717
    }
718
719
    public function testDateMetadataFilterModifiedGreater()
720
    {
721
        $this->assertSearchResults(
722
            array(12, 227, 228),
723
            $this->getContentSearchHandler()->findLocations(
724
                new LocationQuery(
725
                    array(
726
                        'filter' => new Criterion\DateMetadata(
727
                            Criterion\DateMetadata::MODIFIED,
728
                            Criterion\Operator::GT,
729
                            1311154214
730
                        ),
731
                        'limit' => 10,
732
                    )
733
                )
734
            )
735
        );
736
    }
737
738
    public function testDateMetadataFilterModifiedGreaterOrEqual()
739
    {
740
        $this->assertSearchResults(
741
            array(12, 15, 227, 228),
742
            $this->getContentSearchHandler()->findLocations(
743
                new LocationQuery(
744
                    array(
745
                        'filter' => new Criterion\DateMetadata(
746
                            Criterion\DateMetadata::MODIFIED,
747
                            Criterion\Operator::GTE,
748
                            1311154214
749
                        ),
750
                        'limit' => 10,
751
                    )
752
                )
753
            )
754
        );
755
    }
756
757 View Code Duplication
    public function testDateMetadataFilterModifiedIn()
758
    {
759
        $this->assertSearchResults(
760
            array(12, 15, 227, 228),
761
            $this->getContentSearchHandler()->findLocations(
762
                new LocationQuery(
763
                    array(
764
                        'filter' => new Criterion\DateMetadata(
765
                            Criterion\DateMetadata::MODIFIED,
766
                            Criterion\Operator::IN,
767
                            array(1311154214, 1311154215)
768
                        ),
769
                        'limit' => 10,
770
                    )
771
                )
772
            )
773
        );
774
    }
775
776 View Code Duplication
    public function testDateMetadataFilterModifiedBetween()
777
    {
778
        $this->assertSearchResults(
779
            array(12, 15, 227, 228),
780
            $this->getContentSearchHandler()->findLocations(
781
                new LocationQuery(
782
                    array(
783
                        'filter' => new Criterion\DateMetadata(
784
                            Criterion\DateMetadata::MODIFIED,
785
                            Criterion\Operator::BETWEEN,
786
                            array(1311154213, 1311154215)
787
                        ),
788
                        'limit' => 10,
789
                    )
790
                )
791
            )
792
        );
793
    }
794
795 View Code Duplication
    public function testDateMetadataFilterCreatedBetween()
796
    {
797
        $this->assertSearchResults(
798
            array(68, 133, 227),
799
            $this->getContentSearchHandler()->findLocations(
800
                new LocationQuery(
801
                    array(
802
                        'filter' => new Criterion\DateMetadata(
803
                            Criterion\DateMetadata::CREATED,
804
                            Criterion\Operator::BETWEEN,
805
                            array(1299780749, 1311154215)
806
                        ),
807
                        'limit' => 10,
808
                    )
809
                )
810
            )
811
        );
812
    }
813
814
    public function testUserMetadataFilterOwnerWrongUserId()
815
    {
816
        $this->assertSearchResults(
817
            array(),
818
            $this->getContentSearchHandler()->findLocations(
819
                new LocationQuery(
820
                    array(
821
                        'filter' => new Criterion\UserMetadata(
822
                            Criterion\UserMetadata::OWNER,
823
                            Criterion\Operator::EQ,
824
                            2
825
                        ),
826
                    )
827
                )
828
            )
829
        );
830
    }
831
832 View Code Duplication
    public function testUserMetadataFilterOwnerAdministrator()
833
    {
834
        $this->assertSearchResults(
835
            array(2, 5, 12, 13, 14, 15, 43, 44, 45, 48),
836
            $this->getContentSearchHandler()->findLocations(
837
                new LocationQuery(
838
                    array(
839
                        'filter' => new Criterion\UserMetadata(
840
                            Criterion\UserMetadata::OWNER,
841
                            Criterion\Operator::EQ,
842
                            14
843
                        ),
844
                        'limit' => 10,
845
                        'sortClauses' => array(new SortClause\Location\Id()),
846
                    )
847
                )
848
            )
849
        );
850
    }
851
852
    public function testUserMetadataFilterOwnerEqAMember()
853
    {
854
        $this->assertSearchResults(
855
            array(225),
856
            $this->getContentSearchHandler()->findLocations(
857
                new LocationQuery(
858
                    array(
859
                        'filter' => new Criterion\UserMetadata(
860
                            Criterion\UserMetadata::OWNER,
861
                            Criterion\Operator::EQ,
862
                            226
863
                        ),
864
                    )
865
                )
866
            )
867
        );
868
    }
869
870
    public function testUserMetadataFilterOwnerInAMember()
871
    {
872
        $this->assertSearchResults(
873
            array(225),
874
            $this->getContentSearchHandler()->findLocations(
875
                new LocationQuery(
876
                    array(
877
                        'filter' => new Criterion\UserMetadata(
878
                            Criterion\UserMetadata::OWNER,
879
                            Criterion\Operator::IN,
880
                            array(226)
881
                        ),
882
                    )
883
                )
884
            )
885
        );
886
    }
887
888
    public function testUserMetadataFilterCreatorEqAMember()
889
    {
890
        $this->assertSearchResults(
891
            array(225),
892
            $this->getContentSearchHandler()->findLocations(
893
                new LocationQuery(
894
                    array(
895
                        'filter' => new Criterion\UserMetadata(
896
                            Criterion\UserMetadata::MODIFIER,
897
                            Criterion\Operator::EQ,
898
                            226
899
                        ),
900
                    )
901
                )
902
            )
903
        );
904
    }
905
906
    public function testUserMetadataFilterCreatorInAMember()
907
    {
908
        $this->assertSearchResults(
909
            array(225),
910
            $this->getContentSearchHandler()->findLocations(
911
                new LocationQuery(
912
                    array(
913
                        'filter' => new Criterion\UserMetadata(
914
                            Criterion\UserMetadata::MODIFIER,
915
                            Criterion\Operator::IN,
916
                            array(226)
917
                        ),
918
                    )
919
                )
920
            )
921
        );
922
    }
923
924
    public function testUserMetadataFilterEqGroupMember()
925
    {
926
        $this->assertSearchResults(
927
            array(225),
928
            $this->getContentSearchHandler()->findLocations(
929
                new LocationQuery(
930
                    array(
931
                        'filter' => new Criterion\UserMetadata(
932
                            Criterion\UserMetadata::GROUP,
933
                            Criterion\Operator::EQ,
934
                            11
935
                        ),
936
                    )
937
                )
938
            )
939
        );
940
    }
941
942
    public function testUserMetadataFilterInGroupMember()
943
    {
944
        $this->assertSearchResults(
945
            array(225),
946
            $this->getContentSearchHandler()->findLocations(
947
                new LocationQuery(
948
                    array(
949
                        'filter' => new Criterion\UserMetadata(
950
                            Criterion\UserMetadata::GROUP,
951
                            Criterion\Operator::IN,
952
                            array(11)
953
                        ),
954
                    )
955
                )
956
            )
957
        );
958
    }
959
960
    public function testUserMetadataFilterEqGroupMemberNoMatch()
961
    {
962
        $this->assertSearchResults(
963
            array(),
964
            $this->getContentSearchHandler()->findLocations(
965
                new LocationQuery(
966
                    array(
967
                        'filter' => new Criterion\UserMetadata(
968
                            Criterion\UserMetadata::GROUP,
969
                            Criterion\Operator::EQ,
970
                            13
971
                        ),
972
                    )
973
                )
974
            )
975
        );
976
    }
977
978
    public function testUserMetadataFilterInGroupMemberNoMatch()
979
    {
980
        $this->assertSearchResults(
981
            array(),
982
            $this->getContentSearchHandler()->findLocations(
983
                new LocationQuery(
984
                    array(
985
                        'filter' => new Criterion\UserMetadata(
986
                            Criterion\UserMetadata::GROUP,
987
                            Criterion\Operator::IN,
988
                            array(13)
989
                        ),
990
                    )
991
                )
992
            )
993
        );
994
    }
995
996 View Code Duplication
    public function testLanguageCodeFilter()
997
    {
998
        $this->assertSearchResults(
999
            array(2, 5, 12, 13, 14, 15, 43, 44, 45, 48),
1000
            $this->getContentSearchHandler()->findLocations(
1001
                new LocationQuery(
1002
                    array(
1003
                        'filter' => new Criterion\LanguageCode('eng-US'),
1004
                        'limit' => 10,
1005
                        'sortClauses' => array(new SortClause\Location\Id()),
1006
                    )
1007
                )
1008
            )
1009
        );
1010
    }
1011
1012
    public function testLanguageCodeFilterIn()
1013
    {
1014
        $this->assertSearchResults(
1015
            array(2, 5, 12, 13, 14, 15, 43, 44, 45, 48),
1016
            $this->getContentSearchHandler()->findLocations(
1017
                new LocationQuery(
1018
                    array(
1019
                        'filter' => new Criterion\LanguageCode(array('eng-US', 'eng-GB')),
1020
                        'limit' => 10,
1021
                        'sortClauses' => array(new SortClause\Location\Id()),
1022
                    )
1023
                )
1024
            )
1025
        );
1026
    }
1027
1028 View Code Duplication
    public function testLanguageCodeFilterWithAlwaysAvailable()
1029
    {
1030
        $this->assertSearchResults(
1031
            array(2, 5, 12, 13, 14, 15, 43, 44, 45, 48, 51, 52, 53, 58, 59, 70, 72, 76, 78, 82),
1032
            $this->getContentSearchHandler()->findLocations(
1033
                new LocationQuery(
1034
                    array(
1035
                        'filter' => new Criterion\LanguageCode('eng-GB', true),
1036
                        'limit' => 20,
1037
                        'sortClauses' => array(new SortClause\ContentId()),
1038
                    )
1039
                )
1040
            )
1041
        );
1042
    }
1043
1044
    public function testMatchAllFilter()
1045
    {
1046
        $result = $this->getContentSearchHandler()->findLocations(
1047
            new LocationQuery(
1048
                array(
1049
                    'filter' => new Criterion\MatchAll(),
1050
                    'limit' => 10,
1051
                    'sortClauses' => array(new SortClause\Location\Id()),
1052
                )
1053
            )
1054
        );
1055
1056
        $this->assertCount(10, $result->searchHits);
1057
        $this->assertEquals(186, $result->totalCount);
1058
        $this->assertSearchResults(
1059
            array(2, 5, 12, 13, 14, 15, 43, 44, 45, 48),
1060
            $result
1061
        );
1062
    }
1063
1064
    public function testFullTextFilter()
1065
    {
1066
        $this->assertSearchResults(
1067
            array(193),
1068
            $this->getContentSearchHandler()->findLocations(
1069
                new LocationQuery(
1070
                    array(
1071
                        'filter' => new Criterion\FullText('applied webpage'),
1072
                        'limit' => 10,
1073
                    )
1074
                )
1075
            )
1076
        );
1077
    }
1078
1079
    public function testFullTextWildcardFilter()
1080
    {
1081
        $this->assertSearchResults(
1082
            array(193),
1083
            $this->getContentSearchHandler()->findLocations(
1084
                new LocationQuery(
1085
                    array(
1086
                        'filter' => new Criterion\FullText('applie*'),
1087
                        'limit' => 10,
1088
                    )
1089
                )
1090
            )
1091
        );
1092
    }
1093
1094
    public function testFullTextDisabledWildcardFilter()
1095
    {
1096
        $this->assertSearchResults(
1097
            array(),
1098
            $this->getContentSearchHandler(array('enableWildcards' => false))->findLocations(
1099
                new LocationQuery(
1100
                    array(
1101
                        'filter' => new Criterion\FullText('applie*'),
1102
                        'limit' => 10,
1103
                    )
1104
                )
1105
            )
1106
        );
1107
    }
1108
1109
    public function testFullTextFilterStopwordRemoval()
1110
    {
1111
        $handler = $this->getContentSearchHandler(
1112
            array(
1113
                'stopWordThresholdFactor' => 0.1,
1114
            )
1115
        );
1116
        $this->assertSearchResults(
1117
            array(),
1118
            $handler->findLocations(
1119
                new LocationQuery(
1120
                    array(
1121
                        'filter' => new Criterion\FullText('the'),
1122
                        'limit' => 10,
1123
                    )
1124
                )
1125
            )
1126
        );
1127
    }
1128
1129
    public function testFullTextFilterNoStopwordRemoval()
1130
    {
1131
        $handler = $this->getContentSearchHandler(
1132
            array(
1133
                'stopWordThresholdFactor' => 1,
1134
            )
1135
        );
1136
1137
        $result = $handler->findLocations(
1138
            new LocationQuery(
1139
                array(
1140
                    'filter' => new Criterion\FullText(
1141
                        'the'
1142
                    ),
1143
                    'limit' => 10,
1144
                )
1145
            )
1146
        );
1147
1148
        $this->assertEquals(26, $result->totalCount);
1149
        $this->assertCount(10, $result->searchHits);
1150
        $this->assertEquals(
1151
            10,
1152
            count(
1153
                array_map(
1154
                    function ($hit) {
1155
                        return $hit->valueObject->id;
1156
                    },
1157
                    $result->searchHits
1158
                )
1159
            )
1160
        );
1161
    }
1162
1163
    /**
1164
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1165
     */
1166
    public function testFullTextFilterInvalidStopwordThreshold()
1167
    {
1168
        $this->getContentSearchHandler(
1169
            array(
1170
                'stopWordThresholdFactor' => 2,
1171
            )
1172
        );
1173
    }
1174
1175
    public function testFieldRelationFilterContainsSingle()
1176
    {
1177
        $this->assertSearchResults(
1178
            array(69),
1179
            $this->getContentSearchHandler()->findLocations(
1180
                new LocationQuery(
1181
                    array(
1182
                        'filter' => new Criterion\FieldRelation(
1183
                            'billboard',
1184
                            Criterion\Operator::CONTAINS,
1185
                            array(60)
1186
                        ),
1187
                    )
1188
                )
1189
            )
1190
        );
1191
    }
1192
1193
    public function testFieldRelationFilterContainsSingleNoMatch()
1194
    {
1195
        $this->assertSearchResults(
1196
            array(),
1197
            $this->getContentSearchHandler()->findLocations(
1198
                new LocationQuery(
1199
                    array(
1200
                        'filter' => new Criterion\FieldRelation(
1201
                            'billboard',
1202
                            Criterion\Operator::CONTAINS,
1203
                            array(4)
1204
                        ),
1205
                    )
1206
                )
1207
            )
1208
        );
1209
    }
1210
1211 View Code Duplication
    public function testFieldRelationFilterContainsArray()
1212
    {
1213
        $this->assertSearchResults(
1214
            array(69),
1215
            $this->getContentSearchHandler()->findLocations(
1216
                new LocationQuery(
1217
                    array(
1218
                        'filter' => new Criterion\FieldRelation(
1219
                            'billboard',
1220
                            Criterion\Operator::CONTAINS,
1221
                            array(60, 75)
1222
                        ),
1223
                    )
1224
                )
1225
            )
1226
        );
1227
    }
1228
1229
    public function testFieldRelationFilterContainsArrayNotMatch()
1230
    {
1231
        $this->assertSearchResults(
1232
            array(),
1233
            $this->getContentSearchHandler()->findLocations(
1234
                new LocationQuery(
1235
                    array(
1236
                        'filter' => new Criterion\FieldRelation(
1237
                            'billboard',
1238
                            Criterion\Operator::CONTAINS,
1239
                            array(60, 64)
1240
                        ),
1241
                    )
1242
                )
1243
            )
1244
        );
1245
    }
1246
1247 View Code Duplication
    public function testFieldRelationFilterInArray()
1248
    {
1249
        $this->assertSearchResults(
1250
            array(69, 77),
1251
            $this->getContentSearchHandler()->findLocations(
1252
                new LocationQuery(
1253
                    array(
1254
                        'filter' => new Criterion\FieldRelation(
1255
                            'billboard',
1256
                            Criterion\Operator::IN,
1257
                            array(60, 64)
1258
                        ),
1259
                    )
1260
                )
1261
            )
1262
        );
1263
    }
1264
1265
    public function testFieldRelationFilterInArrayNotMatch()
1266
    {
1267
        $this->assertSearchResults(
1268
            array(),
1269
            $this->getContentSearchHandler()->findLocations(
1270
                new LocationQuery(
1271
                    array(
1272
                        'filter' => new Criterion\FieldRelation(
1273
                            'billboard',
1274
                            Criterion\Operator::IN,
1275
                            array(4, 10)
1276
                        ),
1277
                    )
1278
                )
1279
            )
1280
        );
1281
    }
1282
1283
    public function testFieldFilter()
1284
    {
1285
        $this->assertSearchResults(
1286
            array(12),
1287
            $this->getContentSearchHandler()->findLocations(
1288
                new LocationQuery(
1289
                    array(
1290
                        'filter' => new Criterion\Field(
1291
                            'name',
1292
                            Criterion\Operator::EQ,
1293
                            'members'
1294
                        ),
1295
                        'limit' => 10,
1296
                    )
1297
                )
1298
            )
1299
        );
1300
    }
1301
1302
    public function testFieldFilterIn()
1303
    {
1304
        $this->assertSearchResults(
1305
            array(12, 44),
1306
            $this->getContentSearchHandler()->findLocations(
1307
                new LocationQuery(
1308
                    array(
1309
                        'filter' => new Criterion\Field(
1310
                            'name',
1311
                            Criterion\Operator::IN,
1312
                            array('members', 'anonymous users')
1313
                        ),
1314
                        'limit' => 10,
1315
                    )
1316
                )
1317
            )
1318
        );
1319
    }
1320
1321
    public function testFieldFilterContainsPartial()
1322
    {
1323
        $this->assertSearchResults(
1324
            array(44),
1325
            $this->getContentSearchHandler()->findLocations(
1326
                new LocationQuery(
1327
                    array(
1328
                        'filter' => new Criterion\Field(
1329
                            'name',
1330
                            Criterion\Operator::CONTAINS,
1331
                            'nonymous use'
1332
                        ),
1333
                        'limit' => 10,
1334
                    )
1335
                )
1336
            )
1337
        );
1338
    }
1339
1340
    public function testFieldFilterContainsSimple()
1341
    {
1342
        $this->assertSearchResults(
1343
            array(79),
1344
            $this->getContentSearchHandler()->findLocations(
1345
                new LocationQuery(
1346
                    array(
1347
                        'filter' => new Criterion\Field(
1348
                            'publish_date',
1349
                            Criterion\Operator::CONTAINS,
1350
                            1174643880
1351
                        ),
1352
                        'limit' => 10,
1353
                    )
1354
                )
1355
            )
1356
        );
1357
    }
1358
1359
    public function testFieldFilterContainsSimpleNoMatch()
1360
    {
1361
        $this->assertSearchResults(
1362
            array(),
1363
            $this->getContentSearchHandler()->findLocations(
1364
                new LocationQuery(
1365
                    array(
1366
                        'filter' => new Criterion\Field(
1367
                            'publish_date',
1368
                            Criterion\Operator::CONTAINS,
1369
                            1174643
1370
                        ),
1371
                        'limit' => 10,
1372
                    )
1373
                )
1374
            )
1375
        );
1376
    }
1377
1378
    public function testFieldFilterBetween()
1379
    {
1380
        $this->assertSearchResults(
1381
            array(71, 73, 74),
1382
            $this->getContentSearchHandler()->findLocations(
1383
                new LocationQuery(
1384
                    array(
1385
                        'filter' => new Criterion\Field(
1386
                            'price',
1387
                            Criterion\Operator::BETWEEN,
1388
                            array(10000, 1000000)
1389
                        ),
1390
                        'limit' => 10,
1391
                    )
1392
                )
1393
            )
1394
        );
1395
    }
1396
1397 View Code Duplication
    public function testFieldFilterOr()
1398
    {
1399
        $this->assertSearchResults(
1400
            array(12, 71, 73, 74),
1401
            $this->getContentSearchHandler()->findLocations(
1402
                new LocationQuery(
1403
                    array(
1404
                        'filter' => new Criterion\LogicalOr(
1405
                            array(
1406
                                new Criterion\Field(
1407
                                    'name',
1408
                                    Criterion\Operator::EQ,
1409
                                    'members'
1410
                                ),
1411
                                new Criterion\Field(
1412
                                    'price',
1413
                                    Criterion\Operator::BETWEEN,
1414
                                    array(10000, 1000000)
1415
                                ),
1416
                            )
1417
                        ),
1418
                        'limit' => 10,
1419
                    )
1420
                )
1421
            )
1422
        );
1423
    }
1424
1425 View Code Duplication
    public function testIsMainLocationFilter()
1426
    {
1427
        $this->assertSearchResults(
1428
            array(225),
1429
            $this->getContentSearchHandler()->findLocations(
1430
                new LocationQuery(
1431
                    array(
1432
                        'filter' => new Criterion\LogicalAnd(
1433
                            array(
1434
                                new Criterion\ParentLocationId(224),
1435
                                new Criterion\Location\IsMainLocation(
1436
                                    Criterion\Location\IsMainLocation::MAIN
1437
                                ),
1438
                            )
1439
                        ),
1440
                        'limit' => 10,
1441
                    )
1442
                )
1443
            )
1444
        );
1445
    }
1446
1447 View Code Duplication
    public function testIsNotMainLocationFilter()
1448
    {
1449
        $this->assertSearchResults(
1450
            array(510),
1451
            $this->getContentSearchHandler()->findLocations(
1452
                new LocationQuery(
1453
                    array(
1454
                        'filter' => new Criterion\LogicalAnd(
1455
                            array(
1456
                                new Criterion\ParentLocationId(224),
1457
                                new Criterion\Location\IsMainLocation(
1458
                                    Criterion\Location\IsMainLocation::NOT_MAIN
1459
                                ),
1460
                            )
1461
                        ),
1462
                        'limit' => 10,
1463
                    )
1464
                )
1465
            )
1466
        );
1467
    }
1468
}
1469