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\Query; |
23 | ||
24 | use Civi\Api4\Query\Api4SelectQuery; | |
25 | use api\v4\UnitTestCase; | |
26 | ||
27 | /** | |
28 | * @group headless | |
29 | */ | |
30 | class Api4SelectQueryComplexJoinTest extends UnitTestCase { | |
31 | ||
32 | public function setUpHeadless() { | |
33 | $relatedTables = [ | |
34 | 'civicrm_address', | |
35 | 'civicrm_email', | |
36 | 'civicrm_phone', | |
37 | 'civicrm_openid', | |
38 | 'civicrm_im', | |
39 | 'civicrm_website', | |
40 | 'civicrm_activity', | |
41 | 'civicrm_activity_contact', | |
42 | ]; | |
43 | $this->cleanup(['tablesToTruncate' => $relatedTables]); | |
44 | $this->loadDataSet('SingleContact'); | |
45 | return parent::setUpHeadless(); | |
46 | } | |
47 | ||
48 | public function testWithComplexRelatedEntitySelect() { | |
49 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
50 | $query->select[] = 'id'; | |
51 | $query->select[] = 'display_name'; | |
52 | $query->select[] = 'phones.phone'; | |
53 | $query->select[] = 'emails.email'; | |
54 | $query->select[] = 'emails.location_type.name'; | |
55 | $query->select[] = 'created_activities.contact_id'; | |
56 | $query->select[] = 'created_activities.activity.subject'; | |
57 | $query->select[] = 'created_activities.activity.activity_type.name'; | |
58 | $query->where[] = ['first_name', '=', 'Single']; | |
59 | $query->where[] = ['id', '=', $this->getReference('test_contact_1')['id']]; | |
60 | $results = $query->run(); | |
61 | ||
62 | $testActivities = [ | |
63 | $this->getReference('test_activity_1'), | |
64 | $this->getReference('test_activity_2'), | |
65 | ]; | |
66 | $activitySubjects = array_column($testActivities, 'subject'); | |
67 | ||
68 | $this->assertCount(1, $results); | |
69 | $firstResult = array_shift($results); | |
70 | $this->assertArrayHasKey('created_activities', $firstResult); | |
71 | $firstCreatedActivity = array_shift($firstResult['created_activities']); | |
72 | $this->assertArrayHasKey('activity', $firstCreatedActivity); | |
73 | $firstActivity = $firstCreatedActivity['activity']; | |
74 | $this->assertContains($firstActivity['subject'], $activitySubjects); | |
75 | $this->assertArrayHasKey('activity_type', $firstActivity); | |
76 | $activityType = $firstActivity['activity_type']; | |
77 | $this->assertArrayHasKey('name', $activityType); | |
78 | } | |
79 | ||
80 | public function testWithSelectOfOrphanDeepValues() { | |
81 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
82 | $query->select[] = 'id'; | |
83 | $query->select[] = 'first_name'; | |
84 | // emails not selected | |
85 | $query->select[] = 'emails.location_type.name'; | |
86 | $results = $query->run(); | |
87 | $firstResult = array_shift($results); | |
88 | ||
89 | $this->assertEmpty($firstResult['emails']); | |
90 | } | |
91 | ||
92 | public function testOrderDoesNotMatter() { | |
93 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
94 | $query->select[] = 'id'; | |
95 | $query->select[] = 'first_name'; | |
96 | // before emails selection | |
97 | $query->select[] = 'emails.location_type.name'; | |
98 | $query->select[] = 'emails.email'; | |
99 | $query->where[] = ['emails.email', 'IS NOT NULL']; | |
100 | $results = $query->run(); | |
101 | $firstResult = array_shift($results); | |
102 | ||
103 | $this->assertNotEmpty($firstResult['emails'][0]['location_type']['name']); | |
104 | } | |
105 | ||
106 | } |