Completed
Push — EZP-30796 ( f39111...1b67d5 )
by
unknown
24:32
created

testCopyContentThrowsUnauthorizedExceptionWithGivenVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40

Duplication

Lines 40
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 40
loc 40
rs 9.28
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentServiceAuthorizationTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
12
use eZ\Publish\API\Repository\Repository;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14
use eZ\Publish\API\Repository\Values\Content\Location;
15
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
16
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation;
17
18
/**
19
 * Test case for operations in the ContentServiceAuthorization using in memory storage.
20
 *
21
 * @see eZ\Publish\API\Repository\ContentService
22
 * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadAnonymousUser
23
 * @group integration
24
 * @group authorization
25
 */
26
class ContentServiceAuthorizationTest extends BaseContentServiceTest
27
{
28
    /**
29
     * Test for the createContent() method.
30
     *
31
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
32
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
33
     */
34 View Code Duplication
    public function testCreateContentThrowsUnauthorizedException()
35
    {
36
        if ($this->isVersion4()) {
37
            $this->markTestSkipped('This test requires eZ Publish 5');
38
        }
39
40
        $repository = $this->getRepository();
41
42
        $anonymousUserId = $this->generateId('user', 10);
43
        /* BEGIN: Use Case */
44
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
45
        // demo installation
46
        // Load the user service
47
        $userService = $repository->getUserService();
48
49
        // Set anonymous user
50
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
52
        $contentTypeService = $repository->getContentTypeService();
53
54
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
55
56
        $contentService = $repository->getContentService();
57
58
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
59
        $contentCreate->setField('name', 'Awesome Sindelfingen forum');
60
61
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
62
        $contentCreate->alwaysAvailable = true;
63
64
        $this->expectException(UnauthorizedException::class);
65
        $this->expectExceptionMessageRegExp('/\'create\' \'content\'/');
66
67
        $contentService->createContent($contentCreate);
68
        /* END: Use Case */
69
    }
70
71
    /**
72
     * Test for the createContent() method.
73
     *
74
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
75
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
76
     */
77
    public function testCreateContentThrowsUnauthorizedExceptionWithSecondParameter()
78
    {
79
        $repository = $this->getRepository();
80
81
        $anonymousUserId = $this->generateId('user', 10);
82
        /* BEGIN: Use Case */
83
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
84
        // demo installation
85
        // Load the user service
86
        $userService = $repository->getUserService();
87
88
        // Set anonymous user
89
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
91
        $this->expectException(UnauthorizedException::class);
92
        $this->expectExceptionMessageRegExp('/\'create\' \'content\'/');
93
94
        $this->createContentDraftVersion1();
95
        /* END: Use Case */
96
    }
97
98
    /**
99
     * Test for the loadContentInfo() method.
100
     *
101
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
102
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
103
     */
104
    public function testLoadContentInfoThrowsUnauthorizedException()
105
    {
106
        $repository = $this->getRepository();
107
108
        $contentId = $this->generateId('object', 10);
109
        /* BEGIN: Use Case */
110
        $contentService = $repository->getContentService();
111
112
        $pseudoEditor = $this->createAnonymousWithEditorRole();
113
114
        // Set restricted editor user
115
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
116
117
        $this->expectException(UnauthorizedException::class);
118
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
119
120
        // $contentId contains a content object ID not accessible for anonymous
121
        $contentService->loadContentInfo($contentId);
122
        /* END: Use Case */
123
    }
124
125
    /**
126
     * Test for the sudo() method.
127
     *
128
     * @see \eZ\Publish\API\Repository\Repository::sudo()
129
     * @depends testLoadContentInfoThrowsUnauthorizedException
130
     */
131
    public function testSudo()
132
    {
133
        $repository = $this->getRepository();
134
        $contentId = $this->generateId('object', 10);
135
        // Set restricted editor user
136
        $repository->setCurrentUser($this->createAnonymousWithEditorRole());
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
137
138
        $contentInfo = $repository->sudo(function (Repository $repository) use ($contentId) {
139
            return $repository->getContentService()->loadContentInfo($contentId);
140
        });
141
142
        $this->assertInstanceOf(
143
            ContentInfo::class,
144
            $contentInfo
145
        );
146
    }
147
148
    /**
149
     * Test for the loadContentInfoList() method.
150
     *
151
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
152
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoList
153
     */
154
    public function testLoadContentInfoListSkipsUnauthorizedItems()
155
    {
156
        $repository = $this->getRepository();
157
        $contentId = $this->generateId('object', 10);
158
        $contentService = $repository->getContentService();
159
        $repository->setCurrentUser($this->createAnonymousWithEditorRole());
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
160
161
        $list = $contentService->loadContentInfoList([$contentId]);
162
163
        $this->assertCount(0, $list);
164
    }
165
166
    /**
167
     * Test for the loadContentInfoByRemoteId() method.
168
     *
169
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
170
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
171
     */
172
    public function testLoadContentInfoByRemoteIdThrowsUnauthorizedException()
173
    {
174
        $repository = $this->getRepository();
175
176
        /* BEGIN: Use Case */
177
        // RemoteId of the "Anonymous User" in an eZ Publish demo installation
178
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
179
180
        $contentService = $repository->getContentService();
181
182
        $pseudoEditor = $this->createAnonymousWithEditorRole();
183
184
        // Set restricted editor user
185
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
186
187
        $this->expectException(UnauthorizedException::class);
188
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
189
190
        $contentService->loadContentInfoByRemoteId($anonymousRemoteId);
191
        /* END: Use Case */
192
    }
193
194
    /**
195
     * Test for the loadVersionInfo() method.
196
     *
197
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
198
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
199
     */
200
    public function testLoadVersionInfoThrowsUnauthorizedException()
201
    {
202
        $repository = $this->getRepository();
203
204
        $anonymousUserId = $this->generateId('user', 10);
205
        /* BEGIN: Use Case */
206
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
207
        // demo installation
208
209
        $contentService = $repository->getContentService();
210
211
        // Load the ContentInfo for "Anonymous User"
212
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
213
214
        $pseudoEditor = $this->createAnonymousWithEditorRole();
215
216
        // Set restricted editor user
217
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
218
219
        $this->expectException(UnauthorizedException::class);
220
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
221
222
        $contentService->loadVersionInfo($contentInfo);
223
        /* END: Use Case */
224
    }
225
226
    /**
227
     * Test for the loadVersionInfo() method.
228
     *
229
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
230
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
231
     */
232
    public function testLoadVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
233
    {
234
        $repository = $this->getRepository();
235
236
        $anonymousUserId = $this->generateId('user', 10);
237
        /* BEGIN: Use Case */
238
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
239
        // demo installation
240
241
        $contentService = $repository->getContentService();
242
243
        // Load the ContentInfo for "Anonymous User"
244
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
245
246
        $pseudoEditor = $this->createAnonymousWithEditorRole();
247
248
        // Set restricted editor user
249
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
250
251
        $this->expectException(UnauthorizedException::class);
252
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
253
254
        $contentService->loadVersionInfo($contentInfo, 2);
255
        /* END: Use Case */
256
    }
257
258
    /**
259
     * Test for the loadVersionInfoById() method.
260
     *
261
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
262
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
263
     */
264
    public function testLoadVersionInfoByIdThrowsUnauthorizedException()
265
    {
266
        $repository = $this->getRepository();
267
268
        $anonymousUserId = $this->generateId('user', 10);
269
        /* BEGIN: Use Case */
270
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
271
        // demo installation
272
273
        $contentService = $repository->getContentService();
274
275
        $pseudoEditor = $this->createAnonymousWithEditorRole();
276
277
        // Set restricted editor user
278
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
279
280
        $this->expectException(UnauthorizedException::class);
281
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
282
283
        $contentService->loadVersionInfoById($anonymousUserId);
284
        /* END: Use Case */
285
    }
286
287
    /**
288
     * Test for the loadVersionInfoById() method.
289
     *
290
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
291
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
292
     */
293
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionWithSecondParameter()
294
    {
295
        $repository = $this->getRepository();
296
297
        $anonymousUserId = $this->generateId('user', 10);
298
        /* BEGIN: Use Case */
299
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
300
        // demo installation
301
302
        $contentService = $repository->getContentService();
303
304
        $pseudoEditor = $this->createAnonymousWithEditorRole();
305
306
        // Set restricted editor user
307
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
308
309
        $this->expectException(UnauthorizedException::class);
310
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
311
312
        $contentService->loadVersionInfoById($anonymousUserId, 2);
313
        /* END: Use Case */
314
    }
315
316
    /**
317
     * Test for the loadVersionInfoById() method.
318
     *
319
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
320
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
321
     */
322
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionForFirstDraft()
323
    {
324
        $repository = $this->getRepository();
325
326
        $contentService = $repository->getContentService();
327
328
        $anonymousUserId = $this->generateId('user', 10);
329
        /* BEGIN: Use Case */
330
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
331
        // demo installation
332
        $contentDraft = $this->createContentDraftVersion1();
333
334
        // Load the user service
335
        $userService = $repository->getUserService();
336
337
        // Set anonymous user
338
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
339
340
        $this->expectException(UnauthorizedException::class);
341
        // content versionread policy is needed because it is a draft
342
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
343
344
        $contentService->loadVersionInfoById(
345
            $contentDraft->id,
346
            $contentDraft->contentInfo->currentVersionNo
347
        );
348
        /* END: Use Case */
349
    }
350
351
    /**
352
     * Test for the loadContentByContentInfo() method.
353
     *
354
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
355
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
356
     */
357
    public function testLoadContentByContentInfoThrowsUnauthorizedException()
358
    {
359
        $repository = $this->getRepository();
360
361
        $anonymousUserId = $this->generateId('user', 10);
362
        /* BEGIN: Use Case */
363
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
364
        // demo installation
365
366
        $contentService = $repository->getContentService();
367
368
        // Load the ContentInfo for "Anonymous User"
369
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
370
371
        $pseudoEditor = $this->createAnonymousWithEditorRole();
372
373
        // Set restricted editor user
374
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
375
376
        $this->expectException(UnauthorizedException::class);
377
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
378
379
        $contentService->loadContentByContentInfo($contentInfo);
380
        /* END: Use Case */
381
    }
382
383
    /**
384
     * Test for the loadContentByContentInfo() method.
385
     *
386
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
387
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithLanguageParameters
388
     */
389
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithSecondParameter()
390
    {
391
        $repository = $this->getRepository();
392
393
        $anonymousUserId = $this->generateId('user', 10);
394
        /* BEGIN: Use Case */
395
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
396
        // demo installation
397
398
        $contentService = $repository->getContentService();
399
400
        // Load the ContentInfo for "Anonymous User"
401
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
402
403
        $pseudoEditor = $this->createAnonymousWithEditorRole();
404
405
        // Set restricted editor user
406
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
407
408
        $this->expectException(UnauthorizedException::class);
409
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
410
411
        $contentService->loadContentByContentInfo($contentInfo, ['eng-US']);
412
        /* END: Use Case */
413
    }
414
415
    /**
416
     * Test for the loadContentByContentInfo() method.
417
     *
418
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
419
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
420
     */
421
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithThirdParameter()
422
    {
423
        $repository = $this->getRepository();
424
425
        $anonymousUserId = $this->generateId('user', 10);
426
        /* BEGIN: Use Case */
427
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
428
        // demo installation
429
430
        $contentService = $repository->getContentService();
431
432
        // Load the ContentInfo for "Anonymous User"
433
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
434
435
        $pseudoEditor = $this->createAnonymousWithEditorRole();
436
437
        // Set restricted editor user
438
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
439
440
        $this->expectException(UnauthorizedException::class);
441
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
442
443
        $contentService->loadContentByContentInfo($contentInfo, ['eng-US'], 2);
444
        /* END: Use Case */
445
    }
446
447
    /**
448
     * Test for the loadContentByVersionInfo() method.
449
     *
450
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
451
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
452
     */
453
    public function testLoadContentByVersionInfoThrowsUnauthorizedException()
454
    {
455
        $repository = $this->getRepository();
456
457
        $anonymousUserId = $this->generateId('user', 10);
458
        /* BEGIN: Use Case */
459
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
460
        // demo installation
461
462
        $contentService = $repository->getContentService();
463
464
        // Load the ContentInfo for "Anonymous User"
465
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
466
467
        // Load the current VersionInfo
468
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
469
470
        $pseudoEditor = $this->createAnonymousWithEditorRole();
471
472
        // Set restricted editor user
473
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
474
475
        $this->expectException(UnauthorizedException::class);
476
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
477
478
        $contentService->loadContentByVersionInfo($versionInfo);
479
        /* END: Use Case */
480
    }
481
482
    /**
483
     * Test for the loadContentByVersionInfo() method.
484
     *
485
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
486
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfoWithSecondParameter
487
     */
488
    public function testLoadContentByVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
489
    {
490
        $repository = $this->getRepository();
491
492
        $anonymousUserId = $this->generateId('user', 10);
493
        /* BEGIN: Use Case */
494
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
495
        // demo installation
496
497
        $contentService = $repository->getContentService();
498
499
        // Load the ContentInfo for "Anonymous User"
500
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
501
502
        // Load the current VersionInfo
503
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
504
505
        $pseudoEditor = $this->createAnonymousWithEditorRole();
506
507
        // Set restricted editor user
508
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
509
510
        $this->expectException(UnauthorizedException::class);
511
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
512
513
        $contentService->loadContentByVersionInfo($versionInfo, ['eng-US']);
514
        /* END: Use Case */
515
    }
516
517
    /**
518
     * Test for the loadContent() method.
519
     *
520
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
521
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
522
     */
523
    public function testLoadContentThrowsUnauthorizedException()
524
    {
525
        $repository = $this->getRepository();
526
527
        $anonymousUserId = $this->generateId('user', 10);
528
        /* BEGIN: Use Case */
529
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
530
        // demo installation
531
532
        $contentService = $repository->getContentService();
533
534
        $pseudoEditor = $this->createAnonymousWithEditorRole();
535
536
        // Set restricted editor user
537
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
538
539
        $this->expectException(UnauthorizedException::class);
540
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
541
542
        $contentService->loadContent($anonymousUserId);
543
        /* END: Use Case */
544
    }
545
546
    /**
547
     * Test for the loadContent() method.
548
     *
549
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
550
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
551
     */
552
    public function testLoadContentThrowsUnauthorizedExceptionWithSecondParameter()
553
    {
554
        $repository = $this->getRepository();
555
556
        $anonymousUserId = $this->generateId('user', 10);
557
        /* BEGIN: Use Case */
558
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
559
        // demo installation
560
561
        $contentService = $repository->getContentService();
562
563
        $pseudoEditor = $this->createAnonymousWithEditorRole();
564
565
        // Set restricted editor user
566
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
567
568
        $this->expectException(UnauthorizedException::class);
569
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
570
571
        $contentService->loadContent($anonymousUserId, ['eng-US']);
572
        /* END: Use Case */
573
    }
574
575
    /**
576
     * Test for the loadContent() method.
577
     *
578
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
579
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
580
     */
581
    public function testLoadContentThrowsUnauthorizedExceptionWithThirdParameter()
582
    {
583
        $repository = $this->getRepository();
584
585
        $anonymousUserId = $this->generateId('user', 10);
586
        /* BEGIN: Use Case */
587
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
588
        // demo installation
589
590
        $contentService = $repository->getContentService();
591
592
        $pseudoEditor = $this->createAnonymousWithEditorRole();
593
594
        // Set restricted editor user
595
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
596
597
        $this->expectException(UnauthorizedException::class);
598
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
599
600
        $contentService->loadContent($anonymousUserId, ['eng-US'], 2);
601
        /* END: Use Case */
602
    }
603
604
    /**
605
     * Test for the loadContent() method on a draft.
606
     *
607
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
608
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
609
     */
610 View Code Duplication
    public function testLoadContentThrowsUnauthorizedExceptionOnDrafts()
611
    {
612
        /** @var $repository \eZ\Publish\API\Repository\Repository */
613
        $repository = $this->getRepository();
614
615
        $anonymousUserId = $this->generateId('user', 10);
616
        /* BEGIN: Use Case */
617
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
618
        // demo installation
619
        $user = $this->createUserVersion1();
620
621
        // Set new editor as a content owner
622
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
623
624
        // Create draft with this user
625
        $draft = $this->createContentDraftVersion1(2, 'folder');
626
627
        // Load anonymous user
628
        $userService = $repository->getUserService();
629
        $user = $userService->loadUser($anonymousUserId);
630
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
631
632
        // Try to load the draft with anonymous user to make sure access won't be allowed by throwing an exception
633
        $contentService = $repository->getContentService();
634
635
        $this->expectException(UnauthorizedException::class);
636
        // content versionread policy is needed because it is a draft
637
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
638
639
        $contentService->loadContent($draft->id);
640
        /* END: Use Case */
641
    }
642
643
    /**
644
     * Test for the ContentService::loadContent() method on an archive.
645
     *
646
     * This test the version permission on loading archived versions
647
     *
648
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
649
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
650
     */
651
    public function testLoadContentThrowsUnauthorizedExceptionsOnArchives()
652
    {
653
        /** @var $repository \eZ\Publish\API\Repository\Repository */
654
        $repository = $this->getRepository();
655
656
        $anonymousUserId = $this->generateId('user', 10);
657
        /* BEGIN: Use Case */
658
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
659
        // demo installation
660
        // get necessary services
661
        $contentTypeService = $repository->getContentTypeService();
662
        $contentService = $repository->getContentService();
663
        $locationSercice = $repository->getLocationService();
664
665
        // set admin as current user
666
        $repository->setCurrentUser($repository->getUserService()->loadUserByLogin('admin'));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
667
668
        // create folder
669
        $newStruct = $contentService->newContentCreateStruct(
670
            $contentTypeService->loadContentTypeByIdentifier('folder'),
671
            'eng-US'
672
        );
673
        $newStruct->setField('name', 'Test Folder');
674
        $draft = $contentService->createContent(
675
            $newStruct,
676
            [$locationSercice->newLocationCreateStruct(2)]
677
        );
678
        $object = $contentService->publishVersion($draft->versionInfo);
679
680
        // update folder to make an archived version
681
        $updateStruct = $contentService->newContentUpdateStruct();
682
        $updateStruct->setField('name', 'Test Folder Updated');
683
        $draftUpdated = $contentService->updateContent(
684
            $contentService->createContentDraft($object->contentInfo)->versionInfo,
685
            $updateStruct
686
        );
687
        $objectUpdated = $contentService->publishVersion($draftUpdated->versionInfo);
688
689
        // set an anonymous as current user
690
        $repository->setCurrentUser($repository->getUserService()->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
691
692
        $this->expectException(UnauthorizedException::class);
693
        // content versionread policy is needed because it is a draft
694
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
695
696
        $contentService->loadContent($objectUpdated->id, null, 1);
697
        /* END: Use Case */
698
    }
699
700
    /**
701
     * Test for the loadContentByRemoteId() method.
702
     *
703
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
704
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
705
     */
706
    public function testLoadContentByRemoteIdThrowsUnauthorizedException()
707
    {
708
        $repository = $this->getRepository();
709
710
        /* BEGIN: Use Case */
711
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
712
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
713
714
        $contentService = $repository->getContentService();
715
716
        $pseudoEditor = $this->createAnonymousWithEditorRole();
717
718
        // Set restricted editor user
719
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
720
721
        $this->expectException(UnauthorizedException::class);
722
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
723
724
        $contentService->loadContentByRemoteId($anonymousRemoteId);
725
        /* END: Use Case */
726
    }
727
728
    /**
729
     * Test for the loadContentByRemoteId() method.
730
     *
731
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
732
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithSecondParameter
733
     */
734
    public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithSecondParameter()
735
    {
736
        $repository = $this->getRepository();
737
738
        /* BEGIN: Use Case */
739
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
740
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
741
742
        $contentService = $repository->getContentService();
743
744
        $pseudoEditor = $this->createAnonymousWithEditorRole();
745
746
        // Set restricted editor user
747
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
748
749
        $this->expectException(UnauthorizedException::class);
750
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
751
752
        $contentService->loadContentByRemoteId($anonymousRemoteId, ['eng-US']);
753
        /* END: Use Case */
754
    }
755
756
    /**
757
     * Test for the loadContentByRemoteId() method.
758
     *
759
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
760
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
761
     */
762
    public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithThirdParameter()
763
    {
764
        $repository = $this->getRepository();
765
766
        /* BEGIN: Use Case */
767
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
768
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
769
770
        $contentService = $repository->getContentService();
771
772
        $pseudoEditor = $this->createAnonymousWithEditorRole();
773
774
        // Set restricted editor user
775
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
776
777
        $this->expectException(UnauthorizedException::class);
778
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
779
780
        $contentService->loadContentByRemoteId($anonymousRemoteId, ['eng-US'], 2);
781
        /* END: Use Case */
782
    }
783
784
    /**
785
     * Test for the updateContentMetadata() method.
786
     *
787
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
788
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
789
     */
790
    public function testUpdateContentMetadataThrowsUnauthorizedException()
791
    {
792
        $repository = $this->getRepository();
793
794
        $contentService = $repository->getContentService();
795
796
        $anonymousUserId = $this->generateId('user', 10);
797
        /* BEGIN: Use Case */
798
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
799
        // demo installation
800
        $content = $this->createContentVersion1();
801
802
        // Get ContentInfo instance.
803
        $contentInfo = $content->contentInfo;
804
805
        // Load the user service
806
        $userService = $repository->getUserService();
807
808
        // Set anonymous user
809
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
810
811
        // Creates a metadata update struct
812
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
813
814
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
815
        $metadataUpdate->mainLanguageCode = 'eng-US';
816
        $metadataUpdate->alwaysAvailable = false;
817
        $metadataUpdate->publishedDate = $this->createDateTime();
818
        $metadataUpdate->modificationDate = $this->createDateTime();
819
820
        $this->expectException(UnauthorizedException::class);
821
        $this->expectExceptionMessageRegExp('/\'edit\' \'content\'/');
822
823
        $contentService->updateContentMetadata(
824
            $contentInfo,
825
            $metadataUpdate
826
        );
827
        /* END: Use Case */
828
    }
829
830
    /**
831
     * Test for the deleteContent() method.
832
     *
833
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
834
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
835
     */
836
    public function testDeleteContentThrowsUnauthorizedException()
837
    {
838
        $repository = $this->getRepository();
839
        $contentService = $repository->getContentService();
840
841
        $anonymousUserId = $this->generateId('user', 10);
842
        /* BEGIN: Use Case */
843
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
844
        // demo installation
845
        $contentVersion2 = $this->createContentVersion2();
846
847
        // Get ContentInfo instance
848
        $contentInfo = $contentVersion2->contentInfo;
849
850
        // Load the user service
851
        $userService = $repository->getUserService();
852
853
        // Set anonymous user
854
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
855
856
        $this->expectException(UnauthorizedException::class);
857
        $this->expectExceptionMessageRegExp('/\'remove\' \'content\'/');
858
859
        $contentService->deleteContent($contentInfo);
860
        /* END: Use Case */
861
    }
862
863
    /**
864
     * Test for the createContentDraft() method.
865
     *
866
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
867
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
868
     */
869
    public function testCreateContentDraftThrowsUnauthorizedException()
870
    {
871
        $repository = $this->getRepository();
872
873
        $contentService = $repository->getContentService();
874
875
        $anonymousUserId = $this->generateId('user', 10);
876
        /* BEGIN: Use Case */
877
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
878
        // demo installation
879
        $content = $this->createContentVersion1();
880
881
        // Get ContentInfo instance
882
        $contentInfo = $content->contentInfo;
883
884
        // Load the user service
885
        $userService = $repository->getUserService();
886
887
        // Set anonymous user
888
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
889
890
        $this->expectException(UnauthorizedException::class);
891
        $this->expectExceptionMessageRegExp('/\'edit\' \'content\'/');
892
893
        $contentService->createContentDraft($contentInfo);
894
        /* END: Use Case */
895
    }
896
897
    /**
898
     * Test for the createContentDraft() method.
899
     *
900
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
901
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithSecondParameter
902
     */
903 View Code Duplication
    public function testCreateContentDraftThrowsUnauthorizedExceptionWithSecondParameter()
904
    {
905
        $repository = $this->getRepository();
906
907
        $contentService = $repository->getContentService();
908
909
        $anonymousUserId = $this->generateId('user', 10);
910
        /* BEGIN: Use Case */
911
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
912
        // demo installation
913
        $content = $this->createContentVersion1();
914
915
        // Get ContentInfo and VersionInfo instances
916
        $contentInfo = $content->contentInfo;
917
        $versionInfo = $content->getVersionInfo();
918
919
        // Load the user service
920
        $userService = $repository->getUserService();
921
922
        // Set anonymous user
923
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
924
925
        $this->expectException(UnauthorizedException::class);
926
        $this->expectExceptionMessageRegExp('/\'edit\' \'content\'/');
927
928
        $contentService->createContentDraft($contentInfo, $versionInfo);
929
        /* END: Use Case */
930
    }
931
932
    /**
933
     * Test for the loadContentDrafts() method.
934
     *
935
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
936
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
937
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
938
     */
939
    public function testLoadContentDraftsThrowsUnauthorizedException()
940
    {
941
        $repository = $this->getRepository();
942
943
        $anonymousUserId = $this->generateId('user', 10);
944
        /* BEGIN: Use Case */
945
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
946
        // demo installation
947
        $contentService = $repository->getContentService();
948
949
        // Load the user service
950
        $userService = $repository->getUserService();
951
952
        // Set anonymous user
953
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
954
955
        $this->expectException(UnauthorizedException::class);
956
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
957
958
        $contentService->loadContentDrafts();
959
        /* END: Use Case */
960
    }
961
962
    /**
963
     * Test for the loadContentDrafts() method.
964
     *
965
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
966
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
967
     */
968 View Code Duplication
    public function testLoadContentDraftsThrowsUnauthorizedExceptionWithFirstParameter()
969
    {
970
        $repository = $this->getRepository();
971
972
        $administratorUserId = $this->generateId('user', 14);
973
        $anonymousUserId = $this->generateId('user', 10);
974
        /* BEGIN: Use Case */
975
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
976
        // demo installation
977
        // $administratorUserId is  the ID of the "Administrator" user in a eZ
978
        // Publish demo installation.
979
980
        $contentService = $repository->getContentService();
981
982
        // Load the user service
983
        $userService = $repository->getUserService();
984
985
        // Load the "Administrator" user
986
        $administratorUser = $userService->loadUser($administratorUserId);
987
988
        // Set anonymous user
989
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
990
991
        $this->expectException(UnauthorizedException::class);
992
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
993
994
        $contentService->loadContentDrafts($administratorUser);
995
        /* END: Use Case */
996
    }
997
998
    /**
999
     * Test for the updateContent() method.
1000
     *
1001
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1002
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1003
     */
1004 View Code Duplication
    public function testUpdateContentThrowsUnauthorizedException()
1005
    {
1006
        $repository = $this->getRepository();
1007
        $contentService = $repository->getContentService();
1008
1009
        $anonymousUserId = $this->generateId('user', 10);
1010
        /* BEGIN: Use Case */
1011
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1012
        // demo installation
1013
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1014
        // demo installation
1015
        $draftVersion2 = $this->createContentDraftVersion2();
1016
1017
        // Get VersionInfo instance
1018
        $versionInfo = $draftVersion2->getVersionInfo();
1019
1020
        // Load the user service
1021
        $userService = $repository->getUserService();
1022
1023
        // Set anonymous user
1024
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1025
1026
        // Create an update struct and modify some fields
1027
        $contentUpdate = $contentService->newContentUpdateStruct();
1028
        $contentUpdate->setField('name', 'An awesome² story about ezp.');
1029
        $contentUpdate->setField('name', 'An awesome²³ story about ezp.', 'eng-GB');
1030
1031
        $contentUpdate->initialLanguageCode = 'eng-US';
1032
1033
        $this->expectException(UnauthorizedException::class);
1034
        /* TODO - the `content/edit` policy should be probably needed */
1035
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1036
1037
        $contentService->updateContent($versionInfo, $contentUpdate);
1038
        /* END: Use Case */
1039
    }
1040
1041
    /**
1042
     * Test for the publishVersion() method.
1043
     *
1044
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1045
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1046
     */
1047
    public function testPublishVersionThrowsUnauthorizedException()
1048
    {
1049
        $repository = $this->getRepository();
1050
        $contentService = $repository->getContentService();
1051
1052
        $anonymousUserId = $this->generateId('user', 10);
1053
        /* BEGIN: Use Case */
1054
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1055
        // demo installation
1056
        $draft = $this->createContentDraftVersion1();
1057
1058
        // Load the user service
1059
        $userService = $repository->getUserService();
1060
1061
        // Set anonymous user
1062
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1063
1064
        $this->expectException(UnauthorizedException::class);
1065
        $this->expectExceptionMessageRegExp('/\'publish\' \'content\'/');
1066
1067
        $contentService->publishVersion($draft->getVersionInfo());
1068
        /* END: Use Case */
1069
    }
1070
1071
    /**
1072
     * Test for the deleteVersion() method.
1073
     *
1074
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
1075
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteVersion
1076
     */
1077
    public function testDeleteVersionThrowsUnauthorizedException()
1078
    {
1079
        $repository = $this->getRepository();
1080
        $contentService = $repository->getContentService();
1081
1082
        $anonymousUserId = $this->generateId('user', 10);
1083
        /* BEGIN: Use Case */
1084
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1085
        // demo installation
1086
        $draft = $this->createContentDraftVersion1();
1087
1088
        // Load the user service
1089
        $userService = $repository->getUserService();
1090
1091
        // Set anonymous user
1092
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1093
1094
        $this->expectException(UnauthorizedException::class);
1095
        $this->expectExceptionMessageRegExp('/\'versionremove\' \'content\'/');
1096
1097
        $contentService->deleteVersion($draft->getVersionInfo());
1098
        /* END: Use Case */
1099
    }
1100
1101
    /**
1102
     * Test for the loadVersions() method.
1103
     *
1104
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
1105
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
1106
     */
1107
    public function testLoadVersionsThrowsUnauthorizedException()
1108
    {
1109
        $repository = $this->getRepository();
1110
1111
        $contentService = $repository->getContentService();
1112
1113
        $anonymousUserId = $this->generateId('user', 10);
1114
        /* BEGIN: Use Case */
1115
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1116
        // demo installation
1117
        $contentVersion2 = $this->createContentVersion2();
1118
1119
        // Get ContentInfo instance of version 2
1120
        $contentInfo = $contentVersion2->contentInfo;
1121
1122
        // Load the user service
1123
        $userService = $repository->getUserService();
1124
1125
        // Set anonymous user
1126
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1127
1128
        $this->expectException(UnauthorizedException::class);
1129
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1130
1131
        $contentService->loadVersions($contentInfo);
1132
        /* END: Use Case */
1133
    }
1134
1135
    /**
1136
     * Test for the copyContent() method.
1137
     *
1138
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
1139
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
1140
     */
1141 View Code Duplication
    public function testCopyContentThrowsUnauthorizedException()
1142
    {
1143
        $parentLocationId = $this->generateId('location', 52);
1144
1145
        $repository = $this->getRepository();
1146
1147
        $contentService = $repository->getContentService();
1148
        $locationService = $repository->getLocationService();
1149
1150
        $anonymousUserId = $this->generateId('user', 10);
1151
        /* BEGIN: Use Case */
1152
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1153
        // demo installation
1154
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
1155
1156
        // Get ContentInfo instance of version 2
1157
        $contentInfo = $contentVersion2->contentInfo;
1158
1159
        // Load the user service
1160
        $userService = $repository->getUserService();
1161
1162
        // Set anonymous user
1163
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1164
1165
        // Configure new target location
1166
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
1167
1168
        $targetLocationCreate->priority = 42;
1169
        $targetLocationCreate->hidden = true;
1170
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
1171
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
1172
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
1173
1174
        $this->expectException(UnauthorizedException::class);
1175
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
1176
1177
        $contentService->copyContent(
1178
            $contentInfo,
1179
            $targetLocationCreate
1180
        );
1181
        /* END: Use Case */
1182
    }
1183
1184
    /**
1185
     * Test for the copyContent() method.
1186
     *
1187
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
1188
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContentWithGivenVersion
1189
     */
1190 View Code Duplication
    public function testCopyContentThrowsUnauthorizedExceptionWithGivenVersion()
1191
    {
1192
        $parentLocationId = $this->generateId('location', 52);
1193
1194
        $repository = $this->getRepository();
1195
1196
        $contentService = $repository->getContentService();
1197
        $locationService = $repository->getLocationService();
1198
1199
        $anonymousUserId = $this->generateId('user', 10);
1200
        /* BEGIN: Use Case */
1201
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1202
        // demo installation
1203
        $contentVersion2 = $this->createContentVersion2();
1204
1205
        // Load the user service
1206
        $userService = $repository->getUserService();
1207
1208
        // Set anonymous user
1209
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1210
1211
        // Configure new target location
1212
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
1213
1214
        $targetLocationCreate->priority = 42;
1215
        $targetLocationCreate->hidden = true;
1216
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
1217
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
1218
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
1219
1220
        $this->expectException(UnauthorizedException::class);
1221
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1222
1223
        $contentService->copyContent(
1224
            $contentVersion2->contentInfo,
1225
            $targetLocationCreate,
1226
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
1227
        );
1228
        /* END: Use Case */
1229
    }
1230
1231
    /**
1232
     * Test for the loadRelations() method.
1233
     *
1234
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1235
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
1236
     */
1237
    public function testLoadRelationsThrowsUnauthorizedException()
1238
    {
1239
        $repository = $this->getRepository();
1240
1241
        $contentService = $repository->getContentService();
1242
1243
        /* BEGIN: Use Case */
1244
        $user = $this->createMediaUserVersion1();
1245
1246
        // Remote id of the "Setup" page of a eZ Publish demo installation.
1247
        $setupRemoteId = '241d538ce310074e602f29f49e44e938';
1248
1249
        $versionInfo = $contentService->loadVersionInfo(
1250
            $contentService->loadContentInfoByRemoteId(
1251
                $setupRemoteId
1252
            )
1253
        );
1254
1255
        // Set media editor as current user
1256
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1257
1258
        $this->expectException(UnauthorizedException::class);
1259
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
1260
1261
        $contentService->loadRelations($versionInfo);
1262
        /* END: Use Case */
1263
    }
1264
1265
    /**
1266
     * Test for the loadRelations() method.
1267
     *
1268
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1269
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
1270
     */
1271
    public function testLoadRelationsForDraftVersionThrowsUnauthorizedException()
1272
    {
1273
        $repository = $this->getRepository();
1274
1275
        $contentService = $repository->getContentService();
1276
1277
        $anonymousUserId = $this->generateId('user', 10);
1278
        /* BEGIN: Use Case */
1279
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1280
        // demo installation
1281
        $draft = $this->createContentDraftVersion1();
1282
1283
        // Load the user service
1284
        $userService = $repository->getUserService();
1285
1286
        // Set anonymous user
1287
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1288
1289
        $this->expectException(UnauthorizedException::class);
1290
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1291
1292
        $contentService->loadRelations($draft->versionInfo);
1293
        /* END: Use Case */
1294
    }
1295
1296
    /**
1297
     * Test for the loadReverseRelations() method.
1298
     *
1299
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
1300
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
1301
     */
1302
    public function testLoadReverseRelationsThrowsUnauthorizedException()
1303
    {
1304
        $repository = $this->getRepository();
1305
1306
        $contentService = $repository->getContentService();
1307
1308
        /* BEGIN: Use Case */
1309
        $user = $this->createMediaUserVersion1();
1310
1311
        // Remote id of the "Media" page of a eZ Publish demo installation.
1312
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1313
1314
        $contentInfo = $contentService->loadContentInfoByRemoteId(
1315
            $mediaRemoteId
1316
        );
1317
1318
        // Set media editor as current user
1319
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1320
1321
        $this->expectException(UnauthorizedException::class);
1322
        $this->expectExceptionMessageRegExp('/\'reverserelatedlist\' \'content\'/');
1323
1324
        $contentService->loadReverseRelations($contentInfo);
1325
        /* END: Use Case */
1326
    }
1327
1328
    /**
1329
     * Test for the addRelation() method.
1330
     *
1331
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
1332
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
1333
     */
1334 View Code Duplication
    public function testAddRelationThrowsUnauthorizedException()
1335
    {
1336
        $repository = $this->getRepository();
1337
1338
        $contentService = $repository->getContentService();
1339
1340
        $anonymousUserId = $this->generateId('user', 10);
1341
        /* BEGIN: Use Case */
1342
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1343
        // demo installation
1344
        // Remote id of the "Media" page of a eZ Publish demo installation.
1345
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1346
1347
        $draft = $this->createContentDraftVersion1();
1348
1349
        // Get the draft's version info
1350
        $versionInfo = $draft->getVersionInfo();
1351
1352
        // Load other content object
1353
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1354
1355
        // Load the user service
1356
        $userService = $repository->getUserService();
1357
1358
        // Set anonymous user
1359
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1360
1361
        $this->expectException(UnauthorizedException::class);
1362
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1363
1364
        $contentService->addRelation(
1365
            $versionInfo,
1366
            $media
1367
        );
1368
        /* END: Use Case */
1369
    }
1370
1371
    /**
1372
     * Test for the deleteRelation() method.
1373
     *
1374
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
1375
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
1376
     */
1377
    public function testDeleteRelationThrowsUnauthorizedException()
1378
    {
1379
        $repository = $this->getRepository();
1380
1381
        $contentService = $repository->getContentService();
1382
1383
        $anonymousUserId = $this->generateId('user', 10);
1384
        /* BEGIN: Use Case */
1385
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1386
        // demo installation
1387
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
1388
        // demo installation.
1389
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1390
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
1391
1392
        $draft = $this->createContentDraftVersion1();
1393
1394
        // Get the draft's version info
1395
        $versionInfo = $draft->getVersionInfo();
1396
1397
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1398
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
1399
1400
        // Establish some relations
1401
        $contentService->addRelation($draft->getVersionInfo(), $media);
1402
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
1403
1404
        // Load the user service
1405
        $userService = $repository->getUserService();
1406
1407
        // Set anonymous user
1408
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1409
1410
        $this->expectException(UnauthorizedException::class);
1411
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1412
1413
        $contentService->deleteRelation($versionInfo, $media);
1414
        /* END: Use Case */
1415
    }
1416
1417
    /**
1418
     * Creates a pseudo editor with a limitation to objects in the "Media/Images"
1419
     * subtree.
1420
     *
1421
     * @return \eZ\Publish\API\Repository\Values\User\User
1422
     */
1423
    private function createAnonymousWithEditorRole()
1424
    {
1425
        $repository = $this->getRepository();
1426
1427
        $anonymousUserId = $this->generateId('user', 10);
1428
        /* BEGIN: Use Case */
1429
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1430
        // demo installation
1431
        $roleService = $repository->getRoleService();
1432
        $userService = $repository->getUserService();
1433
1434
        $user = $userService->loadUser($anonymousUserId);
1435
        $role = $roleService->loadRoleByIdentifier('Editor');
1436
1437
        // Assign "Editor" role with limitation to "Media/Images"
1438
        $roleService->assignRoleToUser(
1439
            $role,
1440
            $user,
1441
            new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation(
1442
                [
1443
                    'limitationValues' => ['/1/43/51/'],
1444
                ]
1445
            )
1446
        );
1447
1448
        $pseudoEditor = $userService->loadUser($user->id);
1449
        /* END: Inline */
1450
1451
        return $pseudoEditor;
1452
    }
1453
1454
    /**
1455
     * Test that for an user that doesn't have access (read permissions) to an
1456
     * related object, executing loadRelations() would not throw any exception,
1457
     * only that the non-readable related object(s) won't be loaded.
1458
     *
1459
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1460
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
1461
     */
1462
    public function testLoadRelationsWithUnauthorizedRelations()
1463
    {
1464
        $repository = $this->getRepository();
1465
1466
        $anonymousUserId = $this->generateId('user', 10);
1467
        /* BEGIN: Use Case */
1468
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1469
        // demo installation
1470
        $mainLanguage = 'eng-GB';
1471
1472
        $contentService = $repository->getContentService();
1473
        $contenTypeService = $repository->getContentTypeService();
1474
        $locationService = $repository->getLocationService();
1475
        $sectionService = $repository->getSectionService();
1476
        $userService = $repository->getUserService();
1477
1478
        // set the current user as admin to create the environment to test
1479
        $repository->setCurrentUser($userService->loadUserByLogin('admin'));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1480
1481
        // create section
1482
        // since anonymous users have their read permissions to specific sections
1483
        // the created section will be non-readable to them
1484
        $sectionCreate = $sectionService->newSectionCreateStruct();
1485
        $sectionCreate->identifier = 'private';
1486
        $sectionCreate->name = 'Private Section';
1487
        $section = $sectionService->createSection($sectionCreate);
1488
1489
        // create objects for testing
1490
        // here we will create 4 objects which 2 will be readable by an anonymous
1491
        // user, and the other 2 wont these last 2 will go to a private section
1492
        // where anonymous can't read, just like:
1493
        // readable object 1 -> /Main Folder
1494
        // readable object 2 -> /Main Folder/Available Folder
1495
        // non-readable object 1 -> /Restricted Folder
1496
        // non-readable object 2 -> /Restricted Folder/Unavailable Folder
1497
        //
1498
        // here is created - readable object 1 -> /Main Folder
1499
        $mainFolderCreate = $contentService->newContentCreateStruct(
1500
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1501
            $mainLanguage
1502
        );
1503
        $mainFolderCreate->setField('name', 'Main Folder');
1504
        $mainFolder = $contentService->publishVersion(
1505
            $contentService->createContent(
1506
                $mainFolderCreate,
1507
                [$locationService->newLocationCreateStruct(2)]
1508
            )->versionInfo
1509
        );
1510
1511
        // here is created readable object 2 -> /Main Folder/Available Folder
1512
        $availableFolderCreate = $contentService->newContentCreateStruct(
1513
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1514
            $mainLanguage
1515
        );
1516
        $availableFolderCreate->setField('name', 'Avaliable Folder');
1517
        $availableFolder = $contentService->publishVersion(
1518
            $contentService->createContent(
1519
                $availableFolderCreate,
1520
                [$locationService->newLocationCreateStruct($mainFolder->contentInfo->mainLocationId)]
1521
            )->versionInfo
1522
        );
1523
1524
        // here is created the non-readable object 1 -> /Restricted Folder
1525
        $restrictedFolderCreate = $contentService->newContentCreateStruct(
1526
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1527
            $mainLanguage
1528
        );
1529
        $restrictedFolderCreate->setField('name', 'Restricted Folder');
1530
        $restrictedFolderCreate->sectionId = $section->id;
1531
        $restrictedFolder = $contentService->publishVersion(
1532
            $contentService->createContent(
1533
                $restrictedFolderCreate,
1534
                [$locationService->newLocationCreateStruct(2)]
1535
            )->versionInfo
1536
        );
1537
1538
        // here is created non-readable object 2 -> /Restricted Folder/Unavailable Folder
1539
        $unavailableFolderCreate = $contentService->newContentCreateStruct(
1540
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1541
            $mainLanguage
1542
        );
1543
        $unavailableFolderCreate->setField('name', 'Unavailable Folder');
1544
        $unavailableFolder = $contentService->publishVersion(
1545
            $contentService->createContent(
1546
                $unavailableFolderCreate,
1547
                [$locationService->newLocationCreateStruct($restrictedFolder->contentInfo->mainLocationId)]
1548
            )->versionInfo
1549
        );
1550
1551
        // this will be our test object, which will have all the relations (as source)
1552
        // and it is readable by the anonymous user
1553
        $testFolderCreate = $contentService->newContentCreateStruct(
1554
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1555
            $mainLanguage
1556
        );
1557
        $testFolderCreate->setField('name', 'Test Folder');
1558
        $testFolderDraft = $contentService->createContent(
1559
            $testFolderCreate,
1560
            [$locationService->newLocationCreateStruct(2)]
1561
        )->versionInfo;
1562
1563
        // add relations to test folder (as source)
1564
        // the first 2 will be read by the user
1565
        // and the other 2 wont
1566
        //
1567
        // create relation from Test Folder to Main Folder
1568
        $mainRelation = $contentService->addRelation(
1569
            $testFolderDraft,
1570
            $mainFolder->getVersionInfo()->getContentInfo()
1571
        );
1572
        // create relation from Test Folder to Available Folder
1573
        $availableRelation = $contentService->addRelation(
1574
            $testFolderDraft,
1575
            $availableFolder->getVersionInfo()->getContentInfo()
1576
        );
1577
        // create relation from Test Folder to Restricted Folder
1578
        $contentService->addRelation(
1579
            $testFolderDraft,
1580
            $restrictedFolder->getVersionInfo()->getContentInfo()
1581
        );
1582
        //create relation from Test Folder to Unavailable Folder
1583
        $contentService->addRelation(
1584
            $testFolderDraft,
1585
            $unavailableFolder->getVersionInfo()->getContentInfo()
1586
        );
1587
1588
        // publish Test Folder
1589
        $testFolder = $contentService->publishVersion($testFolderDraft);
1590
1591
        // set the current user to be an anonymous user since we want to test that
1592
        // if the user doesn't have access to an related object that object wont
1593
        // be loaded and no exception will be thrown
1594
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1595
1596
        // finaly load relations ( verify no exception is thrown )
1597
        $actualRelations = $contentService->loadRelations($testFolder->getVersionInfo());
1598
1599
        /* END: Use case */
1600
1601
        // assert results
1602
        // verify that the only expected relations are from the 2 readable objects
1603
        // Main Folder and Available Folder
1604
        $expectedRelations = [
1605
            $mainRelation->destinationContentInfo->id => $mainRelation,
1606
            $availableRelation->destinationContentInfo->id => $availableRelation,
1607
        ];
1608
1609
        // assert there are as many expected relations as actual ones
1610
        $this->assertEquals(
1611
            count($expectedRelations),
1612
            count($actualRelations),
1613
            "Expected '" . count($expectedRelations)
1614
            . "' relations found '" . count($actualRelations) . "'"
1615
        );
1616
1617
        // assert each relation
1618
        foreach ($actualRelations as $relation) {
1619
            $destination = $relation->destinationContentInfo;
1620
            $expected = $expectedRelations[$destination->id]->destinationContentInfo;
1621
            $this->assertNotEmpty($expected, "Non expected relation with '{$destination->id}' id found");
1622
            $this->assertEquals(
1623
                $expected->id,
1624
                $destination->id,
1625
                "Expected relation with '{$expected->id}' id found '{$destination->id}' id"
1626
            );
1627
            $this->assertEquals(
1628
                $expected->name,
1629
                $destination->name,
1630
                "Expected relation with '{$expected->name}' name found '{$destination->name}' name"
1631
            );
1632
1633
            // remove from list
1634
            unset($expectedRelations[$destination->id]);
1635
        }
1636
1637
        // verify all expected relations were found
1638
        $this->assertEquals(
1639
            0,
1640
            count($expectedRelations),
1641
            "Expected to find '" . (count($expectedRelations) + count($actualRelations))
1642
            . "' relations found '" . count($actualRelations) . "'"
1643
        );
1644
    }
1645
1646
    /**
1647
     * Test copying Content to the authorized Location (limited by policies).
1648
     */
1649
    public function testCopyContentToAuthorizedLocation()
1650
    {
1651
        $repository = $this->getRepository();
1652
        $contentService = $repository->getContentService();
1653
        $locationService = $repository->getLocationService();
1654
        $roleService = $repository->getRoleService();
1655
1656
        // Create and publish folders for the test case
1657
        $folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']);
1658
        $contentService->publishVersion($folderDraft->versionInfo);
1659
        $authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']);
1660
        $authorizedFolder = $contentService->publishVersion($authorizedFolderDraft->versionInfo);
1661
1662
        // Prepare Role for the test case
1663
        $roleIdentifier = 'authorized_folder';
1664
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleIdentifier);
1665
        $locationLimitation = new LocationLimitation(
1666
            ['limitationValues' => [$authorizedFolder->contentInfo->mainLocationId]]
1667
        );
1668
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'read'));
1669
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'versionread'));
1670
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'manage_locations'));
1671
1672
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
1673
        $policyCreateStruct->addLimitation($locationLimitation);
1674
        $roleCreateStruct->addPolicy($policyCreateStruct);
1675
1676
        $roleDraft = $roleService->createRole($roleCreateStruct);
1677
        $roleService->publishRoleDraft($roleDraft);
1678
1679
        // Create a user with that Role
1680
        $user = $this->createCustomUserVersion1('Users', $roleIdentifier);
1681
        $repository->getPermissionResolver()->setCurrentUserReference($user);
1682
1683
        // Test copying Content to the authorized Location
1684
        $contentService->copyContent(
1685
            $authorizedFolder->contentInfo,
1686
            $locationService->newLocationCreateStruct(
1687
                $authorizedFolder->contentInfo->mainLocationId
1688
            )
1689
        );
1690
    }
1691
1692
    /**
1693
     * Test copying Content to the authorized Location (limited by policies).
1694
     */
1695
    public function testCopyContentToAuthorizedLocationWithSubtreeLimitation()
1696
    {
1697
        $repository = $this->getRepository();
1698
        $contentService = $repository->getContentService();
1699
        $locationService = $repository->getLocationService();
1700
        $roleService = $repository->getRoleService();
0 ignored issues
show
Unused Code introduced by
$roleService is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1701
1702
        // Create and publish folders for the test case
1703
        $folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']);
1704
        $contentService->publishVersion($folderDraft->versionInfo);
1705
        $authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']);
1706
        $authorizedFolder = $contentService->publishVersion($authorizedFolderDraft->versionInfo);
1707
1708
        // Prepare Role for the test case
1709
        $roleIdentifier = 'authorized_subree';
1710
        $subtreeLimitation = new SubtreeLimitation(
1711
            ['limitationValues' => ['/1/2']]
1712
        );
1713
        $policiesData = [
1714
            [
1715
                'module' => 'content',
1716
                'function' => 'read',
1717
                'limitations' => [$subtreeLimitation],
1718
            ],
1719
            [
1720
                'module' => 'content',
1721
                'function' => 'versionread',
1722
                'limitations' => [$subtreeLimitation],
1723
            ],
1724
            [
1725
                'module' => 'content',
1726
                'function' => 'create',
1727
                'limitations' => [$subtreeLimitation],
1728
            ],
1729
            [
1730
                'module' => 'content',
1731
                'function' => 'manage_locations',
1732
            ],
1733
        ];
1734
1735
        $this->createRoleWithPolicies($roleIdentifier, $policiesData);
1736
1737
        // Create a user with that Role
1738
        $user = $this->createCustomUserVersion1('Users', $roleIdentifier);
1739
        $repository->getPermissionResolver()->setCurrentUserReference($user);
1740
1741
        // Test copying Content to the authorized Location
1742
        $contentService->copyContent(
1743
            $authorizedFolder->contentInfo,
1744
            $locationService->newLocationCreateStruct(
1745
                $authorizedFolder->contentInfo->mainLocationId
1746
            )
1747
        );
1748
    }
1749
}
1750