commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / tests / phpunit / WebTest / Contact / InlineFieldsEditTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
28
29 /**
30 * Class WebTest_Contact_InlineFieldsEditTest
31 */
32 class WebTest_Contact_InlineFieldsEditTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testAddAndEditField() {
39 $this->webtestLogin();
40
41 // Add a contact
42 $firstName = 'WebTest' . substr(sha1(rand()), 0, 7);
43 $lastName = 'InlineFieldsEdit' . substr(sha1(rand()), 0, 7);
44 $this->webtestAddContact($firstName, $lastName);
45 $contactId = $this->urlArg('cid');
46 $this->waitForElementPresent('css=.crm-inline-edit-container.crm-edit-ready');
47
48 // Set Communication Prefs
49 $this->inlineEdit('crm-communication-pref-content', array(
50 'email_greeting_id' => TRUE,
51 'privacy_do_not_email' => 1,
52 'preferred_communication_method_1' => 1,
53 'preferred_communication_method_2' => 1,
54 ), 'keep_open');
55 $this->waitForElementPresent('css=.icon.privacy-flag.do-not-email');
56 $this->inlineEdit('crm-communication-pref-content', array(
57 'privacy_do_not_phone' => 1,
58 'privacy_do_not_email' => 0,
59 'preferred_communication_method_1' => 0,
60 'preferred_communication_method_2' => 0,
61 ), 'keep_open');
62 $this->waitForElementPresent('css=.icon.privacy-flag.do-not-phone');
63 $this->inlineEdit('crm-communication-pref-content', array(
64 'email_greeting_custom' => 'Hey You!',
65 ), 'no_open');
66 $this->assertElementNotPresent('css=.icon.privacy-flag.do-not-email');
67
68 // Custom data
69 $this->click('css=div.crm-custom-set-block-1 .collapsible-title');
70 // Because it tends to cause problems, all uses of sleep() must be justified in comments
71 // NOTE: Sleep should never be used for wait for anything to load from the server
72 // Justification for this instance: opening an accordion is predictable
73 sleep(1);
74 $this->openInlineForm('custom-set-content-1');
75 $dateFieldId = $this->getAttribute("xpath=//div[@class='crm-accordion-body']/table/tbody/tr[3]/td[@class='html-adjust']/input@id");
76 $this->inlineEdit('custom-set-content-1', array(
77 'CIVICRM_QFID_Edu_2' => 1,
78 "//div[@class='crm-accordion-body']/table/tbody/tr[2]/td[@class='html-adjust']/select" => array('Single'),
79 $dateFieldId => 'date: now - 10 years',
80 ));
81
82 // Edit contact info
83 $params = array(
84 'job_title' => 'jobtest123',
85 'nick_name' => 'nicktest123',
86 'contact_source' => 'sourcetest123',
87 );
88 $this->inlineEdit('crm-contactinfo-content', $params, 'keep_open');
89 // Clear fields and verify they are deleted
90 $this->inlineEdit('crm-contactinfo-content', array(
91 'job_title' => '',
92 'nick_name' => '',
93 'contact_source' => '',
94 ), 'no_open');
95 foreach ($params as $str) {
96 $this->assertElementNotContainsText('crm-contactinfo-content', $str);
97 }
98
99 // Add a phone
100 $this->inlineEdit('crm-phone-content', array(
101 'phone_1_phone' => '123-456-7890',
102 'phone_1_phone_ext' => '101',
103 'phone_1_location_type_id' => array('Work'),
104 'phone_1_phone_type_id' => array('Mobile'),
105 ));
106
107 // Add im
108 $this->inlineEdit('crm-im-content', array(
109 'im_1_name' => 'testmeout',
110 'im_1_location_type_id' => array('Work'),
111 'im_1_provider_id' => array('Jabber'),
112 ));
113
114 // Add an address - should default to home
115 $this->inlineEdit('address-block-1', array(
116 'address_1_street_address' => '123 St',
117 'address_1_city' => 'San Somewhere',
118 ), 'keep_open');
119 // Try to uncheck is_primary, we should get an error and it should stay checked
120 $this->click('address[1][is_primary]');
121 $this->waitForElementPresent('css=#crm-notification-container .error.ui-notify-message');
122 $this->assertChecked('address[1][is_primary]');
123 // Try to open another form while this one is still open - nothing should happen
124 $this->waitForElementPresent('address-block-2');
125 $this->openInlineForm('address-block-2', FALSE);
126 $this->assertElementNotPresent('css#address-block-2.form');
127 // Update address
128 $this->inlineEdit('address-block-1', array(
129 'address_1_street_address' => '321 Other St',
130 'address_1_city' => 'Sans Nowhere',
131 'address_1_postal_code' => '99999',
132 'address_1_postal_code_suffix' => '99',
133 ), 'no_open');
134 // Another address with same location type as first - should give an error
135 $this->inlineEdit('address-block-2', array(
136 'address_2_street_address' => '123 Foo',
137 'address_2_city' => 'San Anywhere',
138 'address_2_location_type_id' => array('Home'),
139 ), 'error');
140 $this->waitForTextPresent('required');
141 // Share address with a new org
142 $this->click('address[2][use_shared_address]');
143 $orgName = 'Test Org Inline' . substr(sha1(rand()), 0, 7);
144
145 // create new organization with dialog
146 $this->clickAt("xpath=//div[@id='s2id_address_2_master_contact_id']/a");
147 $this->click("xpath=//li[@class='select2-no-results']//a[contains(text(),' New Organization')]");
148 $this->waitForElementPresent("css=div#crm-profile-block");
149 $this->waitForElementPresent("_qf_Edit_next");
150
151 $this->type('organization_name', $orgName);
152 $this->type('street_address-1', 'Test Org Street');
153 $this->type('city-1', 'Test Org City');
154 $this->clickLink('_qf_Edit_next', 'selected_shared_address-2', FALSE);
155 $this->waitForTextPresent('Test Org Street');
156 $this->inlineEdit('address-block-2', array(
157 'address_2_location_type_id' => array('Work'),
158 ), 'no_open');
159 $this->waitForElementPresent('css=.crm-content.crm-contact-current_employer');
160 $this->assertElementContainsText('crm-contactinfo-content', $orgName);
161 $this->assertElementContainsText('address-block-2', $orgName);
162 $this->assertElementContainsText('address-block-2', 'Work Address');
163
164 // Edit demographics
165 $this->inlineEdit('crm-demographic-content', array(
166 'civicrm_gender_Female_1' => 1,
167 'is_deceased' => 1,
168 'birth_date' => 'date: Jan 1 1970',
169 ), 'no_open');
170 $this->assertElementContainsText('crm-demographic-content', 'Female');
171 $this->assertElementContainsText('crm-demographic-content', 'Contact is Deceased');
172 $this->inlineEdit('crm-demographic-content', array(
173 'is_deceased' => 0,
174 ), 'no_open');
175 $age = date('Y') - 1970;
176 $this->assertElementContainsText('crm-demographic-content', "$age years");
177
178 // Add emails
179 $this->inlineEdit('crm-email-content', array(
180 'css=#crm-email-content a.add-more-inline' => TRUE,
181 'email_1_email' => 'test1@monkey.com',
182 'email_2_email' => 'test2@monkey.com',
183 ), 'keep_open');
184
185 // Try an invalid email
186 $this->inlineEdit('crm-email-content', array(
187 'email_2_email' => 'invalid@monkey,com',
188 ), 'errorJs');
189
190 // Delete email
191 $this->inlineEdit('crm-email-content', array(
192 'css=#Email_Block_2 a.crm-delete-inline' => TRUE,
193 ));
194 $this->assertElementNotContainsText('crm-email-content', 'test2@monkey.com');
195
196 // Add website with invalid url
197 $this->inlineEdit('crm-website-content', array(
198 'css=#crm-website-content a.add-more-inline' => TRUE,
199 'website_1_url' => 'http://example.com',
200 'website_2_url' => 'something.wrong',
201 ), 'errorJs');
202
203 // Correct invalid url and add a third website
204 $this->inlineEdit('crm-website-content', array(
205 'css=#crm-website-content a.add-more-inline' => TRUE,
206 'website_2_url' => 'http://example.net',
207 'website_2_website_type_id' => array('Work'),
208 'website_3_url' => 'http://example.biz',
209 'website_3_website_type_id' => array('Main'),
210 ), 'keep_open');
211
212 // Delete website
213 $this->inlineEdit('crm-website-content', array(
214 'css=#Website_Block_2 a.crm-delete-inline' => TRUE,
215 ));
216 $this->assertElementNotContainsText('crm-website-content', 'http://example.net');
217
218 // Change contact name
219 $this->inlineEdit('crm-contactname-content', array(
220 'first_name' => 'NewName',
221 'prefix_id' => array('Mr.'),
222 ));
223 $this->assertElementContainsText('css=div.crm-summary-display_name', "Mr. NewName $lastName");
224 // Page title should be updated with new name on reload
225 $this->openCiviPage('contact/view', "reset=1&cid=$contactId", "crm-record-log");
226 $this->assertElementContainsText('css=title', "Mr. NewName $lastName");
227 }
228
229 /**
230 * Click on an inline-edit block and wait for it to open
231 *
232 * @param string $block
233 * selector.
234 * @param bool $wait
235 */
236 private function openInlineForm($block, $wait = TRUE) {
237 $this->mouseDown($block);
238 $this->mouseUp($block);
239 if ($wait) {
240 $this->waitForElementPresent("css=#$block .crm-container-snippet form");
241 }
242 }
243
244 /**
245 * Enter values in an inline edit block and save.
246 *
247 * @param string $block
248 * selector.
249 * @param array $params
250 * @param \str|string $valid str: submit behavior
251 * 'error' if we are expecting a form validation error,
252 * 're_open' (default) after saving, opens the form and validate inputs
253 * 'keep_open' same as 're_open' but doesn't automatically cancel at the end
254 * 'no_open' do not re-open to validate
255 */
256 private function inlineEdit($block, $params, $valid = 're_open') {
257 $this->openInlineForm($block);
258 foreach ($params as $item => $val) {
259 switch (gettype($val)) {
260 case 'boolean':
261 $this->click($item);
262 break;
263
264 case 'string':
265 if (substr($val, 0, 5) == 'date:') {
266 $this->webtestFillDate($item, trim(substr($val, 5)));
267 }
268 else {
269 $this->type($item, $val);
270 }
271 break;
272
273 case 'integer':
274 $method = $val ? 'check' : 'uncheck';
275 $this->$method($item);
276 break;
277
278 case 'array':
279 foreach ($val as $option) {
280 $selector = is_int($option) ? 'value' : 'label';
281 $this->select($item, "$selector=$option");
282 }
283 break;
284 }
285 }
286 $this->click("css=#$block input.crm-form-submit");
287 if ($valid !== 'error' && $valid != 'errorJs') {
288 // Verify the form saved
289 $this->waitForElementPresent("css=#$block > .crm-inline-block-content");
290 $validate = FALSE;
291 foreach ($params as $val) {
292 if (is_string($val) && $val && substr($val, 0, 5) != 'date:') {
293 $this->assertElementContainsText($block, $val);
294 $validate = TRUE;
295 }
296 elseif (!is_bool($val)) {
297 $validate = TRUE;
298 }
299 if (is_array($val)) {
300 foreach ($val as $option) {
301 if (!is_int($option)) {
302 $this->assertElementContainsText($block, $option);
303 }
304 }
305 }
306 }
307 // Open the form back up and check everything
308 if ($validate && $valid !== 'no_open') {
309 $this->openInlineForm($block);
310 foreach ($params as $item => $val) {
311 switch (gettype($val)) {
312 case 'string':
313 if ($val && substr($val, 0, 5) == 'date:') {
314 $val = date('m/d/Y', strtotime(trim(substr($val, 5))));
315 $item = "xpath=//input[starts-with(@id, '{$item}_display_')]";
316 }
317 if ($val) {
318 $this->assertElementValueEquals($item, $val);
319 }
320 break;
321
322 case 'integer':
323 $method = $val ? 'assertChecked' : 'assertNotChecked';
324 $this->$method($item);
325 break;
326
327 case 'array':
328 foreach ($val as $option) {
329 $method = is_int($option) ? 'assertIsSelected' : 'assertSelected';
330 $this->$method($item, $option);
331 }
332 break;
333 }
334 }
335 if ($valid !== 'keep_open') {
336 $this->click("css=#$block input.cancel");
337 }
338 }
339 }
340 // Verify there was a form error
341 else {
342 switch ($valid) {
343 case 'errorJs':
344 $this->waitForElementPresent('css=label.error');
345 break;
346
347 default:
348 $this->waitForElementPresent('css=#crm-notification-container .error.ui-notify-message');
349 $this->click('css=#crm-notification-container .error .ui-notify-cross');
350 }
351 }
352 }
353
354 }