Completed
Push — 7.5 ( 8c6b05...79049c )
by Łukasz
19:46
created

testPreventTranslationDeletionIfNoAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
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\Query;
15
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
16
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
17
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
18
use eZ\Publish\API\Repository\Values\User\User;
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
final 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
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
45
     */
46
    protected function findContent(string $text, string $languageCode): SearchResult
47
    {
48
        $repository = $this->getRepository();
49
        $searchService = $repository->getSearchService();
50
        $query = new Query();
51
        $query->query = new Criterion\FullText($text);
52
        $query->limit = 0;
53
        $languageFilter = [
54
            'languages' => [$languageCode],
55
            'useAlwaysAvailable' => true,
56
            'excludeTranslationsFromAlwaysAvailable' => false,
57
        ];
58
59
        return $searchService->findContent($query, $languageFilter);
60
    }
61
62
    /**
63
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
64
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
65
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
66
     */
67
    public function testDeleteContentTranslation(): void
68
    {
69
        $repository = $this->getRepository();
70
        $contentService = $repository->getContentService();
71
72
        $testContent = $this->createFolder(['eng-GB' => 'Contact', 'ger-DE' => 'Kontakt'], 2);
73
        $this->createFolder(['eng-GB' => 'OtherEngContent', 'ger-DE' => 'OtherGerContent'], 2);
74
        $this->refreshSearch($repository);
75
76
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
77
        $this->assertEquals(1, $searchResult->totalCount);
78
79
        $contentService->deleteTranslation($testContent->contentInfo, 'ger-DE');
80
        $this->refreshSearch($repository);
81
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
82
        $this->assertEquals(
83
            0,
84
            $searchResult->totalCount,
85
            'Found reference to the deleted Content translation'
86
        );
87
88
        // check if unrelated items were not affected
89
        $searchResult = $this->findContent('OtherGerContent', 'ger-DE');
90
        $this->assertEquals(1, $searchResult->totalCount, 'Unrelated translation was deleted');
91
    }
92
93
    /**
94
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
95
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
96
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
97
     */
98
    public function testDeleteContentTranslationWithContentRemovePolicy(): void
99
    {
100
        $repository = $this->getRepository();
101
        $contentService = $repository->getContentService();
102
103
        $testContent = $this->createFolder(['eng-GB' => 'Contact', 'ger-DE' => 'Kontakt'], 2);
104
        $this->refreshSearch($repository);
105
106
        $user = $this->provideUserWithContentRemovePolicies();
107
        $repository->getPermissionResolver()->setCurrentUserReference($user);
108
109
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
110
111
        $this->assertEquals(1, $searchResult->totalCount);
112
        $contentService->deleteTranslation($testContent->contentInfo, 'ger-DE');
113
114
        $this->refreshSearch($repository);
115
        $searchResult = $this->findContent('Kontakt', 'ger-DE');
116
        $this->assertEquals(0, $searchResult->totalCount);
117
    }
118
119
    /**
120
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
121
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
122
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
123
     */
124
    public function testPreventTranslationDeletionIfNoAccess(): void
125
    {
126
        $repository = $this->getRepository();
127
        $contentService = $repository->getContentService();
128
129
        $testContent = $this->createFolder(
130
            [
131
                'eng-GB' => 'Contact',
132
                'ger-DE' => 'Kontakt',
133
                'eng-US' => 'Contact',
134
            ],
135
            2);
136
137
        $user = $this->provideUserWithContentRemovePolicies();
138
        $repository->getPermissionResolver()->setCurrentUserReference($user);
139
140
        $this->expectException(UnauthorizedException::class);
141
142
        $contentService->deleteTranslation($testContent->contentInfo, 'eng-US');
143
    }
144
145
    public function provideUserWithContentRemovePolicies(): User
146
    {
147
        $limitations = [
148
            new LanguageLimitation(['limitationValues' => ['ger-DE']]),
149
        ];
150
151
        return $this->createUserWithPolicies(
152
            'test',
153
            [
154
                ['module' => 'content', 'function' => 'remove', 'limitations' => $limitations],
155
                ['module' => 'content', 'function' => 'versionread'],
156
                ['module' => 'content', 'function' => 'read'],
157
            ]
158
        );
159
    }
160
}
161