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