4034dbed81497bfa0beabc7827ca2671a65f0767
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CurrentFilterTest.php
1 <?php
2
3 namespace api\v4\Action;
4
5 use Civi\Api4\Relationship;
6 use api\v4\UnitTestCase;
7 use Civi\Api4\Contact;
8
9 /**
10 * @group headless
11 */
12 class CurrentFilterTest extends UnitTestCase {
13
14 public function testCurrentRelationship() {
15 $cid1 = Contact::create()->addValue('first_name', 'Bob1')->execute()->first()['id'];
16 $cid2 = Contact::create()->addValue('first_name', 'Bob2')->execute()->first()['id'];
17
18 $current = Relationship::create()->setValues([
19 'relationship_type_id' => 1,
20 'contact_id_a' => $cid1,
21 'contact_id_b' => $cid2,
22 'end_date' => 'now + 1 week',
23 ])->execute()->first();
24 $indefinite = Relationship::create()->setValues([
25 'relationship_type_id' => 2,
26 'contact_id_a' => $cid1,
27 'contact_id_b' => $cid2,
28 ])->execute()->first();
29 $expiring = Relationship::create()->setValues([
30 'relationship_type_id' => 3,
31 'contact_id_a' => $cid1,
32 'contact_id_b' => $cid2,
33 'end_date' => 'now',
34 ])->execute()->first();
35 $past = Relationship::create()->setValues([
36 'relationship_type_id' => 3,
37 'contact_id_a' => $cid1,
38 'contact_id_b' => $cid2,
39 'end_date' => 'now - 1 week',
40 ])->execute()->first();
41 $inactive = Relationship::create()->setValues([
42 'relationship_type_id' => 4,
43 'contact_id_a' => $cid1,
44 'contact_id_b' => $cid2,
45 'is_active' => 0,
46 ])->execute()->first();
47
48 $getCurrent = (array) Relationship::get()->setCurrent(TRUE)->execute()->indexBy('id');
49 $notCurrent = (array) Relationship::get()->setCurrent(FALSE)->execute()->indexBy('id');
50 $getAll = (array) Relationship::get()->execute()->indexBy('id');
51
52 $this->assertArrayHasKey($current['id'], $getAll);
53 $this->assertArrayHasKey($indefinite['id'], $getAll);
54 $this->assertArrayHasKey($expiring['id'], $getAll);
55 $this->assertArrayHasKey($past['id'], $getAll);
56 $this->assertArrayHasKey($inactive['id'], $getAll);
57
58 $this->assertArrayHasKey($current['id'], $getCurrent);
59 $this->assertArrayHasKey($indefinite['id'], $getCurrent);
60 $this->assertArrayHasKey($expiring['id'], $getCurrent);
61 $this->assertArrayNotHasKey($past['id'], $getCurrent);
62 $this->assertArrayNotHasKey($inactive['id'], $getCurrent);
63
64 $this->assertArrayNotHasKey($current['id'], $notCurrent);
65 $this->assertArrayNotHasKey($indefinite['id'], $notCurrent);
66 $this->assertArrayNotHasKey($expiring['id'], $notCurrent);
67 $this->assertArrayHasKey($past['id'], $notCurrent);
68 $this->assertArrayHasKey($inactive['id'], $notCurrent);
69 }
70
71 }