Merge pull request #10393 from JMAConsulting/exportBatch
[civicrm-core.git] / tests / phpunit / api / v3 / SelectQueryTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
28 /**
29 * Test APIv3 ability to join across multiple entities
30 *
31 * @package CiviCRM_APIv3
32 * @group headless
33 */
34 class api_v3_SelectQueryTest extends CiviUnitTestCase {
35
36 private $hookEntity;
37 private $hookCondition = array();
38
39 public function setUp() {
40 parent::setUp();
41 $this->useTransaction(TRUE);
42 CRM_Utils_Hook::singleton()->setHook('civicrm_selectWhereClause', array($this, 'hook_civicrm_selectWhereClause'));
43 }
44
45 public function testHookPhoneClause() {
46 $person1 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester'));
47 $cid = $person1['id'];
48 for ($number = 1; $number < 6; ++$number) {
49 $this->callAPISuccess('Phone', 'create', array(
50 'contact_id' => $cid,
51 'phone' => $number,
52 ));
53 }
54 $this->hookEntity = 'Phone';
55 $this->hookCondition = array(
56 'phone' => array('= 3'),
57 );
58 $phone = $this->callAPISuccessGetSingle('Phone', array('contact_id' => $cid, 'check_permissions' => 1));
59 $this->assertEquals(3, $phone['phone']);
60 }
61
62 public function testHookContactClause() {
63 $person1 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester', 'email' => 'bob@test.er'));
64 $person2 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Tom', 'last_name' => 'Tester', 'email' => 'tom@test.er'));
65 $person3 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Tim', 'last_name' => 'Tester', 'email' => 'tim@test.er'));
66 $this->hookEntity = 'Contact';
67 $this->hookCondition = array('id' => array('= ' . $person2['id']));
68 $email = $this->callAPISuccessGetSingle('Email', array('check_permissions' => 1));
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) {
78 $clauses[$field] = array_merge(CRM_Utils_Array::value($field, $clauses, array()), $clause);
79 }
80 }
81 }
82
83 }