Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

HandlerLocationSortTest   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 704
Duplicated Lines 51.14 %

Coupling/Cohesion

Components 2
Dependencies 63

Importance

Changes 0
Metric Value
dl 360
loc 704
rs 9.576
c 0
b 0
f 0
wmc 34
lcom 2
cbo 63

25 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 10 10 2
A assertSearchResults() 0 5 1
A getIds() 0 11 1
B getContentSearchHandler() 0 55 1
A getContentTypeHandler() 15 15 2
A getConverterRegistry() 0 23 2
A getLocationMapperMock() 29 29 3
A testNoSorting() 0 22 1
A testSortLocationPath() 0 20 1
A testSortLocationDepth() 20 20 1
A testSortLocationDepthAndPath() 0 23 1
A testSortLocationPriority() 22 22 1
A testSortDateModified() 0 22 1
A testSortDatePublished() 22 22 1
A testSortSectionIdentifier() 0 42 2
A testSortContentName() 0 22 1
A testSortContentId() 0 22 1
A testSortLocationId() 0 22 1
A testSortLocationVisibilityAscending() 22 22 1
A testSortLocationVisibilityDescending() 22 22 1
A testSortSectionName() 53 53 3
B testSortFieldText() 69 69 2
A testSortFieldNumeric() 32 32 1
A testSortIsMainLocationAscending() 22 22 1
A testSortIsMainLocationDescending() 22 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Location;
10
11
use eZ\Publish\Core\Persistence\Legacy\Tests\Content\LanguageAwareTestCase;
12
use eZ\Publish\Core\Search\Legacy\Content;
13
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
14
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
15
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
16
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
17
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter;
18
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler as CommonCriterionHandler;
19
use eZ\Publish\Core\Search\Legacy\Content\Location\Gateway\CriterionHandler as LocationCriterionHandler;
20
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseConverter;
21
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseHandler as CommonSortClauseHandler;
22
use eZ\Publish\Core\Search\Legacy\Content\Location\Gateway\SortClauseHandler as LocationSortClauseHandler;
23
use eZ\Publish\Core\Persistence\Legacy\Content\Type\Gateway\DoctrineDatabase as ContentTypeGateway;
24
use eZ\Publish\Core\Persistence\Legacy\Content\Type\Handler as ContentTypeHandler;
25
use eZ\Publish\Core\Persistence\Legacy\Content\Type\Mapper as ContentTypeMapper;
26
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
27
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry;
28
29
/**
30
 * Location Search test case for ContentSearchHandler.
31
 */
32
class HandlerLocationSortTest extends LanguageAwareTestCase
33
{
34
    protected static $setUp = false;
35
36
    /**
37
     * Only set up once for these read only tests on a large fixture.
38
     *
39
     * Skipping the reset-up, since setting up for these tests takes quite some
40
     * time, which is not required to spent, since we are only reading from the
41
     * database anyways.
42
     */
43 View Code Duplication
    public function setUp()
44
    {
45
        if (!self::$setUp) {
46
            parent::setUp();
47
            $this->insertDatabaseFixture(__DIR__ . '/../_fixtures/full_dump.php');
48
            self::$setUp = $this->handler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->handler of type object<eZ\Publish\Core\P...tabase\DatabaseHandler> is incompatible with the declared type boolean of property $setUp.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        } else {
50
            $this->handler = self::$setUp;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::$setUp of type boolean is incompatible with the declared type object<eZ\Publish\Core\P...tabase\DatabaseHandler> of property $handler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
        }
52
    }
53
54
    /**
55
     * Assert that the elements are.
56
     */
57
    protected function assertSearchResults($expectedIds, $locations)
58
    {
59
        $ids = $this->getIds($locations);
60
        $this->assertEquals($expectedIds, $ids);
61
    }
62
63
    protected function getIds($searchResult)
64
    {
65
        $ids = array_map(
66
            function ($hit) {
67
                return $hit->valueObject->id;
68
            },
69
            $searchResult->searchHits
70
        );
71
72
        return $ids;
73
    }
74
75
    /**
76
     * Returns the location search handler to test.
77
     *
78
     * This method returns a fully functional search handler to perform tests on.
79
     *
80
     * @return \eZ\Publish\Core\Search\Legacy\Content\Handler
81
     */
82
    protected function getContentSearchHandler()
83
    {
84
        return new Content\Handler(
85
            $this->getMock('eZ\\Publish\\Core\\Search\\Legacy\\Content\\Gateway'),
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
            new Content\Location\Gateway\DoctrineDatabase(
87
                $this->getDatabaseHandler(),
88
                new CriteriaConverter(
89
                    array(
90
                        new LocationCriterionHandler\LocationId($this->getDatabaseHandler()),
91
                        new LocationCriterionHandler\ParentLocationId($this->getDatabaseHandler()),
92
                        new CommonCriterionHandler\LogicalAnd($this->getDatabaseHandler()),
93
                        new CommonCriterionHandler\MatchAll($this->getDatabaseHandler()),
94
                        new CommonCriterionHandler\SectionId($this->getDatabaseHandler()),
95
                        new CommonCriterionHandler\ContentTypeIdentifier(
96
                            $this->getDatabaseHandler(),
97
                            $this->getContentTypeHandler()
98
                        ),
99
                    )
100
                ),
101
                new SortClauseConverter(
102
                    array(
103
                        new LocationSortClauseHandler\Location\Id($this->getDatabaseHandler()),
104
                        new LocationSortClauseHandler\Location\Depth($this->getDatabaseHandler()),
105
                        new LocationSortClauseHandler\Location\Path($this->getDatabaseHandler()),
106
                        new LocationSortClauseHandler\Location\Priority($this->getDatabaseHandler()),
107
                        new LocationSortClauseHandler\Location\Visibility($this->getDatabaseHandler()),
108
                        new LocationSortClauseHandler\Location\IsMainLocation($this->getDatabaseHandler()),
109
                        new CommonSortClauseHandler\ContentId($this->getDatabaseHandler()),
110
                        new CommonSortClauseHandler\ContentName($this->getDatabaseHandler()),
111
                        new CommonSortClauseHandler\DateModified($this->getDatabaseHandler()),
112
                        new CommonSortClauseHandler\DatePublished($this->getDatabaseHandler()),
113
                        new CommonSortClauseHandler\SectionIdentifier($this->getDatabaseHandler()),
114
                        new CommonSortClauseHandler\SectionName($this->getDatabaseHandler()),
115
                        new CommonSortClauseHandler\Field(
116
                            $this->getDatabaseHandler(),
117
                            $this->getLanguageHandler(),
118
                            $this->getContentTypeHandler()
119
                        ),
120
                    )
121
                ),
122
                $this->getLanguageHandler()
123
            ),
124
            new Content\WordIndexer\Gateway\DoctrineDatabase(
125
                $this->getDatabaseHandler(),
126
                $this->getContentTypeHandler(),
127
                $this->getDefinitionBasedTransformationProcessor(),
128
                new Content\WordIndexer\Repository\SearchIndex($this->getDatabaseHandler()),
129
                $this->getFullTextSearchConfiguration()
130
            ),
131
            $this->getMockBuilder('eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\Mapper')->disableOriginalConstructor()->getMock(),
132
            $this->getLocationMapperMock(),
133
            $this->getLanguageHandler(),
134
            $this->getFullTextMapper($this->getContentTypeHandler())
135
        );
136
    }
137
138
    protected $contentTypeHandler;
139
140 View Code Duplication
    protected function getContentTypeHandler()
141
    {
142
        if (!isset($this->contentTypeHandler)) {
143
            $this->contentTypeHandler = new ContentTypeHandler(
144
                new ContentTypeGateway(
145
                    $this->getDatabaseHandler(),
146
                    $this->getLanguageMaskGenerator()
147
                ),
148
                new ContentTypeMapper($this->getConverterRegistry()),
149
                $this->getMock('eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\Type\\Update\\Handler')
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
150
            );
151
        }
152
153
        return $this->contentTypeHandler;
154
    }
155
156
    protected $fieldRegistry;
157
158
    protected function getConverterRegistry()
159
    {
160
        if (!isset($this->fieldRegistry)) {
161
            $this->fieldRegistry = new ConverterRegistry(
162
                array(
163
                    'ezdatetime' => new Converter\DateAndTimeConverter(),
164
                    'ezinteger' => new Converter\IntegerConverter(),
165
                    'ezstring' => new Converter\TextLineConverter(),
166
                    'ezprice' => new Converter\IntegerConverter(),
167
                    'ezurl' => new Converter\UrlConverter(),
168
                    'ezrichtext' => new Converter\RichTextConverter(),
169
                    'ezboolean' => new Converter\CheckboxConverter(),
170
                    'ezkeyword' => new Converter\KeywordConverter(),
171
                    'ezauthor' => new Converter\AuthorConverter(),
172
                    'ezimage' => new Converter\NullConverter(),
173
                    'ezsrrating' => new Converter\NullConverter(),
174
                    'ezmultioption' => new Converter\NullConverter(),
175
                )
176
            );
177
        }
178
179
        return $this->fieldRegistry;
180
    }
181
182
    /**
183
     * Returns a location mapper mock.
184
     *
185
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper
186
     */
187 View Code Duplication
    protected function getLocationMapperMock()
188
    {
189
        $mapperMock = $this->getMock(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
190
            'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\Location\\Mapper',
191
            array('createLocationsFromRows')
192
        );
193
        $mapperMock
194
            ->expects($this->any())
195
            ->method('createLocationsFromRows')
196
            ->with($this->isType('array'))
197
            ->will(
198
                $this->returnCallback(
199
                    function ($rows) {
200
                        $locations = array();
201
                        foreach ($rows as $row) {
202
                            $locationId = (int)$row['node_id'];
203
                            if (!isset($locations[$locationId])) {
204
                                $locations[$locationId] = new SPILocation();
205
                                $locations[$locationId]->id = $locationId;
206
                            }
207
                        }
208
209
                        return array_values($locations);
210
                    }
211
                )
212
            );
213
214
        return $mapperMock;
215
    }
216
217
    public function testNoSorting()
218
    {
219
        $handler = $this->getContentSearchHandler();
220
221
        $locations = $handler->findLocations(
222
            new LocationQuery(
223
                array(
224
                    'filter' => new Criterion\ParentLocationId(array(178)),
225
                    'offset' => 0,
226
                    'limit' => 5,
227
                    'sortClauses' => array(),
228
                )
229
            )
230
        );
231
232
        $ids = $this->getIds($locations);
233
        sort($ids);
234
        $this->assertEquals(
235
            array(179, 180, 181, 182, 183),
236
            $ids
237
        );
238
    }
239
240
    public function testSortLocationPath()
241
    {
242
        $handler = $this->getContentSearchHandler();
243
244
        $locations = $handler->findLocations(
245
            new LocationQuery(
246
                array(
247
                    'filter' => new Criterion\ParentLocationId(array(178)),
248
                    'offset' => 0,
249
                    'limit' => 10,
250
                    'sortClauses' => array(new SortClause\Location\Path(LocationQuery::SORT_DESC)),
251
                )
252
            )
253
        );
254
255
        $this->assertSearchResults(
256
            array(186, 185, 184, 183, 182, 181, 180, 179),
257
            $locations
258
        );
259
    }
260
261 View Code Duplication
    public function testSortLocationDepth()
262
    {
263
        $handler = $this->getContentSearchHandler();
264
265
        $locations = $handler->findLocations(
266
            new LocationQuery(
267
                array(
268
                    'filter' => new Criterion\LocationId(array(148, 167, 169, 172)),
269
                    'offset' => 0,
270
                    'limit' => 10,
271
                    'sortClauses' => array(new SortClause\Location\Depth(LocationQuery::SORT_ASC)),
272
                )
273
            )
274
        );
275
276
        $this->assertSearchResults(
277
            array(167, 172, 169, 148),
278
            $locations
279
        );
280
    }
281
282
    public function testSortLocationDepthAndPath()
283
    {
284
        $handler = $this->getContentSearchHandler();
285
286
        $locations = $handler->findLocations(
287
            new LocationQuery(
288
                array(
289
                    'filter' => new Criterion\LocationId(array(141, 142, 143, 144, 146, 147)),
290
                    'offset' => 0,
291
                    'limit' => 10,
292
                    'sortClauses' => array(
293
                        new SortClause\Location\Depth(LocationQuery::SORT_ASC),
294
                        new SortClause\Location\Path(LocationQuery::SORT_DESC),
295
                    ),
296
                )
297
            )
298
        );
299
300
        $this->assertSearchResults(
301
            array(147, 146, 141, 144, 143, 142),
302
            $locations
303
        );
304
    }
305
306 View Code Duplication
    public function testSortLocationPriority()
307
    {
308
        $handler = $this->getContentSearchHandler();
309
310
        $locations = $handler->findLocations(
311
            new LocationQuery(
312
                array(
313
                    'filter' => new Criterion\LocationId(array(149, 156, 167)),
314
                    'offset' => 0,
315
                    'limit' => 10,
316
                    'sortClauses' => array(
317
                        new SortClause\Location\Priority(LocationQuery::SORT_DESC),
318
                    ),
319
                )
320
            )
321
        );
322
323
        $this->assertSearchResults(
324
            array(167, 156, 149),
325
            $locations
326
        );
327
    }
328
329
    public function testSortDateModified()
330
    {
331
        $handler = $this->getContentSearchHandler();
332
333
        $locations = $handler->findLocations(
334
            new LocationQuery(
335
                array(
336
                    'filter' => new Criterion\LocationId(array(148, 167, 169, 172)),
337
                    'offset' => 0,
338
                    'limit' => 10,
339
                    'sortClauses' => array(
340
                        new SortClause\DateModified(),
341
                    ),
342
                )
343
            )
344
        );
345
346
        $this->assertSearchResults(
347
            array(169, 172, 167, 148),
348
            $locations
349
        );
350
    }
351
352 View Code Duplication
    public function testSortDatePublished()
353
    {
354
        $handler = $this->getContentSearchHandler();
355
356
        $locations = $handler->findLocations(
357
            new LocationQuery(
358
                array(
359
                    'filter' => new Criterion\LocationId(array(148, 167, 169, 172)),
360
                    'offset' => 0,
361
                    'limit' => 10,
362
                    'sortClauses' => array(
363
                        new SortClause\DatePublished(LocationQuery::SORT_DESC),
364
                    ),
365
                )
366
            )
367
        );
368
369
        $this->assertSearchResults(
370
            array(148, 172, 169, 167),
371
            $locations
372
        );
373
    }
374
375
    public function testSortSectionIdentifier()
376
    {
377
        $handler = $this->getContentSearchHandler();
378
379
        $locations = $handler->findLocations(
380
            new LocationQuery(
381
                array(
382
                    'filter' => new Criterion\LocationId(
383
                        array(5, 43, 45, 48, 51, 54, 156, 157)
384
                    ),
385
                    'offset' => 0,
386
                    'limit' => null,
387
                    'sortClauses' => array(
388
                        new SortClause\SectionIdentifier(),
389
                    ),
390
                )
391
            )
392
        );
393
394
        // First, results of section 2 should appear, then the ones of 3, 4 and 6
395
        // From inside a specific section, no particular order should be defined
396
        // the logic is then to have a set of sorted id's to compare with
397
        // the comparison being done slice by slice.
398
        $idMapSet = array(
399
            2 => array(5, 45),
400
            3 => array(43, 51),
401
            4 => array(48, 54),
402
            6 => array(156, 157),
403
        );
404
        $locationIds = $this->getIds($locations);
405
        $index = 0;
406
407
        foreach ($idMapSet as $idSet) {
408
            $locationIdsSubset = array_slice($locationIds, $index, $count = count($idSet));
409
            $index += $count;
410
            sort($locationIdsSubset);
411
            $this->assertEquals(
412
                $idSet,
413
                $locationIdsSubset
414
            );
415
        }
416
    }
417
418
    public function testSortContentName()
419
    {
420
        $handler = $this->getContentSearchHandler();
421
422
        $locations = $handler->findLocations(
423
            new LocationQuery(
424
                array(
425
                    'filter' => new Criterion\LocationId(array(13, 15, 44, 45, 228)),
426
                    'offset' => 0,
427
                    'limit' => null,
428
                    'sortClauses' => array(
429
                        new SortClause\ContentName(),
430
                    ),
431
                )
432
            )
433
        );
434
435
        $this->assertSearchResults(
436
            array(228, 15, 13, 45, 44),
437
            $locations
438
        );
439
    }
440
441
    public function testSortContentId()
442
    {
443
        $handler = $this->getContentSearchHandler();
444
445
        $locations = $handler->findLocations(
446
            new LocationQuery(
447
                array(
448
                    'filter' => new Criterion\LocationId(array(13, 15, 44, 45, 228)),
449
                    'offset' => 0,
450
                    'limit' => null,
451
                    'sortClauses' => array(
452
                        new SortClause\ContentId(),
453
                    ),
454
                )
455
            )
456
        );
457
458
        $this->assertSearchResults(
459
            array(45, 13, 15, 44, 228),
460
            $locations
461
        );
462
    }
463
464
    public function testSortLocationId()
465
    {
466
        $handler = $this->getContentSearchHandler();
467
468
        $locations = $handler->findLocations(
469
            new LocationQuery(
470
                array(
471
                    'filter' => new Criterion\LocationId(array(13, 15, 44, 45, 228)),
472
                    'offset' => 0,
473
                    'limit' => null,
474
                    'sortClauses' => array(
475
                        new SortClause\Location\Id(LocationQuery::SORT_DESC),
476
                    ),
477
                )
478
            )
479
        );
480
481
        $this->assertSearchResults(
482
            array(228, 45, 44, 15, 13),
483
            $locations
484
        );
485
    }
486
487 View Code Duplication
    public function testSortLocationVisibilityAscending()
488
    {
489
        $handler = $this->getContentSearchHandler();
490
491
        $locations = $handler->findLocations(
492
            new LocationQuery(
493
                array(
494
                    'filter' => new Criterion\LocationId(array(45, 228)),
495
                    'offset' => 0,
496
                    'limit' => null,
497
                    'sortClauses' => array(
498
                        new SortClause\Location\Visibility(LocationQuery::SORT_ASC),
499
                    ),
500
                )
501
            )
502
        );
503
504
        $this->assertSearchResults(
505
            array(45, 228),
506
            $locations
507
        );
508
    }
509
510 View Code Duplication
    public function testSortLocationVisibilityDescending()
511
    {
512
        $handler = $this->getContentSearchHandler();
513
514
        $locations = $handler->findLocations(
515
            new LocationQuery(
516
                array(
517
                    'filter' => new Criterion\LocationId(array(45, 228)),
518
                    'offset' => 0,
519
                    'limit' => null,
520
                    'sortClauses' => array(
521
                        new SortClause\Location\Visibility(LocationQuery::SORT_DESC),
522
                    ),
523
                )
524
            )
525
        );
526
527
        $this->assertSearchResults(
528
            array(228, 45),
529
            $locations
530
        );
531
    }
532
533 View Code Duplication
    public function testSortSectionName()
534
    {
535
        $handler = $this->getContentSearchHandler();
536
537
        $result = $handler->findLocations(
538
            new LocationQuery(
539
                array(
540
                    'filter' => new Criterion\SectionId(array(4, 2, 6, 3)),
541
                    'offset' => 0,
542
                    'limit' => null,
543
                    'sortClauses' => array(
544
                        new SortClause\SectionName(),
545
                    ),
546
                )
547
            )
548
        );
549
550
        // First, results of section "Media" should appear, then the ones of "Protected",
551
        // "Setup" and "Users"
552
        // From inside a specific section, no particular order should be defined
553
        // the logic is then to have a set of sorted id's to compare with
554
        // the comparison being done slice by slice.
555
        $idMapSet = array(
556
            'media' => array(43, 51, 52, 53, 59, 60, 61, 62, 63, 64, 65, 66, 68, 202, 203),
557
            'protected' => array(156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166),
558
            'setup' => array(48, 54),
559
            'users' => array(5, 12, 13, 14, 15, 44, 45, 228),
560
        );
561
        $locationIds = array_map(
562
            function ($hit) {
563
                return $hit->valueObject->id;
564
            },
565
            $result->searchHits
566
        );
567
568
        $expectedCount = 0;
569
        foreach ($idMapSet as $set) {
570
            $expectedCount += count($set);
571
        }
572
573
        $this->assertEquals($expectedCount, $result->totalCount);
574
575
        $index = 0;
576
        foreach ($idMapSet as $idSet) {
577
            $locationIdsSubset = array_slice($locationIds, $index, $count = count($idSet));
578
            $index += $count;
579
            sort($locationIdsSubset);
580
            $this->assertEquals(
581
                $idSet,
582
                $locationIdsSubset
583
            );
584
        }
585
    }
586
587 View Code Duplication
    public function testSortFieldText()
588
    {
589
        $handler = $this->getContentSearchHandler();
590
591
        $result = $handler->findLocations(
592
            new LocationQuery(
593
                array(
594
                    'filter' => new Criterion\LogicalAnd(
595
                        array(
596
                            new Criterion\SectionId(array(1)),
597
                            new Criterion\ContentTypeIdentifier(array('article')),
598
                        )
599
                    ),
600
                    'offset' => 0,
601
                    'limit' => null,
602
                    'sortClauses' => array(
603
                        new SortClause\Field('article', 'title', LocationQuery::SORT_ASC, 'eng-US'),
604
                    ),
605
                )
606
            )
607
        );
608
609
        // There are several identical titles, need to take care about this
610
        $idMapSet = array(
611
            'aenean malesuada ligula' => array(85),
612
            'aliquam pulvinar suscipit tellus' => array(104),
613
            'asynchronous publishing' => array(150, 217),
614
            'canonical links' => array(149, 218),
615
            'class aptent taciti' => array(90),
616
            'class aptent taciti sociosqu' => array(84),
617
            'duis auctor vehicula erat' => array(91),
618
            'etiam posuere sodales arcu' => array(80),
619
            'etiam sodales mauris' => array(89),
620
            'ez publish enterprise' => array(153),
621
            'fastcgi' => array(146, 220),
622
            'fusce sagittis sagittis' => array(79),
623
            'fusce sagittis sagittis urna' => array(83),
624
            'get involved' => array(109),
625
            'how to develop with ez publish' => array(129, 213),
626
            'how to manage ez publish' => array(120, 204),
627
            'how to use ez publish' => array(110, 195),
628
            'improved block editing' => array(138),
629
            'improved front-end editing' => array(141),
630
            'improved user registration workflow' => array(134),
631
            'in hac habitasse platea' => array(81),
632
            'lots of websites, one ez publish installation' => array(132),
633
            'rest api interface' => array(152, 216),
634
            'separate content & design in ez publish' => array(193),
635
            'support for red hat enterprise' => array(147, 219),
636
            'tutorials for' => array(108),
637
        );
638
        $locationIds = array_map(
639
            function ($hit) {
640
                return $hit->valueObject->id;
641
            },
642
            $result->searchHits
643
        );
644
        $index = 0;
645
646
        foreach ($idMapSet as $idSet) {
647
            $locationIdsSubset = array_slice($locationIds, $index, $count = count($idSet));
648
            $index += $count;
649
            sort($locationIdsSubset);
650
            $this->assertEquals(
651
                $idSet,
652
                $locationIdsSubset
653
            );
654
        }
655
    }
656
657 View Code Duplication
    public function testSortFieldNumeric()
658
    {
659
        $handler = $this->getContentSearchHandler();
660
661
        $result = $handler->findLocations(
662
            new LocationQuery(
663
                array(
664
                    'filter' => new Criterion\LogicalAnd(
665
                        array(
666
                            new Criterion\SectionId(array(1)),
667
                            new Criterion\ContentTypeIdentifier('product'),
668
                        )
669
                    ),
670
                    'offset' => 0,
671
                    'limit' => null,
672
                    'sortClauses' => array(
673
                        new SortClause\Field('product', 'price', LocationQuery::SORT_ASC, 'eng-US'),
674
                    ),
675
                )
676
            )
677
        );
678
679
        $this->assertEquals(
680
            array(75, 73, 74, 71),
681
            array_map(
682
                function ($hit) {
683
                    return $hit->valueObject->id;
684
                },
685
                $result->searchHits
686
            )
687
        );
688
    }
689
690 View Code Duplication
    public function testSortIsMainLocationAscending()
691
    {
692
        $handler = $this->getContentSearchHandler();
693
694
        $locations = $handler->findLocations(
695
            new LocationQuery(
696
                array(
697
                    'filter' => new Criterion\ParentLocationId(224),
698
                    'offset' => 0,
699
                    'limit' => null,
700
                    'sortClauses' => array(
701
                        new SortClause\Location\IsMainLocation(LocationQuery::SORT_ASC),
702
                    ),
703
                )
704
            )
705
        );
706
707
        $this->assertSearchResults(
708
            array(510, 225),
709
            $locations
710
        );
711
    }
712
713 View Code Duplication
    public function testSortIsMainLocationDescending()
714
    {
715
        $handler = $this->getContentSearchHandler();
716
717
        $locations = $handler->findLocations(
718
            new LocationQuery(
719
                array(
720
                    'filter' => new Criterion\ParentLocationId(224),
721
                    'offset' => 0,
722
                    'limit' => null,
723
                    'sortClauses' => array(
724
                        new SortClause\Location\IsMainLocation(LocationQuery::SORT_DESC),
725
                    ),
726
                )
727
            )
728
        );
729
730
        $this->assertSearchResults(
731
            array(225, 510),
732
            $locations
733
        );
734
    }
735
}
736