Merge pull request #15293 from ixiam/dev#issue-826
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CurrentFilterTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
38 namespace api\v4\Action;
39
40 use Civi\Api4\Relationship;
41 use api\v4\UnitTestCase;
42 use Civi\Api4\Contact;
43
44 /**
45 * @group headless
46 */
47 class CurrentFilterTest extends UnitTestCase {
48
49 public function testCurrentRelationship() {
50 $cid1 = Contact::create()->addValue('first_name', 'Bob1')->execute()->first()['id'];
51 $cid2 = Contact::create()->addValue('first_name', 'Bob2')->execute()->first()['id'];
52
53 $current = Relationship::create()->setValues([
54 'relationship_type_id' => 1,
55 'contact_id_a' => $cid1,
56 'contact_id_b' => $cid2,
57 'end_date' => 'now + 1 week',
58 ])->execute()->first();
59 $indefinite = Relationship::create()->setValues([
60 'relationship_type_id' => 2,
61 'contact_id_a' => $cid1,
62 'contact_id_b' => $cid2,
63 ])->execute()->first();
64 $expiring = Relationship::create()->setValues([
65 'relationship_type_id' => 3,
66 'contact_id_a' => $cid1,
67 'contact_id_b' => $cid2,
68 'end_date' => 'now',
69 ])->execute()->first();
70 $past = Relationship::create()->setValues([
71 'relationship_type_id' => 3,
72 'contact_id_a' => $cid1,
73 'contact_id_b' => $cid2,
74 'end_date' => 'now - 1 week',
75 ])->execute()->first();
76 $inactive = Relationship::create()->setValues([
77 'relationship_type_id' => 4,
78 'contact_id_a' => $cid1,
79 'contact_id_b' => $cid2,
80 'is_active' => 0,
81 ])->execute()->first();
82
83 $getCurrent = (array) Relationship::get()->setCurrent(TRUE)->execute()->indexBy('id');
84 $notCurrent = (array) Relationship::get()->setCurrent(FALSE)->execute()->indexBy('id');
85 $getAll = (array) Relationship::get()->execute()->indexBy('id');
86
87 $this->assertArrayHasKey($current['id'], $getAll);
88 $this->assertArrayHasKey($indefinite['id'], $getAll);
89 $this->assertArrayHasKey($expiring['id'], $getAll);
90 $this->assertArrayHasKey($past['id'], $getAll);
91 $this->assertArrayHasKey($inactive['id'], $getAll);
92
93 $this->assertArrayHasKey($current['id'], $getCurrent);
94 $this->assertArrayHasKey($indefinite['id'], $getCurrent);
95 $this->assertArrayHasKey($expiring['id'], $getCurrent);
96 $this->assertArrayNotHasKey($past['id'], $getCurrent);
97 $this->assertArrayNotHasKey($inactive['id'], $getCurrent);
98
99 $this->assertArrayNotHasKey($current['id'], $notCurrent);
100 $this->assertArrayNotHasKey($indefinite['id'], $notCurrent);
101 $this->assertArrayNotHasKey($expiring['id'], $notCurrent);
102 $this->assertArrayHasKey($past['id'], $notCurrent);
103 $this->assertArrayHasKey($inactive['id'], $notCurrent);
104 }
105
106 }