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