Api4 - Support wildcard * in select clause
[civicrm-core.git] / tests / phpunit / api / v4 / Query / Api4SelectQueryComplexJoinTest.php
CommitLineData
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
22namespace api\v4\Query;
23
24use Civi\Api4\Query\Api4SelectQuery;
25use api\v4\UnitTestCase;
26
27/**
28 * @group headless
29 */
30class 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';
39e0f675 52 $query->select[] = 'phones.*_id';
19b53e5b
C
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);
39e0f675
CW
78
79 $this->assertArrayHasKey('name', $firstResult['emails'][0]['location_type']);
80 $this->assertArrayHasKey('location_type_id', $firstResult['phones'][0]);
81 $this->assertArrayHasKey('id', $firstResult['phones'][0]);
82 $this->assertArrayNotHasKey('phone', $firstResult['phones'][0]);
19b53e5b
C
83 }
84
85 public function testWithSelectOfOrphanDeepValues() {
86 $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name'));
87 $query->select[] = 'id';
88 $query->select[] = 'first_name';
89 // emails not selected
90 $query->select[] = 'emails.location_type.name';
91 $results = $query->run();
92 $firstResult = array_shift($results);
93
94 $this->assertEmpty($firstResult['emails']);
95 }
96
97 public function testOrderDoesNotMatter() {
98 $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name'));
99 $query->select[] = 'id';
100 $query->select[] = 'first_name';
101 // before emails selection
102 $query->select[] = 'emails.location_type.name';
103 $query->select[] = 'emails.email';
104 $query->where[] = ['emails.email', 'IS NOT NULL'];
105 $results = $query->run();
106 $firstResult = array_shift($results);
107
108 $this->assertNotEmpty($firstResult['emails'][0]['location_type']['name']);
109 }
110
111}