Completed
Push — master ( 2fd89b...34ee12 )
by André
20:07 queued 12s
created

testCreateContentThrowsUnauthorizedExceptionWithSecondParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
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
    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 loadContentInfoByRemoteId() method.
144
     *
145
     * @see \eZ\Publish\API\Repository\ContentService::loadContentInfoByRemoteId()
146
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
147
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentInfoByRemoteId
148
     */
149 View Code Duplication
    public function testLoadContentInfoByRemoteIdThrowsUnauthorizedException()
150
    {
151
        $repository = $this->getRepository();
152
153
        /* BEGIN: Use Case */
154
        // RemoteId of the "Anonymous User" in an eZ Publish demo installation
155
        $anonymousRemoteId = 'faaeb9be3bd98ed09f606fc16d144eca';
156
157
        $contentService = $repository->getContentService();
158
159
        $pseudoEditor = $this->createAnonymousWithEditorRole();
160
161
        // Set restricted editor user
162
        $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...
163
164
        // This call will fail with a "UnauthorizedException"
165
        $contentService->loadContentInfoByRemoteId($anonymousRemoteId);
166
        /* END: Use Case */
167
    }
168
169
    /**
170
     * Test for the loadVersionInfo() method.
171
     *
172
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo()
173
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
174
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfo
175
     */
176
    public function testLoadVersionInfoThrowsUnauthorizedException()
177
    {
178
        $repository = $this->getRepository();
179
180
        $anonymousUserId = $this->generateId('user', 10);
181
        /* BEGIN: Use Case */
182
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
183
        // demo installation
184
185
        $contentService = $repository->getContentService();
186
187
        // Load the ContentInfo for "Anonymous User"
188
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
189
190
        $pseudoEditor = $this->createAnonymousWithEditorRole();
191
192
        // Set restricted editor user
193
        $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...
194
195
        // This call will fail with a "UnauthorizedException"
196
        $contentService->loadVersionInfo($contentInfo);
197
        /* END: Use Case */
198
    }
199
200
    /**
201
     * Test for the loadVersionInfo() method.
202
     *
203
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfo($contentInfo, $versionNo)
204
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
205
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoWithSecondParameter
206
     */
207
    public function testLoadVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
208
    {
209
        $repository = $this->getRepository();
210
211
        $anonymousUserId = $this->generateId('user', 10);
212
        /* BEGIN: Use Case */
213
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
214
        // demo installation
215
216
        $contentService = $repository->getContentService();
217
218
        // Load the ContentInfo for "Anonymous User"
219
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
220
221
        $pseudoEditor = $this->createAnonymousWithEditorRole();
222
223
        // Set restricted editor user
224
        $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...
225
226
        // This call will fail with a "UnauthorizedException"
227
        $contentService->loadVersionInfo($contentInfo, 2);
228
        /* END: Use Case */
229
    }
230
231
    /**
232
     * Test for the loadVersionInfoById() method.
233
     *
234
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById()
235
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
236
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
237
     */
238 View Code Duplication
    public function testLoadVersionInfoByIdThrowsUnauthorizedException()
239
    {
240
        $repository = $this->getRepository();
241
242
        $anonymousUserId = $this->generateId('user', 10);
243
        /* BEGIN: Use Case */
244
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
245
        // demo installation
246
247
        $contentService = $repository->getContentService();
248
249
        $pseudoEditor = $this->createAnonymousWithEditorRole();
250
251
        // Set restricted editor user
252
        $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...
253
254
        // This call will fail with a "UnauthorizedException"
255
        $contentService->loadVersionInfoById($anonymousUserId);
256
        /* END: Use Case */
257
    }
258
259
    /**
260
     * Test for the loadVersionInfoById() method.
261
     *
262
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
263
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
264
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoByIdWithSecondParameter
265
     */
266 View Code Duplication
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionWithSecondParameter()
267
    {
268
        $repository = $this->getRepository();
269
270
        $anonymousUserId = $this->generateId('user', 10);
271
        /* BEGIN: Use Case */
272
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
273
        // demo installation
274
275
        $contentService = $repository->getContentService();
276
277
        $pseudoEditor = $this->createAnonymousWithEditorRole();
278
279
        // Set restricted editor user
280
        $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...
281
282
        // This call will fail with a "UnauthorizedException"
283
        $contentService->loadVersionInfoById($anonymousUserId, 2);
284
        /* END: Use Case */
285
    }
286
287
    /**
288
     * Test for the loadVersionInfoById() method.
289
     *
290
     * @see \eZ\Publish\API\Repository\ContentService::loadVersionInfoById($contentId, $versionNo)
291
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
292
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadVersionInfoById
293
     */
294
    public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionForFirstDraft()
295
    {
296
        $repository = $this->getRepository();
297
298
        $contentService = $repository->getContentService();
299
300
        $anonymousUserId = $this->generateId('user', 10);
301
        /* BEGIN: Use Case */
302
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
303
        // demo installation
304
        $contentDraft = $this->createContentDraftVersion1();
305
306
        // Load the user service
307
        $userService = $repository->getUserService();
308
309
        // Set anonymous user
310
        $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...
311
312
        // This call will fail with a "UnauthorizedException"
313
        $contentService->loadVersionInfoById(
314
            $contentDraft->id,
315
            $contentDraft->contentInfo->currentVersionNo
316
        );
317
        /* END: Use Case */
318
    }
319
320
    /**
321
     * Test for the loadContentByContentInfo() method.
322
     *
323
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo()
324
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
325
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfo
326
     */
327
    public function testLoadContentByContentInfoThrowsUnauthorizedException()
328
    {
329
        $repository = $this->getRepository();
330
331
        $anonymousUserId = $this->generateId('user', 10);
332
        /* BEGIN: Use Case */
333
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
334
        // demo installation
335
336
        $contentService = $repository->getContentService();
337
338
        // Load the ContentInfo for "Anonymous User"
339
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
340
341
        $pseudoEditor = $this->createAnonymousWithEditorRole();
342
343
        // Set restricted editor user
344
        $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...
345
346
        // This call will fail with a "UnauthorizedException"
347
        $contentService->loadContentByContentInfo($contentInfo);
348
        /* END: Use Case */
349
    }
350
351
    /**
352
     * Test for the loadContentByContentInfo() method.
353
     *
354
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages)
355
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
356
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithLanguageParameters
357
     */
358
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithSecondParameter()
359
    {
360
        $repository = $this->getRepository();
361
362
        $anonymousUserId = $this->generateId('user', 10);
363
        /* BEGIN: Use Case */
364
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
365
        // demo installation
366
367
        $contentService = $repository->getContentService();
368
369
        // Load the ContentInfo for "Anonymous User"
370
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
371
372
        $pseudoEditor = $this->createAnonymousWithEditorRole();
373
374
        // Set restricted editor user
375
        $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...
376
377
        // This call will fail with a "UnauthorizedException"
378
        $contentService->loadContentByContentInfo($contentInfo, array('eng-US'));
379
        /* END: Use Case */
380
    }
381
382
    /**
383
     * Test for the loadContentByContentInfo() method.
384
     *
385
     * @see \eZ\Publish\API\Repository\ContentService::loadContentByContentInfo($contentInfo, $languages, $versionNo)
386
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
387
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByContentInfoWithVersionNumberParameter
388
     */
389
    public function testLoadContentByContentInfoThrowsUnauthorizedExceptionWithThirdParameter()
390
    {
391
        $repository = $this->getRepository();
392
393
        $anonymousUserId = $this->generateId('user', 10);
394
        /* BEGIN: Use Case */
395
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
396
        // demo installation
397
398
        $contentService = $repository->getContentService();
399
400
        // Load the ContentInfo for "Anonymous User"
401
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
402
403
        $pseudoEditor = $this->createAnonymousWithEditorRole();
404
405
        // Set restricted editor user
406
        $repository->setCurrentUser($pseudoEditor);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

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

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

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

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

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

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