7e1cd96bf5
This is the first part of the API. It supports most of contacts, as well as the journal. This PR also brings Monica to Laravel 5.5, as the API required it with the new concept of resources. It needs to run `php artisan migrate` as well as `php artisan passport:keys` to generate the access tokens needed for the API.
192 lines
6.2 KiB
PHP
192 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Contact;
|
|
use Tests\FeatureTestCase;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
class ActivityTest extends FeatureTestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
/**
|
|
* Returns an array containing a user object along with
|
|
* a contact for that user.
|
|
* @return array
|
|
*/
|
|
private function fetchUser()
|
|
{
|
|
$user = $this->signIn();
|
|
|
|
$contact = factory(Contact::class)->create([
|
|
'account_id' => $user->account_id,
|
|
]);
|
|
|
|
return [$user, $contact];
|
|
}
|
|
|
|
public function test_user_sees_empty_state_when_no_activities_are_available()
|
|
{
|
|
list($user, $contact) = $this->fetchUser();
|
|
|
|
$response = $this->get('/people/'.$contact->id);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
// is the default blank state present?
|
|
$response->assertSee(
|
|
'Add an activity'
|
|
);
|
|
|
|
// is the button to add a note present?
|
|
$response->assertSee('Add activity');
|
|
}
|
|
|
|
public function test_user_sees_add_activity_screen()
|
|
{
|
|
list($user, $contact) = $this->fetchUser();
|
|
|
|
$response = $this->get('/activities/add/'.$contact->id);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee(
|
|
'What did you do with John?'
|
|
);
|
|
}
|
|
|
|
public function test_user_can_add_an_activity()
|
|
{
|
|
list($user, $contact) = $this->fetchUser();
|
|
|
|
$activityTitle = 'This is the title';
|
|
$activityDate = \Carbon\Carbon::now();
|
|
|
|
$params = [
|
|
'summary' => $activityTitle,
|
|
'date_it_happened' => $activityDate,
|
|
];
|
|
|
|
$response = $this->post('/activities/store', $params + ['contacts' => [$contact->id]]);
|
|
$response->assertRedirect('/people/'.$contact->id);
|
|
|
|
// Assert the activity has been added
|
|
$params['account_id'] = $user->account_id;
|
|
$params['summary'] = $activityTitle;
|
|
$params['date_it_happened'] = $activityDate;
|
|
|
|
$this->assertDatabaseHas('activities', $params);
|
|
|
|
// Get the activity that we just created
|
|
// and make sure it's in our pivot table
|
|
$latestActivity = \App\Activity::all('id')->last();
|
|
|
|
$this->assertDatabaseHas('activity_contact', [
|
|
'contact_id' => $contact->id,
|
|
'activity_id' => $latestActivity->id,
|
|
]);
|
|
|
|
// Make sure an event has been created for this action
|
|
$eventParams['account_id'] = $user->account_id;
|
|
$eventParams['contact_id'] = $contact->id;
|
|
$eventParams['object_type'] = 'activity';
|
|
$eventParams['nature_of_operation'] = 'create';
|
|
$this->assertDatabaseHas('events', $eventParams);
|
|
|
|
// Check that the Contact view contains the newly created note
|
|
$response = $this->get('/people/'.$contact->id);
|
|
$response->assertSee($activityTitle);
|
|
|
|
// Visit the dashboard and checks that the note event appears on the
|
|
// dashboard
|
|
$response = $this->get('/dashboard');
|
|
$response->assertSee('An activity about John Doe has been added');
|
|
$response->assertSee('<a href="/people/'.$contact->id.'" id="activity_create');
|
|
}
|
|
|
|
public function test_user_can_edit_an_activity()
|
|
{
|
|
list($user, $contact) = $this->fetchUser();
|
|
|
|
$activity = factory(\App\Activity::class)->create([
|
|
'account_id' => $user->account_id,
|
|
'summary' => 'This is the title',
|
|
'date_it_happened' => \Carbon\Carbon::now(),
|
|
]);
|
|
|
|
// Attach the created activity to the current user to make this an update
|
|
$contact->activities()->save($activity);
|
|
|
|
// check that we can access the edit activity view
|
|
$response = $this->get('/activities/'.$activity->id.'/edit/'.$contact->id);
|
|
$response->assertStatus(200);
|
|
|
|
// now edit the activity
|
|
$params = [
|
|
'contacts' => [$contact->id],
|
|
'summary' => 'this is another test',
|
|
'date_it_happened' => \Carbon\Carbon::now(),
|
|
'activity_type_id' => null,
|
|
'description' => null,
|
|
];
|
|
|
|
$this->put('/activities/'.$activity->id, $params);
|
|
|
|
// see if the change is in the database
|
|
$newParams['account_id'] = $user->account_id;
|
|
$newParams['id'] = $activity->id;
|
|
$newParams['summary'] = 'this is another test';
|
|
|
|
$this->assertDatabaseHas('activities', $newParams);
|
|
|
|
// make sure an event has been created for this action
|
|
$eventParams['account_id'] = $user->account_id;
|
|
$eventParams['contact_id'] = $contact->id;
|
|
$eventParams['object_type'] = 'activity';
|
|
$eventParams['object_id'] = $activity->id;
|
|
$eventParams['nature_of_operation'] = 'update';
|
|
|
|
$this->assertDatabaseHas('events', $eventParams);
|
|
|
|
// Visit the dashboard and checks that the note event appears on the
|
|
// dashboard
|
|
$response = $this->get('/dashboard');
|
|
$response->assertSee('An activity about John Doe has been updated');
|
|
$response->assertSee('<a href="/people/'.$contact->id.'" id="activity_update_'.$activity->id.'">');
|
|
}
|
|
|
|
public function test_user_can_delete_an_activity()
|
|
{
|
|
list($user, $contact) = $this->fetchUser();
|
|
|
|
$activity = factory(\App\Activity::class)->create([
|
|
'account_id' => $user->account_id,
|
|
'summary' => 'This is the title',
|
|
'date_it_happened' => \Carbon\Carbon::now(),
|
|
]);
|
|
|
|
// Attach the created activity to the current user to make this an update
|
|
$contact->activities()->save($activity);
|
|
|
|
$response = $this->delete('/activities/'.$activity->id);
|
|
$response->assertStatus(302);
|
|
|
|
$params['id'] = $activity->id;
|
|
|
|
$this->assertDatabaseMissing('activities', $params);
|
|
|
|
$this->assertDatabaseMissing('activity_contact', [
|
|
'activity_id' => $activity->id,
|
|
'contact_id' => $contact->id,
|
|
]);
|
|
|
|
// make sure an event has been created for this action
|
|
$eventParams['account_id'] = $user->account_id;
|
|
$eventParams['contact_id'] = $contact->id;
|
|
$eventParams['object_id'] = $activity->id;
|
|
|
|
$this->assertDatabaseMissing('events', $eventParams);
|
|
}
|
|
}
|