4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Action
;
25 use Civi\Api4\Address
;
27 use api\v
4\Api4TestBase
;
28 use Civi\Test\TransactionalInterface
;
33 class IsPrimaryTest
extends Api4TestBase
implements TransactionalInterface
{
36 * Test that creating a location entity or deleting one re-assigns is_primary correctly.
38 public function testPrimaryHandling() {
39 $contactID = self
::createEntity(['type' => 'Individual'])['id'];
40 // Create an entity of each type.
41 Email
::create()->setValues(['email' => 'b@example.com', 'contact_id' => $contactID])->execute();
42 Phone
::create()->setValues(['phone' => '123', 'contact_id' => $contactID])->execute();
43 IM
::create()->setValues(['name' => 'im', 'contact_id' => $contactID])->execute();
44 OpenID
::create()->setValues(['openid' => 'openid', 'contact_id' => $contactID])->execute();
45 $firstAddressID = Address
::create()->setValues(['street_name' => '1 sesame street', 'contact_id' => $contactID])->execute()->first()['id'];
46 $this->assertValidLocations();
48 // Create an second entity of each type - demoting the first
49 Email
::create()->setValues(['email' => 'b2@example.com', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
50 Phone
::create()->setValues(['phone' => '1232', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
51 IM
::create()->setValues(['name' => 'im2', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
52 OpenID
::create()->setValues(['openid' => 'openid2', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
53 Address
::create()->setValues(['street_name' => '2 sesame street', 'contact_id' => $contactID, 'is_primary' => TRUE])->execute();
54 $this->assertValidLocations();
55 $this->assertNotEquals($firstAddressID, Address
::get()->addWhere('is_primary', '=', TRUE)->addWhere('contact_id', '=', $contactID)->execute()->first()['id']);
57 // Update all the non-primaries
58 // to is_primary TRUE.
59 Email
::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
60 Phone
::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
61 IM
::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
62 OpenID
::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
63 Address
::update()->setValues(['is_primary' => TRUE])->addWhere('contact_id', '=', $contactID)->addWhere('is_primary', '=', FALSE)->execute();
64 $this->assertValidLocations();
65 $this->assertEquals($firstAddressID, Address
::get()->addWhere('is_primary', '=', TRUE)->addWhere('contact_id', '=', $contactID)->execute()->first()['id']);
67 Email
::delete()->addWhere('is_primary', '=', TRUE)->execute();
68 Phone
::delete()->addWhere('is_primary', '=', TRUE)->execute();
69 IM
::delete()->addWhere('is_primary', '=', TRUE)->execute();
70 OpenID
::delete()->addWhere('is_primary', '=', TRUE)->execute();
71 Address
::delete()->addWhere('is_primary', '=', TRUE)->execute();
72 $this->assertValidLocations();
73 $this->assertNotEquals($firstAddressID, Address
::get()->addWhere('is_primary', '=', TRUE)->addWhere('contact_id', '=', $contactID)->execute()->first()['id']);
78 * Check that all location entities have exactly one primary.
80 protected function assertValidLocations() {
81 $this->assertEquals(0, \CRM_Core_DAO
::singleValueQuery('SELECT COUNT(*) FROM
84 FROM civicrm_address a1
85 LEFT JOIN civicrm_address a2 ON a1.id <> a2.id AND a2.is_primary = 1
86 AND a1.contact_id = a2.contact_id
90 AND a1.contact_id IS NOT NULL
93 FROM civicrm_address a1
94 LEFT JOIN civicrm_address a2 ON a1.id <> a2.id AND a2.is_primary = 1
95 AND a1.contact_id = a2.contact_id
96 WHERE a1.is_primary = 0
98 AND a1.contact_id IS NOT NULL
103 FROM civicrm_email a1
104 LEFT JOIN civicrm_email a2 ON a1.id <> a2.id AND a2.is_primary = 1
105 AND a1.contact_id = a2.contact_id
108 AND a2.id IS NOT NULL
109 AND a1.contact_id IS NOT NULL
112 FROM civicrm_email a1
113 LEFT JOIN civicrm_email a2 ON a1.id <> a2.id AND a2.is_primary = 1
114 AND a1.contact_id = a2.contact_id
115 WHERE a1.is_primary = 0
117 AND a1.contact_id IS NOT NULL
122 FROM civicrm_phone a1
123 LEFT JOIN civicrm_phone a2 ON a1.id <> a2.id AND a2.is_primary = 1
124 AND a1.contact_id = a2.contact_id
127 AND a2.id IS NOT NULL
128 AND a1.contact_id IS NOT NULL
131 FROM civicrm_phone a1
132 LEFT JOIN civicrm_phone a2 ON a1.id <> a2.id AND a2.is_primary = 1
133 AND a1.contact_id = a2.contact_id
134 WHERE a1.is_primary = 0
136 AND a1.contact_id IS NOT NULL
142 LEFT JOIN civicrm_im a2 ON a1.id <> a2.id AND a2.is_primary = 1
143 AND a1.contact_id = a2.contact_id
146 AND a2.id IS NOT NULL
147 AND a1.contact_id IS NOT NULL
151 LEFT JOIN civicrm_im a2 ON a1.id <> a2.id AND a2.is_primary = 1
152 AND a1.contact_id = a2.contact_id
153 WHERE a1.is_primary = 0
155 AND a1.contact_id IS NOT NULL
160 FROM civicrm_openid a1
161 LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
162 AND a1.contact_id = a2.contact_id
163 WHERE (a1.is_primary = 1 AND a2.id IS NOT NULL)
167 FROM civicrm_openid a1
168 LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
169 AND a1.contact_id = a2.contact_id
172 AND a2.id IS NOT NULL
173 AND a1.contact_id IS NOT NULL
176 FROM civicrm_openid a1
177 LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
178 AND a1.contact_id = a2.contact_id
179 WHERE a1.is_primary = 0
181 AND a1.contact_id IS NOT NULL) as primary_descrepancies