Move Email, Address, etc. is_primary handling on delete to a hook
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / AddressTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19 namespace api\v4\Entity;
20
21 use api\v4\UnitTestCase;
22 use Civi\Api4\Address;
23 use Civi\Api4\Contact;
24 use Civi\Test\TransactionalInterface;
25
26 /**
27 * Test Address functionality
28 *
29 * @group headless
30 */
31 class AddressTest extends UnitTestCase implements TransactionalInterface {
32
33 /**
34 * Check that 2 addresses for the same contact can't both be primary
35 */
36 public function testPrimary() {
37 $cid = Contact::create(FALSE)->addValue('first_name', uniqid())->execute()->single()['id'];
38
39 $a1 = Address::create(FALSE)
40 ->addValue('is_primary', TRUE)
41 ->addValue('contact_id', $cid)
42 ->addValue('location_type_id', 1)
43 ->addValue('city', 'Somewhere')
44 ->execute();
45
46 $a2 = Address::create(FALSE)
47 ->addValue('is_primary', TRUE)
48 ->addValue('contact_id', $cid)
49 ->addValue('location_type_id', 2)
50 ->addValue('city', 'Elsewhere')
51 ->execute();
52
53 $addresses = Address::get(FALSE)
54 ->addWhere('contact_id', '=', $cid)
55 ->addOrderBy('id')
56 ->execute();
57
58 $this->assertFalse($addresses[0]['is_primary']);
59 $this->assertTrue($addresses[1]['is_primary']);
60 }
61
62 }