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