Merge pull request #15755 from seamuslee001/copywrite_date_update
[civicrm-core.git] / tests / phpunit / api / v4 / Action / ReplaceTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2020
33 * $Id$
34 *
35 */
36
37
38 namespace api\v4\Action;
39
40 use Civi\Api4\CustomField;
41 use Civi\Api4\CustomGroup;
42 use Civi\Api4\CustomValue;
43 use Civi\Api4\Email;
44 use api\v4\Traits\TableDropperTrait;
45 use api\v4\UnitTestCase;
46 use Civi\Api4\Contact;
47
48 /**
49 * @group headless
50 */
51 class ReplaceTest extends UnitTestCase {
52 use TableDropperTrait;
53
54 /**
55 * Set up baseline for testing
56 */
57 public function setUp() {
58 $tablesToTruncate = [
59 'civicrm_custom_group',
60 'civicrm_custom_field',
61 'civicrm_email',
62 ];
63 $this->dropByPrefix('civicrm_value_replacetest');
64 $this->cleanup(['tablesToTruncate' => $tablesToTruncate]);
65 parent::setUp();
66 }
67
68 public function testEmailReplace() {
69 $cid1 = Contact::create()
70 ->addValue('first_name', 'Lotsa')
71 ->addValue('last_name', 'Emails')
72 ->execute()
73 ->first()['id'];
74 $cid2 = Contact::create()
75 ->addValue('first_name', 'Notso')
76 ->addValue('last_name', 'Many')
77 ->execute()
78 ->first()['id'];
79 $e0 = Email::create()
80 ->setValues(['contact_id' => $cid2, 'email' => 'nosomany@example.com', 'location_type_id' => 1])
81 ->execute()
82 ->first()['id'];
83 $e1 = Email::create()
84 ->setValues(['contact_id' => $cid1, 'email' => 'first@example.com', 'location_type_id' => 1])
85 ->execute()
86 ->first()['id'];
87 $e2 = Email::create()
88 ->setValues(['contact_id' => $cid1, 'email' => 'second@example.com', 'location_type_id' => 1])
89 ->execute()
90 ->first()['id'];
91 $replacement = [
92 ['email' => 'firstedited@example.com', 'id' => $e1],
93 ['contact_id' => $cid1, 'email' => 'third@example.com', 'location_type_id' => 1],
94 ];
95 $replaced = Email::replace()
96 ->setRecords($replacement)
97 ->addWhere('contact_id', '=', $cid1)
98 ->execute();
99 // Should have saved 2 records
100 $this->assertEquals(2, $replaced->count());
101 // Should have deleted email2
102 $this->assertEquals([['id' => $e2]], $replaced->deleted);
103 // Verify contact now has the new email records
104 $results = Email::get()
105 ->addWhere('contact_id', '=', $cid1)
106 ->execute()
107 ->indexBy('id');
108 $this->assertEquals('firstedited@example.com', $results[$e1]['email']);
109 $this->assertEquals(2, $results->count());
110 $this->assertArrayNotHasKey($e2, (array) $results);
111 $this->assertArrayNotHasKey($e0, (array) $results);
112 unset($results[$e1]);
113 foreach ($results as $result) {
114 $this->assertEquals('third@example.com', $result['email']);
115 }
116 // Validate our other contact's email did not get deleted
117 $c2email = Email::get()
118 ->addWhere('contact_id', '=', $cid2)
119 ->execute()
120 ->first();
121 $this->assertEquals('nosomany@example.com', $c2email['email']);
122 }
123
124 public function testCustomValueReplace() {
125 $customGroup = CustomGroup::create()
126 ->setCheckPermissions(FALSE)
127 ->addValue('name', 'replaceTest')
128 ->addValue('extends', 'Contact')
129 ->addValue('is_multiple', TRUE)
130 ->execute()
131 ->first();
132
133 CustomField::create()
134 ->addValue('label', 'Custom1')
135 ->addValue('custom_group_id', $customGroup['id'])
136 ->addValue('html_type', 'String')
137 ->addValue('data_type', 'String')
138 ->execute();
139
140 CustomField::create()
141 ->setCheckPermissions(FALSE)
142 ->addValue('label', 'Custom2')
143 ->addValue('custom_group_id', $customGroup['id'])
144 ->addValue('html_type', 'String')
145 ->addValue('data_type', 'String')
146 ->execute();
147
148 $cid1 = Contact::create()
149 ->addValue('first_name', 'Lotsa')
150 ->addValue('last_name', 'Data')
151 ->execute()
152 ->first()['id'];
153 $cid2 = Contact::create()
154 ->addValue('first_name', 'Notso')
155 ->addValue('last_name', 'Much')
156 ->execute()
157 ->first()['id'];
158
159 // Contact 2 gets one row
160 CustomValue::create('replaceTest')
161 ->setCheckPermissions(FALSE)
162 ->addValue('Custom1', "2 1")
163 ->addValue('Custom2', "2 1")
164 ->addValue('entity_id', $cid2)
165 ->execute();
166
167 // Create 3 rows for contact 1
168 foreach ([1, 2, 3] as $i) {
169 CustomValue::create('replaceTest')
170 ->setCheckPermissions(FALSE)
171 ->addValue('Custom1', "1 $i")
172 ->addValue('Custom2', "1 $i")
173 ->addValue('entity_id', $cid1)
174 ->execute();
175 }
176
177 $cid1Records = CustomValue::get('replaceTest')
178 ->setCheckPermissions(FALSE)
179 ->addWhere('entity_id', '=', $cid1)
180 ->execute();
181
182 $this->assertCount(3, $cid1Records);
183 $this->assertCount(1, CustomValue::get('replaceTest')->setCheckPermissions(FALSE)->addWhere('entity_id', '=', $cid2)->execute());
184
185 $result = CustomValue::replace('replaceTest')
186 ->addWhere('entity_id', '=', $cid1)
187 ->addRecord(['Custom1' => 'new one', 'Custom2' => 'new two'])
188 ->addRecord(['id' => $cid1Records[0]['id'], 'Custom1' => 'changed one', 'Custom2' => 'changed two'])
189 ->execute();
190
191 $this->assertCount(2, $result);
192 $this->assertCount(2, $result->deleted);
193
194 $newRecords = CustomValue::get('replaceTest')
195 ->setCheckPermissions(FALSE)
196 ->addWhere('entity_id', '=', $cid1)
197 ->execute()
198 ->indexBy('id');
199
200 $this->assertEquals('new one', $newRecords->last()['Custom1']);
201 $this->assertEquals('new two', $newRecords->last()['Custom2']);
202 $this->assertEquals('changed one', $newRecords[$cid1Records[0]['id']]['Custom1']);
203 $this->assertEquals('changed two', $newRecords[$cid1Records[0]['id']]['Custom2']);
204 }
205
206 }