Completed
Push — bulk_load_content_info_api ( d8de10...0fcbf2 )
by André
16:46
created

testLoadContentInfoListSkipsUnauthorizedItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 11
loc 11
rs 9.9
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\Values\Content\Location;
12
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
13
14
/**
15
 * Test case for operations in the ContentServiceAuthorization using in memory storage.
16
 *
17
 * @see eZ\Publish\API\Repository\ContentService
18
 * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadAnonymousUser
19
 * @group integration
20
 * @group authorization
21
 */
22
class ContentServiceAuthorizationTest extends BaseContentServiceTest
23
{
24
    /**
25
     * Test for the createContent() method.
26
     *
27
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
28
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
29
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
30
     */
31 View Code Duplication
    public function testCreateContentThrowsUnauthorizedException()
32
    {
33
        if ($this->isVersion4()) {
34
            $this->markTestSkipped('This test requires eZ Publish 5');
35
        }
36
37
        $repository = $this->getRepository();
38
39
        $anonymousUserId = $this->generateId('user', 10);
40
        /* BEGIN: Use Case */
41
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
42
        // demo installation
43
        // Load the user service
44
        $userService = $repository->getUserService();
45
46
        // Set anonymous user
47
        $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...
48
49
        $contentTypeService = $repository->getContentTypeService();
50
51
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
52
53
        $contentService = $repository->getContentService();
54
55
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
56
        $contentCreate->setField('name', 'Awesome Sindelfingen forum');
57
58
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
59
        $contentCreate->alwaysAvailable = true;
60
61
        // This call will fail with a "UnauthorizedException"
62
        $contentService->createContent($contentCreate);
63
        /* END: Use Case */
64
    }
65
66
    /**
67
     * Test for the createContent() method.
68
     *
69
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
70
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
71
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
72
     */
73
    public function testCreateContentThrowsUnauthorizedExceptionWithSecondParameter()
74
    {
75
        $repository = $this->getRepository();
76
77
        $anonymousUserId = $this->generateId('user', 10);
78
        /* BEGIN: Use Case */
79
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
80
        // demo installation
81
        // Load the user service
82
        $userService = $repository->getUserService();
83
84
        // Set anonymous user
85
        $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...
86
87
        $this->createContentDraftVersion1();
88
        /* END: Use Case */
89
    }
90
91
    /**
92
     * Test for the loadContentInfo() method.
93
     *
94
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
95
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
96
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
97
     */
98
    public function testLoadContentInfoThrowsUnauthorizedException()
99
    {
100
        $repository = $this->getRepository();
101
102
        $contentId = $this->generateId('object', 10);
103
        /* BEGIN: Use Case */
104
        $contentService = $repository->getContentService();
105
106
        $pseudoEditor = $this->createAnonymousWithEditorRole();
107
108
        // Set restricted editor user
109
        $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...
110
111
        // This call will fail with a "UnauthorizedException"
112
        // $contentId contains a content object ID not accessible for anonymous
113
        $contentService->loadContentInfo($contentId);
114
        /* END: Use Case */
115
    }
116
117
    /**
118
     * Test for the loadContentInfoList() method.
119
     *
120
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
121
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoList
122
     */
123 View Code Duplication
    public function testLoadContentInfoListSkipsUnauthorizedItems()
124
    {
125
        $repository = $this->getRepository();
126
        $contentId = $this->generateId('object', 10);
127
        $contentService = $repository->getContentService();
128
        $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...
129
130
        $list = $contentService->loadContentInfoList([$contentId]);
131
132
        $this->assertCount(0, $list);
133
    }
134
135
    /**
136
     * Test for the loadContentInfoByRemoteId() method.
137
     *
138
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
139
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
140
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
141
     */
142 View Code Duplication
    public function testLoadContentInfoByRemoteIdThrowsUnauthorizedException()
143
    {
144
        $repository = $this->getRepository();
145
146
        /* BEGIN: Use Case */
147
        // RemoteId of the "Anonymous User" in an eZ Publish demo installation
148
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
149
150
        $contentService = $repository->getContentService();
151
152
        $pseudoEditor = $this->createAnonymousWithEditorRole();
153
154
        // Set restricted editor user
155
        $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...
156
157
        // This call will fail with a "UnauthorizedException"
158
        $contentService->loadContentInfoByRemoteId($anonymousRemoteId);
159
        /* END: Use Case */
160
    }
161
162
    /**
163
     * Test for the loadVersionInfo() method.
164
     *
165
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
166
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
167
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
168
     */
169
    public function testLoadVersionInfoThrowsUnauthorizedException()
170
    {
171
        $repository = $this->getRepository();
172
173
        $anonymousUserId = $this->generateId('user', 10);
174
        /* BEGIN: Use Case */
175
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
176
        // demo installation
177
178
        $contentService = $repository->getContentService();
179
180
        // Load the ContentInfo for "Anonymous User"
181
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
182
183
        $pseudoEditor = $this->createAnonymousWithEditorRole();
184
185
        // Set restricted editor user
186
        $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...
187
188
        // This call will fail with a "UnauthorizedException"
189
        $contentService->loadVersionInfo($contentInfo);
190
        /* END: Use Case */
191
    }
192
193
    /**
194
     * Test for the loadVersionInfo() method.
195
     *
196
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
197
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
198
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
199
     */
200
    public function testLoadVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
201
    {
202
        $repository = $this->getRepository();
203
204
        $anonymousUserId = $this->generateId('user', 10);
205
        /* BEGIN: Use Case */
206
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
207
        // demo installation
208
209
        $contentService = $repository->getContentService();
210
211
        // Load the ContentInfo for "Anonymous User"
212
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
213
214
        $pseudoEditor = $this->createAnonymousWithEditorRole();
215
216
        // Set restricted editor user
217
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

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