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 +--------------------------------------------------------------------+
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
29 //Tests for the ability to add a CMS user from a contact's record
30 //See http://issues.civicrm.org/jira/browse/CRM-8723
31 class WebTest_Contact_CreateCmsUserFromContactTest
extends CiviSeleniumTestCase
{
33 protected function setUp() {
37 //Test that option to create a cms user is present on a contact who does not
38 //have a cms account already( in this case, a new contact )
39 function testCreateContactLinkPresent() {
42 $this->webtestLogin('admin');
44 //create a New Contact
45 $firstName = substr(sha1(rand()), 0, 7) . "John";
46 $lastName = substr(sha1(rand()), 0, 7) . "Smith";
47 $email = $this->webtestAddContact($firstName, $lastName, TRUE);
49 //Assert that the user actually does have a CMS Id displayed
50 $this->assertTrue(!$this->isTextPresent("User ID"));
52 //Assert that the contact user record link says create user record
53 $this->assertElementContainsText("css=#actions li.crm-contact-user-record", "Create User Record", "Create User Record link not in action menu of new contact");
56 //Test that the action link is missing for users who already have a contact
57 //record. The contact record for drupal user 1 is used
58 function testCreateContactLinkMissing() {
61 $this->webtestLogin('admin');
63 // go to My Account page
64 $this->open($this->sboxPath
. "user");
66 // click "View Contact Record" link
67 $this->waitForElementPresent("xpath=//div[@class='profile']/span/a[text()='View Contact Record']");
68 $this->click("xpath=//div[@class='profile']/span/a[text()='View Contact Record']");
69 $this->waitForPageToLoad($this->getTimeoutMsec());
71 //Assert that the user actually does have a CMS Id displayed
72 $this->assertTrue($this->isTextPresent("User ID"));
74 //Assert that the text of the user record link does not say Create User Record
75 $this->assertElementNotContainsText("css=#actions li.crm-contact-user-record", "Create User Record", "Create User Record link not in action menu of new contact");
78 //Test the ajax "check username availibity" link when adding cms user
79 function testCheckUsernameAvailability() {
80 $this->webtestLogin('admin');
82 $email = $this->_createUserAndGotoForm();
85 //use the username of the admin user to test if the username is taken
86 $username = $this->settings
->adminUsername
;
88 $this->_fillCMSUserForm($username, $password, $password);
89 $this->click("checkavailability");
90 $this->waitForCondition("selenium.browserbot.getCurrentWindow().jQuery('#msgbox').text() != 'Checking...'");
91 $this->assertElementContainsText("msgbox", "This username is taken", "Taken username is indicated as being available");
93 //fill the form with a good username
94 $username = sha1(rand());
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 currently available", "Available username is indicated as being taken");
101 //Test form submission when the username is taken
102 function testTakenUsernameSubmission() {
105 $this->webtestLogin('admin');
107 //create a New Contact
108 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
109 $password = 'abc123';
111 //submit the form with the bad username
112 $username = $this->settings
->adminUsername
;
113 $this->_fillCMSUserForm($username, $password, $password);
114 $this->click("_qf_Useradd_next-bottom");
115 $this->waitForPageToLoad($this->getTimeoutMsec());
117 //the civicrm messages should indicate the username is taken
118 $this->assertElementContainsText("css=#crm-notification-container", "already taken", "CiviCRM Message does not indicate the username is in user");
120 //check the uf match table that no contact has been created
121 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
122 $this->assertTrue($results['count'] == 0);
125 //Test form sumbission when user passwords dont match
126 function testMismatchPasswordSubmission() {
129 $this->webtestLogin('admin');
131 //create a New Contact
132 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
133 $password = 'abc123';
135 //submit with mismatch passwords
136 $username = $this->settings
->adminUsername
;
137 $this->_fillCMSUserForm($username, $password, $password . "mismatch");
138 $this->click("_qf_Useradd_next-bottom");
139 $this->waitForPageToLoad($this->getTimeoutMsec());
141 //check that that there is a password mismatch text
142 $this->assertElementContainsText("css=#crm-notification-container", "Password mismatch", "No form error given on password missmatch");
144 //check that no user was created;
145 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
146 $this->assertTrue($results['count'] == 0);
149 function testMissingDataSubmission() {
152 $this->webtestLogin('admin');
154 //create a New Contact
155 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
156 $password = 'abc123';
158 //submit with mismatch passwords
159 $username = $this->settings
->adminUsername
;
160 $this->click("_qf_Useradd_next-bottom");
161 $this->waitForPageToLoad($this->getTimeoutMsec());
163 //the civicrm messages section should not indicate that a user has been created
164 $this->assertElementNotContainsText("css=#crm-notification-container", "User has been added", "CiviCRM messages say that a user was created when username left blank");
166 //the civicrm message should say username is required
167 $this->assertElementContainsText("css=#crm-notification-container", "Username is required", "The CiviCRM messae does not indicate that the username is required");
169 //the civicrm message should say password is required
170 $this->assertElementContainsText("css=#crm-notification-container", "Password is required", "The CiviCRM messae does not indicate that the password is required");
172 //check that no user was created;
173 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
174 $this->assertTrue($results['count'] == 0);
177 //Test a valid (username unique and passwords match) submission
178 function testValidSubmission() {
181 $this->webtestLogin('admin');
183 //create a New Contact
184 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
185 $password = 'abc123';
187 //submit with matching passwords
188 $this->_fillCMSUserForm($firstName, $password, $password);
189 $this->click("_qf_Useradd_next-bottom");
190 $this->waitForPageToLoad($this->getTimeoutMsec());
192 //drupal messages should say user created
193 $this->assertTrue($this->isTextPresent("Created a new user account"), "Drupal does not report success creating user in the message");
195 //The new user id should be on the page
196 $this->assertTrue($this->isTextPresent("User ID"));
198 //Assert that a user was actually created AND that they are tied to the record
199 $results = $this->webtest_civicrm_api("UFMatch", "get", array('contact_id' => $cid));
200 $this->assertTrue($results['count'] == 1);
203 function _fillCMSUserForm($username, $password, $confirm_password) {
204 $this->type("cms_name", $username);
205 $this->type("cms_pass", $password);
206 $this->type("cms_confirm_pass", $confirm_password);
209 function _createUserAndGoToForm() {
210 $firstName = substr(sha1(rand()), 0, 7) . "John";
211 $lastName = substr(sha1(rand()), 0, 7) . "Smith";
212 $email = $this->webtestAddContact($firstName, $lastName, TRUE);
214 // Get the contact id of the new contact
215 $contactUrl = $this->parseURL();
216 $cid = $contactUrl['queryString']['cid'];
217 $this->assertType('numeric', $cid);
219 //got to the new cms user form
220 $this->openCiviPage('contact/view/useradd', "reset=1&action=add&cid={$cid}");
222 return array($cid, $firstName, $lastName, $email);