Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
3 | namespace api\v4\Query; | |
4 | ||
5 | use Civi\Api4\Query\Api4SelectQuery; | |
6 | use api\v4\UnitTestCase; | |
7 | ||
8 | /** | |
9 | * @group headless | |
10 | */ | |
11 | class Api4SelectQueryComplexJoinTest extends UnitTestCase { | |
12 | ||
13 | public function setUpHeadless() { | |
14 | $relatedTables = [ | |
15 | 'civicrm_address', | |
16 | 'civicrm_email', | |
17 | 'civicrm_phone', | |
18 | 'civicrm_openid', | |
19 | 'civicrm_im', | |
20 | 'civicrm_website', | |
21 | 'civicrm_activity', | |
22 | 'civicrm_activity_contact', | |
23 | ]; | |
24 | $this->cleanup(['tablesToTruncate' => $relatedTables]); | |
25 | $this->loadDataSet('SingleContact'); | |
26 | return parent::setUpHeadless(); | |
27 | } | |
28 | ||
29 | public function testWithComplexRelatedEntitySelect() { | |
30 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
31 | $query->select[] = 'id'; | |
32 | $query->select[] = 'display_name'; | |
33 | $query->select[] = 'phones.phone'; | |
34 | $query->select[] = 'emails.email'; | |
35 | $query->select[] = 'emails.location_type.name'; | |
36 | $query->select[] = 'created_activities.contact_id'; | |
37 | $query->select[] = 'created_activities.activity.subject'; | |
38 | $query->select[] = 'created_activities.activity.activity_type.name'; | |
39 | $query->where[] = ['first_name', '=', 'Single']; | |
40 | $query->where[] = ['id', '=', $this->getReference('test_contact_1')['id']]; | |
41 | $results = $query->run(); | |
42 | ||
43 | $testActivities = [ | |
44 | $this->getReference('test_activity_1'), | |
45 | $this->getReference('test_activity_2'), | |
46 | ]; | |
47 | $activitySubjects = array_column($testActivities, 'subject'); | |
48 | ||
49 | $this->assertCount(1, $results); | |
50 | $firstResult = array_shift($results); | |
51 | $this->assertArrayHasKey('created_activities', $firstResult); | |
52 | $firstCreatedActivity = array_shift($firstResult['created_activities']); | |
53 | $this->assertArrayHasKey('activity', $firstCreatedActivity); | |
54 | $firstActivity = $firstCreatedActivity['activity']; | |
55 | $this->assertContains($firstActivity['subject'], $activitySubjects); | |
56 | $this->assertArrayHasKey('activity_type', $firstActivity); | |
57 | $activityType = $firstActivity['activity_type']; | |
58 | $this->assertArrayHasKey('name', $activityType); | |
59 | } | |
60 | ||
61 | public function testWithSelectOfOrphanDeepValues() { | |
62 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
63 | $query->select[] = 'id'; | |
64 | $query->select[] = 'first_name'; | |
65 | // emails not selected | |
66 | $query->select[] = 'emails.location_type.name'; | |
67 | $results = $query->run(); | |
68 | $firstResult = array_shift($results); | |
69 | ||
70 | $this->assertEmpty($firstResult['emails']); | |
71 | } | |
72 | ||
73 | public function testOrderDoesNotMatter() { | |
74 | $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name')); | |
75 | $query->select[] = 'id'; | |
76 | $query->select[] = 'first_name'; | |
77 | // before emails selection | |
78 | $query->select[] = 'emails.location_type.name'; | |
79 | $query->select[] = 'emails.email'; | |
80 | $query->where[] = ['emails.email', 'IS NOT NULL']; | |
81 | $results = $query->run(); | |
82 | $firstResult = array_shift($results); | |
83 | ||
84 | $this->assertNotEmpty($firstResult['emails'][0]['location_type']['name']); | |
85 | } | |
86 | ||
87 | } |