EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\Core\Field\EmailAddress;
use Espo\Core\Field\EmailAddress;
use Espo\Core\Field\EmailAddressGroup;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class EmailAddressGroupTest extends TestCase
{
public function testEmpty()
{
$group = EmailAddressGroup::create();
$this->assertEquals(0, count($group->getAddressList()));
$this->assertEquals(0, count($group->getSecondaryList()));
$this->assertNull($group->getPrimary());
$this->assertNull($group->getPrimaryAddress());
$this->assertEquals(0, $group->getCount());
}
public function testDuplicate1()
{
$this->expectException(InvalidArgumentException::class);
EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
]);
}
public function testDuplicate2()
{
$this->expectException(InvalidArgumentException::class);
EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
]);
}
public function testWithPrimary1()
{
$primary = EmailAddress::create('[email protected]')->invalid();
$group = EmailAddressGroup
::create()
->withPrimary($primary);
$this->assertEquals(1, count($group->getList()));
$this->assertEquals(0, count($group->getSecondaryList()));
$this->assertNotNull($group->getPrimary());
$this->assertEquals('[email protected]', $group->getPrimary()->getAddress());
$this->assertEquals('[email protected]', $group->getPrimaryAddress());
$primaryAnother = EmailAddress::create('[email protected]');
$groupAnother = $group->withPrimary($primaryAnother);
$this->assertEquals(2, count($groupAnother->getList()));
$this->assertEquals(1, count($groupAnother->getSecondaryList()));
$this->assertEquals('[email protected]', $groupAnother->getPrimary()->getAddress());
$this->assertEquals('[email protected]', $groupAnother->getList()[1]->getAddress());
$this->assertTrue($groupAnother->getList()[1]->isInvalid());
$this->assertTrue($groupAnother->hasAddress('[email protected]'));
$this->assertTrue($groupAnother->hasAddress('[email protected]'));
}
public function testWithPrimary2()
{
$address = EmailAddress::create('[email protected]')->invalid();
$group = EmailAddressGroup
::create([$address])
->withAdded(
EmailAddress::create('[email protected]')
)
->withPrimary(
EmailAddress::create('[email protected]')
);
$this->assertEquals('[email protected]', $group->getPrimary()->getAddress());
$this->assertEquals(2, count($group->getList()));
}
public function testWithAdded1()
{
$address = EmailAddress::create('[email protected]')->invalid();
$group = EmailAddressGroup
::create([$address])
->withAdded(
EmailAddress::create('[email protected]')
);
$this->assertEquals('[email protected]', $group->getPrimary()->getAddress());
$this->assertEquals(2, count($group->getList()));
$this->assertEquals(2, $group->getCount());
}
public function testWithAddedList()
{
$group = EmailAddressGroup
::create()
->withAddedList([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
]);
$this->assertEquals('[email protected]', $group->getPrimary()->getAddress());
$this->assertEquals(2, count($group->getList()));
}
public function testWithRemoved1()
{
$group = EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
])
->withRemoved(EmailAddress::create('[email protected]'));
$this->assertEquals('[email protected]', $group->getPrimary()->getAddress());
$this->assertEquals(2, count($group->getList()));
}
public function testWithRemoved2()
{
$group = EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
])
->withRemoved(EmailAddress::create('[email protected]'));
$this->assertEquals('[email protected]', $group->getPrimary()->getAddress());
$this->assertEquals(2, count($group->getList()));
}
public function testWithRemoved3()
{
$group = EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
])
->withRemoved(EmailAddress::create('[email protected]'));
$this->assertNull($group->getPrimary());
$this->assertEquals(0, count($group->getList()));
$this->assertEquals(0, $group->getCount());
}
public function testHasAddress()
{
$group = EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
]);
$this->assertTrue($group->hasAddress('[email protected]'));
$this->assertTrue($group->hasAddress('[email protected]'));
$this->assertFalse($group->hasAddress('[email protected]'));
}
public function testGetByAddress()
{
$group = EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
]);
$this->assertEquals('[email protected]', $group->getByAddress('[email protected]')->getAddress());
$this->assertNull($group->getByAddress('[email protected]'));
}
public function testClone()
{
$group = EmailAddressGroup
::create([
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
EmailAddress::create('[email protected]'),
]);
$cloned = clone $group;
$this->assertEquals('[email protected]', $cloned->getByAddress('[email protected]')->getAddress());
$this->assertEquals($cloned->getPrimary()->getAddress(), $group->getPrimary()->getAddress());
$this->assertNotSame($cloned->getPrimary(), $group->getPrimary());
$this->assertNotSame($cloned->getList()[1], $group->getList()[1]);
}
}