/[drupal]/drupal/modules/user/user.test
ViewVC logotype

Diff of /drupal/modules/user/user.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.24 by dries, Tue Dec 30 16:43:20 2008 UTC revision 1.25 by webchick, Thu Jan 8 08:42:13 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: user.test,v 1.23 2008/12/16 23:57:33 dries Exp $  // $Id: user.test,v 1.24 2008/12/30 16:43:20 dries Exp $
3    
4  class UserRegistrationTestCase extends DrupalWebTestCase {  class UserRegistrationTestCase extends DrupalWebTestCase {
5    function getInfo() {    function getInfo() {
# Line 151  class UserValidationTestCase extends Dru Line 151  class UserValidationTestCase extends Dru
151    }    }
152  }  }
153    
154    class UserCancelTestCase extends DrupalWebTestCase {
 class UserDeleteTestCase extends DrupalWebTestCase {  
155    function getInfo() {    function getInfo() {
156      return array(      return array(
157        'name' => t('User delete'),        'name' => t('Cancel account'),
158        'description' => t('Registers a user and deletes it.'),        'description' => t('Ensure that account cancellation methods work as expected.'),
159        'group' => t('User')        'group' => t('User'),
160      );      );
161    }    }
162    
163    /**    /**
164     * Registers a user and deletes it.     * Attempt to cancel account without permission.
165     */     */
166    function testUserRegistration() {    function testUserCancelWithoutPermission() {
167      // Set user registration to "Visitors can create accounts and no administrator approval is required."      variable_set('user_cancel_method', 'user_cancel_reassign');
     variable_set('user_register', 1);  
168    
169      $edit = array();      // Create a user.
170      $edit['name'] = $this->randomName();      $account = $this->drupalCreateUser(array());
171      $edit['mail'] = $edit['name'] . '@example.com';      $this->drupalLogin($account);
172      $this->drupalPost('user/register', $edit, t('Create new account'));      // Load real user object.
173      $this->assertText(t('Your password and further instructions have been sent to your e-mail address.'), t('User registered successfully.'));      $account = user_load($account->uid);
174    
175      $user = user_load($edit);      // Create a node.
176        $node = $this->drupalCreateNode(array('uid' => $account->uid));
177    
178      // Create admin user to delete registered user.      // Attempt to cancel account.
179        $this->drupalGet('user/' . $account->uid . '/edit');
180        $this->assertNoRaw(t('Cancel account'), t('No cancel account button displayed.'));
181    
182        // Attempt bogus account cancellation request confirmation.
183        $timestamp = $account->login;
184        $this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
185        $this->assertResponse(403, t('Bogus cancelling request rejected.'));
186        $this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 1)), t('User account was not canceled.'));
187    
188        // Confirm user's content has not been altered.
189        $test_node = node_load($node->nid, NULL, TRUE);
190        $this->assertTrue(($test_node->uid == $account->uid && $test_node->status == 1), t('Node of the user has not been altered.'));
191      }
192    
193      /**
194       * Attempt invalid account cancellations.
195       */
196      function testUserCancelInvalid() {
197        variable_set('user_cancel_method', 'user_cancel_reassign');
198    
199        // Create a user.
200        $account = $this->drupalCreateUser(array('cancel account'));
201        $this->drupalLogin($account);
202        // Load real user object.
203        $account = user_load($account->uid);
204    
205        // Create a node.
206        $node = $this->drupalCreateNode(array('uid' => $account->uid));
207    
208        // Attempt to cancel account.
209        $this->drupalPost('user/' . $account->uid . '/edit', NULL, t('Cancel account'));
210    
211        // Confirm account cancellation.
212        $timestamp = time();
213        $this->drupalPost(NULL, NULL, t('Cancel account'));
214        $this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
215    
216        // Attempt bogus account cancellation request confirmation.
217        $bogus_timestamp = $timestamp + 60;
218        $this->drupalGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
219        $this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), t('Bogus cancelling request rejected.'));
220        $this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 1)), t('User account was not canceled.'));
221    
222        // Attempt expired account cancellation request confirmation.
223        $bogus_timestamp = $timestamp - 86400 - 60;
224        $this->drupalGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
225        $this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), t('Expired cancel account request rejected.'));
226        $this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 1)), t('User account was not canceled.'));
227    
228        // Confirm user's content has not been altered.
229        $test_node = node_load($node->nid, NULL, TRUE);
230        $this->assertTrue(($test_node->uid == $account->uid && $test_node->status == 1), t('Node of the user has not been altered.'));
231      }
232    
233      /**
234       * Disable account and keep all content.
235       */
236      function testUserBlock() {
237        variable_set('user_cancel_method', 'user_cancel_block');
238    
239        // Create a user.
240        $account = $this->drupalCreateUser(array('cancel account'));
241        $this->drupalLogin($account);
242        // Load real user object.
243        $account = user_load($account->uid);
244    
245        // Attempt to cancel account.
246        $this->drupalGet('user/' . $account->uid . '/edit');
247        $this->drupalPost(NULL, NULL, t('Cancel account'));
248        $this->assertText(t('Are you sure you want to cancel your account?'), t('Confirmation form to cancel account displayed.'));
249        $this->assertText(t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'), t('Informs that all content will be remain as is.'));
250        $this->assertNoText(t('Select the method to cancel the account above.'), t('Does not allow user to select account cancellation method.'));
251    
252        // Confirm account cancellation.
253        $timestamp = time();
254        $this->drupalPost(NULL, NULL, t('Cancel account'));
255        $this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
256    
257        // Confirm account cancellation request.
258        $this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
259        $this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 0)), t('User has been blocked.'));
260    
261        // Confirm user is logged out.
262        $this->assertNoText($account->name, t('Logged out.'));
263      }
264    
265      /**
266       * Disable account and unpublish all content.
267       */
268      function testUserBlockUnpublish() {
269        variable_set('user_cancel_method', 'user_cancel_block_unpublish');
270    
271        // Create a user.
272        $account = $this->drupalCreateUser(array('cancel account'));
273        $this->drupalLogin($account);
274        // Load real user object.
275        $account = user_load($account->uid);
276    
277        // Create a node with two revisions.
278        $node = $this->drupalCreateNode(array('uid' => $account->uid));
279        $settings = get_object_vars($node);
280        $settings['revision'] = 1;
281        $node = $this->drupalCreateNode($settings);
282    
283        // Attempt to cancel account.
284        $this->drupalGet('user/' . $account->uid . '/edit');
285        $this->drupalPost(NULL, NULL, t('Cancel account'));
286        $this->assertText(t('Are you sure you want to cancel your account?'), t('Confirmation form to cancel account displayed.'));
287        $this->assertText(t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'), t('Informs that all content will be unpublished.'));
288    
289        // Confirm account cancellation.
290        $timestamp = time();
291        $this->drupalPost(NULL, NULL, t('Cancel account'));
292        $this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
293    
294        // Confirm account cancellation request.
295        $this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
296        $this->assertTrue(user_load(array('uid' => $account->uid, 'status' => 0)), t('User has been blocked.'));
297    
298        // Confirm user's content has been unpublished.
299        $test_node = node_load($node->nid, NULL, TRUE);
300        $this->assertTrue($test_node->status == 0, t('Node of the user has been unpublished.'));
301        $test_node = node_load($node->nid, $node->vid, TRUE);
302        $this->assertTrue($test_node->status == 0, t('Node revision of the user has been unpublished.'));
303    
304        // Confirm user is logged out.
305        $this->assertNoText($account->name, t('Logged out.'));
306      }
307    
308      /**
309       * Delete account and anonymize all content.
310       */
311      function testUserAnonymize() {
312        variable_set('user_cancel_method', 'user_cancel_reassign');
313    
314        // Create a user.
315        $account = $this->drupalCreateUser(array('cancel account'));
316        $this->drupalLogin($account);
317        // Load real user object.
318        $account = user_load($account->uid);
319    
320        // Create a simple node.
321        $node = $this->drupalCreateNode(array('uid' => $account->uid));
322    
323        // Create a node with two revisions, the initial one belonging to the
324        // cancelling user.
325        $revision_node = $this->drupalCreateNode(array('uid' => $account->uid));
326        $revision = $revision_node->vid;
327        $settings = get_object_vars($revision_node);
328        $settings['revision'] = 1;
329        $settings['uid'] = 1; // Set new/current revision to someone else.
330        $revision_node = $this->drupalCreateNode($settings);
331    
332        // Attempt to cancel account.
333        $this->drupalGet('user/' . $account->uid . '/edit');
334        $this->drupalPost(NULL, NULL, t('Cancel account'));
335        $this->assertText(t('Are you sure you want to cancel your account?'), t('Confirmation form to cancel account displayed.'));
336        $this->assertRaw(t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))), t('Informs that all content will be attributed to anonymous account.'));
337    
338        // Confirm account cancellation.
339        $timestamp = time();
340        $this->drupalPost(NULL, NULL, t('Cancel account'));
341        $this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
342    
343        // Confirm account cancellation request.
344        $this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
345        $this->assertFalse(user_load($account->uid), t('User is not found in the database.'));
346    
347        // Confirm that user's content has been attributed to anonymous user.
348        $test_node = node_load($node->nid, NULL, TRUE);
349        $this->assertTrue(($test_node->uid == 0 && $test_node->status == 1), t('Node of the user has been attributed to anonymous user.'));
350        $test_node = node_load($revision_node->nid, $revision, TRUE);
351        $this->assertTrue(($test_node->uid == 0 && $test_node->status == 1), t('Node revision of the user has been attributed to anonymous user.'));
352        $test_node = node_load($revision_node->nid, NULL, TRUE);
353        $this->assertTrue(($test_node->uid != 0 && $test_node->status == 1), t("Current revision of the user's node was not attributed to anonymous user."));
354    
355        // Confirm that user is logged out.
356        $this->assertNoText($account->name, t('Logged out.'));
357      }
358    
359      /**
360       * Delete account and remove all content.
361       */
362      function testUserDelete() {
363        variable_set('user_cancel_method', 'user_cancel_delete');
364    
365        // Create a user.
366        $account = $this->drupalCreateUser(array('cancel account'));
367        $this->drupalLogin($account);
368        // Load real user object.
369        $account = user_load($account->uid);
370    
371        // Create a simple node.
372        $node = $this->drupalCreateNode(array('uid' => $account->uid));
373    
374        // Create comment.
375        module_load_include('test', 'comment');
376        $comment = CommentHelperCase::postComment($node, '', $this->randomName(32), TRUE, TRUE);
377        $this->assertTrue(comment_load($comment->id), t('Comment found.'));
378    
379        // Create a node with two revisions, the initial one belonging to the
380        // cancelling user.
381        $revision_node = $this->drupalCreateNode(array('uid' => $account->uid));
382        $revision = $revision_node->vid;
383        $settings = get_object_vars($revision_node);
384        $settings['revision'] = 1;
385        $settings['uid'] = 1; // Set new/current revision to someone else.
386        $revision_node = $this->drupalCreateNode($settings);
387    
388        // Attempt to cancel account.
389        $this->drupalGet('user/' . $account->uid . '/edit');
390        $this->drupalPost(NULL, NULL, t('Cancel account'));
391        $this->assertText(t('Are you sure you want to cancel your account?'), t('Confirmation form to cancel account displayed.'));
392        $this->assertText(t('Your account will be removed and all account information deleted. All of your content will also be deleted.'), t('Informs that all content will be deleted.'));
393    
394        // Confirm account cancellation.
395        $timestamp = time();
396        $this->drupalPost(NULL, NULL, t('Cancel account'));
397        $this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
398    
399        // Confirm account cancellation request.
400        $this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
401        $this->assertFalse(user_load($account->uid), t('User is not found in the database.'));
402    
403        // Confirm that user's content has been deleted.
404        $this->assertFalse(node_load($node->nid, NULL, TRUE), t('Node of the user has been deleted.'));
405        $this->assertFalse(node_load($node->nid, $revision, TRUE), t('Node revision of the user has been deleted.'));
406        $this->assertTrue(node_load($revision_node->nid, NULL, TRUE), t("Current revision of the user's node was not deleted."));
407        $this->assertFalse(comment_load($comment->id), t('Comment of the user has been deleted.'));
408    
409        // Confirm that user is logged out.
410        $this->assertNoText($account->name, t('Logged out.'));
411      }
412    
413      /**
414       * Create an administrative user and delete another user.
415       */
416      function testUserCancelByAdmin() {
417        variable_set('user_cancel_method', 'user_cancel_reassign');
418    
419        // Create a regular user.
420        $account = $this->drupalCreateUser(array());
421    
422        // Create administrative user.
423      $admin_user = $this->drupalCreateUser(array('administer users'));      $admin_user = $this->drupalCreateUser(array('administer users'));
424      $this->drupalLogin($admin_user);      $this->drupalLogin($admin_user);
425    
426      // Delete user.      // Delete regular user.
427      $this->drupalGet('user/' . $user->uid . '/edit');      $this->drupalGet('user/' . $account->uid . '/edit');
428      $this->drupalPost(NULL, NULL, t('Delete'));      $this->drupalPost(NULL, NULL, t('Cancel account'));
429      $this->assertRaw(t('Are you sure you want to delete the account %name?', array('%name' => $user->name)), t('[confirm deletion] Asks for confirmation.'));      $this->assertRaw(t('Are you sure you want to cancel the account %name?', array('%name' => $account->name)), t('Confirmation form to cancel account displayed.'));
430      $this->assertText(t('All submissions made by this user will be attributed to the anonymous account. This action cannot be undone.'), t('[confirm deletion] Inform that all submissions will be attributed to anonymous account.'));      $this->assertText(t('Select the method to cancel the account above.'), t('Allows to select account cancellation method.'));
431    
432      // Confirm deletion.      // Confirm deletion.
433      $this->drupalPost(NULL, NULL, t('Delete'));      $this->drupalPost(NULL, NULL, t('Cancel account'));
434      $this->assertRaw(t('%name has been deleted.', array('%name' => $user->name)), t('User deleted'));      $this->assertRaw(t('%name has been deleted.', array('%name' => $account->name)), t('User deleted.'));
435      $this->assertFalse(user_load($edit), t('User is not found in the database'));      $this->assertFalse(user_load($account->uid), t('User is not found in the database.'));
436      }
437    
438      /**
439       * Create an administrative user and mass-delete other users.
440       */
441      function testMassUserCancelByAdmin() {
442        variable_set('user_cancel_method', 'user_cancel_reassign');
443        // Enable account cancellation notification.
444        variable_set('user_mail_status_canceled_notify', TRUE);
445    
446        // Create administrative user.
447        $admin_user = $this->drupalCreateUser(array('administer users'));
448        $this->drupalLogin($admin_user);
449    
450        // Create some users.
451        $users = array();
452        for ($i = 0; $i < 3; $i++) {
453          $account = $this->drupalCreateUser(array());
454          $users[$account->uid] = $account;
455        }
456    
457        // Cancel user accounts, including own one.
458        $edit = array();
459        $edit['operation'] = 'cancel';
460        foreach ($users as $uid => $account) {
461          $edit['accounts['. $uid .']'] = TRUE;
462        }
463        $edit['accounts['. $admin_user->uid .']'] = TRUE;
464        $this->drupalPost('admin/user/user', $edit, t('Update'));
465        $this->assertText(t('Are you sure you want to cancel these user accounts?'), t('Confirmation form to cancel accounts displayed.'));
466        $this->assertText(t('When cancelling these accounts'), t('Allows to select account cancellation method.'));
467        $this->assertText(t('Require e-mail confirmation to cancel account.'), t('Allows to send confirmation mail.'));
468        $this->assertText(t('Notify user when account is canceled.'), t('Allows to send notification mail.'));
469    
470        // Confirm deletion.
471        $this->drupalPost(NULL, NULL, t('Cancel accounts'));
472        $status = TRUE;
473        foreach ($users as $account) {
474          $status = $status && (strpos($this->content, t('%name has been deleted.', array('%name' => $account->name))) !== FALSE);
475          $status = $status && !user_load($account->uid);
476        }
477        $this->assertTrue($status, t('Users deleted and not found in the database.'));
478    
479        // Ensure that admin account was not cancelled.
480        $this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
481        $this->assertTrue(user_load(array('uid' => $admin_user->uid, 'status' => 1)), t('Administrative user is found in the database and enabled.'));
482    }    }
483  }  }
484    

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.25

  ViewVC Help
Powered by ViewVC 1.1.3