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 api\v4\UnitTestCase; | |
25 | use Civi\Api4\Activity; | |
26 | use Civi\Api4\Contact; | |
16f5a13d CW |
27 | use Civi\Api4\Email; |
28 | use Civi\Api4\Phone; | |
19b53e5b C |
29 | |
30 | /** | |
31 | * @group headless | |
32 | */ | |
33 | class FkJoinTest extends UnitTestCase { | |
34 | ||
35 | public function setUpHeadless() { | |
36 | $relatedTables = [ | |
37 | 'civicrm_activity', | |
38 | 'civicrm_phone', | |
39 | 'civicrm_activity_contact', | |
40 | ]; | |
41 | $this->cleanup(['tablesToTruncate' => $relatedTables]); | |
42 | $this->loadDataSet('DefaultDataSet'); | |
43 | ||
44 | return parent::setUpHeadless(); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Fetch all phone call activities. Expects a single activity | |
49 | * loaded from the data set. | |
50 | */ | |
51 | public function testThreeLevelJoin() { | |
52 | $results = Activity::get() | |
53 | ->setCheckPermissions(FALSE) | |
340afbdf | 54 | ->addWhere('activity_type_id:name', '=', 'Phone Call') |
19b53e5b C |
55 | ->execute(); |
56 | ||
57 | $this->assertCount(1, $results); | |
58 | } | |
59 | ||
16f5a13d CW |
60 | public function testOptionalJoin() { |
61 | // DefaultDataSet includes 2 phones for contact 1, 0 for contact 2. | |
62 | // We'll add one for contact 2 as a red herring to make sure we only get back the correct ones. | |
63 | Phone::create()->setCheckPermissions(FALSE) | |
64 | ->setValues(['contact_id' => $this->getReference('test_contact_2')['id'], 'phone' => '123456']) | |
65 | ->execute(); | |
66 | $contacts = Contact::get() | |
67 | ->setCheckPermissions(FALSE) | |
68 | ->addJoin('Phone', FALSE) | |
69 | ->addSelect('id', 'phone.phone') | |
70 | ->addWhere('id', 'IN', [$this->getReference('test_contact_1')['id']]) | |
71 | ->addOrderBy('phone.id') | |
72 | ->execute(); | |
73 | $this->assertCount(2, $contacts); | |
74 | $this->assertEquals($this->getReference('test_contact_1')['id'], $contacts[0]['id']); | |
75 | $this->assertEquals($this->getReference('test_contact_1')['id'], $contacts[1]['id']); | |
76 | } | |
77 | ||
78 | public function testRequiredJoin() { | |
79 | // Joining with no condition | |
80 | $contacts = Contact::get() | |
81 | ->setCheckPermissions(FALSE) | |
82 | ->addSelect('id', 'phone.phone') | |
83 | ->addJoin('Phone', TRUE) | |
84 | ->addWhere('id', 'IN', [$this->getReference('test_contact_1')['id'], $this->getReference('test_contact_2')['id']]) | |
85 | ->addOrderBy('phone.id') | |
86 | ->execute(); | |
87 | $this->assertCount(2, $contacts); | |
88 | $this->assertEquals($this->getReference('test_contact_1')['id'], $contacts[0]['id']); | |
89 | $this->assertEquals($this->getReference('test_contact_1')['id'], $contacts[1]['id']); | |
90 | ||
91 | // Add is_primary condition, should result in only one record | |
92 | $contacts = Contact::get() | |
93 | ->setCheckPermissions(FALSE) | |
94 | ->addSelect('id', 'phone.phone', 'phone.location_type_id') | |
95 | ->addJoin('Phone', TRUE, ['phone.is_primary', '=', TRUE]) | |
96 | ->addWhere('id', 'IN', [$this->getReference('test_contact_1')['id'], $this->getReference('test_contact_2')['id']]) | |
97 | ->addOrderBy('phone.id') | |
98 | ->execute(); | |
99 | $this->assertCount(1, $contacts); | |
100 | $this->assertEquals($this->getReference('test_contact_1')['id'], $contacts[0]['id']); | |
101 | $this->assertEquals('+35355439483', $contacts[0]['phone.phone']); | |
102 | $this->assertEquals('1', $contacts[0]['phone.location_type_id']); | |
103 | } | |
104 | ||
105 | public function testJoinToTheSameTableTwice() { | |
106 | $cid1 = Contact::create()->setCheckPermissions(FALSE) | |
107 | ->addValue('first_name', 'Aaa') | |
108 | ->addChain('email1', Email::create()->setValues(['email' => 'yoohoo@yahoo.test', 'contact_id' => '$id', 'location_type_id:name' => 'Home'])) | |
109 | ->addChain('email2', Email::create()->setValues(['email' => 'yahoo@yoohoo.test', 'contact_id' => '$id', 'location_type_id:name' => 'Work'])) | |
110 | ->execute() | |
111 | ->first()['id']; | |
112 | ||
113 | $cid2 = Contact::create()->setCheckPermissions(FALSE) | |
114 | ->addValue('first_name', 'Bbb') | |
115 | ->addChain('email1', Email::create()->setValues(['email' => '1@test.test', 'contact_id' => '$id', 'location_type_id:name' => 'Home'])) | |
116 | ->addChain('email2', Email::create()->setValues(['email' => '2@test.test', 'contact_id' => '$id', 'location_type_id:name' => 'Work'])) | |
117 | ->addChain('email3', Email::create()->setValues(['email' => '3@test.test', 'contact_id' => '$id', 'location_type_id:name' => 'Other'])) | |
118 | ->execute() | |
119 | ->first()['id']; | |
120 | ||
121 | $cid3 = Contact::create()->setCheckPermissions(FALSE) | |
122 | ->addValue('first_name', 'Ccc') | |
123 | ->execute() | |
124 | ->first()['id']; | |
125 | ||
126 | $contacts = Contact::get() | |
127 | ->setCheckPermissions(FALSE) | |
128 | ->addSelect('id', 'first_name', 'any_email.email', 'any_email.location_type_id:name', 'any_email.is_primary', 'primary_email.email') | |
129 | ->addJoin('Email AS any_email', TRUE) | |
130 | ->addJoin('Email AS primary_email', FALSE, ['primary_email.is_primary', '=', TRUE]) | |
131 | ->addWhere('id', 'IN', [$cid1, $cid2, $cid3]) | |
132 | ->addOrderBy('any_email.id') | |
133 | ->setDebug(TRUE) | |
134 | ->execute(); | |
135 | $this->assertCount(5, $contacts); | |
136 | $this->assertEquals('Home', $contacts[0]['any_email.location_type_id:name']); | |
137 | $this->assertEquals('yoohoo@yahoo.test', $contacts[1]['primary_email.email']); | |
138 | $this->assertEquals('1@test.test', $contacts[2]['primary_email.email']); | |
139 | $this->assertEquals('1@test.test', $contacts[3]['primary_email.email']); | |
140 | $this->assertEquals('1@test.test', $contacts[4]['primary_email.email']); | |
141 | } | |
142 | ||
19b53e5b | 143 | } |