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.',