1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
<?php
namespace Civi\Test\Api4\Query;
use Civi\Api4\Query\Api4SelectQuery;
use Civi\Test\Api4\UnitTestCase;
/**
* @group headless
*/
class Api4SelectQueryTest extends UnitTestCase {
public function setUpHeadless() {
$relatedTables = [
'civicrm_address',
'civicrm_email',
'civicrm_phone',
'civicrm_openid',
'civicrm_im',
'civicrm_website',
'civicrm_activity',
'civicrm_activity_contact',
];
$this->cleanup(['tablesToTruncate' => $relatedTables]);
$this->loadDataSet('DefaultDataSet');
$displayNameFormat = '{contact.first_name}{ }{contact.last_name}';
\Civi::settings()->set('display_name_format', $displayNameFormat);
return parent::setUpHeadless();
}
public function testWithSingleWhereJoin() {
$phoneNum = $this->getReference('test_phone_1')['phone'];
$query = new Api4SelectQuery('Contact', FALSE);
$query->where[] = ['phones.phone', '=', $phoneNum];
$results = $query->run();
$this->assertCount(1, $results);
}
public function testOneToManyJoin() {
$phoneNum = $this->getReference('test_phone_1')['phone'];
$query = new Api4SelectQuery('Contact', FALSE);
$query->select[] = 'id';
$query->select[] = 'first_name';
$query->select[] = 'phones.phone';
$query->where[] = ['phones.phone', '=', $phoneNum];
$results = $query->run();
$this->assertCount(1, $results);
$firstResult = array_shift($results);
$this->assertArrayHasKey('phones', $firstResult);
$firstPhone = array_shift($firstResult['phones']);
$this->assertEquals($phoneNum, $firstPhone['phone']);
}
public function testManyToOneJoin() {
$phoneNum = $this->getReference('test_phone_1')['phone'];
$contact = $this->getReference('test_contact_1');
$query = new Api4SelectQuery('Phone', FALSE);
$query->select[] = 'id';
$query->select[] = 'phone';
$query->select[] = 'contact.display_name';
$query->select[] = 'contact.first_name';
$query->where[] = ['phone', '=', $phoneNum];
$results = $query->run();
$this->assertCount(1, $results);
$firstResult = array_shift($results);
$this->assertArrayHasKey('contact', $firstResult);
$resultContact = $firstResult['contact'];
$this->assertEquals($contact['display_name'], $resultContact['display_name']);
}
public function testOneToManyMultipleJoin() {
$query = new Api4SelectQuery('Contact', FALSE);
$query->select[] = 'id';
$query->select[] = 'first_name';
$query->select[] = 'phones.phone';
$query->where[] = ['first_name', '=', 'Phoney'];
$results = $query->run();
$result = array_pop($results);
$this->assertEquals('Phoney', $result['first_name']);
$this->assertCount(2, $result['phones']);
}
}
|