a866558c043703f9dc069ca89ec0d1eb404249db
[civicrm-core.git] / tests / phpunit / api / v4 / Query / Api4SelectQueryTest.php
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 Api4SelectQueryTest 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('DefaultDataSet');
26 $displayNameFormat = '{contact.first_name}{ }{contact.last_name}';
27 \Civi::settings()->set('display_name_format', $displayNameFormat);
28
29 return parent::setUpHeadless();
30 }
31
32 public function testWithSingleWhereJoin() {
33 $phoneNum = $this->getReference('test_phone_1')['phone'];
34
35 $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name'));
36 $query->where[] = ['phones.phone', '=', $phoneNum];
37 $results = $query->run();
38
39 $this->assertCount(1, $results);
40 }
41
42 public function testOneToManyJoin() {
43 $phoneNum = $this->getReference('test_phone_1')['phone'];
44
45 $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name'));
46 $query->select[] = 'id';
47 $query->select[] = 'first_name';
48 $query->select[] = 'phones.phone';
49 $query->where[] = ['phones.phone', '=', $phoneNum];
50 $results = $query->run();
51
52 $this->assertCount(1, $results);
53 $firstResult = array_shift($results);
54 $this->assertArrayHasKey('phones', $firstResult);
55 $firstPhone = array_shift($firstResult['phones']);
56 $this->assertEquals($phoneNum, $firstPhone['phone']);
57 }
58
59 public function testManyToOneJoin() {
60 $phoneNum = $this->getReference('test_phone_1')['phone'];
61 $contact = $this->getReference('test_contact_1');
62
63 $query = new Api4SelectQuery('Phone', FALSE, civicrm_api4('Phone', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name'));
64 $query->select[] = 'id';
65 $query->select[] = 'phone';
66 $query->select[] = 'contact.display_name';
67 $query->select[] = 'contact.first_name';
68 $query->where[] = ['phone', '=', $phoneNum];
69 $results = $query->run();
70
71 $this->assertCount(1, $results);
72 $firstResult = array_shift($results);
73 $this->assertEquals($contact['display_name'], $firstResult['contact.display_name']);
74 }
75
76 public function testOneToManyMultipleJoin() {
77 $query = new Api4SelectQuery('Contact', FALSE, civicrm_api4('Contact', 'getFields', ['includeCustom' => FALSE, 'checkPermissions' => FALSE, 'action' => 'get'], 'name'));
78 $query->select[] = 'id';
79 $query->select[] = 'first_name';
80 $query->select[] = 'phones.phone';
81 $query->where[] = ['first_name', '=', 'Phoney'];
82 $results = $query->run();
83 $result = array_pop($results);
84
85 $this->assertEquals('Phoney', $result['first_name']);
86 $this->assertCount(2, $result['phones']);
87 }
88
89 }