Completed
Push — EZP-30925 ( b7e3d1 )
by
unknown
19:31
created

testPreventTranslationDeletionIfNoAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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