3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test APIv3 ability to join across multiple entities
15 * @package CiviCRM_APIv3
18 class api_v3_SelectQueryTest
extends CiviUnitTestCase
{
21 private $hookCondition = [];
23 public function setUp() {
25 $this->useTransaction(TRUE);
26 CRM_Utils_Hook
::singleton()->setHook('civicrm_selectWhereClause', [$this, 'hook_civicrm_selectWhereClause']);
29 public function testHookPhoneClause() {
30 $person1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester']);
31 $cid = $person1['id'];
32 for ($number = 1; $number < 6; ++
$number) {
33 $this->callAPISuccess('Phone', 'create', [
38 $this->hookEntity
= 'Phone';
39 $this->hookCondition
= [
42 $phone = $this->callAPISuccessGetSingle('Phone', ['contact_id' => $cid, 'check_permissions' => 1]);
43 $this->assertEquals(3, $phone['phone']);
46 public function testHookContactClause() {
47 $person1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester', 'email' => 'bob@test.er']);
48 $person2 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Tom', 'last_name' => 'Tester', 'email' => 'tom@test.er']);
49 $person3 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Tim', 'last_name' => 'Tester', 'email' => 'tim@test.er']);
50 $this->hookEntity
= 'Contact';
51 $this->hookCondition
= ['id' => ['= ' . $person2['id']]];
52 $email = $this->callAPISuccessGetSingle('Email', ['check_permissions' => 1]);
53 $this->assertEquals($person2['id'], $email['contact_id']);
57 * Implements hook_civicrm_selectWhereClause().
59 public function hook_civicrm_selectWhereClause($entity, &$clauses) {
60 if ($entity == $this->hookEntity
) {
61 foreach ($this->hookCondition
as $field => $clause) {
62 $clauses[$field] = array_merge(CRM_Utils_Array
::value($field, $clauses, []), $clause);