Skip to content

Commit

Permalink
feature/only-single-contact-required (#255)
Browse files Browse the repository at this point in the history
* Added failing unit tests

* Added logic for organisations

* Updated new organisation sign up form validation

* Updated API docs
  • Loading branch information
matthew-inamdar authored Sep 17, 2019
1 parent e514e04 commit 45442c4
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 14 deletions.
6 changes: 4 additions & 2 deletions app/Docs/Schemas/Organisation/OrganisationSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public static function create(string $objectId = null): BaseObject
Schema::string('slug'),
Schema::string('description'),
Schema::string('url'),
Schema::string('email'),
Schema::string('phone'),
Schema::string('email')
->nullable(),
Schema::string('phone')
->nullable(),
Schema::string('created_at')
->format(Schema::FORMAT_DATE_TIME)
->nullable(),
Expand Down
10 changes: 5 additions & 5 deletions app/Docs/Schemas/Organisation/UpdateOrganisationSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public static function create(string $objectId = null): BaseObject
'name',
'slug',
'description',
'url',
'email',
'phone'
'url'
)
->properties(
Schema::string('name'),
Schema::string('slug'),
Schema::string('description'),
Schema::string('url'),
Schema::string('email'),
Schema::string('phone'),
Schema::string('email')
->nullable(),
Schema::string('phone')
->nullable(),
Schema::string('logo_file_id')
->format(Schema::FORMAT_UUID)
->description('The ID of the file uploaded')
Expand Down
20 changes: 17 additions & 3 deletions app/Http/Requests/Organisation/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,26 @@ public function authorize()
public function rules()
{
return [
'slug' => ['required', 'string', 'min:1', 'max:255', 'unique:' . table(Organisation::class) . ',slug', new Slug()],
'slug' => [
'required',
'string',
'min:1',
'max:255',
'unique:' . table(Organisation::class) . ',slug',
new Slug(),
],
'name' => ['required', 'string', 'min:1', 'max:255'],
'description' => ['required', 'string', 'min:1', 'max:10000'],
'url' => ['required', 'url', 'max:255'],
'email' => ['required', 'email', 'max:255'],
'phone' => ['required', 'string', 'min:1', 'max:255'],
'email' => ['present', 'nullable', 'required_without:phone', 'email', 'max:255'],
'phone' => [
'present',
'nullable',
'required_without:email',
'string',
'min:1',
'max:255',
],
'logo_file_id' => [
'nullable',
'exists:files,id',
Expand Down
18 changes: 16 additions & 2 deletions app/Http/Requests/Organisation/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Organisation;
use App\Rules\FileIsMimeType;
use App\Rules\FileIsPendingAssignment;
use App\Rules\NullableIf;
use App\Rules\Slug;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
Expand Down Expand Up @@ -48,8 +49,21 @@ public function rules()
'name' => ['string', 'min:1', 'max:255'],
'description' => ['string', 'min:1', 'max:10000'],
'url' => ['url', 'max:255'],
'email' => ['email', 'max:255'],
'phone' => ['string', 'min:1', 'max:255'],
'email' => [
new NullableIf(function () {
return $this->input('phone', $this->organisation->phone) !== null;
}),
'email',
'max:255',
],
'phone' => [
new NullableIf(function () {
return $this->input('email', $this->organisation->email) !== null;
}),
'string',
'min:1',
'max:255',
],
'logo_file_id' => [
'nullable',
'exists:files,id',
Expand Down
15 changes: 13 additions & 2 deletions app/Http/Requests/OrganisationSignUpForm/StoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ public function rules()
'organisation.name' => ['required', 'string', 'min:1', 'max:255'],
'organisation.description' => ['required', 'string', 'min:1', 'max:10000'],
'organisation.url' => ['required', 'url', 'max:255'],
'organisation.email' => ['required', 'email', 'max:255'],
'organisation.phone' => ['required', 'string', 'min:1', 'max:255'],
'organisation.email' => [
'nullable',
'required_without:organisation.phone',
'email',
'max:255',
],
'organisation.phone' => [
'nullable',
'required_without:organisation.email',
'string',
'min:1',
'max:255',
],

'service' => ['required', 'array'],
'service.slug' => [
Expand Down
Binary file modified database/diagrams/ERD.mwb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class MakeEmailAndPhoneColumnsNullableOnOrganisationsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('organisations', function (Blueprint $table) {
$table->string('email')->nullable()->change();
$table->string('phone')->nullable()->change();
});
}

/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('organisations', function (Blueprint $table) {
$table->string('email')->nullable(false)->change();
$table->string('phone')->nullable(false)->change();
});
}
}
68 changes: 68 additions & 0 deletions tests/Feature/OrganisationSignUpFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,74 @@ public function test_guest_can_create_one()
$response->assertStatus(Response::HTTP_CREATED);
}

public function test_guest_can_create_one_with_single_form_of_contact()
{
$response = $this->json('POST', '/core/v1/organisation-sign-up-forms', [
'user' => [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'email' => $this->faker->safeEmail,
'phone' => random_uk_phone(),
],
'organisation' => [
'slug' => 'test-org',
'name' => 'Test Org',
'description' => 'Test description',
'url' => 'http://test-org.example.com',
'email' => 'info@test-org.example.com',
'phone' => null,
],
'service' => [
'slug' => 'test-service',
'name' => 'Test Service',
'type' => Service::TYPE_SERVICE,
'intro' => 'This is a test intro',
'description' => 'Lorem ipsum',
'wait_time' => null,
'is_free' => true,
'fees_text' => null,
'fees_url' => null,
'testimonial' => null,
'video_embed' => null,
'url' => $this->faker->url,
'contact_name' => $this->faker->name,
'contact_phone' => random_uk_phone(),
'contact_email' => null,
'criteria' => [
'age_group' => '18+',
'disability' => null,
'employment' => null,
'gender' => null,
'housing' => null,
'income' => null,
'language' => null,
'other' => null,
],
'useful_infos' => [
[
'title' => 'Did you know?',
'description' => 'Lorem ipsum',
'order' => 1,
],
],
'offerings' => [
[
'offering' => 'Weekly club',
'order' => 1,
],
],
'social_medias' => [
[
'type' => SocialMedia::TYPE_INSTAGRAM,
'url' => 'https://www.instagram.com/ayupdigital',
],
],
],
]);

$response->assertStatus(Response::HTTP_CREATED);
}

public function test_service_worker_cannot_create_one()
{
/** @var \App\Models\Service $service */
Expand Down
100 changes: 100 additions & 0 deletions tests/Feature/OrganisationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,53 @@ public function test_global_admin_can_create_one()
$response->assertJsonFragment($payload);
}

public function test_global_admin_can_create_one_with_single_form_of_contact()
{
/**
* @var \App\Models\User $user
*/
$user = factory(User::class)->create();
$user->makeGlobalAdmin();
$payload = [
'slug' => 'test-org',
'name' => 'Test Org',
'description' => 'Test description',
'url' => 'http://test-org.example.com',
'email' => 'info@test-org.example.com',
'phone' => null,
];

Passport::actingAs($user);

$response = $this->json('POST', '/core/v1/organisations', $payload);

$response->assertStatus(Response::HTTP_CREATED);
$response->assertJsonFragment($payload);
}

public function test_global_admin_cannot_create_one_with_no_form_of_contact()
{
/**
* @var \App\Models\User $user
*/
$user = factory(User::class)->create();
$user->makeGlobalAdmin();
$payload = [
'slug' => 'test-org',
'name' => 'Test Org',
'description' => 'Test description',
'url' => 'http://test-org.example.com',
'email' => null,
'phone' => null,
];

Passport::actingAs($user);

$response = $this->json('POST', '/core/v1/organisations', $payload);

$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
}

public function test_audit_created_when_created()
{
$this->fakeEvents();
Expand Down Expand Up @@ -321,6 +368,59 @@ public function test_organisation_admin_can_update_one()
$this->assertEquals($data, $payload);
}

public function test_organisation_admin_can_update_with_single_form_of_contact()
{
$organisation = factory(Organisation::class)->create();
$user = factory(User::class)->create()->makeOrganisationAdmin($organisation);
$payload = [
'slug' => 'test-org',
'name' => 'Test Org',
'description' => 'Test description',
'url' => 'http://test-org.example.com',
'email' => 'info@test-org.example.com',
'phone' => null,
];

Passport::actingAs($user);

$response = $this->json('PUT', "/core/v1/organisations/{$organisation->id}", $payload);

$response->assertStatus(Response::HTTP_OK);
$response->assertJsonFragment(['data' => $payload]);
$this->assertDatabaseHas((new UpdateRequest())->getTable(), [
'user_id' => $user->id,
'updateable_type' => UpdateRequest::EXISTING_TYPE_ORGANISATION,
'updateable_id' => $organisation->id,
]);
$data = UpdateRequest::query()
->where('updateable_type', UpdateRequest::EXISTING_TYPE_ORGANISATION)
->where('updateable_id', $organisation->id)
->firstOrFail()->data;
$this->assertEquals($data, $payload);
}

public function test_organisation_admin_cannot_update_with_no_form_of_contact()
{
$organisation = factory(Organisation::class)->create([
'email' => 'info@test-org.example.com',
'phone' => null,
]);
$user = factory(User::class)->create()->makeOrganisationAdmin($organisation);

Passport::actingAs($user);

$response = $this->json('PUT', "/core/v1/organisations/{$organisation->id}", [
'slug' => 'test-org',
'name' => 'Test Org',
'description' => 'Test description',
'url' => 'http://test-org.example.com',
'email' => null,
'phone' => null,
]);

$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
}

public function test_only_partial_fields_can_be_updated()
{
$organisation = factory(Organisation::class)->create();
Expand Down
Loading

0 comments on commit 45442c4

Please sign in to comment.