3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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';
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
34 class WebTest_Contact_CreateCmsUserFromContactTest
extends CiviSeleniumTestCase
{
36 protected function setUp() {
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
45 public function testCreateContactLinkPresent() {
48 $this->webtestLogin('admin');
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);
55 //Assert that the user actually does have a CMS Id displayed
56 $this->assertTrue(!$this->isTextPresent("User ID"));
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");
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.
66 public function testCreateContactLinkMissing() {
69 $this->webtestLogin('admin');
71 // go to My Account page
72 $this->open($this->sboxPath
. "user");
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());
79 //Assert that the user actually does have a CMS Id displayed
80 $this->assertTrue($this->isTextPresent("User ID"));
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");
87 * Test the ajax "check username availibity" link when adding cms user.
89 public function testCheckUsernameAvailability() {
90 $this->webtestLogin('admin');
92 $email = $this->_createUserAndGotoForm();
95 //use the username of the admin user to test if the username is taken
96 $username = $this->settings
->adminUsername
;
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");
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");
112 * Test form submission when the username is taken.
114 public function testTakenUsernameSubmission() {
117 $this->webtestLogin('admin');
119 //create a New Contact
120 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
121 $password = 'abc123';
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());
129 //the civicrm messages should indicate the username is taken
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);
138 * Test form sumbission when user passwords dont match.
140 public function testMismatchPasswordSubmission() {
143 $this->webtestLogin('admin');
145 //create a New Contact
146 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
147 $password = 'abc123';
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());
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");
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);
163 public function testMissingDataSubmission() {
166 $this->webtestLogin('admin');
168 //create a New Contact
169 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
170 $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("xpath=//span[@class='crm-error']", "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("xpath=//span[@class='crm-error']", "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("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");
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);
192 * Test a valid (username unique and passwords match) submission.
194 public function testValidSubmission() {
197 $this->webtestLogin('admin');
199 //create a New Contact
200 list($cid, $firstName, $lastName, $email) = $this->_createUserAndGotoForm();
201 $password = 'abc123';
203 //submit with matching passwords
204 $this->_fillCMSUserForm($firstName, $password, $password);
205 $this->waitForAjaxContent();
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);
221 * @param string $username
223 * @param $confirm_password
225 public function _fillCMSUserForm($username, $password, $confirm_password) {
226 $this->type("cms_name", $username);
227 $this->type("cms_pass", $password);
228 $this->type("cms_confirm_pass", $confirm_password);
234 public function _createUserAndGoToForm() {
235 $firstName = substr(sha1(rand()), 0, 7) . "John";
236 $lastName = substr(sha1(rand()), 0, 7) . "Smith";
237 $email = $this->webtestAddContact($firstName, $lastName, TRUE);
239 // Get the contact id of the new contact
240 $cid = $this->urlArg('cid');
242 //got to the new cms user form
243 $this->openCiviPage('contact/view/useradd', "reset=1&action=add&cid={$cid}");
245 return array($cid, $firstName, $lastName, $email);