Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
7d61e75f | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
7d61e75f TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace api\v4\Action; |
21 | ||
22 | use Civi\Api4\Contact; | |
23 | ||
24 | /** | |
25 | * @group headless | |
26 | */ | |
27 | class ContactGetTest extends \api\v4\UnitTestCase { | |
28 | ||
29 | public function testGetDeletedContacts() { | |
30 | $last_name = uniqid('deleteContactTest'); | |
31 | ||
32 | $bob = Contact::create() | |
33 | ->setValues(['first_name' => 'Bob', 'last_name' => $last_name]) | |
34 | ->execute()->first(); | |
35 | ||
36 | $jan = Contact::create() | |
37 | ->setValues(['first_name' => 'Jan', 'last_name' => $last_name]) | |
38 | ->execute()->first(); | |
39 | ||
40 | $del = Contact::create() | |
41 | ->setValues(['first_name' => 'Del', 'last_name' => $last_name, 'is_deleted' => 1]) | |
42 | ->execute()->first(); | |
43 | ||
44 | // Deleted contacts are not fetched by default | |
45 | $this->assertCount(2, Contact::get()->addWhere('last_name', '=', $last_name)->selectRowCount()->execute()); | |
46 | ||
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']); | |
50 | ||
51 | // Or by id | |
52 | $this->assertCount(3, Contact::get()->addWhere('id', 'IN', [$bob['id'], $jan['id'], $del['id']])->selectRowCount()->execute()); | |
53 | ||
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')); | |
57 | } | |
58 | ||
f8bf8e26 CW |
59 | public function testGetWithLimit() { |
60 | $last_name = uniqid('getWithLimitTest'); | |
61 | ||
62 | $bob = Contact::create() | |
63 | ->setValues(['first_name' => 'Bob', 'last_name' => $last_name]) | |
64 | ->execute()->first(); | |
65 | ||
66 | $jan = Contact::create() | |
67 | ->setValues(['first_name' => 'Jan', 'last_name' => $last_name]) | |
68 | ->execute()->first(); | |
69 | ||
70 | $dan = Contact::create() | |
71 | ->setValues(['first_name' => 'Dan', 'last_name' => $last_name]) | |
72 | ->execute()->first(); | |
73 | ||
fe806431 | 74 | $num = Contact::get(FALSE)->selectRowCount()->execute()->count(); |
651c4c95 CW |
75 | |
76 | // The object's count() method will account for all results, ignoring limit & offset, while the array results are limited | |
fe806431 | 77 | $offset1 = Contact::get(FALSE)->setOffset(1)->execute(); |
651c4c95 CW |
78 | $this->assertCount($num, $offset1); |
79 | $this->assertCount($num - 1, (array) $offset1); | |
fe806431 | 80 | $offset2 = Contact::get(FALSE)->setOffset(2)->execute(); |
651c4c95 CW |
81 | $this->assertCount($num - 2, (array) $offset2); |
82 | $this->assertCount($num, $offset2); | |
83 | // With limit, it doesn't fetch total count by default | |
fe806431 | 84 | $limit2 = Contact::get(FALSE)->setLimit(2)->execute(); |
651c4c95 CW |
85 | $this->assertCount(2, (array) $limit2); |
86 | $this->assertCount(2, $limit2); | |
87 | // With limit, you have to trigger the full row count manually | |
fe806431 | 88 | $limit2 = Contact::get(FALSE)->setLimit(2)->addSelect('sort_name', 'row_count')->execute(); |
651c4c95 CW |
89 | $this->assertCount(2, (array) $limit2); |
90 | $this->assertCount($num, $limit2); | |
4c6ad36a TO |
91 | try { |
92 | $limit2->single(); | |
93 | } | |
94 | catch (\API_Exception $e) { | |
95 | $this->assertRegExp(';Expected to find one Contact record;', $e->getMessage()); | |
96 | } | |
97 | $limit1 = Contact::get(FALSE)->setLimit(1)->execute(); | |
98 | $this->assertCount(1, (array) $limit1); | |
99 | $this->assertCount(1, $limit1); | |
100 | $this->assertTrue(!empty($limit1->single()['sort_name'])); | |
f8bf8e26 CW |
101 | } |
102 | ||
19b53e5b | 103 | } |