Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix contact create with birthdate age 0 #2624

Merged
merged 6 commits into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Enhancements:

Fixes:

* Fix contact create with birthdate age 0
* Fix contact link create on job queue
* Fix /settings/dav route
* Fix display relationship without a ofContact property
Expand Down
15 changes: 6 additions & 9 deletions app/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services;

use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;

abstract class BaseService
Expand All @@ -18,7 +19,7 @@ public function rules()
}

/**
* Validate all documents in an account.
* Validate all datas to execute the service.
*
* @param array $data
* @return bool
Expand All @@ -40,11 +41,9 @@ public function validate(array $data) : bool
*/
protected function nullOrValue($data, $index)
{
if (empty($data[$index])) {
return;
}
$value = Arr::get($data, $index, null);

return $data[$index] == '' ? null : $data[$index];
return is_null($value) || $value === '' ? null : $value;
}

/**
Expand All @@ -56,10 +55,8 @@ protected function nullOrValue($data, $index)
*/
protected function nullOrDate($data, $index)
{
if (empty($data[$index])) {
return;
}
$value = Arr::get($data, $index, null);

return $data[$index] == '' ? null : Carbon::parse($data[$index]);
return is_null($value) || $value === '' ? null : Carbon::parse($value);
}
}
34 changes: 30 additions & 4 deletions app/Services/Contact/Contact/UpdateBirthdayInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

namespace App\Services\Contact\Contact;

use Illuminate\Support\Arr;
use App\Services\BaseService;
use App\Models\Contact\Contact;
use Illuminate\Validation\Rule;
use App\Models\Contact\Reminder;
use App\Models\Instance\SpecialDate;
use App\Services\Contact\Reminder\CreateReminder;
use App\Services\Contact\Reminder\DestroyReminder;

class UpdateBirthdayInformation extends BaseService
{
/**
* @var array
*/
public $data;

/**
* Get the validation rules that apply to the service.
*
Expand All @@ -22,11 +29,29 @@ public function rules()
'account_id' => 'required|integer|exists:accounts,id',
'contact_id' => 'required|integer|exists:contacts,id',
'is_date_known' => 'required|boolean',
'day' => 'nullable|integer',
'month' => 'nullable|integer',
'year' => 'nullable|integer',
'is_age_based' => 'nullable|boolean',
'age' => 'nullable|integer',
'day' => [
'integer',
'nullable',
Rule::requiredIf(function () {
return Arr::get($this->data, 'is_date_known', false) && ! Arr::get($this->data, 'is_age_based', false);
}),
],
'month' => [
'integer',
'nullable',
Rule::requiredIf(function () {
return Arr::get($this->data, 'is_date_known', false) && ! Arr::get($this->data, 'is_age_based', false);
}),
],
'year' => 'nullable|integer',
'age' => [
'integer',
'nullable',
Rule::requiredIf(function () {
return Arr::get($this->data, 'is_date_known', false) && Arr::get($this->data, 'is_age_based', false);
}),
],
'add_reminder' => 'nullable|boolean',
];
}
Expand All @@ -39,6 +64,7 @@ public function rules()
*/
public function execute(array $data)
{
$this->data = $data;
$this->validate($data);

$contact = Contact::where('account_id', $data['account_id'])
Expand Down
2 changes: 1 addition & 1 deletion app/Services/VCard/ImportVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,11 +672,11 @@ private function importBirthday(Contact $contact, VCard $entry): void
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'is_date_known' => true,
'is_age_based' => false,
'day' => $birthdate->day,
'month' => $birthdate->month,
'year' => $is_year_unknown ? null : $birthdate->year,
'add_reminder' => true,
'is_age_based' => null,
]);
}
}
Expand Down
5 changes: 1 addition & 4 deletions tests/Unit/Helpers/RandomHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public function test_it_returns_a_unique_uuid()
36,
strlen(RandomHelper::uuid())
);
$this->assertInternalType(
'string',
RandomHelper::uuid()
);
$this->assertIsString(RandomHelper::uuid());
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Jobs/ScheduleStayInTouchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function ($notification, $channels) use ($contact) {
$notifications = NotificationFacade::sent($user, StayInTouchEmail::class);
$message = $notifications[0]->toMail($user);

$this->assertArraySubset(['You asked to be reminded to stay in touch with John Doe every 5 days.'], $message->introLines);
$this->assertContains('You asked to be reminded to stay in touch with John Doe every 5 days.', $message->introLines);

$this->assertDatabaseHas('contacts', [
'stay_in_touch_trigger_date' => '2017-01-06 07:00:00',
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Models/IdHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ public function testGetIDback()
$this->assertEquals($test_id, $result_id);
}

/**
* @expectedException App\Exceptions\WrongIdException
*/
public function test_bad_id_get_exception()
{
$idHasher = new IdHasher();

$test_id = rand();

$this->expectException(\App\Exceptions\WrongIdException::class);

$idHasher->decodeId($test_id);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Models/NoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ public function testGetCreatedAtReturnsAString()
$note = new Note;
$note->created_at = '2017-01-22 17:56:03';

$this->assertInternalType('string', $note->getCreatedAt());
$this->assertIsString($note->getCreatedAt());
}

public function testGetContentReturnsAString()
{
$note = factory(Note::class)->make();

$this->assertInternalType('string', $note->getContent());
$this->assertIsString($note->getContent());
}
}