Completed
Push — ezp_31059 ( 42fc46 )
by
unknown
18:17
created

assertUsagesSearchResultItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
namespace eZ\Publish\API\Repository\Tests;
8
9
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
10
use eZ\Publish\API\Repository\Values\Content\Content;
11
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
12
use eZ\Publish\API\Repository\Values\URL\SearchResult;
13
use eZ\Publish\API\Repository\Values\URL\URLQuery;
14
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;
15
16
/**
17
 * Base class for URLService tests.
18
 */
19
abstract class BaseURLServiceTest extends BaseTest
20
{
21
    private const URL_CONTENT_TYPE_IDENTIFIER = 'linkjk';
22
23
    protected function doTestFindUrls(URLQuery $query, array $expectedUrls, $expectedTotalCount = null, $ignoreOrder = true)
24
    {
25
        $repository = $this->getRepository();
26
27
        /* BEGIN: Use Case */
28
        $searchResult = $repository->getURLService()->findUrls($query);
29
        /* END: Use Case */
30
31
        if ($expectedTotalCount === null) {
32
            $expectedTotalCount = count($expectedUrls);
33
        }
34
35
        $this->assertInstanceOf(SearchResult::class, $searchResult);
36
        $this->assertEquals($expectedTotalCount, $searchResult->totalCount);
37
        $this->assertSearchResultItems($searchResult, $expectedUrls, $ignoreOrder);
38
    }
39
40
    protected function assertSearchResultItems(SearchResult $searchResult, array $expectedUrls, $ignoreOrder)
41
    {
42
        $this->assertCount(count($expectedUrls), $searchResult->items);
43
44
        foreach ($searchResult->items as $i => $item) {
45
            if ($ignoreOrder) {
46
                $this->assertContains($item->url, $expectedUrls);
47
            } else {
48
                $this->assertEquals($expectedUrls[$i], $item->url);
49
            }
50
        }
51
    }
52
53
    protected function assertSearchResultItemsAreUnique(SearchResult $results): void
54
    {
55
        $visitedUrls = [];
56
57
        foreach ($results->items as $item) {
58
            $this->assertNotContains(
59
                $item->url,
60
                $visitedUrls,
61
                'Search results contains duplicated url: ' . $item->url
62
            );
63
64
            $visitedUrls[] = $item->url;
65
        }
66
    }
67
68
    protected function assertUsagesSearchResultItems(UsageSearchResult $searchResult, array $expectedContentInfoIds)
69
    {
70
        $this->assertCount(count($expectedContentInfoIds), $searchResult->items);
71
        foreach ($searchResult->items as $contentInfo) {
72
            $this->assertContains($contentInfo->id, $expectedContentInfoIds);
73
        }
74
    }
75
76
    protected function createContentWithLink(string $name, string $url, string $languageCode = 'eng-GB', int $parentLocationId = 2): Content
77
    {
78
        $repository = $this->getRepository(false);
79
        $contentService = $repository->getContentService();
80
        $contentTypeService = $repository->getContentTypeService();
81
        $locationService = $repository->getLocationService();
82
83
        try {
84
            $contentType = $contentTypeService->loadContentTypeByIdentifier(self::URL_CONTENT_TYPE_IDENTIFIER);
85
        } catch (NotFoundException $e) {
86
            $contentType = $this->createContentTypeWithUrl();
87
        }
88
89
        $struct = $contentService->newContentCreateStruct($contentType, $languageCode);
90
        $struct->setField('name', $name, $languageCode);
91
        $struct->setField('url', $url, $languageCode);
92
93
        $contentDraft = $contentService->createContent(
94
            $struct,
95
            [$locationService->newLocationCreateStruct($parentLocationId)]
96
        );
97
98
        return $contentService->publishVersion($contentDraft->versionInfo);
99
    }
100
101
    private function createContentTypeWithUrl(): ContentType
102
    {
103
        $repository = $this->getRepository();
104
105
        $contentTypeService = $repository->getContentTypeService();
106
107
        $typeCreate = $contentTypeService->newContentTypeCreateStruct(self::URL_CONTENT_TYPE_IDENTIFIER);
108
        $typeCreate->mainLanguageCode = 'eng-GB';
109
        $typeCreate->urlAliasSchema = 'url|scheme';
110
        $typeCreate->nameSchema = 'name|scheme';
111
        $typeCreate->names = [
112
            'eng-GB' => 'URL: ' . self::URL_CONTENT_TYPE_IDENTIFIER,
113
        ];
114
        $typeCreate->descriptions = [
115
            'eng-GB' => '',
116
        ];
117
        $typeCreate->creatorId = $this->generateId('user', $repository->getPermissionResolver()->getCurrentUserReference()->getUserId());
118
        $typeCreate->creationDate = $this->createDateTime();
119
120
        $nameFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('name', 'ezstring');
121
        $nameFieldCreate->names = [
122
            'eng-GB' => 'Name',
123
        ];
124
        $nameFieldCreate->descriptions = [
125
            'eng-GB' => '',
126
        ];
127
        $nameFieldCreate->fieldGroup = 'default';
128
        $nameFieldCreate->position = 1;
129
        $nameFieldCreate->isTranslatable = false;
130
        $nameFieldCreate->isRequired = true;
131
        $nameFieldCreate->isInfoCollector = false;
132
        $nameFieldCreate->validatorConfiguration = [
133
            'StringLengthValidator' => [
134
                'minStringLength' => 0,
135
                'maxStringLength' => 0,
136
            ],
137
        ];
138
        $nameFieldCreate->fieldSettings = [];
139
        $nameFieldCreate->isSearchable = true;
140
        $nameFieldCreate->defaultValue = '';
141
142
        $typeCreate->addFieldDefinition($nameFieldCreate);
143
144
        $urlFieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('url', 'ezurl');
145
        $urlFieldCreate->names = [
146
            'eng-GB' => 'URL',
147
        ];
148
        $urlFieldCreate->descriptions = [
149
            'eng-GB' => '',
150
        ];
151
        $urlFieldCreate->fieldGroup = 'default';
152
        $urlFieldCreate->position = 2;
153
        $urlFieldCreate->isTranslatable = false;
154
        $urlFieldCreate->isRequired = true;
155
        $urlFieldCreate->isInfoCollector = false;
156
        $nameFieldCreate->validatorConfiguration = [];
157
        $urlFieldCreate->fieldSettings = [];
158
        $urlFieldCreate->isSearchable = false;
159
        $urlFieldCreate->defaultValue = '';
160
161
        $typeCreate->addFieldDefinition($urlFieldCreate);
162
163
        $contentTypeDraft = $contentTypeService->createContentType($typeCreate, [
164
            $contentTypeService->loadContentTypeGroupByIdentifier('Content'),
165
        ]);
166
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
167
168
        return $contentTypeService->loadContentTypeByIdentifier(self::URL_CONTENT_TYPE_IDENTIFIER);
169
    }
170
}
171