Completed
Push — 7.5 ( 302b83...d9c892 )
by
unknown
44:21 queued 26:35
created

testPublishVersionIsNotAllowedIfModifiedOtherTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 22
rs 9.568
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
    /**
254
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
255
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
256
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
257
     */
258
    public function testPublishVersionTranslation(): void
259
    {
260
        $repository = $this->getRepository();
261
        $contentService = $repository->getContentService();
262
        $permissionResolver = $repository->getPermissionResolver();
263
264
        $draft = $this->createMultilingualFolderDraft($contentService);
265
266
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
267
268
        $contentUpdateStruct->setField('name', 'Draft 1 DE', self::GER_DE);
269
270
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
271
272
        $admin = $permissionResolver->getCurrentUserReference();
273
        $permissionResolver->setCurrentUserReference($this->createEditorUserWithLanguageLimitation([self::GER_DE]));
274
275
        $contentService->publishVersion($draft->versionInfo, [self::GER_DE]);
276
277
        $permissionResolver->setCurrentUserReference($admin);
278
        $content = $contentService->loadContent($draft->contentInfo->id);
279
        $this->assertEquals(
280
            [
281
                self::ENG_US => 'Published US',
282
                self::GER_DE => 'Draft 1 DE',
283
            ],
284
            $content->fields['name']
285
        );
286
    }
287
288
    /**
289
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
290
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
291
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
292
     */
293
    public function testPublishVersionTranslationIsNotAllowed(): void
294
    {
295
        $repository = $this->getRepository();
296
        $contentService = $repository->getContentService();
297
        $permissionResolver = $repository->getPermissionResolver();
298
299
        $draft = $this->createMultilingualFolderDraft($contentService);
300
301
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
302
303
        $contentUpdateStruct->setField('name', 'Draft 1 EN', self::ENG_US);
304
305
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
306
307
        $permissionResolver->setCurrentUserReference($this->createEditorUserWithLanguageLimitation([self::GER_DE]));
308
309
        $this->expectException(UnauthorizedException::class);
310
        $contentService->publishVersion($draft->versionInfo, [self::ENG_US]);
311
    }
312
313
    /**
314
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
315
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
316
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
317
     */
318
    public function testPublishVersionTranslationIsNotAllowedWithTwoEditors(): void
319
    {
320
        $repository = $this->getRepository();
321
        $contentService = $repository->getContentService();
322
        $permissionResolver = $repository->getPermissionResolver();
323
324
        $editorDE = $this->createEditorUserWithLanguageLimitation([self::GER_DE], 'editor-de');
325
        $editorUS = $this->createEditorUserWithLanguageLimitation([self::ENG_US], 'editor-us');
326
327
        // German editor publishes content in German language
328
        $permissionResolver->setCurrentUserReference($editorDE);
329
330
        $folder = $this->createFolder([self::GER_DE => 'German Folder'], 2);
331
332
        // American editor creates and saves English draft
333
        $permissionResolver->setCurrentUserReference($editorUS);
334
335
        $folder = $contentService->loadContent($folder->id);
336
        $folderDraft = $contentService->createContentDraft($folder->contentInfo);
337
        $folderUpdateStruct = $contentService->newContentUpdateStruct();
338
        $folderUpdateStruct->setField('name', 'English Folder', self::ENG_US);
339
        $folderDraft = $contentService->updateContent(
340
            $folderDraft->versionInfo,
341
            $folderUpdateStruct
342
        );
343
344
        // German editor tries to publish English translation
345
        $permissionResolver->setCurrentUserReference($editorDE);
346
        $folderDraftVersionInfo = $contentService->loadVersionInfo(
347
            $folderDraft->contentInfo,
348
            $folderDraft->versionInfo->versionNo
349
        );
350
        self::assertTrue($folderDraftVersionInfo->isDraft());
351
        $this->expectException(UnauthorizedException::class);
352
        $this->expectExceptionMessage("User does not have access to 'publish' 'content'");
353
        $contentService->publishVersion($folderDraftVersionInfo, [self::ENG_US]);
354
    }
355
356
    /**
357
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
358
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
359
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
360
     */
361
    public function testPublishVersionTranslationWhenUserHasNoAccessToAllLanguages(): void
362
    {
363
        $repository = $this->getRepository();
364
        $contentService = $repository->getContentService();
365
        $permissionResolver = $repository->getPermissionResolver();
366
367
        $draft = $this->createMultilingualFolderDraft($contentService);
368
369
        $contentUpdateStruct = $contentService->newContentUpdateStruct();
370
371
        $contentUpdateStruct->setField('name', 'Draft 1 DE', self::GER_DE);
372
        $contentUpdateStruct->setField('name', 'Draft 1 GB', self::ENG_GB);
373
374
        $contentService->updateContent($draft->versionInfo, $contentUpdateStruct);
375
376
        $permissionResolver->setCurrentUserReference(
377
            $this->createEditorUserWithLanguageLimitation([self::GER_DE])
378
        );
379
        $this->expectException(UnauthorizedException::class);
380
        $this->expectExceptionMessage("User does not have access to 'publish' 'content'");
381
        $contentService->publishVersion($draft->versionInfo, [self::GER_DE, self::ENG_GB]);
382
    }
383
384
    /**
385
     * @param \eZ\Publish\API\Repository\ContentService $contentService
386
     *
387
     * @return \eZ\Publish\API\Repository\Values\Content\Content
388
     *
389
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
390
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
391
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
392
     */
393
    private function createMultilingualFolderDraft(ContentService $contentService): Content
394
    {
395
        $publishedContent = $this->createFolder(
396
            [
397
                self::ENG_US => 'Published US',
398
                self::GER_DE => 'Published DE',
399
            ],
400
            $this->generateId('location', 2)
401
        );
402
403
        return $contentService->createContentDraft($publishedContent->contentInfo);
404
    }
405
}
406