INFRA-132 - FunctionDeclarationArgumentSpacing.SpaceBeforeEquals
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / QueryTest.php
CommitLineData
6a488035
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
3require_once 'CiviTest/Contact.php';
4
5/**
6 * Include dataProvider for tests
7 */
8class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
6a488035 9
e9479dcf
EM
10 /**
11 * @return CRM_Contact_BAO_QueryTestDataProvider
12 */
6a488035
TO
13 public function dataProvider() {
14 return new CRM_Contact_BAO_QueryTestDataProvider;
15 }
16
00be9182 17 public function setUp() {
6a488035
TO
18 parent::setUp();
19 }
20
00be9182 21 public function tearDown() {
6a488035
TO
22 $tablesToTruncate = array(
23 'civicrm_group_contact',
24 'civicrm_group',
25 'civicrm_saved_search',
26 'civicrm_entity_tag',
27 'civicrm_tag',
28 'civicrm_contact',
29 );
30 $this->quickCleanup($tablesToTruncate);
31 }
32
33 /**
34 * Test CRM_Contact_BAO_Query::searchQuery()
6c6e6187 35 * @dataProvider dataProvider
6a488035 36 */
00be9182 37 public function testSearch($fv, $count, $ids, $full) {
6a488035
TO
38 $op = new PHPUnit_Extensions_Database_Operation_Insert();
39 $op->execute($this->_dbconn,
bbfd46a5 40 $this->createFlatXMLDataSet(
6a488035
TO
41 dirname(__FILE__) . '/queryDataset.xml'
42 )
43 );
44
45 $params = CRM_Contact_BAO_Query::convertFormValues($fv);
46 $obj = new CRM_Contact_BAO_Query($params);
b81f44dd 47
48 // let's set useGroupBy=true since we are listing contacts here who might belong to
49 // more than one group / tag / notes etc.
50 $obj->_useGroupBy = TRUE;
51
6a488035
TO
52 $dao = $obj->searchQuery();
53
54 $contacts = array();
55 while ($dao->fetch()) {
56 $contacts[] = $dao->contact_id;
57 }
58
59 sort($contacts, SORT_NUMERIC);
60
61 $this->assertEquals($ids, $contacts, 'In line ' . __LINE__);
62 }
e5fccefb
EM
63
64 /**
55eb4e22 65 * Check that we get a successful result querying for home address
e5fccefb
EM
66 * CRM-14263 search builder failure with search profile & address in criteria
67 */
00be9182 68 public function testSearchProfileHomeCityCRM14263() {
e5fccefb
EM
69 $contactID = $this->individualCreate();
70 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
6c6e6187 71 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1));
e5fccefb
EM
72 $params = array(
73 0 => array(
74 0 => 'city-1',
75 1 => '=',
76 2 => 'Cool City',
77 3 => 1,
78 4 => 0,
21dfd5f5 79 ),
e5fccefb
EM
80 );
81 $returnProperties = array(
82 'contact_type' => 1,
83 'contact_sub_type' => 1,
84 'sort_name' => 1,
85 );
86
87 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
88 try {
55eb4e22 89 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
e5fccefb
EM
90 FALSE, FALSE,
91 FALSE, FALSE,
92 FALSE);
55eb4e22 93 $this->assertTrue($resultDAO->fetch());
e5fccefb 94 }
55eb4e22
EM
95 catch (PEAR_Exception $e) {
96 $err = $e->getCause();
97 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
e5fccefb 98
55eb4e22 99 }
e5fccefb
EM
100 }
101
55eb4e22
EM
102 /**
103 * Check that we get a successful result querying for home address
104 * CRM-14263 search builder failure with search profile & address in criteria
105 */
00be9182 106 public function testSearchProfileHomeCityNoResultsCRM14263() {
55eb4e22
EM
107 $contactID = $this->individualCreate();
108 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
6c6e6187 109 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1));
55eb4e22
EM
110 $params = array(
111 0 => array(
112 0 => 'city-1',
113 1 => '=',
114 2 => 'Dumb City',
115 3 => 1,
116 4 => 0,
21dfd5f5 117 ),
55eb4e22
EM
118 );
119 $returnProperties = array(
120 'contact_type' => 1,
121 'contact_sub_type' => 1,
122 'sort_name' => 1,
123 );
124
125 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
126 try {
127 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
128 FALSE, FALSE,
129 FALSE, FALSE,
130 FALSE);
131 $this->assertFalse($resultDAO->fetch());
132 }
133 catch (PEAR_Exception $e) {
134 $err = $e->getCause();
135 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
136
137 }
138 }
6c6e6187
TO
139 /**
140 * CRM-14263 search builder failure with search profile & address in criteria
141 * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has
142 * so few tests detecting any change seems good here :-)
143 */
6ea503d4 144 public function testSearchProfilePrimaryCityCRM14263() {
6c6e6187
TO
145 $contactID = $this->individualCreate();
146 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
147 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1));
148 $params = array(
55eb4e22
EM
149 0 => array(
150 0 => 'city',
151 1 => '=',
152 2 => 'Cool City',
153 3 => 1,
154 4 => 0,
21dfd5f5 155 ),
55eb4e22 156 );
6c6e6187 157 $returnProperties = array(
55eb4e22
EM
158 'contact_type' => 1,
159 'contact_sub_type' => 1,
160 'sort_name' => 1,
161 );
6c6e6187
TO
162 $expectedSQL = "SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, civicrm_address.id as address_id, civicrm_address.city as `city` FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) WHERE ( ( LOWER(civicrm_address.city) = 'cool city' ) ) AND (contact_a.is_deleted = 0) ORDER BY contact_a.sort_name asc, contact_a.id ";
163 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
164 try {
165 $this->assertEquals($expectedSQL, $queryObj->searchQuery(0, 0, NULL,
55eb4e22
EM
166 FALSE, FALSE,
167 FALSE, FALSE,
168 TRUE));
6c6e6187
TO
169 }
170 catch (PEAR_Exception $e) {
171 $err = $e->getCause();
172 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
55eb4e22 173
55eb4e22 174 }
6c6e6187 175 }
6a488035 176}