Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
f299f7db | 7 | | Copyright CiviCRM LLC (c) 2004-2020 | |
380f3545 TO |
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 | |
ca5cec67 | 32 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | namespace api\v4\Query; |
39 | ||
40 | use Civi\Api4\Query\Api4SelectQuery; | |
41 | use api\v4\UnitTestCase; | |
42 | ||
43 | /** | |
44 | * @group headless | |
45 | */ | |
46 | class Api4SelectQueryComplexJoinTest extends UnitTestCase { | |
47 | ||
48 | public function setUpHeadless() { | |
49 | $relatedTables = [ | |
50 | 'civicrm_address', | |
51 | 'civicrm_email', | |
52 | 'civicrm_phone', | |
53 | 'civicrm_openid', | |
54 | 'civicrm_im', | |
55 | 'civicrm_website', | |
56 | 'civicrm_activity', | |
57 | 'civicrm_activity_contact', | |
58 | ]; | |
59 | $this->cleanup(['tablesToTruncate' => $relatedTables]); | |
60 | $this->loadDataSet('SingleContact'); | |
61 | return parent::setUpHeadless(); | |
62 | } | |
63 | ||
64 | public function testWithComplexRelatedEntitySelect() { | |
65 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
66 | $query->select[] = 'id'; | |
67 | $query->select[] = 'display_name'; | |
68 | $query->select[] = 'phones.phone'; | |
69 | $query->select[] = 'emails.email'; | |
70 | $query->select[] = 'emails.location_type.name'; | |
71 | $query->select[] = 'created_activities.contact_id'; | |
72 | $query->select[] = 'created_activities.activity.subject'; | |
73 | $query->select[] = 'created_activities.activity.activity_type.name'; | |
74 | $query->where[] = ['first_name', '=', 'Single']; | |
75 | $query->where[] = ['id', '=', $this->getReference('test_contact_1')['id']]; | |
76 | $results = $query->run(); | |
77 | ||
78 | $testActivities = [ | |
79 | $this->getReference('test_activity_1'), | |
80 | $this->getReference('test_activity_2'), | |
81 | ]; | |
82 | $activitySubjects = array_column($testActivities, 'subject'); | |
83 | ||
84 | $this->assertCount(1, $results); | |
85 | $firstResult = array_shift($results); | |
86 | $this->assertArrayHasKey('created_activities', $firstResult); | |
87 | $firstCreatedActivity = array_shift($firstResult['created_activities']); | |
88 | $this->assertArrayHasKey('activity', $firstCreatedActivity); | |
89 | $firstActivity = $firstCreatedActivity['activity']; | |
90 | $this->assertContains($firstActivity['subject'], $activitySubjects); | |
91 | $this->assertArrayHasKey('activity_type', $firstActivity); | |
92 | $activityType = $firstActivity['activity_type']; | |
93 | $this->assertArrayHasKey('name', $activityType); | |
94 | } | |
95 | ||
96 | public function testWithSelectOfOrphanDeepValues() { | |
97 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
98 | $query->select[] = 'id'; | |
99 | $query->select[] = 'first_name'; | |
100 | // emails not selected | |
101 | $query->select[] = 'emails.location_type.name'; | |
102 | $results = $query->run(); | |
103 | $firstResult = array_shift($results); | |
104 | ||
105 | $this->assertEmpty($firstResult['emails']); | |
106 | } | |
107 | ||
108 | public function testOrderDoesNotMatter() { | |
109 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
110 | $query->select[] = 'id'; | |
111 | $query->select[] = 'first_name'; | |
112 | // before emails selection | |
113 | $query->select[] = 'emails.location_type.name'; | |
114 | $query->select[] = 'emails.email'; | |
115 | $query->where[] = ['emails.email', 'IS NOT NULL']; | |
116 | $results = $query->run(); | |
117 | $firstResult = array_shift($results); | |
118 | ||
119 | $this->assertNotEmpty($firstResult['emails'][0]['location_type']['name']); | |
120 | } | |
121 | ||
122 | } |