Completed
Push — ezp-30882-thumbnail-strategy-i... ( b3e424...83a23b )
by
unknown
13:58
created

ContentViewBuilderTest::testBuildViewWithDeprecatedControllerReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 39
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 39
loc 39
rs 9.296
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\HttpFoundation\RequestStack;
27
use PHPUnit\Framework\TestCase;
28
29
/**
30
 * @group mvc
31
 */
32
class ContentViewBuilderTest extends TestCase
33
{
34
    /** @var \eZ\Publish\API\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject */
35
    private $repository;
36
37
    /** @var \eZ\Publish\Core\MVC\Symfony\View\Configurator|\PHPUnit\Framework\MockObject\MockObject */
38
    private $viewConfigurator;
39
40
    /** @var \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector|\PHPUnit\Framework\MockObject\MockObject */
41
    private $parametersInjector;
42
43
    /** @var \eZ\Publish\Core\Helper\ContentInfoLocationLoader|\PHPUnit\Framework\MockObject\MockObject */
44
    private $contentInfoLocationLoader;
45
46
    /** @var \eZ\Publish\Core\MVC\Symfony\View\Builder\ContentViewBuilder|\PHPUnit\Framework\MockObject\MockObject */
47
    private $contentViewBuilder;
48
49
    /** @var \eZ\Publish\API\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */
50
    private $permissionResolver;
51
52
    /** @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit\Framework\MockObject\MockObject */
53
    private $requestStack;
54
55
    protected function setUp(): void
56
    {
57
        $this->repository = $this->getMockBuilder(Repository::class)->disableOriginalConstructor()->setMethods(['sudo', 'getPermissionResolver'])->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...
58
        $this->viewConfigurator = $this->getMockBuilder(Configurator::class)->getMock();
59
        $this->parametersInjector = $this->getMockBuilder(ParametersInjector::class)->getMock();
60
        $this->contentInfoLocationLoader = $this->getMockBuilder(ContentInfoLocationLoader::class)->getMock();
61
        $this->permissionResolver = $this->getMockBuilder(PermissionResolver::class)->getMock();
62
        $this->requestStack = $this->getMockBuilder(RequestStack::class)->getMock();
63
        $this->repository
64
            ->expects($this->any())
65
            ->method('getPermissionResolver')
66
            ->willReturn($this->permissionResolver);
67
68
        $this->contentViewBuilder = new ContentViewBuilder(
69
            $this->repository,
70
            $this->viewConfigurator,
71
            $this->parametersInjector,
72
            $this->requestStack,
73
            $this->contentInfoLocationLoader
74
        );
75
    }
76
77
    public function testMatches(): void
78
    {
79
        $this->assertTrue($this->contentViewBuilder->matches('ez_content:55'));
80
        $this->assertFalse($this->contentViewBuilder->matches('dummy_value'));
81
    }
82
83
    public function testBuildViewWithoutLocationIdAndContentId(): void
84
    {
85
        $parameters = [
86
            'viewType' => 'full',
87
            '_controller' => 'ez_content:viewContent',
88
        ];
89
90
        $this->expectException(InvalidArgumentException::class);
91
92
        $this->contentViewBuilder->buildView($parameters);
93
    }
94
95
    public function testBuildViewWithInvalidLocationId(): void
96
    {
97
        $parameters = [
98
            'viewType' => 'full',
99
            '_controller' => 'ez_content:viewContent',
100
            'locationId' => 865,
101
        ];
102
103
        $this->repository
104
            ->expects($this->once())
105
            ->method('sudo')
106
            ->willThrowException(new NotFoundException('location', 865));
107
108
        $this->expectException(APINotFoundException::class);
109
110
        $this->contentViewBuilder->buildView($parameters);
111
    }
112
113
    public function testBuildViewWithHiddenLocation(): void
114
    {
115
        $parameters = [
116
            'viewType' => 'full',
117
            '_controller' => 'ez_content:viewContent',
118
            'locationId' => 2,
119
        ];
120
121
        $location = new Location(['invisible' => true]);
122
123
        $this->repository
124
            ->expects($this->once())
125
            ->method('sudo')
126
            ->willReturn($location);
127
128
        $this->expectException(HiddenLocationException::class);
129
130
        $this->contentViewBuilder->buildView($parameters);
131
    }
132
133
    public function testBuildViewWithoutContentReadPermission(): void
134
    {
135
        $location = new Location(
136
            [
137
                'invisible' => false,
138
                'content' => new Content([
139
                    'versionInfo' => new VersionInfo([
140
                        'contentInfo' => new ContentInfo(),
141
                    ]),
142
                ]),
143
            ]
144
        );
145
146
        $parameters = [
147
            'viewType' => 'full',
148
            '_controller' => 'ez_content:viewContent',
149
            'locationId' => 2,
150
        ];
151
152
        // It's call for LocationService::loadLocation()
153
        $this->repository
154
            ->expects($this->once())
155
            ->method('sudo')
156
            ->willReturn($location);
157
158
        $this->permissionResolver
159
            ->expects($this->any())
160
            ->method('canUser')
161
            ->willReturn(false);
162
163
        $this->expectException(UnauthorizedException::class);
164
165
        $this->contentViewBuilder->buildView($parameters);
166
    }
167
168
    public function testBuildEmbedViewWithoutContentViewEmbedPermission(): void
169
    {
170
        $location = new Location(
171
            [
172
                'invisible' => false,
173
                'contentInfo' => new ContentInfo([
174
                    'id' => 120,
175
                ]),
176
                'content' => new Content([
177
                    'versionInfo' => new VersionInfo([
178
                        'contentInfo' => new ContentInfo([
179
                            'id' => 91,
180
                        ]),
181
                    ]),
182
                ]),
183
            ]
184
        );
185
186
        $parameters = [
187
            'viewType' => 'embed',
188
            '_controller' => 'ez_content:viewContent',
189
            'locationId' => 2,
190
        ];
191
192
        // It's call for LocationService::loadLocation()
193
        $this->repository
194
            ->expects($this->once())
195
            ->method('sudo')
196
            ->willReturn($location);
197
198
        $this->permissionResolver
199
            ->expects($this->at(0))
200
            ->method('canUser')
201
            ->willReturn(false);
202
203
        $this->permissionResolver
204
            ->expects($this->at(1))
205
            ->method('canUser')
206
            ->willReturn(false);
207
208
        $this->expectException(UnauthorizedException::class);
209
210
        $this->contentViewBuilder->buildView($parameters);
211
    }
212
213
    public function testBuildViewWithContentWhichDoesNotBelongsToLocation(): void
214
    {
215
        $location = new Location(
216
            [
217
                'invisible' => false,
218
                'contentInfo' => new ContentInfo([
219
                   'id' => 120,
220
                ]),
221
                'content' => new Content([
222
                    'versionInfo' => new VersionInfo([
223
                        'contentInfo' => new ContentInfo([
224
                            'id' => 91,
225
                        ]),
226
                    ]),
227
                ]),
228
            ]
229
        );
230
231
        $parameters = [
232
            'viewType' => 'full',
233
            '_controller' => 'ez_content:viewContent',
234
            'locationId' => 2,
235
        ];
236
237
        // It's call for LocationService::loadLocation()
238
        $this->repository
239
            ->expects($this->once())
240
            ->method('sudo')
241
            ->willReturn($location);
242
243
        $this->permissionResolver
244
            ->expects($this->at(0))
245
            ->method('canUser')
246
            ->willReturn(true);
247
248
        $this->expectException(InvalidArgumentException::class);
249
250
        $this->contentViewBuilder->buildView($parameters);
251
    }
252
253
    public function testBuildView(): void
254
    {
255
        $contentInfo = new ContentInfo(['id' => 120]);
256
        $content = new Content([
257
            'versionInfo' => new VersionInfo([
258
                'contentInfo' => $contentInfo,
259
            ]),
260
        ]);
261
        $location = new Location(
262
            [
263
                'invisible' => false,
264
                'contentInfo' => $contentInfo,
265
                'content' => $content,
266
            ]
267
        );
268
269
        $expectedView = new ContentView(null, [], 'full');
270
        $expectedView->setLocation($location);
271
        $expectedView->setContent($content);
272
273
        $parameters = [
274
            'viewType' => 'full',
275
            '_controller' => 'ez_content:viewAction',
276
            'locationId' => 2,
277
        ];
278
279
        $this->repository
280
            ->expects($this->once())
281
            ->method('sudo')
282
            ->willReturn($location);
283
284
        $this->permissionResolver
285
            ->expects($this->at(0))
286
            ->method('canUser')
287
            ->willReturn(true);
288
289
        $this->assertEquals($expectedView, $this->contentViewBuilder->buildView($parameters));
290
    }
291
}
292