APIv4 - Fix saving NULL as custom field value
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CurrentFilterTest.php
CommitLineData
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
20namespace api\v4\Action;
21
22use Civi\Api4\Relationship;
23use api\v4\UnitTestCase;
24use Civi\Api4\Contact;
25
26/**
27 * @group headless
28 */
29class CurrentFilterTest extends UnitTestCase {
30
31 public function testCurrentRelationship() {
32 $cid1 = Contact::create()->addValue('first_name', 'Bob1')->execute()->first()['id'];
33 $cid2 = Contact::create()->addValue('first_name', 'Bob2')->execute()->first()['id'];
34
35 $current = Relationship::create()->setValues([
36 'relationship_type_id' => 1,
37 'contact_id_a' => $cid1,
38 'contact_id_b' => $cid2,
39 'end_date' => 'now + 1 week',
40 ])->execute()->first();
41 $indefinite = Relationship::create()->setValues([
42 'relationship_type_id' => 2,
43 'contact_id_a' => $cid1,
44 'contact_id_b' => $cid2,
45 ])->execute()->first();
46 $expiring = Relationship::create()->setValues([
47 'relationship_type_id' => 3,
48 'contact_id_a' => $cid1,
49 'contact_id_b' => $cid2,
50 'end_date' => 'now',
51 ])->execute()->first();
52 $past = Relationship::create()->setValues([
53 'relationship_type_id' => 3,
54 'contact_id_a' => $cid1,
55 'contact_id_b' => $cid2,
56 'end_date' => 'now - 1 week',
57 ])->execute()->first();
58 $inactive = Relationship::create()->setValues([
59 'relationship_type_id' => 4,
60 'contact_id_a' => $cid1,
61 'contact_id_b' => $cid2,
62 'is_active' => 0,
63 ])->execute()->first();
64
9c961a3a
CW
65 $getCurrent = Relationship::get()->addWhere('is_current', '=', TRUE)->execute()->indexBy('id');
66 $notCurrent = Relationship::get()->addWhere('is_current', '=', FALSE)->execute()->indexBy('id');
67 $getAll = Relationship::get()->addSelect('is_current')->execute()->indexBy('id');
19b53e5b 68
9c961a3a
CW
69 $this->assertTrue($getAll[$current['id']]['is_current']);
70 $this->assertTrue($getAll[$indefinite['id']]['is_current']);
71 $this->assertTrue($getAll[$expiring['id']]['is_current']);
72 $this->assertFalse($getAll[$past['id']]['is_current']);
73 $this->assertFalse($getAll[$inactive['id']]['is_current']);
19b53e5b
C
74
75 $this->assertArrayHasKey($current['id'], $getCurrent);
76 $this->assertArrayHasKey($indefinite['id'], $getCurrent);
77 $this->assertArrayHasKey($expiring['id'], $getCurrent);
78 $this->assertArrayNotHasKey($past['id'], $getCurrent);
79 $this->assertArrayNotHasKey($inactive['id'], $getCurrent);
80
81 $this->assertArrayNotHasKey($current['id'], $notCurrent);
82 $this->assertArrayNotHasKey($indefinite['id'], $notCurrent);
83 $this->assertArrayNotHasKey($expiring['id'], $notCurrent);
84 $this->assertArrayHasKey($past['id'], $notCurrent);
85 $this->assertArrayHasKey($inactive['id'], $notCurrent);
60a62215
CW
86
87 // Assert that "Extra" fields like is_current are not returned with select *
88 $defaultGet = Relationship::get()->setLimit(1)->execute()->single();
89 $this->assertArrayNotHasKey('is_current', $defaultGet);
90 $starGet = Relationship::get()->addSelect('*')->setLimit(1)->execute()->single();
91 $this->assertArrayNotHasKey('is_current', $starGet);
19b53e5b
C
92 }
93
94}