Completed
Push — EZP-31460 ( fb49b1...efa331 )
by
unknown
19:23 queued 11s
created

RemoteIdIndexingTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 3
A testIndexingRemoteId() 0 33 2
A getRemoteIDs() 0 12 1
A providerForTestIndexingRemoteId() 0 7 2
A createTestFolder() 0 21 1
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\API\Repository\Tests\SearchService;
10
11
use eZ\Publish\API\Repository\Tests\BaseTest;
12
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
13
use eZ\Publish\API\Repository\Values\Content\Query;
14
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
15
16
final class RemoteIdIndexingTest extends BaseTest
17
{
18
    /** @var int[] */
19
    private static $contentIdByRemoteIdIndex = [];
20
21
    /**
22
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
23
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
24
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
25
     */
26
    protected function setUp(): void
27
    {
28
        parent::setUp();
29
30
        if (empty(self::$contentIdByRemoteIdIndex)) {
31
            foreach ($this->getRemoteIDs() as $remoteId) {
32
                self::$contentIdByRemoteIdIndex[$remoteId] = $this->createTestFolder(
33
                    $remoteId
34
                );
35
            }
36
37
            $this->refreshSearch($this->getRepository(false));
38
        }
39
    }
40
41
    /**
42
     * @dataProvider providerForTestIndexingRemoteId
43
     *
44
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
45
     */
46
    public function testIndexingRemoteId(Criterion $criterion): void
47
    {
48
        $repository = $this->getRepository(false);
49
        $searchService = $repository->getSearchService();
50
51
        $remoteId = $criterion->value[0];
52
        if (!isset(self::$contentIdByRemoteIdIndex[$remoteId])) {
53
            self::fail("Failed to find Content ID for remote ID = {$criterion->value}");
54
        }
55
56
        $contentId = self::$contentIdByRemoteIdIndex[$remoteId];
57
58
        // test searching using both Content & Location remote IDs for both Content items
59
        $query = new Query();
60
        $query->filter = $criterion;
61
        $result = $searchService->findContent($query);
62
        self::assertSame(
63
            1,
64
            $result->totalCount,
65
            "Failed to find Content ID = {$contentId} filtering by remote ID = '{$remoteId}'"
66
        );
67
        self::assertSame($contentId, $result->searchHits[0]->valueObject->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
69
        $query = new LocationQuery();
70
        $query->filter = $criterion;
71
        $result = $searchService->findLocations($query);
72
        self::assertSame(
73
            1,
74
            $result->totalCount,
75
            "Failed to find Location for Content ID = {$contentId} filtering by remote ID = '{$remoteId}'"
76
        );
77
        self::assertSame($contentId, $result->searchHits[0]->valueObject->contentInfo->id);
0 ignored issues
show
Documentation introduced by
The property contentInfo does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
    }
79
80
    private function getRemoteIDs(): array
81
    {
82
        return [
83
            'md5sum' => 'dec23f2de27399a4c0561187b805aa74',
84
            'sha512' => '3ad11d1424370b5a258f7dc7724b25535f3fb2e1fa3f7047839f68d18cf58eb5',
85
            'external:10' => 'external:10',
86
            'external_10' => 'external_10',
87
            'with space' => 'with space',
88
            'with national characters' => 'zażółć gęślą jaźń',
89
            'with emoji' => json_decode('"\ud83d\ude00"'),
90
        ];
91
    }
92
93
    /**
94
     * Create 2 * number of remote IDs test data sets (one for Content, another for Location).
95
     *
96
     * @return iterable
97
     */
98
    public function providerForTestIndexingRemoteId(): iterable
99
    {
100
        foreach ($this->getRemoteIDs() as $description => $remoteId) {
101
            yield "Content remote ID = {$description}" => [new Criterion\RemoteId($remoteId)];
102
            yield "Location remote ID = {$description}" => [new Criterion\LocationRemoteId($remoteId)];
103
        }
104
    }
105
106
    /**
107
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
108
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
109
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
110
     */
111
    private function createTestFolder(string $remoteId): int
112
    {
113
        $repository = $this->getRepository(false);
114
        $contentTypeService = $repository->getContentTypeService();
115
        $contentService = $repository->getContentService();
116
        $locationService = $repository->getLocationService();
117
118
        $folderType = $contentTypeService->loadContentTypeByIdentifier('folder');
119
        $folderCreateStruct = $contentService->newContentCreateStruct($folderType, 'eng-GB');
120
        $folderCreateStruct->remoteId = $remoteId;
121
        $locationCreateStruct = $locationService->newLocationCreateStruct(2);
122
        $locationCreateStruct->remoteId = $remoteId;
123
        $folder = $contentService->publishVersion(
124
            $contentService->createContent(
125
                $folderCreateStruct,
126
                [$locationCreateStruct]
127
            )->getVersionInfo()
128
        );
129
130
        return $folder->id;
131
    }
132
}
133