CRM-18454: webtest fixes
[civicrm-core.git] / tests / phpunit / CiviTest / CiviSeleniumTestCase.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
aa2733c0 28define('CIVICRM_WEBTEST', 1);
6a488035 29
6a488035
TO
30/**
31 * Base class for CiviCRM Selenium tests
32 *
33 * Common functions for unit tests
34 * @package CiviCRM
35 */
36class CiviSeleniumTestCase extends PHPUnit_Extensions_SeleniumTestCase {
37
42daf119
CW
38 // Current logged-in user
39 protected $loggedInAs = NULL;
6a488035 40
d3f2fd13
TO
41 private $settingCache;
42
6a488035 43 /**
eceb18cc 44 * Constructor.
6a488035
TO
45 *
46 * Because we are overriding the parent class constructor, we
47 * need to show the same arguments as exist in the constructor of
48 * PHPUnit_Framework_TestCase, since
49 * PHPUnit_Framework_TestSuite::createTest() creates a
50 * ReflectionClass of the Test class and checks the constructor
51 * of that class to decide how to set up the test.
52 *
e16033b4
TO
53 * @param string $name
54 * @param array $data
55 * @param string $dataName
2a6da8d7 56 * @param array $browser
6a488035 57 */
00be9182 58 public function __construct($name = NULL, array$data = array(), $dataName = '', array$browser = array()) {
6a488035 59 parent::__construct($name, $data, $dataName, $browser);
42daf119 60 $this->loggedInAs = NULL;
6a488035 61
6a488035 62 $this->settings = new CiviSeleniumSettings();
b5c0ad9a
TO
63 if (property_exists($this->settings, 'serverStartupTimeOut') && $this->settings->serverStartupTimeOut) {
64 global $CiviSeleniumTestCase_polled;
65 if (!$CiviSeleniumTestCase_polled) {
66 $CiviSeleniumTestCase_polled = TRUE;
67 CRM_Utils_Network::waitForServiceStartup(
68 $this->drivers[0]->getHost(),
69 $this->drivers[0]->getPort(),
70 $this->settings->serverStartupTimeOut
71 );
72 }
73 }
6a488035
TO
74
75 // autoload
76 require_once 'CRM/Core/ClassLoader.php';
77 CRM_Core_ClassLoader::singleton()->register();
78
79 // also initialize a connection to the db
42daf119 80 // FIXME: not necessary for most tests, consider moving into functions that need this
6a488035
TO
81 $config = CRM_Core_Config::singleton();
82 }
83
84 protected function setUp() {
85 $this->setBrowser($this->settings->browser);
86 // Make sure that below strings have path separator at the end
87 $this->setBrowserUrl($this->settings->sandboxURL);
88 $this->sboxPath = $this->settings->sandboxPATH;
b5c0ad9a
TO
89 if (property_exists($this->settings, 'rcHost') && $this->settings->rcHost) {
90 $this->setHost($this->settings->rcHost);
91 }
92 if (property_exists($this->settings, 'rcPort') && $this->settings->rcPort) {
93 $this->setPort($this->settings->rcPort);
94 }
d3f2fd13 95 $this->settingCache = array();
6a488035
TO
96 }
97
7fe37828
EM
98 /**
99 * @return string
100 */
bf446982
TO
101 protected function prepareTestSession() {
102 $result = parent::prepareTestSession();
103
104 // Set any cookies required by local installation
105 // Note: considered doing this in setUp(), but the Selenium session wasn't yet initialized.
106 if (property_exists($this->settings, 'cookies')) {
107 // We don't really care about this page, but it seems we need
108 // to open a page before setting a cookie.
109 $this->open($this->sboxPath);
110 $this->waitForPageToLoad($this->getTimeoutMsec());
111 $this->setCookies($this->settings->cookies);
112 }
113 return $result;
114 }
115
116 /**
e16033b4 117 * @param array $cookies
16b10e64
CW
118 * Each item is an Array with keys:
119 * - name: string
120 * - value: string; note that RFC's don't define particular encoding scheme, so
bf446982
TO
121 * you must pick one yourself and pre-encode; does not allow values with
122 * commas, semicolons, or whitespace
16b10e64
CW
123 * - path: string; default: '/'
124 * - max_age: int; default: 1 week (7*24*60*60)
bf446982
TO
125 */
126 protected function setCookies($cookies) {
127 foreach ($cookies as $cookie) {
128 if (!isset($cookie['path'])) {
129 $cookie['path'] = '/';
130 }
131 if (!isset($cookie['max_age'])) {
6c6e6187 132 $cookie['max_age'] = 7 * 24 * 60 * 60;
bf446982
TO
133 }
134 $this->deleteCookie($cookie['name'], $cookie['path']);
135 $optionExprs = array();
136 foreach ($cookie as $key => $value) {
137 if ($key != 'name' && $key != 'value') {
138 $optionExprs[] = "$key=$value";
139 }
140 }
141 $this->createCookie("{$cookie['name']}={$cookie['value']}", implode(', ', $optionExprs));
142 }
143 }
144
6a488035 145 protected function tearDown() {
6a488035
TO
146 }
147
148 /**
eceb18cc 149 * Authenticate as drupal user.
5896d037
TO
150 * @param $user : (str) the key 'user' or 'admin', or a literal username
151 * @param $pass : (str) if $user is a literal username and not 'user' or 'admin', supply the password
6a488035 152 */
00be9182 153 public function webtestLogin($user = 'user', $pass = NULL) {
42daf119
CW
154 // If already logged in as correct user, do nothing
155 if ($this->loggedInAs === $user) {
156 return;
157 }
158 // If we are logged in as a different user, log out first
159 if ($this->loggedInAs) {
160 $this->webtestLogout();
161 }
6a488035 162 $this->open("{$this->sboxPath}user");
42daf119
CW
163 // Lookup username & password if not supplied
164 $username = $user;
165 if ($pass === NULL) {
166 $pass = $user == 'admin' ? $this->settings->adminPassword : $this->settings->password;
167 $username = $user == 'admin' ? $this->settings->adminUsername : $this->settings->username;
168 }
6a488035
TO
169 // Make sure login form is available
170 $this->waitForElementPresent('edit-submit');
171 $this->type('edit-name', $username);
42daf119 172 $this->type('edit-pass', $pass);
6a488035
TO
173 $this->click('edit-submit');
174 $this->waitForPageToLoad($this->getTimeoutMsec());
42daf119
CW
175 $this->loggedInAs = $user;
176 }
177
00be9182 178 public function webtestLogout() {
42daf119
CW
179 if ($this->loggedInAs) {
180 $this->open($this->sboxPath . "user/logout");
181 $this->waitForPageToLoad($this->getTimeoutMsec());
182 }
183 $this->loggedInAs = NULL;
6a488035
TO
184 }
185
186 /**
187 * Open an internal path beginning with 'civicrm/'
188 *
5a4f6742
CW
189 * @param string $url
190 * omit the 'civicrm/' it will be added for you.
191 * @param string|array $args
192 * optional url arguments.
e16033b4
TO
193 * @param $waitFor
194 * Page element to wait for - using this is recommended to ensure the document is fully loaded.
6a488035
TO
195 *
196 * Although it doesn't seem to do much now, using this function is recommended for
197 * opening all civi pages, and using the $args param is also strongly encouraged
198 * This will make it much easier to run webtests in other CMSs in the future
199 */
00be9182 200 public function openCiviPage($url, $args = NULL, $waitFor = 'civicrm-footer') {
6a488035
TO
201 // Construct full url with args
202 // This could be extended in future to work with other CMS style urls
203 if ($args) {
204 if (is_array($args)) {
205 $sep = '?';
206 foreach ($args as $key => $val) {
207 $url .= $sep . $key . '=' . $val;
208 $sep = '&';
209 }
210 }
211 else {
212 $url .= "?$args";
213 }
214 }
215 $this->open("{$this->sboxPath}civicrm/$url");
42daf119 216 $this->waitForPageToLoad($this->getTimeoutMsec());
b50973e8 217 $this->checkForErrorsOnPage();
6a488035
TO
218 if ($waitFor) {
219 $this->waitForElementPresent($waitFor);
220 }
221 }
222
b45c587e 223 /**
eceb18cc 224 * Click on a link or button.
b45c587e
CW
225 * Wait for the page to load
226 * Wait for an element to be present
1e1fdcf6
EM
227 * @param $element
228 * @param string $waitFor
229 * @param bool $waitForPageLoad
b45c587e 230 */
00be9182 231 public function clickLink($element, $waitFor = 'civicrm-footer', $waitForPageLoad = TRUE) {
b45c587e 232 $this->click($element);
28154ee7
PJ
233 // conditional wait for page load e.g for ajax form save
234 if ($waitForPageLoad) {
235 $this->waitForPageToLoad($this->getTimeoutMsec());
b50973e8 236 $this->checkForErrorsOnPage();
28154ee7 237 }
b45c587e
CW
238 if ($waitFor) {
239 $this->waitForElementPresent($waitFor);
240 }
241 }
405bc6ee
CW
242
243 /**
eceb18cc 244 * Click a link or button and wait for an ajax dialog to load.
50d95825
CW
245 * @param string $element
246 * @param string $waitFor
247 */
6c6e6187 248 public function clickPopupLink($element, $waitFor = NULL) {
a5d61f09
CW
249 $this->clickAjaxLink($element, 'css=.ui-dialog');
250 if ($waitFor) {
251 $this->waitForElementPresent($waitFor);
252 }
253 }
254
255 /**
eceb18cc 256 * Click a link or button and wait for ajax content to load or refresh.
a5d61f09
CW
257 * @param string $element
258 * @param string $waitFor
259 */
6c6e6187 260 public function clickAjaxLink($element, $waitFor = NULL) {
50d95825 261 $this->click($element);
50d95825
CW
262 if ($waitFor) {
263 $this->waitForElementPresent($waitFor);
264 }
57d32317 265 $this->waitForAjaxContent();
50d95825
CW
266 }
267
268 /**
269 * Force a link to open full-page, even if it would normally open in a popup
e739afc3 270 * @note: works with links only, not buttons
50d95825 271 * @param string $element
405bc6ee
CW
272 * @param string $waitFor
273 */
00be9182 274 public function clickLinkSuppressPopup($element, $waitFor = 'civicrm-footer') {
405bc6ee
CW
275 $link = $this->getAttribute($element . '@href');
276 $this->open($link);
277 $this->waitForPageToLoad($this->getTimeoutMsec());
278 if ($waitFor) {
279 $this->waitForElementPresent($waitFor);
280 }
281 }
b45c587e 282
50d95825 283 /**
eceb18cc 284 * Wait for all ajax snippets to finish loading.
50d95825 285 */
00be9182 286 public function waitForAjaxContent() {
50d95825 287 $this->waitForElementNotPresent('css=.blockOverlay');
e739afc3
CW
288 // Some ajax calls happen in pairs (e.g. submit a popup form then refresh the underlying content)
289 // So we'll wait a sec and recheck to see if any more stuff is loading
290 sleep(1);
291 if ($this->isElementPresent('css=.blockOverlay')) {
292 $this->waitForAjaxContent();
293 }
50d95825
CW
294 }
295
6a488035 296 /**
eceb18cc 297 * Call the API on the local server.
6a488035 298 * (kind of defeats the point of a webtest - see CRM-11889)
1e1fdcf6
EM
299 * @param $entity
300 * @param $action
301 * @param $params
302 * @return array|int
6a488035 303 */
00be9182 304 public function webtest_civicrm_api($entity, $action, $params) {
6a488035
TO
305 if (!isset($params['version'])) {
306 $params['version'] = 3;
307 }
308
309 $result = civicrm_api($entity, $action, $params);
ef9cbdb7 310 $this->assertAPISuccess($result);
6a488035
TO
311 return $result;
312 }
313
314 /**
ef9cbdb7
TO
315 * Call the API on the remote server using the AJAX endpoint.
316 *
6a488035 317 * @see CRM-11889
1e1fdcf6
EM
318 * @param $entity
319 * @param $action
320 * @param array $params
321 * @return mixed
6a488035 322 */
00be9182 323 public function rest_civicrm_api($entity, $action, $params = array()) {
6a488035
TO
324 $params += array(
325 'version' => 3,
326 );
ef9cbdb7
TO
327 static $reqId = 0;
328 $reqId++;
329
330 $jsCmd = '
331 setTimeout(function(){
332 CRM.api3("@entity", "@action", @params).then(function(a){
333 cj("<textarea>").css("display", "none").prop("id","crmajax@reqId").val(JSON.stringify(a)).appendTo("body");
334 });
335 }, 500);
336 ';
337 $jsArgs = array(
338 '@entity' => $entity,
339 '@action' => $action,
340 '@params' => json_encode($params),
341 '@reqId' => $reqId,
6a488035 342 );
ef9cbdb7
TO
343 $js = strtr($jsCmd, $jsArgs);
344 $this->runScript($js);
345 $this->waitForElementPresent("crmajax{$reqId}");
346 $result = json_decode($this->getValue("crmajax{$reqId}"), TRUE);
347 $this->runScript("cj('#crmajax{$reqId}').remove();");
348 return $result;
6a488035
TO
349 }
350
4cbe18b8 351 /**
100fef9d 352 * @param string $option_group_name
4cbe18b8
EM
353 *
354 * @return array|int
355 */
00be9182 356 public function webtestGetFirstValueForOptionGroup($option_group_name) {
6a488035
TO
357 $result = $this->webtest_civicrm_api("OptionValue", "getvalue", array(
358 'option_group_name' => $option_group_name,
359 'option.limit' => 1,
21dfd5f5 360 'return' => 'value',
6a488035
TO
361 ));
362 return $result;
363 }
364
4cbe18b8
EM
365 /**
366 * @return mixed
367 */
00be9182 368 public function webtestGetValidCountryID() {
6a488035
TO
369 static $_country_id;
370 if (is_null($_country_id)) {
3d21830f 371 $_country_id = $this->webtestGetSetting('defaultContactCountry');
6a488035
TO
372 }
373 return $_country_id;
374 }
375
4cbe18b8
EM
376 /**
377 * @param $entity
378 *
379 * @return mixed|null
380 */
00be9182 381 public function webtestGetValidEntityID($entity) {
6a488035
TO
382 // michaelmcandrew: would like to use getvalue but there is a bug
383 // for e.g. group where option.limit not working at the moment CRM-9110
384 $result = $this->webtest_civicrm_api($entity, "get", array('option.limit' => 1, 'return' => 'id'));
385 if (!empty($result['values'])) {
386 return current(array_keys($result['values']));
387 }
388 return NULL;
389 }
390
d3f2fd13
TO
391 /**
392 * @param string $field
393 * @return mixed
394 */
395 public function webtestGetSetting($field) {
396 if (!isset($this->settingCache[$field])) {
397 $result = $this->webtest_civicrm_api("Setting", "getsingle", array(
398 'return' => $field,
399 ));
400 $this->settingCache[$field] = $result[$field];
401 }
402 return $this->settingCache[$field];
403 }
404
fc8cb638 405 /**
406 * override this since the built in function doesn't work with the new phpunit.
407 * @param string $element
408 * @param string $text
409 */
410 public function waitForText($element, $text) {
411 $this->waitForTextPresent($text);
412 $this->assertElementContainsText($element, $text);
413 }
414
1fbd57f8 415 /**
eceb18cc 416 * Ensures the required CiviCRM components are enabled.
1e1fdcf6 417 * @param $components
1fbd57f8 418 */
00be9182 419 public function enableComponents($components) {
1fbd57f8
CW
420 $this->openCiviPage("admin/setting/component", "reset=1", "_qf_Component_next-bottom");
421 $enabledComponents = $this->getSelectOptions("enableComponents-t");
422 $added = FALSE;
423 foreach ((array) $components as $comp) {
424 if (!in_array($comp, $enabledComponents)) {
0bd37c06 425 $this->addSelection("enableComponents-f", "label=$comp");
1fbd57f8
CW
426 $this->click("//option[@value='$comp']");
427 $this->click("add");
428 $added = TRUE;
429 }
430 }
431 if ($added) {
59843a57
CW
432 $this->clickLink("_qf_Component_next-bottom");
433 $this->checkCRMAlert("Saved");
1fbd57f8
CW
434 }
435 }
436
b25b6f70
WA
437 /**
438 * Ensures the required currencies are enabled.
439 * @param $currencies
440 */
441 public function enableCurrency($currencies) {
442 $this->openCiviPage("admin/setting/localization", "reset=1", "_qf_Localization_next-bottom");
443 $enabledCurrency = $this->getSelectOptions("currencyLimit-t");
444 foreach ($enabledCurrency as $k => $val) {
445 $enabledCurrency[$k] = substr($val, 0, 3);
446 }
447 $added = FALSE;
448 foreach ((array) $currencies as $curr) {
449 if (!in_array($curr, $enabledCurrency)) {
450 $this->addSelection("currencyLimit-f", "value=$curr");
451 $this->click("//option[@value='$curr']");
452 $this->click("add");
453 $added = TRUE;
454 }
455 }
456 if ($added) {
457 $this->clickLink("_qf_Localization_next-bottom");
458 }
459 }
460
6a488035 461 /**
eceb18cc 462 * Add a contact with the given first and last names and either a given email.
6a488035
TO
463 * (when specified), a random email (when true) or no email (when unspecified or null).
464 *
e16033b4
TO
465 * @param string $fname
466 * Contact’s first name.
467 * @param string $lname
468 * Contact’s last name.
469 * @param mixed $email
470 * Contact’s email (when string) or random email (when true) or no email (when null).
72b3a70c 471 * @param string $contactSubtype
2a6da8d7 472 *
72b3a70c
CW
473 * @return string|null
474 * either a string with the (either generated or provided) email or null (if no email)
6a488035 475 */
00be9182 476 public function webtestAddContact($fname = 'Anthony', $lname = 'Anderson', $email = NULL, $contactSubtype = NULL) {
5c88df60 477 $args = 'reset=1&ct=Individual';
6a488035 478 if ($contactSubtype) {
5c88df60 479 $args .= "&cst={$contactSubtype}";
6a488035 480 }
5c88df60 481 $this->openCiviPage('contact/add', $args, '_qf_Contact_upload_view-bottom');
6a488035
TO
482 $this->type('first_name', $fname);
483 $this->type('last_name', $lname);
484 if ($email === TRUE) {
485 $email = substr(sha1(rand()), 0, 7) . '@example.org';
486 }
487 if ($email) {
488 $this->type('email_1_email', $email);
489 }
5c88df60 490 $this->clickLink('_qf_Contact_upload_view-bottom');
6a488035
TO
491 return $email;
492 }
493
4cbe18b8
EM
494 /**
495 * @param string $householdName
496 * @param null $email
497 *
498 * @return null|string
499 */
00be9182 500 public function webtestAddHousehold($householdName = "Smith's Home", $email = NULL) {
b45c587e 501 $this->openCiviPage("contact/add", "reset=1&ct=Household");
6a488035
TO
502 $this->click('household_name');
503 $this->type('household_name', $householdName);
504
505 if ($email === TRUE) {
506 $email = substr(sha1(rand()), 0, 7) . '@example.org';
507 }
508 if ($email) {
509 $this->type('email_1_email', $email);
510 }
511
5c88df60 512 $this->clickLink('_qf_Contact_upload_view');
6a488035
TO
513 return $email;
514 }
515
4cbe18b8
EM
516 /**
517 * @param string $organizationName
518 * @param null $email
519 * @param null $contactSubtype
520 *
521 * @return null|string
522 */
00be9182 523 public function webtestAddOrganization($organizationName = "Organization XYZ", $email = NULL, $contactSubtype = NULL) {
5c88df60 524 $args = 'reset=1&ct=Organization';
2ddaadeb 525 if ($contactSubtype) {
5c88df60 526 $args .= "&cst={$contactSubtype}";
2ddaadeb 527 }
5c88df60 528 $this->openCiviPage('contact/add', $args, '_qf_Contact_upload_view-bottom');
6a488035
TO
529 $this->click('organization_name');
530 $this->type('organization_name', $organizationName);
531
532 if ($email === TRUE) {
533 $email = substr(sha1(rand()), 0, 7) . '@example.org';
534 }
535 if ($email) {
536 $this->type('email_1_email', $email);
537 }
5c88df60 538 $this->clickLink('_qf_Contact_upload_view');
6a488035
TO
539 return $email;
540 }
541
542 /**
1e1fdcf6
EM
543 * @param $sortName
544 * @param string $fieldName
6a488035 545 */
00be9182 546 public function webtestFillAutocomplete($sortName, $fieldName = 'contact_id') {
6c6e6187 547 $this->select2($fieldName, $sortName);
5320adf4 548 //$this->assertContains($sortName, $this->getValue($fieldName), "autocomplete expected $sortName but didn’t find it in " . $this->getValue($fieldName));
6a488035
TO
549 }
550
551 /**
1e1fdcf6 552 * @param $sortName
6a488035 553 */
00be9182 554 public function webtestOrganisationAutocomplete($sortName) {
33cf86bd 555 $this->clickAt("//*[@id='contact_id']/../div/a");
5320adf4
WA
556 $this->waitForElementPresent("//*[@id='select2-drop']/div/input");
557 $this->keyDown("//*[@id='select2-drop']/div/input", " ");
558 $this->type("//*[@id='select2-drop']/div/input", $sortName);
559 $this->typeKeys("//*[@id='select2-drop']/div/input", $sortName);
560 $this->waitForElementPresent("//*[@class='select2-result-label']");
561 $this->clickAt("//*[@class='select2-results']/li[1]");
6a488035
TO
562 //$this->assertContains($sortName, $this->getValue('contact_1'), "autocomplete expected $sortName but didn’t find it in " . $this->getValue('contact_1'));
563 }
564
4cbe18b8 565 /**
c490a46a
CW
566 * 1. By default, when no strtotime arg is specified, sets date to "now + 1 month"
567 * 2. Does not set time. For setting both date and time use webtestFillDateTime() method.
568 * 3. Examples of $strToTime arguments -
569 * webtestFillDate('start_date',"now")
570 * webtestFillDate('start_date',"10 September 2000")
571 * webtestFillDate('start_date',"+1 day")
572 * webtestFillDate('start_date',"+1 week")
573 * webtestFillDate('start_date',"+1 week 2 days 4 hours 2 seconds")
574 * webtestFillDate('start_date',"next Thursday")
575 * webtestFillDate('start_date',"last Monday")
4cbe18b8
EM
576 * @param $dateElement
577 * @param null $strToTimeArgs
578 */
cd71816e 579 public function webtestFillDate($dateElement, $strToTimeArgs = NULL, $multiselect = FALSE) {
6a488035
TO
580 $timeStamp = strtotime($strToTimeArgs ? $strToTimeArgs : '+1 month');
581
582 $year = date('Y', $timeStamp);
583 // -1 ensures month number is inline with calender widget's month
584 $mon = date('n', $timeStamp) - 1;
585 $day = date('j', $timeStamp);
586
cd71816e 587 if (!$multiselect) {
588 $this->click("xpath=//input[starts-with(@id, '{$dateElement}_display_')]");
589 }
6a488035
TO
590 $this->waitForElementPresent("css=div#ui-datepicker-div.ui-datepicker div.ui-datepicker-header div.ui-datepicker-title select.ui-datepicker-month");
591 $this->select("css=div#ui-datepicker-div.ui-datepicker div.ui-datepicker-header div.ui-datepicker-title select.ui-datepicker-month", "value=$mon");
592 $this->select("css=div#ui-datepicker-div div.ui-datepicker-header div.ui-datepicker-title select.ui-datepicker-year", "value=$year");
593 $this->click("link=$day");
594 }
595
4cbe18b8 596 /**
c490a46a 597 * 1. set both date and time.
4cbe18b8
EM
598 * @param $dateElement
599 * @param null $strToTimeArgs
600 */
00be9182 601 public function webtestFillDateTime($dateElement, $strToTimeArgs = NULL) {
6a488035
TO
602 $this->webtestFillDate($dateElement, $strToTimeArgs);
603
604 $timeStamp = strtotime($strToTimeArgs ? $strToTimeArgs : '+1 month');
605 $hour = date('h', $timeStamp);
606 $min = date('i', $timeStamp);
607 $meri = date('A', $timeStamp);
608
609 $this->type("{$dateElement}_time", "{$hour}:{$min}{$meri}");
610 }
611
612 /**
613 * Verify that given label/value pairs are in *sibling* td cells somewhere on the page.
614 *
e16033b4
TO
615 * @param array $expected
616 * Array of key/value pairs (like Status/Registered) to be checked.
617 * @param string $xpathPrefix
618 * Pass in an xpath locator to "get to" the desired table or tables. Will be prefixed to xpath.
6a488035 619 * table path. Include leading forward slashes (e.g. "//div[@id='activity-content']").
e16033b4
TO
620 * @param string $tableId
621 * Pass in the id attribute of a table to be verified if you want to only check a specific table.
6a488035
TO
622 * on the web page.
623 */
00be9182 624 public function webtestVerifyTabularData($expected, $xpathPrefix = NULL, $tableId = NULL) {
6a488035
TO
625 $tableLocator = "";
626 if ($tableId) {
627 $tableLocator = "[@id='$tableId']";
628 }
629 foreach ($expected as $label => $value) {
24f591b0 630 //assertContains() accepts param as string
631 $value = "$value";
6a488035 632 if ($xpathPrefix) {
5ddf4885 633 $this->waitForElementPresent("xpath=//table{$tableLocator}/tbody/tr/td{$xpathPrefix}[text()='{$label}']/../following-sibling::td");
b02d5792 634 $this->assertElementContainsText("xpath=//table{$tableLocator}/tbody/tr/td{$xpathPrefix}[text()='{$label}']/../following-sibling::td", $value);
6a488035
TO
635 }
636 else {
5ddf4885 637 $this->waitForElementPresent("xpath=//table{$tableLocator}/tbody/tr/td[text()='{$label}']/following-sibling::td");
b02d5792 638 $this->assertElementContainsText("xpath=//table{$tableLocator}/tbody/tr/td[text()='{$label}']/following-sibling::td", $value);
6a488035
TO
639 }
640 }
641 }
642
643 /**
eceb18cc 644 * Types text into a ckEditor rich text field in a form.
6a488035 645 *
e16033b4
TO
646 * @param string $fieldName
647 * Form field name (as assigned by PHP buildForm class).
648 * @param string $text
649 * Text to type into the field.
650 * @param string $editor
651 * Which text editor (valid values are 'CKEditor', 'TinyMCE').
6a488035 652 *
1e1fdcf6
EM
653 * @param bool $compressed
654 * @throws \PHPUnit_Framework_AssertionFailedError
6a488035 655 */
00be9182 656 public function fillRichTextField($fieldName, $text = 'Typing this text into editor.', $editor = 'CKEditor', $compressed = FALSE) {
6a488035
TO
657 // make sure cursor focuses on the field
658 $this->fireEvent($fieldName, 'focus');
659 if ($editor == 'CKEditor') {
02e08b2e 660 if ($compressed) {
000a086f 661 $this->click("xpath=//textarea[@id='{$fieldName}']/../div[1]");
02e08b2e 662 }
6a488035
TO
663 $this->waitForElementPresent("xpath=//div[@id='cke_{$fieldName}']//iframe");
664 $this->runScript("CKEDITOR.instances['{$fieldName}'].setData('<p>{$text}</p>');");
665 }
666 elseif ($editor == 'TinyMCE') {
f97d29d1
RN
667 $this->waitForElementPresent("xpath=//iframe[@id='{$fieldName}_ifr']");
668 $this->runScript("tinyMCE.activeEditor.setContent('<p>{$text}</p>');");
6a488035
TO
669 }
670 else {
671 $this->fail("Unknown editor value: $editor, failing (in CiviSeleniumTestCase::fillRichTextField ...");
672 }
673 $this->selectFrame('relative=top');
674 }
675
676 /**
eceb18cc 677 * Types option label and name into a table of multiple choice options.
6a488035
TO
678 * (for price set fields of type select, radio, or checkbox)
679 * TODO: extend for custom field multiple choice table input
680 *
e16033b4
TO
681 * @param array $options
682 * Form field name (as assigned by PHP buildForm class).
683 * @param array $validateStrings
684 * Appends label and name strings to this array so they can be validated later.
6a488035
TO
685 *
686 * @return void
687 */
00be9182 688 public function addMultipleChoiceOptions($options, &$validateStrings) {
6a488035
TO
689 foreach ($options as $oIndex => $oValue) {
690 $validateStrings[] = $oValue['label'];
691 $validateStrings[] = $oValue['amount'];
a7488080 692 if (!empty($oValue['membership_type_id'])) {
6a488035
TO
693 $this->select("membership_type_id_{$oIndex}", "value={$oValue['membership_type_id']}");
694 }
a7488080 695 if (!empty($oValue['financial_type_id'])) {
6a488035
TO
696 $this->select("option_financial_type_id_{$oIndex}", "label={$oValue['financial_type_id']}");
697 }
698 $this->type("option_label_{$oIndex}", $oValue['label']);
699 $this->type("option_amount_{$oIndex}", $oValue['amount']);
b02d5792 700 $this->click('link=add another choice');
6a488035
TO
701 }
702 }
703
704 /**
eceb18cc 705 * Use a contact EntityRef field to add a new contact.
e16033b4
TO
706 * @param string $field
707 * Selector.
80f3b91d 708 * @param string $contactType
a6c01b45 709 * @return array
16b10e64 710 * Array of contact attributes (id, names, email)
80f3b91d 711 */
00be9182 712 public function createDialogContact($field = 'contact_id', $contactType = 'Individual') {
80f3b91d
CW
713 $selectId = 's2id_' . $this->getAttribute($field . '@id');
714 $this->clickAt("xpath=//div[@id='$selectId']/a");
715 $this->clickAjaxLink("xpath=//li[@class='select2-no-results']//a[contains(text(), 'New $contactType')]", '_qf_Edit_next');
716
717 $name = substr(sha1(rand()), 0, rand(6, 8));
718 $params = array();
719 if ($contactType == 'Individual') {
720 $params['first_name'] = "$name $contactType";
721 $params['last_name'] = substr(sha1(rand()), 0, rand(5, 9));
722 }
723 else {
724 $params[strtolower($contactType) . '_name'] = "$name $contactType";
725 }
5896d037 726 foreach ($params as $param => $val) {
80f3b91d
CW
727 $this->type($param, $val);
728 }
729 $this->type('email-Primary', $params['email'] = "{$name}@example.com");
730 $this->clickAjaxLink('_qf_Edit_next');
731
6c6e6187 732 $this->waitForText("xpath=//div[@id='$selectId']", "$name");
80f3b91d
CW
733
734 $params['sort_name'] = $contactType == 'Individual' ? $params['last_name'] . ', ' . $params['first_name'] : "$name $contactType";
735 $params['display_name'] = $contactType == 'Individual' ? $params['first_name'] . ' ' . $params['last_name'] : $params['sort_name'];
736 $params['id'] = $this->getValue($field);
737 return $params;
738 }
739
740 /**
741 * @deprecated in favor of createDialogContact
1e1fdcf6
EM
742 * @param string $fname
743 * @param string $lname
744 * @param string $email
745 * @param int $type
746 * @param string $selectId
747 * @param int $row
748 * @param string $prefix
6a488035 749 */
3bdca100 750 public function webtestNewDialogContact(
5896d037
TO
751 $fname = 'Anthony', $lname = 'Anderson', $email = 'anthony@anderson.biz',
752 $type = 4, $selectId = 's2id_contact_id', $row = 1, $prefix = '') {
6a488035
TO
753 // 4 - Individual profile
754 // 5 - Organization profile
755 // 6 - Household profile
0e32ba2d 756 $profile = array('4' => 'New Individual', '5' => 'New Organization', '6' => 'New Household');
201e298c 757 $this->clickAt("xpath=//div[@id='$selectId']/a");
a5d61f09 758 $this->clickPopupLink("xpath=//li[@class='select2-no-results']//a[contains(text(),' $profile[$type]')]", '_qf_Edit_next');
6a488035
TO
759
760 switch ($type) {
761 case 4:
762 $this->type('first_name', $fname);
763 $this->type('last_name', $lname);
764 break;
765
766 case 5:
767 $this->type('organization_name', $fname);
768 break;
769
770 case 6:
771 $this->type('household_name', $fname);
772 break;
773 }
774
775 $this->type('email-Primary', $email);
9c5c0056 776 $this->clickAjaxLink('_qf_Edit_next');
6a488035
TO
777
778 // Is new contact created?
779 if ($lname) {
6c6e6187 780 $this->waitForText("xpath=//div[@id='$selectId']", "$lname, $fname");
6a488035
TO
781 }
782 else {
6c6e6187 783 $this->waitForText("xpath=//div[@id='$selectId']", "$fname");
6a488035
TO
784 }
785 }
786
787 /**
eceb18cc 788 * Generic function to check that strings are present in the page.
6a488035
TO
789 *
790 * @strings array array of strings or a single string
791 *
2a6da8d7 792 * @param $strings
2b37475d 793 * @return void
6a488035 794 */
00be9182 795 public function assertStringsPresent($strings) {
6a488035
TO
796 foreach ((array) $strings as $string) {
797 $this->assertTrue($this->isTextPresent($string), "Could not find $string on page");
798 }
799 }
800
801 /**
802 * Generic function to parse a URL string into it's elements.extract a variable value from a string (url)
803 *
804 * @url string url to parse or retrieve current url if null
805 *
2a6da8d7 806 * @param null $url
a6c01b45
CW
807 * @return array
808 * returns an associative array containing any of the various components
6a488035
TO
809 * of the URL that are present. Querystring elements are returned in sub-array (elements.queryString)
810 * http://php.net/manual/en/function.parse-url.php
6a488035 811 */
00be9182 812 public function parseURL($url = NULL) {
6a488035
TO
813 if (!$url) {
814 $url = $this->getLocation();
815 }
816
817 $elements = parse_url($url);
818 if (!empty($elements['query'])) {
819 $elements['queryString'] = array();
820 parse_str($elements['query'], $elements['queryString']);
821 }
822 return $elements;
823 }
824
efb29358 825 /**
eceb18cc 826 * Returns a single argument from the url query.
1e1fdcf6
EM
827 * @param $arg
828 * @param null $url
829 * @return null
efb29358 830 */
6c6e6187
TO
831 public function urlArg($arg, $url = NULL) {
832 $elements = $this->parseURL($url);
833 return isset($elements['queryString'][$arg]) ? $elements['queryString'][$arg] : NULL;
834 }
efb29358 835
6a488035
TO
836 /**
837 * Define a payment processor for use by a webtest. Default is to create Dummy processor
838 * which is useful for testing online public forms (online contribution pages and event registration)
839 *
e16033b4
TO
840 * @param string $processorName
841 * Name assigned to new processor.
842 * @param string $processorType
843 * Name for processor type (e.g. PayPal, Dummy, etc.).
844 * @param array $processorSettings
845 * Array of fieldname => value for required settings for the processor.
6a488035 846 *
2a6da8d7
EM
847 * @param string $financialAccount
848 * @throws PHPUnit_Framework_AssertionFailedError
57d32317 849 * @return int
6a488035 850 */
00be9182 851 public function webtestAddPaymentProcessor($processorName = 'Test Processor', $processorType = 'Dummy', $processorSettings = NULL, $financialAccount = 'Deposit Bank Account') {
6a488035
TO
852 if (!$processorName) {
853 $this->fail("webTestAddPaymentProcessor requires $processorName.");
854 }
e8eccbf5
CW
855 // Ensure we are logged in as admin before we proceed
856 $this->webtestLogin('admin');
857
c3ad8633
CW
858 if ($processorName === 'Test Processor') {
859 // Use the default test processor, no need to create a new one
0512e9c6 860 $this->openCiviPage('admin/paymentProcessor', 'action=update&id=1&reset=1', '_qf_PaymentProcessor_cancel-bottom');
c3ad8633 861 $this->check('is_default');
39c74f87 862 $this->select('financial_account_id', "label={$financialAccount}");
c3ad8633
CW
863 $this->clickLink('_qf_PaymentProcessor_next-bottom');
864 return 1;
6a488035 865 }
e8eccbf5
CW
866
867 if ($processorType == 'Dummy') {
868 $processorSettings = array(
869 'user_name' => 'dummy',
870 'url_site' => 'http://dummy.com',
871 'test_user_name' => 'dummytest',
872 'test_url_site' => 'http://dummytest.com',
873 );
874 }
6a488035
TO
875 elseif ($processorType == 'AuthNet') {
876 // FIXME: we 'll need to make a new separate account for testing
877 $processorSettings = array(
599e7e41 878 //dummy live username/password are passed to bypass processor validation on live credential
879 'user_name' => '3HcY62mY',
880 'password' => '69943NrwaQA92b8J',
6a488035 881 'test_user_name' => '5ULu56ex',
e390a377 882 'password' => '7ARxW575w736eF5p',
6a488035
TO
883 'test_password' => '7ARxW575w736eF5p',
884 );
885 }
6a488035
TO
886 elseif ($processorType == 'PayPal') {
887 $processorSettings = array(
888 'test_user_name' => '559999327053114',
3109a145 889 'user_name' => '559999327053114',
6a488035
TO
890 'test_password' => 'R2zv2g60-A7GXKJYl0nR0g',
891 'test_signature' => 'R2zv2g60-A7GXKJYl0nR0g',
3109a145 892 'password' => 'R2zv2g60-A7GXKJYl0nR0g',
893 'signature' => 'R2zv2g60-A7GXKJYl0nR0g',
6a488035
TO
894 );
895 }
896 elseif ($processorType == 'PayPal_Standard') {
897 $processorSettings = array(
599e7e41 898 'user_name' => 'V18ki@9r5Bf.org',
6a488035
TO
899 'test_user_name' => 'V18ki@9r5Bf.org',
900 );
901 }
902 elseif (empty($processorSettings)) {
903 $this->fail("webTestAddPaymentProcessor requires $processorSettings array if processorType is not Dummy.");
904 }
905 $pid = CRM_Core_DAO::getFieldValue("CRM_Financial_DAO_PaymentProcessorType", $processorType, "id", "name");
906 if (empty($pid)) {
907 $this->fail("$processorType processortype not found.");
908 }
57d32317 909 $this->openCiviPage('admin/paymentProcessor', 'action=add&reset=1&pp=' . $pid, 'name');
6a488035
TO
910 $this->type('name', $processorName);
911 $this->select('financial_account_id', "label={$financialAccount}");
6c6e6187 912 foreach ($processorSettings as $f => $v) {
6a488035
TO
913 $this->type($f, $v);
914 }
14d071ba 915
57d32317
CW
916 // Save
917 $this->clickLink('_qf_PaymentProcessor_next-bottom');
918
f48a56fc 919 $this->waitForTextPresent($processorName);
6a488035 920
57d32317
CW
921 // Get payment processor id
922 $paymentProcessorLink = $this->getAttribute("xpath=//table[@class='selector row-highlight']//tbody//tr/td[text()='{$processorName}']/../td[7]/span/a[1]@href");
923 return $this->urlArg('id', $paymentProcessorLink);
6a488035 924 }
5c88df60 925
00be9182 926 public function webtestAddCreditCardDetails() {
6a488035
TO
927 $this->waitForElementPresent('credit_card_type');
928 $this->select('credit_card_type', 'label=Visa');
929 $this->type('credit_card_number', '4807731747657838');
930 $this->type('cvv2', '123');
931 $this->select('credit_card_exp_date[M]', 'label=Feb');
932 $this->select('credit_card_exp_date[Y]', 'label=2019');
933 }
934
4cbe18b8
EM
935 /**
936 * @param null $firstName
937 * @param null $middleName
938 * @param null $lastName
939 *
940 * @return array
941 */
00be9182 942 public function webtestAddBillingDetails($firstName = NULL, $middleName = NULL, $lastName = NULL) {
6a488035
TO
943 if (!$firstName) {
944 $firstName = 'John';
945 }
946
947 if (!$middleName) {
948 $middleName = 'Apple';
949 }
950
951 if (!$lastName) {
952 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
953 }
954
955 $this->type('billing_first_name', $firstName);
956 $this->type('billing_middle_name', $middleName);
957 $this->type('billing_last_name', $lastName);
958
959 $this->type('billing_street_address-5', '234 Lincoln Ave');
960 $this->type('billing_city-5', 'San Bernadino');
86797006 961 $this->select2('billing_country_id-5', 'UNITED STATES');
b3d40287 962 $this->select2('billing_state_province_id-5', 'California');
6a488035
TO
963 $this->type('billing_postal_code-5', '93245');
964
965 return array($firstName, $middleName, $lastName);
966 }
967
4cbe18b8
EM
968 /**
969 * @param $fieldLocator
970 * @param null $filePath
971 *
972 * @return null|string
973 */
00be9182 974 public function webtestAttachFile($fieldLocator, $filePath = NULL) {
6a488035
TO
975 if (!$filePath) {
976 $filePath = '/tmp/testfile_' . substr(sha1(rand()), 0, 7) . '.txt';
977 $fp = @fopen($filePath, 'w');
3bdca100 978 fwrite($fp, 'Test file created by selenium test.');
6a488035
TO
979 @fclose($fp);
980 }
981
982 $this->assertTrue(file_exists($filePath), 'Not able to locate file: ' . $filePath);
983
984 $this->attachFile($fieldLocator, "file://{$filePath}");
985
986 return $filePath;
987 }
988
4cbe18b8
EM
989 /**
990 * @param $headers
991 * @param $rows
992 * @param null $filePath
993 *
994 * @return null|string
995 */
00be9182 996 public function webtestCreateCSV($headers, $rows, $filePath = NULL) {
6a488035
TO
997 if (!$filePath) {
998 $filePath = '/tmp/testcsv_' . substr(sha1(rand()), 0, 7) . '.csv';
999 }
1000
1001 $data = '"' . implode('", "', $headers) . '"' . "\r\n";
1002
1003 foreach ($rows as $row) {
1004 $temp = array();
1005 foreach ($headers as $field => $header) {
1006 $temp[$field] = isset($row[$field]) ? '"' . $row[$field] . '"' : '""';
1007 }
1008 $data .= implode(', ', $temp) . "\r\n";
1009 }
1010
1011 $fp = @fopen($filePath, 'w');
1012 @fwrite($fp, $data);
1013 @fclose($fp);
1014
1015 $this->assertTrue(file_exists($filePath), 'Not able to locate file: ' . $filePath);
1016
1017 return $filePath;
1018 }
1019
1020 /**
1021 * Create new relationship type w/ user specified params or default.
1022 *
5a4f6742 1023 * @param array $params
72b3a70c 1024 * array of required params.
6a488035 1025 *
72b3a70c
CW
1026 * @return array
1027 * array of saved params values.
6a488035 1028 */
00be9182 1029 public function webtestAddRelationshipType($params = array()) {
b45c587e 1030 $this->openCiviPage("admin/reltype", "reset=1&action=add");
6a488035
TO
1031
1032 //build the params if not passed.
1033 if (!is_array($params) || empty($params)) {
1034 $params = array(
1035 'label_a_b' => 'Test Relationship Type A - B -' . rand(),
1036 'label_b_a' => 'Test Relationship Type B - A -' . rand(),
1037 'contact_types_a' => 'Individual',
1038 'contact_types_b' => 'Individual',
1039 'description' => 'Test Relationship Type Description',
1040 );
1041 }
1042 //make sure we have minimum required params.
1043 if (!isset($params['label_a_b']) || empty($params['label_a_b'])) {
1044 $params['label_a_b'] = 'Test Relationship Type A - B -' . rand();
1045 }
1046
1047 //start the form fill.
1048 $this->type('label_a_b', $params['label_a_b']);
1049 $this->type('label_b_a', $params['label_b_a']);
1050 $this->select('contact_types_a', "value={$params['contact_type_a']}");
1051 $this->select('contact_types_b', "value={$params['contact_type_b']}");
1052 $this->type('description', $params['description']);
1053
1054 //save the data.
1055 $this->click('_qf_RelationshipType_next-bottom');
1056 $this->waitForPageToLoad($this->getTimeoutMsec());
1057
1058 //does data saved.
1059 $this->assertTrue($this->isTextPresent('The Relationship Type has been saved.'),
1060 "Status message didn't show up after saving!"
1061 );
1062
b45c587e 1063 $this->openCiviPage("admin/reltype", "reset=1");
6a488035
TO
1064
1065 //validate data on selector.
1066 $data = $params;
1067 if (isset($data['description'])) {
1068 unset($data['description']);
1069 }
1070 $this->assertStringsPresent($data);
1071
1072 return $params;
1073 }
1074
1075 /**
1076 * Create new online contribution page w/ user specified params or defaults.
b45c587e 1077 * FIXME: this function take an absurd number of params - very unwieldy :(
6a488035 1078 *
2a6da8d7
EM
1079 * @param null $hash
1080 * @param null $rand
1081 * @param null $pageTitle
1082 * @param array $processor
1083 * @param bool $amountSection
1084 * @param bool $payLater
1085 * @param bool $onBehalf
1086 * @param bool $pledges
1087 * @param bool $recurring
1088 * @param bool $membershipTypes
100fef9d 1089 * @param int $memPriceSetId
2a6da8d7
EM
1090 * @param bool $friend
1091 * @param int $profilePreId
1092 * @param int $profilePostId
1093 * @param bool $premiums
1094 * @param bool $widget
1095 * @param bool $pcp
1096 * @param bool $isAddPaymentProcessor
1097 * @param bool $isPcpApprovalNeeded
1098 * @param bool $isSeparatePayment
1099 * @param bool $honoreeSection
91d49cae 1100 * @param bool $allowOtherAmount
2a6da8d7
EM
1101 * @param bool $isConfirmEnabled
1102 * @param string $financialType
1103 * @param bool $fixedAmount
1104 * @param bool $membershipsRequired
6a488035 1105 *
a6c01b45
CW
1106 * @return null
1107 * of newly created online contribution page.
6a488035 1108 */
3bdca100 1109 public function webtestAddContributionPage(
5896d037
TO
1110 $hash = NULL,
1111 $rand = NULL,
1112 $pageTitle = NULL,
1113 $processor = array('Test Processor' => 'Dummy'),
1114 $amountSection = TRUE,
1115 $payLater = TRUE,
1116 $onBehalf = TRUE,
1117 $pledges = TRUE,
1118 $recurring = FALSE,
1119 $membershipTypes = TRUE,
1120 $memPriceSetId = NULL,
1121 $friend = TRUE,
1122 $profilePreId = 1,
1123 $profilePostId = 7,
1124 $premiums = TRUE,
1125 $widget = TRUE,
1126 $pcp = TRUE,
1127 $isAddPaymentProcessor = TRUE,
1128 $isPcpApprovalNeeded = FALSE,
1129 $isSeparatePayment = FALSE,
1130 $honoreeSection = TRUE,
1131 $allowOtherAmount = TRUE,
1132 $isConfirmEnabled = TRUE,
1133 $financialType = 'Donation',
1134 $fixedAmount = TRUE,
1135 $membershipsRequired = TRUE
6a488035
TO
1136 ) {
1137 if (!$hash) {
1138 $hash = substr(sha1(rand()), 0, 7);
1139 }
1140 if (!$pageTitle) {
1141 $pageTitle = 'Donate Online ' . $hash;
1142 }
1143
1144 if (!$rand) {
1145 $rand = 2 * rand(2, 50);
1146 }
1147
1148 // Create a new payment processor if requested
1149 if ($isAddPaymentProcessor) {
1150 while (list($processorName, $processorType) = each($processor)) {
1151 $this->webtestAddPaymentProcessor($processorName, $processorType);
1152 }
1153 }
1154
1155 // go to the New Contribution Page page
b45c587e 1156 $this->openCiviPage('admin/contribute', 'action=add&reset=1');
6a488035
TO
1157
1158 // fill in step 1 (Title and Settings)
1159 $this->type('title', $pageTitle);
1160
1161 //to select financial type
1162 $this->select('financial_type_id', "label={$financialType}");
1163
1164 if ($onBehalf) {
1165 $this->click('is_organization');
d340a0e8 1166 $this->select("xpath=//*[@class='crm-contribution-onbehalf_profile_id']//span[@class='crm-profile-selector-select']//select", 'label=On Behalf Of Organization');
6a488035
TO
1167 $this->type('for_organization', "On behalf $hash");
1168
1169 if ($onBehalf == 'required') {
1170 $this->click('CIVICRM_QFID_2_4');
1171 }
1172 elseif ($onBehalf == 'optional') {
1173 $this->click('CIVICRM_QFID_1_2');
1174 }
1175 }
1176
1177 $this->fillRichTextField('intro_text', 'This is introductory message for ' . $pageTitle, 'CKEditor');
1178 $this->fillRichTextField('footer_text', 'This is footer message for ' . $pageTitle, 'CKEditor');
1179
1180 $this->type('goal_amount', 10 * $rand);
1181
1182 // FIXME: handle Start/End Date/Time
1183 if ($honoreeSection) {
1184 $this->click('honor_block_is_active');
1185 $this->type('honor_block_title', "Honoree Section Title $hash");
1186 $this->type('honor_block_text', "Honoree Introductory Message $hash");
5ddf4885 1187 $this->click("//*[@id='s2id_soft_credit_types']/ul");
1188 $this->waitForElementPresent("//*[@id='select2-drop']/ul");
1189 $this->waitForElementPresent("//*[@class='select2-result-label']");
1190 $this->clickAt("//*[@class='select2-results']/li[1]");
6a488035
TO
1191 }
1192
1193 // is confirm enabled? it starts out enabled, so uncheck it if false
1194 if (!$isConfirmEnabled) {
1195 $this->click("id=is_confirm_enabled");
1196 }
1197
b45c587e
CW
1198 // Submit form
1199 $this->clickLink('_qf_Settings_next', "_qf_Amount_next-bottom");
1200
1201 // Get contribution page id
1202 $pageId = $this->urlArg('id');
6a488035
TO
1203
1204 // fill in step 2 (Processor, Pay Later, Amounts)
1205 if (!empty($processor)) {
1206 reset($processor);
1207 while (list($processorName) = each($processor)) {
1208 // select newly created processor
1209 $xpath = "xpath=//label[text() = '{$processorName}']/preceding-sibling::input[1]";
1210 $this->assertTrue($this->isTextPresent($processorName));
1211 $this->check($xpath);
1212 }
1213 }
1214
1215 if ($amountSection && !$memPriceSetId) {
1216 if ($payLater) {
1217 $this->click('is_pay_later');
1218 $this->type('pay_later_text', "Pay later label $hash");
3c001b0e 1219 $this->fillRichTextField('pay_later_receipt', "Pay later instructions $hash");
6a488035
TO
1220 }
1221
1222 if ($pledges) {
1223 $this->click('is_pledge_active');
1224 $this->click('pledge_frequency_unit[week]');
1225 $this->click('is_pledge_interval');
1226 $this->type('initial_reminder_day', 3);
1227 $this->type('max_reminders', 2);
1228 $this->type('additional_reminder_day', 1);
1229 }
1230 elseif ($recurring) {
1231 $this->click('is_recur');
1232 $this->click("is_recur_interval");
1233 $this->click("is_recur_installments");
1234 }
91d49cae 1235 if ($allowOtherAmount) {
6a488035
TO
1236
1237 $this->click('is_allow_other_amount');
1238
1239 // there shouldn't be minimums and maximums on test contribution forms unless you specify it
1240 //$this->type('min_amount', $rand / 2);
1241 //$this->type('max_amount', $rand * 10);
1242 }
91d49cae 1243 if ($fixedAmount || !$allowOtherAmount) {
1019390e
PN
1244 $this->type('label_1', "Label $hash");
1245 $this->type('value_1', "$rand");
1246 }
77f1e6b1 1247 $this->click('CIVICRM_QFID_1_4');
6a488035
TO
1248 }
1249 else {
1250 $this->click('amount_block_is_active');
1251 }
1252
1253 $this->click('_qf_Amount_next');
1254 $this->waitForElementPresent('_qf_Amount_next-bottom');
1255 $this->waitForPageToLoad($this->getTimeoutMsec());
1256 $text = "'Amount' information has been saved.";
1257 $this->assertTrue($this->isTextPresent($text), 'Missing text: ' . $text);
1258
1259 if ($memPriceSetId || (($membershipTypes === TRUE) || (is_array($membershipTypes) && !empty($membershipTypes)))) {
1260 // go to step 3 (memberships)
1261 $this->click('link=Memberships');
6de2d1f6 1262 $this->waitForElementPresent('_qf_MembershipBlock_submit_savenext');
6a488035
TO
1263
1264 // fill in step 3 (Memberships)
1265 $this->click('member_is_active');
1266 $this->waitForElementPresent('displayFee');
1267 $this->type('new_title', "Title - New Membership $hash");
1268 $this->type('renewal_title', "Title - Renewals $hash");
1269
1270 if ($memPriceSetId) {
1271 $this->click('member_price_set_id');
1272 $this->select('member_price_set_id', "value={$memPriceSetId}");
1273 }
1274 else {
1275 if ($membershipTypes === TRUE) {
0b49a3ec 1276 $membershipTypes = array(array('id' => 2, 'name' => 'Student', 'default' => 1));
6a488035
TO
1277 }
1278
1279 // FIXME: handle Introductory Message - New Memberships/Renewals
1280 foreach ($membershipTypes as $mType) {
1281 $this->click("membership_type_{$mType['id']}");
1282 if (array_key_exists('default', $mType)) {
0b49a3ec 1283 $this->click("xpath=//label[text() = '$mType[name]']/parent::td/parent::tr/td[2]/input");
6a488035
TO
1284 }
1285 if (array_key_exists('auto_renew', $mType)) {
1286 $this->select("auto_renew_{$mType['id']}", "label=Give option");
1287 }
1288 }
1019390e
PN
1289 if ($membershipsRequired) {
1290 $this->click('is_required');
1291 }
6a488035
TO
1292 if ($isSeparatePayment) {
1293 $this->click('is_separate_payment');
1294 }
1295 }
225a8648 1296 $this->clickLink('_qf_MembershipBlock_next', '_qf_MembershipBlock_next-bottom');
6a488035 1297 $text = "'MembershipBlock' information has been saved.";
0b49a3ec 1298 $this->isTextPresent($text);
6a488035
TO
1299 }
1300
1301 // go to step 4 (thank-you and receipting)
1302 $this->click('link=Receipt');
1303 $this->waitForElementPresent('_qf_ThankYou_next-bottom');
1304
1305 // fill in step 4
1306 $this->type('thankyou_title', "Thank-you Page Title $hash");
1307 // FIXME: handle Thank-you Message/Page Footer
1308 $this->type('receipt_from_name', "Receipt From Name $hash");
1309 $this->type('receipt_from_email', "$hash@example.org");
1310 $this->type('receipt_text', "Receipt Message $hash");
1311 $this->type('cc_receipt', "$hash@example.net");
1312 $this->type('bcc_receipt', "$hash@example.com");
1313
1314 $this->click('_qf_ThankYou_next');
1315 $this->waitForElementPresent('_qf_ThankYou_next-bottom');
1316 $this->waitForPageToLoad($this->getTimeoutMsec());
1317 $text = "'ThankYou' information has been saved.";
1318 $this->assertTrue($this->isTextPresent($text), 'Missing text: ' . $text);
1319
1320 if ($friend) {
1321 // fill in step 5 (Tell a Friend)
1322 $this->click('link=Tell a Friend');
1323 $this->waitForElementPresent('_qf_Contribute_next-bottom');
1324 $this->click('tf_is_active');
1325 $this->type('tf_title', "TaF Title $hash");
1326 $this->type('intro', "TaF Introduction $hash");
1327 $this->type('suggested_message', "TaF Suggested Message $hash");
1328 $this->type('general_link', "TaF Info Page Link $hash");
1329 $this->type('tf_thankyou_title', "TaF Thank-you Title $hash");
1330 $this->type('tf_thankyou_text', "TaF Thank-you Message $hash");
1331
1332 //$this->click('_qf_Contribute_next');
1333 $this->click('_qf_Contribute_next-bottom');
1334 $this->waitForPageToLoad($this->getTimeoutMsec());
1335 $text = "'Friend' information has been saved.";
1336 $this->assertTrue($this->isTextPresent($text), 'Missing text: ' . $text);
1337 }
1338
1339 if ($profilePreId || $profilePostId) {
1340 // fill in step 6 (Include Profiles)
1341 $this->click('css=li#tab_custom a');
1342 $this->waitForElementPresent('_qf_Custom_next-bottom');
1343
1344 if ($profilePreId) {
1421174e 1345 $this->select('css=tr.crm-contribution-contributionpage-custom-form-block-custom_pre_id span.crm-profile-selector-select select', "value={$profilePreId}");
6a488035
TO
1346 }
1347
1348 if ($profilePostId) {
1421174e 1349 $this->select('css=tr.crm-contribution-contributionpage-custom-form-block-custom_post_id span.crm-profile-selector-select select', "value={$profilePostId}");
6a488035
TO
1350 }
1351
1352 $this->click('_qf_Custom_next-bottom');
1353 //$this->waitForElementPresent('_qf_Custom_next-bottom');
1354
1355 $this->waitForPageToLoad($this->getTimeoutMsec());
1356 $text = "'Custom' information has been saved.";
1357 $this->assertTrue($this->isTextPresent($text), 'Missing text: ' . $text);
1358 }
1359
1360 if ($premiums) {
1361 // fill in step 7 (Premiums)
1362 $this->click('link=Premiums');
1363 $this->waitForElementPresent('_qf_Premium_next-bottom');
1364 $this->click('premiums_active');
1365 $this->type('premiums_intro_title', "Prem Title $hash");
1366 $this->type('premiums_intro_text', "Prem Introductory Message $hash");
1367 $this->type('premiums_contact_email', "$hash@example.info");
1368 $this->type('premiums_contact_phone', rand(100000000, 999999999));
1369 $this->click('premiums_display_min_contribution');
1370 $this->type('premiums_nothankyou_label', 'No thank-you');
1371 $this->click('_qf_Premium_next');
1372 $this->waitForElementPresent('_qf_Premium_next-bottom');
1373
1374 $this->waitForPageToLoad($this->getTimeoutMsec());
1375 $text = "'Premium' information has been saved.";
1376 $this->assertTrue($this->isTextPresent($text), 'Missing text: ' . $text);
c432c016 1377 $this->openCiviPage("admin/contribute/premium", "reset=1&action=update&id={$pageId}");
feb02c31 1378 $this->waitForAjaxContent();
feb02c31
WA
1379 $this->waitForElementPresent('_qf_Premium_cancel-bottom');
1380 $this->click("xpath=//div[@class='messages status no-popup']/a[text()='add one']");
1381 $this->waitForElementPresent('_qf_AddProduct_cancel-bottom');
1382 $this->select('product_id', "value=1");
7051f2e5 1383 $this->select('financial_type_id', "value=1");
feb02c31
WA
1384 $this->click('_qf_AddProduct_next-bottom');
1385 $this->waitForElementPresent('_qf_Premium_cancel-bottom');
1386 $this->click('_qf_Premium_next-bottom');
df90c6dd 1387 $this->waitForPageToLoad($this->getTimeoutMsec());
6a488035
TO
1388 }
1389
6a488035
TO
1390 if ($widget) {
1391 // fill in step 8 (Widget Settings)
1392 $this->click('link=Widgets');
1393 $this->waitForElementPresent('_qf_Widget_next-bottom');
6a488035
TO
1394 $this->click('is_active');
1395 $this->type('url_logo', "URL to Logo Image $hash");
1396 $this->type('button_title', "Button Title $hash");
1397 // Type About text in ckEditor (fieldname, text to type, editor)
1398 $this->fillRichTextField('about', 'This is for ' . $pageTitle, 'CKEditor');
1399
1400 $this->click('_qf_Widget_next');
1401 $this->waitForElementPresent('_qf_Widget_next-bottom');
1402
1403 $this->waitForPageToLoad($this->getTimeoutMsec());
1404 $text = "'Widget' information has been saved.";
1405 $this->assertTrue($this->isTextPresent($text), 'Missing text: ' . $text);
1406 }
1407
1408 if ($pcp) {
1409 // fill in step 9 (Enable Personal Campaign Pages)
1410 $this->click('link=Personal Campaigns');
1411 $this->waitForElementPresent('_qf_Contribute_next-bottom');
1412 $this->click('pcp_active');
1413 if (!$isPcpApprovalNeeded) {
1414 $this->click('is_approval_needed');
1415 }
1416 $this->type('notify_email', "$hash@example.name");
1417 $this->select('supporter_profile_id', 'value=2');
1418 $this->type('tellfriend_limit', 7);
1419 $this->type('link_text', "'Create Personal Campaign Page' link text $hash");
1420
1421 $this->click('_qf_Contribute_next-bottom');
1422 //$this->waitForElementPresent('_qf_PCP_next-bottom');
1423 $this->waitForPageToLoad($this->getTimeoutMsec());
1424 $text = "'Pcp' information has been saved.";
1425 $this->assertTrue($this->isTextPresent($text), 'Missing text: ' . $text);
1426 }
1427
b45c587e 1428 return $pageId;
6a488035
TO
1429 }
1430
1431 /**
100fef9d 1432 * Update default strict rule.
6a488035 1433 *
2a6da8d7 1434 * @param string $contactType
e16033b4
TO
1435 * @param array $fields
1436 * Fields to be set for strict rule.
1437 * @param int $threshold
1438 * Rule's threshold value.
6a488035 1439 */
00be9182 1440 public function webtestStrictDedupeRuleDefault($contactType = 'Individual', $fields = array(), $threshold = 10) {
6a488035
TO
1441 // set default strict rule.
1442 $strictRuleId = 4;
1443 if ($contactType == 'Organization') {
1444 $strictRuleId = 5;
1445 }
1446 elseif ($contactType == 'Household') {
1447 $strictRuleId = 6;
1448 }
1449
1450 // Default dedupe fields for each Contact type.
1451 if (empty($fields)) {
1452 $fields = array('civicrm_email.email' => 10);
1453 if ($contactType == 'Organization') {
1454 $fields = array(
1455 'civicrm_contact.organization_name' => 10,
1456 'civicrm_email.email' => 10,
1457 );
1458 }
1459 elseif ($contactType == 'Household') {
1460 $fields = array(
1461 'civicrm_contact.household_name' => 10,
1462 'civicrm_email.email' => 10,
1463 );
1464 }
1465 }
1466
b45c587e 1467 $this->openCiviPage('contact/deduperules', "action=update&id=$strictRuleId", '_qf_DedupeRules_next-bottom');
6a488035
TO
1468
1469 $count = 0;
1470 foreach ($fields as $field => $weight) {
1471 $this->select("where_{$count}", "value={$field}");
1472 $this->type("length_{$count}", '');
1473 $this->type("weight_{$count}", $weight);
1474 $count++;
1475 }
1476
1477 if ($count > 4) {
1478 $this->type('threshold', $threshold);
1479 // click save
1480 $this->click('_qf_DedupeRules_next-bottom');
1481 $this->waitForPageToLoad($this->getTimeoutMsec());
1482 return;
1483 }
1484
1485 for ($i = $count; $i <= 4; $i++) {
1486 $this->select("where_{$i}", 'label=- none -');
1487 $this->type("length_{$i}", '');
1488 $this->type("weight_{$i}", '');
1489 }
1490
1491 $this->type('threshold', $threshold);
1492
1493 // click save
1494 $this->click('_qf_DedupeRules_next-bottom');
1495 $this->waitForPageToLoad($this->getTimeoutMsec());
1496 }
1497
4cbe18b8
EM
1498 /**
1499 * @param string $period_type
1500 * @param int $duration_interval
1501 * @param string $duration_unit
1502 * @param string $auto_renew
bd265d68 1503 * @param int $minimumFee
1504 * @param string $financialType
4cbe18b8
EM
1505 *
1506 * @return array
1507 */
7a731220 1508 public function webtestAddMembershipType($period_type = 'rolling', $duration_interval = 1, $duration_unit = 'year', $auto_renew = 'no', $minimumFee = 100, $financialType = 'Member Dues') {
6a488035
TO
1509 $membershipTitle = substr(sha1(rand()), 0, 7);
1510 $membershipOrg = $membershipTitle . ' memorg';
1511 $this->webtestAddOrganization($membershipOrg, TRUE);
1512
1513 $title = 'Membership Type ' . substr(sha1(rand()), 0, 7);
1514 $memTypeParams = array(
1515 'membership_type' => $title,
1516 'member_of_contact' => $membershipOrg,
7a731220 1517 'financial_type' => $financialType,
6a488035
TO
1518 'period_type' => $period_type,
1519 );
1520
42daf119 1521 $this->openCiviPage("admin/member/membershipType/add", "action=add&reset=1", '_qf_MembershipType_cancel-bottom');
6a488035
TO
1522
1523 $this->type('name', $memTypeParams['membership_type']);
1524
1525 // if auto_renew optional or required - a valid payment processor must be created first (e.g Auth.net)
1526 // select the radio first since the element id changes after membership org search results are loaded
1527 switch ($auto_renew) {
1528 case 'optional':
1529 $this->click("xpath=//div[@id='membership_type_form']//table/tbody/tr[6]/td/label[contains(text(), 'Auto-renew Option')]/../../td[2]/label[contains(text(), 'Give option, but not required')]");
1530 break;
1531
1532 case 'required':
1533 $this->click("xpath=//div[@id='membership_type_form']//table/tbody/tr[6]/td/label[contains(text(), 'Auto-renew Option')]/../../td[2]/label[contains(text(), 'Auto-renew required')]");
1534 break;
1535
1536 default:
1537 //check if for the element presence (the Auto renew options can be absent when proper payment processor not configured)
1538 if ($this->isElementPresent("xpath=//div[@id='membership_type_form']//table/tbody/tr[6]/td/label[contains(text(), 'Auto-renew Option')]/../../td[2]/label[contains(text(), 'No auto-renew option')]")) {
1539 $this->click("xpath=//div[@id='membership_type_form']//table/tbody/tr[6]/td/label[contains(text(), 'Auto-renew Option')]/../../td[2]/label[contains(text(), 'No auto-renew option')]");
1540 }
1541 break;
1542 }
1543
6c6e6187 1544 $this->select2('member_of_contact_id', $membershipTitle);
6a488035 1545
4da07767 1546 $this->type('minimum_fee', $minimumFee);
7a731220 1547 $this->select('financial_type_id', "label={$memTypeParams['financial_type']}");
6a488035
TO
1548
1549 $this->type('duration_interval', $duration_interval);
1550 $this->select('duration_unit', "label={$duration_unit}");
1551
5ddf4885 1552 $this->select('period_type', "value={$period_type}");
6a488035
TO
1553
1554 $this->click('_qf_MembershipType_upload-bottom');
1555 $this->waitForElementPresent('link=Add Membership Type');
1556 $this->assertTrue($this->isTextPresent("The membership type '$title' has been saved."));
1557
1558 return $memTypeParams;
1559 }
1560
4cbe18b8
EM
1561 /**
1562 * @param null $groupName
1563 * @param null $parentGroupName
1564 *
1565 * @return null|string
1566 */
00be9182 1567 public function WebtestAddGroup($groupName = NULL, $parentGroupName = NULL) {
6a488035
TO
1568 $this->openCiviPage('group/add', 'reset=1', '_qf_Edit_upload-bottom');
1569
1570 // fill group name
1571 if (!$groupName) {
1572 $groupName = 'group_' . substr(sha1(rand()), 0, 7);
1573 }
1574 $this->type('title', $groupName);
1575
1576 // fill description
1577 $this->type('description', 'Adding new group.');
1578
1579 // check Access Control
1580 $this->click('group_type[1]');
1581
1582 // check Mailing List
1583 $this->click('group_type[2]');
1584
1585 // select Visibility as Public Pages
1586 $this->select('visibility', 'value=Public Pages');
1587
1588 // select parent group
1589 if ($parentGroupName) {
1590 $this->select('parents', "*$parentGroupName");
1591 }
1592
1593 // Clicking save.
225a8648 1594 $this->clickLink('_qf_Edit_upload-bottom');
6a488035
TO
1595
1596 // Is status message correct?
fc8cb638 1597 $this->waitForText('crm-notification-container', "The Group '$groupName' has been saved.");
6a488035
TO
1598 return $groupName;
1599 }
1600
4cbe18b8
EM
1601 /**
1602 * @param string $activityType
1603 *
1604 * @return null
1605 */
00be9182 1606 public function WebtestAddActivity($activityType = "Meeting") {
6a488035
TO
1607 // Adding Adding contact with randomized first name for test testContactContextActivityAdd
1608 // We're using Quick Add block on the main page for this.
1609 $firstName1 = substr(sha1(rand()), 0, 7);
1610 $this->webtestAddContact($firstName1, "Summerson", $firstName1 . "@summerson.name");
1611 $firstName2 = substr(sha1(rand()), 0, 7);
1612 $this->webtestAddContact($firstName2, "Anderson", $firstName2 . "@anderson.name");
1613
6a488035
TO
1614 $this->click("css=li#tab_activity a");
1615
1616 // waiting for the activity dropdown to show up
1617 $this->waitForElementPresent("other_activity");
1618
1619 // Select the activity type from the activity dropdown
1620 $this->select("other_activity", "label=Meeting");
1621
1622 $this->waitForElementPresent("_qf_Activity_upload-bottom");
44a0d8ea 1623 $this->waitForElementPresent("s2id_target_contact_id");
6a488035
TO
1624
1625 $this->assertTrue($this->isTextPresent("Anderson, " . $firstName2), "Contact not found in line " . __LINE__);
1626
1627 // Typing contact's name into the field (using typeKeys(), not type()!)...
44a0d8ea 1628 $this->select2("assignee_contact_id", $firstName1, TRUE);
6a488035
TO
1629
1630 // ...and verifying if the page contains properly formatted display name for chosen contact.
1631 $this->assertTrue($this->isTextPresent("Summerson, " . $firstName1), "Contact not found in line " . __LINE__);
1632
1633 // Putting the contents into subject field - assigning the text to variable, it'll come in handy later
1634 $subject = "This is subject of test activity being added through activity tab of contact summary screen.";
1635 // For simple input fields we can use field id as selector
1636 $this->type("subject", $subject);
1637 $this->type("location", "Some location needs to be put in this field.");
1638
1639 $this->webtestFillDateTime('activity_date_time', '+1 month 11:10PM');
1640
1641 // Setting duration.
1642 $this->type("duration", "30");
1643
1644 // Putting in details.
1645 $this->type("details", "Really brief details information.");
1646
1647 // Making sure that status is set to Scheduled (using value, not label).
1648 $this->select("status_id", "value=1");
1649
1650 // Setting priority.
1651 $this->select("priority_id", "value=1");
1652
1653 // Scheduling follow-up.
1654 $this->click("css=.crm-activity-form-block-schedule_followup div.crm-accordion-header");
1655 $this->select("followup_activity_type_id", "value=1");
1656 $this->webtestFillDateTime('followup_date', '+2 month 11:10PM');
1657 $this->type("followup_activity_subject", "This is subject of schedule follow-up activity");
1658
1659 // Clicking save.
1660 $this->click("_qf_Activity_upload-bottom");
44a0d8ea 1661 $this->waitForElementPresent("xpath=//div[@id='crm-notification-container']");
6a488035
TO
1662
1663 // Is status message correct?
44a0d8ea 1664 $this->waitForText('crm-notification-container', "Activity '$subject' has been saved.");
6a488035 1665
bf333c47 1666 $this->waitForElementPresent("xpath=//div[@class='dataTables_wrapper no-footer']//table/tbody/tr[2]/td[8]/span/a[text()='View']");
6a488035
TO
1667
1668 // click through to the Activity view screen
b3d40287 1669 $this->clickLinkSuppressPopup("xpath=//div[@class='dataTables_wrapper no-footer']//table/tbody/tr[2]/td[8]/span/a[text()='View']", '_qf_Activity_cancel-bottom');
76e86fd8 1670
efb29358
CW
1671 // parse URL to grab the activity id
1672 // pass id back to any other tests that call this class
1673 return $this->urlArg('id');
6a488035
TO
1674 }
1675
4cbe18b8
EM
1676 /**
1677 * @return bool
1678 */
3bdca100 1679 public static function checkDoLocalDBTest() {
6a488035
TO
1680 if (defined('CIVICRM_WEBTEST_LOCAL_DB') &&
1681 CIVICRM_WEBTEST_LOCAL_DB
1682 ) {
1683 require_once 'tests/phpunit/CiviTest/CiviDBAssert.php';
1684 return TRUE;
1685 }
1686 return FALSE;
1687 }
1688
1689 /**
eceb18cc 1690 * Generic function to compare expected values after an api call to retrieved.
6a488035
TO
1691 * DB values.
1692 *
fcb93467
EM
1693 * @param string $daoName
1694 * DAO Name of object we're evaluating.
1695 * @param int $id
1696 * Id of object
1697 * @param array $match
1698 * Associative array of field name => expected value. Empty if asserting
6a488035 1699 * that a DELETE occurred
3bdca100 1700 * @param bool $delete
fcb93467 1701 * are we checking that a DELETE action occurred?
6a488035 1702 */
00be9182 1703 public function assertDBState($daoName, $id, $match, $delete = FALSE) {
fcb93467
EM
1704 if (self::checkDoLocalDBTest()) {
1705 CiviDBAssert::assertDBState($this, $daoName, $id, $match, $delete);
6a488035 1706 }
6a488035
TO
1707 }
1708
4cbe18b8 1709 /**
4f1f1f2a 1710 * Request a record from the DB by seachColumn+searchValue. Success if a record is found.
c490a46a 1711 * @param string $daoName
08caad8e
EM
1712 * @param string $searchValue
1713 * @param string $returnColumn
1714 * @param string $searchColumn
1715 * @param string $message
4cbe18b8 1716 */
00be9182 1717 public function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) {
08caad8e
EM
1718 if (self::checkDoLocalDBTest()) {
1719 CiviDBAssert::assertDBNotNull($this, $daoName, $searchValue, $returnColumn, $searchColumn, $message);
6a488035 1720 }
6a488035
TO
1721 }
1722
4cbe18b8 1723 /**
08caad8e 1724 * Request a record from the DB by searchColumn+searchValue. Success if returnColumn value is NULL.
c490a46a 1725 * @param string $daoName
08caad8e
EM
1726 * @param string $searchValue
1727 * @param string $returnColumn
1728 * @param string $searchColumn
1729 * @param string $message
4cbe18b8 1730 */
00be9182 1731 public function assertDBNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) {
08caad8e
EM
1732 if (self::checkDoLocalDBTest()) {
1733 CiviDBAssert::assertDBNull($this, $daoName, $searchValue, $returnColumn, $searchColumn, $message);
6a488035 1734 }
6a488035
TO
1735 }
1736
4cbe18b8 1737 /**
4f1f1f2a 1738 * Request a record from the DB by id. Success if row not found.
c490a46a 1739 * @param string $daoName
100fef9d 1740 * @param int $id
9d06e7e6 1741 * @param string $message
4cbe18b8 1742 */
08caad8e
EM
1743 public function assertDBRowNotExist($daoName, $id, $message) {
1744 if (self::checkDoLocalDBTest()) {
1745 CiviDBAssert::assertDBRowNotExist($this, $daoName, $id, $message);
6a488035 1746 }
6a488035
TO
1747 }
1748
4cbe18b8 1749 /**
eceb18cc 1750 * Compare all values in a single retrieved DB record to an array of expected values.
c490a46a 1751 * @param string $daoName
100fef9d 1752 * @param array $searchParams
4cbe18b8
EM
1753 * @param $expectedValues
1754 */
00be9182 1755 public function assertDBCompareValues($daoName, $searchParams, $expectedValues) {
b7d77cba
EM
1756 if (self::checkDoLocalDBTest()) {
1757 CiviDBAssert::assertDBCompareValues($this, $daoName, $searchParams, $expectedValues);
6a488035 1758 }
6a488035
TO
1759 }
1760
4cbe18b8
EM
1761 /**
1762 * @param $expected
1763 * @param $actual
1764 * @param string $message
1765 */
00be9182 1766 public function assertType($expected, $actual, $message = '') {
b7d77cba 1767 $this->assertInternalType($expected, $actual, $message);
6a488035
TO
1768 }
1769
6a488035 1770 /**
eceb18cc 1771 * Add new Financial Account.
f0be539a
EM
1772 * @param $financialAccountTitle
1773 * @param bool $financialAccountDescription
1774 * @param bool $accountingCode
1775 * @param bool $firstName
1776 * @param bool $financialAccountType
1777 * @param bool $taxDeductible
1778 * @param bool $isActive
1779 * @param bool $isTax
1780 * @param bool $taxRate
1781 * @param bool $isDefault
6a488035 1782 */
3bdca100 1783 public function _testAddFinancialAccount(
5896d037
TO
1784 $financialAccountTitle,
1785 $financialAccountDescription = FALSE,
1786 $accountingCode = FALSE,
1787 $firstName = FALSE,
1788 $financialAccountType = FALSE,
1789 $taxDeductible = FALSE,
1790 $isActive = FALSE,
1791 $isTax = FALSE,
1792 $taxRate = FALSE,
1793 $isDefault = FALSE
6a488035
TO
1794 ) {
1795
b45c587e 1796 $this->openCiviPage("admin/financial/financialAccount", "reset=1");
6a488035
TO
1797
1798 $this->click("link=Add Financial Account");
1799 $this->waitForElementPresent('_qf_FinancialAccount_cancel-botttom');
1800
1801 // Financial Account Name
1802 $this->type('name', $financialAccountTitle);
1803
1804 // Financial Description
1805 if ($financialAccountDescription) {
1806 $this->type('description', $financialAccountDescription);
1807 }
1808
1809 //Accounting Code
1810 if ($accountingCode) {
1811 $this->type('accounting_code', $accountingCode);
1812 }
1813
1814 // Autofill Organization
1815 if ($firstName) {
1816 $this->webtestOrganisationAutocomplete($firstName);
1817 }
1818
1819 // Financial Account Type
1820 if ($financialAccountType) {
1821 $this->select('financial_account_type_id', "label={$financialAccountType}");
1822 }
1823
1824 // Is Tax Deductible
1825 if ($taxDeductible) {
1826 $this->check('is_deductible');
1827 }
1828 else {
1829 $this->uncheck('is_deductible');
1830 }
1831 // Is Active
1832 if (!$isActive) {
1833 $this->check('is_active');
1834 }
1835 else {
1836 $this->uncheck('is_active');
1837 }
1838 // Is Tax
1839 if ($isTax) {
1840 $this->check('is_tax');
1841 }
1842 else {
1843 $this->uncheck('is_tax');
1844 }
1845 // Tax Rate
1846 if ($taxRate) {
1847 $this->type('tax_rate', $taxRate);
1848 }
1849
1850 // Set Default
1851 if ($isDefault) {
1852 $this->check('is_default');
1853 }
1854 else {
1855 $this->uncheck('is_default');
1856 }
1857 $this->click('_qf_FinancialAccount_next-botttom');
6a488035
TO
1858 }
1859
6a488035 1860 /**
eceb18cc 1861 * Edit Financial Account.
1e1fdcf6
EM
1862 * @param $editfinancialAccount
1863 * @param bool $financialAccountTitle
1864 * @param bool $financialAccountDescription
1865 * @param bool $accountingCode
1866 * @param bool $firstName
1867 * @param bool $financialAccountType
1868 * @param bool $taxDeductible
1869 * @param bool $isActive
1870 * @param bool $isTax
1871 * @param bool $taxRate
1872 * @param bool $isDefault
6a488035 1873 */
3bdca100 1874 public function _testEditFinancialAccount(
5896d037 1875 $editfinancialAccount,
92915c55
TO
1876 $financialAccountTitle = FALSE,
1877 $financialAccountDescription = FALSE,
1878 $accountingCode = FALSE,
1879 $firstName = FALSE,
1880 $financialAccountType = FALSE,
1881 $taxDeductible = FALSE,
1882 $isActive = TRUE,
1883 $isTax = FALSE,
1884 $taxRate = FALSE,
1885 $isDefault = FALSE
6a488035
TO
1886 ) {
1887 if ($firstName) {
b45c587e 1888 $this->openCiviPage("admin/financial/financialAccount", "reset=1");
6a488035
TO
1889 }
1890
4a058f26
WA
1891 $this->waitForElementPresent("xpath=//table/tbody//tr/td[1]/div[text()='{$editfinancialAccount}']/../../td[9]/span/a[text()='Edit']");
1892 $this->clickLink("xpath=//table/tbody//tr/td[1]/div[text()='{$editfinancialAccount}']/../../td[9]/span/a[text()='Edit']", '_qf_FinancialAccount_cancel-botttom', FALSE);
6a488035
TO
1893
1894 // Change Financial Account Name
1895 if ($financialAccountTitle) {
1896 $this->type('name', $financialAccountTitle);
1897 }
1898
1899 // Financial Description
1900 if ($financialAccountDescription) {
1901 $this->type('description', $financialAccountDescription);
1902 }
1903
1904 //Accounting Code
1905 if ($accountingCode) {
1906 $this->type('accounting_code', $accountingCode);
1907 }
1908
6a488035
TO
1909 // Autofill Edit Organization
1910 if ($firstName) {
1911 $this->webtestOrganisationAutocomplete($firstName);
1912 }
1913
1914 // Financial Account Type
1915 if ($financialAccountType) {
1916 $this->select('financial_account_type_id', "label={$financialAccountType}");
1917 }
1918
1919 // Is Tax Deductible
1920 if ($taxDeductible) {
1921 $this->check('is_deductible');
1922 }
1923 else {
1924 $this->uncheck('is_deductible');
1925 }
1926
1927 // Is Tax
1928 if ($isTax) {
1929 $this->check('is_tax');
1930 }
1931 else {
1932 $this->uncheck('is_tax');
1933 }
1934
1935 // Tax Rate
1936 if ($taxRate) {
1937 $this->type('tax_rate', $taxRate);
1938 }
1939
1940 // Set Default
1941 if ($isDefault) {
1942 $this->check('is_default');
1943 }
1944 else {
1945 $this->uncheck('is_default');
1946 }
1947
1948 // Is Active
1949 if ($isActive) {
1950 $this->check('is_active');
1951 }
1952 else {
1953 $this->uncheck('is_active');
1954 }
1955 $this->click('_qf_FinancialAccount_next-botttom');
6cbe4dc3 1956 $this->waitForElementPresent('link=Add Financial Account');
6a488035
TO
1957 }
1958
6a488035 1959 /**
eceb18cc 1960 * Delete Financial Account.
1e1fdcf6 1961 * @param $financialAccountTitle
6a488035 1962 */
00be9182 1963 public function _testDeleteFinancialAccount($financialAccountTitle) {
4a058f26 1964 $this->click("xpath=//table/tbody//tr/td[1]/div[text()='{$financialAccountTitle}']/../../td[9]/span/a[text()='Delete']");
6a488035
TO
1965 $this->waitForElementPresent('_qf_FinancialAccount_next-botttom');
1966 $this->click('_qf_FinancialAccount_next-botttom');
1967 $this->waitForElementPresent('link=Add Financial Account');
6cbe4dc3 1968 $this->waitForText('crm-notification-container', "Selected Financial Account has been deleted.");
6a488035
TO
1969 }
1970
1971 /**
eceb18cc 1972 * Verify data after ADD and EDIT.
1e1fdcf6 1973 * @param $verifyData
6a488035 1974 */
00be9182 1975 public function _assertFinancialAccount($verifyData) {
6a488035
TO
1976 foreach ($verifyData as $key => $expectedValue) {
1977 $actualValue = $this->getValue($key);
1978 if ($key == 'parent_financial_account') {
1979 $this->assertTrue((bool) preg_match("/^{$expectedValue}/", $actualValue));
1980 }
1981 else {
1982 $this->assertEquals($expectedValue, $actualValue);
1983 }
1984 }
1985 }
1986
4cbe18b8
EM
1987 /**
1988 * @param $verifySelectFieldData
1989 */
00be9182 1990 public function _assertSelectVerify($verifySelectFieldData) {
6a488035
TO
1991 foreach ($verifySelectFieldData as $key => $expectedvalue) {
1992 $actualvalue = $this->getSelectedLabel($key);
1993 $this->assertEquals($expectedvalue, $actualvalue);
1994 }
1995 }
1996
4cbe18b8
EM
1997 /**
1998 * @param $financialType
1999 * @param string $option
2000 */
00be9182 2001 public function addeditFinancialType($financialType, $option = 'new') {
b45c587e 2002 $this->openCiviPage("admin/financial/financialType", "reset=1");
6a488035
TO
2003
2004 if ($option == 'Delete') {
4a058f26 2005 $this->click("xpath=id('ltype')/div/table/tbody/tr/td[1]/div[text()='$financialType[name]']/../../td[7]/span[2]");
6a488035 2006 $this->waitForElementPresent("css=span.btn-slide-active");
4a058f26 2007 $this->click("xpath=id('ltype')/div/table/tbody/tr/td[1]/div[text()='$financialType[name]']/../../td[7]/span[2]/ul/li[2]/a");
6a488035
TO
2008 $this->waitForElementPresent("_qf_FinancialType_next");
2009 $this->click("_qf_FinancialType_next");
6cbe4dc3
AS
2010 $this->waitForElementPresent("newFinancialType");
2011 $this->waitForText('crm-notification-container', 'Selected financial type has been deleted.');
6a488035
TO
2012 return;
2013 }
2014 if ($option == 'new') {
2015 $this->click("link=Add Financial Type");
2016 }
2017 else {
4a058f26 2018 $this->click("xpath=id('ltype')/div/table/tbody/tr/td[1]/div[text()='$financialType[oldname]']/../../td[7]/span/a[text()='Edit']");
6a488035 2019 }
c1d1bf14 2020 $this->waitForElementPresent("name");
6a488035
TO
2021 $this->type('name', $financialType['name']);
2022 if ($option == 'new') {
2023 $this->type('description', $financialType['name'] . ' description');
2024 }
2025
2026 if ($financialType['is_reserved']) {
2027 $this->check('is_reserved');
2028 }
2029 else {
2030 $this->uncheck('is_reserved');
2031 }
2032
2033 if ($financialType['is_deductible']) {
2034 $this->check('is_deductible');
2035 }
2036 else {
2037 $this->uncheck('is_deductible');
2038 }
2039
2040 $this->click('_qf_FinancialType_next');
6a488035 2041 if ($option == 'new') {
ba6f320f 2042 $text = "Your Financial \"{$financialType['name']}\" Type has been created, along with a corresponding income account \"{$financialType['name']}\". That income account, along with standard financial accounts \"Accounts Receivable\", \"Banking Fees\" and \"Premiums\" have been linked to the financial type. You may edit or replace those relationships here.";
6a488035
TO
2043 }
2044 else {
c51f8adb 2045 $text = "The financial type \"{$financialType['name']}\" has been updated.";
6a488035 2046 }
5c88df60 2047 $this->checkCRMAlert($text);
6a488035
TO
2048 }
2049
42daf119 2050 /**
eceb18cc 2051 * Give the specified permissions.
b45c587e 2052 * Note: this function logs in as 'admin' (logging out if necessary)
1e1fdcf6 2053 * @param $permission
42daf119 2054 */
00be9182 2055 public function changePermissions($permission) {
42daf119
CW
2056 $this->webtestLogin('admin');
2057 $this->open("{$this->sboxPath}admin/people/permissions");
6a488035 2058 $this->waitForElementPresent('edit-submit');
42daf119
CW
2059 foreach ((array) $permission as $perm) {
2060 $this->check($perm);
6a488035
TO
2061 }
2062 $this->click('edit-submit');
2063 $this->waitForPageToLoad($this->getTimeoutMsec());
2064 $this->assertTrue($this->isTextPresent('The changes have been saved.'));
6a488035
TO
2065 }
2066
4cbe18b8
EM
2067 /**
2068 * @param $profileTitle
2069 * @param $profileFields
2070 */
00be9182 2071 public function addProfile($profileTitle, $profileFields) {
42daf119 2072 $this->openCiviPage('admin/uf/group', "reset=1");
6a488035 2073
39a2888d 2074 $this->clickLink('link=Add Profile', '_qf_Group_cancel-bottom');
6a488035 2075 $this->type('title', $profileTitle);
7df6dc24 2076 $this->clickLink('_qf_Group_next-bottom');
6a488035 2077
39a2888d 2078 $this->waitForText('crm-notification-container', "Your CiviCRM Profile '{$profileTitle}' has been added. You can add fields to this profile now.");
6a488035
TO
2079
2080 foreach ($profileFields as $field) {
2081 $this->waitForElementPresent('field_name_0');
6a488035
TO
2082 $this->click("id=field_name_0");
2083 $this->select("id=field_name_0", "label=" . $field['type']);
2084 $this->waitForElementPresent('field_name_1');
2085 $this->click("id=field_name_1");
2086 $this->select("id=field_name_1", "label=" . $field['name']);
2087 $this->waitForElementPresent('label');
2088 $this->type("id=label", $field['label']);
2089 $this->click("id=_qf_Field_next_new-top");
bf2c3b74 2090 $this->waitForElementPresent("xpath=//select[@id='field_name_1'][@style='display: none;']");
6a488035
TO
2091 //$this->assertTrue($this->isTextPresent("Your CiviCRM Profile Field '" . $field['name'] . "' has been saved to '" . $profileTitle . "'. You can add another profile field."));
2092 }
2093 }
2094
4cbe18b8 2095 /**
100fef9d 2096 * @param string $name
4cbe18b8
EM
2097 * @param $sku
2098 * @param $amount
2099 * @param $price
2100 * @param $cost
2101 * @param $financialType
2102 */
00be9182 2103 public function addPremium($name, $sku, $amount, $price, $cost, $financialType) {
022def1d 2104 $this->waitForElementPresent("_qf_ManagePremiums_upload-bottom");
6a488035
TO
2105 $this->type("name", $name);
2106 $this->type("sku", $sku);
2107 $this->click("CIVICRM_QFID_noImage_16");
2108 $this->type("min_contribution", $amount);
2109 $this->type("price", $price);
2110 $this->type("cost", $cost);
2111 if ($financialType) {
2112 $this->select("financial_type_id", "label={$financialType}");
2113 }
022def1d 2114 $this->click("_qf_ManagePremiums_upload-bottom");
6a488035
TO
2115 $this->waitForPageToLoad($this->getTimeoutMsec());
2116 }
2117
4cbe18b8
EM
2118 /**
2119 * @param $label
2120 * @param $financialAccount
2121 */
00be9182 2122 public function addPaymentInstrument($label, $financialAccount) {
118e964e 2123 $this->openCiviPage('admin/options/payment_instrument', 'action=add&reset=1', "_qf_Options_next-bottom");
6a488035 2124 $this->type("label", $label);
45d22844 2125 $this->type("value", "value" . $label);
6a488035
TO
2126 $this->select("financial_account_id", "value=$financialAccount");
2127 $this->click("_qf_Options_next-bottom");
2128 $this->waitForPageToLoad($this->getTimeoutMsec());
2129 }
2130
5ff68892 2131 /**
eceb18cc 2132 * Ensure we have a default mailbox set up for CiviMail.
5ff68892 2133 */
00be9182 2134 public function setupDefaultMailbox() {
5ff68892
CW
2135 $this->openCiviPage('admin/mailSettings', 'action=update&id=1&reset=1');
2136 // Check if it hasn't already been set up
2137 if (!$this->getSelectedValue('protocol')) {
2138 $this->type('name', 'Test Domain');
2139 $this->select('protocol', "IMAP");
2140 $this->type('server', 'localhost');
2141 $this->type('domain', 'example.com');
e739afc3 2142 $this->clickLink('_qf_MailSettings_next-top');
5ff68892
CW
2143 }
2144 }
2145
6a488035
TO
2146 /**
2147 * Determine the default time-out in milliseconds.
2148 *
2149 * @return string, timeout expressed in milliseconds
2150 */
00be9182 2151 public function getTimeoutMsec() {
6a488035
TO
2152 // note: existing local versions of CiviSeleniumSettings may not declare $timeout, so use @
2153 $timeout = ($this->settings && @$this->settings->timeout) ? ($this->settings->timeout * 1000) : 30000;
2154 return (string) $timeout; // don't know why, but all our old code used a string
2155 }
6a488035 2156
c4e6d4e8
PJ
2157 /**
2158 * CRM-12378
2159 * checks custom fields rendering / loading properly on the fly WRT entity passed as parameter
2160 *
2161 *
e16033b4
TO
2162 * @param array $customSets
2163 * Custom sets i.e entity wise sets want to be created and checked.
16b10e64 2164 * e.g $customSets = array(array('entity' => 'Contribution', 'subEntity' => 'Donation',
5896d037
TO
2165 * 'triggerElement' => $triggerElement))
2166 * array $triggerElement: the element which is responsible for custom group to load
2167 *
2168 * which uses the entity info as its selection value
e16033b4
TO
2169 * @param array $pageUrl
2170 * The url which on which the ajax custom group load takes place.
b7d77cba 2171 * @param string $beforeTriggering
c4e6d4e8
PJ
2172 * @return void
2173 */
00be9182 2174 public function customFieldSetLoadOnTheFlyCheck($customSets, $pageUrl, $beforeTriggering = NULL) {
a40669b6
CW
2175 // FIXME: Testing a theory that these failures have something to do with permissions
2176 $this->webtestLogin('admin');
2177
c4e6d4e8
PJ
2178 //add the custom set
2179 $return = $this->addCustomGroupField($customSets);
2180
87f534f9 2181 // FIXME: Hack to ensure caches are properly cleared
dc499911
CW
2182 if (TRUE) {
2183 $userName = $this->loggedInAs;
e42c089b 2184 $this->webtestLogout();
dc499911 2185 $this->webtestLogin($userName);
e42c089b
CW
2186 }
2187
87f534f9 2188 $this->openCiviPage($pageUrl['url'], $pageUrl['args']);
b50973e8
CW
2189
2190 // FIXME: Try to find out what the heck is going on with these tests
2191 $this->waitForAjaxContent();
2192 $this->checkForErrorsOnPage();
2193
5896d037 2194 foreach ($return as $values) {
c4e6d4e8
PJ
2195 foreach ($values as $entityType => $customData) {
2196 //initiate necessary variables
2197 list($entity, $entityData) = explode('_', $entityType);
2198 $elementType = CRM_Utils_Array::value('type', $customData['triggerElement'], 'select');
2199 $elementName = CRM_Utils_Array::value('name', $customData['triggerElement']);
e42c089b 2200 if (is_callable($beforeTriggering)) {
5b8ddc60
PJ
2201 call_user_func($beforeTriggering);
2202 }
c4e6d4e8
PJ
2203 if ($elementType == 'select') {
2204 //reset the select box, so triggering of ajax only happens
2205 //WRT input of value in this function
2206 $this->select($elementName, "index=0");
2207 }
2208 if (!empty($entityData)) {
2209 if ($elementType == 'select') {
2210 $this->select($elementName, "label=regexp:{$entityData}");
2211 }
2212 elseif ($elementType == 'checkbox') {
2213 $val = explode(',', $entityData);
5896d037 2214 foreach ($val as $v) {
c4e6d4e8
PJ
2215 $checkId = $this->getAttribute("xpath=//label[text()='{$v}']/@for");
2216 $this->check($checkId);
2217 }
2218 }
16345393
AS
2219 elseif ($elementType == 'select2') {
2220 $this->select2($elementName, $entityData);
2221 }
c4e6d4e8 2222 }
a2cfaf9e
CW
2223 // FIXME: Try to find out what the heck is going on with these tests
2224 $this->waitForAjaxContent();
2225 $this->checkForErrorsOnPage();
a40669b6 2226
c4e6d4e8 2227 //checking for proper custom data which is loading through ajax
a40669b6 2228 $this->waitForElementPresent("css=.custom-group-{$customData['cgtitle']}");
16345393 2229 $this->assertElementPresent("xpath=//div[contains(@class, 'custom-group-{$customData['cgtitle']}')]/div[contains(@class, 'crm-accordion-body')]/table/tbody/tr/td[2]/input",
c4e6d4e8
PJ
2230 "The on the fly custom group field is not present for entity : {$entity} => {$entityData}");
2231 }
2232 }
2233 }
2234
4cbe18b8
EM
2235 /**
2236 * @param $customSets
2237 *
2238 * @return array
2239 */
00be9182 2240 public function addCustomGroupField($customSets) {
57d32317 2241 $return = array();
c4e6d4e8
PJ
2242 foreach ($customSets as $customSet) {
2243 $this->openCiviPage("admin/custom/group", "action=add&reset=1");
2244
2245 //fill custom group title
2246 $customGroupTitle = "webtest_for_ajax_cd" . substr(sha1(rand()), 0, 4);
2247 $this->click("title");
2248 $this->type("title", $customGroupTitle);
2249
2250 //custom group extends
2251 $this->click("extends_0");
2252 $this->select("extends_0", "value={$customSet['entity']}");
2253 if (!empty($customSet['subEntity'])) {
2254 $this->addSelection("extends_1", "label={$customSet['subEntity']}");
2255 }
2256
2257 // Don't collapse
2258 $this->uncheck('collapse_display');
2259
2260 // Save
2261 $this->click('_qf_Group_next-bottom');
c4e6d4e8
PJ
2262
2263 //Is custom group created?
2264 $this->waitForText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added.");
5dac229e 2265
c4e6d4e8 2266 $gid = $this->urlArg('gid');
5dac229e 2267 $this->waitForTextPresent("{$customGroupTitle} - New Field");
c4e6d4e8
PJ
2268
2269 $fieldLabel = "custom_field_for_{$customSet['entity']}_{$customSet['subEntity']}" . substr(sha1(rand()), 0, 4);
c3ad8633 2270 $this->waitForElementPresent('label');
c4e6d4e8 2271 $this->type('label', $fieldLabel);
50d95825 2272 $this->click('_qf_Field_done-bottom');
c4e6d4e8 2273
c3ad8633
CW
2274 $this->waitForText('crm-notification-container', $fieldLabel);
2275 $this->waitForAjaxContent();
2276
2277 $customGroupTitle = preg_replace('/\s/', '_', trim($customGroupTitle));
c4e6d4e8 2278 $return[] = array(
5896d037
TO
2279 "{$customSet['entity']}_{$customSet['subEntity']}" => array(
2280 'cgtitle' => $customGroupTitle,
2281 'gid' => $gid,
21dfd5f5
TO
2282 'triggerElement' => $customSet['triggerElement'],
2283 ),
5896d037 2284 );
e739afc3
CW
2285
2286 // Go home for a sec to give time for caches to clear
2287 $this->openCiviPage('');
c4e6d4e8
PJ
2288 }
2289 return $return;
2290 }
5320adf4
WA
2291
2292 /**
e4f46be0 2293 * Type and select first occurrence of autocomplete.
ea3ddccf 2294 *
1e1fdcf6 2295 * @param $fieldName
ea3ddccf 2296 * @param string $labels
1e1fdcf6
EM
2297 * @param bool $multiple
2298 * @param bool $xpath
5320adf4 2299 */
0b49a3ec 2300 public function select2($fieldName, $labels, $multiple = FALSE, $xpath = FALSE) {
57d32317
CW
2301 // In the case of chainSelect, wait for options to load
2302 $this->waitForElementNotPresent('css=select.loading');
2af8120a 2303 if ($multiple) {
761fe7bb 2304 foreach ((array) $labels as $label) {
0b49a3ec 2305 $this->clickAt("//*[@id='$fieldName']/../div/ul/li");
2306 $this->keyDown("//*[@id='$fieldName']/../div/ul/li//input", " ");
2307 $this->type("//*[@id='$fieldName']/../div/ul/li//input", $label);
2308 $this->typeKeys("//*[@id='$fieldName']/../div/ul/li//input", $label);
2309 $this->waitForElementPresent("//*[@class='select2-result-label']");
2310 $this->clickAt("//*[contains(@class,'select2-result-selectable')]/div[contains(@class, 'select2-result-label')]");
2311 }
2af8120a
AS
2312 }
2313 else {
3e60ff7f
JP
2314 if ($xpath) {
2315 $this->clickAt($fieldName);
2316 }
2317 else {
2318 $this->clickAt("//*[@id='$fieldName']/../div/a");
2319 }
2af8120a
AS
2320 $this->waitForElementPresent("//*[@id='select2-drop']/div/input");
2321 $this->keyDown("//*[@id='select2-drop']/div/input", " ");
0b49a3ec 2322 $this->type("//*[@id='select2-drop']/div/input", $labels);
2323 $this->typeKeys("//*[@id='select2-drop']/div/input", $labels);
2af8120a 2324 $this->waitForElementPresent("//*[@class='select2-result-label']");
e9322446 2325 $this->clickAt("//*[contains(@class,'select2-result-selectable')]/div[contains(@class, 'select2-result-label')]");
2af8120a 2326 }
e739afc3
CW
2327 // Wait a sec for select2 to update the original element
2328 sleep(1);
5320adf4 2329 }
f1690d4a 2330
2331 /**
eceb18cc 2332 * Select multiple options.
8a65f8da 2333 * @param $fieldId
2334 * @param $options
06dfe3b5 2335 * @param $isDate if multiple date is to be selected from datepicker
f1690d4a 2336 */
8a65f8da 2337 public function multiselect2($fieldId, $options, $isDate = FALSE) {
50d95825
CW
2338 // In the case of chainSelect, wait for options to load
2339 $this->waitForElementNotPresent('css=select.loading');
8a65f8da 2340 foreach ($options as $value) {
06dfe3b5 2341 if ($isDate) {
8a65f8da 2342 $this->clickAt("xpath=//*[@id='$fieldId']/../div/ul//li/input");
2343 $this->webtestFillDate($fieldId, $value, TRUE);
06dfe3b5 2344 }
2345 else {
8a65f8da 2346 $this->clickAt("xpath=//*[@id='$fieldId']/../div/ul//li/input");
06dfe3b5 2347 $this->waitForElementPresent("xpath=//ul[@class='select2-results']");
2348 $this->clickAt("xpath=//ul[@class='select2-results']//li/div[text()='$value']");
8a65f8da 2349 $this->assertElementContainsText("xpath=//*[@id='$fieldId']/preceding-sibling::div[1]/", $value);
06dfe3b5 2350 }
f1690d4a 2351 }
e739afc3
CW
2352 // Wait a sec for select2 to update the original element
2353 sleep(1);
f1690d4a 2354 }
c41b442d
CW
2355
2356 /**
5c88df60 2357 * Check for unobtrusive status message as set by CRM.status
1e1fdcf6 2358 * @param null $text
c41b442d 2359 */
6c6e6187 2360 public function checkCRMStatus($text = NULL) {
c41b442d 2361 $this->waitForElementPresent("css=.crm-status-box-outer.status-success");
5c88df60
CW
2362 if ($text) {
2363 $this->assertElementContainsText("css=.crm-status-box-outer.status-success", $text);
2364 }
c41b442d 2365 }
5c88df60 2366
dcba1580 2367 /**
5c88df60 2368 * Check for obtrusive status message as set by CRM.alert
1e1fdcf6
EM
2369 * @param $text
2370 * @param string $type
dcba1580 2371 */
6c6e6187 2372 public function checkCRMAlert($text, $type = 'success') {
5c88df60
CW
2373 $this->waitForElementPresent("css=div.ui-notify-message.$type");
2374 $this->waitForText("css=div.ui-notify-message.$type", $text);
9c5c0056
CW
2375 // We got the message, now let's close it so the webtest doesn't get confused by lots of open alerts
2376 $this->click('css=.ui-notify-cross');
dcba1580 2377 }
5c88df60 2378
d8d3508a 2379 /**
100fef9d 2380 * Enable or disable Pop-ups via Display Preferences
b7d77cba 2381 * @param bool $enabled
d8d3508a 2382 */
00be9182 2383 public function enableDisablePopups($enabled = TRUE) {
d8d3508a
DG
2384 $this->openCiviPage('admin/setting/preferences/display', 'reset=1');
2385 $isChecked = $this->isChecked('ajaxPopupsEnabled');
2386 if (($isChecked && !$enabled) || (!$isChecked && $enabled)) {
6c6e6187 2387 $this->click('ajaxPopupsEnabled');
d8d3508a
DG
2388 }
2389 if ($enabled) {
5c88df60 2390 $this->assertChecked('ajaxPopupsEnabled');
d8d3508a
DG
2391 }
2392 else {
2393 $this->assertNotChecked('ajaxPopupsEnabled');
2394 }
c41b442d 2395 $this->clickLink("_qf_Display_next-bottom");
d8d3508a 2396 }
b50973e8
CW
2397
2398 /**
eceb18cc 2399 * Attempt to get information about what went wrong if we encounter an error when loading a page.
b50973e8 2400 */
00be9182 2401 public function checkForErrorsOnPage() {
b50973e8 2402 foreach (array('Access denied', 'Page not found') as $err) {
037454ae
CW
2403 if ($this->isElementPresent("xpath=//h1[contains(., '$err')]")) {
2404 $this->fail("'$err' encountered at " . $this->getLocation() . "\nwhile logged in as '{$this->loggedInAs}'");
b50973e8
CW
2405 }
2406 }
2407 if ($this->isElementPresent("xpath=//span[text()='Sorry but we are not able to provide this at the moment.']")) {
2408 $msg = '"Fatal Error" encountered at ' . $this->getLocation();
2409 if ($this->isElementPresent('css=div.crm-section.crm-error-message')) {
2410 $msg .= "\nError Message: " . $this->getText('css=div.crm-section.crm-error-message');
2411 }
2412 $this->fail($msg);
2413 }
2414 }
96025800 2415
ef9cbdb7
TO
2416 /**
2417 * @return array
2418 * Contact record (per APIv3).
2419 */
2420 public function webtestGetLoggedInContact() {
2421 $result = $this->rest_civicrm_api('Contact', 'get', array(
2422 'id' => 'user_contact_id',
2423 ));
2424 $this->assertAPISuccess($result, 'Load logged-in contact');
2425 return CRM_Utils_Array::first($result['values']);
2426 }
2427
2428 public function assertAPISuccess($apiResult, $prefix = '') {
2429 if (!empty($prefix)) {
2430 $prefix .= ': ';
2431 }
2432 $errorMessage = empty($apiResult['error_message']) ? '' : " " . $apiResult['error_message'];
2433
2434 if (!empty($apiResult['debug_information'])) {
2435 $errorMessage .= "\n " . print_r($apiResult['debug_information'], TRUE);
2436 }
2437 if (!empty($apiResult['trace'])) {
2438 $errorMessage .= "\n" . print_r($apiResult['trace'], TRUE);
2439 }
2440 $this->assertFalse(civicrm_error($apiResult), $prefix . $errorMessage);
2441 //$this->assertEquals(0, $apiResult['is_error']);
2442 }
2443
7051f2e5
WA
2444 /**
2445 * Add a pledge
2446 *
2447 * @return array
2448 */
2449 public function webtestStandalonePledgeAdd() {
2450 $this->webtestLogin();
2451
2452 $this->openCiviPage('pledge/add', 'reset=1&context=standalone', '_qf_Pledge_upload');
2453
2454 // create new contact using dialog
2455 $contact = $this->createDialogContact();
2456
2457 $this->type('amount', '100');
2458 $this->type('installments', '10');
2459 $this->select('frequency_unit', 'value=week');
2460 $this->type('frequency_day', '2');
2461
2462 $this->webtestFillDate('acknowledge_date', 'now');
2463
2464 $this->select('contribution_page_id', 'value=3');
2465
2466 //PaymentReminders
2467 $this->click('PaymentReminders');
2468 $this->waitForElementPresent('additional_reminder_day');
2469 $this->type('initial_reminder_day', '4');
2470 $this->type('max_reminders', '2');
2471 $this->type('additional_reminder_day', '4');
2472
2473 $this->click('_qf_Pledge_upload-bottom');
2474
2475 $this->waitForText('crm-notification-container', "Pledge has been recorded and the payment schedule has been created.");
2476
2477 // verify if Pledge is created
2478 $this->waitForElementPresent("xpath=//div[@class='view-content']//table//tbody/tr[1]/td[10]/span/a[text()='View']");
2479
2480 //click through to the Pledge view screen
2481 $this->click("xpath=//div[@class='view-content']//table//tbody/tr[1]/td[10]/span/a[text()='View']");
2482 $this->waitForElementPresent('_qf_PledgeView_next-bottom');
2483 $pledgeDate = date('F jS, Y', strtotime('now'));
2484
2485 $this->webtestVerifyTabularData(array(
2486 'Pledge By' => $contact['display_name'],
2487 'Total Pledge Amount' => '$ 100.00',
2488 'To be paid in' => '10 installments of $ 10.00 every 1 week(s)',
2489 'Payments are due on the' => '2 day of the period',
2490 'Pledge Made' => $pledgeDate,
2491 'Financial Type' => 'Donation',
2492 'Pledge Status' => 'Pending',
2493 'Initial Reminder Day' => '4 days prior to schedule date',
2494 'Maximum Reminders Send' => 2,
2495 'Send additional reminders' => '4 days after the last one sent',
2496 )
2497 );
e2414672 2498 $this->clickLink('_qf_PledgeView_next-bottom', "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[1]/a", FALSE);
7051f2e5 2499 $this->waitForAjaxContent();
e2414672 2500 $this->click("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[1]/a");
2501 $this->waitForElementPresent("xpath=//div[@class='view-content']//table//tbody/tr[2]/td/div/table/tbody/tr[2]/td[8]/a[text()='Record Payment']");
7051f2e5
WA
2502 return $contact;
2503 }
2504
b9fdba0a
WA
2505 /**
2506 * Add a contact via profile
2507 */
2508 public function webtestAddViaCreateProfile() {
2509 $this->webtestLogin();
2510
2511 $this->openCiviPage('profile/create', 'reset=1&gid=1', '_qf_Edit_next');
2512
2513 $firstName = 'Jo' . substr(sha1(rand()), 0, 4);
2514 $lastName = 'Ad' . substr(sha1(rand()), 0, 7);
2515
2516 //contact details section
2517 //fill in first name
2518 $this->type("first_name", $firstName);
2519
2520 //fill in last name
2521 $this->type("last_name", $lastName);
2522
2523 //address section
9fdcdf6f 2524 $this->waitForElementPresent('street_address-1');
b9fdba0a 2525 $this->type("street_address-1", "902C El Camino Way SW");
9fdcdf6f 2526 $this->waitForElementPresent('city-1');
b9fdba0a
WA
2527 $this->type("city-1", "Dumfries");
2528 $this->type("postal_code-1", "1234");
2529 $this->assertSelected('country-1', "UNITED STATES");
2530 $this->select("state_province-1", "value=1019");
2531
2532 // Clicking save.
2533 $this->click("_qf_Edit_next");
2534 $this->waitForPageToLoad($this->getTimeoutMsec());
2535
2536 $this->assertElementContainsText('css=.msg-text', "Your information has been saved.");
2537 }
2538
65c1908a 2539}