Merge pull request #7672 from eileenmcnaughton/index
[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-2015 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test APIv3 ability to join across multiple entities
32 *
33 * @package CiviCRM_APIv3
34 */
35 class api_v3_SelectQueryTest extends CiviUnitTestCase {
36
37 private $hookEntity;
38 private $hookCondition = array();
39
40 public function setUp() {
41 parent::setUp();
42 $this->useTransaction(TRUE);
43 CRM_Utils_Hook::singleton()->setHook('civicrm_selectWhereClause', array($this, 'hook_civicrm_selectWhereClause'));
44 }
45
46 public function testHookPhoneClause() {
47 $person1 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester'));
48 $cid = $person1['id'];
49 for ($number = 1; $number < 6; ++$number) {
50 $this->callAPISuccess('Phone', 'create', array(
51 'contact_id' => $cid,
52 'phone' => $number,
53 ));
54 }
55 $this->hookEntity = 'Phone';
56 $this->hookCondition = array(
57 'phone' => array('= 3'),
58 );
59 $phone = $this->callAPISuccessGetSingle('Phone', array('contact_id' => $cid, 'check_permissions' => 1));
60 $this->assertEquals(3, $phone['phone']);
61 }
62
63 public function testHookContactClause() {
64 $person1 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Bob', 'last_name' => 'Tester', 'email' => 'bob@test.er'));
65 $person2 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Tom', 'last_name' => 'Tester', 'email' => 'tom@test.er'));
66 $person3 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'Tim', 'last_name' => 'Tester', 'email' => 'tim@test.er'));
67 $this->hookEntity = 'Contact';
68 $this->hookCondition = array('id' => array('= ' . $person2['id']));
69 $email = $this->callAPISuccessGetSingle('Email', array('check_permissions' => 1));
70 $this->assertEquals($person2['id'], $email['contact_id']);
71 }
72
73 /**
74 * Implements hook_civicrm_selectWhereClause().
75 */
76 public function hook_civicrm_selectWhereClause($entity, &$clauses) {
77 if ($entity == $this->hookEntity) {
78 foreach ($this->hookCondition as $field => $clause) {
79 $clauses[$field] = array_merge(CRM_Utils_Array::value($field, $clauses, array()), $clause);
80 }
81 }
82 }
83
84 }