Completed
Push — master ( 0495e5...5f0df9 )
by André
25:05
created

testBuildViewWithoutContentReadPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Publish\Core\MVC\Symfony\View\Tests\Builder;
7
8
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
9
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
10
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
11
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
12
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
13
use eZ\Publish\Core\Helper\ContentInfoLocationLoader;
14
use eZ\Publish\Core\MVC\Exception\HiddenLocationException;
15
use eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder;
16
use eZ\Publish\Core\MVC\Symfony\View\Configurator;
17
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
18
use eZ\Publish\Core\MVC\Symfony\View\ParametersInjector;
19
use eZ\Publish\Core\Repository\Repository;
20
use eZ\Publish\Core\Repository\Values\Content\Content;
21
use eZ\Publish\Core\Repository\Values\Content\Location;
22
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
23
use Symfony\Component\HttpKernel\Controller\ControllerReference;
24
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 * @group mvc
29
 */
30
class ContentViewBuilderTest extends TestCase
31
{
32
    /** @var \eZ\Publish\API\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject */
33
    private $repository;
34
35
    /** @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface|\PHPUnit\Framework\MockObject\MockObject */
36
    private $authorizationChecker;
37
38
    /** @var \eZ\Publish\Core\MVC\Symfony\View\Configurator|\PHPUnit\Framework\MockObject\MockObject */
39
    private $viewConfigurator;
40
41
    /** @var \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector|\PHPUnit\Framework\MockObject\MockObject */
42
    private $parametersInjector;
43
44
    /** @var \eZ\Publish\Core\Helper\ContentInfoLocationLoader|\PHPUnit\Framework\MockObject\MockObject */
45
    private $contentInfoLocationLoader;
46
47
    /** @var \eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder|\PHPUnit\Framework\MockObject\MockObject */
48
    private $contentViewBuilder;
49
50
    public function setUp(): void
51
    {
52
        $this->repository = $this->getMockBuilder(Repository::class)->disableOriginalConstructor()->setMethods(['sudo'])->getMock();
53
        $this->authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
54
        $this->viewConfigurator = $this->getMockBuilder(Configurator::class)->getMock();
55
        $this->parametersInjector = $this->getMockBuilder(ParametersInjector::class)->getMock();
56
        $this->contentInfoLocationLoader = $this->getMockBuilder(ContentInfoLocationLoader::class)->getMock();
57
        $this->contentViewBuilder = new ContentViewBuilder(
58
            $this->repository,
59
            $this->authorizationChecker,
60
            $this->viewConfigurator,
61
            $this->parametersInjector,
62
            $this->contentInfoLocationLoader
63
        );
64
    }
65
66
    public function testMatches(): void
67
    {
68
        $this->assertTrue($this->contentViewBuilder->matches('ez_content:55'));
69
        $this->assertFalse($this->contentViewBuilder->matches('dummy_value'));
70
    }
71
72
    public function testBuildViewWithoutLocationIdAndContentId(): void
73
    {
74
        $parameters = [
75
            'viewType' => 'full',
76
            '_controller' => 'ez_content:viewContent',
77
        ];
78
79
        $this->expectException(InvalidArgumentException::class);
80
81
        $this->contentViewBuilder->buildView($parameters);
82
    }
83
84
    public function testBuildViewWithInvalidLocationId(): void
85
    {
86
        $parameters = [
87
            'viewType' => 'full',
88
            '_controller' => 'ez_content:viewContent',
89
            'locationId' => 865,
90
        ];
91
92
        $this->repository
93
            ->expects($this->once())
94
            ->method('sudo')
95
            ->willThrowException(new NotFoundException('location', 865));
96
97
        $this->expectException(APINotFoundException::class);
98
99
        $this->contentViewBuilder->buildView($parameters);
100
    }
101
102
    public function testBuildViewWithHiddenLocation(): void
103
    {
104
        $parameters = [
105
            'viewType' => 'full',
106
            '_controller' => 'ez_content:viewContent',
107
            'locationId' => 2,
108
        ];
109
110
        $location = new Location(['invisible' => true]);
111
112
        $this->repository
113
            ->expects($this->once())
114
            ->method('sudo')
115
            ->willReturn($location);
116
117
        $this->expectException(HiddenLocationException::class);
118
119
        $this->contentViewBuilder->buildView($parameters);
120
    }
121
122
    public function testBuildViewWithoutContentReadPermission(): void
123
    {
124
        $location = new Location(
125
            [
126
                'invisible' => false,
127
                'content' => new Content([
128
                    'versionInfo' => new VersionInfo([
129
                        'contentInfo' => new ContentInfo(),
130
                    ]),
131
                ]),
132
            ]
133
        );
134
135
        $parameters = [
136
            'viewType' => 'full',
137
            '_controller' => 'ez_content:viewContent',
138
            'locationId' => 2,
139
        ];
140
141
        // It's call for LocationService::loadLocation()
142
        $this->repository
143
            ->expects($this->once())
144
            ->method('sudo')
145
            ->willReturn($location);
146
147
        $this->authorizationChecker
148
            ->expects($this->once())
149
            ->method('isGranted')
150
            ->willReturn(false);
151
152
        $this->expectException(UnauthorizedException::class);
153
154
        $this->contentViewBuilder->buildView($parameters);
155
    }
156
157
    public function testBuildEmbedViewWithoutContentViewEmbedPermission(): void
158
    {
159
        $location = new Location(
160
            [
161
                'invisible' => false,
162
                'contentInfo' => new ContentInfo([
163
                    'id' => 120,
164
                ]),
165
                'content' => new Content([
166
                    'versionInfo' => new VersionInfo([
167
                        'contentInfo' => new ContentInfo([
168
                            'id' => 91,
169
                        ]),
170
                    ]),
171
                ]),
172
            ]
173
        );
174
175
        $parameters = [
176
            'viewType' => 'embed',
177
            '_controller' => 'ez_content:viewContent',
178
            'locationId' => 2,
179
        ];
180
181
        // It's call for LocationService::loadLocation()
182
        $this->repository
183
            ->expects($this->once())
184
            ->method('sudo')
185
            ->willReturn($location);
186
187
        $this->authorizationChecker
188
            ->expects($this->at(0))
189
            ->method('isGranted')
190
            ->willReturn(false);
191
192
        $this->authorizationChecker
193
            ->expects($this->at(1))
194
            ->method('isGranted')
195
            ->willReturn(false);
196
197
        $this->expectException(UnauthorizedException::class);
198
199
        $this->contentViewBuilder->buildView($parameters);
200
    }
201
202
    public function testBuildViewWithContentWhichDoesNotBelongsToLocation(): void
203
    {
204
        $location = new Location(
205
            [
206
                'invisible' => false,
207
                'contentInfo' => new ContentInfo([
208
                   'id' => 120,
209
                ]),
210
                'content' => new Content([
211
                    'versionInfo' => new VersionInfo([
212
                        'contentInfo' => new ContentInfo([
213
                            'id' => 91,
214
                        ]),
215
                    ]),
216
                ]),
217
            ]
218
        );
219
220
        $parameters = [
221
            'viewType' => 'full',
222
            '_controller' => 'ez_content:viewContent',
223
            'locationId' => 2,
224
        ];
225
226
        // It's call for LocationService::loadLocation()
227
        $this->repository
228
            ->expects($this->once())
229
            ->method('sudo')
230
            ->willReturn($location);
231
232
        $this->authorizationChecker
233
            ->expects($this->at(0))
234
            ->method('isGranted')
235
            ->willReturn(true);
236
237
        $this->expectException(InvalidArgumentException::class);
238
239
        $this->contentViewBuilder->buildView($parameters);
240
    }
241
242 View Code Duplication
    public function testBuildViewWithDeprecatedControllerReference(): void
243
    {
244
        $contentInfo = new ContentInfo(['id' => 120]);
245
        $content = new Content([
246
            'versionInfo' => new VersionInfo([
247
                'contentInfo' => $contentInfo,
248
            ]),
249
        ]);
250
        $location = new Location(
251
            [
252
                'invisible' => false,
253
                'contentInfo' => $contentInfo,
254
                'content' => $content,
255
            ]
256
        );
257
258
        $expectedView = new ContentView(null, [], 'full');
259
        $expectedView->setControllerReference(new ControllerReference('ez_content:viewAction'));
260
        $expectedView->setLocation($location);
261
        $expectedView->setContent($content);
262
263
        $parameters = [
264
            'viewType' => 'full',
265
            '_controller' => 'ez_content:viewLocation',
266
            'locationId' => 2,
267
        ];
268
269
        $this->repository
270
            ->expects($this->once())
271
            ->method('sudo')
272
            ->willReturn($location);
273
274
        $this->authorizationChecker
275
            ->expects($this->at(0))
276
            ->method('isGranted')
277
            ->willReturn(true);
278
279
        $this->assertEquals($expectedView, $this->contentViewBuilder->buildView($parameters));
280
    }
281
282 View Code Duplication
    public function testBuildView(): void
283
    {
284
        $contentInfo = new ContentInfo(['id' => 120]);
285
        $content = new Content([
286
            'versionInfo' => new VersionInfo([
287
                'contentInfo' => $contentInfo,
288
            ]),
289
        ]);
290
        $location = new Location(
291
            [
292
                'invisible' => false,
293
                'contentInfo' => $contentInfo,
294
                'content' => $content,
295
            ]
296
        );
297
298
        $expectedView = new ContentView(null, [], 'full');
299
        $expectedView->setControllerReference(new ControllerReference('ez_content:viewAction'));
300
        $expectedView->setLocation($location);
301
        $expectedView->setContent($content);
302
303
        $parameters = [
304
            'viewType' => 'full',
305
            '_controller' => 'ez_content:viewContent',
306
            'locationId' => 2,
307
        ];
308
309
        $this->repository
310
            ->expects($this->once())
311
            ->method('sudo')
312
            ->willReturn($location);
313
314
        $this->authorizationChecker
315
            ->expects($this->at(0))
316
            ->method('isGranted')
317
            ->willReturn(true);
318
319
        $this->assertEquals($expectedView, $this->contentViewBuilder->buildView($parameters));
320
    }
321
}
322