Merge pull request #14367 from MegaphoneJon/financial-58
[civicrm-core.git] / tests / phpunit / api / v3 / SelectQueryTest.php
CommitLineData
2cfe873c
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
2cfe873c 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
2cfe873c
CW
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
2cfe873c
CW
28/**
29 * Test APIv3 ability to join across multiple entities
30 *
31 * @package CiviCRM_APIv3
acb109b7 32 * @group headless
2cfe873c
CW
33 */
34class api_v3_SelectQueryTest extends CiviUnitTestCase {
35
36 private $hookEntity;
9099cab3 37 private $hookCondition = [];
2cfe873c
CW
38
39 public function setUp() {
40 parent::setUp();
41 $this->useTransaction(TRUE);
9099cab3 42 CRM_Utils_Hook::singleton()->setHook('civicrm_selectWhereClause', [$this, 'hook_civicrm_selectWhereClause']);
2cfe873c
CW
43 }
44
45 public function testHookPhoneClause() {
9099cab3 46 $person1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester']);
2cfe873c
CW
47 $cid = $person1['id'];
48 for ($number = 1; $number < 6; ++$number) {
9099cab3 49 $this->callAPISuccess('Phone', 'create', [
2cfe873c
CW
50 'contact_id' => $cid,
51 'phone' => $number,
9099cab3 52 ]);
2cfe873c
CW
53 }
54 $this->hookEntity = 'Phone';
9099cab3
CW
55 $this->hookCondition = [
56 'phone' => ['= 3'],
57 ];
58 $phone = $this->callAPISuccessGetSingle('Phone', ['contact_id' => $cid, 'check_permissions' => 1]);
2cfe873c
CW
59 $this->assertEquals(3, $phone['phone']);
60 }
61
62 public function testHookContactClause() {
9099cab3
CW
63 $person1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester', 'email' => 'bob@test.er']);
64 $person2 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Tom', 'last_name' => 'Tester', 'email' => 'tom@test.er']);
65 $person3 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Tim', 'last_name' => 'Tester', 'email' => 'tim@test.er']);
2cfe873c 66 $this->hookEntity = 'Contact';
9099cab3
CW
67 $this->hookCondition = ['id' => ['= ' . $person2['id']]];
68 $email = $this->callAPISuccessGetSingle('Email', ['check_permissions' => 1]);
2cfe873c
CW
69 $this->assertEquals($person2['id'], $email['contact_id']);
70 }
71
72 /**
73 * Implements hook_civicrm_selectWhereClause().
74 */
75 public function hook_civicrm_selectWhereClause($entity, &$clauses) {
76 if ($entity == $this->hookEntity) {
77 foreach ($this->hookCondition as $field => $clause) {
9099cab3 78 $clauses[$field] = array_merge(CRM_Utils_Array::value($field, $clauses, []), $clause);
2cfe873c
CW
79 }
80 }
81 }
82
83}