2015-06-01 21:32:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-06-01 21:32:02 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace CachetHQ\Tests\Cachet\Api;
|
|
|
|
|
2018-06-17 09:04:14 +01:00
|
|
|
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasCreatedEvent;
|
|
|
|
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasRemovedEvent;
|
|
|
|
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasUpdatedEvent;
|
2018-06-16 21:25:23 +01:00
|
|
|
use CachetHQ\Cachet\Models\Component;
|
2018-06-16 21:21:38 +01:00
|
|
|
use CachetHQ\Cachet\Models\Incident;
|
|
|
|
use CachetHQ\Cachet\Models\IncidentTemplate;
|
|
|
|
|
2015-12-06 11:04:02 +00:00
|
|
|
/**
|
|
|
|
* This is the incident test class.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
* @author Graham Campbell <graham@alt-three.com>
|
|
|
|
*/
|
|
|
|
class IncidentTest extends AbstractApiTestCase
|
2015-06-01 21:32:02 +01:00
|
|
|
{
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_get_all_incidents()
|
2015-06-01 21:32:02 +01:00
|
|
|
{
|
2018-06-16 21:21:38 +01:00
|
|
|
$incidents = factory(Incident::class, 3)->create();
|
|
|
|
|
|
|
|
$response = $this->json('GET', '/api/v1/incidents');
|
2015-05-23 19:09:24 +01:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
$response->assertJsonFragment(['id' => $incidents[0]->id]);
|
|
|
|
$response->assertJsonFragment(['id' => $incidents[1]->id]);
|
|
|
|
$response->assertJsonFragment(['id' => $incidents[2]->id]);
|
2015-06-01 21:32:02 +01:00
|
|
|
}
|
2015-06-02 14:19:24 +01:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_cannot_get_invalid_component()
|
2015-06-02 14:19:24 +01:00
|
|
|
{
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('GET', '/api/v1/incidents/0');
|
|
|
|
|
|
|
|
$response->assertStatus(404);
|
2015-06-02 14:19:24 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_cannot_create_incident_without_authorization()
|
2015-06-02 14:19:24 +01:00
|
|
|
{
|
2018-06-17 09:04:14 +01:00
|
|
|
$this->doesntExpectEvents(IncidentWasCreatedEvent::class);
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('POST', '/api/v1/incidents');
|
|
|
|
|
|
|
|
$response->assertStatus(401);
|
2015-06-02 14:19:24 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_cannot_create_incident_with_missing_data()
|
2015-06-02 14:19:24 +01:00
|
|
|
{
|
|
|
|
$this->beUser();
|
|
|
|
|
2018-06-17 09:04:14 +01:00
|
|
|
$this->doesntExpectEvents(IncidentWasCreatedEvent::class);
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('POST', '/api/v1/incidents');
|
|
|
|
|
|
|
|
$response->assertStatus(400);
|
2015-06-02 14:19:24 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_create_incident()
|
2015-06-02 14:19:24 +01:00
|
|
|
{
|
|
|
|
$this->beUser();
|
|
|
|
|
2018-06-17 09:04:14 +01:00
|
|
|
$this->expectsEvents(IncidentWasCreatedEvent::class);
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('POST', '/api/v1/incidents', [
|
2016-08-17 01:12:21 +02:00
|
|
|
'name' => 'Foo',
|
|
|
|
'message' => 'Lorem ipsum dolor sit amet',
|
|
|
|
'status' => 1,
|
|
|
|
'visible' => 1,
|
|
|
|
'stickied' => false,
|
2015-06-02 14:19:24 +01:00
|
|
|
]);
|
2018-06-16 21:21:38 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment(['name' => 'Foo']);
|
2015-06-02 14:19:24 +01:00
|
|
|
}
|
2015-06-02 19:48:09 +01:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_create_incident_with_component_status()
|
2016-01-29 16:42:04 +00:00
|
|
|
{
|
2018-06-16 21:25:23 +01:00
|
|
|
$component = factory(Component::class)->create();
|
2016-01-29 16:42:04 +00:00
|
|
|
|
|
|
|
$this->beUser();
|
|
|
|
|
2018-06-17 09:04:14 +01:00
|
|
|
$this->expectsEvents(IncidentWasCreatedEvent::class);
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('POST', '/api/v1/incidents', [
|
2016-01-29 16:42:04 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'message' => 'Lorem ipsum dolor sit amet',
|
|
|
|
'status' => 1,
|
|
|
|
'component_id' => $component->id,
|
|
|
|
'component_status' => 1,
|
|
|
|
'visible' => 1,
|
2016-08-17 01:12:21 +02:00
|
|
|
'stickied' => false,
|
2016-01-29 16:42:04 +00:00
|
|
|
]);
|
2018-06-16 21:21:38 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment(['name' => 'Foo']);
|
2016-01-29 16:42:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_create_incident_with_template()
|
2015-11-12 14:55:25 +00:00
|
|
|
{
|
2018-06-16 21:21:38 +01:00
|
|
|
$template = factory(IncidentTemplate::class)->create();
|
2015-11-12 14:55:25 +00:00
|
|
|
$this->beUser();
|
|
|
|
|
2018-06-17 09:04:14 +01:00
|
|
|
$this->expectsEvents(IncidentWasCreatedEvent::class);
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('POST', '/api/v1/incidents', [
|
2015-11-12 14:55:25 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'status' => 1,
|
|
|
|
'visible' => 1,
|
2016-08-17 01:12:21 +02:00
|
|
|
'stickied' => false,
|
2015-11-12 14:55:25 +00:00
|
|
|
'template' => $template->slug,
|
|
|
|
'vars' => [
|
|
|
|
'name' => 'Foo',
|
|
|
|
'message' => 'Hello there this is a foo!',
|
|
|
|
],
|
|
|
|
]);
|
2018-06-16 21:21:38 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment([
|
2015-11-12 14:55:25 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'message' => "Name: Foo,\nMessage: Hello there this is a foo!",
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_get_newly_created_incident()
|
2015-06-02 19:48:09 +01:00
|
|
|
{
|
2018-06-16 21:21:38 +01:00
|
|
|
$incident = factory(Incident::class)->create();
|
2015-06-02 19:48:09 +01:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('GET', '/api/v1/incidents/1');
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment(['name' => $incident->name]);
|
2015-06-02 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_update_incident()
|
2015-06-02 19:48:09 +01:00
|
|
|
{
|
|
|
|
$this->beUser();
|
2018-06-17 09:04:14 +01:00
|
|
|
$incident = factory(Incident::class)->create();
|
|
|
|
|
|
|
|
$this->expectsEvents(IncidentWasUpdatedEvent::class);
|
2015-06-02 19:48:09 +01:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('PUT', '/api/v1/incidents/1', [
|
2015-06-02 19:48:09 +01:00
|
|
|
'name' => 'Foo',
|
|
|
|
]);
|
2018-06-16 21:21:38 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment(['name' => 'Foo']);
|
2015-06-02 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_update_incident_with_template()
|
2015-11-12 14:55:25 +00:00
|
|
|
{
|
|
|
|
$this->beUser();
|
2018-06-16 21:21:38 +01:00
|
|
|
$template = factory(IncidentTemplate::class)->create([
|
2016-10-14 08:03:19 +01:00
|
|
|
'template' => 'Hello there this is a foo in my {{ incident.name }}!',
|
|
|
|
]);
|
2018-06-17 09:04:14 +01:00
|
|
|
$incident = factory(Incident::class)->create();
|
|
|
|
|
|
|
|
$this->expectsEvents(IncidentWasUpdatedEvent::class);
|
2015-11-12 14:55:25 +00:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('PUT', '/api/v1/incidents/1', [
|
2015-11-12 14:55:25 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'template' => $template->slug,
|
|
|
|
]);
|
2018-06-16 21:21:38 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment([
|
2015-11-12 14:55:25 +00:00
|
|
|
'name' => 'Foo',
|
2016-10-14 08:03:19 +01:00
|
|
|
'message' => 'Hello there this is a foo in my Foo!',
|
2015-11-12 14:55:25 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:12:34 +02:00
|
|
|
public function test_can_update_incident_when_no_user_is_associated()
|
|
|
|
{
|
|
|
|
$incident = factory(Incident::class)->create(['user_id' => null]);
|
|
|
|
$this->beUser();
|
|
|
|
$this->expectsEvents(IncidentWasUpdatedEvent::class);
|
|
|
|
|
|
|
|
$response = $this->json('PUT', '/api/v1/incidents/1', [
|
|
|
|
'name' => 'Updated incident name',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment([
|
|
|
|
'name' => 'Updated incident name',
|
|
|
|
'user_id' => null,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_delete_incident()
|
2015-06-02 19:48:09 +01:00
|
|
|
{
|
|
|
|
$this->beUser();
|
2018-06-17 09:04:14 +01:00
|
|
|
$incident = factory(Incident::class)->create();
|
|
|
|
|
|
|
|
$this->expectsEvents(IncidentWasRemovedEvent::class);
|
2018-06-16 21:21:38 +01:00
|
|
|
|
|
|
|
$response = $this->json('DELETE', '/api/v1/incidents/1');
|
2015-06-02 19:48:09 +01:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response->assertStatus(204);
|
2015-06-02 19:48:09 +01:00
|
|
|
}
|
2017-06-13 19:34:50 +01:00
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
public function test_can_create_incident_with_meta_data()
|
2017-06-13 19:34:50 +01:00
|
|
|
{
|
|
|
|
$this->beUser();
|
|
|
|
|
2018-06-17 09:04:14 +01:00
|
|
|
$this->expectsEvents(IncidentWasCreatedEvent::class);
|
|
|
|
|
2018-06-16 21:21:38 +01:00
|
|
|
$response = $this->json('POST', '/api/v1/incidents', [
|
2017-06-13 19:34:50 +01:00
|
|
|
'name' => 'Foo',
|
|
|
|
'message' => 'Lorem ipsum dolor sit amet',
|
|
|
|
'status' => 1,
|
|
|
|
'meta' => [
|
|
|
|
'id' => 123456789,
|
|
|
|
],
|
|
|
|
]);
|
2018-06-16 21:21:38 +01:00
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJsonFragment([
|
2017-06-13 19:34:50 +01:00
|
|
|
'meta' => [
|
|
|
|
'id' => 123456789,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
2015-06-01 21:32:02 +01:00
|
|
|
}
|