Commit | Line | Data |
---|---|---|
1db5a675 | 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 | ||
20 | namespace api\v4\Action; | |
21 | ||
22 | use Civi\Api4\Email; | |
23 | use Civi\Api4\IM; | |
24 | use Civi\Api4\Phone; | |
25 | use Civi\Api4\Address; | |
26 | use Civi\Api4\OpenID; | |
46f571dd CW |
27 | use api\v4\Api4TestBase; |
28 | use Civi\Test\TransactionalInterface; | |
1db5a675 | 29 | |
30 | /** | |
31 | * @group headless | |
32 | */ | |
46f571dd | 33 | class IsPrimaryTest extends Api4TestBase implements TransactionalInterface { |
1db5a675 | 34 | |
35 | /** | |
36 | * Test that creating a location entity or deleting one re-assigns is_primary correctly. | |
37 | */ | |
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(); | |
47 | ||
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']); | |
56 | ||
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']); | |
66 | ||
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']); | |
74 | ||
75 | } | |
76 | ||
77 | /** | |
78 | * Check that all location entities have exactly one primary. | |
79 | */ | |
80 | protected function assertValidLocations() { | |
81 | $this->assertEquals(0, \CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM | |
82 | ||
83 | (SELECT a1.contact_id | |
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 | |
87 | WHERE | |
88 | a1.is_primary = 1 | |
89 | AND a2.id IS NOT NULL | |
90 | AND a1.contact_id IS NOT NULL | |
91 | UNION | |
92 | SELECT a1.contact_id | |
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 | |
97 | AND a2.id IS NULL | |
98 | AND a1.contact_id IS NOT NULL | |
99 | ||
100 | UNION | |
101 | ||
102 | SELECT a1.contact_id | |
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 | |
106 | WHERE | |
107 | a1.is_primary = 1 | |
108 | AND a2.id IS NOT NULL | |
109 | AND a1.contact_id IS NOT NULL | |
110 | UNION | |
111 | SELECT a1.contact_id | |
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 | |
116 | AND a2.id IS NULL | |
117 | AND a1.contact_id IS NOT NULL | |
118 | ||
119 | UNION | |
120 | ||
121 | SELECT a1.contact_id | |
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 | |
125 | WHERE | |
126 | a1.is_primary = 1 | |
127 | AND a2.id IS NOT NULL | |
128 | AND a1.contact_id IS NOT NULL | |
129 | UNION | |
130 | SELECT a1.contact_id | |
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 | |
135 | AND a2.id IS NULL | |
136 | AND a1.contact_id IS NOT NULL | |
137 | ||
138 | UNION | |
139 | ||
140 | SELECT a1.contact_id | |
141 | FROM civicrm_im a1 | |
142 | LEFT JOIN civicrm_im a2 ON a1.id <> a2.id AND a2.is_primary = 1 | |
143 | AND a1.contact_id = a2.contact_id | |
144 | WHERE | |
145 | a1.is_primary = 1 | |
146 | AND a2.id IS NOT NULL | |
147 | AND a1.contact_id IS NOT NULL | |
148 | UNION | |
149 | SELECT a1.contact_id | |
150 | FROM civicrm_im a1 | |
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 | |
154 | AND a2.id IS NULL | |
155 | AND a1.contact_id IS NOT NULL | |
156 | ||
157 | UNION | |
158 | ||
159 | SELECT a1.contact_id | |
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) | |
164 | UNION | |
165 | ||
166 | SELECT a1.contact_id | |
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 | |
170 | WHERE | |
171 | a1.is_primary = 1 | |
172 | AND a2.id IS NOT NULL | |
173 | AND a1.contact_id IS NOT NULL | |
174 | UNION | |
175 | SELECT a1.contact_id | |
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 | |
180 | AND a2.id IS NULL | |
181 | AND a1.contact_id IS NOT NULL) as primary_descrepancies | |
182 | ')); | |
183 | } | |
184 | ||
185 | } |