Completed
Push — master ( 6c3e1a...f2dfe9 )
by André
17:55
created

testDeleteContentThrowsUnauthorizedException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 24
rs 9.536
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\Repository;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\Location;
14
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
15
16
/**
17
 * Test case for operations in the ContentServiceAuthorization using in memory storage.
18
 *
19
 * @see eZ\Publish\API\Repository\ContentService
20
 * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadAnonymousUser
21
 * @group integration
22
 * @group authorization
23
 */
24
class ContentServiceAuthorizationTest extends BaseContentServiceTest
25
{
26
    /**
27
     * Test for the createContent() method.
28
     *
29
     * @see \eZ\Publish\API\Repository\ContentService::createContent()
30
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
31
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
32
     */
33 View Code Duplication
    public function testCreateContentThrowsUnauthorizedException()
34
    {
35
        if ($this->isVersion4()) {
36
            $this->markTestSkipped('This test requires eZ Publish 5');
37
        }
38
39
        $repository = $this->getRepository();
40
41
        $anonymousUserId = $this->generateId('user', 10);
42
        /* BEGIN: Use Case */
43
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
44
        // demo installation
45
        // Load the user service
46
        $userService = $repository->getUserService();
47
48
        // Set anonymous user
49
        $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...
50
51
        $contentTypeService = $repository->getContentTypeService();
52
53
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
54
55
        $contentService = $repository->getContentService();
56
57
        $contentCreate = $contentService->newContentCreateStruct($contentType, 'eng-US');
58
        $contentCreate->setField('name', 'Awesome Sindelfingen forum');
59
60
        $contentCreate->remoteId = 'abcdef0123456789abcdef0123456789';
61
        $contentCreate->alwaysAvailable = true;
62
63
        // This call will fail with a "UnauthorizedException"
64
        $contentService->createContent($contentCreate);
65
        /* END: Use Case */
66
    }
67
68
    /**
69
     * Test for the createContent() method.
70
     *
71
     * @see \eZ\Publish\API\Repository\ContentService::createContent($contentCreateStruct, $locationCreateStructs)
72
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
73
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent
74
     */
75
    public function testCreateContentThrowsUnauthorizedExceptionWithSecondParameter()
76
    {
77
        $repository = $this->getRepository();
78
79
        $anonymousUserId = $this->generateId('user', 10);
80
        /* BEGIN: Use Case */
81
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
82
        // demo installation
83
        // Load the user service
84
        $userService = $repository->getUserService();
85
86
        // Set anonymous user
87
        $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...
88
89
        $this->createContentDraftVersion1();
90
        /* END: Use Case */
91
    }
92
93
    /**
94
     * Test for the loadContentInfo() method.
95
     *
96
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfo()
97
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
98
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfo
99
     */
100
    public function testLoadContentInfoThrowsUnauthorizedException()
101
    {
102
        $repository = $this->getRepository();
103
104
        $contentId = $this->generateId('object', 10);
105
        /* BEGIN: Use Case */
106
        $contentService = $repository->getContentService();
107
108
        $pseudoEditor = $this->createAnonymousWithEditorRole();
109
110
        // Set restricted editor user
111
        $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...
112
113
        // This call will fail with a "UnauthorizedException"
114
        // $contentId contains a content object ID not accessible for anonymous
115
        $contentService->loadContentInfo($contentId);
116
        /* END: Use Case */
117
    }
118
119
    /**
120
     * Test for the sudo() method.
121
     *
122
     * @see \eZ\Publish\API\Repository\Repository::sudo()
123
     * @depends testLoadContentInfoThrowsUnauthorizedException
124
     */
125
    public function testSudo()
126
    {
127
        $repository = $this->getRepository();
128
        $contentId = $this->generateId('object', 10);
129
        // Set restricted editor user
130
        $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...
131
132
        $contentInfo = $repository->sudo(function (Repository $repository) use ($contentId) {
133
            return $repository->getContentService()->loadContentInfo($contentId);
134
        });
135
136
        $this->assertInstanceOf(
137
            ContentInfo::class,
138
            $contentInfo
139
        );
140
    }
141
142
    /**
143
     * Test for the loadContentInfoList() method.
144
     *
145
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoList()
146
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoList
147
     */
148 View Code Duplication
    public function testLoadContentInfoListSkipsUnauthorizedItems()
149
    {
150
        $repository = $this->getRepository();
151
        $contentId = $this->generateId('object', 10);
152
        $contentService = $repository->getContentService();
153
        $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...
154
155
        $list = $contentService->loadContentInfoList([$contentId]);
156
157
        $this->assertCount(0, $list);
158
    }
159
160
    /**
161
     * Test for the loadContentInfoByRemoteId() method.
162
     *
163
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
164
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
165
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
166
     */
167 View Code Duplication
    public function testLoadContentInfoByRemoteIdThrowsUnauthorizedException()
168
    {
169
        $repository = $this->getRepository();
170
171
        /* BEGIN: Use Case */
172
        // RemoteId of the "Anonymous User" in an eZ Publish demo installation
173
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
174
175
        $contentService = $repository->getContentService();
176
177
        $pseudoEditor = $this->createAnonymousWithEditorRole();
178
179
        // Set restricted editor user
180
        $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...
181
182
        // This call will fail with a "UnauthorizedException"
183
        $contentService->loadContentInfoByRemoteId($anonymousRemoteId);
184
        /* END: Use Case */
185
    }
186
187
    /**
188
     * Test for the loadVersionInfo() method.
189
     *
190
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
191
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
192
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
193
     */
194
    public function testLoadVersionInfoThrowsUnauthorizedException()
195
    {
196
        $repository = $this->getRepository();
197
198
        $anonymousUserId = $this->generateId('user', 10);
199
        /* BEGIN: Use Case */
200
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
201
        // demo installation
202
203
        $contentService = $repository->getContentService();
204
205
        // Load the ContentInfo for "Anonymous User"
206
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
207
208
        $pseudoEditor = $this->createAnonymousWithEditorRole();
209
210
        // Set restricted editor user
211
        $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...
212
213
        // This call will fail with a "UnauthorizedException"
214
        $contentService->loadVersionInfo($contentInfo);
215
        /* END: Use Case */
216
    }
217
218
    /**
219
     * Test for the loadVersionInfo() method.
220
     *
221
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
222
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
223
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
224
     */
225
    public function testLoadVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
226
    {
227
        $repository = $this->getRepository();
228
229
        $anonymousUserId = $this->generateId('user', 10);
230
        /* BEGIN: Use Case */
231
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
232
        // demo installation
233
234
        $contentService = $repository->getContentService();
235
236
        // Load the ContentInfo for "Anonymous User"
237
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
238
239
        $pseudoEditor = $this->createAnonymousWithEditorRole();
240
241
        // Set restricted editor user
242
        $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...
243
244
        // This call will fail with a "UnauthorizedException"
245
        $contentService->loadVersionInfo($contentInfo, 2);
246
        /* END: Use Case */
247
    }
248
249
    /**
250
     * Test for the loadVersionInfoById() method.
251
     *
252
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
253
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
254
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
255
     */
256 View Code Duplication
    public function testLoadVersionInfoByIdThrowsUnauthorizedException()
257
    {
258
        $repository = $this->getRepository();
259
260
        $anonymousUserId = $this->generateId('user', 10);
261
        /* BEGIN: Use Case */
262
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
263
        // demo installation
264
265
        $contentService = $repository->getContentService();
266
267
        $pseudoEditor = $this->createAnonymousWithEditorRole();
268
269
        // Set restricted editor user
270
        $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...
271
272
        // This call will fail with a "UnauthorizedException"
273
        $contentService->loadVersionInfoById($anonymousUserId);
274
        /* END: Use Case */
275
    }
276
277
    /**
278
     * Test for the loadVersionInfoById() method.
279
     *
280
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
281
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
282
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
283
     */
284 View Code Duplication
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionWithSecondParameter()
285
    {
286
        $repository = $this->getRepository();
287
288
        $anonymousUserId = $this->generateId('user', 10);
289
        /* BEGIN: Use Case */
290
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
291
        // demo installation
292
293
        $contentService = $repository->getContentService();
294
295
        $pseudoEditor = $this->createAnonymousWithEditorRole();
296
297
        // Set restricted editor user
298
        $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...
299
300
        // This call will fail with a "UnauthorizedException"
301
        $contentService->loadVersionInfoById($anonymousUserId, 2);
302
        /* END: Use Case */
303
    }
304
305
    /**
306
     * Test for the loadVersionInfoById() method.
307
     *
308
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
309
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
310
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
311
     */
312
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionForFirstDraft()
313
    {
314
        $repository = $this->getRepository();
315
316
        $contentService = $repository->getContentService();
317
318
        $anonymousUserId = $this->generateId('user', 10);
319
        /* BEGIN: Use Case */
320
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
321
        // demo installation
322
        $contentDraft = $this->createContentDraftVersion1();
323
324
        // Load the user service
325
        $userService = $repository->getUserService();
326
327
        // Set anonymous user
328
        $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...
329
330
        // This call will fail with a "UnauthorizedException"
331
        $contentService->loadVersionInfoById(
332
            $contentDraft->id,
333
            $contentDraft->contentInfo->currentVersionNo
334
        );
335
        /* END: Use Case */
336
    }
337
338
    /**
339
     * Test for the loadContentByContentInfo() method.
340
     *
341
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
342
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
343
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
344
     */
345
    public function testLoadContentByContentInfoThrowsUnauthorizedException()
346
    {
347
        $repository = $this->getRepository();
348
349
        $anonymousUserId = $this->generateId('user', 10);
350
        /* BEGIN: Use Case */
351
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
352
        // demo installation
353
354
        $contentService = $repository->getContentService();
355
356
        // Load the ContentInfo for "Anonymous User"
357
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
358
359
        $pseudoEditor = $this->createAnonymousWithEditorRole();
360
361
        // Set restricted editor user
362
        $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...
363
364
        // This call will fail with a "UnauthorizedException"
365
        $contentService->loadContentByContentInfo($contentInfo);
366
        /* END: Use Case */
367
    }
368
369
    /**
370
     * Test for the loadContentByContentInfo() method.
371
     *
372
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
373
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
374
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithLanguageParameters
375
     */
376
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithSecondParameter()
377
    {
378
        $repository = $this->getRepository();
379
380
        $anonymousUserId = $this->generateId('user', 10);
381
        /* BEGIN: Use Case */
382
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
383
        // demo installation
384
385
        $contentService = $repository->getContentService();
386
387
        // Load the ContentInfo for "Anonymous User"
388
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
389
390
        $pseudoEditor = $this->createAnonymousWithEditorRole();
391
392
        // Set restricted editor user
393
        $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...
394
395
        // This call will fail with a "UnauthorizedException"
396
        $contentService->loadContentByContentInfo($contentInfo, array('eng-US'));
397
        /* END: Use Case */
398
    }
399
400
    /**
401
     * Test for the loadContentByContentInfo() method.
402
     *
403
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
404
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
405
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
406
     */
407
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithThirdParameter()
408
    {
409
        $repository = $this->getRepository();
410
411
        $anonymousUserId = $this->generateId('user', 10);
412
        /* BEGIN: Use Case */
413
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
414
        // demo installation
415
416
        $contentService = $repository->getContentService();
417
418
        // Load the ContentInfo for "Anonymous User"
419
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
420
421
        $pseudoEditor = $this->createAnonymousWithEditorRole();
422
423
        // Set restricted editor user
424
        $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...
425
426
        // This call will fail with a "UnauthorizedException"
427
        $contentService->loadContentByContentInfo($contentInfo, array('eng-US'), 2);
428
        /* END: Use Case */
429
    }
430
431
    /**
432
     * Test for the loadContentByVersionInfo() method.
433
     *
434
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo()
435
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
436
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfo
437
     */
438
    public function testLoadContentByVersionInfoThrowsUnauthorizedException()
439
    {
440
        $repository = $this->getRepository();
441
442
        $anonymousUserId = $this->generateId('user', 10);
443
        /* BEGIN: Use Case */
444
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
445
        // demo installation
446
447
        $contentService = $repository->getContentService();
448
449
        // Load the ContentInfo for "Anonymous User"
450
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
451
452
        // Load the current VersionInfo
453
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
454
455
        $pseudoEditor = $this->createAnonymousWithEditorRole();
456
457
        // Set restricted editor user
458
        $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...
459
460
        // This call will fail with a "UnauthorizedException"
461
        $contentService->loadContentByVersionInfo($versionInfo);
462
        /* END: Use Case */
463
    }
464
465
    /**
466
     * Test for the loadContentByVersionInfo() method.
467
     *
468
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByVersionInfo($versionInfo, $languages)
469
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
470
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfoWithSecondParameter
471
     */
472
    public function testLoadContentByVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
473
    {
474
        $repository = $this->getRepository();
475
476
        $anonymousUserId = $this->generateId('user', 10);
477
        /* BEGIN: Use Case */
478
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
479
        // demo installation
480
481
        $contentService = $repository->getContentService();
482
483
        // Load the ContentInfo for "Anonymous User"
484
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
485
486
        // Load the current VersionInfo
487
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
488
489
        $pseudoEditor = $this->createAnonymousWithEditorRole();
490
491
        // Set restricted editor user
492
        $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...
493
494
        // This call will fail with a "UnauthorizedException"
495
        $contentService->loadContentByVersionInfo($versionInfo, array('eng-US'));
496
        /* END: Use Case */
497
    }
498
499
    /**
500
     * Test for the loadContent() method.
501
     *
502
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
503
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
504
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
505
     */
506
    public function testLoadContentThrowsUnauthorizedException()
507
    {
508
        $repository = $this->getRepository();
509
510
        $anonymousUserId = $this->generateId('user', 10);
511
        /* BEGIN: Use Case */
512
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
513
        // demo installation
514
515
        $contentService = $repository->getContentService();
516
517
        $pseudoEditor = $this->createAnonymousWithEditorRole();
518
519
        // Set restricted editor user
520
        $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...
521
522
        // This call will fail with a "UnauthorizedException"
523
        $contentService->loadContent($anonymousUserId);
524
        /* END: Use Case */
525
    }
526
527
    /**
528
     * Test for the loadContent() method.
529
     *
530
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages)
531
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
532
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithSecondParameter
533
     */
534
    public function testLoadContentThrowsUnauthorizedExceptionWithSecondParameter()
535
    {
536
        $repository = $this->getRepository();
537
538
        $anonymousUserId = $this->generateId('user', 10);
539
        /* BEGIN: Use Case */
540
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
541
        // demo installation
542
543
        $contentService = $repository->getContentService();
544
545
        $pseudoEditor = $this->createAnonymousWithEditorRole();
546
547
        // Set restricted editor user
548
        $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...
549
550
        // This call will fail with a "UnauthorizedException"
551
        $contentService->loadContent($anonymousUserId, array('eng-US'));
552
        /* END: Use Case */
553
    }
554
555
    /**
556
     * Test for the loadContent() method.
557
     *
558
     * @see \eZ\Publish\API\Repository\ContentService::loadContent($contentId, $languages, $versionNo)
559
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
560
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentWithThirdParameter
561
     */
562
    public function testLoadContentThrowsUnauthorizedExceptionWithThirdParameter()
563
    {
564
        $repository = $this->getRepository();
565
566
        $anonymousUserId = $this->generateId('user', 10);
567
        /* BEGIN: Use Case */
568
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
569
        // demo installation
570
571
        $contentService = $repository->getContentService();
572
573
        $pseudoEditor = $this->createAnonymousWithEditorRole();
574
575
        // Set restricted editor user
576
        $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...
577
578
        // This call will fail with a "UnauthorizedException"
579
        $contentService->loadContent($anonymousUserId, array('eng-US'), 2);
580
        /* END: Use Case */
581
    }
582
583
    /**
584
     * Test for the loadContent() method on a draft.
585
     *
586
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
587
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
588
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
589
     */
590
    public function testLoadContentThrowsUnauthorizedExceptionOnDrafts()
591
    {
592
        /** @var $repository \eZ\Publish\API\Repository\Repository */
593
        $repository = $this->getRepository();
594
595
        $anonymousUserId = $this->generateId('user', 10);
596
        /* BEGIN: Use Case */
597
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
598
        // demo installation
599
        $user = $this->createUserVersion1();
600
601
        // Set new editor as a content owner
602
        $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...
603
604
        // Create draft with this user
605
        $draft = $this->createContentDraftVersion1(2, 'folder');
606
607
        // Load anonymous user
608
        $userService = $repository->getUserService();
609
        $user = $userService->loadUser($anonymousUserId);
610
        $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...
611
612
        // Try to load the draft with anonymous user to make sure access won't be allowed by throwing an exception
613
        $contentService = $repository->getContentService();
614
        $contentService->loadContent($draft->id);
615
        /* END: Use Case */
616
    }
617
618
    /**
619
     * Test for the ContentService::loadContent() method on an archive.
620
     *
621
     * This test the version permission on loading archived versions
622
     *
623
     * @see \eZ\Publish\API\Repository\ContentService::loadContent()
624
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
625
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
626
     */
627
    public function testLoadContentThrowsUnauthorizedExceptionsOnArchives()
628
    {
629
        /** @var $repository \eZ\Publish\API\Repository\Repository */
630
        $repository = $this->getRepository();
631
632
        $anonymousUserId = $this->generateId('user', 10);
633
        /* BEGIN: Use Case */
634
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
635
        // demo installation
636
        // get necessary services
637
        $contentTypeService = $repository->getContentTypeService();
638
        $contentService = $repository->getContentService();
639
        $locationSercice = $repository->getLocationService();
640
641
        // set admin as current user
642
        $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...
643
644
        // create folder
645
        $newStruct = $contentService->newContentCreateStruct(
646
            $contentTypeService->loadContentTypeByIdentifier('folder'),
647
            'eng-US'
648
        );
649
        $newStruct->setField('name', 'Test Folder');
650
        $draft = $contentService->createContent(
651
            $newStruct,
652
            array($locationSercice->newLocationCreateStruct(2))
653
        );
654
        $object = $contentService->publishVersion($draft->versionInfo);
655
656
        // update folder to make an archived version
657
        $updateStruct = $contentService->newContentUpdateStruct();
658
        $updateStruct->setField('name', 'Test Folder Updated');
659
        $draftUpdated = $contentService->updateContent(
660
            $contentService->createContentDraft($object->contentInfo)->versionInfo,
661
            $updateStruct
662
        );
663
        $objectUpdated = $contentService->publishVersion($draftUpdated->versionInfo);
664
665
        // set an anonymous as current user
666
        $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...
667
668
        // throws an unauthorized exception since anonymous user don't have access to archived versions
669
        $contentService->loadContent($objectUpdated->id, null, 1);
670
        /* END: Use Case */
671
    }
672
673
    /**
674
     * Test for the loadContentByRemoteId() method.
675
     *
676
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId()
677
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
678
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteId
679
     */
680 View Code Duplication
    public function testLoadContentByRemoteIdThrowsUnauthorizedException()
681
    {
682
        $repository = $this->getRepository();
683
684
        /* BEGIN: Use Case */
685
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
686
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
687
688
        $contentService = $repository->getContentService();
689
690
        $pseudoEditor = $this->createAnonymousWithEditorRole();
691
692
        // Set restricted editor user
693
        $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...
694
695
        // This call will fail with a "UnauthorizedException"
696
        $contentService->loadContentByRemoteId($anonymousRemoteId);
697
        /* END: Use Case */
698
    }
699
700
    /**
701
     * Test for the loadContentByRemoteId() method.
702
     *
703
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages)
704
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
705
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithSecondParameter
706
     */
707 View Code Duplication
    public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithSecondParameter()
708
    {
709
        $repository = $this->getRepository();
710
711
        /* BEGIN: Use Case */
712
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
713
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
714
715
        $contentService = $repository->getContentService();
716
717
        $pseudoEditor = $this->createAnonymousWithEditorRole();
718
719
        // Set restricted editor user
720
        $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...
721
722
        // This call will fail with a "UnauthorizedException"
723
        $contentService->loadContentByRemoteId($anonymousRemoteId, array('eng-US'));
724
        /* END: Use Case */
725
    }
726
727
    /**
728
     * Test for the loadContentByRemoteId() method.
729
     *
730
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByRemoteId($remoteId, $languages, $versionNo)
731
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
732
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByRemoteIdWithThirdParameter
733
     */
734 View Code Duplication
    public function testLoadContentByRemoteIdThrowsUnauthorizedExceptionWithThirdParameter()
735
    {
736
        $repository = $this->getRepository();
737
738
        /* BEGIN: Use Case */
739
        // Remote id of the "Anonymous" user in a eZ Publish demo installation
740
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
741
742
        $contentService = $repository->getContentService();
743
744
        $pseudoEditor = $this->createAnonymousWithEditorRole();
745
746
        // Set restricted editor user
747
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
748
749
        // This call will fail with a "UnauthorizedException"
750
        $contentService->loadContentByRemoteId($anonymousRemoteId, array('eng-US'), 2);
751
        /* END: Use Case */
752
    }
753
754
    /**
755
     * Test for the updateContentMetadata() method.
756
     *
757
     * @see \eZ\Publish\API\Repository\ContentService::updateContentMetadata()
758
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
759
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata
760
     */
761
    public function testUpdateContentMetadataThrowsUnauthorizedException()
762
    {
763
        $repository = $this->getRepository();
764
765
        $contentService = $repository->getContentService();
766
767
        $anonymousUserId = $this->generateId('user', 10);
768
        /* BEGIN: Use Case */
769
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
770
        // demo installation
771
        $content = $this->createContentVersion1();
772
773
        // Get ContentInfo instance.
774
        $contentInfo = $content->contentInfo;
775
776
        // Load the user service
777
        $userService = $repository->getUserService();
778
779
        // Set anonymous user
780
        $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...
781
782
        // Creates a metadata update struct
783
        $metadataUpdate = $contentService->newContentMetadataUpdateStruct();
784
785
        $metadataUpdate->remoteId = 'aaaabbbbccccddddeeeeffff11112222';
786
        $metadataUpdate->mainLanguageCode = 'eng-US';
787
        $metadataUpdate->alwaysAvailable = false;
788
        $metadataUpdate->publishedDate = $this->createDateTime();
789
        $metadataUpdate->modificationDate = $this->createDateTime();
790
791
        // This call will fail with a "UnauthorizedException"
792
        $contentService->updateContentMetadata(
793
            $contentInfo,
794
            $metadataUpdate
795
        );
796
        /* END: Use Case */
797
    }
798
799
    /**
800
     * Test for the deleteContent() method.
801
     *
802
     * @see \eZ\Publish\API\Repository\ContentService::deleteContent()
803
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
804
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteContent
805
     */
806
    public function testDeleteContentThrowsUnauthorizedException()
807
    {
808
        $repository = $this->getRepository();
809
        $contentService = $repository->getContentService();
810
811
        $anonymousUserId = $this->generateId('user', 10);
812
        /* BEGIN: Use Case */
813
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
814
        // demo installation
815
        $contentVersion2 = $this->createContentVersion2();
816
817
        // Get ContentInfo instance
818
        $contentInfo = $contentVersion2->contentInfo;
819
820
        // Load the user service
821
        $userService = $repository->getUserService();
822
823
        // Set anonymous user
824
        $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...
825
826
        // This call will fail with a "UnauthorizedException"
827
        $contentService->deleteContent($contentInfo);
828
        /* END: Use Case */
829
    }
830
831
    /**
832
     * Test for the createContentDraft() method.
833
     *
834
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft()
835
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
836
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraft
837
     */
838
    public function testCreateContentDraftThrowsUnauthorizedException()
839
    {
840
        $repository = $this->getRepository();
841
842
        $contentService = $repository->getContentService();
843
844
        $anonymousUserId = $this->generateId('user', 10);
845
        /* BEGIN: Use Case */
846
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
847
        // demo installation
848
        $content = $this->createContentVersion1();
849
850
        // Get ContentInfo instance
851
        $contentInfo = $content->contentInfo;
852
853
        // Load the user service
854
        $userService = $repository->getUserService();
855
856
        // Set anonymous user
857
        $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...
858
859
        // This call will fail with a "UnauthorizedException"
860
        $contentService->createContentDraft($contentInfo);
861
        /* END: Use Case */
862
    }
863
864
    /**
865
     * Test for the createContentDraft() method.
866
     *
867
     * @see \eZ\Publish\API\Repository\ContentService::createContentDraft($contentInfo, $versionInfo)
868
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
869
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContentDraftWithSecondParameter
870
     */
871 View Code Duplication
    public function testCreateContentDraftThrowsUnauthorizedExceptionWithSecondParameter()
872
    {
873
        $repository = $this->getRepository();
874
875
        $contentService = $repository->getContentService();
876
877
        $anonymousUserId = $this->generateId('user', 10);
878
        /* BEGIN: Use Case */
879
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
880
        // demo installation
881
        $content = $this->createContentVersion1();
882
883
        // Get ContentInfo and VersionInfo instances
884
        $contentInfo = $content->contentInfo;
885
        $versionInfo = $content->getVersionInfo();
886
887
        // Load the user service
888
        $userService = $repository->getUserService();
889
890
        // Set anonymous user
891
        $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...
892
893
        // This call will fail with a "UnauthorizedException"
894
        $contentService->createContentDraft($contentInfo, $versionInfo);
895
        /* END: Use Case */
896
    }
897
898
    /**
899
     * Test for the loadContentDrafts() method.
900
     *
901
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts()
902
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
903
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
904
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
905
     */
906
    public function testLoadContentDraftsThrowsUnauthorizedException()
907
    {
908
        $repository = $this->getRepository();
909
910
        $anonymousUserId = $this->generateId('user', 10);
911
        /* BEGIN: Use Case */
912
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
913
        // demo installation
914
        $contentService = $repository->getContentService();
915
916
        // Load the user service
917
        $userService = $repository->getUserService();
918
919
        // Set anonymous user
920
        $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...
921
922
        // This call will fail with a "UnauthorizedException"
923
        $contentService->loadContentDrafts();
924
        /* END: Use Case */
925
    }
926
927
    /**
928
     * Test for the loadContentDrafts() method.
929
     *
930
     * @see \eZ\Publish\API\Repository\ContentService::loadContentDrafts($user)
931
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
932
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
933
     */
934
    public function testLoadContentDraftsThrowsUnauthorizedExceptionWithFirstParameter()
935
    {
936
        $repository = $this->getRepository();
937
938
        $administratorUserId = $this->generateId('user', 14);
939
        $anonymousUserId = $this->generateId('user', 10);
940
        /* BEGIN: Use Case */
941
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
942
        // demo installation
943
        // $administratorUserId is  the ID of the "Administrator" user in a eZ
944
        // Publish demo installation.
945
946
        $contentService = $repository->getContentService();
947
948
        // Load the user service
949
        $userService = $repository->getUserService();
950
951
        // Load the "Administrator" user
952
        $administratorUser = $userService->loadUser($administratorUserId);
953
954
        // Set anonymous user
955
        $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...
956
957
        // This call will fail with a "UnauthorizedException"
958
        $contentService->loadContentDrafts($administratorUser);
959
        /* END: Use Case */
960
    }
961
962
    /**
963
     * Test for the updateContent() method.
964
     *
965
     * @see \eZ\Publish\API\Repository\ContentService::updateContent()
966
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
967
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent
968
     */
969 View Code Duplication
    public function testUpdateContentThrowsUnauthorizedException()
970
    {
971
        $repository = $this->getRepository();
972
        $contentService = $repository->getContentService();
973
974
        $anonymousUserId = $this->generateId('user', 10);
975
        /* BEGIN: Use Case */
976
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
977
        // demo installation
978
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
979
        // demo installation
980
        $draftVersion2 = $this->createContentDraftVersion2();
981
982
        // Get VersionInfo instance
983
        $versionInfo = $draftVersion2->getVersionInfo();
984
985
        // Load the user service
986
        $userService = $repository->getUserService();
987
988
        // Set anonymous user
989
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
990
991
        // Create an update struct and modify some fields
992
        $contentUpdate = $contentService->newContentUpdateStruct();
993
        $contentUpdate->setField('name', 'An awesome² story about ezp.');
994
        $contentUpdate->setField('name', 'An awesome²³ story about ezp.', 'eng-GB');
995
996
        $contentUpdate->initialLanguageCode = 'eng-US';
997
998
        // This call will fail with a "UnauthorizedException"
999
        $contentService->updateContent($versionInfo, $contentUpdate);
1000
        /* END: Use Case */
1001
    }
1002
1003
    /**
1004
     * Test for the publishVersion() method.
1005
     *
1006
     * @see \eZ\Publish\API\Repository\ContentService::publishVersion()
1007
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1008
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testPublishVersion
1009
     */
1010
    public function testPublishVersionThrowsUnauthorizedException()
1011
    {
1012
        $repository = $this->getRepository();
1013
        $contentService = $repository->getContentService();
1014
1015
        $anonymousUserId = $this->generateId('user', 10);
1016
        /* BEGIN: Use Case */
1017
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1018
        // demo installation
1019
        $draft = $this->createContentDraftVersion1();
1020
1021
        // Load the user service
1022
        $userService = $repository->getUserService();
1023
1024
        // Set anonymous user
1025
        $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...
1026
1027
        // This call will fail with a "UnauthorizedException"
1028
        $contentService->publishVersion($draft->getVersionInfo());
1029
        /* END: Use Case */
1030
    }
1031
1032
    /**
1033
     * Test for the deleteVersion() method.
1034
     *
1035
     * @see \eZ\Publish\API\Repository\ContentService::deleteVersion()
1036
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1037
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteVersion
1038
     */
1039
    public function testDeleteVersionThrowsUnauthorizedException()
1040
    {
1041
        $repository = $this->getRepository();
1042
        $contentService = $repository->getContentService();
1043
1044
        $anonymousUserId = $this->generateId('user', 10);
1045
        /* BEGIN: Use Case */
1046
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1047
        // demo installation
1048
        $draft = $this->createContentDraftVersion1();
1049
1050
        // Load the user service
1051
        $userService = $repository->getUserService();
1052
1053
        // Set anonymous user
1054
        $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...
1055
1056
        // This call will fail with a "UnauthorizedException", because "content"
1057
        // "versionremove" permission is missing.
1058
        $contentService->deleteVersion($draft->getVersionInfo());
1059
        /* END: Use Case */
1060
    }
1061
1062
    /**
1063
     * Test for the loadVersions() method.
1064
     *
1065
     * @see \eZ\Publish\API\Repository\ContentService::loadVersions()
1066
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1067
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersions
1068
     */
1069
    public function testLoadVersionsThrowsUnauthorizedException()
1070
    {
1071
        $repository = $this->getRepository();
1072
1073
        $contentService = $repository->getContentService();
1074
1075
        $anonymousUserId = $this->generateId('user', 10);
1076
        /* BEGIN: Use Case */
1077
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1078
        // demo installation
1079
        $contentVersion2 = $this->createContentVersion2();
1080
1081
        // Get ContentInfo instance of version 2
1082
        $contentInfo = $contentVersion2->contentInfo;
1083
1084
        // Load the user service
1085
        $userService = $repository->getUserService();
1086
1087
        // Set anonymous user
1088
        $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...
1089
1090
        // This call will fail with a "UnauthorizedException"
1091
        $contentService->loadVersions($contentInfo);
1092
        /* END: Use Case */
1093
    }
1094
1095
    /**
1096
     * Test for the copyContent() method.
1097
     *
1098
     * @see \eZ\Publish\API\Repository\ContentService::copyContent()
1099
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1100
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContent
1101
     */
1102 View Code Duplication
    public function testCopyContentThrowsUnauthorizedException()
1103
    {
1104
        $parentLocationId = $this->generateId('location', 52);
1105
1106
        $repository = $this->getRepository();
1107
1108
        $contentService = $repository->getContentService();
1109
        $locationService = $repository->getLocationService();
1110
1111
        $anonymousUserId = $this->generateId('user', 10);
1112
        /* BEGIN: Use Case */
1113
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1114
        // demo installation
1115
        $contentVersion2 = $this->createMultipleLanguageContentVersion2();
1116
1117
        // Get ContentInfo instance of version 2
1118
        $contentInfo = $contentVersion2->contentInfo;
1119
1120
        // Load the user service
1121
        $userService = $repository->getUserService();
1122
1123
        // Set anonymous user
1124
        $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...
1125
1126
        // Configure new target location
1127
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
1128
1129
        $targetLocationCreate->priority = 42;
1130
        $targetLocationCreate->hidden = true;
1131
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
1132
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
1133
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
1134
1135
        // This call will fail with a "UnauthorizedException"
1136
        $contentService->copyContent(
1137
            $contentInfo,
1138
            $targetLocationCreate
1139
        );
1140
        /* END: Use Case */
1141
    }
1142
1143
    /**
1144
     * Test for the copyContent() method.
1145
     *
1146
     * @see \eZ\Publish\API\Repository\ContentService::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo)
1147
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1148
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCopyContentWithThirdParameter
1149
     */
1150 View Code Duplication
    public function testCopyContentThrowsUnauthorizedExceptionWithThirdParameter()
1151
    {
1152
        $parentLocationId = $this->generateId('location', 52);
1153
1154
        $repository = $this->getRepository();
1155
1156
        $contentService = $repository->getContentService();
1157
        $locationService = $repository->getLocationService();
1158
1159
        $anonymousUserId = $this->generateId('user', 10);
1160
        /* BEGIN: Use Case */
1161
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1162
        // demo installation
1163
        $contentVersion2 = $this->createContentVersion2();
1164
1165
        // Load the user service
1166
        $userService = $repository->getUserService();
1167
1168
        // Set anonymous user
1169
        $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...
1170
1171
        // Configure new target location
1172
        $targetLocationCreate = $locationService->newLocationCreateStruct($parentLocationId);
1173
1174
        $targetLocationCreate->priority = 42;
1175
        $targetLocationCreate->hidden = true;
1176
        $targetLocationCreate->remoteId = '01234abcdef5678901234abcdef56789';
1177
        $targetLocationCreate->sortField = Location::SORT_FIELD_NODE_ID;
1178
        $targetLocationCreate->sortOrder = Location::SORT_ORDER_DESC;
1179
1180
        // This call will fail with a "UnauthorizedException"
1181
        $contentService->copyContent(
1182
            $contentVersion2->contentInfo,
1183
            $targetLocationCreate,
1184
            $contentService->loadVersionInfo($contentVersion2->contentInfo, 1)
1185
        );
1186
        /* END: Use Case */
1187
    }
1188
1189
    /**
1190
     * Test for the loadRelations() method.
1191
     *
1192
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1193
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1194
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
1195
     */
1196 View Code Duplication
    public function testLoadRelationsThrowsUnauthorizedException()
1197
    {
1198
        $repository = $this->getRepository();
1199
1200
        $contentService = $repository->getContentService();
1201
1202
        /* BEGIN: Use Case */
1203
        $user = $this->createMediaUserVersion1();
1204
1205
        // Remote id of the "Setup" page of a eZ Publish demo installation.
1206
        $setupRemoteId = '241d538ce310074e602f29f49e44e938';
1207
1208
        $versionInfo = $contentService->loadVersionInfo(
1209
            $contentService->loadContentInfoByRemoteId(
1210
                $setupRemoteId
1211
            )
1212
        );
1213
1214
        // Set media editor as current user
1215
        $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...
1216
1217
        // This call will fail with a "UnauthorizedException"
1218
        $contentService->loadRelations($versionInfo);
1219
        /* END: Use Case */
1220
    }
1221
1222
    /**
1223
     * Test for the loadRelations() method.
1224
     *
1225
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1226
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1227
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadRelations
1228
     */
1229
    public function testLoadRelationsForDraftVersionThrowsUnauthorizedException()
1230
    {
1231
        $repository = $this->getRepository();
1232
1233
        $contentService = $repository->getContentService();
1234
1235
        $anonymousUserId = $this->generateId('user', 10);
1236
        /* BEGIN: Use Case */
1237
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1238
        // demo installation
1239
        $draft = $this->createContentDraftVersion1();
1240
1241
        // Load the user service
1242
        $userService = $repository->getUserService();
1243
1244
        // Set anonymous user
1245
        $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...
1246
1247
        // This call will fail with a "UnauthorizedException"
1248
        $contentService->loadRelations($draft->versionInfo);
1249
        /* END: Use Case */
1250
    }
1251
1252
    /**
1253
     * Test for the loadReverseRelations() method.
1254
     *
1255
     * @see \eZ\Publish\API\Repository\ContentService::loadReverseRelations()
1256
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1257
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadReverseRelations
1258
     */
1259 View Code Duplication
    public function testLoadReverseRelationsThrowsUnauthorizedException()
1260
    {
1261
        $repository = $this->getRepository();
1262
1263
        $contentService = $repository->getContentService();
1264
1265
        /* BEGIN: Use Case */
1266
        $user = $this->createMediaUserVersion1();
1267
1268
        // Remote id of the "Media" page of a eZ Publish demo installation.
1269
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1270
1271
        $contentInfo = $contentService->loadContentInfoByRemoteId(
1272
            $mediaRemoteId
1273
        );
1274
1275
        // Set media editor as current user
1276
        $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...
1277
1278
        // This call will fail with a "UnauthorizedException"
1279
        $contentService->loadReverseRelations($contentInfo);
1280
        /* END: Use Case */
1281
    }
1282
1283
    /**
1284
     * Test for the addRelation() method.
1285
     *
1286
     * @see \eZ\Publish\API\Repository\ContentService::addRelation()
1287
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1288
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
1289
     */
1290
    public function testAddRelationThrowsUnauthorizedException()
1291
    {
1292
        $repository = $this->getRepository();
1293
1294
        $contentService = $repository->getContentService();
1295
1296
        $anonymousUserId = $this->generateId('user', 10);
1297
        /* BEGIN: Use Case */
1298
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1299
        // demo installation
1300
        // Remote id of the "Media" page of a eZ Publish demo installation.
1301
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1302
1303
        $draft = $this->createContentDraftVersion1();
1304
1305
        // Get the draft's version info
1306
        $versionInfo = $draft->getVersionInfo();
1307
1308
        // Load other content object
1309
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1310
1311
        // Load the user service
1312
        $userService = $repository->getUserService();
1313
1314
        // Set anonymous user
1315
        $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...
1316
1317
        // This call will fail with a "UnauthorizedException"
1318
        $contentService->addRelation(
1319
            $versionInfo,
1320
            $media
1321
        );
1322
        /* END: Use Case */
1323
    }
1324
1325
    /**
1326
     * Test for the deleteRelation() method.
1327
     *
1328
     * @see \eZ\Publish\API\Repository\ContentService::deleteRelation()
1329
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
1330
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testDeleteRelation
1331
     */
1332
    public function testDeleteRelationThrowsUnauthorizedException()
1333
    {
1334
        $repository = $this->getRepository();
1335
1336
        $contentService = $repository->getContentService();
1337
1338
        $anonymousUserId = $this->generateId('user', 10);
1339
        /* BEGIN: Use Case */
1340
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1341
        // demo installation
1342
        // Remote ids of the "Media" and the "Demo Design" page of a eZ Publish
1343
        // demo installation.
1344
        $mediaRemoteId = 'a6e35cbcb7cd6ae4b691f3eee30cd262';
1345
        $demoDesignRemoteId = '8b8b22fe3c6061ed500fbd2b377b885f';
1346
1347
        $draft = $this->createContentDraftVersion1();
1348
1349
        // Get the draft's version info
1350
        $versionInfo = $draft->getVersionInfo();
1351
1352
        $media = $contentService->loadContentInfoByRemoteId($mediaRemoteId);
1353
        $demoDesign = $contentService->loadContentInfoByRemoteId($demoDesignRemoteId);
1354
1355
        // Establish some relations
1356
        $contentService->addRelation($draft->getVersionInfo(), $media);
1357
        $contentService->addRelation($draft->getVersionInfo(), $demoDesign);
1358
1359
        // Load the user service
1360
        $userService = $repository->getUserService();
1361
1362
        // Set anonymous user
1363
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

Loading history...
1364
1365
        // This call will fail with a "UnauthorizedException"
1366
        $contentService->deleteRelation($versionInfo, $media);
1367
        /* END: Use Case */
1368
    }
1369
1370
    /**
1371
     * Creates a pseudo editor with a limitation to objects in the "Media/Images"
1372
     * subtree.
1373
     *
1374
     * @return \eZ\Publish\API\Repository\Values\User\User
1375
     */
1376
    private function createAnonymousWithEditorRole()
1377
    {
1378
        $repository = $this->getRepository();
1379
1380
        $anonymousUserId = $this->generateId('user', 10);
1381
        /* BEGIN: Use Case */
1382
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1383
        // demo installation
1384
        $roleService = $repository->getRoleService();
1385
        $userService = $repository->getUserService();
1386
1387
        $user = $userService->loadUser($anonymousUserId);
1388
        $role = $roleService->loadRoleByIdentifier('Editor');
1389
1390
        // Assign "Editor" role with limitation to "Media/Images"
1391
        $roleService->assignRoleToUser(
1392
            $role,
1393
            $user,
1394
            new \eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation(
1395
                array(
1396
                    'limitationValues' => array('/1/43/51/'),
1397
                )
1398
            )
1399
        );
1400
1401
        $pseudoEditor = $userService->loadUser($user->id);
1402
        /* END: Inline */
1403
1404
        return $pseudoEditor;
1405
    }
1406
1407
    /**
1408
     * Test that for an user that doesn't have access (read permissions) to an
1409
     * related object, executing loadRelations() would not throw any exception,
1410
     * only that the non-readable related object(s) won't be loaded.
1411
     *
1412
     * @see \eZ\Publish\API\Repository\ContentService::loadRelations()
1413
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testAddRelation
1414
     */
1415
    public function testLoadRelationsWithUnauthorizedRelations()
1416
    {
1417
        $repository = $this->getRepository();
1418
1419
        $anonymousUserId = $this->generateId('user', 10);
1420
        /* BEGIN: Use Case */
1421
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
1422
        // demo installation
1423
        $mainLanguage = 'eng-GB';
1424
1425
        $contentService = $repository->getContentService();
1426
        $contenTypeService = $repository->getContentTypeService();
1427
        $locationService = $repository->getLocationService();
1428
        $sectionService = $repository->getSectionService();
1429
        $userService = $repository->getUserService();
1430
1431
        // set the current user as admin to create the environment to test
1432
        $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...
1433
1434
        // create section
1435
        // since anonymous users have their read permissions to specific sections
1436
        // the created section will be non-readable to them
1437
        $sectionCreate = $sectionService->newSectionCreateStruct();
1438
        $sectionCreate->identifier = 'private';
1439
        $sectionCreate->name = 'Private Section';
1440
        $section = $sectionService->createSection($sectionCreate);
1441
1442
        // create objects for testing
1443
        // here we will create 4 objects which 2 will be readable by an anonymous
1444
        // user, and the other 2 wont these last 2 will go to a private section
1445
        // where anonymous can't read, just like:
1446
        // readable object 1 -> /Main Folder
1447
        // readable object 2 -> /Main Folder/Available Folder
1448
        // non-readable object 1 -> /Restricted Folder
1449
        // non-readable object 2 -> /Restricted Folder/Unavailable Folder
1450
        //
1451
        // here is created - readable object 1 -> /Main Folder
1452
        $mainFolderCreate = $contentService->newContentCreateStruct(
1453
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1454
            $mainLanguage
1455
        );
1456
        $mainFolderCreate->setField('name', 'Main Folder');
1457
        $mainFolder = $contentService->publishVersion(
1458
            $contentService->createContent(
1459
                $mainFolderCreate,
1460
                array($locationService->newLocationCreateStruct(2))
1461
            )->versionInfo
1462
        );
1463
1464
        // here is created readable object 2 -> /Main Folder/Available Folder
1465
        $availableFolderCreate = $contentService->newContentCreateStruct(
1466
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1467
            $mainLanguage
1468
        );
1469
        $availableFolderCreate->setField('name', 'Avaliable Folder');
1470
        $availableFolder = $contentService->publishVersion(
1471
            $contentService->createContent(
1472
                $availableFolderCreate,
1473
                array($locationService->newLocationCreateStruct($mainFolder->contentInfo->mainLocationId))
1474
            )->versionInfo
1475
        );
1476
1477
        // here is created the non-readable object 1 -> /Restricted Folder
1478
        $restrictedFolderCreate = $contentService->newContentCreateStruct(
1479
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1480
            $mainLanguage
1481
        );
1482
        $restrictedFolderCreate->setField('name', 'Restricted Folder');
1483
        $restrictedFolderCreate->sectionId = $section->id;
1484
        $restrictedFolder = $contentService->publishVersion(
1485
            $contentService->createContent(
1486
                $restrictedFolderCreate,
1487
                array($locationService->newLocationCreateStruct(2))
1488
            )->versionInfo
1489
        );
1490
1491
        // here is created non-readable object 2 -> /Restricted Folder/Unavailable Folder
1492
        $unavailableFolderCreate = $contentService->newContentCreateStruct(
1493
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1494
            $mainLanguage
1495
        );
1496
        $unavailableFolderCreate->setField('name', 'Unavailable Folder');
1497
        $unavailableFolder = $contentService->publishVersion(
1498
            $contentService->createContent(
1499
                $unavailableFolderCreate,
1500
                array($locationService->newLocationCreateStruct($restrictedFolder->contentInfo->mainLocationId))
1501
            )->versionInfo
1502
        );
1503
1504
        // this will be our test object, which will have all the relations (as source)
1505
        // and it is readable by the anonymous user
1506
        $testFolderCreate = $contentService->newContentCreateStruct(
1507
            $contenTypeService->loadContentTypeByIdentifier('folder'),
1508
            $mainLanguage
1509
        );
1510
        $testFolderCreate->setField('name', 'Test Folder');
1511
        $testFolderDraft = $contentService->createContent(
1512
            $testFolderCreate,
1513
            array($locationService->newLocationCreateStruct(2))
1514
        )->versionInfo;
1515
1516
        // add relations to test folder (as source)
1517
        // the first 2 will be read by the user
1518
        // and the other 2 wont
1519
        //
1520
        // create relation from Test Folder to Main Folder
1521
        $mainRelation = $contentService->addRelation(
1522
            $testFolderDraft,
1523
            $mainFolder->getVersionInfo()->getContentInfo()
1524
        );
1525
        // create relation from Test Folder to Available Folder
1526
        $availableRelation = $contentService->addRelation(
1527
            $testFolderDraft,
1528
            $availableFolder->getVersionInfo()->getContentInfo()
1529
        );
1530
        // create relation from Test Folder to Restricted Folder
1531
        $contentService->addRelation(
1532
            $testFolderDraft,
1533
            $restrictedFolder->getVersionInfo()->getContentInfo()
1534
        );
1535
        //create relation from Test Folder to Unavailable Folder
1536
        $contentService->addRelation(
1537
            $testFolderDraft,
1538
            $unavailableFolder->getVersionInfo()->getContentInfo()
1539
        );
1540
1541
        // publish Test Folder
1542
        $testFolder = $contentService->publishVersion($testFolderDraft);
1543
1544
        // set the current user to be an anonymous user since we want to test that
1545
        // if the user doesn't have access to an related object that object wont
1546
        // be loaded and no exception will be thrown
1547
        $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...
1548
1549
        // finaly load relations ( verify no exception is thrown )
1550
        $actualRelations = $contentService->loadRelations($testFolder->getVersionInfo());
1551
1552
        /* END: Use case */
1553
1554
        // assert results
1555
        // verify that the only expected relations are from the 2 readable objects
1556
        // Main Folder and Available Folder
1557
        $expectedRelations = array(
1558
            $mainRelation->destinationContentInfo->id => $mainRelation,
1559
            $availableRelation->destinationContentInfo->id => $availableRelation,
1560
        );
1561
1562
        // assert there are as many expected relations as actual ones
1563
        $this->assertEquals(
1564
            count($expectedRelations),
1565
            count($actualRelations),
1566
            "Expected '" . count($expectedRelations)
1567
            . "' relations found '" . count($actualRelations) . "'"
1568
        );
1569
1570
        // assert each relation
1571
        foreach ($actualRelations as $relation) {
1572
            $destination = $relation->destinationContentInfo;
1573
            $expected = $expectedRelations[$destination->id]->destinationContentInfo;
1574
            $this->assertNotEmpty($expected, "Non expected relation with '{$destination->id}' id found");
1575
            $this->assertEquals(
1576
                $expected->id,
1577
                $destination->id,
1578
                "Expected relation with '{$expected->id}' id found '{$destination->id}' id"
1579
            );
1580
            $this->assertEquals(
1581
                $expected->name,
1582
                $destination->name,
1583
                "Expected relation with '{$expected->name}' name found '{$destination->name}' name"
1584
            );
1585
1586
            // remove from list
1587
            unset($expectedRelations[$destination->id]);
1588
        }
1589
1590
        // verify all expected relations were found
1591
        $this->assertEquals(
1592
            0,
1593
            count($expectedRelations),
1594
            "Expected to find '" . (count($expectedRelations) + count($actualRelations))
1595
            . "' relations found '" . count($actualRelations) . "'"
1596
        );
1597
    }
1598
1599
    /**
1600
     * Test copying Content to the authorized Location (limited by policies).
1601
     */
1602
    public function testCopyContentToAuthorizedLocation()
1603
    {
1604
        $repository = $this->getRepository();
1605
        $contentService = $repository->getContentService();
1606
        $locationService = $repository->getLocationService();
1607
        $roleService = $repository->getRoleService();
1608
1609
        // Create and publish folders for the test case
1610
        $folderDraft = $this->createContentDraft('folder', 2, ['name' => 'Folder1']);
1611
        $contentService->publishVersion($folderDraft->versionInfo);
1612
        $authorizedFolderDraft = $this->createContentDraft('folder', 2, ['name' => 'AuthorizedFolder']);
1613
        $authorizedFolder = $contentService->publishVersion($authorizedFolderDraft->versionInfo);
1614
1615
        // Prepare Role for the test case
1616
        $roleIdentifier = 'authorized_folder';
1617
        $roleCreateStruct = $roleService->newRoleCreateStruct($roleIdentifier);
1618
        $locationLimitation = new LocationLimitation(
1619
            ['limitationValues' => [$authorizedFolder->contentInfo->mainLocationId]]
1620
        );
1621
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'read'));
1622
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'versionread'));
1623
        $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'manage_locations'));
1624
1625
        $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'create');
1626
        $policyCreateStruct->addLimitation($locationLimitation);
1627
        $roleCreateStruct->addPolicy($policyCreateStruct);
1628
1629
        // check if content/publish policy is available (@since 6.8)
1630
        $limitations = $roleService->getLimitationTypesByModuleFunction('content', 'publish');
1631
        if (array_key_exists('Node', $limitations)) {
1632
            $policyCreateStruct = $roleService->newPolicyCreateStruct('content', 'publish');
1633
            $policyCreateStruct->addLimitation($locationLimitation);
1634
            $roleCreateStruct->addPolicy($policyCreateStruct);
1635
        }
1636
1637
        $roleDraft = $roleService->createRole($roleCreateStruct);
1638
        $roleService->publishRoleDraft($roleDraft);
1639
1640
        // Create a user with that Role
1641
        $user = $this->createCustomUserVersion1('Users', $roleIdentifier);
1642
        $repository->getPermissionResolver()->setCurrentUserReference($user);
1643
1644
        // Test copying Content to the authorized Location
1645
        $contentService->copyContent(
1646
            $authorizedFolder->contentInfo,
1647
            $locationService->newLocationCreateStruct(
1648
                $authorizedFolder->contentInfo->mainLocationId
1649
            )
1650
        );
1651
    }
1652
}
1653