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