INFRA-132 - tests/ - PHPStorm cleanup
[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);
92915c55 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
92915c55 52 $dao = $obj->searchQuery();
6a488035
TO
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;
92915c55
TO
71 $this->callAPISuccess('address', 'create', array(
72 'contact_id' => $contactID,
73 'city' => 'Cool City',
74 'location_type_id' => 1
75 ));
e5fccefb
EM
76 $params = array(
77 0 => array(
78 0 => 'city-1',
79 1 => '=',
80 2 => 'Cool City',
81 3 => 1,
82 4 => 0,
21dfd5f5 83 ),
e5fccefb
EM
84 );
85 $returnProperties = array(
86 'contact_type' => 1,
87 'contact_sub_type' => 1,
88 'sort_name' => 1,
89 );
90
91 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
92 try {
55eb4e22 93 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
e5fccefb
EM
94 FALSE, FALSE,
95 FALSE, FALSE,
96 FALSE);
55eb4e22 97 $this->assertTrue($resultDAO->fetch());
e5fccefb 98 }
55eb4e22
EM
99 catch (PEAR_Exception $e) {
100 $err = $e->getCause();
101 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
e5fccefb 102
55eb4e22 103 }
e5fccefb
EM
104 }
105
55eb4e22
EM
106 /**
107 * Check that we get a successful result querying for home address
108 * CRM-14263 search builder failure with search profile & address in criteria
109 */
00be9182 110 public function testSearchProfileHomeCityNoResultsCRM14263() {
55eb4e22
EM
111 $contactID = $this->individualCreate();
112 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
92915c55
TO
113 $this->callAPISuccess('address', 'create', array(
114 'contact_id' => $contactID,
115 'city' => 'Cool City',
116 'location_type_id' => 1
117 ));
55eb4e22
EM
118 $params = array(
119 0 => array(
120 0 => 'city-1',
121 1 => '=',
122 2 => 'Dumb City',
123 3 => 1,
124 4 => 0,
21dfd5f5 125 ),
55eb4e22
EM
126 );
127 $returnProperties = array(
128 'contact_type' => 1,
129 'contact_sub_type' => 1,
130 'sort_name' => 1,
131 );
132
133 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
134 try {
135 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
136 FALSE, FALSE,
137 FALSE, FALSE,
138 FALSE);
139 $this->assertFalse($resultDAO->fetch());
140 }
141 catch (PEAR_Exception $e) {
142 $err = $e->getCause();
143 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
144
145 }
146 }
92915c55 147
6c6e6187
TO
148 /**
149 * CRM-14263 search builder failure with search profile & address in criteria
150 * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has
151 * so few tests detecting any change seems good here :-)
152 */
6ea503d4 153 public function testSearchProfilePrimaryCityCRM14263() {
6c6e6187
TO
154 $contactID = $this->individualCreate();
155 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
92915c55
TO
156 $this->callAPISuccess('address', 'create', array(
157 'contact_id' => $contactID,
158 'city' => 'Cool City',
159 'location_type_id' => 1
160 ));
6c6e6187 161 $params = array(
92915c55
TO
162 0 => array(
163 0 => 'city',
164 1 => '=',
165 2 => 'Cool City',
166 3 => 1,
167 4 => 0,
168 ),
169 );
6c6e6187 170 $returnProperties = array(
92915c55
TO
171 'contact_type' => 1,
172 'contact_sub_type' => 1,
173 'sort_name' => 1,
174 );
6c6e6187
TO
175 $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 ";
176 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
177 try {
178 $this->assertEquals($expectedSQL, $queryObj->searchQuery(0, 0, NULL,
92915c55
TO
179 FALSE, FALSE,
180 FALSE, FALSE,
181 TRUE));
6c6e6187
TO
182 }
183 catch (PEAR_Exception $e) {
184 $err = $e->getCause();
185 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
55eb4e22 186
55eb4e22 187 }
6c6e6187 188 }
6a488035 189}