Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / SelectQueryTest.php
CommitLineData
2cfe873c
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
2cfe873c 5 | |
7d61e75f
TO
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 |
2cfe873c
CW
9 +--------------------------------------------------------------------+
10 */
11
2cfe873c
CW
12/**
13 * Test APIv3 ability to join across multiple entities
14 *
15 * @package CiviCRM_APIv3
acb109b7 16 * @group headless
2cfe873c
CW
17 */
18class api_v3_SelectQueryTest extends CiviUnitTestCase {
19
20 private $hookEntity;
9099cab3 21 private $hookCondition = [];
2cfe873c
CW
22
23 public function setUp() {
24 parent::setUp();
25 $this->useTransaction(TRUE);
9099cab3 26 CRM_Utils_Hook::singleton()->setHook('civicrm_selectWhereClause', [$this, 'hook_civicrm_selectWhereClause']);
2cfe873c
CW
27 }
28
29 public function testHookPhoneClause() {
9099cab3 30 $person1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester']);
2cfe873c
CW
31 $cid = $person1['id'];
32 for ($number = 1; $number < 6; ++$number) {
9099cab3 33 $this->callAPISuccess('Phone', 'create', [
2cfe873c
CW
34 'contact_id' => $cid,
35 'phone' => $number,
9099cab3 36 ]);
2cfe873c
CW
37 }
38 $this->hookEntity = 'Phone';
9099cab3
CW
39 $this->hookCondition = [
40 'phone' => ['= 3'],
41 ];
42 $phone = $this->callAPISuccessGetSingle('Phone', ['contact_id' => $cid, 'check_permissions' => 1]);
2cfe873c
CW
43 $this->assertEquals(3, $phone['phone']);
44 }
45
46 public function testHookContactClause() {
9099cab3
CW
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']);
2cfe873c 50 $this->hookEntity = 'Contact';
9099cab3
CW
51 $this->hookCondition = ['id' => ['= ' . $person2['id']]];
52 $email = $this->callAPISuccessGetSingle('Email', ['check_permissions' => 1]);
2cfe873c
CW
53 $this->assertEquals($person2['id'], $email['contact_id']);
54 }
55
56 /**
57 * Implements hook_civicrm_selectWhereClause().
58 */
59 public function hook_civicrm_selectWhereClause($entity, &$clauses) {
60 if ($entity == $this->hookEntity) {
61 foreach ($this->hookCondition as $field => $clause) {
9099cab3 62 $clauses[$field] = array_merge(CRM_Utils_Array::value($field, $clauses, []), $clause);
2cfe873c
CW
63 }
64 }
65 }
66
67}