Merge pull request #9779 from WeMoveEU/CRM-19963
[civicrm-core.git] / tests / phpunit / WebTest / Contact / MultipleContactSubTypes.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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_MultipleContactSubTypes
31 */
32 class WebTest_Contact_MultipleContactSubTypes extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testIndividualAdd() {
39 $this->webtestLogin();
40
41 $selection1 = 'Student';
42 $selection2 = 'Parent';
43 $selection3 = 'Staff';
44
45 //Create custom group for contact sub-types
46 list($groupTitleForStudent, $customGroupIdForStudent) = $this->_addCustomData($selection1);
47 list($groupTitleForParent, $customGroupIdForParent) = $this->_addCustomData($selection2);
48 list($groupTitleForStaff, $customGroupIdForStaff) = $this->_addCustomData($selection3);
49
50 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
51
52 //contact details section
53 //select prefix
54 $this->click("prefix_id");
55 $this->select("prefix_id", "value=" . $this->webtestGetFirstValueForOptionGroup('individual_prefix'));
56
57 $this->addSelection('contact_sub_type', "value={$selection1}");
58 $this->addSelection('contact_sub_type', "value={$selection2}");
59
60 //fill in first name
61 $this->type("first_name", substr(sha1(rand()), 0, 7) . "John");
62
63 //fill in middle name
64 $this->type("middle_name", "Bruce");
65
66 //fill in last name
67 $this->type("last_name", substr(sha1(rand()), 0, 7) . "Smith");
68
69 //select suffix
70 $this->select("suffix_id", "value=3");
71
72 //fill in nick name
73 $this->type("nick_name", "jsmith");
74
75 //fill in email
76 $this->type("email_1_email", substr(sha1(rand()), 0, 7) . "john@gmail.com");
77
78 //fill in phone
79 $this->type("phone_1_phone", "2222-4444");
80
81 //fill in IM
82 $this->type("im_1_name", "testYahoo");
83
84 //fill in website
85 $this->type("website_1_url", "http://www.john.com");
86
87 //fill in source
88 $this->type("contact_source", "johnSource");
89
90 //fill in external identifier
91 $indExternalId = substr(sha1(rand()), 0, 4);
92 $this->type("external_identifier", $indExternalId);
93
94 $this->waitForElementPresent("customData$customGroupIdForStudent");
95 $this->waitForElementPresent("customData$customGroupIdForParent");
96
97 // Make sure our staff custom set does NOT show up
98 $this->assertFalse($this->isElementPresent("customData$customGroupIdForStaff"), "A custom field showed up for the wrong subtype!");
99
100 $this->type("xpath=//div[@id='customData{$customGroupIdForStudent}']/table/tbody/tr//td/input", "dummy text for customData{$customGroupIdForStudent}");
101 $this->type("xpath=//div[@id='customData{$customGroupIdForParent}']/table/tbody/tr//td/input", "dummy text for customData{$customGroupIdForParent}");
102
103 //address section
104 $this->click("addressBlock");
105 $this->waitForElementPresent("address_1_street_address");
106 //fill in address 1
107 $this->type("address_1_street_address", "902C El Camino Way SW");
108 $this->type("address_1_city", "Dumfries");
109 $this->type("address_1_postal_code", "1234");
110
111 $this->click("address_1_country_id");
112 $this->select("address_1_country_id", "value=" . $this->webtestGetValidCountryID());
113
114 if ($this->isElementPresent("address_1_geo_code_1")) {
115 $this->type("address_1_geo_code_1", "1234");
116 $this->type("address_1_geo_code_2", "5678");
117 }
118
119 //fill in address 2
120 $this->click("//div[@id='addMoreAddress1']/a/span");
121 $this->waitForElementPresent("address_2_street_address");
122 $this->type("address_2_street_address", "2782Y Dowlen Path W");
123 $this->type("address_2_city", "Birmingham");
124 $this->type("address_2_postal_code", "3456");
125
126 $this->click("address_2_country_id");
127 $this->select("address_2_country_id", "value=" . $this->webtestGetValidCountryID());
128
129 if ($this->isElementPresent("address_2_geo_code_1")) {
130 $this->type("address_2_geo_code_1", "1234");
131 $this->type("address_2_geo_code_2", "5678");
132 }
133
134 //Communication Preferences section
135 $this->click("//div[@class='crm-accordion-header' and contains(.,'Communication Preferences')]");
136
137 //select greeting/addressee options
138 $this->waitForElementPresent("email_greeting_id");
139 $this->select("email_greeting_id", "value=2");
140 $this->select("postal_greeting_id", "value=3");
141
142 //Select preferred method for Privacy
143 $this->click("privacy[do_not_trade]");
144 $this->click("privacy[do_not_sms]");
145
146 //Select preferred method(s) of communication
147 $this->click("preferred_communication_method[1]");
148 $this->click("preferred_communication_method[2]");
149
150 //select preferred language
151 $this->waitForElementPresent("preferred_language");
152 $this->select("preferred_language", "value=en_US");
153
154 //Demographics section
155 $this->click("//div[@class='crm-accordion-header' and contains(.,'Demographics')]");
156 $this->waitForElementPresent("birth_date");
157
158 $this->webtestFillDate('birth_date', "-1 year");
159
160 //Tags and Groups section
161 $this->click("//div[@class='crm-accordion-header' and contains(.,'Tags and Groups')]");
162
163 $this->click("group[{$this->webtestGetValidEntityID('Group')}]");
164 $this->click("tag[{$this->webtestGetValidEntityID('Tag')}]");
165
166 // Clicking save.
167 $this->click("_qf_Contact_upload_view");
168 $this->waitForPageToLoad($this->getTimeoutMsec());
169
170 //checking the contact sub-type of newly created individual
171 $this->waitForText('crm-notification-container', "Contact Saved");
172 $this->assertElementContainsText('css=.crm-contact_type_label', "Student");
173 $this->assertElementContainsText('css=.crm-contact_type_label', "Parent");
174
175 //custom data check
176 $this->waitForText("custom-set-content-{$customGroupIdForParent}", "dummy text for customData{$customGroupIdForParent}");
177 $this->waitForText("custom-set-content-{$customGroupIdForStudent}", "dummy text for customData{$customGroupIdForStudent}");
178
179 // Get contact id
180 $cid = $this->urlArg('cid');
181
182 //editing contact sub-type
183 $this->openCiviPage('contact/add', "reset=1&action=update&cid=$cid");
184
185 //edit contact sub-type
186 $this->removeSelection("contact_sub_type", "value={$selection1}");
187 $this->addSelection('contact_sub_type', "value={$selection3}");
188
189 $this->waitForElementPresent("customData$customGroupIdForStaff");
190
191 // Make sure our staff custom set does NOT show up
192 $this->assertFalse($this->isElementPresent("customData$customGroupIdForStudent"), "A custom field showed up for the wrong subtype!");
193
194 $this->type("xpath=//div[@id='customData{$customGroupIdForParent}']/table/tbody/tr//td/input", "dummy text for customData{$customGroupIdForParent}");
195 $this->type("xpath=//div[@id='customData{$customGroupIdForStaff}']/table/tbody/tr//td/input", "dummy text for customData{$customGroupIdForStaff}");
196
197 $this->click("_qf_Contact_upload_view-bottom");
198 $this->waitForPageToLoad($this->getTimeoutMsec());
199
200 // Check confirmation alert.
201 $this->assertTrue((bool) preg_match("/One or more contact subtypes have been de-selected from the list for this contact. Any custom data associated with de-selected subtype will be removed. Click OK to proceed, or Cancel to review your changes before saving./", $this->getConfirmation()));
202 $this->chooseOkOnNextConfirmation();
203 $this->waitForPageToLoad($this->getTimeoutMsec());
204
205 // Verify contact types
206 $this->waitForText('crm-notification-container', "Contact Saved");
207 $this->assertElementNotContainsText('css=.crm-contact_type_label', "Student");
208 $this->assertElementContainsText('css=.crm-contact_type_label', "Staff");
209 $this->assertElementContainsText('css=.crm-contact_type_label', "Parent");
210
211 //custom data check
212 $this->waitForText("custom-set-content-{$customGroupIdForParent}", "dummy text for customData{$customGroupIdForParent}");
213 $this->waitForText("custom-set-content-{$customGroupIdForStaff}", "dummy text for customData{$customGroupIdForStaff}");
214 }
215
216 /**
217 * Add custom fields for a contact sub-type
218 * @param $contactSubType
219 * @return array
220 */
221 public function _addCustomData($contactSubType) {
222 $this->openCiviPage("admin/custom/group", "action=add&reset=1");
223
224 //fill custom group title
225 $customGroupTitle = "Custom group For {$contactSubType}" . substr(sha1(rand()), 0, 4);
226 $this->click("title");
227 $this->type("title", $customGroupTitle);
228
229 //custom group extends
230 $this->click("extends_0");
231 $this->select("extends_0", "value=Individual");
232 $this->addSelection("extends_1", "label={$contactSubType}");
233
234 // Don't collapse
235 $this->uncheck('collapse_display');
236
237 // Save
238 $this->click('_qf_Group_next-bottom');
239 $this->waitForElementPresent('_qf_Field_cancel-bottom');
240
241 //Is custom group created?
242 $this->waitForText('crm-notification-container', "Your custom field set '{$customGroupTitle}' has been added.");
243 $gid = $this->urlArg('gid');
244
245 // Add field
246 $this->openCiviPage('admin/custom/group/field/add', "reset=1&action=add&gid=$gid");
247 $fieldLabel = "custom_field_for_{$contactSubType}" . substr(sha1(rand()), 0, 4);
248 $this->type('label', $fieldLabel);
249 $this->click('_qf_Field_done-bottom');
250 $this->waitForPageToLoad($this->getTimeoutMsec());
251 $customGroupTitle = preg_replace('/\s/', '_', trim($customGroupTitle));
252 return array($customGroupTitle, $gid);
253 }
254
255 }