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
22 namespace api\v
4\Action
;
24 use Civi\Api4\Relationship
;
25 use api\v
4\UnitTestCase
;
26 use Civi\Api4\Contact
;
31 class CurrentFilterTest
extends UnitTestCase
{
33 public function testCurrentRelationship() {
34 $cid1 = Contact
::create()->addValue('first_name', 'Bob1')->execute()->first()['id'];
35 $cid2 = Contact
::create()->addValue('first_name', 'Bob2')->execute()->first()['id'];
37 $current = Relationship
::create()->setValues([
38 'relationship_type_id' => 1,
39 'contact_id_a' => $cid1,
40 'contact_id_b' => $cid2,
41 'end_date' => 'now + 1 week',
42 ])->execute()->first();
43 $indefinite = Relationship
::create()->setValues([
44 'relationship_type_id' => 2,
45 'contact_id_a' => $cid1,
46 'contact_id_b' => $cid2,
47 ])->execute()->first();
48 $expiring = Relationship
::create()->setValues([
49 'relationship_type_id' => 3,
50 'contact_id_a' => $cid1,
51 'contact_id_b' => $cid2,
53 ])->execute()->first();
54 $past = Relationship
::create()->setValues([
55 'relationship_type_id' => 3,
56 'contact_id_a' => $cid1,
57 'contact_id_b' => $cid2,
58 'end_date' => 'now - 1 week',
59 ])->execute()->first();
60 $inactive = Relationship
::create()->setValues([
61 'relationship_type_id' => 4,
62 'contact_id_a' => $cid1,
63 'contact_id_b' => $cid2,
65 ])->execute()->first();
67 $getCurrent = (array) Relationship
::get()->setCurrent(TRUE)->execute()->indexBy('id');
68 $notCurrent = (array) Relationship
::get()->setCurrent(FALSE)->execute()->indexBy('id');
69 $getAll = (array) Relationship
::get()->execute()->indexBy('id');
71 $this->assertArrayHasKey($current['id'], $getAll);
72 $this->assertArrayHasKey($indefinite['id'], $getAll);
73 $this->assertArrayHasKey($expiring['id'], $getAll);
74 $this->assertArrayHasKey($past['id'], $getAll);
75 $this->assertArrayHasKey($inactive['id'], $getAll);
77 $this->assertArrayHasKey($current['id'], $getCurrent);
78 $this->assertArrayHasKey($indefinite['id'], $getCurrent);
79 $this->assertArrayHasKey($expiring['id'], $getCurrent);
80 $this->assertArrayNotHasKey($past['id'], $getCurrent);
81 $this->assertArrayNotHasKey($inactive['id'], $getCurrent);
83 $this->assertArrayNotHasKey($current['id'], $notCurrent);
84 $this->assertArrayNotHasKey($indefinite['id'], $notCurrent);
85 $this->assertArrayNotHasKey($expiring['id'], $notCurrent);
86 $this->assertArrayHasKey($past['id'], $notCurrent);
87 $this->assertArrayHasKey($inactive['id'], $notCurrent);