Merge pull request #17874 from colemanw/checkPermShort
[civicrm-core.git] / tests / phpunit / api / v4 / Action / ContactGetTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace api\v4\Action;
23
24 use Civi\Api4\Contact;
25
26 /**
27 * @group headless
28 */
29 class ContactGetTest extends \api\v4\UnitTestCase {
30
31 public function testGetDeletedContacts() {
32 $last_name = uniqid('deleteContactTest');
33
34 $bob = Contact::create()
35 ->setValues(['first_name' => 'Bob', 'last_name' => $last_name])
36 ->execute()->first();
37
38 $jan = Contact::create()
39 ->setValues(['first_name' => 'Jan', 'last_name' => $last_name])
40 ->execute()->first();
41
42 $del = Contact::create()
43 ->setValues(['first_name' => 'Del', 'last_name' => $last_name, 'is_deleted' => 1])
44 ->execute()->first();
45
46 // Deleted contacts are not fetched by default
47 $this->assertCount(2, Contact::get()->addWhere('last_name', '=', $last_name)->selectRowCount()->execute());
48
49 // You can search for them specifically
50 $contacts = Contact::get()->addWhere('last_name', '=', $last_name)->addWhere('is_deleted', '=', 1)->addSelect('id')->execute();
51 $this->assertEquals($del['id'], $contacts->first()['id']);
52
53 // Or by id
54 $this->assertCount(3, Contact::get()->addWhere('id', 'IN', [$bob['id'], $jan['id'], $del['id']])->selectRowCount()->execute());
55
56 // Putting is_deleted anywhere in the where clause will disable the default
57 $contacts = Contact::get()->addClause('OR', ['last_name', '=', $last_name], ['is_deleted', '=', 0])->addSelect('id')->execute();
58 $this->assertContains($del['id'], $contacts->column('id'));
59 }
60
61 public function testGetWithLimit() {
62 $last_name = uniqid('getWithLimitTest');
63
64 $bob = Contact::create()
65 ->setValues(['first_name' => 'Bob', 'last_name' => $last_name])
66 ->execute()->first();
67
68 $jan = Contact::create()
69 ->setValues(['first_name' => 'Jan', 'last_name' => $last_name])
70 ->execute()->first();
71
72 $dan = Contact::create()
73 ->setValues(['first_name' => 'Dan', 'last_name' => $last_name])
74 ->execute()->first();
75
76 $num = Contact::get(FALSE)->selectRowCount()->execute()->count();
77
78 // The object's count() method will account for all results, ignoring limit & offset, while the array results are limited
79 $offset1 = Contact::get(FALSE)->setOffset(1)->execute();
80 $this->assertCount($num, $offset1);
81 $this->assertCount($num - 1, (array) $offset1);
82 $offset2 = Contact::get(FALSE)->setOffset(2)->execute();
83 $this->assertCount($num - 2, (array) $offset2);
84 $this->assertCount($num, $offset2);
85 // With limit, it doesn't fetch total count by default
86 $limit2 = Contact::get(FALSE)->setLimit(2)->execute();
87 $this->assertCount(2, (array) $limit2);
88 $this->assertCount(2, $limit2);
89 // With limit, you have to trigger the full row count manually
90 $limit2 = Contact::get(FALSE)->setLimit(2)->addSelect('sort_name', 'row_count')->execute();
91 $this->assertCount(2, (array) $limit2);
92 $this->assertCount($num, $limit2);
93 }
94
95 }