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