refactor: use services in import vcard (#5513)
This commit is contained in:
@@ -2,14 +2,17 @@
|
||||
|
||||
namespace Tests\Unit\Services\VCard;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Contact\Tag;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Account\Account;
|
||||
use App\Models\Contact\Address;
|
||||
use App\Models\Contact\Contact;
|
||||
use Sabre\VObject\Component\VCard;
|
||||
use App\Models\Account\AddressBook;
|
||||
use App\Services\VCard\ImportVCard;
|
||||
use App\Models\Contact\ContactField;
|
||||
use Sabre\VObject\PHPUnitAssertions;
|
||||
@@ -245,16 +248,17 @@ class ImportVCardTest extends TestCase
|
||||
/** @test */
|
||||
public function it_creates_a_contact()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $account->id;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['John', 'Doe', '', '', ''],
|
||||
'EMAIL' => 'john@doe.com',
|
||||
]);
|
||||
$contactFieldType = factory(ContactFieldType::class)->create([
|
||||
'account_id' => $account->id,
|
||||
factory(ContactFieldType::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'type' => 'email',
|
||||
]);
|
||||
|
||||
@@ -264,18 +268,152 @@ class ImportVCardTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_creates_a_contact_with_process()
|
||||
public function it_creates_a_contact_in_address_book()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([]);
|
||||
$addressBook = AddressBook::create([
|
||||
'account_id' => $user->account_id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts',
|
||||
]);
|
||||
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $account->id;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$this->setPrivateValue($importVCard, 'addressBook', $addressBook);
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['John', 'Doe', '', '', ''],
|
||||
'EMAIL' => 'john@doe.com',
|
||||
]);
|
||||
$contactFieldType = factory(ContactFieldType::class)->create([
|
||||
'account_id' => $account->id,
|
||||
factory(ContactFieldType::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'type' => 'email',
|
||||
]);
|
||||
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importEntry', [null, $vcard]);
|
||||
|
||||
$this->assertTrue($contact->exists);
|
||||
$this->assertEquals($addressBook->id, $contact->address_book_id);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_update_a_contact_with_birthdate()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['John', 'Doe', '', '', ''],
|
||||
]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
$contact->setSpecialDate('birthdate', 2001, 04, 01);
|
||||
|
||||
$newContact = $this->invokePrivateMethod($importVCard, 'importGeneralInformation', [$contact, $vcard]);
|
||||
|
||||
$this->assertEquals('John', $newContact->last_name);
|
||||
$this->assertEquals('Doe', $newContact->first_name);
|
||||
$this->assertNotNull($newContact->birthdate);
|
||||
$this->assertEquals('2001-04-01', $newContact->birthdate->date->format('Y-m-d'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_update_a_contact_with_birthdate_age_based()
|
||||
{
|
||||
Carbon::setTestNow(Carbon::create(2021, 8, 25, 7, 0, 0));
|
||||
|
||||
$user = factory(User::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['John', 'Doe', '', '', ''],
|
||||
]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
$contact->setSpecialDateFromAge('birthdate', 19);
|
||||
|
||||
$newContact = $this->invokePrivateMethod($importVCard, 'importGeneralInformation', [$contact, $vcard]);
|
||||
|
||||
$this->assertEquals('John', $newContact->last_name);
|
||||
$this->assertEquals('Doe', $newContact->first_name);
|
||||
$this->assertNotNull($newContact->birthdate);
|
||||
$this->assertTrue($newContact->birthdate->is_age_based);
|
||||
$this->assertEquals('2002-01-01', $newContact->birthdate->date->format('Y-m-d'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_update_a_contact_with_birthdate_and_replace_it()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['John', 'Doe', '', '', ''],
|
||||
'BDAY' => '1990-01-01',
|
||||
]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
$contact->setSpecialDate('birthdate', 2001, 04, 01);
|
||||
|
||||
$newContact = $this->invokePrivateMethod($importVCard, 'importGeneralInformation', [$contact, $vcard]);
|
||||
|
||||
$this->assertEquals('John', $newContact->last_name);
|
||||
$this->assertEquals('Doe', $newContact->first_name);
|
||||
$this->assertNotNull($newContact->birthdate);
|
||||
$this->assertEquals('1990-01-01', $newContact->birthdate->date->format('Y-m-d'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_update_a_contact_with_deceased_date()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['John', 'Doe', '', '', ''],
|
||||
]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'is_dead' => true,
|
||||
]);
|
||||
$contact->setSpecialDate('deceased_date', 2021, 07, 01);
|
||||
|
||||
$newContact = $this->invokePrivateMethod($importVCard, 'importGeneralInformation', [$contact, $vcard]);
|
||||
|
||||
$this->assertEquals('John', $newContact->last_name);
|
||||
$this->assertEquals('Doe', $newContact->first_name);
|
||||
$this->assertTrue($newContact->is_dead);
|
||||
$this->assertNotNull($newContact->deceasedDate);
|
||||
$this->assertEquals('2021-07-01', $newContact->deceasedDate->date->format('Y-m-d'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_creates_a_contact_with_process()
|
||||
{
|
||||
$user = factory(User::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['John', 'Doe', '', '', ''],
|
||||
'EMAIL' => 'john@doe.com',
|
||||
]);
|
||||
factory(ContactFieldType::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
'type' => 'email',
|
||||
]);
|
||||
|
||||
@@ -285,7 +423,7 @@ class ImportVCardTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('contacts', [
|
||||
'account_id' => $account->id,
|
||||
'account_id' => $user->account_id,
|
||||
'id' => $result['contact_id'],
|
||||
]);
|
||||
}
|
||||
@@ -293,18 +431,19 @@ class ImportVCardTest extends TestCase
|
||||
/** @test */
|
||||
public function it_updates_a_contact_with_process()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $account->id;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['Miles', 'Davis', '', '', ''],
|
||||
]);
|
||||
|
||||
$result = $this->invokePrivateMethod($importVCard, 'processEntry', [
|
||||
$this->invokePrivateMethod($importVCard, 'processEntry', [
|
||||
[
|
||||
'behaviour' => 'behaviour_replace',
|
||||
'contact_id' => $contact->id,
|
||||
@@ -313,7 +452,7 @@ class ImportVCardTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('contacts', [
|
||||
'account_id' => $account->id,
|
||||
'account_id' => $user->account_id,
|
||||
'id' => $contact->id,
|
||||
]);
|
||||
$contact->refresh();
|
||||
@@ -325,42 +464,34 @@ class ImportVCardTest extends TestCase
|
||||
/** @test */
|
||||
public function it_imports_names_N()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
|
||||
$vcard = new VCard([
|
||||
'N' => ['Doe', 'John', 'Jane', '', ''],
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('John', $contact->first_name);
|
||||
$this->assertEquals('Doe', $contact->last_name);
|
||||
$this->assertEquals('Jane', $contact->middle_name);
|
||||
$this->assertEquals('John', $contact['first_name']);
|
||||
$this->assertEquals('Doe', $contact['last_name']);
|
||||
$this->assertEquals('Jane', $contact['middle_name']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_names_NICKNAME()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$importVCard = new ImportVCard;
|
||||
|
||||
$vcard = new VCard([
|
||||
'NICKNAME' => 'John',
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('John', $contact->first_name);
|
||||
$this->assertEquals('John', $contact['first_name']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_names_FN()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create(['account_id' => $account->id]);
|
||||
$importVCard = new ImportVCard;
|
||||
@@ -370,17 +501,15 @@ class ImportVCardTest extends TestCase
|
||||
$vcard = new VCard([
|
||||
'FN' => 'John Doe',
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('John', $contact->first_name);
|
||||
$this->assertEquals('Doe', $contact->last_name);
|
||||
$this->assertEquals('John', $contact['first_name']);
|
||||
$this->assertEquals('Doe', $contact['last_name']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_names_FN_last()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
@@ -393,17 +522,15 @@ class ImportVCardTest extends TestCase
|
||||
$vcard = new VCard([
|
||||
'FN' => 'John Doe',
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('Doe', $contact->first_name);
|
||||
$this->assertEquals('John', $contact->last_name);
|
||||
$this->assertEquals('Doe', $contact['first_name']);
|
||||
$this->assertEquals('John', $contact['last_name']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_names_FN_extra_space()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create(['account_id' => $account->id]);
|
||||
$importVCard = new ImportVCard;
|
||||
@@ -413,17 +540,15 @@ class ImportVCardTest extends TestCase
|
||||
$vcard = new VCard([
|
||||
'FN' => 'John Doe',
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('John', $contact->first_name);
|
||||
$this->assertEquals('Doe', $contact->last_name);
|
||||
$this->assertEquals('John', $contact['first_name']);
|
||||
$this->assertEquals('Doe', $contact['last_name']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_name_FN()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create(['account_id' => $account->id]);
|
||||
$importVCard = new ImportVCard;
|
||||
@@ -434,17 +559,15 @@ class ImportVCardTest extends TestCase
|
||||
'FN' => 'John',
|
||||
'N' => 'Mike;;;;',
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('John', $contact->first_name);
|
||||
$this->assertEquals('', $contact->last_name);
|
||||
$this->assertEquals('John', $contact['first_name']);
|
||||
$this->assertEquals('', Arr::get($contact, 'last_name'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_name_FN_last()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
@@ -458,17 +581,15 @@ class ImportVCardTest extends TestCase
|
||||
'FN' => 'John',
|
||||
'N' => 'Mike;;;;',
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('John', $contact->first_name);
|
||||
$this->assertEquals('', $contact->last_name);
|
||||
$this->assertEquals('John', $contact['first_name']);
|
||||
$this->assertEquals('', Arr::get($contact, 'last_name'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_names_FN_multiple()
|
||||
{
|
||||
$contact = new Contact;
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create(['account_id' => $account->id]);
|
||||
$importVCard = new ImportVCard;
|
||||
@@ -479,26 +600,31 @@ class ImportVCardTest extends TestCase
|
||||
'FN' => 'John Doe Marco',
|
||||
'N' => 'Mike;;;;',
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importNames', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importNames', [[], $vcard]);
|
||||
|
||||
$this->assertEquals('John', $contact->first_name);
|
||||
$this->assertEquals('Doe Marco', $contact->last_name);
|
||||
$this->assertEquals('John', $contact['first_name']);
|
||||
$this->assertEquals('Doe Marco', $contact['last_name']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_imports_work_information()
|
||||
{
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create();
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'ORG' => 'Company',
|
||||
'ROLE' => 'Branleur',
|
||||
]);
|
||||
|
||||
$contact = new Contact;
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $user->account_id,
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importWorkInformation', [$contact, $vcard]);
|
||||
|
||||
$contact->refresh();
|
||||
$this->assertEquals(
|
||||
'Company',
|
||||
$contact->company
|
||||
@@ -515,25 +641,26 @@ class ImportVCardTest extends TestCase
|
||||
{
|
||||
config(['monica.requires_subscription' => false]);
|
||||
|
||||
$account = factory(Account::class)->create([]);
|
||||
$user = factory(User::class)->create();
|
||||
$importVCard = new ImportVCard;
|
||||
$importVCard->accountId = $user->account_id;
|
||||
$importVCard->userId = $user->id;
|
||||
|
||||
$vcard = new VCard([
|
||||
'BDAY' => '1990-01-01',
|
||||
]);
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importBirthday', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importBirthday', [[], $vcard]);
|
||||
|
||||
$contact->refresh();
|
||||
$this->assertNotNull($contact->birthday_special_date_id);
|
||||
$this->assertNotNull($contact->birthday_reminder_id);
|
||||
$this->assertDatabaseHas('special_dates', [
|
||||
'date' => '1990-01-01',
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
$this->assertEquals([
|
||||
'is_birthdate_known' => true,
|
||||
'birthdate_is_age_based' => false,
|
||||
'birthdate_day' => 1,
|
||||
'birthdate_month' => 1,
|
||||
'birthdate_year' => 1990,
|
||||
'birthdate_add_reminder' => true,
|
||||
'is_deceased' => false,
|
||||
], $contact);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -548,18 +675,17 @@ class ImportVCardTest extends TestCase
|
||||
'BDAY' => '19900101',
|
||||
]);
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importBirthday', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importBirthday', [[], $vcard]);
|
||||
|
||||
$contact->refresh();
|
||||
$this->assertNotNull($contact->birthday_special_date_id);
|
||||
$this->assertNotNull($contact->birthday_reminder_id);
|
||||
$this->assertDatabaseHas('special_dates', [
|
||||
'date' => '1990-01-01',
|
||||
'contact_id' => $contact->id,
|
||||
]);
|
||||
$this->assertEquals([
|
||||
'is_birthdate_known' => true,
|
||||
'birthdate_is_age_based' => false,
|
||||
'birthdate_day' => 1,
|
||||
'birthdate_month' => 1,
|
||||
'birthdate_year' => 1990,
|
||||
'birthdate_add_reminder' => true,
|
||||
'is_deceased' => false,
|
||||
], $contact);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -574,19 +700,17 @@ class ImportVCardTest extends TestCase
|
||||
'BDAY' => '--05-22',
|
||||
]);
|
||||
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
$this->invokePrivateMethod($importVCard, 'importBirthday', [$contact, $vcard]);
|
||||
$contact = $this->invokePrivateMethod($importVCard, 'importBirthday', [[], $vcard]);
|
||||
|
||||
$contact->refresh();
|
||||
$this->assertNotNull($contact->birthday_special_date_id);
|
||||
$this->assertNotNull($contact->birthday_reminder_id);
|
||||
$this->assertDatabaseHas('special_dates', [
|
||||
'date' => now()->year.'-05-22',
|
||||
'contact_id' => $contact->id,
|
||||
'is_year_unknown' => true,
|
||||
]);
|
||||
$this->assertEquals([
|
||||
'is_birthdate_known' => true,
|
||||
'birthdate_is_age_based' => false,
|
||||
'birthdate_day' => 22,
|
||||
'birthdate_month' => 5,
|
||||
'birthdate_year' => null,
|
||||
'birthdate_add_reminder' => true,
|
||||
'is_deceased' => false,
|
||||
], $contact);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
||||
Reference in New Issue
Block a user