1 contact.test ContactPersonalTestCase::testPersonalContactAccess()

Tests access to the personal contact form.

File

core/modules/contact/tests/contact.test, line 348
Tests for the Contact module.

Class

ContactPersonalTestCase
Tests the personal contact form.

Code

function testPersonalContactAccess() {
  // Test allowed access to user with contact form enabled.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(200);

  // Test denied access to the user's own contact form.
  $this->backdropGet('user/' . $this->web_user->uid . '/contact');
  $this->assertResponse(403);

  // Test always denied access to the anonymous user contact form.
  $this->backdropGet('user/0/contact');
  $this->assertResponse(403);

  // Test that anonymous users can access the contact form.
  $this->backdropLogout();
  user_role_grant_permissions(BACKDROP_ANONYMOUS_ROLE, array('access user contact forms'));
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(200);

  // Test that users can disable their contact form.
  $this->backdropLogin($this->contact_user);
  $edit = array('contact' => FALSE);
  $this->backdropPost('user/' . $this->contact_user->uid . '/edit', $edit, 'Save');
  $this->backdropLogout();
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(403);

  // Test that user's contact status stays disabled when saving.
  $contact_user_temp = user_load($this->contact_user->uid, TRUE);
  user_save($contact_user_temp);
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(403);

  // Test that users can enable their contact form.
  $this->backdropLogin($this->contact_user);
  $edit = array('contact' => TRUE);
  $this->backdropPost('user/' . $this->contact_user->uid . '/edit', $edit, 'Save');
  $this->backdropLogout();
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(200);

  // Revoke the personal contact permission for the anonymous user.
  user_role_revoke_permissions(BACKDROP_ANONYMOUS_ROLE, array('access user contact forms'));
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(403);

  // Disable the personal contact form.
  $this->backdropLogin($this->admin_user);
  $edit = array('contact_default_status' => FALSE);
  $this->backdropPost('admin/config/people/settings', $edit, t('Save configuration'));
  $this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
  $this->backdropLogout();

  // Re-create our contacted user with personal contact forms disabled by
  // default.
  $this->contact_user = $this->backdropCreateUser();

  // Test denied access to a user with contact form disabled.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(403);

  // Test allowed access for admin user to a user with contact form disabled.
  $this->backdropLogin($this->admin_user);
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(200);

  // Re-create our contacted user as a blocked user.
  $this->contact_user = $this->backdropCreateUser();
  $this->contact_user->status = 0;
  $this->contact_user->save();

  // Test that blocked users can still be contacted by admin.
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(200);

  // Test that blocked users cannot be contacted by non-admins.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('user/' . $this->contact_user->uid . '/contact');
  $this->assertResponse(403);
}