Completed
Push — ezp26297-rest_embedding_http_c... ( 6dcd26...7b4207 )
by
unknown
44:13 queued 12:57
created

UrlAliasTest::extractLocationIdFromHref()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Functional\UrlAliasTest 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
 * @version //autogentag//
10
 */
11
namespace eZ\Bundle\EzPublishRestBundle\Tests\Functional;
12
13
use Buzz\Message\Response;
14
use eZ\Bundle\EzPublishRestBundle\Tests\Functional\TestCase as RESTFunctionalTestCase;
15
use eZ\Publish\API\Repository\Values\Content\URLAlias;
16
use eZ\Publish\Core\Repository\Values\Content\Location;
17
use eZ\Publish\Core\REST\Server\Values\URLAliasRefList;
18
19
class UrlAliasTest extends RESTFunctionalTestCase
20
{
21
    /**
22
     * @covers nothing. Creates a folder for other tests.
23
     *
24
     * @return string The folder's main location href
25
     */
26 View Code Duplication
    public function testCreateFolder()
27
    {
28
        $folderArray = $this->createFolder(__FUNCTION__, '/api/ezp/v2/content/locations/1/2');
29
        $folderLocations = $this->getContentLocations($folderArray['_href']);
30
31
        return $folderLocations['LocationList']['Location'][0]['_href'];
32
    }
33
34
    /**
35
     * @covers GET /content/urlaliases
36
     */
37
    public function testListGlobalURLAliases()
38
    {
39
        $response = $this->sendHttpRequest(
40
            $this->createHttpRequest('GET', '/api/ezp/v2/content/urlaliases')
41
        );
42
43
        self::assertHttpResponseCodeEquals($response, 200);
44
    }
45
46
    /**
47
     * @depends testCreateFolder
48
     * @covers POST /content/urlaliases
49
     * @returns string The created url alias href
50
     */
51
    public function testCreateUrlAlias($locationHref)
52
    {
53
        $text = $this->addTestSuffix(__FUNCTION__);
54
        $xml = <<< XML
55
<?xml version="1.0" encoding="UTF-8"?>
56
<UrlAliasCreate type="LOCATION">
57
  <location href="{$locationHref}" />
58
  <path>/{$text}</path>
59
  <languageCode>eng-GB</languageCode>
60
  <alwaysAvailable>false</alwaysAvailable>
61
  <forward>true</forward>
62
</UrlAliasCreate>
63
XML;
64
65
        $request = $this->createHttpRequest(
66
            'POST',
67
            '/api/ezp/v2/content/urlaliases',
68
            'UrlAliasCreate+xml',
69
            'UrlAlias+json'
70
        );
71
        $request->setContent($xml);
72
73
        $response = $this->sendHttpRequest($request);
74
75
        self::assertHttpResponseCodeEquals($response, 201);
76
        self::assertHttpResponseHasHeader($response, 'Location');
77
78
        $href = $response->getHeader('Location');
79
        $this->addCreatedElement($href);
80
81
        return $href;
82
    }
83
84
    /**
85
     * @covers POST /content/urlaliases
86
     * @returns string The created url alias href
87
     */
88 View Code Duplication
    public function testCreateGlobalUrlAlias()
89
    {
90
        $text = $this->addTestSuffix(__FUNCTION__);
91
        $xml = <<< XML
92
<?xml version="1.0" encoding="UTF-8"?>
93
<UrlAliasCreate type="RESOURCE">
94
  <resource>module:/content/search</resource>
95
  <path>/$text</path>
96
  <languageCode>eng-GB</languageCode>
97
  <alwaysAvailable>false</alwaysAvailable>
98
  <forward>true</forward>
99
</UrlAliasCreate>
100
XML;
101
102
        $request = $this->createHttpRequest(
103
            'POST',
104
            '/api/ezp/v2/content/urlaliases',
105
            'UrlAliasCreate+xml',
106
            'UrlAlias+json'
107
        );
108
        $request->setContent($xml);
109
110
        $response = $this->sendHttpRequest($request);
111
112
        self::assertHttpResponseCodeEquals($response, 201);
113
        self::assertHttpResponseHasHeader($response, 'Location');
114
115
        $href = $response->getHeader('Location');
116
        $this->addCreatedElement($href);
117
118
        return $href;
119
    }
120
121
    /**
122
     * @depends testCreateUrlAlias
123
     * @covers GET /content/urlaliases/{urlAliasId}
124
     */
125 View Code Duplication
    public function testLoadURLAlias($urlAliasHref)
126
    {
127
        $response = $this->sendHttpRequest(
128
            $this->createHttpRequest('GET', $urlAliasHref, '', 'UrlAlias+json')
129
        );
130
131
        self::assertHttpResponseCodeEquals($response, 200);
132
133
        $urlAlias = $this->parseUrlAliasFromResponse($response);
134
        $this->assertHttpResponseHasCacheTags(
135
            $response,
136
            [
137
                'location-' . $urlAlias->destination->id,
138
            ]
139
        );
140
    }
141
142
    /**
143
     * @depends testCreateFolder
144
     * @covers GET /content/locations/{locationPath}/urlaliases
145
     */
146
    public function testListLocationURLAliases($contentLocationHref)
147
    {
148
        $response = $this->sendHttpRequest(
149
            $this->createHttpRequest('GET', "$contentLocationHref/urlaliases", '', 'UrlAliasRefList+json')
150
        );
151
152
        self::assertHttpResponseCodeEquals($response, 200);
153
154
        $urlAliasList = $this->parseUrlAliasRefListFromResponse($response);
155
        $this->assertEquals(1, count($urlAliasList));
156
        $this->assertHttpResponseHasCacheTags(
157
            $response,
158
            [
159
                'location-' . $this->extractLocationIdFromHref($contentLocationHref),
160
            ]
161
        );
162
    }
163
164
    /**
165
     * @depends testCreateUrlAlias
166
     * @covers DELETE /content/urlaliases/{urlAliasId}
167
     */
168
    public function testDeleteURLAlias($urlAliasHref)
169
    {
170
        $response = $this->sendHttpRequest(
171
            $request = $this->createHttpRequest('DELETE', $urlAliasHref)
172
        );
173
174
        self::assertHttpResponseCodeEquals( $response, 204 );
175
    }
176
177
    /**
178
     * @param Response $response
179
     * @return URLAliasRefList
180
     */
181
    private function parseUrlAliasRefListFromResponse(Response $response)
182
    {
183
        $responseStruct = json_decode($response->getContent(), true);
184
185
        return new URLAliasRefList(
186
            [
0 ignored issues
show
Documentation introduced by
array('urlaliases' => ar...RefList']['UrlAlias'])) is of type array<string,array,{"urlaliases":"array"}>, but the function expects a array<integer,object<eZ\...lues\Content\URLAlias>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
187
                'urlaliases' => array_map(
188
                    function (array $urlAliasRefRow) {
189
                        return new URLAlias(
190
                            [
191
                                'id' => $this->extractLastIdFromHref($urlAliasRefRow['_href']),
192
                            ]
193
                        );
194
                    },
195
                    $responseStruct['UrlAliasRefList']['UrlAlias']
196
                )
197
            ],
198
            ''
199
        );
200
    }
201
202
    /**
203
     * Extracts a location id from any location path href.
204
     * @param string $href Ex: /api/ezp/v2/content/locations/1/2/3
205
     * @return int
206
     */
207
    private function extractLocationIdFromHref($href)
208
    {
209
        // iterates over href parts, and returns the last numeric part before a non numeric part
210
        foreach (explode('/', $href) as $part) {
211
            if (is_numeric($part)) {
212
                $id = $part;
213
            } else if (isset($id)) {
214
                return $id;
215
            }
216
        }
217
    }
218
219
    /**
220
     * @param Response $response
221
     * @return URLAlias
222
     */
223
    private function parseUrlAliasFromResponse(Response $response)
224
    {
225
        $responseStruct = json_decode($response->getContent(), true);
226
227
        return new URLAlias(
228
            [
229
                'id' => $responseStruct['UrlAlias']['_id'],
230
                'destination' => new Location(['id' => $this->extractLastIdFromHref($responseStruct['UrlAlias']['location']['_href'])]),
231
            ]
232
        );
233
    }
234
}
235