Spike PHPCoverage Details: user.test

Line #FrequencySource Line
1 <?php
2 // $Id: user.test,v 1.10 2008/04/01 23:33:54 boombatower Exp $
3 
4 /**
5  * Class to test the user registration process,
6  * - based on initial version by Kuba Zygmunt -
7  */
8 class UserRegistrationTestCase extends DrupalTestCase {
9   function getInfo() {
101    return array('name' => t('User registration'), 'description' => t('Registers a user, fails login, resets password, successfully logs in with the one time password, changes password, logs out, successfully logs in with the new password, visits profile page.') , 'group' => 'User tests');
11   }
12 
13   function testUserRegistration() {
14     /* We first allow every user to login instantly. */
15     $this->drupalVariableSet('user_register', 1);
16 
17     /* make sure the profile module is disabled to avoid conflicts */
18     $this->drupalModuleDisable('profile');
19 
20     $name = $this->randomName();
21     $mail = "$name@example.com";
22     $edit = array('name' => $name,
23                   'mail' => $mail);
24     $this->drupalPost('user/register', $edit, t('Create new account'));
25 
26     $this->assertText(t('Your password and further instructions have been sent to your e-mail address.'), 'Your password and further instructions ... found');
27     $this->assertNoText(t('The name %name has been denied access.', array('%name' => $name)), 'not denied access');
28 
29     // now we check database fields
30     // we can use an 'edit' array to load user variable
31     $user = user_load($edit);
32 
33     $this->assertTrue(isset($user->uid), 'user->uid set');
34     $this->assertTrue(($user->uid > 0), 'uid > 0');
35     if (!isset($user->uid) || ($user->uid == 0)) {
36       return FALSE;
37     }
38 
39     $this->assertEqual($user->name, $name, 'Checking name of user');
40     $this->assertEqual($user->mail, $mail, 'Checking e-mail address');
41     $this->assertEqual($user->mode, 0, 'Checking mode field');
42     $this->assertEqual($user->sort, 0, 'Checking sort field');
43     $this->assertEqual($user->threshold, 0,'Checking treshold field');
44     $this->assertEqual($user->theme, '','Checking theme field');
45     $this->assertEqual($user->signature, '','Checking signature field');
46     $this->assertTrue(($user->created > time() - 20 ), 0,'Checking creation time.');
47     $this->assertEqual($user->status, variable_get('user_register', 1) == 1 ? 1 : 0,'Checking status field');
48     $this->assertEqual($user->timezone, variable_get('date_default_timezone', NULL), 'Checking timezone field');
49     $this->assertEqual($user->language, '', 'Checking language field');
50     $this->assertEqual($user->picture, '', 'Check picture field');
51     $this->assertEqual($user->init, $mail, 'Check init field');
52 
53     /* We try to login with a wrong password */
54     $login_edit = array('name' => $name, 'pass' => 'foo');
55     $this->drupalPost('user', $login_edit, t('Log in'));
56     $this->assertText(t('Sorry, unrecognized username or password. Have you forgotten your password?'), 'Test for failed Login');
57     $url = user_pass_reset_url($user);
58     /* TODO: find a better way, we currently have to do it that way, see user.module line 1041. */
59     sleep(1);
60     $this->drupalGet($url);
61 
62     // Will proabaly not work localised as the text is sent to tranlate wrapped in <p> usually
63 
64     $this->assertText(t('This login can be used only once.'), "Check for 'used only once' notice");
65 
66     $this->drupalPost(NULL, array(), t('Log in'));
67     $this->assertText(t('You have just used your one-time login link. It is no longer necessary to use this link to login. Please change your password.'), "Check for one time login notice after clicking Login button.");
68 
69     /* now lets change our password */
70     $new_pass = user_password();
71     $edit = array(
72       'pass[pass1]' => $new_pass,
73       'pass[pass2]' => $new_pass,
74     );
75     $this->drupalPost(NULL, $edit, t('Save'));
76     $this->assertText(t('The changes have been saved.'), "Changed password to '$new_pass'");
77 
78     /* Check if the password changes are present in db */
79     $user = user_load(array('uid' => $user->uid));
80     $this->assertEqual($user->pass, md5($new_pass), 'Correct password in database');
81 
82     /* logout */
83     $this->clickLink(t('Log out'));
84     $this->assertNoText($user->name, 'Logged out');
85 
86     /* login again */
87     $login_edit['pass'] = $new_pass;
88     $this->drupalPost('user', $login_edit, t('Log in'));
89 
90     $pname = $user->name;
91 
92     $this->assertText($pname, 'Logged in (name found)');
93     $this->assertNoText(t('Sorry. Unrecognized username or password.'), 'Logged in (no message for unrecognized username or password)');
94     $this->assertNoText(t('User login'), 'Logged in (no user login form present)');
95     // I can't find this in Drupal anywhere, but I left it in for now.
96     $this->assertNoRaw(t('The username %name has been blocked.', array('%name' => $pname)), 'Not blocked');
97     $this->assertNoRaw(t('The name %name is a reserved username.', array('%name' => $pname)), 'Access granted');
98 
99     $this->drupalGet('user');
100     $this->assertText($pname, 'user as auth lands on the user profile');
101     $this->assertText(t('View'), 'View tab on the profile page');
102     $this->assertText(t('Edit'), 'Edit tab on the profile page');
103 
104     /* delete test user, roles and maybe authmap */
105     db_query('DELETE FROM {users} WHERE uid = %d', $user->uid);
106     db_query('DELETE FROM {users_roles} WHERE uid = %d', $user->uid);
107     db_query('DELETE FROM {authmap} WHERE uid = %d', $user->uid);
108   }
109 }
110 
111 /**
112  * This class is based on the original Simpletest Module by Moshe Weitzman
113  */
114 class UserValidationTestCase extends DrupalTestCase {
115   function getInfo() {
1161    return array('name' => 'Username/email validation', 'description' => 'Verify that username/email validity checks behave as designed.' , 'group' => 'User tests');
117   }
118 
119   // username validation
120   function testMinLengthName() {
121     $name = '';
122     $result = user_validate_name($name);
123     $this->assertNotNull($result, 'Excessively short username');
124   }
125   function testValidCharsName() {
126     $name = 'ab/';
127     $result = user_validate_name($name);
128     $this->assertNotNull($result, 'Invalid chars in username');
129   }
130   function testMaxLengthName() {
131     $name = str_repeat('a', 61);
132     $result = user_validate_name($name);
133     $this->assertNotNull($result, 'Excessively long username');
134   }
135   function testValidName() {
136     $name = 'abc';
137     $result = user_validate_name($name);
138     $this->assertNull($result, 'Valid username');
139   }
140 
141   // mail validation
142   function testMinLengthMail() {
143     $name = '';
144     $result = user_validate_mail($name);
145     $this->assertNotNull($result, 'Empty mail');
146   }
147   function testInValidMail() {
148     $name = 'abc';
149     $result = user_validate_mail($name);
150     $this->assertNotNull($result, 'Invalid mail');
151   }
152   function testValidMail() {
153     $name = 'absdsdsdc@dsdsde.com';
154     $result = user_validate_mail($name);
155     $this->assertNull($result, 'Valid mail');
156   }
157 }
158 
159 class UserAccessTestCase extends DrupalTestCase {
160   var $_cleanup_masks = array();
161 
162   function getInfo() {
1631    return array('name' => t('User access rules'),
164                  'description' => t('Assure that negative and positive access rules behave as designed.') ,
165                  'group' => 'User tests'
166            );
167   }
168 
169   function addMask($mask, $type, $status = 0) {
170     db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $mask, $type, $status);
171     $aid = db_last_insert_id('access', 'aid');
172     $str_status = ($status == 0) ? 'deny' : 'allow';
173     $this->assertTrue(db_affected_rows() > 0, "$str_status Mask added for $type '$mask'");
174     $this->_cleanup_masks[] = $aid;
175   }
176 
177   function tearDown() {
178     while (sizeof($this->_cleanup_masks) > 0) {
179       $aid = array_pop($this->_cleanup_masks);
180       db_query("DELETE FROM {access} WHERE aid = %d", $aid);
181     }
182   }
183 
184   function testAccess() {
185     /* To avoid conflicts with non allowed account creations */
186     $this->drupalVariableSet('user_register', 1);
187 
188     $this->addMask('simpletest_block%', 'user');
189     $this->addMask('simpletest_block_allow%', 'user', 1);
190 
191     /* first try blocked user */
192     $name = $this->randomName(2, 'simpletest_block_');
193     $mail = "$name@example.com";
194     $edit = array('name' => $name,
195                   'mail' => $mail);
196 
197     $this->drupalPost('user/register', $edit, t('Create new account'));
198 
199     $this->assertNoText(t('Your password and further instructions have been sent to your e-mail address.'), 'blocked user: Your password and further instructions - not found');
200     $this->assertText(t('The name @name has been denied access.', array('@name' => $name)), 'blocked user: denied access - found');
201 
202     /* now try allowed user */
203     $name = $this->randomName(2, 'simpletest_block_allow_');
204     $mail = "$name@example.com";
205     $edit = array('name' => $name,
206                   'mail' => $mail);
207 
208     /* We need new cookies */
209     unset($this->ch);
210     $this->drupalPost('user/register', $edit, t('Create new account'));
211 
212     $this->assertText(t('Your password and further instructions have been sent to your e-mail address.'), 'access user: Your password and further instructions - found');
213     $this->assertNoText(t('The name @name has been denied access.', array('@name' => $name)), 'access user: denied access - not found');
214 
215     $user = user_load($edit);
216 
217     $this->assertTrue(isset($user->uid), 'user->uid set');
218     $this->assertTrue(($user->uid > 0), 'uid > 0');
219     if (isset($user->uid) && ($user->uid > 0)) {
220       /* delete test user, roles and maybe authmap */
221       db_query('DELETE FROM {users} WHERE uid = %d', $user->uid);
222       db_query('DELETE FROM {users_roles} WHERE uid = %d', $user->uid);
223       db_query('DELETE FROM {authmap} WHERE uid = %d', $user->uid);
224     }
225   }
226 }
227 
228 class UserDeleteTestCase extends DrupalTestCase {
229   function getInfo() {
2301    return array('name' => t('User delete'), 'description' => t('Registers a user and deletes it.') , 'group' => 'User tests');
231   }
232 
233  function tearDown() {
234     parent::tearDown();
235   }
236 
237   function testUserRegistration() {
238     /* We first allow every user to login instantly. */
239     $this->drupalVariableSet('user_register', 1);
240 
241     /* make sure the profile module is disabled to avoid conflicts */
242     $this->drupalModuleDisable('profile');
243 
244     $name = $this->randomName();
245     $pname = theme('placeholder', $name);
246     $mail = "$name@example.com";
247     $edit = array('name' => $name,
248                   'mail' => $mail);
249     $this->drupalPost('user/register', $edit, t('Create new account'));
250     $user_to_delete = user_load($edit);
251     $uid = $user_to_delete->uid;
252     $web_user = $this->drupalCreateUser(array('administer users'));
253     $this->drupalLogin($web_user);
254     $this->drupalGet(url('user/'. $uid .'/edit', array('absolute' => TRUE)));
255     $this->drupalPost(NULL, array(), t('Delete'));
256     $this->assertRaw(t('Are you sure you want to delete the account %name?', array('%name' => $name)), 'Confirm title');
257     $this->assertText(t('All submissions made by this user will be attributed to the anonymous account. This action cannot be undone.'), 'Confirm text');
258     $this->drupalPost(NULL, array(), t('Delete'));
259     $this->assertRaw(t('%name has been deleted.', array('%name' => $name)), 'User deleted');
260     $this->assertFalse(user_load($edit), 'User is not found in the database');
261   }
262 }