Completed
Push — refactor-content-service-integ... ( 5a024e )
by
unknown
18:50
created

testLoadContentDraftsThrowsUnauthorizedExceptionWithUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
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
    /** @var \eZ\Publish\API\Repository\Values\User\User */
29
    private $administratorUser;
30
31
    /** @var \eZ\Publish\API\Repository\Values\User\User */
32
    private $anonymousUser;
33
34
    /** @var \eZ\Publish\API\Repository\Repository */
35
    private $repository;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
36
37
    /** @var \eZ\Publish\API\Repository\PermissionResolver */
38
    private $permissionResolver;
39
40
    /** @var \eZ\Publish\API\Repository\UserService */
41
    private $userService;
42
43
    /** @var \eZ\Publish\API\Repository\ContentService */
44
    private $contentService;
45
46
    public function setUp(): void
47
    {
48
        parent::setUp();
49
50
        $anonymousUserId = $this->generateId('user', 10);
51
        $administratorUserId = $this->generateId('user', 14);
52
53
        $this->repository = $this->getRepository();
54
        $this->permissionResolver = $this->repository->getPermissionResolver();
55
        $this->userService = $this->repository->getUserService();
56
        $this->contentService = $this->repository->getContentService();
57
58
        $this->administratorUser = $this->userService->loadUser($administratorUserId);
59
        $this->anonymousUser = $this->userService->loadUser($anonymousUserId);
60
    }
61
62
    /**
63
     * Test for the createContent() method.
64
     *
65
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
66
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
67
     */
68 View Code Duplication
    public function testCreateContentThrowsUnauthorizedException()
69
    {
70
        if ($this->isVersion4()) {
71
            $this->markTestSkipped('This test requires eZ Publish 5');
72
        }
73
74
        $repository = $this->getRepository();
75
76
        $anonymousUserId = $this->generateId('user', 10);
77
        /* BEGIN: Use Case */
78
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
79
        // demo installation
80
        // Load the user service
81
        $userService = $repository->getUserService();
82
83
        // Set anonymous user
84
        $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...
85
86
        $contentTypeService = $repository->getContentTypeService();
87
88
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
89
90
        $contentService = $repository->getContentService();
91
92
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
93
        $contentCreate->setField('name', 'Awesome Sindelfingen forum');
94
95
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
96
        $contentCreate->alwaysAvailable = true;
97
98
        $this->expectException(UnauthorizedException::class);
99
        $this->expectExceptionMessageRegExp('/\'create\' \'content\'/');
100
101
        $contentService->createContent($contentCreate);
102
        /* END: Use Case */
103
    }
104
105
    /**
106
     * Test for the createContent() method.
107
     *
108
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
109
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
110
     */
111
    public function testCreateContentThrowsUnauthorizedExceptionWithSecondParameter()
112
    {
113
        $repository = $this->getRepository();
114
115
        $anonymousUserId = $this->generateId('user', 10);
116
        /* BEGIN: Use Case */
117
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
118
        // demo installation
119
        // Load the user service
120
        $userService = $repository->getUserService();
121
122
        // Set anonymous user
123
        $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...
124
125
        $this->expectException(UnauthorizedException::class);
126
        $this->expectExceptionMessageRegExp('/\'create\' \'content\'/');
127
128
        $this->createContentDraftVersion1();
129
        /* END: Use Case */
130
    }
131
132
    /**
133
     * Test for the loadContentInfo() method.
134
     *
135
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
136
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
137
     */
138 View Code Duplication
    public function testLoadContentInfoThrowsUnauthorizedException()
139
    {
140
        $repository = $this->getRepository();
141
142
        $contentId = $this->generateId('object', 10);
143
        /* BEGIN: Use Case */
144
        $contentService = $repository->getContentService();
145
146
        $pseudoEditor = $this->createAnonymousWithEditorRole();
147
148
        // Set restricted editor user
149
        $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...
150
151
        $this->expectException(UnauthorizedException::class);
152
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
153
154
        // $contentId contains a content object ID not accessible for anonymous
155
        $contentService->loadContentInfo($contentId);
156
        /* END: Use Case */
157
    }
158
159
    /**
160
     * Test for the sudo() method.
161
     *
162
     * @see \eZ\Publish\API\Repository\Repository::sudo()
163
     * @depends testLoadContentInfoThrowsUnauthorizedException
164
     */
165 View Code Duplication
    public function testSudo()
166
    {
167
        $repository = $this->getRepository();
168
        $contentId = $this->generateId('object', 10);
169
        // Set restricted editor user
170
        $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...
171
172
        $contentInfo = $repository->sudo(function (Repository $repository) use ($contentId) {
173
            return $repository->getContentService()->loadContentInfo($contentId);
174
        });
175
176
        $this->assertInstanceOf(
177
            ContentInfo::class,
178
            $contentInfo
179
        );
180
    }
181
182
    /**
183
     * Test for the loadContentInfoList() method.
184
     *
185
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
186
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoList
187
     */
188
    public function testLoadContentInfoListSkipsUnauthorizedItems()
189
    {
190
        $repository = $this->getRepository();
191
        $contentId = $this->generateId('object', 10);
192
        $contentService = $repository->getContentService();
193
        $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...
194
195
        $list = $contentService->loadContentInfoList([$contentId]);
196
197
        $this->assertCount(0, $list);
198
    }
199
200
    /**
201
     * Test for the loadContentInfoByRemoteId() method.
202
     *
203
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
204
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
205
     */
206
    public function testLoadContentInfoByRemoteIdThrowsUnauthorizedException()
207
    {
208
        $repository = $this->getRepository();
209
210
        /* BEGIN: Use Case */
211
        // RemoteId of the "Anonymous User" in an eZ Publish demo installation
212
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
213
214
        $contentService = $repository->getContentService();
215
216
        $pseudoEditor = $this->createAnonymousWithEditorRole();
217
218
        // Set restricted editor user
219
        $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...
220
221
        $this->expectException(UnauthorizedException::class);
222
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
223
224
        $contentService->loadContentInfoByRemoteId($anonymousRemoteId);
225
        /* END: Use Case */
226
    }
227
228
    /**
229
     * Test for the loadVersionInfo() method.
230
     *
231
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
232
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
233
     */
234
    public function testLoadVersionInfoThrowsUnauthorizedException()
235
    {
236
        $repository = $this->getRepository();
237
238
        $anonymousUserId = $this->generateId('user', 10);
239
        /* BEGIN: Use Case */
240
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
241
        // demo installation
242
243
        $contentService = $repository->getContentService();
244
245
        // Load the ContentInfo for "Anonymous User"
246
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
247
248
        $pseudoEditor = $this->createAnonymousWithEditorRole();
249
250
        // Set restricted editor user
251
        $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...
252
253
        $this->expectException(UnauthorizedException::class);
254
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
255
256
        $contentService->loadVersionInfo($contentInfo);
257
        /* END: Use Case */
258
    }
259
260
    /**
261
     * Test for the loadVersionInfo() method.
262
     *
263
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
264
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
265
     */
266
    public function testLoadVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
267
    {
268
        $repository = $this->getRepository();
269
270
        $anonymousUserId = $this->generateId('user', 10);
271
        /* BEGIN: Use Case */
272
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
273
        // demo installation
274
275
        $contentService = $repository->getContentService();
276
277
        // Load the ContentInfo for "Anonymous User"
278
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
279
280
        $pseudoEditor = $this->createAnonymousWithEditorRole();
281
282
        // Set restricted editor user
283
        $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...
284
285
        $this->expectException(UnauthorizedException::class);
286
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
287
288
        $contentService->loadVersionInfo($contentInfo, 2);
289
        /* END: Use Case */
290
    }
291
292
    /**
293
     * Test for the loadVersionInfoById() method.
294
     *
295
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
296
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
297
     */
298 View Code Duplication
    public function testLoadVersionInfoByIdThrowsUnauthorizedException()
299
    {
300
        $repository = $this->getRepository();
301
302
        $anonymousUserId = $this->generateId('user', 10);
303
        /* BEGIN: Use Case */
304
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
305
        // demo installation
306
307
        $contentService = $repository->getContentService();
308
309
        $pseudoEditor = $this->createAnonymousWithEditorRole();
310
311
        // Set restricted editor user
312
        $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...
313
314
        $this->expectException(UnauthorizedException::class);
315
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
316
317
        $contentService->loadVersionInfoById($anonymousUserId);
318
        /* END: Use Case */
319
    }
320
321
    /**
322
     * Test for the loadVersionInfoById() method.
323
     *
324
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
325
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
326
     */
327 View Code Duplication
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionWithSecondParameter()
328
    {
329
        $repository = $this->getRepository();
330
331
        $anonymousUserId = $this->generateId('user', 10);
332
        /* BEGIN: Use Case */
333
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
334
        // demo installation
335
336
        $contentService = $repository->getContentService();
337
338
        $pseudoEditor = $this->createAnonymousWithEditorRole();
339
340
        // Set restricted editor user
341
        $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...
342
343
        $this->expectException(UnauthorizedException::class);
344
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
345
346
        $contentService->loadVersionInfoById($anonymousUserId, 2);
347
        /* END: Use Case */
348
    }
349
350
    /**
351
     * Test for the loadVersionInfoById() method.
352
     *
353
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
354
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
355
     */
356
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionForFirstDraft()
357
    {
358
        $repository = $this->getRepository();
359
360
        $contentService = $repository->getContentService();
361
362
        $anonymousUserId = $this->generateId('user', 10);
363
        /* BEGIN: Use Case */
364
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
365
        // demo installation
366
        $contentDraft = $this->createContentDraftVersion1();
367
368
        // Load the user service
369
        $userService = $repository->getUserService();
370
371
        // Set anonymous user
372
        $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...
373
374
        $this->expectException(UnauthorizedException::class);
375
        // content versionread policy is needed because it is a draft
376
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
377
378
        $contentService->loadVersionInfoById(
379
            $contentDraft->id,
380
            $contentDraft->contentInfo->currentVersionNo
381
        );
382
        /* END: Use Case */
383
    }
384
385
    /**
386
     * Test for the loadContentByContentInfo() method.
387
     *
388
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
389
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
390
     */
391
    public function testLoadContentByContentInfoThrowsUnauthorizedException()
392
    {
393
        $repository = $this->getRepository();
394
395
        $anonymousUserId = $this->generateId('user', 10);
396
        /* BEGIN: Use Case */
397
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
398
        // demo installation
399
400
        $contentService = $repository->getContentService();
401
402
        // Load the ContentInfo for "Anonymous User"
403
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
404
405
        $pseudoEditor = $this->createAnonymousWithEditorRole();
406
407
        // Set restricted editor user
408
        $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...
409
410
        $this->expectException(UnauthorizedException::class);
411
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
412
413
        $contentService->loadContentByContentInfo($contentInfo);
414
        /* END: Use Case */
415
    }
416
417
    /**
418
     * Test for the loadContentByContentInfo() method.
419
     *
420
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
421
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithLanguageParameters
422
     */
423
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithSecondParameter()
424
    {
425
        $repository = $this->getRepository();
426
427
        $anonymousUserId = $this->generateId('user', 10);
428
        /* BEGIN: Use Case */
429
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
430
        // demo installation
431
432
        $contentService = $repository->getContentService();
433
434
        // Load the ContentInfo for "Anonymous User"
435
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
436
437
        $pseudoEditor = $this->createAnonymousWithEditorRole();
438
439
        // Set restricted editor user
440
        $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...
441
442
        $this->expectException(UnauthorizedException::class);
443
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
444
445
        $contentService->loadContentByContentInfo($contentInfo, ['eng-US']);
446
        /* END: Use Case */
447
    }
448
449
    /**
450
     * Test for the loadContentByContentInfo() method.
451
     *
452
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
453
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
454
     */
455
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithThirdParameter()
456
    {
457
        $repository = $this->getRepository();
458
459
        $anonymousUserId = $this->generateId('user', 10);
460
        /* BEGIN: Use Case */
461
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
462
        // demo installation
463
464
        $contentService = $repository->getContentService();
465
466
        // Load the ContentInfo for "Anonymous User"
467
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
468
469
        $pseudoEditor = $this->createAnonymousWithEditorRole();
470
471
        // Set restricted editor user
472
        $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...
473
474
        $this->expectException(UnauthorizedException::class);
475
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
476
477
        $contentService->loadContentByContentInfo($contentInfo, ['eng-US'], 2);
478
        /* END: Use Case */
479
    }
480
481
    /**
482
     * Test for the loadContentByVersionInfo() method.
483
     *
484
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
485
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
486
     */
487
    public function testLoadContentByVersionInfoThrowsUnauthorizedException()
488
    {
489
        $repository = $this->getRepository();
490
491
        $anonymousUserId = $this->generateId('user', 10);
492
        /* BEGIN: Use Case */
493
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
494
        // demo installation
495
496
        $contentService = $repository->getContentService();
497
498
        // Load the ContentInfo for "Anonymous User"
499
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
500
501
        // Load the current VersionInfo
502
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
503
504
        $pseudoEditor = $this->createAnonymousWithEditorRole();
505
506
        // Set restricted editor user
507
        $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...
508
509
        $this->expectException(UnauthorizedException::class);
510
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
511
512
        $contentService->loadContentByVersionInfo($versionInfo);
513
        /* END: Use Case */
514
    }
515
516
    /**
517
     * Test for the loadContentByVersionInfo() method.
518
     *
519
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
520
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfoWithSecondParameter
521
     */
522
    public function testLoadContentByVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
523
    {
524
        $repository = $this->getRepository();
525
526
        $anonymousUserId = $this->generateId('user', 10);
527
        /* BEGIN: Use Case */
528
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
529
        // demo installation
530
531
        $contentService = $repository->getContentService();
532
533
        // Load the ContentInfo for "Anonymous User"
534
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
535
536
        // Load the current VersionInfo
537
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
538
539
        $pseudoEditor = $this->createAnonymousWithEditorRole();
540
541
        // Set restricted editor user
542
        $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...
543
544
        $this->expectException(UnauthorizedException::class);
545
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
546
547
        $contentService->loadContentByVersionInfo($versionInfo, ['eng-US']);
548
        /* END: Use Case */
549
    }
550
551
    /**
552
     * Test for the loadContent() method.
553
     *
554
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
555
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
556
     */
557 View Code Duplication
    public function testLoadContentThrowsUnauthorizedException()
558
    {
559
        $repository = $this->getRepository();
560
561
        $anonymousUserId = $this->generateId('user', 10);
562
        /* BEGIN: Use Case */
563
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
564
        // demo installation
565
566
        $contentService = $repository->getContentService();
567
568
        $pseudoEditor = $this->createAnonymousWithEditorRole();
569
570
        // Set restricted editor user
571
        $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...
572
573
        $this->expectException(UnauthorizedException::class);
574
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
575
576
        $contentService->loadContent($anonymousUserId);
577
        /* END: Use Case */
578
    }
579
580
    /**
581
     * Test for the loadContent() method.
582
     *
583
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
584
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
585
     */
586 View Code Duplication
    public function testLoadContentThrowsUnauthorizedExceptionWithSecondParameter()
587
    {
588
        $repository = $this->getRepository();
589
590
        $anonymousUserId = $this->generateId('user', 10);
591
        /* BEGIN: Use Case */
592
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
593
        // demo installation
594
595
        $contentService = $repository->getContentService();
596
597
        $pseudoEditor = $this->createAnonymousWithEditorRole();
598
599
        // Set restricted editor user
600
        $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...
601
602
        $this->expectException(UnauthorizedException::class);
603
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
604
605
        $contentService->loadContent($anonymousUserId, ['eng-US']);
606
        /* END: Use Case */
607
    }
608
609
    /**
610
     * Test for the loadContent() method.
611
     *
612
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
613
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
614
     */
615 View Code Duplication
    public function testLoadContentThrowsUnauthorizedExceptionWithThirdParameter()
616
    {
617
        $repository = $this->getRepository();
618
619
        $anonymousUserId = $this->generateId('user', 10);
620
        /* BEGIN: Use Case */
621
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
622
        // demo installation
623
624
        $contentService = $repository->getContentService();
625
626
        $pseudoEditor = $this->createAnonymousWithEditorRole();
627
628
        // Set restricted editor user
629
        $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...
630
631
        $this->expectException(UnauthorizedException::class);
632
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
633
634
        $contentService->loadContent($anonymousUserId, ['eng-US'], 2);
635
        /* END: Use Case */
636
    }
637
638
    /**
639
     * Test for the loadContent() method on a draft.
640
     *
641
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
642
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
643
     */
644 View Code Duplication
    public function testLoadContentThrowsUnauthorizedExceptionOnDrafts()
645
    {
646
        /** @var $repository \eZ\Publish\API\Repository\Repository */
647
        $repository = $this->getRepository();
648
649
        $anonymousUserId = $this->generateId('user', 10);
650
        /* BEGIN: Use Case */
651
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
652
        // demo installation
653
        $user = $this->createUserVersion1();
654
655
        // Set new editor as a content owner
656
        $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...
657
658
        // Create draft with this user
659
        $draft = $this->createContentDraftVersion1(2, 'folder');
660
661
        // Load anonymous user
662
        $userService = $repository->getUserService();
663
        $user = $userService->loadUser($anonymousUserId);
664
        $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...
665
666
        // Try to load the draft with anonymous user to make sure access won't be allowed by throwing an exception
667
        $contentService = $repository->getContentService();
668
669
        $this->expectException(UnauthorizedException::class);
670
        // content versionread policy is needed because it is a draft
671
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
672
673
        $contentService->loadContent($draft->id);
674
        /* END: Use Case */
675
    }
676
677
    /**
678
     * Test for the ContentService::loadContent() method on an archive.
679
     *
680
     * This test the version permission on loading archived versions
681
     *
682
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
683
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
684
     */
685
    public function testLoadContentThrowsUnauthorizedExceptionsOnArchives()
686
    {
687
        /** @var $repository \eZ\Publish\API\Repository\Repository */
688
        $repository = $this->getRepository();
689
690
        $anonymousUserId = $this->generateId('user', 10);
691
        /* BEGIN: Use Case */
692
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
693
        // demo installation
694
        // get necessary services
695
        $contentTypeService = $repository->getContentTypeService();
696
        $contentService = $repository->getContentService();
697
        $locationSercice = $repository->getLocationService();
698
699
        // set admin as current user
700
        $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...
701
702
        // create folder
703
        $newStruct = $contentService->newContentCreateStruct(
704
            $contentTypeService->loadContentTypeByIdentifier('folder'),
705
            'eng-US'
706
        );
707
        $newStruct->setField('name', 'Test Folder');
708
        $draft = $contentService->createContent(
709
            $newStruct,
710
            [$locationSercice->newLocationCreateStruct(2)]
711
        );
712
        $object = $contentService->publishVersion($draft->versionInfo);
713
714
        // update folder to make an archived version
715
        $updateStruct = $contentService->newContentUpdateStruct();
716
        $updateStruct->setField('name', 'Test Folder Updated');
717
        $draftUpdated = $contentService->updateContent(
718
            $contentService->createContentDraft($object->contentInfo)->versionInfo,
719
            $updateStruct
720
        );
721
        $objectUpdated = $contentService->publishVersion($draftUpdated->versionInfo);
722
723
        // set an anonymous as current user
724
        $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...
725
726
        $this->expectException(UnauthorizedException::class);
727
        // content versionread policy is needed because it is a draft
728
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
729
730
        $contentService->loadContent($objectUpdated->id, null, 1);
731
        /* END: Use Case */
732
    }
733
734
    /**
735
     * Test for the loadContentByRemoteId() method.
736
     *
737
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
738
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
739
     */
740
    public function testLoadContentByRemoteIdThrowsUnauthorizedException()
741
    {
742
        $repository = $this->getRepository();
743
744
        /* BEGIN: Use Case */
745
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
746
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
747
748
        $contentService = $repository->getContentService();
749
750
        $pseudoEditor = $this->createAnonymousWithEditorRole();
751
752
        // Set restricted editor user
753
        $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...
754
755
        $this->expectException(UnauthorizedException::class);
756
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
757
758
        $contentService->loadContentByRemoteId($anonymousRemoteId);
759
        /* END: Use Case */
760
    }
761
762
    /**
763
     * Test for the loadContentByRemoteId() method.
764
     *
765
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
766
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithSecondParameter
767
     */
768 View Code Duplication
    public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithSecondParameter()
769
    {
770
        $repository = $this->getRepository();
771
772
        /* BEGIN: Use Case */
773
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
774
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
775
776
        $contentService = $repository->getContentService();
777
778
        $pseudoEditor = $this->createAnonymousWithEditorRole();
779
780
        // Set restricted editor user
781
        $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...
782
783
        $this->expectException(UnauthorizedException::class);
784
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
785
786
        $contentService->loadContentByRemoteId($anonymousRemoteId, ['eng-US']);
787
        /* END: Use Case */
788
    }
789
790
    /**
791
     * Test for the loadContentByRemoteId() method.
792
     *
793
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
794
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
795
     */
796 View Code Duplication
    public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithThirdParameter()
797
    {
798
        $repository = $this->getRepository();
799
800
        /* BEGIN: Use Case */
801
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
802
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
803
804
        $contentService = $repository->getContentService();
805
806
        $pseudoEditor = $this->createAnonymousWithEditorRole();
807
808
        // Set restricted editor user
809
        $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...
810
811
        $this->expectException(UnauthorizedException::class);
812
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
813
814
        $contentService->loadContentByRemoteId($anonymousRemoteId, ['eng-US'], 2);
815
        /* END: Use Case */
816
    }
817
818
    /**
819
     * Test for the updateContentMetadata() method.
820
     *
821
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
822
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
823
     */
824
    public function testUpdateContentMetadataThrowsUnauthorizedException()
825
    {
826
        $repository = $this->getRepository();
827
828
        $contentService = $repository->getContentService();
829
830
        $anonymousUserId = $this->generateId('user', 10);
831
        /* BEGIN: Use Case */
832
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
833
        // demo installation
834
        $content = $this->createContentVersion1();
835
836
        // Get ContentInfo instance.
837
        $contentInfo = $content->contentInfo;
838
839
        // Load the user service
840
        $userService = $repository->getUserService();
841
842
        // Set anonymous user
843
        $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...
844
845
        // Creates a metadata update struct
846
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
847
848
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
849
        $metadataUpdate->mainLanguageCode = 'eng-US';
850
        $metadataUpdate->alwaysAvailable = false;
851
        $metadataUpdate->publishedDate = $this->createDateTime();
852
        $metadataUpdate->modificationDate = $this->createDateTime();
853
854
        $this->expectException(UnauthorizedException::class);
855
        $this->expectExceptionMessageRegExp('/\'edit\' \'content\'/');
856
857
        $contentService->updateContentMetadata(
858
            $contentInfo,
859
            $metadataUpdate
860
        );
861
        /* END: Use Case */
862
    }
863
864
    /**
865
     * Test for the deleteContent() method.
866
     *
867
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
868
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
869
     */
870
    public function testDeleteContentThrowsUnauthorizedException()
871
    {
872
        $repository = $this->getRepository();
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
        $contentVersion2 = $this->createContentVersion2();
880
881
        // Get ContentInfo instance
882
        $contentInfo = $contentVersion2->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('/\'remove\' \'content\'/');
892
893
        $contentService->deleteContent($contentInfo);
894
        /* END: Use Case */
895
    }
896
897
    /**
898
     * Test for the createContentDraft() method.
899
     *
900
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
901
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
902
     */
903
    public function testCreateContentDraftThrowsUnauthorizedException()
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 instance
916
        $contentInfo = $content->contentInfo;
917
918
        // Load the user service
919
        $userService = $repository->getUserService();
920
921
        // Set anonymous user
922
        $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...
923
924
        $this->expectException(UnauthorizedException::class);
925
        $this->expectExceptionMessageRegExp('/\'edit\' \'content\'/');
926
927
        $contentService->createContentDraft($contentInfo);
928
        /* END: Use Case */
929
    }
930
931
    /**
932
     * Test for the createContentDraft() method.
933
     *
934
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
935
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithSecondParameter
936
     */
937 View Code Duplication
    public function testCreateContentDraftThrowsUnauthorizedExceptionWithSecondParameter()
938
    {
939
        $repository = $this->getRepository();
940
941
        $contentService = $repository->getContentService();
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
        $content = $this->createContentVersion1();
948
949
        // Get ContentInfo and VersionInfo instances
950
        $contentInfo = $content->contentInfo;
951
        $versionInfo = $content->getVersionInfo();
952
953
        // Load the user service
954
        $userService = $repository->getUserService();
955
956
        // Set anonymous user
957
        $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...
958
959
        $this->expectException(UnauthorizedException::class);
960
        $this->expectExceptionMessageRegExp('/\'edit\' \'content\'/');
961
962
        $contentService->createContentDraft($contentInfo, $versionInfo);
963
        /* END: Use Case */
964
    }
965
966
    /**
967
     * Test for the loadContentDrafts() method.
968
     *
969
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
970
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
971
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
972
     */
973
    public function testLoadContentDraftsThrowsUnauthorizedException()
974
    {
975
        /* BEGIN: Use Case */
976
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
977
978
        $this->expectException(UnauthorizedException::class);
979
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
980
981
        $this->contentService->loadContentDrafts();
982
        /* END: Use Case */
983
    }
984
985
    /**
986
     * Test for the loadContentDrafts() method.
987
     *
988
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
989
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
990
     */
991
    public function testLoadContentDraftsThrowsUnauthorizedExceptionWithUser()
992
    {
993
        $this->permissionResolver->setCurrentUserReference($this->anonymousUser);
994
995
        $this->expectException(UnauthorizedException::class);
996
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
997
998
        $this->contentService->loadContentDrafts($this->administratorUser);
999
        /* END: Use Case */
1000
    }
1001
1002
    /**
1003
     * Test for the updateContent() method.
1004
     *
1005
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
1006
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
1007
     */
1008 View Code Duplication
    public function testUpdateContentThrowsUnauthorizedException()
1009
    {
1010
        $repository = $this->getRepository();
1011
        $contentService = $repository->getContentService();
1012
1013
        $anonymousUserId = $this->generateId('user', 10);
1014
        /* BEGIN: Use Case */
1015
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1016
        // demo installation
1017
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1018
        // demo installation
1019
        $draftVersion2 = $this->createContentDraftVersion2();
1020
1021
        // Get VersionInfo instance
1022
        $versionInfo = $draftVersion2->getVersionInfo();
1023
1024
        // Load the user service
1025
        $userService = $repository->getUserService();
1026
1027
        // Set anonymous user
1028
        $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...
1029
1030
        // Create an update struct and modify some fields
1031
        $contentUpdate = $contentService->newContentUpdateStruct();
1032
        $contentUpdate->setField('name', 'An awesome² story about ezp.');
1033
        $contentUpdate->setField('name', 'An awesome²³ story about ezp.', 'eng-GB');
1034
1035
        $contentUpdate->initialLanguageCode = 'eng-US';
1036
1037
        $this->expectException(UnauthorizedException::class);
1038
        /* TODO - the `content/edit` policy should be probably needed */
1039
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1040
1041
        $contentService->updateContent($versionInfo, $contentUpdate);
1042
        /* END: Use Case */
1043
    }
1044
1045
    /**
1046
     * Test for the publishVersion() method.
1047
     *
1048
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1049
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1050
     */
1051
    public function testPublishVersionThrowsUnauthorizedException()
1052
    {
1053
        $repository = $this->getRepository();
1054
        $contentService = $repository->getContentService();
1055
1056
        $anonymousUserId = $this->generateId('user', 10);
1057
        /* BEGIN: Use Case */
1058
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1059
        // demo installation
1060
        $draft = $this->createContentDraftVersion1();
1061
1062
        // Load the user service
1063
        $userService = $repository->getUserService();
1064
1065
        // Set anonymous user
1066
        $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...
1067
1068
        $this->expectException(UnauthorizedException::class);
1069
        $this->expectExceptionMessageRegExp('/\'publish\' \'content\'/');
1070
1071
        $contentService->publishVersion($draft->getVersionInfo());
1072
        /* END: Use Case */
1073
    }
1074
1075
    /**
1076
     * Test for the deleteVersion() method.
1077
     *
1078
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
1079
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteVersion
1080
     */
1081
    public function testDeleteVersionThrowsUnauthorizedException()
1082
    {
1083
        $repository = $this->getRepository();
1084
        $contentService = $repository->getContentService();
1085
1086
        $anonymousUserId = $this->generateId('user', 10);
1087
        /* BEGIN: Use Case */
1088
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1089
        // demo installation
1090
        $draft = $this->createContentDraftVersion1();
1091
1092
        // Load the user service
1093
        $userService = $repository->getUserService();
1094
1095
        // Set anonymous user
1096
        $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...
1097
1098
        $this->expectException(UnauthorizedException::class);
1099
        $this->expectExceptionMessageRegExp('/\'versionremove\' \'content\'/');
1100
1101
        $contentService->deleteVersion($draft->getVersionInfo());
1102
        /* END: Use Case */
1103
    }
1104
1105
    /**
1106
     * Test for the loadVersions() method.
1107
     *
1108
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
1109
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
1110
     */
1111
    public function testLoadVersionsThrowsUnauthorizedException()
1112
    {
1113
        $repository = $this->getRepository();
1114
1115
        $contentService = $repository->getContentService();
1116
1117
        $anonymousUserId = $this->generateId('user', 10);
1118
        /* BEGIN: Use Case */
1119
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1120
        // demo installation
1121
        $contentVersion2 = $this->createContentVersion2();
1122
1123
        // Get ContentInfo instance of version 2
1124
        $contentInfo = $contentVersion2->contentInfo;
1125
1126
        // Load the user service
1127
        $userService = $repository->getUserService();
1128
1129
        // Set anonymous user
1130
        $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...
1131
1132
        $this->expectException(UnauthorizedException::class);
1133
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1134
1135
        $contentService->loadVersions($contentInfo);
1136
        /* END: Use Case */
1137
    }
1138
1139
    /**
1140
     * Test for the copyContent() method.
1141
     *
1142
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
1143
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
1144
     */
1145 View Code Duplication
    public function testCopyContentThrowsUnauthorizedException()
1146
    {
1147
        $parentLocationId = $this->generateId('location', 52);
1148
1149
        $repository = $this->getRepository();
1150
1151
        $contentService = $repository->getContentService();
1152
        $locationService = $repository->getLocationService();
1153
1154
        $anonymousUserId = $this->generateId('user', 10);
1155
        /* BEGIN: Use Case */
1156
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1157
        // demo installation
1158
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
1159
1160
        // Get ContentInfo instance of version 2
1161
        $contentInfo = $contentVersion2->contentInfo;
1162
1163
        // Load the user service
1164
        $userService = $repository->getUserService();
1165
1166
        // Set anonymous user
1167
        $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...
1168
1169
        // Configure new target location
1170
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
1171
1172
        $targetLocationCreate->priority = 42;
1173
        $targetLocationCreate->hidden = true;
1174
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
1175
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
1176
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
1177
1178
        $this->expectException(UnauthorizedException::class);
1179
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
1180
1181
        $contentService->copyContent(
1182
            $contentInfo,
1183
            $targetLocationCreate
1184
        );
1185
        /* END: Use Case */
1186
    }
1187
1188
    /**
1189
     * Test for the copyContent() method.
1190
     *
1191
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
1192
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContentWithGivenVersion
1193
     */
1194 View Code Duplication
    public function testCopyContentThrowsUnauthorizedExceptionWithGivenVersion()
1195
    {
1196
        $parentLocationId = $this->generateId('location', 52);
1197
1198
        $repository = $this->getRepository();
1199
1200
        $contentService = $repository->getContentService();
1201
        $locationService = $repository->getLocationService();
1202
1203
        $anonymousUserId = $this->generateId('user', 10);
1204
        /* BEGIN: Use Case */
1205
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1206
        // demo installation
1207
        $contentVersion2 = $this->createContentVersion2();
1208
1209
        // Load the user service
1210
        $userService = $repository->getUserService();
1211
1212
        // Set anonymous user
1213
        $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...
1214
1215
        // Configure new target location
1216
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
1217
1218
        $targetLocationCreate->priority = 42;
1219
        $targetLocationCreate->hidden = true;
1220
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
1221
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
1222
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
1223
1224
        $this->expectException(UnauthorizedException::class);
1225
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1226
1227
        $contentService->copyContent(
1228
            $contentVersion2->contentInfo,
1229
            $targetLocationCreate,
1230
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
1231
        );
1232
        /* END: Use Case */
1233
    }
1234
1235
    /**
1236
     * Test for the loadRelations() method.
1237
     *
1238
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1239
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
1240
     */
1241
    public function testLoadRelationsThrowsUnauthorizedException()
1242
    {
1243
        $repository = $this->getRepository();
1244
1245
        $contentService = $repository->getContentService();
1246
1247
        /* BEGIN: Use Case */
1248
        $user = $this->createMediaUserVersion1();
1249
1250
        // Remote id of the "Setup" page of a eZ Publish demo installation.
1251
        $setupRemoteId = '241d538ce310074e602f29f49e44e938';
1252
1253
        $versionInfo = $contentService->loadVersionInfo(
1254
            $contentService->loadContentInfoByRemoteId(
1255
                $setupRemoteId
1256
            )
1257
        );
1258
1259
        // Set media editor as current user
1260
        $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...
1261
1262
        $this->expectException(UnauthorizedException::class);
1263
        $this->expectExceptionMessageRegExp('/\'read\' \'content\'/');
1264
1265
        $contentService->loadRelations($versionInfo);
1266
        /* END: Use Case */
1267
    }
1268
1269
    /**
1270
     * Test for the loadRelations() method.
1271
     *
1272
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1273
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
1274
     */
1275
    public function testLoadRelationsForDraftVersionThrowsUnauthorizedException()
1276
    {
1277
        $repository = $this->getRepository();
1278
1279
        $contentService = $repository->getContentService();
1280
1281
        $anonymousUserId = $this->generateId('user', 10);
1282
        /* BEGIN: Use Case */
1283
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1284
        // demo installation
1285
        $draft = $this->createContentDraftVersion1();
1286
1287
        // Load the user service
1288
        $userService = $repository->getUserService();
1289
1290
        // Set anonymous user
1291
        $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...
1292
1293
        $this->expectException(UnauthorizedException::class);
1294
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1295
1296
        $contentService->loadRelations($draft->versionInfo);
1297
        /* END: Use Case */
1298
    }
1299
1300
    /**
1301
     * Test for the loadReverseRelations() method.
1302
     *
1303
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
1304
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
1305
     */
1306
    public function testLoadReverseRelationsThrowsUnauthorizedException()
1307
    {
1308
        $repository = $this->getRepository();
1309
1310
        $contentService = $repository->getContentService();
1311
1312
        /* BEGIN: Use Case */
1313
        $user = $this->createMediaUserVersion1();
1314
1315
        // Remote id of the "Media" page of a eZ Publish demo installation.
1316
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1317
1318
        $contentInfo = $contentService->loadContentInfoByRemoteId(
1319
            $mediaRemoteId
1320
        );
1321
1322
        // Set media editor as current user
1323
        $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...
1324
1325
        $this->expectException(UnauthorizedException::class);
1326
        $this->expectExceptionMessageRegExp('/\'reverserelatedlist\' \'content\'/');
1327
1328
        $contentService->loadReverseRelations($contentInfo);
1329
        /* END: Use Case */
1330
    }
1331
1332
    /**
1333
     * Test for the addRelation() method.
1334
     *
1335
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
1336
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
1337
     */
1338 View Code Duplication
    public function testAddRelationThrowsUnauthorizedException()
1339
    {
1340
        $repository = $this->getRepository();
1341
1342
        $contentService = $repository->getContentService();
1343
1344
        $anonymousUserId = $this->generateId('user', 10);
1345
        /* BEGIN: Use Case */
1346
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1347
        // demo installation
1348
        // Remote id of the "Media" page of a eZ Publish demo installation.
1349
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1350
1351
        $draft = $this->createContentDraftVersion1();
1352
1353
        // Get the draft's version info
1354
        $versionInfo = $draft->getVersionInfo();
1355
1356
        // Load other content object
1357
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1358
1359
        // Load the user service
1360
        $userService = $repository->getUserService();
1361
1362
        // Set anonymous user
1363
        $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...
1364
1365
        $this->expectException(UnauthorizedException::class);
1366
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1367
1368
        $contentService->addRelation(
1369
            $versionInfo,
1370
            $media
1371
        );
1372
        /* END: Use Case */
1373
    }
1374
1375
    /**
1376
     * Test for the deleteRelation() method.
1377
     *
1378
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
1379
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
1380
     */
1381
    public function testDeleteRelationThrowsUnauthorizedException()
1382
    {
1383
        $repository = $this->getRepository();
1384
1385
        $contentService = $repository->getContentService();
1386
1387
        $anonymousUserId = $this->generateId('user', 10);
1388
        /* BEGIN: Use Case */
1389
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1390
        // demo installation
1391
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
1392
        // demo installation.
1393
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1394
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
1395
1396
        $draft = $this->createContentDraftVersion1();
1397
1398
        // Get the draft's version info
1399
        $versionInfo = $draft->getVersionInfo();
1400
1401
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1402
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
1403
1404
        // Establish some relations
1405
        $contentService->addRelation($draft->getVersionInfo(), $media);
1406
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
1407
1408
        // Load the user service
1409
        $userService = $repository->getUserService();
1410
1411
        // Set anonymous user
1412
        $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...
1413
1414
        $this->expectException(UnauthorizedException::class);
1415
        $this->expectExceptionMessageRegExp('/\'versionread\' \'content\'/');
1416
1417
        $contentService->deleteRelation($versionInfo, $media);
1418
        /* END: Use Case */
1419
    }
1420
1421
    /**
1422
     * Creates a pseudo editor with a limitation to objects in the "Media/Images"
1423
     * subtree.
1424
     *
1425
     * @return \eZ\Publish\API\Repository\Values\User\User
1426
     */
1427 View Code Duplication
    private function createAnonymousWithEditorRole()
1428
    {
1429
        $repository = $this->getRepository();
1430
1431
        $anonymousUserId = $this->generateId('user', 10);
1432
        /* BEGIN: Use Case */
1433
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1434
        // demo installation
1435
        $roleService = $repository->getRoleService();
1436
        $userService = $repository->getUserService();
1437
1438
        $user = $userService->loadUser($anonymousUserId);
1439
        $role = $roleService->loadRoleByIdentifier('Editor');
1440
1441
        // Assign "Editor" role with limitation to "Media/Images"
1442
        $roleService->assignRoleToUser(
1443
            $role,
1444
            $user,
1445
            new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation(
1446
                [
1447
                    'limitationValues' => ['/1/43/51/'],
1448
                ]
1449
            )
1450
        );
1451
1452
        $pseudoEditor = $userService->loadUser($user->id);
1453
        /* END: Inline */
1454
1455
        return $pseudoEditor;
1456
    }
1457
1458
    /**
1459
     * Test that for an user that doesn't have access (read permissions) to an
1460
     * related object, executing loadRelations() would not throw any exception,
1461
     * only that the non-readable related object(s) won't be loaded.
1462
     *
1463
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1464
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
1465
     */
1466
    public function testLoadRelationsWithUnauthorizedRelations()
1467
    {
1468
        $repository = $this->getRepository();
1469
1470
        $anonymousUserId = $this->generateId('user', 10);
1471
        /* BEGIN: Use Case */
1472
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1473
        // demo installation
1474
        $mainLanguage = 'eng-GB';
1475
1476
        $contentService = $repository->getContentService();
1477
        $contenTypeService = $repository->getContentTypeService();
1478
        $locationService = $repository->getLocationService();
1479
        $sectionService = $repository->getSectionService();
1480
        $userService = $repository->getUserService();
1481
1482
        // set the current user as admin to create the environment to test
1483
        $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...
1484
1485
        // create section
1486
        // since anonymous users have their read permissions to specific sections
1487
        // the created section will be non-readable to them
1488
        $sectionCreate = $sectionService->newSectionCreateStruct();
1489
        $sectionCreate->identifier = 'private';
1490
        $sectionCreate->name = 'Private Section';
1491
        $section = $sectionService->createSection($sectionCreate);
1492
1493
        // create objects for testing
1494
        // here we will create 4 objects which 2 will be readable by an anonymous
1495
        // user, and the other 2 wont these last 2 will go to a private section
1496
        // where anonymous can't read, just like:
1497
        // readable object 1 -> /Main Folder
1498
        // readable object 2 -> /Main Folder/Available Folder
1499
        // non-readable object 1 -> /Restricted Folder
1500
        // non-readable object 2 -> /Restricted Folder/Unavailable Folder
1501
        //
1502
        // here is created - readable object 1 -> /Main Folder
1503
        $mainFolderCreate = $contentService->newContentCreateStruct(
1504
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1505
            $mainLanguage
1506
        );
1507
        $mainFolderCreate->setField('name', 'Main Folder');
1508
        $mainFolder = $contentService->publishVersion(
1509
            $contentService->createContent(
1510
                $mainFolderCreate,
1511
                [$locationService->newLocationCreateStruct(2)]
1512
            )->versionInfo
1513
        );
1514
1515
        // here is created readable object 2 -> /Main Folder/Available Folder
1516
        $availableFolderCreate = $contentService->newContentCreateStruct(
1517
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1518
            $mainLanguage
1519
        );
1520
        $availableFolderCreate->setField('name', 'Avaliable Folder');
1521
        $availableFolder = $contentService->publishVersion(
1522
            $contentService->createContent(
1523
                $availableFolderCreate,
1524
                [$locationService->newLocationCreateStruct($mainFolder->contentInfo->mainLocationId)]
1525
            )->versionInfo
1526
        );
1527
1528
        // here is created the non-readable object 1 -> /Restricted Folder
1529
        $restrictedFolderCreate = $contentService->newContentCreateStruct(
1530
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1531
            $mainLanguage
1532
        );
1533
        $restrictedFolderCreate->setField('name', 'Restricted Folder');
1534
        $restrictedFolderCreate->sectionId = $section->id;
1535
        $restrictedFolder = $contentService->publishVersion(
1536
            $contentService->createContent(
1537
                $restrictedFolderCreate,
1538
                [$locationService->newLocationCreateStruct(2)]
1539
            )->versionInfo
1540
        );
1541
1542
        // here is created non-readable object 2 -> /Restricted Folder/Unavailable Folder
1543
        $unavailableFolderCreate = $contentService->newContentCreateStruct(
1544
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1545
            $mainLanguage
1546
        );
1547
        $unavailableFolderCreate->setField('name', 'Unavailable Folder');
1548
        $unavailableFolder = $contentService->publishVersion(
1549
            $contentService->createContent(
1550
                $unavailableFolderCreate,
1551
                [$locationService->newLocationCreateStruct($restrictedFolder->contentInfo->mainLocationId)]
1552
            )->versionInfo
1553
        );
1554
1555
        // this will be our test object, which will have all the relations (as source)
1556
        // and it is readable by the anonymous user
1557
        $testFolderCreate = $contentService->newContentCreateStruct(
1558
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1559
            $mainLanguage
1560
        );
1561
        $testFolderCreate->setField('name', 'Test Folder');
1562
        $testFolderDraft = $contentService->createContent(
1563
            $testFolderCreate,
1564
            [$locationService->newLocationCreateStruct(2)]
1565
        )->versionInfo;
1566
1567
        // add relations to test folder (as source)
1568
        // the first 2 will be read by the user
1569
        // and the other 2 wont
1570
        //
1571
        // create relation from Test Folder to Main Folder
1572
        $mainRelation = $contentService->addRelation(
1573
            $testFolderDraft,
1574
            $mainFolder->getVersionInfo()->getContentInfo()
1575
        );
1576
        // create relation from Test Folder to Available Folder
1577
        $availableRelation = $contentService->addRelation(
1578
            $testFolderDraft,
1579
            $availableFolder->getVersionInfo()->getContentInfo()
1580
        );
1581
        // create relation from Test Folder to Restricted Folder
1582
        $contentService->addRelation(
1583
            $testFolderDraft,
1584
            $restrictedFolder->getVersionInfo()->getContentInfo()
1585
        );
1586
        //create relation from Test Folder to Unavailable Folder
1587
        $contentService->addRelation(
1588
            $testFolderDraft,
1589
            $unavailableFolder->getVersionInfo()->getContentInfo()
1590
        );
1591
1592
        // publish Test Folder
1593
        $testFolder = $contentService->publishVersion($testFolderDraft);
1594
1595
        // set the current user to be an anonymous user since we want to test that
1596
        // if the user doesn't have access to an related object that object wont
1597
        // be loaded and no exception will be thrown
1598
        $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...
1599
1600
        // finaly load relations ( verify no exception is thrown )
1601
        $actualRelations = $contentService->loadRelations($testFolder->getVersionInfo());
1602
1603
        /* END: Use case */
1604
1605
        // assert results
1606
        // verify that the only expected relations are from the 2 readable objects
1607
        // Main Folder and Available Folder
1608
        $expectedRelations = [
1609
            $mainRelation->destinationContentInfo->id => $mainRelation,
1610
            $availableRelation->destinationContentInfo->id => $availableRelation,
1611
        ];
1612
1613
        // assert there are as many expected relations as actual ones
1614
        $this->assertEquals(
1615
            count($expectedRelations),
1616
            count($actualRelations),
1617
            "Expected '" . count($expectedRelations)
1618
            . "' relations found '" . count($actualRelations) . "'"
1619
        );
1620
1621
        // assert each relation
1622
        foreach ($actualRelations as $relation) {
1623
            $destination = $relation->destinationContentInfo;
1624
            $expected = $expectedRelations[$destination->id]->destinationContentInfo;
1625
            $this->assertNotEmpty($expected, "Non expected relation with '{$destination->id}' id found");
1626
            $this->assertEquals(
1627
                $expected->id,
1628
                $destination->id,
1629
                "Expected relation with '{$expected->id}' id found '{$destination->id}' id"
1630
            );
1631
            $this->assertEquals(
1632
                $expected->name,
1633
                $destination->name,
1634
                "Expected relation with '{$expected->name}' name found '{$destination->name}' name"
1635
            );
1636
1637
            // remove from list
1638
            unset($expectedRelations[$destination->id]);
1639
        }
1640
1641
        // verify all expected relations were found
1642
        $this->assertEquals(
1643
            0,
1644
            count($expectedRelations),
1645
            "Expected to find '" . (count($expectedRelations) + count($actualRelations))
1646
            . "' relations found '" . count($actualRelations) . "'"
1647
        );
1648
    }
1649
1650
    /**
1651
     * Test copying Content to the authorized Location (limited by policies).
1652
     */
1653
    public function testCopyContentToAuthorizedLocation()
1654
    {
1655
        $repository = $this->getRepository();
1656
        $contentService = $repository->getContentService();
1657
        $locationService = $repository->getLocationService();
1658
        $roleService = $repository->getRoleService();
1659
1660
        // Create and publish folders for the test case
1661
        $folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']);
1662
        $contentService->publishVersion($folderDraft->versionInfo);
1663
        $authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']);
1664
        $authorizedFolder = $contentService->publishVersion($authorizedFolderDraft->versionInfo);
1665
1666
        // Prepare Role for the test case
1667
        $roleIdentifier = 'authorized_folder';
1668
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleIdentifier);
1669
        $locationLimitation = new LocationLimitation(
1670
            ['limitationValues' => [$authorizedFolder->contentInfo->mainLocationId]]
1671
        );
1672
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'read'));
1673
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'versionread'));
1674
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'manage_locations'));
1675
1676
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
1677
        $policyCreateStruct->addLimitation($locationLimitation);
1678
        $roleCreateStruct->addPolicy($policyCreateStruct);
1679
1680
        $roleDraft = $roleService->createRole($roleCreateStruct);
1681
        $roleService->publishRoleDraft($roleDraft);
1682
1683
        // Create a user with that Role
1684
        $user = $this->createCustomUserVersion1('Users', $roleIdentifier);
1685
        $repository->getPermissionResolver()->setCurrentUserReference($user);
1686
1687
        // Test copying Content to the authorized Location
1688
        $contentService->copyContent(
1689
            $authorizedFolder->contentInfo,
1690
            $locationService->newLocationCreateStruct(
1691
                $authorizedFolder->contentInfo->mainLocationId
1692
            )
1693
        );
1694
    }
1695
1696
    /**
1697
     * Test copying Content to the authorized Location (limited by policies).
1698
     */
1699
    public function testCopyContentToAuthorizedLocationWithSubtreeLimitation()
1700
    {
1701
        $repository = $this->getRepository();
1702
        $contentService = $repository->getContentService();
1703
        $locationService = $repository->getLocationService();
1704
        $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...
1705
1706
        // Create and publish folders for the test case
1707
        $folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']);
1708
        $contentService->publishVersion($folderDraft->versionInfo);
1709
        $authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']);
1710
        $authorizedFolder = $contentService->publishVersion($authorizedFolderDraft->versionInfo);
1711
1712
        // Prepare Role for the test case
1713
        $roleIdentifier = 'authorized_subree';
1714
        $subtreeLimitation = new SubtreeLimitation(
1715
            ['limitationValues' => ['/1/2']]
1716
        );
1717
        $policiesData = [
1718
            [
1719
                'module' => 'content',
1720
                'function' => 'read',
1721
                'limitations' => [$subtreeLimitation],
1722
            ],
1723
            [
1724
                'module' => 'content',
1725
                'function' => 'versionread',
1726
                'limitations' => [$subtreeLimitation],
1727
            ],
1728
            [
1729
                'module' => 'content',
1730
                'function' => 'create',
1731
                'limitations' => [$subtreeLimitation],
1732
            ],
1733
            [
1734
                'module' => 'content',
1735
                'function' => 'manage_locations',
1736
            ],
1737
        ];
1738
1739
        $this->createRoleWithPolicies($roleIdentifier, $policiesData);
1740
1741
        // Create a user with that Role
1742
        $user = $this->createCustomUserVersion1('Users', $roleIdentifier);
1743
        $repository->getPermissionResolver()->setCurrentUserReference($user);
1744
1745
        // Test copying Content to the authorized Location
1746
        $contentService->copyContent(
1747
            $authorizedFolder->contentInfo,
1748
            $locationService->newLocationCreateStruct(
1749
                $authorizedFolder->contentInfo->mainLocationId
1750
            )
1751
        );
1752
    }
1753
}
1754