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