commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tests / phpunit / WebTest / Contact / SearchBuilderTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
28
29 /**
30 * Class WebTest_Contact_SearchBuilderTest
31 */
32 class WebTest_Contact_SearchBuilderTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testSearchBuilderOptions() {
39 $this->webtestLogin();
40
41 $groupName = $this->WebtestAddGroup();
42
43 // Open the search builder
44 $this->openCiviPage('contact/search/builder', 'reset=1');
45
46 $this->enterValues(1, 1, 'Contacts', 'Group(s)', NULL, '=', array($groupName));
47 $this->enterValues(1, 2, 'Contacts', 'Country', NULL, '=', array('United States'));
48 $this->enterValues(1, 3, 'Individual', 'Gender', NULL, '=', array('Male'));
49 $this->click('_qf_Builder_refresh');
50 $this->waitForPageToLoad();
51
52 // We should get no results. But check the options are all still set
53 $this->waitForTextPresent('No matches found for:');
54 foreach (array($groupName, 'United States', 'Male') as $i => $label) {
55 $this->waitForElementPresent("//span[@id='crm_search_value_1_$i']/select/option[2]");
56 $this->assertSelectedLabel("//span[@id='crm_search_value_1_$i']/select", $label);
57 }
58 }
59
60 public function testSearchBuilderRLIKE() {
61 $this->webtestLogin();
62
63 // Adding contact
64 // We're using Quick Add block on the main page for this.
65 $firstName = substr(sha1(rand()), 0, 7);
66 $this->createDetailContact($firstName);
67
68 $sortName = "adv$firstName, $firstName";
69 $displayName = "$firstName adv$firstName";
70
71 $this->_searchBuilder("Postal Code", "100[0-9]", $sortName, "RLIKE");
72 }
73
74 /**
75 * function to create contact with details (contact details, address, Constituent information ...)
76 * @param null $firstName
77 */
78 public function createDetailContact($firstName = NULL) {
79
80 if (!$firstName) {
81 $firstName = substr(sha1(rand()), 0, 7);
82 }
83
84 // create contact type Individual with subtype
85 // with most of values to required to search
86 $Subtype = "Student";
87 $this->openCiviPage('contact/add', array('reset' => 1, 'ct' => 'Individual'), '_qf_Contact_cancel');
88
89 // --- fill few values in Contact Detail block
90 $this->type("first_name", "$firstName");
91 $this->type("middle_name", "mid$firstName");
92 $this->type("last_name", "adv$firstName");
93 $this->select("contact_sub_type", "label=$Subtype");
94 $this->type("email_1_email", "$firstName@advsearch.co.in");
95 $this->type("phone_1_phone", "123456789");
96 $this->type("external_identifier", "extid$firstName");
97
98 // --- fill few values in address
99 $this->click("//form[@id='Contact']/div[2]/div[4]/div[1]");
100 $this->waitForElementPresent("address_1_geo_code_2");
101 $this->type("address_1_street_address", "street 1 $firstName");
102 $this->type("address_1_supplemental_address_1", "street supplement 1 $firstName");
103 $this->type("address_1_supplemental_address_2", "street supplement 2 $firstName");
104 $this->type("address_1_city", "city$firstName");
105 $this->type("address_1_postal_code", "100100");
106 $this->select("address_1_country_id", "United States");
107 $this->select("address_1_state_province_id", "Alaska");
108
109 // save contact
110 $this->click("_qf_Contact_upload_view");
111 $this->waitForPageToLoad($this->getTimeoutMsec());
112 $this->assertTrue($this->isTextPresent("$firstName adv$firstName"));
113 }
114
115 public function testSearchBuilderContacts() {
116 $this->webtestLogin();
117
118 //Individual
119 $firstName = substr(sha1(rand()), 0, 7);
120 $streetName = "street $firstName";
121 $sortName = "adv$firstName, $firstName";
122 $this->_createContact('Individual', $firstName, "$firstName@advsearch.co.in", $streetName);
123 // search using search builder and advanced search
124 $this->_searchBuilder('Street Address', $streetName, $sortName, '=', '1');
125 $this->_advancedSearch($streetName, $sortName, 'Individual', '1', 'street_address');
126
127 //Organization
128 $orgName = substr(sha1(rand()), 0, 7) . "org";
129 $orgEmail = "ab" . rand() . "@{$orgName}.com";
130 $this->_createContact('Organization', $orgName, $orgEmail, "street $orgName");
131 // search using search builder and advanced search
132 $this->_searchBuilder('Email', $orgEmail, $orgName, '=', '1');
133 $this->_advancedSearch($orgEmail, $orgName, 'Organization', '1', 'email');
134
135 //Household
136 $householdName = "household" . substr(sha1(rand()), 0, 7);
137 $householdEmail = "h1" . rand() . "@{$householdName}.com";
138 $this->_createContact('Household', $householdName, $householdEmail, "street $householdName");
139 // search using search builder and advanced search
140 $this->_searchBuilder('Email', $householdEmail, $householdName, '=', '1');
141 $this->_advancedSearch($householdEmail, $householdName, 'Household', '1', 'email');
142
143 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
144
145 // searching contacts whose email is not set
146 $firstName1 = "00a1" . substr(sha1(rand()), 0, 7);
147 $this->type("first_name", $firstName1);
148 $this->type("last_name", "01adv$firstName1");
149 // save contact
150 $this->click("_qf_Contact_upload_view");
151 $this->waitForPageToLoad($this->getTimeoutMsec());
152 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
153
154 $firstName2 = "00a2" . substr(sha1(rand()), 0, 7);
155 $this->type("first_name", $firstName2);
156 $this->type("last_name", "02adv$firstName2");
157 // save contact
158 $this->click("_qf_Contact_upload_view");
159 $this->waitForPageToLoad($this->getTimeoutMsec());
160 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
161
162 $firstName3 = "00a3" . substr(sha1(rand()), 0, 7);
163 $this->type("first_name", $firstName3);
164 $this->type("last_name", "03adv$firstName3");
165 // save contact
166 $this->click("_qf_Contact_upload_view");
167 $this->waitForPageToLoad($this->getTimeoutMsec());
168 $this->_searchBuilder('Email', NULL, NULL, 'IS NULL');
169 $this->type('CRM_Contact_Form_Search_Builder-rows-per-page-select', '100');
170 $this->waitForElementPresent('CRM_Contact_Form_Search_Builder-rows-per-page-select');
171 $names = array(
172 1 => $firstName1,
173 2 => $firstName2,
174 3 => $firstName3,
175 );
176 foreach ($names as $key => $value) {
177 $this->assertTrue($this->isTextPresent($value));
178 }
179 //searching contacts whose phone field is empty
180 $this->_searchBuilder('Phone', NULL, NULL, 'IS EMPTY');
181 foreach ($names as $key => $value) {
182 $this->assertTrue($this->isTextPresent($value));
183 }
184 //searching contacts whose phone field is not empty
185 $this->_searchBuilder('Phone', NULL, $firstName, 'IS NOT EMPTY');
186 $this->type('CRM_Contact_Form_Search_Builder-rows-per-page-select', '100');
187 $this->waitForElementPresent('CRM_Contact_Form_Search_Builder-rows-per-page-select');
188 $this->assertTrue($this->isTextPresent($firstName));
189
190 $firstName4 = "AB" . substr(sha1(rand()), 0, 7);
191 $postalCode = rand();
192 $this->_createContact('Individual', $firstName4, "$firstName4@advsearch.co.in", NULL, $postalCode);
193 $firstName5 = "CD" . substr(sha1(rand()), 0, 7);
194 $this->_createContact('Individual', $firstName5, "$firstName5@advsearch.co.in", NULL, $postalCode);
195 $firstName6 = "EF" . substr(sha1(rand()), 0, 7);
196 $this->_createContact('Organization', $firstName6, "$firstName6@advsearch.co.in", NULL, $postalCode);
197 $firstName7 = "GH" . substr(sha1(rand()), 0, 7);
198 $this->_createContact('Household', $firstName7, "$firstName7@advsearch.co.in", NULL, $postalCode);
199
200 // check if the resultset of search builder and advanced search match for the postal code
201 $this->_searchBuilder('Postal Code', $postalCode, NULL, 'LIKE', '4');
202 $this->_advancedSearch($postalCode, NULL, NULL, '4', 'postal_code');
203
204 $firstName8 = "abcc" . substr(sha1(rand()), 0, 7);
205 $this->_createContact('Individual', $firstName8, "$firstName8@advsearch.co.in", NULL);
206 $this->_searchBuilder('Note(s): Body and Subject', "this is notes by $firstName8", $firstName8, 'LIKE');
207 $this->_searchBuilder('Note(s): Subject Only', "this is subject by $firstName8", $firstName8, 'LIKE');
208 $this->_searchBuilder('Note(s): Body Only', "this is notes by $firstName8", $firstName8, 'LIKE');
209 $this->_advancedSearch("this is notes by $firstName8", $firstName8, NULL, NULL, 'note_body', 'notes');
210 $this->_advancedSearch("this is subject by $firstName8", $firstName8, NULL, NULL, 'note_subject', 'notes');
211 $this->_advancedSearch("this is notes by $firstName8", $firstName8, NULL, NULL, 'note_both', 'notes');
212 $this->_advancedSearch("this is notes by $firstName8", $firstName8, NULL, NULL, 'note_both', 'notes');
213 }
214
215 /**
216 * @param $field
217 * @param null $fieldValue
218 * @param null $name
219 * @param string $op
220 * @param null $count
221 */
222 public function _searchBuilder($field, $fieldValue = NULL, $name = NULL, $op = '=', $count = NULL) {
223 // search builder using contacts(not using contactType)
224 $this->openCiviPage("contact/search/builder", "reset=1");
225 $this->enterValues(1, 1, 'Contacts', $field, NULL, $op, "$fieldValue");
226 $this->click("id=_qf_Builder_refresh");
227 $this->waitForPageToLoad($this->getTimeoutMsec());
228 if (($op == '=' || $op == 'LIKE') && $fieldValue) {
229 $this->assertElementContainsText('css=.crm-search-results > table.row-highlight', "$fieldValue");
230 }
231 if ($name) {
232 $this->assertElementContainsText('css=.crm-search-results > table.row-highlight', "$name");
233 }
234 if ($count) {
235 $this->assertElementContainsText('search-status', "$count Contact");
236 }
237 }
238
239 /**
240 * Enter form values in a Search Builder row.
241 * @param $set
242 * @param $row
243 * @param $entity
244 * @param $field
245 * @param $loc
246 * @param $op
247 * @param string $value
248 */
249 public function enterValues($set, $row, $entity, $field, $loc, $op, $value = '') {
250 if ($set > 1 && $row == 1) {
251 $this->click('addBlock');
252 }
253 if ($row > 1) {
254 $this->click("addMore_{$set}");
255 }
256 // In the DOM rows are 0 indexed and sets are 1 indexed, so normalizing
257 $row--;
258
259 $this->waitForElementPresent("mapper_{$set}_{$row}_0");
260 $this->select("mapper_{$set}_{$row}_0", "label=$entity");
261 $this->select("mapper_{$set}_{$row}_1", "label=$field");
262 if ($loc) {
263 $this->select("mapper_{$set}_{$row}_2", "label=$loc");
264 }
265 $this->select("operator_{$set}_{$row}", "value=$op");
266 if (is_array($value)) {
267 $this->waitForElementPresent("css=#crm_search_value_{$set}_{$row} select option + option");
268 foreach ($value as $val) {
269 if ($op != 'IN') {
270 $select = 'select';
271
272 }
273 else {
274 $select = 'addSelection';
275
276 }
277 $this->$select("css=#crm_search_value_{$set}_{$row} select", "label=$val");
278 }
279 }
280 elseif ($value && substr($value, 0, 5) == 'date:') {
281 $this->webtestFillDate("value_{$set}_{$row}", trim(substr($value, 5)));
282 }
283 elseif ($value) {
284 $this->type("value_{$set}_{$row}", $value);
285 }
286 }
287
288 /**
289 * @param null $fieldValue
290 * @param null $name
291 * @param null $contactType
292 * @param null $count
293 * @param $field
294 */
295 public function _advancedSearch($fieldValue = NULL, $name = NULL, $contactType = NULL, $count = NULL, $field) {
296 //advanced search by selecting the contactType
297 $this->openCiviPage("contact/search/advanced", "reset=1");
298 if (isset($contactType)) {
299 $this->select("id=contact_type", "value=$contactType");
300 }
301 if (substr($field, 0, 5) == 'note_') {
302 $this->click("notes");
303 $this->waitForElementPresent("xpath=//div[@id='notes-search']/table/tbody/tr/td[2]/input[3]");
304 if ($field == 'note_body') {
305 $this->click("CIVICRM_QFID_2_note_option");
306 }
307 elseif ($field == 'note_subject') {
308 $this->click("CIVICRM_QFID_3_note_option");
309 }
310 else {
311 $this->click("CIVICRM_QFID_6_note_option");
312 }
313 $this->type("note", $fieldValue);
314 }
315 else {
316 $this->click("location");
317 $this->waitForElementPresent("$field");
318 if ($contactType == 'Individual') {
319 $this->type("$field", $fieldValue);
320 }
321 else {
322 $this->type("$field", $fieldValue);
323 }
324 }
325 $this->click("_qf_Advanced_refresh");
326 $this->waitForPageToLoad($this->getTimeoutMsec());
327
328 //the search result should be same as the one that we got in search builder
329 if ($fieldValue) {
330 $this->assertElementContainsText('Advanced', "$fieldValue");
331 }
332 if ($name) {
333 $this->assertElementContainsText('css=.crm-search-results > table.row-highlight', "$name");
334 }
335 if ($count) {
336 $this->assertElementContainsText('search-status', "$count Contact");
337 }
338 }
339
340 /**
341 * @param $contactType
342 * @param string $name
343 * @param $email
344 * @param null $streetName
345 * @param null $postalCode
346 */
347 public function _createContact($contactType, $name, $email, $streetName = NULL, $postalCode = NULL) {
348 $this->openCiviPage('contact/add', array('reset' => 1, 'ct' => $contactType), '_qf_Contact_cancel');
349
350 if ($contactType == 'Individual') {
351 $this->type("first_name", "$name");
352 $this->type("last_name", "adv$name");
353 $name = "$name adv$name";
354 }
355 elseif ($contactType == 'Organization') {
356 $this->type("organization_name", $name);
357 }
358 else {
359 $this->type("household_name", $name);
360 }
361 $this->click("//form[@id='Contact']/div[2]/div[4]/div[1]");
362 $this->waitForElementPresent("address_1_geo_code_2");
363 $this->type("email_1_email", $email);
364 $this->type("phone_1_phone", "9876543210");
365 $this->type("address_1_street_address", $streetName);
366 $this->select("address_1_country_id", "United States");
367 $this->select("address_1_state_province_id", "Alaska");
368 $this->type("address_1_postal_code", $postalCode);
369
370 $this->click("//form[@id='Contact']/div[2]/div[6]/div[1]");
371 $this->waitForElementPresent("note");
372 $this->type("subject", "this is subject by $name");
373 $this->type("note", "this is notes by $name");
374
375 // save contact
376 $this->click("_qf_Contact_upload_view");
377 $this->waitForPageToLoad($this->getTimeoutMsec());
378 $this->assertTrue($this->isTextPresent("$name has been created."));
379 }
380
381 /**
382 * Webtest for CRM-12148
383 */
384 public function testSearchBuilderfinancialType() {
385 $this->webtestLogin();
386
387 // add financial type
388 $financialTypeName1 = 'Financial Type' . substr(sha1(rand()), 0, 5);;
389 $financialTypeName2 = 'Financial Type' . substr(sha1(rand()), 0, 5);;
390 $financialType = array(
391 'name' => $financialTypeName1,
392 'is_reserved' => FALSE,
393 'is_deductible' => FALSE,
394 );
395 $this->addeditFinancialType($financialType);
396 $financialType['name'] = $financialTypeName2;
397 $this->addeditFinancialType($financialType);
398 //create 6 contribution
399 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone", "_qf_Contribution_upload");
400 for ($i = 1; $i <= 6; $i++) {
401 if ($i % 2 == 0) {
402 $financialType = $financialTypeName1;
403 }
404 else {
405 $financialType = $financialTypeName2;
406 }
407 // create new contact using dialog
408 $this->createDialogContact();
409 $this->select('financial_type_id', $financialType);
410 $this->type('total_amount', 100 * $i);
411 $this->clickLink('_qf_Contribution_upload_new', '_qf_Contribution_upload_new');
412 }
413 $this->openCiviPage("contact/search/builder", "reset=1", "_qf_Builder_refresh");
414
415 $this->enterValues(1, 1, 'Contribution', 'Financial Type', NULL, '=', array($financialTypeName1));
416 $this->clickLink('_qf_Builder_refresh');
417
418 $this->assertTrue($this->isTextPresent('3 Contacts'), 'Missing text: ' . '3 Contacts');
419
420 $this->click("xpath=//div[@class='crm-accordion-header crm-master-accordion-header']");
421 $this->enterValues(1, 1, 'Contribution', 'Financial Type', NULL, '=', array($financialTypeName2));
422 $this->clickLink('_qf_Builder_refresh');
423
424 $this->assertTrue($this->isTextPresent('3 Contacts'), 'Missing text: ' . '3 Contacts');
425
426 $this->click("xpath=//div[@class='crm-accordion-header crm-master-accordion-header']");
427 $this->enterValues(1, 1, 'Contribution', 'Financial Type', NULL, 'IN', array(
428 $financialTypeName1,
429 $financialTypeName2,
430 ));
431 $this->clickLink('_qf_Builder_refresh');
432
433 $this->assertTrue($this->isTextPresent('6 Contacts'), 'Missing text: ' . '6 Contacts');
434 }
435
436 /**
437 * Webtest for CRM-12588
438 */
439 public function testSearchBuilderMembershipType() {
440 $this->webtestLogin();
441
442 // create first contact
443 $firstName1 = substr(sha1(rand()), 0, 7);
444 $this->webtestAddContact($firstName1, "Memberson", "Memberson{$firstName1}@memberson.name");
445 $contactName1 = "Memberson, $firstName1";
446
447 // create Second contact
448 $firstName2 = substr(sha1(rand()), 0, 7);
449 $this->webtestAddContact($firstName2, "Memberson", "Memberson{$firstName2}@memberson.name");
450 $contactName2 = "Memberson, $firstName2";
451
452 // add membership type
453 $membershipTypes = $this->webtestAddMembershipType();
454
455 // now add membership
456 $this->openCiviPage("member/add", "reset=1&action=add&context=standalone", "_qf_Membership_upload");
457
458 // select contact
459 $this->webtestFillAutocomplete($firstName1);
460
461 // fill in Membership Organization
462 $this->select("membership_type_id[0]", "label={$membershipTypes['member_of_contact']}");
463
464 // select membership type
465 $this->select("membership_type_id[1]", "label={$membershipTypes['membership_type']}");
466
467 // fill in Source
468 $this->type("source", "Membership StandaloneAddTest Webtest");
469
470 // fill in Start Date
471 $this->webtestFillDate('start_date');
472
473 // Clicking save.
474 $this->clickLink("_qf_Membership_upload");
475
476 // page was loaded
477 $this->waitForTextPresent("Membership StandaloneAddTest Webtest");
478
479 // now add membership for second contact
480 $this->openCiviPage("member/add", "reset=1&action=add&context=standalone", "_qf_Membership_upload");
481 $this->webtestFillAutocomplete($firstName2);
482 $this->select("membership_type_id[0]", "label={$membershipTypes['member_of_contact']}");
483 $this->select("membership_type_id[1]", "label={$membershipTypes['membership_type']}");
484 $this->type("source", "Membership StandaloneAddTest Webtest");
485 $this->webtestFillDate('start_date');
486
487 // fill in Status Override?
488 $this->click("is_override");
489 $this->waitForElementPresent("status_id");
490 $this->select("status_id", "label=Grace");
491
492 // Clicking save.
493 $this->clickLink("_qf_Membership_upload");
494 $this->waitForTextPresent("Membership StandaloneAddTest Webtest");
495
496 // Open the search builder
497 $this->openCiviPage('contact/search/builder', 'reset=1');
498 $this->enterValues(1, 1, 'Membership', 'Membership Type', NULL, '=', array($membershipTypes['membership_type']));
499
500 $this->clickLink('_qf_Builder_refresh');
501 $this->waitForText('search-status', "2 Contacts");
502
503 $this->click("xpath=//div[@class='crm-accordion-header crm-master-accordion-header']");
504 $this->enterValues(1, 2, 'Membership', 'Membership Status', NULL, '=', array('New'));
505 $this->clickLink('_qf_Builder_refresh');
506 $this->waitForText('search-status', "1 Contact");
507
508 $this->enterValues(1, 2, 'Membership', 'Membership Status', NULL, '=', array('Grace'));
509 $this->clickLink('_qf_Builder_refresh');
510 $this->waitForText('search-status', "1 Contact");
511
512 $this->click("xpath=//div[@class='crm-accordion-header crm-master-accordion-header']");
513 $this->click("xpath=//div[@id='map-field']/div[1]/table/tbody/tr[2]/td/a");
514 $this->enterValues(1, 2, 'Membership', 'Membership Status', NULL, 'IN', array('New', 'Grace'));
515 $this->clickLink('_qf_Builder_refresh');
516 $this->waitForText('search-status', "2 Contacts");
517
518 $this->click("xpath=//div[@class='crm-accordion-header crm-master-accordion-header']");
519 $this->click("xpath=//div[@id='map-field']/div[1]/table/tbody/tr[2]/td/a");
520 $this->enterValues(1, 2, 'Membership', 'Membership Status', NULL, 'IN', array('Current', 'Expired'));
521 $this->clickLink('_qf_Builder_refresh');
522 $this->waitForText("xpath=//form[@id='Builder']/div[3]/div/div", "No matches found for");
523
524 // Find Membership
525 $this->openCiviPage("member/search", "reset=1", "_qf_Search_refresh");
526 $this->multiselect2("membership_type_id", array($membershipTypes['membership_type']));
527 $this->clickLink('_qf_Search_refresh');
528 $this->waitForText('search-status', "2 Results");
529
530 $this->click("xpath=//div[@class='crm-accordion-header crm-master-accordion-header']");
531 $this->multiselect2("membership_status_id", array("New", "Grace"));
532 $this->clickLink('_qf_Search_refresh');
533 $this->waitForText('search-status', "2 Results");
534
535 $this->openCiviPage("member/search", "reset=1", "_qf_Search_refresh");
536 $this->click("xpath=//div[@class='crm-accordion-header crm-master-accordion-header']");
537 $this->multiselect2("membership_type_id", array($membershipTypes['membership_type']));
538 $this->multiselect2("membership_status_id", array("New"));
539 $this->clickLink('_qf_Search_refresh');
540 $this->waitForText('search-status', "1 Result");
541 }
542
543 }