Completed
Push — EZP-31046-chain-config-resolve... ( de9afc...926161 )
by
unknown
13:31
created

BaseURLServiceTest::createContentWithLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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