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
20 namespace api\v
4\Action
;
22 use Civi\Api4\Contact
;
27 class ContactGetTest
extends \api\v
4\UnitTestCase
{
29 public function testGetDeletedContacts() {
30 $last_name = uniqid('deleteContactTest');
32 $bob = Contact
::create()
33 ->setValues(['first_name' => 'Bob', 'last_name' => $last_name])
36 $jan = Contact
::create()
37 ->setValues(['first_name' => 'Jan', 'last_name' => $last_name])
40 $del = Contact
::create()
41 ->setValues(['first_name' => 'Del', 'last_name' => $last_name, 'is_deleted' => 1])
44 // Deleted contacts are not fetched by default
45 $this->assertCount(2, Contact
::get()->addWhere('last_name', '=', $last_name)->selectRowCount()->execute());
47 // You can search for them specifically
48 $contacts = Contact
::get()->addWhere('last_name', '=', $last_name)->addWhere('is_deleted', '=', 1)->addSelect('id')->execute();
49 $this->assertEquals($del['id'], $contacts->first()['id']);
52 $this->assertCount(3, Contact
::get()->addWhere('id', 'IN', [$bob['id'], $jan['id'], $del['id']])->selectRowCount()->execute());
54 // Putting is_deleted anywhere in the where clause will disable the default
55 $contacts = Contact
::get()->addClause('OR', ['last_name', '=', $last_name], ['is_deleted', '=', 0])->addSelect('id')->execute();
56 $this->assertContains($del['id'], $contacts->column('id'));
59 public function testGetWithLimit() {
60 $last_name = uniqid('getWithLimitTest');
62 $bob = Contact
::create()
63 ->setValues(['first_name' => 'Bob', 'last_name' => $last_name])
66 $jan = Contact
::create()
67 ->setValues(['first_name' => 'Jan', 'last_name' => $last_name])
70 $dan = Contact
::create()
71 ->setValues(['first_name' => 'Dan', 'last_name' => $last_name])
74 $num = Contact
::get(FALSE)->selectRowCount()->execute()->count();
76 // The object's count() method will account for all results, ignoring limit & offset, while the array results are limited
77 $offset1 = Contact
::get(FALSE)->setOffset(1)->execute();
78 $this->assertCount($num, $offset1);
79 $this->assertCount($num - 1, (array) $offset1);
80 $offset2 = Contact
::get(FALSE)->setOffset(2)->execute();
81 $this->assertCount($num - 2, (array) $offset2);
82 $this->assertCount($num, $offset2);
83 // With limit, it doesn't fetch total count by default
84 $limit2 = Contact
::get(FALSE)->setLimit(2)->execute();
85 $this->assertCount(2, (array) $limit2);
86 $this->assertCount(2, $limit2);
87 // With limit, you have to trigger the full row count manually
88 $limit2 = Contact
::get(FALSE)->setLimit(2)->addSelect('sort_name', 'row_count')->execute();
89 $this->assertCount(2, (array) $limit2);
90 $this->assertCount($num, $limit2);
95 catch (\API_Exception
$e) {
96 $msg = $e->getMessage();
98 $this->assertRegExp(';Expected to find one Contact record;', $msg);
99 $limit1 = Contact
::get(FALSE)->setLimit(1)->execute();
100 $this->assertCount(1, (array) $limit1);
101 $this->assertCount(1, $limit1);
102 $this->assertTrue(!empty($limit1->single()['sort_name']));
106 * Test a lack of fatal errors when the where contains an emoji.
108 * By default our DBs are not 🦉 compliant. This test will age
111 * @throws \API_Exception
113 public function testEmoji(): void
{
114 $schemaNeedsAlter = \CRM_Core_BAO_SchemaHandler
::databaseSupportsUTF8MB4();
115 if ($schemaNeedsAlter) {
116 \CRM_Core_DAO
::executeQuery("
117 ALTER TABLE civicrm_contact MODIFY COLUMN
118 `first_name` VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'First Name.',
124 ->addWhere('first_name', '=', '🦉Claire')
126 if ($schemaNeedsAlter) {
127 \CRM_Core_DAO
::executeQuery("
128 ALTER TABLE civicrm_contact MODIFY COLUMN
129 `first_name` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'First Name.',
135 public function testEmptyAndNullOperators() {
136 $last_name = uniqid(__FUNCTION__
);
138 $bob = Contact
::create()
139 ->setValues(['first_name' => 'Bob', 'last_name' => $last_name, 'prefix_id' => 0])
140 ->execute()->first();
141 // Initial value is NULL, but to test the empty operator, change it to an empty string
142 \CRM_Core_DAO
::executeQuery("UPDATE civicrm_contact SET middle_name = '' WHERE id = " . $bob['id']);
144 $jan = Contact
::create()
145 ->setValues(['first_name' => 'Jan', 'middle_name' => 'J', 'last_name' => $last_name, 'prefix_id' => 1])
146 ->execute()->first();
148 $dan = Contact
::create()
149 ->setValues(['first_name' => 'Dan', 'last_name' => $last_name, 'prefix_id' => NULL])
150 ->execute()->first();
152 // Test EMPTY and NULL operators on string fields
153 $result = Contact
::get(FALSE)
154 ->addWhere('last_name', '=', $last_name)
155 ->addWhere('middle_name', 'IS EMPTY')
156 ->execute()->indexBy('id');
157 $this->assertCount(2, $result);
158 $this->assertArrayNotHasKey($jan['id'], (array) $result);
160 $result = Contact
::get(FALSE)
161 ->addWhere('last_name', '=', $last_name)
162 ->addWhere('middle_name', 'IS NOT NULL')
163 ->execute()->indexBy('id');
164 $this->assertCount(2, $result);
165 $this->assertArrayNotHasKey($dan['id'], (array) $result);
167 $result = Contact
::get(FALSE)
168 ->addWhere('last_name', '=', $last_name)
169 ->addWhere('middle_name', 'IS NOT EMPTY')
170 ->execute()->indexBy('id');
171 $this->assertCount(1, $result);
172 $this->assertArrayHasKey($jan['id'], (array) $result);
174 // Test EMPTY and NULL operators on Integer fields
175 $result = Contact
::get(FALSE)
176 ->addWhere('last_name', '=', $last_name)
177 ->addWhere('prefix_id', 'IS EMPTY')
178 ->execute()->indexBy('id');
179 $this->assertCount(2, $result);
180 $this->assertArrayNotHasKey($jan['id'], (array) $result);
182 $result = Contact
::get(FALSE)
183 ->addWhere('last_name', '=', $last_name)
184 ->addWhere('prefix_id', 'IS NOT NULL')
185 ->execute()->indexBy('id');
186 $this->assertCount(2, $result);
187 $this->assertArrayNotHasKey($dan['id'], (array) $result);
189 $result = Contact
::get(FALSE)
190 ->addWhere('last_name', '=', $last_name)
191 ->addWhere('prefix_id', 'IS NOT EMPTY')
192 ->execute()->indexBy('id');
193 $this->assertCount(1, $result);
194 $this->assertArrayHasKey($jan['id'], (array) $result);