Completed
Push — 7.5 ( 5c8d05...441ec5 )
by
unknown
19:22
created

RestContentTest::testContentIdCorrect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing a test class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\REST\Server\Tests\Output\ValueObjectVisitor;
10
11
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
12
use eZ\Publish\Core\Helper\TranslationHelper;
13
use eZ\Publish\Core\REST\Common\Tests\Output\ValueObjectVisitorBaseTest;
14
use eZ\Publish\Core\REST\Server\Values\RestContent;
15
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor;
16
use eZ\Publish\Core\Repository\Values;
17
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
18
use eZ\Publish\Core\REST\Server\Values\Version;
19
20
class RestContentTest extends ValueObjectVisitorBaseTest
21
{
22
    /** @var \eZ\Publish\Core\Helper\TranslationHelper|\PHPUnit\Framework\MockObject\MockObject */
23
    private $translationHelper;
24
25
    protected function setUp(): void
26
    {
27
        $this->translationHelper = $this->createMock(TranslationHelper::class);
28
        $this->translationHelper
29
            ->method('getTranslatedContentNameByContentInfo')
30
            ->willReturnCallback(function (ContentInfo $content) {
31
                return $content->name . ' (Translated)';
32
            });
33
    }
34
35
    /**
36
     * @return \DOMDocument
37
     */
38
    public function testVisitWithoutEmbeddedVersion()
39
    {
40
        $visitor = $this->getVisitor();
41
        $generator = $this->getGenerator();
42
43
        $generator->startDocument(null);
44
45
        $restContent = $this->getBasicRestContent();
46
47
        $this->getVisitorMock()->expects($this->never())
48
            ->method('visitValueObject');
49
50
        $this->addRouteExpectation(
51
            'ezpublish_rest_loadContent',
52
            ['contentId' => $restContent->contentInfo->id],
53
            "/content/objects/{$restContent->contentInfo->id}"
54
        );
55
        $this->addRouteExpectation(
56
            'ezpublish_rest_loadContentType',
57
            ['contentTypeId' => $restContent->contentInfo->contentTypeId],
58
            "/content/types/{$restContent->contentInfo->contentTypeId}"
59
        );
60
        $this->addRouteExpectation(
61
            'ezpublish_rest_loadContentVersions',
62
            ['contentId' => $restContent->contentInfo->id],
63
            "/content/objects/{$restContent->contentInfo->id}/versions"
64
        );
65
        $this->addRouteExpectation(
66
            'ezpublish_rest_redirectCurrentVersion',
67
            ['contentId' => $restContent->contentInfo->id],
68
            "/content/objects/{$restContent->contentInfo->id}/currentversion"
69
        );
70
        $this->addRouteExpectation(
71
            'ezpublish_rest_loadSection',
72
            ['sectionId' => $restContent->contentInfo->sectionId],
73
            "/content/sections/{$restContent->contentInfo->sectionId}"
74
        );
75
        $this->addRouteExpectation(
76
            'ezpublish_rest_loadLocation',
77
            ['locationPath' => $locationPath = trim($restContent->mainLocation->pathString, '/')],
78
            "/content/locations/{$locationPath}"
79
        );
80
        $this->addRouteExpectation(
81
            'ezpublish_rest_loadLocationsForContent',
82
            ['contentId' => $restContent->contentInfo->id],
83
            "/content/objects/{$restContent->contentInfo->id}/locations"
84
        );
85
        $this->addRouteExpectation(
86
            'ezpublish_rest_loadUser',
87
            ['userId' => $restContent->contentInfo->ownerId],
88
            "/user/users/{$restContent->contentInfo->ownerId}"
89
        );
90
        $this->addRouteExpectation(
91
            'ezpublish_rest_getObjectStatesForContent',
92
            ['contentId' => $restContent->contentInfo->id],
93
            "/content/objects/{$restContent->contentInfo->id}/objectstates"
94
        );
95
96
        $visitor->visit(
97
            $this->getVisitorMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getVisitorMock() targeting eZ\Publish\Core\REST\Com...eTest::getVisitorMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\REST\Com...eObjectVisitor::visit() does only seem to accept object<eZ\Publish\Core\R...\Common\Output\Visitor>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
98
            $generator,
99
            $restContent
100
        );
101
102
        $result = $generator->endDocument(null);
103
104
        $this->assertNotNull($result);
105
106
        $dom = new \DOMDocument();
107
        $dom->loadXml($result);
108
109
        return $dom;
110
    }
111
112
    protected function getBasicRestContent()
113
    {
114
        return new RestContent(
115
            new ContentInfo(
116
                [
117
                    'id' => 'content23',
118
                    'name' => 'Sindelfingen',
119
                    'sectionId' => 'section23',
120
                    'currentVersionNo' => 5,
121
                    'published' => true,
122
                    'ownerId' => 'user23',
123
                    'modificationDate' => new \DateTime('2012-09-05 15:27 Europe/Berlin'),
124
                    'publishedDate' => null,
125
                    'alwaysAvailable' => true,
126
                    'status' => ContentInfo::STATUS_PUBLISHED,
127
                    'remoteId' => 'abc123',
128
                    'mainLanguageCode' => 'eng-US',
129
                    'mainLocationId' => 'location23',
130
                    'contentTypeId' => 'contentType23',
131
                    'isHidden' => true,
132
                ]
133
            ),
134
            new Values\Content\Location(
135
                [
136
                    'pathString' => '/1/2/23',
137
                ]
138
            ),
139
            null
140
        );
141
    }
142
143
    /**
144
     * @param \DOMDocument $dom
145
     *
146
     * @depends testVisitWithoutEmbeddedVersion
147
     */
148
    public function testContentHrefCorrect(\DOMDocument $dom)
149
    {
150
        $this->assertXPath($dom, '/Content[@href="/content/objects/content23"]');
151
    }
152
153
    /**
154
     * @param \DOMDocument $dom
155
     *
156
     * @depends testVisitWithoutEmbeddedVersion
157
     */
158
    public function testContentIdCorrect(\DOMDocument $dom)
159
    {
160
        $this->assertXPath($dom, '/Content[@id="content23"]');
161
    }
162
163
    /**
164
     * @param \DOMDocument $dom
165
     *
166
     * @depends testVisitWithoutEmbeddedVersion
167
     */
168
    public function testContentMediaTypeWithoutVersionCorrect(\DOMDocument $dom)
169
    {
170
        $this->assertXPath($dom, '/Content[@media-type="application/vnd.ez.api.ContentInfo+xml"]');
171
    }
172
173
    /**
174
     * @param \DOMDocument $dom
175
     *
176
     * @depends testVisitWithoutEmbeddedVersion
177
     */
178
    public function testContentRemoteIdCorrect(\DOMDocument $dom)
179
    {
180
        $this->assertXPath($dom, '/Content[@remoteId="abc123"]');
181
    }
182
183
    /**
184
     * @param \DOMDocument $dom
185
     *
186
     * @depends testVisitWithoutEmbeddedVersion
187
     */
188
    public function testContentTypeHrefCorrect(\DOMDocument $dom)
189
    {
190
        $this->assertXPath($dom, '/Content/ContentType[@href="/content/types/contentType23"]');
191
    }
192
193
    /**
194
     * @param \DOMDocument $dom
195
     *
196
     * @depends testVisitWithoutEmbeddedVersion
197
     */
198
    public function testContentTypeMediaTypeCorrect(\DOMDocument $dom)
199
    {
200
        $this->assertXPath($dom, '/Content/ContentType[@media-type="application/vnd.ez.api.ContentType+xml"]');
201
    }
202
203
    /**
204
     * @param \DOMDocument $dom
205
     *
206
     * @depends testVisitWithoutEmbeddedVersion
207
     */
208
    public function testNameCorrect(\DOMDocument $dom)
209
    {
210
        $this->assertXPath($dom, '/Content/Name[text()="Sindelfingen"]');
211
    }
212
213
    /**
214
     * @param \DOMDocument $dom
215
     *
216
     * @depends testVisitWithoutEmbeddedVersion
217
     */
218
    public function testTranslatedNameCorrect(\DOMDocument $dom)
219
    {
220
        $this->assertXPath($dom, '/Content/TranslatedName[text()="Sindelfingen (Translated)"]');
221
    }
222
223
    /**
224
     * @param \DOMDocument $dom
225
     *
226
     * @depends testVisitWithoutEmbeddedVersion
227
     */
228
    public function testVersionsHrefCorrect(\DOMDocument $dom)
229
    {
230
        $this->assertXPath($dom, '/Content/Versions[@href="/content/objects/content23/versions"]');
231
    }
232
233
    /**
234
     * @param \DOMDocument $dom
235
     *
236
     * @depends testVisitWithoutEmbeddedVersion
237
     */
238
    public function testVersionsMediaTypeCorrect(\DOMDocument $dom)
239
    {
240
        $this->assertXPath($dom, '/Content/Versions[@media-type="application/vnd.ez.api.VersionList+xml"]');
241
    }
242
243
    /**
244
     * @param \DOMDocument $dom
245
     *
246
     * @depends testVisitWithoutEmbeddedVersion
247
     */
248
    public function testCurrentVersionHrefCorrect(\DOMDocument $dom)
249
    {
250
        $this->assertXPath($dom, '/Content/CurrentVersion[@href="/content/objects/content23/currentversion"]');
251
    }
252
253
    /**
254
     * @param \DOMDocument $dom
255
     *
256
     * @depends testVisitWithoutEmbeddedVersion
257
     */
258
    public function testCurrentVersionMediaTypeCorrect(\DOMDocument $dom)
259
    {
260
        $this->assertXPath($dom, '/Content/CurrentVersion[@media-type="application/vnd.ez.api.Version+xml"]');
261
    }
262
263
    /**
264
     * @param \DOMDocument $dom
265
     *
266
     * @depends testVisitWithoutEmbeddedVersion
267
     */
268
    public function testSectionHrefCorrect(\DOMDocument $dom)
269
    {
270
        $this->assertXPath($dom, '/Content/Section[@href="/content/sections/section23"]');
271
    }
272
273
    /**
274
     * @param \DOMDocument $dom
275
     *
276
     * @depends testVisitWithoutEmbeddedVersion
277
     */
278
    public function testSectionMediaTypeCorrect(\DOMDocument $dom)
279
    {
280
        $this->assertXPath($dom, '/Content/Section[@media-type="application/vnd.ez.api.Section+xml"]');
281
    }
282
283
    /**
284
     * @param \DOMDocument $dom
285
     *
286
     * @depends testVisitWithoutEmbeddedVersion
287
     */
288
    public function testMainLocationHrefCorrect(\DOMDocument $dom)
289
    {
290
        $this->assertXPath($dom, '/Content/MainLocation[@href="/content/locations/1/2/23"]');
291
    }
292
293
    /**
294
     * @param \DOMDocument $dom
295
     *
296
     * @depends testVisitWithoutEmbeddedVersion
297
     */
298
    public function testMainLocationMediaTypeCorrect(\DOMDocument $dom)
299
    {
300
        $this->assertXPath($dom, '/Content/MainLocation[@media-type="application/vnd.ez.api.Location+xml"]');
301
    }
302
303
    /**
304
     * @param \DOMDocument $dom
305
     *
306
     * @depends testVisitWithoutEmbeddedVersion
307
     */
308
    public function testLocationsHrefCorrect(\DOMDocument $dom)
309
    {
310
        $this->assertXPath($dom, '/Content/Locations[@href="/content/objects/content23/locations"]');
311
    }
312
313
    /**
314
     * @param \DOMDocument $dom
315
     *
316
     * @depends testVisitWithoutEmbeddedVersion
317
     */
318
    public function testLocationsMediaTypeCorrect(\DOMDocument $dom)
319
    {
320
        $this->assertXPath($dom, '/Content/Locations[@media-type="application/vnd.ez.api.LocationList+xml"]');
321
    }
322
323
    /**
324
     * @param \DOMDocument $dom
325
     *
326
     * @depends testVisitWithoutEmbeddedVersion
327
     */
328
    public function testOwnerHrefCorrect(\DOMDocument $dom)
329
    {
330
        $this->assertXPath($dom, '/Content/Owner[@href="/user/users/user23"]');
331
    }
332
333
    /**
334
     * @param \DOMDocument $dom
335
     *
336
     * @depends testVisitWithoutEmbeddedVersion
337
     */
338
    public function testOwnerMediaTypeCorrect(\DOMDocument $dom)
339
    {
340
        $this->assertXPath($dom, '/Content/Owner[@media-type="application/vnd.ez.api.User+xml"]');
341
    }
342
343
    /**
344
     * @param \DOMDocument $dom
345
     *
346
     * @depends testVisitWithoutEmbeddedVersion
347
     */
348
    public function testLastModificationDateCorrect(\DOMDocument $dom)
349
    {
350
        $this->assertXPath($dom, '/Content/lastModificationDate[text()="2012-09-05T15:27:00+02:00"]');
351
    }
352
353
    /**
354
     * @param \DOMDocument $dom
355
     *
356
     * @depends testVisitWithoutEmbeddedVersion
357
     */
358
    public function testMainLanguageCodeCorrect(\DOMDocument $dom)
359
    {
360
        $this->assertXPath($dom, '/Content/mainLanguageCode[text()="eng-US"]');
361
    }
362
363
    /**
364
     * @param \DOMDocument $dom
365
     *
366
     * @depends testVisitWithoutEmbeddedVersion
367
     */
368
    public function testCurrentVersionNoCorrect(\DOMDocument $dom)
369
    {
370
        $this->assertXPath($dom, '/Content/currentVersionNo[text()="5"]');
371
    }
372
373
    /**
374
     * @param \DOMDocument $dom
375
     *
376
     * @depends testVisitWithoutEmbeddedVersion
377
     */
378
    public function testAlwaysAvailableCorrect(\DOMDocument $dom)
379
    {
380
        $this->assertXPath($dom, '/Content/alwaysAvailable[text()="true"]');
381
    }
382
383
    /**
384
     * @param \DOMDocument $dom
385
     *
386
     * @depends testVisitWithoutEmbeddedVersion
387
     */
388
    public function testIsHiddenCorrect(\DOMDocument $dom)
389
    {
390
        $this->assertXPath($dom, '/Content/isHidden[text()="true"]');
391
    }
392
393
    /**
394
     * @param \DOMDocument $dom
395
     *
396
     * @depends testVisitWithoutEmbeddedVersion
397
     */
398
    public function testStatusCorrect(\DOMDocument $dom)
399
    {
400
        $this->assertXPath($dom, '/Content/status[text()="PUBLISHED"]');
401
    }
402
403
    /**
404
     * @return \DOMDocument
405
     */
406
    public function testVisitWithEmbeddedVersion()
407
    {
408
        $visitor = $this->getVisitor();
409
        $generator = $this->getGenerator();
410
411
        $generator->startDocument(null);
412
413
        $restContent = $this->getBasicRestContent();
414
        $restContent->currentVersion = new Values\Content\Content(
415
            [
416
                'versionInfo' => new Values\Content\VersionInfo(['versionNo' => 5]),
417
                'internalFields' => [],
418
            ]
419
        );
420
        $restContent->relations = [];
421
        $restContent->contentType = $this->getMockForAbstractClass(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForAbstrac...ype\ContentType::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\API\Re...ontentType\ContentType> of property $contentType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
422
            ContentType::class
423
        );
424
425
        $this->getVisitorMock()->expects($this->once())
426
            ->method('visitValueObject')
427
            ->with($this->isInstanceOf(Version::class));
428
429
        $this->addRouteExpectation(
430
            'ezpublish_rest_loadContent',
431
            ['contentId' => $restContent->contentInfo->id],
432
            "/content/objects/{$restContent->contentInfo->id}"
433
        );
434
        $this->addRouteExpectation(
435
            'ezpublish_rest_loadContentType',
436
            ['contentTypeId' => $restContent->contentInfo->contentTypeId],
437
            "/content/types/{$restContent->contentInfo->contentTypeId}"
438
        );
439
        $this->addRouteExpectation(
440
            'ezpublish_rest_loadContentVersions',
441
            ['contentId' => $restContent->contentInfo->id],
442
            "/content/objects/{$restContent->contentInfo->id}/versions"
443
        );
444
        $this->addRouteExpectation(
445
            'ezpublish_rest_redirectCurrentVersion',
446
            ['contentId' => $restContent->contentInfo->id],
447
            "/content/objects/{$restContent->contentInfo->id}/currentversion"
448
        );
449
450
        $this->addRouteExpectation(
451
            'ezpublish_rest_loadSection',
452
            ['sectionId' => $restContent->contentInfo->sectionId],
453
            "/content/sections/{$restContent->contentInfo->sectionId}"
454
        );
455
        $this->addRouteExpectation(
456
            'ezpublish_rest_loadLocation',
457
            ['locationPath' => $locationPath = trim($restContent->mainLocation->pathString, '/')],
458
            "/content/locations/{$locationPath}"
459
        );
460
        $this->addRouteExpectation(
461
            'ezpublish_rest_loadLocationsForContent',
462
            ['contentId' => $restContent->contentInfo->id],
463
            "/content/objects/{$restContent->contentInfo->id}/locations"
464
        );
465
        $this->addRouteExpectation(
466
            'ezpublish_rest_loadUser',
467
            ['userId' => $restContent->contentInfo->ownerId],
468
            "/user/users/{$restContent->contentInfo->ownerId}"
469
        );
470
471
        $visitor->visit(
472
            $this->getVisitorMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getVisitorMock() targeting eZ\Publish\Core\REST\Com...eTest::getVisitorMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\REST\Com...eObjectVisitor::visit() does only seem to accept object<eZ\Publish\Core\R...\Common\Output\Visitor>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
473
            $generator,
474
            $restContent
475
        );
476
477
        $result = $generator->endDocument(null);
478
479
        $this->assertNotNull($result);
480
481
        $dom = new \DOMDocument();
482
        $dom->loadXml($result);
483
484
        return $dom;
485
    }
486
487
    /**
488
     * @param \DOMDocument $dom
489
     *
490
     * @depends testVisitWithEmbeddedVersion
491
     */
492
    public function testContentMediaTypeWithVersionCorrect(\DOMDocument $dom)
493
    {
494
        $this->assertXPath($dom, '/Content[@media-type="application/vnd.ez.api.Content+xml"]');
495
    }
496
497
    /**
498
     * @param \DOMDocument $dom
499
     *
500
     * @depends testVisitWithEmbeddedVersion
501
     */
502
    public function testEmbeddedCurrentVersionHrefCorrect(\DOMDocument $dom)
503
    {
504
        $this->assertXPath($dom, '/Content/CurrentVersion[@href="/content/objects/content23/currentversion"]');
505
    }
506
507
    /**
508
     * @param \DOMDocument $dom
509
     *
510
     * @depends testVisitWithEmbeddedVersion
511
     */
512
    public function testEmbeddedCurrentVersionMediaTypeCorrect(\DOMDocument $dom)
513
    {
514
        $this->assertXPath($dom, '/Content/CurrentVersion[@media-type="application/vnd.ez.api.Version+xml"]');
515
    }
516
517
    /**
518
     * Get the Content visitor.
519
     *
520
     * @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\RestContent
521
     */
522
    protected function internalGetVisitor()
523
    {
524
        return new ValueObjectVisitor\RestContent(
525
            $this->translationHelper
0 ignored issues
show
Bug introduced by
It seems like $this->translationHelper can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\REST\Ser...tContent::__construct() does only seem to accept object<eZ\Publish\Core\Helper\TranslationHelper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
526
        );
527
    }
528
}
529