You are here

public function TaxonomyAccessConfigTest::testRoleEnableDisable in Taxonomy Access Control 7

Tests enabling and disabling TAC for a custom role.

File

./taxonomy_access.test, line 993
Automated tests for the Taxonomy Access Control module.

Class

TaxonomyAccessConfigTest
Tests the module's configuration forms.

Code

public function testRoleEnableDisable() {

  // Save some typing.
  $rid = $this->user_roles['regular_user']->rid;
  $name = $this->user_roles['regular_user']->name;

  // Check that the role is disabled by default.
  $this
    ->checkRoleConfig(array(
    DRUPAL_ANONYMOUS_RID => TRUE,
    DRUPAL_AUTHENTICATED_RID => TRUE,
    $rid => FALSE,
  ));

  // Test enabling the role.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . "/role/{$rid}/edit");

  // Check that there is:
  // - An enable link
  // - No disable link
  // @todo
  //   - No grant tables.
  $this
    ->checkRoleEnableLink($rid, TRUE);
  $this
    ->checkRoleDisableLink($rid, FALSE);

  // Enable the role and check that there is:
  // - A disable link
  // - No enable link
  // @todo
  //   - A global default table (with correct values?)
  //   - An "Add vocabulary" fieldset.
  //   - No vocabulary fieldsets or term data.
  $this
    ->clickLink(format_string('Enable @name', array(
    '@name' => $name,
  )));
  $this
    ->checkRoleEnableLink($rid, FALSE);
  $this
    ->checkRoleDisableLink($rid, TRUE);

  // Update the global default to allow view.
  $edit = array();
  $this
    ->configureFormRow($edit, TAXONOMY_ACCESS_GLOBAL_DEFAULT, TAXONOMY_ACCESS_VOCABULARY_DEFAULT, TAXONOMY_ACCESS_NODE_ALLOW);
  $this
    ->drupalPost(NULL, $edit, 'Save all');

  // Confirm that all three roles are enabled.
  $this
    ->checkRoleConfig(array(
    DRUPAL_ANONYMOUS_RID => TRUE,
    DRUPAL_AUTHENTICATED_RID => TRUE,
    $rid => TRUE,
  ));

  // Check that the role is configured.
  $r = db_query('SELECT grant_view FROM {taxonomy_access_default}
         WHERE vid = :vid AND rid = :rid', array(
    ':vid' => TAXONOMY_ACCESS_GLOBAL_DEFAULT,
    ':rid' => $rid,
  ))
    ->fetchField();
  $this
    ->assertTrue($r == TAXONOMY_ACCESS_NODE_ALLOW, t('Used form to grant the role %role view in the global default.', array(
    '%role' => $name,
  )));

  // Log in as the regular_user.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->users['regular_user']);

  // Visit each node and verify that access is allowed.
  foreach ($this->articles as $key => $article) {
    $this
      ->drupalGet('node/' . $article->nid);
    $this
      ->assertResponse(200, t("Access to %name article (nid %nid) is allowed.", array(
      '%name' => $key,
      '%nid' => $article->nid,
    )));
  }
  foreach ($this->pages as $key => $page) {
    $this
      ->drupalGet('node/' . $page->nid);
    $this
      ->assertResponse(200, t("Access to %name page (nid %nid) is allowed.", array(
      '%name' => $key,
      '%nid' => $page->nid,
    )));
  }

  // Log in as the administrator.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->users['site_admin']);

  // Test disabling the role.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . "/role/{$rid}/edit");
  $this
    ->clickLink(t('Disable @name', array(
    '@name' => $name,
  )));
  $this
    ->assertText("Are you sure you want to delete all taxonomy access rules for the role {$name}", t('Disable form for role loaded.'));
  $this
    ->drupalPost(NULL, array(), 'Delete all');

  // Confirm that a confirmation message appears.
  $this
    ->assertText("All taxonomy access rules deleted for role {$name}", t('Confirmation message found.'));

  // Check that there is:
  // - An enable link
  // - No disable link
  // @todo
  //   - No grant tables.
  $this
    ->checkRoleEnableLink($rid, TRUE);
  $this
    ->checkRoleDisableLink($rid, FALSE);

  // Confirm edit/enable/disable links are in their original state.
  $this
    ->checkRoleConfig(array(
    DRUPAL_ANONYMOUS_RID => TRUE,
    DRUPAL_AUTHENTICATED_RID => TRUE,
    $rid => FALSE,
  ));

  // Check that the role is no longer configured.
  $r = db_query('SELECT grant_view FROM {taxonomy_access_default}
         WHERE rid = :rid', array(
    ':rid' => $rid,
  ))
    ->fetchAll();
  $this
    ->assertTrue(empty($r), t('All records removed for role %role.', array(
    '%role' => $name,
  )));

  // @todo
  //   - Add a term configuration and make sure that gets deleted too.
  // Log in as the regular_user.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->users['regular_user']);

  // Visit all nodes and verify that access is again denied.
  foreach ($this->articles as $key => $article) {
    $this
      ->drupalGet('node/' . $article->nid);
    $this
      ->assertResponse(403, t("Access to %name article (nid %nid) is denied.", array(
      '%name' => $key,
      '%nid' => $article->nid,
    )));
  }
  foreach ($this->pages as $key => $page) {
    $this
      ->drupalGet('node/' . $page->nid);
    $this
      ->assertResponse(403, t("Access to %name page (nid %nid) is denied.", array(
      '%name' => $key,
      '%nid' => $page->nid,
    )));
  }
}