3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
31 //Tests for the ability to add a CMS user from a contact's record
32 //See http://issues.civicrm.org/jira/browse/CRM-8723
33 class WebTest_Contact_CreateCmsUserFromContactTest
extends CiviSeleniumTestCase
{
35 protected function setUp() {
41 //Test that option to create a cms user is present on a contact who does not
42 //have a cms account already( in this case, a new contact )
43 function testCreateContactLinkPresent() {
44 $this->open($this->sboxPath
);
47 $this->webtestLogin(TRUE);
49 //create a New Contact
50 $firstName = substr(sha1(rand()), 0, 7) . "John";
51 $lastName = substr(sha1(rand()), 0, 7) . "Smith";
52 $email = $this->webtestAddContact($firstName, $lastName, TRUE);
54 //Assert that the user actually does have a CMS Id displayed
55 $this->assertTrue(!$this->isTextPresent("User ID"));
57 //Assert that the contact user record link says create user record
58 $this->assertElementContainsText("css=#actions li.crm-contact-user-record", "Create User Record", "Create User Record link not in action menu of new contact");
61 //Test that the action link is missing for users who already have a contact
62 //record. The contact record for drupal user 1 is used
63 function testCreateContactLinkMissing() {
64 $this->open($this->sboxPath
);
67 $this->webtestLogin(TRUE);
69 // go to My Account page
70 $this->open($this->sboxPath
. "user");
72 // click "View Contact Record" link
73 $this->waitForElementPresent("xpath=//div[@class='profile']/span/a[text()='View Contact Record']");
74 $this->click("xpath=//div[@class='profile']/span/a[text()='View Contact Record']");
75 $this->waitForPageToLoad($this->getTimeoutMsec());
77 //Assert that the user actually does have a CMS Id displayed
78 $this->assertTrue($this->isTextPresent("User ID"));
80 //Assert that the text of the user record link does not say Create User Record
81 $this->assertElementNotContainsText("css=#actions li.crm-contact-user-record", "Create User Record", "Create User Record link not in action menu of new contact");
84 //Test the ajax "check username availibity" link when adding cms user
85 function testCheckUsernameAvailability() {
86 $this->open($this->sboxPath
);
87 $this->webtestLogin(TRUE);
89 $email = $this->_createUserAndGotoForm();
92 //use the username of the admin user to test if the username is taken
93 $username = $this->settings
->adminUsername
;
95 $this->_fillCMSUserForm($username, $password, $password);
96 $this->click("checkavailability");
97 $this->waitForCondition("selenium.browserbot.getCurrentWindow().jQuery('#msgbox').text() != 'Checking...'");
98 $this->assertElementContainsText("msgbox", "This username is taken", "Taken username is indicated as being available");
100 //fill the form with a good username
101 $username = sha1(rand());
102 $this->_fillCMSUserForm($username, $password, $password);
103 $this->click("checkavailability");
104 $this->waitForCondition("selenium.browserbot.getCurrentWindow().jQuery('#msgbox').text() != 'Checking...'");
105 $this->assertElementContainsText("msgbox", "This username is currently available", "Available username is indicated as being taken");
109 //Test form submission when the username is taken
110 function testTakenUsernameSubmission() {
111 $this->open($this->sboxPath
);
114 $this->webtestLogin(TRUE);
116 //create a New Contact
117 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
118 $password = 'abc123';
121 //submit the form with the bad username
122 $username = $this->settings
->adminUsername
;
123 $this->_fillCMSUserForm($username, $password, $password);
124 $this->click("_qf_Useradd_next-bottom");
125 $this->waitForPageToLoad($this->getTimeoutMsec());
127 //the civicrm messages should indicate the username is taken
128 $this->assertElementContainsText("css=#crm-notification-container", "already taken", "CiviCRM Message does not indicate the username is in user");
130 //check the uf match table that no contact has been created
131 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
132 $this->assertTrue($results['count'] == 0);
135 //Test form sumbission when user passwords dont match
136 function testMismatchPasswordSubmission() {
137 $this->open($this->sboxPath
);
140 $this->webtestLogin(TRUE);
142 //create a New Contact
143 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
144 $password = 'abc123';
147 //submit with mismatch passwords
148 $username = $this->settings
->adminUsername
;
149 $this->_fillCMSUserForm($username, $password, $password . "mismatch");
150 $this->click("_qf_Useradd_next-bottom");
151 $this->waitForPageToLoad($this->getTimeoutMsec());
153 //check that that there is a password mismatch text
154 $this->assertElementContainsText("css=#crm-notification-container", "Password mismatch", "No form error given on password missmatch");
156 //check that no user was created;
157 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
158 $this->assertTrue($results['count'] == 0);
161 function testMissingDataSubmission() {
162 $this->open($this->sboxPath
);
165 $this->webtestLogin(TRUE);
167 //create a New Contact
168 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
169 $password = 'abc123';
172 //submit with mismatch passwords
173 $username = $this->settings
->adminUsername
;
174 $this->click("_qf_Useradd_next-bottom");
175 $this->waitForPageToLoad($this->getTimeoutMsec());
177 //the civicrm messages section should not indicate that a user has been created
178 $this->assertElementNotContainsText("css=#crm-notification-container", "User has been added", "CiviCRM messages say that a user was created when username left blank");
180 //the civicrm message should say username is required
181 $this->assertElementContainsText("css=#crm-notification-container", "Username is required", "The CiviCRM messae does not indicate that the username is required");
183 //the civicrm message should say password is required
184 $this->assertElementContainsText("css=#crm-notification-container", "Password is required", "The CiviCRM messae does not indicate that the password is required");
187 //check that no user was created;
188 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
189 $this->assertTrue($results['count'] == 0);
192 //Test a valid (username unique and passwords match) submission
193 function testValidSubmission() {
194 $this->open($this->sboxPath
);
197 $this->webtestLogin(TRUE);
199 //create a New Contact
200 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
201 $password = 'abc123';
204 //submit with matching passwords
205 $this->_fillCMSUserForm($firstName, $password, $password);
206 $this->click("_qf_Useradd_next-bottom");
207 $this->waitForPageToLoad($this->getTimeoutMsec());
209 //drupal messages should say user created
210 $this->assertTrue($this->isTextPresent("Created a new user account"), "Drupal does not report success creating user in the message");
212 //The new user id should be on the page
213 $this->assertTrue($this->isTextPresent("User ID"));
215 //Assert that a user was actually created AND that they are tied to the record
216 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
217 $this->assertTrue($results['count'] == 1);
220 function _fillCMSUserForm($username, $password, $confirm_password) {
221 $this->type("cms_name", $username);
222 $this->type("cms_pass", $password);
223 $this->type("cms_confirm_pass", $confirm_password);
226 function _createUserAndGoToForm() {
227 $firstName = substr(sha1(rand()), 0, 7) . "John";
228 $lastName = substr(sha1(rand()), 0, 7) . "Smith";
229 $email = $this->webtestAddContact($firstName, $lastName, TRUE);
231 // Get the contact id of the new contact
232 $contactUrl = $this->parseURL();
233 $cid = $contactUrl['queryString']['cid'];
234 $this->assertType('numeric', $cid);
236 //got to the new cms user form
237 $this->open($this->sboxPath
. "civicrm/contact/view/useradd?reset=1&action=add&cid=" . $cid);
238 $this->waitForPageToLoad($this->getTimeoutMsec());
240 return array($cid, $firstName, $lastName, $email);