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
22 namespace api\v
4\Action
;
24 use Civi\Api4\CustomField
;
25 use Civi\Api4\CustomGroup
;
26 use Civi\Api4\CustomValue
;
28 use api\v
4\Traits\TableDropperTrait
;
29 use api\v
4\UnitTestCase
;
30 use Civi\Api4\Contact
;
35 class ReplaceTest
extends UnitTestCase
{
36 use TableDropperTrait
;
39 * Set up baseline for testing
41 public function setUp() {
43 'civicrm_custom_group',
44 'civicrm_custom_field',
47 $this->dropByPrefix('civicrm_value_replacetest');
48 $this->cleanup(['tablesToTruncate' => $tablesToTruncate]);
52 public function testEmailReplace() {
53 $cid1 = Contact
::create()
54 ->addValue('first_name', 'Lotsa')
55 ->addValue('last_name', 'Emails')
58 $cid2 = Contact
::create()
59 ->addValue('first_name', 'Notso')
60 ->addValue('last_name', 'Many')
64 ->setValues(['contact_id' => $cid2, 'email' => 'nosomany@example.com', 'location_type_id' => 1])
68 ->setValues(['contact_id' => $cid1, 'email' => 'first@example.com', 'location_type_id' => 1])
72 ->setValues(['contact_id' => $cid1, 'email' => 'second@example.com', 'location_type_id' => 1])
76 ['email' => 'firstedited@example.com', 'id' => $e1],
77 ['contact_id' => $cid1, 'email' => 'third@example.com', 'location_type_id' => 1],
79 $replaced = Email
::replace()
80 ->setRecords($replacement)
81 ->addWhere('contact_id', '=', $cid1)
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)
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);
97 foreach ($results as $result) {
98 $this->assertEquals('third@example.com', $result['email']);
100 // Validate our other contact's email did not get deleted
101 $c2email = Email
::get()
102 ->addWhere('contact_id', '=', $cid2)
105 $this->assertEquals('nosomany@example.com', $c2email['email']);
108 public function testCustomValueReplace() {
109 $customGroup = CustomGroup
::create()
110 ->setCheckPermissions(FALSE)
111 ->addValue('name', 'replaceTest')
112 ->addValue('extends', 'Contact')
113 ->addValue('is_multiple', TRUE)
117 CustomField
::create()
118 ->addValue('label', 'Custom1')
119 ->addValue('custom_group_id', $customGroup['id'])
120 ->addValue('html_type', 'String')
121 ->addValue('data_type', 'String')
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')
132 $cid1 = Contact
::create()
133 ->addValue('first_name', 'Lotsa')
134 ->addValue('last_name', 'Data')
137 $cid2 = Contact
::create()
138 ->addValue('first_name', 'Notso')
139 ->addValue('last_name', 'Much')
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)
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)
161 $cid1Records = CustomValue
::get('replaceTest')
162 ->setCheckPermissions(FALSE)
163 ->addWhere('entity_id', '=', $cid1)
166 $this->assertCount(3, $cid1Records);
167 $this->assertCount(1, CustomValue
::get('replaceTest')->setCheckPermissions(FALSE)->addWhere('entity_id', '=', $cid2)->execute());
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'])
175 $this->assertCount(2, $result);
176 $this->assertCount(2, $result->deleted
);
178 $newRecords = CustomValue
::get('replaceTest')
179 ->setCheckPermissions(FALSE)
180 ->addWhere('entity_id', '=', $cid1)
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']);