Completed
Push — EZP-30925 ( b7e3d1...4064f0 )
by
unknown
18:46
created

provideUserWithContentRemovePolicies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
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
declare(strict_types=1);
8
9
namespace eZ\Publish\API\Repository\Tests\SearchService;
10
11
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
12
use eZ\Publish\API\Repository\Tests\BaseTest;
13
use eZ\Publish\API\Repository\Tests\SetupFactory\LegacyElasticsearch;
14
use eZ\Publish\API\Repository\Values\Content\Content;
15
use eZ\Publish\API\Repository\Values\Content\Query;
16
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
17
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
18
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
19
use eZ\Publish\API\Repository\Values\User\User;
20
21
/**
22
 * Test case for delete content translation with the SearchService.
23
 *
24
 * @see \eZ\Publish\API\Repository\SearchService
25
 * @group integration
26
 * @group search
27
 */
28
class DeleteTranslationTest extends BaseTest
29
{
30
    /**
31
     * @throws \ErrorException
32
     */
33
    public function setUp(): void
34
    {
35
        $setupFactory = $this->getSetupFactory();
36
37
        if ($setupFactory instanceof LegacyElasticsearch) {
38
            $this->markTestIncomplete('Not implemented for Elasticsearch Search Engine');
39
        }
40
41
        parent::setUp();
42
    }
43
44
    /**
45
     * @param array $languages
46
     *
47
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
48
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
49
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
50
     */
51
    protected function createTestContentWithLanguages(array $languages): Content
52
    {
53
        $repository = $this->getRepository();
54
        $contentTypeService = $repository->getContentTypeService();
55
        $contentService = $repository->getContentService();
56
        $locationService = $repository->getLocationService();
57
58
        $contentTypeArticle = $contentTypeService->loadContentTypeByIdentifier('article');
59
        $contentCreateStructArticle = $contentService->newContentCreateStruct(
60
            $contentTypeArticle,
61
            'eng-GB'
62
        );
63
64
        foreach ($languages as $langCode => $title) {
65
            $contentCreateStructArticle->setField('title', $title, $langCode);
66
            $contentCreateStructArticle->setField(
67
                'intro',
68
                '<?xml version="1.0" encoding="UTF-8"?>
69
<section xmlns="http://docbook.org/ns/docbook" version="5.0-variant ezpublish-1.0">
70
  <para>' . $title . '</para>
71
</section>',
72
                $langCode
73
            );
74
        }
75
76
        $locationCreateStructArticle = $locationService->newLocationCreateStruct(2);
77
        $draftArticle = $contentService->createContent(
78
            $contentCreateStructArticle,
79
            [$locationCreateStructArticle]
80
        );
81
        $content = $contentService->publishVersion($draftArticle->getVersionInfo());
82
        $this->refreshSearch($repository);
83
84
        return $content;
85
    }
86
87
    /**
88
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
89
     */
90
    protected function findContent(string $text, string $languageCode): SearchResult
91
    {
92
        $repository = $this->getRepository();
93
        $searchService = $repository->getSearchService();
94
        $query = new Query();
95
        $query->query = new Criterion\FullText($text);
96
        $query->limit = 0;
97
        $languageFilter = [
98
            'languages' => [$languageCode],
99
            'useAlwaysAvailable' => true,
100
            'excludeTranslationsFromAlwaysAvailable' => false,
101
        ];
102
103
        return $searchService->findContent($query, $languageFilter);
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
    public function testDeleteContentTranslation(): void
112
    {
113
        $repository = $this->getRepository();
114
        $testContent = $this->createTestContentWithLanguages(
115
            [
116
                'eng-GB' => 'Contact',
117
                'ger-DE' => 'Kontakt',
118
            ]
119
        );
120
        $contentService = $repository->getContentService();
121
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
122
        $this->assertEquals(1, $searchResult->totalCount);
123
124
        $contentService->deleteTranslation($testContent->contentInfo, 'ger-DE');
125
        $this->refreshSearch($repository);
126
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
127
        $this->assertEquals(0, $searchResult->totalCount);
128
    }
129
130
    /**
131
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
132
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
133
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
134
     */
135
    public function testDeleteContentTranslationWithContentRemovePolicy(): void
136
    {
137
        $repository = $this->getRepository();
138
        $testContent = $this->createTestContentWithLanguages(
139
            [
140
                'eng-GB' => 'Contact',
141
                'ger-DE' => 'Kontakt',
142
                'eng-US' => 'Contact',
143
            ]
144
        );
145
146
        $repository->getPermissionResolver()->setCurrentUserReference($this->provideUserWithContentRemovePolicies());
147
148
        $contentService = $repository->getContentService();
149
        $searchResult = $this->findContent('Contact', 'eng-US');
150
        $this->assertEquals(1, $searchResult->totalCount);
151
152
        $contentService->deleteTranslation($testContent->contentInfo, 'eng-US');
153
        $this->refreshSearch($repository);
154
        $searchResult = $this->findContent('Contact', 'eng-US');
155
        $this->assertEquals(0, $searchResult->totalCount);
156
    }
157
158
    /**
159
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
160
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
161
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
162
     */
163
    public function testPreventTranslationDeletionIfNoAccess(): void
164
    {
165
        $repository = $this->getRepository();
166
        $contentService = $repository->getContentService();
167
        $testContent = $this->createTestContentWithLanguages(
168
            [
169
                'eng-GB' => 'Contact',
170
                'ger-DE' => 'Kontakt',
171
                'eng-US' => 'Contact',
172
            ]
173
        );
174
175
        $repository->getPermissionResolver()->setCurrentUserReference($this->provideUserWithContentRemovePolicies());
176
177
        $this->expectException(UnauthorizedException::class);
178
179
        $contentService->deleteTranslation($testContent->contentInfo, 'ger-DE');
180
    }
181
182
    public function provideUserWithContentRemovePolicies(): User
183
    {
184
        $limitations = [
185
            new LanguageLimitation(['limitationValues' => ['eng-US']]),
186
        ];
187
188
        return $this->createUserWithPolicies(
189
                'test',
190
                [
191
                    ['module' => 'content', 'function' => 'remove', 'limitations' => $limitations],
192
                    ['module' => 'content', 'function' => 'versionread'],
193
                    ['module' => 'content', 'function' => 'read'],
194
                ]
195
            );
196
    }
197
}
198