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