Completed
Push — master ( 256b3e...67d26f )
by Łukasz
13:35
created

testBuildViewWithTranslatedContentWithoutLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 42
rs 9.248
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\ContentService;
11
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
12
use eZ\Publish\API\Repository\PermissionResolver;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
15
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
16
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
17
use eZ\Publish\Core\Helper\ContentInfoLocationLoader;
18
use eZ\Publish\Core\MVC\Exception\HiddenLocationException;
19
use eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder;
20
use eZ\Publish\Core\MVC\Symfony\View\Configurator;
21
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
22
use eZ\Publish\Core\MVC\Symfony\View\ParametersInjector;
23
use eZ\Publish\Core\Repository\Repository;
24
use eZ\Publish\Core\Repository\Values\Content\Content;
25
use eZ\Publish\Core\Repository\Values\Content\Location;
26
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
27
use Symfony\Component\HttpFoundation\RequestStack;
28
use PHPUnit\Framework\TestCase;
29
30
/**
31
 * @group mvc
32
 */
33
class ContentViewBuilderTest extends TestCase
34
{
35
    /** @var \eZ\Publish\API\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject */
36
    private $repository;
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
    /** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */
51
    private $permissionResolver;
52
53
    /** @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit\Framework\MockObject\MockObject */
54
    private $requestStack;
55
56
    protected function setUp(): void
57
    {
58
        $this->repository = $this
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

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...
59
            ->getMockBuilder(Repository::class)
60
            ->disableOriginalConstructor()
61
            ->setMethods([
62
                'sudo',
63
                'getPermissionResolver',
64
                'getLocationService',
65
                'getContentService',
66
            ])
67
            ->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->permissionResolver = $this->getMockBuilder(PermissionResolver::class)->getMock();
72
        $this->requestStack = $this->getMockBuilder(RequestStack::class)->getMock();
73
        $this->repository
74
            ->expects($this->any())
75
            ->method('getPermissionResolver')
76
            ->willReturn($this->permissionResolver);
77
78
        $this->contentViewBuilder = new ContentViewBuilder(
79
            $this->repository,
80
            $this->viewConfigurator,
81
            $this->parametersInjector,
82
            $this->requestStack,
83
            $this->contentInfoLocationLoader
84
        );
85
    }
86
87
    public function testMatches(): void
88
    {
89
        $this->assertTrue($this->contentViewBuilder->matches('ez_content:55'));
90
        $this->assertFalse($this->contentViewBuilder->matches('dummy_value'));
91
    }
92
93
    public function testBuildViewWithoutLocationIdAndContentId(): void
94
    {
95
        $parameters = [
96
            'viewType' => 'full',
97
            '_controller' => 'ez_content:viewContent',
98
        ];
99
100
        $this->expectException(InvalidArgumentException::class);
101
102
        $this->contentViewBuilder->buildView($parameters);
103
    }
104
105
    public function testBuildViewWithInvalidLocationId(): void
106
    {
107
        $parameters = [
108
            'viewType' => 'full',
109
            '_controller' => 'ez_content:viewContent',
110
            'locationId' => 865,
111
        ];
112
113
        $this->repository
114
            ->expects($this->once())
115
            ->method('sudo')
116
            ->willThrowException(new NotFoundException('location', 865));
117
118
        $this->expectException(APINotFoundException::class);
119
120
        $this->contentViewBuilder->buildView($parameters);
121
    }
122
123
    public function testBuildViewWithHiddenLocation(): void
124
    {
125
        $parameters = [
126
            'viewType' => 'full',
127
            '_controller' => 'ez_content:viewContent',
128
            'locationId' => 2,
129
        ];
130
131
        $location = new Location(['invisible' => true]);
132
133
        $this->repository
134
            ->expects($this->once())
135
            ->method('sudo')
136
            ->willReturn($location);
137
138
        $this->expectException(HiddenLocationException::class);
139
140
        $this->contentViewBuilder->buildView($parameters);
141
    }
142
143
    public function testBuildViewWithoutContentReadPermission(): void
144
    {
145
        $location = new Location(
146
            [
147
                'invisible' => false,
148
                'content' => new Content([
149
                    'versionInfo' => new VersionInfo([
150
                        'contentInfo' => new ContentInfo(),
151
                    ]),
152
                ]),
153
            ]
154
        );
155
156
        $parameters = [
157
            'viewType' => 'full',
158
            '_controller' => 'ez_content:viewContent',
159
            'locationId' => 2,
160
        ];
161
162
        // It's call for LocationService::loadLocation()
163
        $this->repository
164
            ->expects($this->once())
165
            ->method('sudo')
166
            ->willReturn($location);
167
168
        $this->permissionResolver
169
            ->expects($this->any())
170
            ->method('canUser')
171
            ->willReturn(false);
172
173
        $this->expectException(UnauthorizedException::class);
174
175
        $this->contentViewBuilder->buildView($parameters);
176
    }
177
178
    public function testBuildEmbedViewWithoutContentViewEmbedPermission(): void
179
    {
180
        $location = new Location(
181
            [
182
                'invisible' => false,
183
                'contentInfo' => new ContentInfo([
184
                    'id' => 120,
185
                ]),
186
                'content' => new Content([
187
                    'versionInfo' => new VersionInfo([
188
                        'contentInfo' => new ContentInfo([
189
                            'id' => 91,
190
                        ]),
191
                    ]),
192
                ]),
193
            ]
194
        );
195
196
        $parameters = [
197
            'viewType' => 'embed',
198
            '_controller' => 'ez_content:viewContent',
199
            'locationId' => 2,
200
        ];
201
202
        // It's call for LocationService::loadLocation()
203
        $this->repository
204
            ->expects($this->once())
205
            ->method('sudo')
206
            ->willReturn($location);
207
208
        $this->permissionResolver
209
            ->expects($this->at(0))
210
            ->method('canUser')
211
            ->willReturn(false);
212
213
        $this->permissionResolver
214
            ->expects($this->at(1))
215
            ->method('canUser')
216
            ->willReturn(false);
217
218
        $this->expectException(UnauthorizedException::class);
219
220
        $this->contentViewBuilder->buildView($parameters);
221
    }
222
223
    public function testBuildViewWithContentWhichDoesNotBelongsToLocation(): void
224
    {
225
        $location = new Location(
226
            [
227
                'invisible' => false,
228
                'contentInfo' => new ContentInfo([
229
                   'id' => 120,
230
                ]),
231
                'content' => new Content([
232
                    'versionInfo' => new VersionInfo([
233
                        'contentInfo' => new ContentInfo([
234
                            'id' => 91,
235
                        ]),
236
                    ]),
237
                ]),
238
            ]
239
        );
240
241
        $parameters = [
242
            'viewType' => 'full',
243
            '_controller' => 'ez_content:viewContent',
244
            'locationId' => 2,
245
        ];
246
247
        // It's call for LocationService::loadLocation()
248
        $this->repository
249
            ->expects($this->once())
250
            ->method('sudo')
251
            ->willReturn($location);
252
253
        $this->permissionResolver
254
            ->expects($this->at(0))
255
            ->method('canUser')
256
            ->willReturn(true);
257
258
        $this->expectException(InvalidArgumentException::class);
259
260
        $this->contentViewBuilder->buildView($parameters);
261
    }
262
263
    public function testBuildViewWithTranslatedContentWithoutLocation(): void
264
    {
265
        $contentInfo = new ContentInfo(['id' => 120, 'mainLanguageCode' => 'eng-GB']);
266
        $content = new Content([
267
            'versionInfo' => new VersionInfo([
268
                'contentInfo' => $contentInfo,
269
            ]),
270
        ]);
271
272
        $parameters = [
273
            'viewType' => 'full',
274
            '_controller' => 'ez_content:viewContent',
275
            'contentId' => 120,
276
            'languageCode' => 'eng-GB',
277
        ];
278
279
        $contentServiceMock = $this
280
            ->getMockBuilder(ContentService::class)
281
            ->disableOriginalConstructor()
282
            ->getMock();
283
284
        $contentServiceMock
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
285
            ->method('loadContent')
286
            ->with(120, ['eng-GB'])
287
            ->willReturn($content);
288
289
        // No call for LocationService::loadLocation()
290
        $this->repository
291
            ->expects($this->never())
292
            ->method('sudo');
293
294
        $this->repository
295
            ->method('getContentService')
296
            ->willReturn($contentServiceMock);
297
298
        $this->contentViewBuilder->buildView($parameters);
299
300
        $expectedView = new ContentView(null, [], 'full');
301
        $expectedView->setContent($content);
302
303
        $this->assertEquals($expectedView, $this->contentViewBuilder->buildView($parameters));
304
    }
305
306
    public function testBuildView(): void
307
    {
308
        $contentInfo = new ContentInfo(['id' => 120]);
309
        $content = new Content([
310
            'versionInfo' => new VersionInfo([
311
                'contentInfo' => $contentInfo,
312
            ]),
313
        ]);
314
        $location = new Location(
315
            [
316
                'invisible' => false,
317
                'contentInfo' => $contentInfo,
318
                'content' => $content,
319
            ]
320
        );
321
322
        $expectedView = new ContentView(null, [], 'full');
323
        $expectedView->setLocation($location);
324
        $expectedView->setContent($content);
325
326
        $parameters = [
327
            'viewType' => 'full',
328
            '_controller' => 'ez_content:viewAction',
329
            'locationId' => 2,
330
        ];
331
332
        $this->repository
333
            ->expects($this->once())
334
            ->method('sudo')
335
            ->willReturn($location);
336
337
        $this->permissionResolver
338
            ->expects($this->at(0))
339
            ->method('canUser')
340
            ->willReturn(true);
341
342
        $this->assertEquals($expectedView, $this->contentViewBuilder->buildView($parameters));
343
    }
344
}
345