Merge branch 'phpunit-ob-fix' of https://github.com/giant-rabbit/civicrm-core into...
[civicrm-core.git] / tests / phpunit / WebTest / Contact / InlineFieldsEditTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 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 'css=#email_greeting_display a' => 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 $block string selector
233 * @param bool $wait
234 */
235 private function openInlineForm($block, $wait = TRUE) {
236 $this->mouseDown($block);
237 $this->mouseUp($block);
238 if ($wait) {
239 $this->waitForElementPresent("css=#$block .crm-container-snippet form");
240 }
241 }
242
243 /**
244 * Enter values in an inline edit block and save
245 *
246 * @param $block string selector
247 * @param $params array
248 * @param \str|string $valid str: submit behavior
249 * 'error' if we are expecting a form validation error,
250 * 're_open' (default) after saving, opens the form and validate inputs
251 * 'keep_open' same as 're_open' but doesn't automatically cancel at the end
252 * 'no_open' do not re-open to validate
253 */
254 private function inlineEdit($block, $params, $valid = 're_open') {
255 $this->openInlineForm($block);
256 foreach ($params as $item => $val) {
257 switch(gettype($val)) {
258 case 'boolean':
259 $this->click($item);
260 break;
261 case 'string':
262 if (substr($val, 0, 5) == 'date:') {
263 $this->webtestFillDate($item, trim(substr($val, 5)));
264 }
265 else {
266 $this->type($item, $val);
267 }
268 break;
269 case 'integer':
270 $method = $val ? 'check' : 'uncheck';
271 $this->$method($item);
272 break;
273 case 'array':
274 foreach ($val as $option) {
275 $selector = is_int($option) ? 'value' : 'label';
276 $this->select($item, "$selector=$option");
277 }
278 break;
279 }
280 }
281 $this->click("css=#$block input.crm-form-submit");
282 if ($valid !== 'error' && $valid !='errorJs') {
283 // Verify the form saved
284 $this->waitForElementPresent("css=#$block > .crm-inline-block-content");
285 $validate = FALSE;
286 foreach ($params as $val) {
287 if (is_string($val) && $val && substr($val, 0, 5) != 'date:') {
288 $this->assertElementContainsText($block, $val);
289 $validate = TRUE;
290 }
291 elseif (!is_bool($val)) {
292 $validate = TRUE;
293 }
294 if (is_array($val)) {
295 foreach ($val as $option) {
296 if (!is_int($option)) {
297 $this->assertElementContainsText($block, $option);
298 }
299 }
300 }
301 }
302 // Open the form back up and check everything
303 if ($validate && $valid !== 'no_open') {
304 $this->openInlineForm($block);
305 foreach ($params as $item => $val) {
306 switch(gettype($val)) {
307 case 'string':
308 if ($val && substr($val, 0, 5) == 'date:') {
309 $val = date('m/d/Y', strtotime(trim(substr($val, 5))));
310 $item .= '_display';
311 }
312 if ($val) {
313 $this->assertElementValueEquals($item, $val);
314 }
315 break;
316 case 'integer':
317 $method = $val ? 'assertChecked' : 'assertNotChecked';
318 $this->$method($item);
319 break;
320 case 'array':
321 foreach ($val as $option) {
322 $method = is_int($option) ? 'assertIsSelected' : 'assertSelected';
323 $this->$method($item, $option);
324 }
325 break;
326 }
327 }
328 if ($valid !== 'keep_open') {
329 $this->click("css=#$block input.cancel");
330 }
331 }
332 }
333 // Verify there was a form error
334 else {
335 switch($valid) {
336 case 'errorJs':
337 $this->waitForElementPresent('css=label.error');
338 break;
339 default:
340 $this->waitForElementPresent('css=#crm-notification-container .error.ui-notify-message');
341 $this->click('css=#crm-notification-container .error .ui-notify-cross');
342 }
343 }
344 }
345 }