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