Completed
Push — EZP-30997 ( af07b3...5db064 )
by
unknown
19:07
created

providerForPublishVersionWithLanguageLimitation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 30
rs 9.44
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\Values\User\Limitation;
10
11
use eZ\Publish\API\Repository\ContentService;
12
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
13
use eZ\Publish\API\Repository\Tests\BaseTest;
14
use eZ\Publish\API\Repository\Values\Content\Content;
15
use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation;
16
use eZ\Publish\API\Repository\Values\User\User;
17
18
/**
19
 * Test cases for ContentService APIs calls made by user with LanguageLimitation on chosen policies.
20
 *
21
 * @uses \eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation
22
 *
23
 * @group integration
24
 * @group authorization
25
 * @group language-limited-content-mgm
26
 */
27
class LanguageLimitationTest extends BaseTest
28
{
29
    /** @var string */
30
    private const ENG_US = 'eng-US';
31
32
    /** @var string */
33
    private const ENG_GB = 'eng-GB';
34
35
    /** @var string */
36
    private const GER_DE = 'ger-DE';
37
38
    /**
39
     * Create editor who is allowed to modify only specific translations of a Content item.
40
     *
41
     * @param array $allowedTranslationsList list of translations (language codes) which editor can modify.
42
     * @param string $login
43
     *
44
     * @return \eZ\Publish\API\Repository\Values\User\User
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
    private function createEditorUserWithLanguageLimitation(
51
        array $allowedTranslationsList,
52
        string $login = 'editor'
53
    ): User {
54
        $limitations = [
55
            // limitation for specific translations
56
            new LanguageLimitation(['limitationValues' => $allowedTranslationsList]),
57
        ];
58
59
        return $this->createUserWithPolicies(
60
            $login,
61
            [
62
                ['module' => 'content', 'function' => 'read'],
63
                ['module' => 'content', 'function' => 'versionread'],
64
                ['module' => 'content', 'function' => 'view_embed'],
65
                ['module' => 'content', 'function' => 'create', 'limitations' => $limitations],
66
                ['module' => 'content', 'function' => 'edit', 'limitations' => $limitations],
67
                ['module' => 'content', 'function' => 'publish', 'limitations' => $limitations],
68
            ]
69
        );
70
    }
71
72
    /**
73
     * @return array
74
     * @see testCreateAndPublishContent
75
     */
76
    public function providerForCreateAndPublishContent(): array
77
    {
78
        // $names (as admin), $allowedTranslationsList (editor limitations)
79
        return [
80
            [
81
                ['ger-DE' => 'German Folder'],
82
                ['ger-DE'],
83
            ],
84
            [
85
                ['ger-DE' => 'German Folder', 'eng-GB' => 'British Folder'],
86
                ['ger-DE', 'eng-GB'],
87
            ],
88
        ];
89
    }
90
91
    /**
92
     * Test creating and publishing a fresh Content item in a language restricted by LanguageLimitation.
93
     *
94
     * @param array $names
95
     * @param array $allowedTranslationsList
96
     *
97
     * @dataProvider providerForCreateAndPublishContent
98
     *
99
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
100
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
101
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
102
     */
103
    public function testCreateAndPublishContent(array $names, array $allowedTranslationsList): void
104
    {
105
        $repository = $this->getRepository();
106
        $repository->getPermissionResolver()->setCurrentUserReference(
107
            $this->createEditorUserWithLanguageLimitation($allowedTranslationsList)
108
        );
109
110
        $folder = $this->createFolder($names, 2);
111
112
        foreach ($names as $languageCode => $translatedName) {
113
            self::assertEquals(
114
                $translatedName,
115
                $folder->getField('name', $languageCode)->value->text
116
            );
117
        }
118
    }
119
120
    /**
121
     * Data provider for testPublishVersionWithLanguageLimitation.
122
     *
123
     * @return array
124
     * @see testPublishVersionIsNotAllowedIfModifiedOtherTranslations
125
     *
126
     * @see testPublishVersion
127
     */
128
    public function providerForPublishVersionWithLanguageLimitation(): array
129
    {
130
        // $names (as admin), $namesToUpdate (as editor), $allowedTranslationsList (editor limitations)
131
        return [
132
            [
133
                ['eng-US' => 'American Folder'],
134
                ['ger-DE' => 'Updated German Folder'],
135
                ['ger-DE'],
136
            ],
137
            [
138
                ['eng-US' => 'American Folder', 'ger-DE' => 'German Folder'],
139
                ['ger-DE' => 'Updated German Folder'],
140
                ['ger-DE'],
141
            ],
142
            [
143
                [
144
                    'eng-US' => 'American Folder',
145
                    'eng-GB' => 'British Folder',
146
                    'ger-DE' => 'German Folder',
147
                ],
148
                ['ger-DE' => 'Updated German Folder', 'eng-GB' => 'British Folder'],
149
                ['ger-DE', 'eng-GB'],
150
            ],
151
            [
152
                ['eng-US' => 'American Folder', 'ger-DE' => 'German Folder'],
153
                ['ger-DE' => 'Updated German Folder', 'eng-GB' => 'British Folder'],
154
                ['ger-DE', 'eng-GB'],
155
            ],
156
        ];
157
    }
158
159
    /**
160
     * Test publishing Version with translations restricted by LanguageLimitation.
161
     *
162
     * @param array $names
163
     * @param array $namesToUpdate
164
     * @param array $allowedTranslationsList
165
     *
166
     * @dataProvider providerForPublishVersionWithLanguageLimitation
167
     *
168
     * @covers \eZ\Publish\API\Repository\ContentService::createContentDraft
169
     * @covers \eZ\Publish\API\Repository\ContentService::updateContent
170
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
171
     *
172
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
173
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
174
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
175
     * @throws \Exception
176
     */
177
    public function testPublishVersion(
178
        array $names,
179
        array $namesToUpdate,
180
        array $allowedTranslationsList
181
    ): void {
182
        $repository = $this->getRepository();
183
        $contentService = $repository->getContentService();
184
185
        $folder = $this->createFolder($names, 2);
186
187
        $repository->getPermissionResolver()->setCurrentUserReference(
188
            $this->createEditorUserWithLanguageLimitation($allowedTranslationsList)
189
        );
190
191
        $folderDraft = $contentService->createContentDraft($folder->contentInfo);
192
        $folderUpdateStruct = $contentService->newContentUpdateStruct();
193
        // set modified translation of Version to the first modified as multiple are not supported yet
194
        $folderUpdateStruct->initialLanguageCode = array_keys($namesToUpdate)[0];
0 ignored issues
show
Documentation Bug introduced by
It seems like array_keys($namesToUpdate)[0] can also be of type integer. However, the property $initialLanguageCode is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
195
        foreach ($namesToUpdate as $languageCode => $translatedName) {
196
            $folderUpdateStruct->setField('name', $translatedName, $languageCode);
197
        }
198
        $folderDraft = $contentService->updateContent(
199
            $folderDraft->getVersionInfo(),
200
            $folderUpdateStruct
201
        );
202
        $contentService->publishVersion($folderDraft->getVersionInfo());
203
204
        $folder = $contentService->loadContent($folder->id);
205
        $updatedNames = array_merge($names, $namesToUpdate);
206
        foreach ($updatedNames as $languageCode => $expectedValue) {
207
            self::assertEquals(
208
                $expectedValue,
209
                $folder->getField('name', $languageCode)->value->text,
210
                "Unexpected Field value for {$languageCode}"
211
            );
212
        }
213
    }
214
215
    /**
216
     * Test that publishing version with changes to translations outside limitation values throws unauthorized exception.
217
     *
218
     * @param array $names
219
     *
220
     * @dataProvider providerForPublishVersionWithLanguageLimitation
221
     *
222
     * @covers \eZ\Publish\API\Repository\ContentService::createContentDraft
223
     * @covers \eZ\Publish\API\Repository\ContentService::updateContent
224
     * @covers \eZ\Publish\API\Repository\ContentService::publishVersion
225
     *
226
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
227
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
228
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
229
     */
230
    public function testPublishVersionIsNotAllowedIfModifiedOtherTranslations(array $names): void
231
    {
232
        $repository = $this->getRepository();
233
        $contentService = $repository->getContentService();
234
235
        $folder = $this->createFolder($names, 2);
236
        $folderDraft = $contentService->createContentDraft($folder->contentInfo);
237
        $folderUpdateStruct = $contentService->newContentUpdateStruct();
238
        $folderUpdateStruct->setField('name', 'Updated American Folder', 'eng-US');
239
        $folderDraft = $contentService->updateContent(
240
            $folderDraft->getVersionInfo(),
241
            $folderUpdateStruct
242
        );
243
244
        // switch context to the user not allowed to publish eng-US
245
        $repository->getPermissionResolver()->setCurrentUserReference(
246
            $this->createEditorUserWithLanguageLimitation(['ger-DE'])
247
        );
248
249
        $this->expectException(UnauthorizedException::class);
250
        $contentService->publishVersion($folderDraft->getVersionInfo());
251
    }
252
253
    public function testPublishVersionTranslation(): void
254
    {
255
        $repository = $this->getRepository();
256
        $contentService = $repository->getContentService();
257
        $permissionResolver = $repository->getPermissionResolver();
258
259
        $draft = $this->createMultilingualFolderDraft($contentService);
260
261
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
262
263
        $contentUpdateStruct->setField('name', 'Draft 1 DE', self::GER_DE);
264
265
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
266
267
        $admin = $permissionResolver->getCurrentUserReference();
268
        $permissionResolver->setCurrentUserReference($this->createEditorUserWithLanguageLimitation(['ger-DE']));
269
270
        $contentService->publishVersion($draft->versionInfo, [self::GER_DE]);
271
272
        $permissionResolver->setCurrentUserReference($admin);
273
        $content = $contentService->loadContent($draft->contentInfo->id);
274
        $this->assertEquals(
275
            [
276
                self::ENG_US => 'Published US',
277
                self::GER_DE => 'Draft 1 DE',
278
            ],
279
            $content->fields['name']
280
        );
281
    }
282
283
    public function testPublishVersionTranslationIsNotAllowed(): void
284
    {
285
        $repository = $this->getRepository();
286
        $contentService = $repository->getContentService();
287
        $permissionResolver = $repository->getPermissionResolver();
288
289
        $draft = $this->createMultilingualFolderDraft($contentService);
290
291
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
292
293
        $contentUpdateStruct->setField('name', 'Draft 1 EN', self::ENG_US);
294
295
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
296
297
        $permissionResolver->setCurrentUserReference($this->createEditorUserWithLanguageLimitation(['ger-DE']));
298
299
        $this->expectException(UnauthorizedException::class);
300
        $contentService->publishVersion($draft->versionInfo, [self::ENG_US]);
301
    }
302
303
    public function testPublishVersionTranslationIsNotAllowedWithTwoEditors(): void
304
    {
305
        $repository = $this->getRepository();
306
        $contentService = $repository->getContentService();
307
        $permissionResolver = $repository->getPermissionResolver();
308
309
        $editorDE = $this->createEditorUserWithLanguageLimitation(['ger-DE'], 'editor-de');
310
        $editorUS = $this->createEditorUserWithLanguageLimitation(['eng-US'], 'editor-us');
311
312
        // German editor publishes content in German language
313
        $permissionResolver->setCurrentUserReference($editorDE);
314
315
        $folder = $this->createFolder(['ger-DE' => 'German Folder'], 2);
316
317
        // American editor creates and saves English draft
318
        $permissionResolver->setCurrentUserReference($editorUS);
319
320
        $folder = $contentService->loadContent($folder->id);
321
        $folderDraft = $contentService->createContentDraft($folder->contentInfo);
322
        $folderUpdateStruct = $contentService->newContentUpdateStruct();
323
        $folderUpdateStruct->setField('name', 'English Folder', 'eng-US');
324
        $folderDraft = $contentService->updateContent(
325
            $folderDraft->versionInfo,
326
            $folderUpdateStruct
327
        );
328
329
        // German editor tries to publish English translation
330
        $permissionResolver->setCurrentUserReference($editorDE);
331
        $folderDraftVersionInfo = $contentService->loadVersionInfo(
332
            $folderDraft->contentInfo,
333
            $folderDraft->versionInfo->versionNo
334
        );
335
        self::assertTrue($folderDraftVersionInfo->isDraft());
336
        $this->expectException(UnauthorizedException::class);
337
        $this->expectExceptionMessage("User does not have access to 'publish' 'content'");
338
        $contentService->publishVersion($folderDraftVersionInfo, [self::ENG_US]);
339
    }
340
341
    public function testPublishVersionTranslationWhenUserHasNoAccessToAllLanguages(): void
342
    {
343
        $repository = $this->getRepository();
344
        $contentService = $repository->getContentService();
345
        $permissionResolver = $repository->getPermissionResolver();
346
347
        $draft = $this->createMultilingualFolderDraft($contentService);
348
349
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
350
351
        $contentUpdateStruct->setField('name', 'Draft 1 DE', self::GER_DE);
352
        $contentUpdateStruct->setField('name', 'Draft 1 GB', self::ENG_GB);
353
354
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
355
356
        $permissionResolver->setCurrentUserReference(
357
            $this->createEditorUserWithLanguageLimitation([self::GER_DE])
358
        );
359
        $this->expectException(UnauthorizedException::class);
360
        $this->expectExceptionMessage("User does not have access to 'publish' 'content'");
361
        $contentService->publishVersion($draft->versionInfo, [self::GER_DE, self::ENG_GB]);
362
    }
363
364
    /**
365
     * @param \eZ\Publish\API\Repository\ContentService $contentService
366
     *
367
     * @return \eZ\Publish\API\Repository\Values\Content\Content
368
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
369
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
370
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
371
     */
372
    private function createMultilingualFolderDraft(ContentService $contentService): Content
373
    {
374
        $publishedContent = $this->createFolder(
375
            [
376
                self::ENG_US => 'Published US',
377
                self::GER_DE => 'Published DE',
378
            ],
379
            $this->generateId('location', 2)
380
        );
381
382
        return $contentService->createContentDraft($publishedContent->contentInfo);
383
    }
384
}
385