Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
7d61e75f | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
7d61e75f TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
22 | namespace api\v4\Action; |
23 | ||
24 | use Civi\Api4\CustomField; | |
25 | use Civi\Api4\CustomGroup; | |
26 | use Civi\Api4\CustomValue; | |
27 | use Civi\Api4\Email; | |
28 | use api\v4\Traits\TableDropperTrait; | |
29 | use api\v4\UnitTestCase; | |
30 | use Civi\Api4\Contact; | |
31 | ||
32 | /** | |
33 | * @group headless | |
34 | */ | |
35 | class ReplaceTest extends UnitTestCase { | |
36 | use TableDropperTrait; | |
37 | ||
38 | /** | |
39 | * Set up baseline for testing | |
40 | */ | |
41 | public function setUp() { | |
42 | $tablesToTruncate = [ | |
43 | 'civicrm_custom_group', | |
44 | 'civicrm_custom_field', | |
45 | 'civicrm_email', | |
46 | ]; | |
47 | $this->dropByPrefix('civicrm_value_replacetest'); | |
48 | $this->cleanup(['tablesToTruncate' => $tablesToTruncate]); | |
49 | parent::setUp(); | |
50 | } | |
51 | ||
52 | public function testEmailReplace() { | |
53 | $cid1 = Contact::create() | |
54 | ->addValue('first_name', 'Lotsa') | |
55 | ->addValue('last_name', 'Emails') | |
56 | ->execute() | |
57 | ->first()['id']; | |
58 | $cid2 = Contact::create() | |
59 | ->addValue('first_name', 'Notso') | |
60 | ->addValue('last_name', 'Many') | |
61 | ->execute() | |
62 | ->first()['id']; | |
63 | $e0 = Email::create() | |
64 | ->setValues(['contact_id' => $cid2, 'email' => 'nosomany@example.com', 'location_type_id' => 1]) | |
65 | ->execute() | |
66 | ->first()['id']; | |
67 | $e1 = Email::create() | |
68 | ->setValues(['contact_id' => $cid1, 'email' => 'first@example.com', 'location_type_id' => 1]) | |
69 | ->execute() | |
70 | ->first()['id']; | |
71 | $e2 = Email::create() | |
72 | ->setValues(['contact_id' => $cid1, 'email' => 'second@example.com', 'location_type_id' => 1]) | |
73 | ->execute() | |
74 | ->first()['id']; | |
75 | $replacement = [ | |
76 | ['email' => 'firstedited@example.com', 'id' => $e1], | |
77 | ['contact_id' => $cid1, 'email' => 'third@example.com', 'location_type_id' => 1], | |
78 | ]; | |
79 | $replaced = Email::replace() | |
80 | ->setRecords($replacement) | |
81 | ->addWhere('contact_id', '=', $cid1) | |
82 | ->execute(); | |
83 | // Should have saved 2 records | |
84 | $this->assertEquals(2, $replaced->count()); | |
85 | // Should have deleted email2 | |
86 | $this->assertEquals([['id' => $e2]], $replaced->deleted); | |
87 | // Verify contact now has the new email records | |
88 | $results = Email::get() | |
89 | ->addWhere('contact_id', '=', $cid1) | |
90 | ->execute() | |
91 | ->indexBy('id'); | |
92 | $this->assertEquals('firstedited@example.com', $results[$e1]['email']); | |
93 | $this->assertEquals(2, $results->count()); | |
94 | $this->assertArrayNotHasKey($e2, (array) $results); | |
95 | $this->assertArrayNotHasKey($e0, (array) $results); | |
96 | unset($results[$e1]); | |
97 | foreach ($results as $result) { | |
98 | $this->assertEquals('third@example.com', $result['email']); | |
99 | } | |
100 | // Validate our other contact's email did not get deleted | |
101 | $c2email = Email::get() | |
102 | ->addWhere('contact_id', '=', $cid2) | |
103 | ->execute() | |
104 | ->first(); | |
105 | $this->assertEquals('nosomany@example.com', $c2email['email']); | |
106 | } | |
107 | ||
108 | public function testCustomValueReplace() { | |
109 | $customGroup = CustomGroup::create() | |
110 | ->setCheckPermissions(FALSE) | |
111 | ->addValue('name', 'replaceTest') | |
112 | ->addValue('extends', 'Contact') | |
113 | ->addValue('is_multiple', TRUE) | |
114 | ->execute() | |
115 | ->first(); | |
116 | ||
117 | CustomField::create() | |
118 | ->addValue('label', 'Custom1') | |
119 | ->addValue('custom_group_id', $customGroup['id']) | |
120 | ->addValue('html_type', 'String') | |
121 | ->addValue('data_type', 'String') | |
122 | ->execute(); | |
123 | ||
124 | CustomField::create() | |
125 | ->setCheckPermissions(FALSE) | |
126 | ->addValue('label', 'Custom2') | |
127 | ->addValue('custom_group_id', $customGroup['id']) | |
128 | ->addValue('html_type', 'String') | |
129 | ->addValue('data_type', 'String') | |
130 | ->execute(); | |
131 | ||
132 | $cid1 = Contact::create() | |
133 | ->addValue('first_name', 'Lotsa') | |
134 | ->addValue('last_name', 'Data') | |
135 | ->execute() | |
136 | ->first()['id']; | |
137 | $cid2 = Contact::create() | |
138 | ->addValue('first_name', 'Notso') | |
139 | ->addValue('last_name', 'Much') | |
140 | ->execute() | |
141 | ->first()['id']; | |
142 | ||
143 | // Contact 2 gets one row | |
144 | CustomValue::create('replaceTest') | |
145 | ->setCheckPermissions(FALSE) | |
146 | ->addValue('Custom1', "2 1") | |
147 | ->addValue('Custom2', "2 1") | |
148 | ->addValue('entity_id', $cid2) | |
149 | ->execute(); | |
150 | ||
151 | // Create 3 rows for contact 1 | |
152 | foreach ([1, 2, 3] as $i) { | |
153 | CustomValue::create('replaceTest') | |
154 | ->setCheckPermissions(FALSE) | |
155 | ->addValue('Custom1', "1 $i") | |
156 | ->addValue('Custom2', "1 $i") | |
157 | ->addValue('entity_id', $cid1) | |
158 | ->execute(); | |
159 | } | |
160 | ||
161 | $cid1Records = CustomValue::get('replaceTest') | |
162 | ->setCheckPermissions(FALSE) | |
163 | ->addWhere('entity_id', '=', $cid1) | |
164 | ->execute(); | |
165 | ||
166 | $this->assertCount(3, $cid1Records); | |
167 | $this->assertCount(1, CustomValue::get('replaceTest')->setCheckPermissions(FALSE)->addWhere('entity_id', '=', $cid2)->execute()); | |
168 | ||
169 | $result = CustomValue::replace('replaceTest') | |
170 | ->addWhere('entity_id', '=', $cid1) | |
171 | ->addRecord(['Custom1' => 'new one', 'Custom2' => 'new two']) | |
172 | ->addRecord(['id' => $cid1Records[0]['id'], 'Custom1' => 'changed one', 'Custom2' => 'changed two']) | |
173 | ->execute(); | |
174 | ||
175 | $this->assertCount(2, $result); | |
176 | $this->assertCount(2, $result->deleted); | |
177 | ||
178 | $newRecords = CustomValue::get('replaceTest') | |
179 | ->setCheckPermissions(FALSE) | |
180 | ->addWhere('entity_id', '=', $cid1) | |
181 | ->execute() | |
182 | ->indexBy('id'); | |
183 | ||
184 | $this->assertEquals('new one', $newRecords->last()['Custom1']); | |
185 | $this->assertEquals('new two', $newRecords->last()['Custom2']); | |
186 | $this->assertEquals('changed one', $newRecords[$cid1Records[0]['id']]['Custom1']); | |
187 | $this->assertEquals('changed two', $newRecords[$cid1Records[0]['id']]['Custom2']); | |
188 | } | |
189 | ||
190 | } |