APIv4 - Cleanup and restructure the way get query objects are constructed
[civicrm-core.git] / tests / phpunit / api / v4 / Query / Api4SelectQueryTest.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 Api4SelectQueryTest 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('DefaultDataSet');
45 $displayNameFormat = '{contact.first_name}{ }{contact.last_name}';
46 \Civi::settings()->set('display_name_format', $displayNameFormat);
47
48 return parent::setUpHeadless();
49 }
50
51 public function testWithSingleWhereJoin() {
52 $phoneNum = $this->getReference('test_phone_1')['phone'];
53
3c7c8fa6
CW
54 $api = \Civi\API\Request::create('Contact', 'get', ['version' => 4, 'checkPermissions' => FALSE]);
55 $query = new Api4SelectQuery($api);
19b53e5b
C
56 $query->where[] = ['phones.phone', '=', $phoneNum];
57 $results = $query->run();
58
59 $this->assertCount(1, $results);
60 }
61
62 public function testOneToManyJoin() {
63 $phoneNum = $this->getReference('test_phone_1')['phone'];
64
3c7c8fa6
CW
65 $api = \Civi\API\Request::create('Contact', 'get', ['version' => 4, 'checkPermissions' => FALSE]);
66 $query = new Api4SelectQuery($api);
19b53e5b
C
67 $query->select[] = 'id';
68 $query->select[] = 'first_name';
69 $query->select[] = 'phones.phone';
70 $query->where[] = ['phones.phone', '=', $phoneNum];
71 $results = $query->run();
72
73 $this->assertCount(1, $results);
74 $firstResult = array_shift($results);
75 $this->assertArrayHasKey('phones', $firstResult);
76 $firstPhone = array_shift($firstResult['phones']);
77 $this->assertEquals($phoneNum, $firstPhone['phone']);
78 }
79
80 public function testManyToOneJoin() {
81 $phoneNum = $this->getReference('test_phone_1')['phone'];
82 $contact = $this->getReference('test_contact_1');
83
3c7c8fa6
CW
84 $api = \Civi\API\Request::create('Phone', 'get', ['version' => 4, 'checkPermissions' => FALSE]);
85 $query = new Api4SelectQuery($api);
19b53e5b
C
86 $query->select[] = 'id';
87 $query->select[] = 'phone';
88 $query->select[] = 'contact.display_name';
89 $query->select[] = 'contact.first_name';
90 $query->where[] = ['phone', '=', $phoneNum];
91 $results = $query->run();
92
93 $this->assertCount(1, $results);
94 $firstResult = array_shift($results);
95 $this->assertEquals($contact['display_name'], $firstResult['contact.display_name']);
96 }
97
98 public function testOneToManyMultipleJoin() {
3c7c8fa6
CW
99 $api = \Civi\API\Request::create('Contact', 'get', ['version' => 4, 'checkPermissions' => FALSE]);
100 $query = new Api4SelectQuery($api);
19b53e5b
C
101 $query->select[] = 'id';
102 $query->select[] = 'first_name';
103 $query->select[] = 'phones.phone';
104 $query->where[] = ['first_name', '=', 'Phoney'];
105 $results = $query->run();
106 $result = array_pop($results);
107
108 $this->assertEquals('Phoney', $result['first_name']);
109 $this->assertCount(2, $result['phones']);
110 }
111
112}