You are here

function PrivatemsgTestCase::testDelete in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.test \PrivatemsgTestCase::testDelete()
  2. 6 privatemsg.test \PrivatemsgTestCase::testDelete()
  3. 7 privatemsg.test \PrivatemsgTestCase::testDelete()

File

./privatemsg.test, line 663
Test file for privatemsg.module

Class

PrivatemsgTestCase

Code

function testDelete() {

  // Create users.
  $author = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
    'delete privatemsg',
  ));
  $recipient = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
    'delete privatemsg',
  ));
  $recipient2 = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
    'cancel account',
  ));
  $admin = $this
    ->drupalCreateUser(array(
    'write privatemsg',
    'read privatemsg',
    'delete privatemsg',
    'read all private messages',
  ));
  $dummy = $this
    ->drupalCreateUser(array(
    'cancel account',
  ));

  // Create texts.
  $subject = $this
    ->randomName(20);
  $body1 = $this
    ->randomName(100);
  $body2 = $this
    ->randomName(100);

  // Create message and response.
  $return = privatemsg_new_thread(array(
    $recipient,
    $recipient2,
  ), $subject, $body1, array(
    'author' => $author,
  ));
  privatemsg_reply($return['message']->thread_id, $body2, array(
    'author' => $recipient,
  ));

  // Check with user without delete permission.
  $this
    ->drupalLogin($recipient2);
  $this
    ->drupalGet('messages/view/' . $return['message']->thread_id);
  $this
    ->assertText($subject, 'Subject is displayed');
  $this
    ->assertText($body1, 'First message is displayed');
  $this
    ->assertText($body2, 'Second message is displayed');
  $this
    ->assertNoLink(t('Delete'), 'Delete message is link is not displayed for user without permission');

  // Check if access for that user is denied.
  $this
    ->drupalGet('messages/delete/' . $return['message']->thread_id . '/' . $return['message']->mid);
  $this
    ->assertText(t('Access denied'));

  // Check with user with delete access.
  $this
    ->drupalLogin($recipient);
  $this
    ->drupalGet('messages/view/' . $return['message']->thread_id);
  $this
    ->assertText(t('Delete'), 'Delete message is link is displayed for user without permission');

  // Click delete link of the second message and cancel.
  $this
    ->clickLink(t('Delete'), 1);
  $this
    ->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
  $this
    ->clickLink(t('Cancel'));
  $this
    ->assertText($body2, 'Second message is still displayed');

  // Confirm message deletion.
  $this
    ->clickLink(t('Delete'), 1);
  $this
    ->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
  $this
    ->drupalPost(NULL, array(), t('Delete'));
  $this
    ->assertText(t('Message has been deleted.'), 'Message has been deleted');
  $this
    ->assertText($body1, 'First message is still displayed');
  $this
    ->assertNoText($body2, 'Second message was deleted');

  // Click delete link of the first message and cancel.
  $this
    ->clickLink(t('Delete'));
  $this
    ->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
  $this
    ->clickLink(t('Cancel'));
  $this
    ->assertText($body1, 'First message is still displayed');

  // Confirm message deletion.
  $this
    ->clickLink(t('Delete'));
  $this
    ->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
  $this
    ->drupalPost(NULL, array(), t('Delete'));
  $this
    ->assertText(t('Message has been deleted.'), 'Message deleted has been deleted');
  $this
    ->assertNoText($subject, 'All messages of that thread have been deleted');

  // Test if the message has not been deleted for other users.
  $this
    ->drupalLogin($recipient2);
  $this
    ->drupalGet('messages/view/' . $return['message']->thread_id);
  $this
    ->assertText($body1, 'First message is still displayed');
  $this
    ->assertText($body2, 'First message is still displayed');

  // Test delete all checkbox.
  $this
    ->drupalLogin($admin);
  $this
    ->drupalGet('messages/view/' . $return['message']->thread_id);
  $this
    ->clickLink(t('Delete'), 1);
  $this
    ->drupalPost(NULL, array(
    'delete_options' => TRUE,
  ), t('Delete'));
  $this
    ->assertText(t('Message has been deleted for all users.'), 'Message deleted has been deleted');

  // Test if the message has been deleted for all users.
  $this
    ->drupalLogin($recipient2);
  $this
    ->drupalGet('messages/view/' . $return['message']->thread_id);
  $this
    ->assertText($body1, 'First message is still displayed');
  $this
    ->assertNoText($body2, 'Second message has been deleted for all users');

  // Check that messages of canceled users (user_cancel_delete) are deleted too.
  $edit = array(
    'body[value]' => $this
      ->randomName(100),
  );
  $this
    ->drupalPost(NULL, $edit, t('Send message'));
  $this
    ->drupalGet('messages/view/' . $return['message']->thread_id);
  $this
    ->assertText($edit['body[value]'], t('New reply is displayed'));

  // Reload user object, don't use the cache.
  $recipient2 = user_load($recipient2->uid, TRUE);

  // Attempt to cancel account.
  variable_set('user_cancel_method', 'user_cancel_delete');
  $this
    ->drupalGet('user/' . $recipient2->uid . '/edit');
  $this
    ->drupalPost(NULL, NULL, t('Cancel account'));

  // Confirm account cancellation.
  // Explicitly use time(), REQUEST_TIME is not exact enough.
  $timestamp = time();
  $this
    ->drupalPost(NULL, NULL, t('Cancel account'));

  // Confirm account cancellation request.
  $this
    ->drupalGet("user/{$recipient2->uid}/cancel/confirm/{$timestamp}/" . user_pass_rehash($recipient2->pass, $timestamp, $recipient2->login, $recipient2->uid));

  // Simpletest thinks that we are still logged in.
  $this->loggedInUser = NULL;
  $this
    ->drupalLogin($admin);
  $this
    ->drupalGet('messages/view/' . $return['message']->thread_id);
  $this
    ->assertText($body1, 'First message is still displayed');
  $this
    ->assertNoText($edit['body[value]'], t('Reply of deleted user is not displayed anymore'));

  // Test if admin is allowed to delete messages of other users.
  $this
    ->drupalGet('user/' . $author->uid . '/messages');
  $this
    ->checkThreadDelete($return['message']);

  // Check if user is allowed to delete messages.
  $this
    ->drupalLogin($author);
  $this
    ->drupalGet('messages');
  $this
    ->checkThreadDelete($return['message']);

  // Check that deleting users that didn't send any message does not cause
  // errors.
  $this
    ->drupalLogin($dummy);

  // Reload user object, don't use the cache.
  $dummy = user_load($dummy->uid, TRUE);

  // Attempt to cancel account.
  $this
    ->drupalGet('user/' . $dummy->uid . '/edit');
  $this
    ->drupalPost(NULL, NULL, t('Cancel account'));

  // Confirm account cancellation.
  // Explicitly use time(), REQUEST_TIME is not exact enough.
  $timestamp = time();
  $this
    ->drupalPost(NULL, NULL, t('Cancel account'));

  // Confirm account cancellation request.
  $this
    ->drupalGet("user/{$dummy->uid}/cancel/confirm/{$timestamp}/" . user_pass_rehash($dummy->pass, $timestamp, $dummy->login, $dummy->uid));
}