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 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
22 | namespace api\v4\Action; |
23 | ||
24 | use Civi\Api4\Relationship; | |
25 | use api\v4\UnitTestCase; | |
26 | use Civi\Api4\Contact; | |
27 | ||
28 | /** | |
29 | * @group headless | |
30 | */ | |
31 | class CurrentFilterTest extends UnitTestCase { | |
32 | ||
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']; | |
36 | ||
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, | |
52 | 'end_date' => 'now', | |
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, | |
64 | 'is_active' => 0, | |
65 | ])->execute()->first(); | |
66 | ||
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'); | |
70 | ||
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); | |
76 | ||
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); | |
82 | ||
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); | |
88 | } | |
89 | ||
90 | } |