Skip to content

Commit

Permalink
EZP-28801: As a REST consumer, I want to get the location that matche…
Browse files Browse the repository at this point in the history
…s an URL alias (ezsystems#2255)
  • Loading branch information
ViniTou authored and alongosz committed Feb 22, 2018
1 parent 1fb1b70 commit 0b53cc2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
7 changes: 4 additions & 3 deletions doc/specifications/rest/REST-API-V2.rst
Expand Up @@ -1874,13 +1874,14 @@ XML Example
<Location href="/content/locations/1/4/73/133" media-type="application/vnd.ez.api.Location+xml"/>
</LocationList>
Load locations by id
Load locations by id/remoteId/urlAlias
````````````````````
:Resource: /content/locations
:Method: GET
:Description: loads the location for a given id (x)or remote id
:Description: loads the location for a given id (x), remote id or url alias.
:Parameters: :id: the id of the location. If present the location is with the given id is returned.
:remoteId: the remoteId of the location. If present the location with the given remoteId is returned
:urlAlias: one of the url Aliases of the location. If present the location with given url Alias is returned
:Response:

.. code:: http
Expand All @@ -1889,7 +1890,7 @@ Load locations by id
Location: /content/locations/<path>
:Error Codes:
:404: If the location with the given id (remoteId) does not exist
:404: If the location with the given id (remoteId or urlAlias) does not exist

Load location
`````````````
Expand Down
Expand Up @@ -178,6 +178,7 @@ services:
- "@ezpublish.api.service.location"
- "@ezpublish.api.service.content"
- "@ezpublish.api.service.trash"
- "@ezpublish.api.service.url_alias"

ezpublish_rest.controller.object_state:
class: "%ezpublish_rest.controller.object_state.class%"
Expand Down
45 changes: 45 additions & 0 deletions eZ/Bundle/EzPublishRestBundle/Tests/Functional/LocationTest.php
Expand Up @@ -76,6 +76,23 @@ public function testRedirectLocationById($locationHref)
self::assertHttpResponseHasHeader($response, 'Location', $locationHref);
}

/**
* @depends testCreateLocation
* Covers GET /content/locations?urlAlias=<Path/To-Content>
*/
public function testRedirectLocationByURLAlias($locationHref)
{
$testUrlAlias = 'firstPart/secondPart/testUrlAlias';
$this->createUrlAlias($locationHref, $testUrlAlias);

$response = $this->sendHttpRequest(
$this->createHttpRequest('GET', "/api/ezp/v2/content/locations?urlAlias={$testUrlAlias}")
);

self::assertHttpResponseCodeEquals($response, 307);
self::assertHttpResponseHasHeader($response, 'Location', $locationHref);
}

/**
* @depends testCreateLocation
* Covers GET /content/locations/{locationPath}
Expand Down Expand Up @@ -194,4 +211,32 @@ public function testDeleteSubtree($locationHref)

self::assertHttpResponseCodeEquals($response, 204);
}

private function createUrlAlias(string $locationHref, string $urlAlias): string
{
$xml = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<UrlAliasCreate type="LOCATION">
<location href="{$locationHref}" />
<path>/{$urlAlias}</path>
<languageCode>eng-GB</languageCode>
<alwaysAvailable>false</alwaysAvailable>
<forward>true</forward>
</UrlAliasCreate>
XML;

$request = $this->createHttpRequest(
'POST',
'/api/ezp/v2/content/urlaliases',
'UrlAliasCreate+xml',
'UrlAlias+json'
);
$request->setContent($xml);

$response = $this->sendHttpRequest($request);
$href = $response->getHeader('Location');
$this->addCreatedElement($href);

return $href;
}
}
29 changes: 22 additions & 7 deletions eZ/Publish/Core/REST/Server/Controller/Location.php
Expand Up @@ -8,6 +8,7 @@
*/
namespace eZ\Publish\Core\REST\Server\Controller;

use eZ\Publish\API\Repository\URLAliasService;
use eZ\Publish\Core\REST\Common\Message;
use eZ\Publish\Core\REST\Common\Exceptions;
use eZ\Publish\Core\REST\Server\Values;
Expand Down Expand Up @@ -46,18 +47,31 @@ class Location extends RestController
*/
protected $trashService;

/**
* URLAlias Service.
*
* @var \eZ\Publish\API\Repository\URLAliasService
*/
protected $urlAliasService;

/**
* Construct controller.
*
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\TrashService $trashService
* @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
*/
public function __construct(LocationService $locationService, ContentService $contentService, TrashService $trashService)
{
public function __construct(
LocationService $locationService,
ContentService $contentService,
TrashService $trashService,
URLAliasService $urlAliasService
) {
$this->locationService = $locationService;
$this->contentService = $contentService;
$this->trashService = $trashService;
$this->urlAliasService = $urlAliasService;
}

/**
Expand All @@ -69,14 +83,15 @@ public function __construct(LocationService $locationService, ContentService $co
*/
public function redirectLocation(Request $request)
{
if (!$request->query->has('id') && !$request->query->has('remoteId')) {
throw new BadRequestException("At least one of 'id' or 'remoteId' parameters is required.");
}

if ($request->query->has('id')) {
$location = $this->locationService->loadLocation($request->query->get('id'));
} else {
} elseif ($request->query->has('remoteId')) {
$location = $this->locationService->loadLocationByRemoteId($request->query->get('remoteId'));
} elseif ($request->query->has('urlAlias')) {
$urlAlias = $this->urlAliasService->lookup($request->query->get('urlAlias'));
$location = $this->locationService->loadLocation($urlAlias->destination);
} else {
throw new BadRequestException("At least one of 'id', 'remoteId' or 'urlAlias' parameters is required.");
}

return new Values\TemporaryRedirect(
Expand Down

0 comments on commit 0b53cc2

Please sign in to comment.