Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / SelectQueryTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 ability to join across multiple entities
14 *
15 * @package CiviCRM_APIv3
16 * @group headless
17 */
18 class api_v3_SelectQueryTest extends CiviUnitTestCase {
19
20 private $hookEntity;
21 private $hookCondition = [];
22
23 public function setUp() {
24 parent::setUp();
25 $this->useTransaction(TRUE);
26 CRM_Utils_Hook::singleton()->setHook('civicrm_selectWhereClause', [$this, 'hook_civicrm_selectWhereClause']);
27 }
28
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', [
34 'contact_id' => $cid,
35 'phone' => $number,
36 ]);
37 }
38 $this->hookEntity = 'Phone';
39 $this->hookCondition = [
40 'phone' => ['= 3'],
41 ];
42 $phone = $this->callAPISuccessGetSingle('Phone', ['contact_id' => $cid, 'check_permissions' => 1]);
43 $this->assertEquals(3, $phone['phone']);
44 }
45
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']);
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) {
62 $clauses[$field] = array_merge(CRM_Utils_Array::value($field, $clauses, []), $clause);
63 }
64 }
65 }
66
67 }