From 9accf90c32c0c9efaf0f420e0f1809bc2a7e8e46 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sun, 17 Jun 2018 17:52:34 +0100 Subject: [PATCH] Test for status changes before firing event. Fixes #3082 --- .../Component/UpdateComponentCommand.php | 32 ++++++++-------- .../UpdateComponentCommandHandler.php | 4 +- .../Controllers/Api/ComponentController.php | 2 +- tests/Api/ComponentTest.php | 38 +++++++++++++++++++ 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/app/Bus/Commands/Component/UpdateComponentCommand.php b/app/Bus/Commands/Component/UpdateComponentCommand.php index 8bb054223cd3..96550fd63555 100644 --- a/app/Bus/Commands/Component/UpdateComponentCommand.php +++ b/app/Bus/Commands/Component/UpdateComponentCommand.php @@ -25,49 +25,49 @@ final class UpdateComponentCommand /** * The component name. * - * @var string + * @var string|null */ public $name; /** * The component description. * - * @var string + * @var string|null */ public $description; /** * The component status. * - * @var int + * @var int|null */ public $status; /** * The component link. * - * @var string + * @var string|null */ public $link; /** * The component order. * - * @var int + * @var int|null */ public $order; /** * The component group. * - * @var int + * @var int|null */ public $group_id; /** * Is the component enabled? * - * @var bool + * @var bool|null */ public $enabled; @@ -106,24 +106,24 @@ final class UpdateComponentCommand * Create a new update component command instance. * * @param \CachetHQ\Cachet\Models\Component $component - * @param string $name - * @param string $description - * @param int $status - * @param string $link - * @param int $order - * @param int $group_id - * @param bool $enabled + * @param string|null $name + * @param string|null $description + * @param int|null $status + * @param string|null $link + * @param int|null $order + * @param int|null $group_id + * @param bool|null $enabled * @param array|null $meta * @param bool $silent * * @return void */ - public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled, $meta, $silent) + public function __construct(Component $component, $name = null, $description = null, $status = null, $link = null, $order = null, $group_id = null, $enabled = null, $meta = null, $silent = null) { $this->component = $component; $this->name = $name; $this->description = $description; - $this->status = (int) $status; + $this->status = $status; $this->link = $link; $this->order = $order; $this->group_id = $group_id; diff --git a/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php b/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php index 736ff23cce44..6598b166540f 100644 --- a/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php +++ b/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php @@ -50,7 +50,9 @@ public function handle(UpdateComponentCommand $command) $component = $command->component; $originalStatus = $component->status; - event(new ComponentStatusWasChangedEvent($this->auth->user(), $component, $originalStatus, $command->status, $command->silent)); + if ($command->status && (int) $originalStatus !== (int) $command->status) { + event(new ComponentStatusWasChangedEvent($this->auth->user(), $component, $originalStatus, $command->status, $command->silent)); + } $component->update($this->filter($command)); diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 2717d626f7c0..4b9d2964b948 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -119,7 +119,7 @@ public function update(Component $component) Binput::get('link'), Binput::get('order'), Binput::get('group_id'), - (bool) Binput::get('enabled', true), + (bool) Binput::get('enabled'), Binput::get('meta', null), (bool) Binput::get('silent', false) )); diff --git a/tests/Api/ComponentTest.php b/tests/Api/ComponentTest.php index a67e603695d8..a63f810245ce 100644 --- a/tests/Api/ComponentTest.php +++ b/tests/Api/ComponentTest.php @@ -11,6 +11,7 @@ namespace CachetHQ\Tests\Cachet\Api; +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; @@ -176,6 +177,43 @@ public function test_can_update_component() $response->assertJsonFragment(['name' => 'Foo']); } + public function test_can_update_component_without_status_change() + { + $this->beUser(); + $component = factory(Component::class)->create(); + + $this->expectsEvents(ComponentWasUpdatedEvent::class); + $this->doesntExpectEvents(ComponentStatusWasChangedEvent::class); + + $response = $this->json('PUT', '/api/v1/components/1', [ + 'name' => 'Foo', + ]); + + $response->assertStatus(200); + $response->assertJsonFragment(['name' => 'Foo']); + } + + public function test_can_update_component_with_status_change() + { + $this->beUser(); + $component = factory(Component::class)->create([ + 'status' => 1, + ]); + + $this->expectsEvents([ + ComponentWasUpdatedEvent::class, + ComponentStatusWasChangedEvent::class, + ]); + + $response = $this->json('PUT', '/api/v1/components/1', [ + 'name' => 'Foo', + 'status' => 2, + ]); + + $response->assertStatus(200); + $response->assertJsonFragment(['name' => 'Foo', 'status' => 2]); + } + public function test_can_update_component_with_meta_data() { $this->beUser();