Merge pull request #4882 from colemanw/CRM-15789
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / QueryTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4
5 /**
6 * Include dataProvider for tests
7 */
8 class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
9
10 /**
11 * @return CRM_Contact_BAO_QueryTestDataProvider
12 */
13 public function dataProvider() {
14 return new CRM_Contact_BAO_QueryTestDataProvider;
15 }
16
17 public function setUp() {
18 parent::setUp();
19 }
20
21 public function tearDown() {
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()
35 * @dataProvider dataProvider
36 */
37 public function testSearch($fv, $count, $ids, $full) {
38 $op = new PHPUnit_Extensions_Database_Operation_Insert();
39 $op->execute($this->_dbconn,
40 $this->createFlatXMLDataSet(
41 dirname(__FILE__) . '/queryDataset.xml'
42 )
43 );
44
45 $params = CRM_Contact_BAO_Query::convertFormValues($fv);
46 $obj = new CRM_Contact_BAO_Query($params);
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
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 }
63
64 /**
65 * Check that we get a successful result querying for home address
66 * CRM-14263 search builder failure with search profile & address in criteria
67 */
68 public function testSearchProfileHomeCityCRM14263() {
69 $contactID = $this->individualCreate();
70 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
71 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1));
72 $params = array(
73 0 => array(
74 0 => 'city-1',
75 1 => '=',
76 2 => 'Cool City',
77 3 => 1,
78 4 => 0,
79 ),
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 {
89 $resultDAO = $queryObj->searchQuery(0, 0, NULL,
90 FALSE, FALSE,
91 FALSE, FALSE,
92 FALSE);
93 $this->assertTrue($resultDAO->fetch());
94 }
95 catch (PEAR_Exception $e) {
96 $err = $e->getCause();
97 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
98
99 }
100 }
101
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 */
106 public function testSearchProfileHomeCityNoResultsCRM14263() {
107 $contactID = $this->individualCreate();
108 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
109 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1));
110 $params = array(
111 0 => array(
112 0 => 'city-1',
113 1 => '=',
114 2 => 'Dumb City',
115 3 => 1,
116 4 => 0,
117 ),
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 }
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 */
144 public function testSearchProfilePrimaryCityCRM14263()
145 {
146 $contactID = $this->individualCreate();
147 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
148 $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1));
149 $params = array(
150 0 => array(
151 0 => 'city',
152 1 => '=',
153 2 => 'Cool City',
154 3 => 1,
155 4 => 0,
156 ),
157 );
158 $returnProperties = array(
159 'contact_type' => 1,
160 'contact_sub_type' => 1,
161 'sort_name' => 1,
162 );
163 $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 ";
164 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
165 try {
166 $this->assertEquals($expectedSQL, $queryObj->searchQuery(0, 0, NULL,
167 FALSE, FALSE,
168 FALSE, FALSE,
169 TRUE));
170 }
171 catch (PEAR_Exception $e) {
172 $err = $e->getCause();
173 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
174
175 }
176 }
177 }