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