Skip to content

Commit

Permalink
Return route id in menus API instead of an object (#258)
Browse files Browse the repository at this point in the history
* Return route id in menus API instead of an object

* fixed migration script

* ByLine is required in storage but it's not required by validation

* tests
  • Loading branch information
takeit authored Feb 20, 2017
1 parent 407dff2 commit 5edf24c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 2 deletions.
3 changes: 1 addition & 2 deletions app/migrations/Version20170217123721.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function up(Schema $schema)
$this->addSql('ALTER TABLE swp_article ALTER keywords DROP NOT NULL');
$this->addSql('DROP INDEX swp_name_idx');
$this->addSql('ALTER TABLE swp_container ADD revision_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE swp_container ADD uuid VARCHAR(255) NOT NULL');
$this->addSql('ALTER TABLE swp_container ADD uuid VARCHAR(255) NOT NULL DEFAULT substr(md5(random()::text), 0, 12);');
$this->addSql('ALTER TABLE swp_container DROP width');
$this->addSql('ALTER TABLE swp_container DROP height');
$this->addSql('ALTER TABLE swp_container ALTER created_at SET DEFAULT \'now\'');
Expand All @@ -50,7 +50,6 @@ public function down(Schema $schema)
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE swp_revision_log DROP CONSTRAINT FK_A1F96AFD9AC03385');
$this->addSql('ALTER TABLE swp_revision_log DROP CONSTRAINT FK_A1F96AFD21852C2F');
$this->addSql('ALTER TABLE swp_container DROP CONSTRAINT FK_CF0E49301DFA7C8F');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ SWP\Component\Bridge\Model\Item:
type: string
byline:
type: string
nullable: true
language:
type: string
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SWP\Component\Bridge\Model\Package:
type: string
byline:
type: string
nullable: true
language:
type: string
description:
Expand Down
5 changes: 5 additions & 0 deletions src/SWP/Bundle/ContentBundle/Model/RouteAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public function getRoute();
* @param RouteInterface|null $route
*/
public function setRoute(RouteInterface $route = null);

/**
* @return int|null
*/
public function getRouteId();
}
24 changes: 24 additions & 0 deletions src/SWP/Bundle/ContentBundle/Model/RouteAwareTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Superdesk Web Publisher Content Bundle.
*
* Copyright 2017 Sourcefabric z.ú. and contributors.
*
* For the full copyright and license information, please see the
* AUTHORS and LICENSE files distributed with this source code.
*
* @copyright 2017 Sourcefabric z.ú
* @license http://www.superdesk.org/license
*/

namespace SWP\Bundle\ContentBundle\Model;

trait RouteAwareTrait
Expand All @@ -24,4 +38,14 @@ public function setRoute(RouteInterface $route = null)
{
$this->route = $route;
}

/**
* @return int|null
*/
public function getRouteId()
{
if (null !== $this->route) {
return $this->route->getId();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ SWP\Bundle\CoreBundle\Model\MenuItem:
properties:
route:
expose: true
type: integer
accessor:
getter: getRouteId
relations:
- rel: self
href:
Expand Down
37 changes: 37 additions & 0 deletions src/SWP/Bundle/CoreBundle/Tests/Controller/MenuControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,41 @@ private function assertCreatingChildren()

return $firstChild;
}

public function testAssignRouteToMenuApi()
{
$client = static::createClient();
$client->request('POST', $this->router->generate('swp_api_core_create_menu'), [
'menu' => [
'name' => 'navigation',
'label' => 'Navigation',
],
]);

self::assertEquals(201, $client->getResponse()->getStatusCode());
$content = $client->getResponse()->getContent();

self::assertContains('"name":"navigation"', $content);
self::assertContains('"label":"Navigation"', $content);
self::assertContains('"route":null', $content);

$client->request('POST', $this->router->generate('swp_api_content_create_routes'), [
'route' => [
'name' => 'my-menu-route',
'type' => 'collection',
],
]);

self::assertEquals(201, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);

$client->request('PATCH', $this->router->generate('swp_api_core_update_menu', ['id' => 1]), [
'menu' => [
'route' => $content['id'],
],
]);

self::assertEquals(200, $client->getResponse()->getStatusCode());
self::assertContains('"route":3', $content);
}
}

0 comments on commit 5edf24c

Please sign in to comment.