3 namespace api\v
4\Query
;
5 use Civi\Api4\Query\Api4SelectQuery
;
6 use api\v
4\UnitTestCase
;
11 class Api4SelectQueryTest
extends UnitTestCase
{
13 public function setUpHeadless() {
22 'civicrm_activity_contact',
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);
29 return parent
::setUpHeadless();
32 public function testWithSingleWhereJoin() {
33 $phoneNum = $this->getReference('test_phone_1')['phone'];
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();
39 $this->assertCount(1, $results);
42 public function testOneToManyJoin() {
43 $phoneNum = $this->getReference('test_phone_1')['phone'];
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();
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']);
59 public function testManyToOneJoin() {
60 $phoneNum = $this->getReference('test_phone_1')['phone'];
61 $contact = $this->getReference('test_contact_1');
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();
71 $this->assertCount(1, $results);
72 $firstResult = array_shift($results);
73 $this->assertEquals($contact['display_name'], $firstResult['contact.display_name']);
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);
85 $this->assertEquals('Phoney', $result['first_name']);
86 $this->assertCount(2, $result['phones']);