commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / tests / phpunit / WebTest / Contact / CreateCmsUserFromContactTest.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 * Tests for the ability to add a CMS user from a contact's record
31 * See http://issues.civicrm.org/jira/browse/CRM-8723
32 * Class WebTest_Contact_CreateCmsUserFromContactTest
33 */
34 class WebTest_Contact_CreateCmsUserFromContactTest extends CiviSeleniumTestCase {
35
36 protected function setUp() {
37 parent::setUp();
38 }
39
40 /**
41 * Test that option to create a cms user is present on a contact who.
42 * does not have a cms account already (in this case, a new
43 * contact).
44 */
45 public function testCreateContactLinkPresent() {
46
47 //login
48 $this->webtestLogin('admin');
49
50 //create a New Contact
51 $firstName = substr(sha1(rand()), 0, 7) . "John";
52 $lastName = substr(sha1(rand()), 0, 7) . "Smith";
53 $email = $this->webtestAddContact($firstName, $lastName, TRUE);
54
55 //Assert that the user actually does have a CMS Id displayed
56 $this->assertTrue(!$this->isTextPresent("User ID"));
57
58 //Assert that the contact user record link says create user record
59 $this->assertElementContainsText("css=#actions li.crm-contact-user-record", "Create User Record", "Create User Record link not in action menu of new contact");
60 }
61
62 /**
63 * Test that the action link is missing for users who already have a
64 * contact record. The contact record for drupal user 1 is used.
65 */
66 public function testCreateContactLinkMissing() {
67
68 //login
69 $this->webtestLogin('admin');
70
71 // go to My Account page
72 $this->open($this->sboxPath . "user");
73
74 // click "View contact record" link
75 $this->waitForElementPresent("xpath=//div[@class='profile']/span/a[text()='View contact record']");
76 $this->click("xpath=//div[@class='profile']/span/a[text()='View contact record']");
77 $this->waitForPageToLoad($this->getTimeoutMsec());
78
79 //Assert that the user actually does have a CMS Id displayed
80 $this->assertTrue($this->isTextPresent("User ID"));
81
82 //Assert that the text of the user record link does not say Create User Record
83 $this->assertElementNotContainsText("css=#actions li.crm-contact-user-record", "Create User Record", "Create User Record link not in action menu of new contact");
84 }
85
86 /**
87 * Test the ajax "check username availibity" link when adding cms user.
88 */
89 public function testCheckUsernameAvailability() {
90 $this->webtestLogin('admin');
91
92 $email = $this->_createUserAndGotoForm();
93 $password = "abc123";
94
95 //use the username of the admin user to test if the username is taken
96 $username = $this->settings->adminUsername;
97
98 $this->_fillCMSUserForm($username, $password, $password);
99 $this->click("checkavailability");
100 $this->waitForCondition("selenium.browserbot.getCurrentWindow().jQuery('#msgbox').text() != 'Checking...'");
101 $this->assertElementContainsText("msgbox", "This username is taken", "Taken username is indicated as being available");
102
103 //fill the form with a good username
104 $username = sha1(rand());
105 $this->_fillCMSUserForm($username, $password, $password);
106 $this->click("checkavailability");
107 $this->waitForCondition("selenium.browserbot.getCurrentWindow().jQuery('#msgbox').text() != 'Checking...'");
108 $this->assertElementContainsText("msgbox", "This username is currently available", "Available username is indicated as being taken");
109 }
110
111 /**
112 * Test form submission when the username is taken.
113 */
114 public function testTakenUsernameSubmission() {
115
116 //login
117 $this->webtestLogin('admin');
118
119 //create a New Contact
120 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
121 $password = 'abc123';
122
123 //submit the form with the bad username
124 $username = $this->settings->adminUsername;
125 $this->_fillCMSUserForm($username, $password, $password);
126 $this->click("_qf_Useradd_next-bottom");
127 $this->waitForPageToLoad($this->getTimeoutMsec());
128
129 //the civicrm messages should indicate the username is taken
130
131 $this->assertElementContainsText("xpath=//span[@class = 'crm-error']", "already taken", "CiviCRM Message does not indicate the username is in user");
132 //check the uf match table that no contact has been created
133 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
134 $this->assertTrue($results['count'] == 0);
135 }
136
137 /**
138 * Test form sumbission when user passwords dont match.
139 */
140 public function testMismatchPasswordSubmission() {
141
142 //login
143 $this->webtestLogin('admin');
144
145 //create a New Contact
146 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
147 $password = 'abc123';
148
149 //submit with mismatch passwords
150 $username = $this->settings->adminUsername;
151 $this->_fillCMSUserForm($username, $password, $password . "mismatch");
152 $this->click("_qf_Useradd_next-bottom");
153 $this->waitForPageToLoad($this->getTimeoutMsec());
154
155 //check that that there is a password mismatch text
156 $this->assertElementContainsText("xpath=//table[@class='form-layout-compressed']/tbody/tr[3]/td/span[@class='crm-error']", "Password mismatch", "No form error given on password missmatch");
157
158 //check that no user was created;
159 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
160 $this->assertTrue($results['count'] == 0);
161 }
162
163 public function testMissingDataSubmission() {
164
165 //login
166 $this->webtestLogin('admin');
167
168 //create a New Contact
169 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
170 $password = 'abc123';
171
172 //submit with mismatch passwords
173 $username = $this->settings->adminUsername;
174 $this->click("_qf_Useradd_next-bottom");
175 $this->waitForPageToLoad($this->getTimeoutMsec());
176
177 //the civicrm messages section should not indicate that a user has been created
178 $this->assertElementNotContainsText("xpath=//span[@class='crm-error']", "User has been added", "CiviCRM messages say that a user was created when username left blank");
179
180 //the civicrm message should say username is required
181 $this->assertElementContainsText("xpath=//span[@class='crm-error']", "Username is required", "The CiviCRM messae does not indicate that the username is required");
182
183 //the civicrm message should say password is required
184 $this->assertElementContainsText("xpath=//table[@class='form-layout-compressed']/tbody/tr[3]/td/span[@class='crm-error']", "Password is required", "The CiviCRM messae does not indicate that the password is required");
185
186 //check that no user was created;
187 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
188 $this->assertTrue($results['count'] == 0);
189 }
190
191 /**
192 * Test a valid (username unique and passwords match) submission.
193 */
194 public function testValidSubmission() {
195
196 //login
197 $this->webtestLogin('admin');
198
199 //create a New Contact
200 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
201 $password = 'abc123';
202
203 //submit with matching passwords
204 $this->_fillCMSUserForm($firstName, $password, $password);
205 $this->click("_qf_Useradd_next-bottom");
206 $this->waitForPageToLoad($this->getTimeoutMsec());
207
208 //drupal messages should say user created
209 $this->assertTrue($this->isTextPresent("Created a new user account"), "Drupal does not report success creating user in the message");
210
211 //The new user id should be on the page
212 $this->assertTrue($this->isTextPresent("User ID"));
213
214 //Assert that a user was actually created AND that they are tied to the record
215 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
216 $this->assertTrue($results['count'] == 1);
217 }
218
219 /**
220 * @param string $username
221 * @param $password
222 * @param $confirm_password
223 */
224 public function _fillCMSUserForm($username, $password, $confirm_password) {
225 $this->type("cms_name", $username);
226 $this->type("cms_pass", $password);
227 $this->type("cms_confirm_pass", $confirm_password);
228 }
229
230 /**
231 * @return array
232 */
233 public function _createUserAndGoToForm() {
234 $firstName = substr(sha1(rand()), 0, 7) . "John";
235 $lastName = substr(sha1(rand()), 0, 7) . "Smith";
236 $email = $this->webtestAddContact($firstName, $lastName, TRUE);
237
238 // Get the contact id of the new contact
239 $cid = $this->urlArg('cid');
240
241 //got to the new cms user form
242 $this->openCiviPage('contact/view/useradd', "reset=1&action=add&cid={$cid}");
243
244 return array($cid, $firstName, $lastName, $email);
245 }
246
247 }