|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\Repository\Tests\LocationResolver; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Exceptions\BadStateException; |
|
12
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
|
13
|
|
|
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; |
|
14
|
|
|
use eZ\Publish\API\Repository\LocationService; |
|
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
|
16
|
|
|
use eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver; |
|
17
|
|
|
use eZ\Publish\Core\Repository\Values\Content\Location; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
|
|
20
|
|
|
final class PermissionAwareLocationResolverTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var \eZ\Publish\API\Repository\LocationService */ |
|
23
|
|
|
private $locationService; |
|
24
|
|
|
|
|
25
|
|
|
/** @var \eZ\Publish\Core\Repository\LocationResolver\LocationResolver */ |
|
26
|
|
|
private $locationResolver; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->locationService = $this->createMock(LocationService::class); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$this->locationResolver = new PermissionAwareLocationResolver($this->locationService); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Test for the resolveLocation() method. |
|
37
|
|
|
* |
|
38
|
|
|
* @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testResolveMainLocation(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$contentInfo = new ContentInfo(['mainLocationId' => 42]); |
|
43
|
|
|
$location = new Location(['id' => 42]); |
|
44
|
|
|
|
|
45
|
|
|
// User has access to the main Location |
|
46
|
|
|
$this->locationService |
|
|
|
|
|
|
47
|
|
|
->method('loadLocation') |
|
48
|
|
|
->willReturn($location); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame($location, $this->locationResolver->resolveLocation($contentInfo)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Test for the resolveLocation() method. |
|
55
|
|
|
* |
|
56
|
|
|
* @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testResolveSecondaryLocation(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$contentInfo = new ContentInfo(['mainLocationId' => 42]); |
|
61
|
|
|
$location1 = new Location(['id' => 43]); |
|
62
|
|
|
$location2 = new Location(['id' => 44]); |
|
63
|
|
|
|
|
64
|
|
|
// User doesn't have access to main location but to the third Content's location |
|
65
|
|
|
$this->locationService |
|
|
|
|
|
|
66
|
|
|
->method('loadLocation') |
|
67
|
|
|
->willThrowException($this->createMock(UnauthorizedException::class)); |
|
68
|
|
|
|
|
69
|
|
|
$this->locationService |
|
|
|
|
|
|
70
|
|
|
->method('loadLocations') |
|
71
|
|
|
->willReturn([$location1, $location2]); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertSame($location1, $this->locationResolver->resolveLocation($contentInfo)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Test for the resolveLocation() method when Locations don't exist. |
|
78
|
|
|
* |
|
79
|
|
|
* @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation |
|
80
|
|
|
*/ |
|
81
|
|
|
public function testExpectNotFoundExceptionWhenLocationDoesNotExist(): void |
|
82
|
|
|
{ |
|
83
|
|
|
$contentInfo = new ContentInfo(['mainLocationId' => 42]); |
|
84
|
|
|
|
|
85
|
|
|
$this->locationService |
|
|
|
|
|
|
86
|
|
|
->method('loadLocation') |
|
87
|
|
|
->willThrowException($this->createMock(NotFoundException::class)); |
|
88
|
|
|
|
|
89
|
|
|
$this->locationService |
|
|
|
|
|
|
90
|
|
|
->method('loadLocations') |
|
91
|
|
|
->willReturn([]); |
|
92
|
|
|
|
|
93
|
|
|
$this->expectException(NotFoundException::class); |
|
94
|
|
|
|
|
95
|
|
|
$this->locationResolver->resolveLocation($contentInfo); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Test for the resolveLocation() method when ContentInfo's mainLocationId is null. |
|
100
|
|
|
* |
|
101
|
|
|
* @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testExpectNotFoundExceptionWhenMainLocationIdIsNull(): void |
|
104
|
|
|
{ |
|
105
|
|
|
$contentInfo = new ContentInfo(['mainLocationId' => null]); |
|
106
|
|
|
|
|
107
|
|
|
$this->expectException(NotFoundException::class); |
|
108
|
|
|
|
|
109
|
|
|
$this->locationResolver->resolveLocation($contentInfo); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Test for the resolveLocation() method when Location is not yet published. |
|
114
|
|
|
* |
|
115
|
|
|
* @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testExpectBadStateExceptionWhenContentNotYetPublished(): void |
|
118
|
|
|
{ |
|
119
|
|
|
$contentInfo = new ContentInfo(['mainLocationId' => 42, 'status' => ContentInfo::STATUS_DRAFT]); |
|
120
|
|
|
|
|
121
|
|
|
$this->locationService |
|
|
|
|
|
|
122
|
|
|
->method('loadLocation') |
|
123
|
|
|
->willThrowException($this->createMock(NotFoundException::class)); |
|
124
|
|
|
|
|
125
|
|
|
$this->locationService |
|
|
|
|
|
|
126
|
|
|
->method('loadLocations') |
|
127
|
|
|
->willThrowException($this->createMock(BadStateException::class)); |
|
128
|
|
|
|
|
129
|
|
|
$this->expectException(BadStateException::class); |
|
130
|
|
|
|
|
131
|
|
|
$this->locationResolver->resolveLocation($contentInfo); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..