Skip to content

Commit

Permalink
(#4) Don't overwrite tags with "1" when reordering components
Browse files Browse the repository at this point in the history
  • Loading branch information
sedan07 authored and Seb Dangerfield committed Jan 28, 2021
1 parent 1fa60f9 commit 2ce88c2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
14 changes: 2 additions & 12 deletions app/Http/Controllers/Dashboard/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,8 @@ public function postUpdateComponentOrder()
try {
$component = Component::find($componentId);

execute(new UpdateComponentCommand(
$component,
$component->name,
$component->description,
$component->status,
$component->link,
$order + 1,
$component->group_id,
$component->enabled,
$component->meta,
true
));
$component->order = $order + 1;
$component->save();
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
Expand Down
70 changes: 70 additions & 0 deletions tests/Dashboard/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CachetHQ\Tests\Cachet\Dashboard;

use CachetHQ\Cachet\Bus\Events\Component\ComponentStatusWasChangedEvent;
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasCreatedEvent;
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasRemovedEvent;
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasUpdatedEvent;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Tests\Cachet\Api\AbstractApiTestCase;

/**
* This is the Dashboard API test class.
*/
class ApiTest extends AbstractApiTestCase
{
public function test_can_reorder_components()
{
$this->beUser();

$components = array();
$components[] = factory(Component::class)->create(['id' => 1]);
$components[] = factory(Component::class)->create(['id' => 2]);
$components[] = factory(Component::class)->create(['id' => 3]);


$response = $this->json('POST', '/dashboard/api/components/order', [
'ids' => [
0 => "3",
1 => "1",
2 => "2"
]
]);

$this->assertEquals(2, Component::find(1)->order);
$this->assertEquals(3, Component::find(2)->order);
$this->assertEquals(1, Component::find(3)->order);
}

public function test_reordering_components_retains_tags()
{
$this->beUser();

$components = array();
$components[] = factory(Component::class)->create(['id' => 1]);
$components[] = factory(Component::class)->create(['id' => 2]);
$components[0]->attachTags(['Internal']);
$components[1]->attachTags(['Foo', 'Bar']);


$response = $this->json('POST', '/dashboard/api/components/order', [
'ids' => [
0 => "2",
1 => "1",
]
]);

$this->assertEquals("Internal", Component::find(1)->tags[0]->name);
$this->assertEquals(2, Component::find(1)->order);
}
}

0 comments on commit 2ce88c2

Please sign in to comment.