Completed
Push — master ( dfa392...375d71 )
by
unknown
13:09
created

ContentTest::testDeleteContentVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Functional\ContentTest 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\Bundle\EzPublishRestBundle\Tests\Functional;
10
11
use Buzz\Message\Response;
12
use eZ\Bundle\EzPublishRestBundle\Tests\Functional\TestCase as RESTFunctionalTestCase;
13
14
class ContentTest extends RESTFunctionalTestCase
15
{
16
    /**
17
     * Covers POST /content/objects.
18
     *
19
     * @return string REST content ID
20
     */
21 View Code Duplication
    public function testCreateContent()
22
    {
23
        $request = $this->createHttpRequest('POST', '/api/ezp/v2/content/objects', 'ContentCreate+xml', 'ContentInfo+json');
24
        $string = $this->addTestSuffix(__FUNCTION__);
25
        $body = <<< XML
26
<?xml version="1.0" encoding="UTF-8"?>
27
<ContentCreate>
28
  <ContentType href="/api/ezp/v2/content/types/1" />
29
  <mainLanguageCode>eng-GB</mainLanguageCode>
30
  <LocationCreate>
31
    <ParentLocation href="/api/ezp/v2/content/locations/1/2" />
32
    <priority>0</priority>
33
    <hidden>false</hidden>
34
    <sortField>PATH</sortField>
35
    <sortOrder>ASC</sortOrder>
36
  </LocationCreate>
37
  <Section href="/api/ezp/v2/content/sections/1" />
38
  <alwaysAvailable>true</alwaysAvailable>
39
  <remoteId>{$string}</remoteId>
40
  <User href="/api/ezp/v2/user/users/14" />
41
  <modificationDate>2012-09-30T12:30:00</modificationDate>
42
  <fields>
43
    <field>
44
      <fieldDefinitionIdentifier>name</fieldDefinitionIdentifier>
45
      <languageCode>eng-GB</languageCode>
46
      <fieldValue>{$string}</fieldValue>
47
    </field>
48
  </fields>
49
</ContentCreate>
50
XML;
51
        $request->setContent($body);
52
53
        $response = $this->sendHttpRequest($request);
54
55
        self::assertHttpResponseCodeEquals($response, 201);
56
        self::assertHttpResponseHasHeader($response, 'Location');
57
58
        $href = $response->getHeader('Location');
59
        $this->addCreatedElement($href);
60
61
        return $href;
62
    }
63
64
    /**
65
     * @depends testCreateContent
66
     * Covers PUBLISH /content/objects/<contentId>/versions/<versionNumber>
67
     *
68
     * @return string REST content ID
69
     */
70
    public function testPublishContent($restContentHref)
71
    {
72
        $response = $this->sendHttpRequest(
73
            $this->createHttpRequest('PUBLISH', "$restContentHref/versions/1")
74
        );
75
        self::assertHttpResponseCodeEquals($response, 204);
76
77
        return $restContentHref;
78
    }
79
80
    /**
81
     * @depends testPublishContent
82
     * Covers GET /content/objects?remoteId=<remoteId>
83
     */
84
    public function testRedirectContent($restContentHref)
85
    {
86
        $response = $this->sendHttpRequest(
87
            $this->createHttpRequest('GET', '/api/ezp/v2/content/objects?remoteId=' . $this->addTestSuffix('testCreateContent'))
88
        );
89
90
        self::assertHttpResponseCodeEquals($response, 307);
91
        self::assertEquals($response->getHeader('Location'), $restContentHref);
92
    }
93
94
    /**
95
     * @depends testPublishContent
96
     */
97
    public function testLoadContent($restContentHref)
98
    {
99
        $response = $this->sendHttpRequest(
100
            $this->createHttpRequest('GET', $restContentHref)
101
        );
102
103
        self::assertHttpResponseCodeEquals($response, 200);
104
        // @todo test data a bit ?
105
    }
106
107
    /**
108
     * @depends testPublishContent
109
     */
110 View Code Duplication
    public function testUpdateContentMetadata($restContentHref)
111
    {
112
        $string = $this->addTestSuffix(__FUNCTION__);
113
        $content = <<< XML
114
<?xml version="1.0" encoding="UTF-8"?>
115
<ContentUpdate>
116
  <Owner href="/api/ezp/v2/user/users/10"/>
117
  <remoteId>{$string}</remoteId>
118
</ContentUpdate>
119
XML;
120
        $request = $this->createHttpRequest('PATCH', $restContentHref, 'ContentUpdate+xml', 'ContentInfo+json');
121
        $request->setContent($content);
122
        $response = $this->sendHttpRequest($request);
123
        self::assertHttpResponseCodeEquals($response, 200);
124
125
        // @todo test data
126
    }
127
128
    /**
129
     * @depends testPublishContent
130
     *
131
     * @return string ContentVersion REST ID
132
     */
133 View Code Duplication
    public function testCreateDraftFromVersion($restContentHref)
134
    {
135
        $response = $this->sendHttpRequest(
136
            $this->createHttpRequest('COPY', "{$restContentHref}/versions/1")
137
        );
138
139
        self::assertHttpResponseCodeEquals($response, 201);
140
        self::assertEquals($response->getHeader('Location'), "{$restContentHref}/versions/2");
141
142
        return $response->getHeader('Location');
143
    }
144
145
    /**
146
     * @depends testPublishContent
147
     * Covers GET /content/objects/<contentId>/currentversion
148
     * @covers \eZ\Publish\Core\REST\Server\Controller\Content::redirectCurrentVersion
149
     */
150
    public function testRedirectCurrentVersion($restContentHref)
151
    {
152
        $response = $this->sendHttpRequest(
153
            $this->createHttpRequest('GET', "$restContentHref/currentversion")
154
        );
155
156
        self::assertHttpResponseCodeEquals($response, 307);
157
158
        self::assertHttpResponseHasHeader($response, 'Location', "$restContentHref/versions/1");
159
    }
160
161
    /**
162
     * @depends testCreateDraftFromVersion
163
     * Covers GET /content/objects/<contentId>/versions/<versionNumber>
164
     *
165
     * @param string $restContentVersionHref
166
     */
167
    public function testLoadContentVersion($restContentVersionHref)
168
    {
169
        $response = $this->sendHttpRequest(
170
            $this->createHttpRequest('GET', $restContentVersionHref)
171
        );
172
173
        self::assertHttpResponseCodeEquals($response, 200);
174
        $this->assertVersionResponseContainsExpectedFields($response);
175
        // @todo test filtering (language, fields, etc)
176
    }
177
178
    /**
179
     * Covers COPY /content/objects/<contentId>.
180
     * @depends testPublishContent
181
     *
182
     * @return string the copied content href
183
     */
184
    public function testCopyContent($restContentHref)
185
    {
186
        $testContent = $this->loadContent($restContentHref);
187
188
        $request = $this->createHttpRequest('COPY', $restContentHref);
189
        $request->addHeader('Destination: ' . $testContent['MainLocation']['_href']);
190
191
        $response = $this->sendHttpRequest($request);
192
193
        self::assertHttpResponseCodeEquals($response, 201);
194
        self::assertStringStartsWith('/api/ezp/v2/content/objects/', $response->getHeader('Location'));
195
196
        $this->addCreatedElement($response->getHeader('Location'));
197
198
        return $response->getHeader('Location');
199
    }
200
201
    /**
202
     * Covers DELETE /content/objects/<versionNumber>.
203
     * @depends testCopyContent
204
     */
205
    public function testDeleteContent($restContentHref)
206
    {
207
        self::markTestSkipped("Fails as the content created by copyContent isn't found");
208
        $response = $this->sendHttpRequest(
209
            $this->createHttpRequest('DELETE', $restContentHref)
210
        );
211
212
        self::assertHttpResponseCodeEquals($response, 204);
213
    }
214
215
    /**
216
     * @depends testPublishContent
217
     * Covers GET /content/objects/<contentId>/versions
218
     */
219
    public function testLoadContentVersions($restContentHref)
220
    {
221
        $response = $this->sendHttpRequest(
222
            $this->createHttpRequest('GET', "$restContentHref/versions", '', 'VersionList')
223
        );
224
225
        self::assertHttpResponseCodeEquals($response, 200);
226
    }
227
228
    /**
229
     * @depends testPublishContent
230
     *
231
     * @param string $restContentHref /content/objects/<contentId>
232
     * Covers COPY /content/objects/<contentId>/currentversion
233
     *
234
     * @return string the ID of the created version (/content/objects/<contentId>/versions/<versionNumber>
235
     */
236 View Code Duplication
    public function testCreateDraftFromCurrentVersion($restContentHref)
237
    {
238
        $response = $this->sendHttpRequest(
239
            $this->createHttpRequest('COPY', "$restContentHref/currentversion")
240
        );
241
242
        self::assertHttpResponseCodeEquals($response, 201);
243
        self::assertHttpResponseHasHeader($response, 'Location');
244
245
        return $response->getHeader('Location');
246
    }
247
248
    /**
249
     * @depends testCreateDraftFromCurrentVersion
250
     *
251
     * @param string $restContentVersionHref /api/ezp/v2/content/objects/<contentId>/versions>/<versionNumber>
252
     * Covers DELETE /api/ezp/v2/content/objects/<contentId>/versions>/<versionNumber>
253
     */
254
    public function testDeleteContentVersion($restContentVersionHref)
255
    {
256
        $response = $this->sendHttpRequest(
257
            $this->createHttpRequest('DELETE', $restContentVersionHref)
258
        );
259
260
        self::assertHttpResponseCodeEquals($response, 204);
261
    }
262
263
    /**
264
     * @depends testCreateDraftFromVersion
265
     * Covers PATCH /content/objects/<contentId>/versions>/<versionNumber>
266
     *
267
     * @param string $restContentVersionHref /content/objects/<contentId>/versions>/<versionNumber>
268
     */
269 View Code Duplication
    public function testUpdateVersion($restContentVersionHref)
270
    {
271
        $xml = <<< XML
272
<VersionUpdate>
273
    <fields>
274
        <field>
275
            <fieldDefinitionIdentifier>name</fieldDefinitionIdentifier>
276
            <languageCode>eng-GB</languageCode>
277
            <fieldValue>testUpdateVersion</fieldValue>
278
        </field>
279
    </fields>
280
</VersionUpdate>
281
XML;
282
283
        $request = $this->createHttpRequest('PATCH', $restContentVersionHref, 'VersionUpdate+xml', 'Version+json');
284
        $request->setContent($xml);
285
        $response = $this->sendHttpRequest(
286
            $request
287
        );
288
289
        self::assertHttpResponseCodeEquals($response, 200);
290
    }
291
292
    /**
293
     * @depends testPublishContent
294
     * Covers GET /content/objects/<contentId>/relations
295
     */
296
    public function testRedirectCurrentVersionRelations($restContentHref)
297
    {
298
        $response = $this->sendHttpRequest(
299
            $this->createHttpRequest('GET', "$restContentHref/relations")
300
        );
301
302
        self::assertHttpResponseCodeEquals($response, 307);
303
304
        // @todo Fix, see EZP-21059. Meanwhile, the test is skipped if it fails as expected
305
        // self::assertHttpResponseHasHeader( $response, 'Location', "$restContentHref/versions/1/relations" );
306
        self::assertHttpResponseHasHeader($response, 'Location', "$restContentHref/relations?versionNumber=1");
307
        self::markTestIncomplete('@todo Fix issue EZP-21059');
308
    }
309
310
    /**
311
     * @depends testCreateDraftFromVersion
312
     * Covers GET /content/objects/<contentId>/versions/<versionNumber>/relations
313
     */
314
    public function testLoadVersionRelations($restContentVersionHref)
315
    {
316
        $response = $this->sendHttpRequest(
317
            $this->createHttpRequest('GET', "$restContentVersionHref/relations")
318
        );
319
320
        self::assertHttpResponseCodeEquals($response, 200);
321
    }
322
323
    /**
324
     * @depends testCreateDraftFromVersion
325
     * Covers POST /content/objects/<contentId>/versions/<versionNumber>/relations/<relationId>
326
     *
327
     * @return string created relation HREF (/content/objects/<contentId>/versions/<versionNumber>/relations/<relationId>
328
     */
329 View Code Duplication
    public function testCreateRelation($restContentVersionHref)
330
    {
331
        $content = <<< XML
332
<?xml version="1.0" encoding="UTF-8"?>
333
<RelationCreate>
334
  <Destination href="/api/ezp/v2/content/objects/10"/>
335
</RelationCreate>
336
XML;
337
338
        $request = $this->createHttpRequest('POST', "$restContentVersionHref/relations", 'RelationCreate+xml', 'Relation+json');
339
        $request->setContent($content);
340
341
        $response = $this->sendHttpRequest($request);
342
343
        self::assertHttpResponseCodeEquals($response, 201);
344
345
        $response = json_decode($response->getContent(), true);
346
347
        return $response['Relation']['_href'];
348
    }
349
350
    /**
351
     * @depends testCreateRelation
352
     * Covers GET /content/objects/<contentId>/versions/<versionNo>/relations/<relationId>
353
     */
354
    public function testLoadVersionRelation($restContentRelationHref)
355
    {
356
        $response = $this->sendHttpRequest(
357
            $this->createHttpRequest('GET', $restContentRelationHref)
358
        );
359
360
        self::assertHttpResponseCodeEquals($response, 200);
361
362
        // @todo test data
363
    }
364
365
    /**
366
     * Returns the Content key from the decoded JSON of $restContentId's contentInfo.
367
     *
368
     *
369
     * @throws \InvalidArgumentException
370
     *
371
     * @param string $restContentHref /api/ezp/v2/content/objects/<contentId>
372
     *
373
     * @return array
374
     */
375
    private function loadContent($restContentHref)
376
    {
377
        $response = $this->sendHttpRequest(
378
            $this->createHttpRequest('GET', $restContentHref, '', 'ContentInfo+json')
379
        );
380
381
        if ($response->getStatusCode() != 200) {
382
            throw new \InvalidArgumentException("Content with ID $restContentHref could not be loaded");
383
        }
384
385
        $array = json_decode($response->getContent(), true);
386
        if ($array === null) {
387
            self::fail('Error loading content. Response: ' . $response->getContent());
388
        }
389
390
        return $array['Content'];
391
    }
392
393 View Code Duplication
    public function testCreateView()
394
    {
395
        $body = <<< XML
396
<?xml version="1.0" encoding="UTF-8"?>
397
<ViewInput>
398
  <identifier>testCreateView</identifier>
399
  <Query>
400
    <Criteria>
401
      <ContentTypeIdentifierCriterion>folder</ContentTypeIdentifierCriterion>
402
    </Criteria>
403
    <limit>10</limit>
404
    <offset>0</offset>
405
  </Query>
406
</ViewInput>
407
XML;
408
        $request = $this->createHttpRequest('POST', '/api/ezp/v2/content/views', 'ViewInput+xml', 'View+json');
409
        $request->setContent($body);
410
        $response = $this->sendHttpRequest(
411
            $request
412
        );
413
414
        // Returns 301 since 6.0 (deprecated in favour of /views)
415
        self::assertHttpResponseCodeEquals($response, 301);
416
        self::assertHttpResponseHasHeader($response, 'Location');
417
    }
418
419
    /**
420
     * Covers DELETE /content/objects/<contentId>/versions/<versionNo>/translations/<languageCode>.
421
     *
422
     * @depends testCreateDraftFromVersion
423
     *
424
     * @param string $restContentVersionHref
425
     */
426
    public function testDeleteTranslationFromDraft($restContentVersionHref)
427
    {
428
        // create pol-PL Translation
429
        $translationToDelete = 'pol-PL';
430
        $this->createVersionTranslation($restContentVersionHref, $translationToDelete, 'Polish');
431
432
        $response = $this->sendHttpRequest(
433
            $this->createHttpRequest('DELETE', $restContentVersionHref . "/translations/{$translationToDelete}")
434
        );
435
        self::assertHttpResponseCodeEquals($response, 204);
436
437
        // check that the Translation was deleted by reloading Version
438
        $response = $this->sendHttpRequest(
439
            $this->createHttpRequest('GET', $restContentVersionHref, '', 'Version+json')
440
        );
441
442
        $version = json_decode($response->getContent(), true);
443
        self::assertNotContains($translationToDelete, $version['Version']['VersionInfo']['languageCodes']);
444
    }
445
446
    /**
447
     * Test that VersionInfo loaded in VersionList contains working DeleteTranslation resource link.
448
     *
449
     * Covers DELETE /content/objects/<contentId>/versions/<versionNo>/translations/<languageCode>.
450
     * Covers GET /content/objects/<contentId>/versions
451
     *
452
     * @depends testCreateDraftFromVersion
453
     *
454
     * @param string $restContentVersionHref
455
     */
456
    public function testLoadContentVersionsProvidesDeleteTranslationFromDraftResourceLink($restContentVersionHref)
457
    {
458
        $translationToDelete = 'pol-PL';
459
        // create Version Draft containing pol-PL Translation
460
        $this->createVersionTranslation($restContentVersionHref, $translationToDelete, 'Polish');
461
462
        // load Version
463
        $response = $this->sendHttpRequest(
464
            $this->createHttpRequest('GET', $restContentVersionHref, '', 'Version+json')
465
        );
466
        self::assertHttpResponseCodeEquals($response, 200);
467
        $version = json_decode($response->getContent(), true);
468
469
        // load all Versions
470
        self::assertNotEmpty($version['Version']['VersionInfo']['Content']['_href']);
471
        $restLoadContentVersionsHref = $version['Version']['VersionInfo']['Content']['_href'] . '/versions';
472
        $response = $this->sendHttpRequest(
473
            $this->createHttpRequest('GET', $restLoadContentVersionsHref, '', 'VersionList+json')
474
        );
475
        self::assertHttpResponseCodeEquals($response, 200);
476
477
        // load Version list
478
        $versionList = json_decode($response->getContent(), true);
479
        $version = $this->getVersionInfoFromJSONVersionListByStatus(
480
            $versionList['VersionList'],
481
            'DRAFT'
482
        );
483
484
        // validate VersionTranslationInfo structure
485
        self::assertNotEmpty($version['VersionTranslationInfo']['Language']);
486
        foreach ($version['VersionTranslationInfo']['Language'] as $versionTranslationInfo) {
487
            // Other Translation, as the main one, shouldn't be deletable
488
            if ($versionTranslationInfo['languageCode'] !== $translationToDelete) {
489
                // check that endpoint is not provided for non-deletable Translation
490
                self::assertTrue(empty($versionTranslationInfo['DeleteTranslation']['_href']));
491
            } else {
492
                // check that provided endpoint works
493
                self::assertNotEmpty($versionTranslationInfo['DeleteTranslation']['_href']);
494
                $response = $this->sendHttpRequest(
495
                    $this->createHttpRequest(
496
                        'DELETE',
497
                        $versionTranslationInfo['DeleteTranslation']['_href']
498
                    )
499
                );
500
                self::assertHttpResponseCodeEquals($response, 204);
501
            }
502
        }
503
    }
504
505
    /**
506
     * Covers DELETE /content/objects/<contentId>/translations/<languageCode>.
507
     */
508
    public function testDeleteTranslation()
509
    {
510
        // create independent Content
511
        $content = $this->createContentDraft(
512
            '/api/ezp/v2/content/types/1',
513
            '/api/ezp/v2/content/locations/1/2',
514
            '/api/ezp/v2/content/sections/1',
515
            '/api/ezp/v2/user/users/14',
516
            [
517
                'name' => [
518
                    'eng-GB' => $this->addTestSuffix(__FUNCTION__),
519
                ],
520
            ]
521
        );
522
        $restContentHref = $content['_href'];
523
        $restContentVersionHref = "{$content['Versions']['_href']}/{$content['currentVersionNo']}";
524
        $this->publishContentVersionDraft($restContentVersionHref);
525
        $restContentVersionHref = $this->createDraftFromVersion($content['CurrentVersion']['_href']);
526
527
        // create pol-PL Translation
528
        $translationToDelete = 'pol-PL';
529
        $this->createVersionTranslation($restContentVersionHref, $translationToDelete, 'Polish');
0 ignored issues
show
Bug introduced by
It seems like $restContentVersionHref defined by $this->createDraftFromVe...rentVersion']['_href']) on line 525 can also be of type array or null; however, eZ\Bundle\EzPublishRestB...ateVersionTranslation() does only seem to accept string, 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...
530
        $this->publishContentVersionDraft($restContentVersionHref);
0 ignored issues
show
Bug introduced by
It seems like $restContentVersionHref defined by $this->createDraftFromVe...rentVersion']['_href']) on line 525 can also be of type array or null; however, eZ\Bundle\EzPublishRestB...shContentVersionDraft() does only seem to accept string, 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...
531
532
        // delete Translation
533
        $response = $this->sendHttpRequest(
534
            $this->createHttpRequest('DELETE', "{$restContentHref}/translations/{$translationToDelete}")
535
        );
536
        self::assertHttpResponseCodeEquals($response, 204);
537
538
        // check that deleted Translation no longer exists
539
        $response = $this->sendHttpRequest(
540
            $this->createHttpRequest('GET', "$restContentHref/versions", '', 'VersionList+json')
541
        );
542
        self::assertHttpResponseCodeEquals($response, 200);
543
        $versionList = json_decode($response->getContent(), true);
544
        foreach ($versionList['VersionList']['VersionItem'] as $versionItem) {
545
            self::assertNotContains($translationToDelete, $versionItem['VersionInfo']['languageCodes']);
546
            foreach ($versionItem['VersionInfo']['names']['value'] as $name) {
547
                self::assertNotEquals($translationToDelete, $name['_languageCode']);
548
            }
549
        }
550
551
        return $restContentHref;
552
    }
553
554
    /**
555
     * Test that deleting content which has Version(s) with single Translation being deleted is supported.
556
     *
557
     * Covers DELETE /content/objects/<contentId>/translations/<languageCode>.
558
     *
559
     * @depends testDeleteTranslation
560
     *
561
     * @param string $restContentHref
562
     */
563
    public function testDeleteTranslationOfContentWithSingleTranslationVersion($restContentHref)
564
    {
565
        // create draft independent from other tests
566
        $restContentVersionHref = $this->createDraftFromVersion("$restContentHref/versions/1");
567
568
        // create pol-PL Translation to have more than one Translation
569
        $this->createVersionTranslation($restContentVersionHref, 'pol-PL', 'Polish');
0 ignored issues
show
Bug introduced by
It seems like $restContentVersionHref defined by $this->createDraftFromVe...ntentHref}/versions/1") on line 566 can also be of type array or null; however, eZ\Bundle\EzPublishRestB...ateVersionTranslation() does only seem to accept string, 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...
570
        $this->publishContentVersionDraft($restContentVersionHref);
0 ignored issues
show
Bug introduced by
It seems like $restContentVersionHref defined by $this->createDraftFromVe...ntentHref}/versions/1") on line 566 can also be of type array or null; however, eZ\Bundle\EzPublishRestB...shContentVersionDraft() does only seem to accept string, 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...
571
572
        // change Main Translation to just created pol-PL
573
        $this->updateMainTranslation($restContentHref, 'pol-PL');
574
575
        // delete eng-GB Translation
576
        $translationToDelete = 'eng-GB';
577
        $response = $this->sendHttpRequest(
578
            $this->createHttpRequest('DELETE', "{$restContentHref}/translations/{$translationToDelete}")
579
        );
580
        self::assertHttpResponseCodeEquals($response, 204);
581
582
        // check that deleted Translation no longer exists
583
        $response = $this->sendHttpRequest(
584
            $this->createHttpRequest('GET', "$restContentHref/versions", '', 'VersionList+json')
585
        );
586
        self::assertHttpResponseCodeEquals($response, 200);
587
        $versionList = json_decode($response->getContent(), true);
588
        foreach ($versionList['VersionList']['VersionItem'] as $versionItem) {
589
            self::assertNotEmpty($versionItem['VersionInfo']['languageCodes']);
590
            self::assertNotContains($translationToDelete, $versionItem['VersionInfo']['languageCodes']);
591
            foreach ($versionItem['VersionInfo']['names']['value'] as $name) {
592
                self::assertNotEquals($translationToDelete, $name['_languageCode']);
593
            }
594
        }
595
    }
596
597
    /**
598
     * Publish another Version with new Translation.
599
     *
600
     * @param string $restContentVersionHref
601
     *
602
     * @param string $languageCode
603
     * @param string $languageName
604
     *
605
     * @return string
606
     */
607 View Code Duplication
    private function createVersionTranslation($restContentVersionHref, $languageCode, $languageName)
608
    {
609
        // @todo Implement EZP-21171 to check if Language exists and add it
610
        // for now adding is done by ez:behat:create-language command executed in Travis job
611
612
        $xml = <<< XML
613
<VersionUpdate>
614
    <fields>
615
        <field>
616
            <fieldDefinitionIdentifier>name</fieldDefinitionIdentifier>
617
            <languageCode>{$languageCode}</languageCode>
618
            <fieldValue>{$languageName} translated name</fieldValue>
619
        </field>
620
    </fields>
621
</VersionUpdate>
622
XML;
623
624
        $request = $this->createHttpRequest('PATCH', $restContentVersionHref, 'VersionUpdate+xml', 'Version+json');
625
        $request->setContent($xml);
626
        $response = $this->sendHttpRequest(
627
            $request
628
        );
629
630
        self::assertHttpResponseCodeEquals($response, 200);
631
    }
632
633
    /**
634
     * Iterate through Version Items returned by REST view for ContentType: VersionList+json
635
     * and return first VersionInfo data matching given status.
636
     *
637
     * @param array $versionList
638
     * @param string $status uppercase string representation of Version status
639
     *
640
     * @return array
641
     */
642
    private function getVersionInfoFromJSONVersionListByStatus(array $versionList, $status)
643
    {
644
        foreach ($versionList['VersionItem'] as $versionItem) {
645
            if ($versionItem['VersionInfo']['status'] === $status) {
646
                return $versionItem['VersionInfo'];
647
            }
648
        }
649
650
        throw new \RuntimeException("Test internal error: Version with status {$status} not found");
651
    }
652
653
    /**
654
     * Assert that Version REST Response contains proper fields.
655
     *
656
     * @param \Buzz\Message\Response $response
657
     */
658
    private function assertVersionResponseContainsExpectedFields(Response $response)
659
    {
660
        $contentType = $response->getHeader('Content-Type');
661
        self::assertNotEmpty($contentType);
662
663
        $responseBody = $response->getContent();
664
665
        // check if response is of an expected Content-Type
666
        self::assertEquals('Version+xml', $this->getMediaFromTypeString($contentType));
667
668
        // validate by custom XSD
669
        $document = new \DOMDocument();
670
        $document->loadXML($responseBody);
671
        $document->schemaValidate(__DIR__ . '/xsd/Version.xsd');
672
    }
673
674
    /**
675
     * Create new Content Draft.
676
     *
677
     * @param string $restContentTypeHref Content Type REST resource link
678
     * @param string $restParentLocationHref Parent Location REST resource link
679
     * @param string $restSectionHref Section REST resource link
680
     * @param string $restUserHref User REST resource link
681
     * @param array $fieldValues multilingual field values <code>['fieldIdentifier' => ['languageCode' => 'value']]</code>
682
     *
683
     * @return array Content structure decoded from JSON
684
     */
685
    private function createContentDraft($restContentTypeHref, $restParentLocationHref, $restSectionHref, $restUserHref, array $fieldValues)
686
    {
687
        $remoteId = md5(microtime() . uniqid());
688
        $modificationDate = new \DateTime();
689
690
        $fieldsXML = '';
691
        foreach ($fieldValues as $fieldIdentifier => $multilingualValues) {
692
            foreach ($multilingualValues as $languageCode => $fieldValue) {
693
                $fieldsXML .= <<< XML
694
<field>
695
  <fieldDefinitionIdentifier>{$fieldIdentifier}</fieldDefinitionIdentifier>
696
  <languageCode>{$languageCode}</languageCode>
697
  <fieldValue>{$fieldValue}</fieldValue>
698
</field>
699
XML;
700
            }
701
        }
702
703
        $body = <<< XML
704
<?xml version="1.0" encoding="UTF-8"?>
705
<ContentCreate>
706
  <ContentType href="{$restContentTypeHref}" />
707
  <mainLanguageCode>eng-GB</mainLanguageCode>
708
  <LocationCreate>
709
    <ParentLocation href="{$restParentLocationHref}" />
710
    <priority>0</priority>
711
    <hidden>false</hidden>
712
    <sortField>PATH</sortField>
713
    <sortOrder>ASC</sortOrder>
714
  </LocationCreate>
715
  <Section href="{$restSectionHref}" />
716
  <alwaysAvailable>true</alwaysAvailable>
717
  <remoteId>{$remoteId}</remoteId>
718
  <User href="{$restUserHref}" />
719
  <modificationDate>{$modificationDate->format('c')}</modificationDate>
720
  <fields>
721
    {$fieldsXML}
722
  </fields>
723
</ContentCreate>
724
XML;
725
        $request = $this->createHttpRequest('POST', '/api/ezp/v2/content/objects', 'ContentCreate+xml', 'ContentInfo+json');
726
        $request->setContent($body);
727
728
        $response = $this->sendHttpRequest($request);
729
730
        self::assertHttpResponseCodeEquals($response, 201);
731
        self::assertHttpResponseHasHeader($response, 'Location');
732
733
        $href = $response->getHeader('Location');
734
        $this->addCreatedElement($href);
735
736
        $content = json_decode($response->getContent(), true);
737
        self::assertNotEmpty($content['Content']);
738
739
        return $content['Content'];
740
    }
741
742
    /**
743
     * Create Draft of a given Content and versionNo.
744
     *
745
     * @param string $restContentVersionHref REST resource link of Content Version
746
     *
747
     * @return string Content Version Draft REST resource link
748
     */
749 View Code Duplication
    private function createDraftFromVersion($restContentVersionHref)
750
    {
751
        $response = $this->sendHttpRequest(
752
            $this->createHttpRequest('COPY', "{$restContentVersionHref}")
753
        );
754
        self::assertHttpResponseCodeEquals($response, 201);
755
756
        $href = $response->getHeader('Location');
757
        self::assertNotEmpty($href);
758
759
        return $href;
760
    }
761
762
    /**
763
     * Publish Content Version Draft given by REST resource link.
764
     *
765
     * @param string $restContentVersionHref REST resource link of Version Draft
766
     */
767
    private function publishContentVersionDraft($restContentVersionHref)
768
    {
769
        $response = $this->sendHttpRequest(
770
            $this->createHttpRequest('PUBLISH', $restContentVersionHref)
771
        );
772
        self::assertHttpResponseCodeEquals($response, 204);
773
    }
774
775
    /**
776
     * Update Main Translation of a Content.
777
     *
778
     * @param string $restContentHref REST resource link of Content
779
     * @param string $languageCode new Main Translation language code
780
     */
781 View Code Duplication
    private function updateMainTranslation($restContentHref, $languageCode)
782
    {
783
        $content = <<< XML
784
<?xml version="1.0" encoding="UTF-8"?>
785
<ContentUpdate>
786
  <mainLanguageCode>{$languageCode}</mainLanguageCode>
787
</ContentUpdate>
788
XML;
789
790
        $request = $this->createHttpRequest('PATCH', $restContentHref, 'ContentUpdate+xml', 'ContentInfo+json');
791
        $request->setContent($content);
792
        $response = $this->sendHttpRequest($request);
793
794
        self::assertHttpResponseCodeEquals($response, 200);
795
    }
796
}
797