APIv4 - Reorganize test classes, don't use transactions for custom value tests
[civicrm-core.git] / tests / phpunit / api / v4 / Action / IsPrimaryTest.php
CommitLineData
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
20namespace api\v4\Action;
21
22use Civi\Api4\Email;
23use Civi\Api4\IM;
24use Civi\Api4\Phone;
25use Civi\Api4\Address;
26use Civi\Api4\OpenID;
46f571dd
CW
27use api\v4\Api4TestBase;
28use Civi\Test\TransactionalInterface;
1db5a675 29
30/**
31 * @group headless
32 */
46f571dd 33class 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
84FROM 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
87WHERE
88 a1.is_primary = 1
89 AND a2.id IS NOT NULL
90 AND a1.contact_id IS NOT NULL
91UNION
92SELECT a1.contact_id
93FROM 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
96WHERE a1.is_primary = 0
97 AND a2.id IS NULL
98 AND a1.contact_id IS NOT NULL
99
100UNION
101
102SELECT a1.contact_id
103FROM 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
106WHERE
107 a1.is_primary = 1
108 AND a2.id IS NOT NULL
109 AND a1.contact_id IS NOT NULL
110UNION
111SELECT a1.contact_id
112FROM 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
115WHERE a1.is_primary = 0
116 AND a2.id IS NULL
117 AND a1.contact_id IS NOT NULL
118
119UNION
120
121SELECT a1.contact_id
122FROM 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
125WHERE
126 a1.is_primary = 1
127 AND a2.id IS NOT NULL
128 AND a1.contact_id IS NOT NULL
129UNION
130SELECT a1.contact_id
131FROM 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
134WHERE a1.is_primary = 0
135 AND a2.id IS NULL
136 AND a1.contact_id IS NOT NULL
137
138UNION
139
140SELECT a1.contact_id
141FROM 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
144WHERE
145 a1.is_primary = 1
146 AND a2.id IS NOT NULL
147 AND a1.contact_id IS NOT NULL
148UNION
149SELECT a1.contact_id
150FROM 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
153WHERE a1.is_primary = 0
154 AND a2.id IS NULL
155 AND a1.contact_id IS NOT NULL
156
157UNION
158
159SELECT a1.contact_id
160FROM 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
163WHERE (a1.is_primary = 1 AND a2.id IS NOT NULL)
164UNION
165
166SELECT a1.contact_id
167FROM 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
170WHERE
171 a1.is_primary = 1
172 AND a2.id IS NOT NULL
173 AND a1.contact_id IS NOT NULL
174UNION
175SELECT a1.contact_id
176FROM 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
179WHERE 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}