You are here

public function TaxonomyAccessConfigTest::testVocabularyDefaultConfig in Taxonomy Access Control 7

Tests configuring vocabulary defaults.

Verifies that:

  • Access is updated correctly when the vocabulary default is added and configured.
  • Access is updated correctly when there is a specific term configuration in the vocabulary.
  • Access is updated correctly when multiple defaults are changed.
  • Access is updated correctly when the vocabulary default is deleted.

File

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

Class

TaxonomyAccessConfigTest
Tests the module's configuration forms.

Code

public function testVocabularyDefaultConfig() {

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

  // Enable the vocabulary.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . '/role/' . DRUPAL_ANONYMOUS_RID . '/edit');

  // @todo
  //   - Ensure that all vocabularies are options in the "Add" fieldset.
  $edit = array();
  $edit['enable_vocab'] = $this->vocabs['v1']->vid;
  $this
    ->drupalPost(NULL, $edit, t('Add'));

  // @todo
  //   - Ensure that the vocabulary is removed from the "Add" fieldset.
  //   - Ensure that the fieldset for the vocabulary appears.
  //   - Ensure that no other fieldsets or rows appear.
  // Give anonymous view allow for the v1 default.
  $edit = array();
  $this
    ->configureFormRow($edit, $this->vocabs['v1']->vid, TAXONOMY_ACCESS_VOCABULARY_DEFAULT, TAXONOMY_ACCESS_NODE_ALLOW);
  $this
    ->drupalPost(NULL, $edit, 'Save all');

  // Log out.
  $this
    ->drupalLogout();

  // Visit each page and verify whether access is allowed or denied.
  foreach ($this->pages as $key => $page) {
    $this
      ->drupalGet('node/' . $page->nid);

    // If the page is tagged with a v1 term, access should be allowed.
    if (strpos($key, 'v1') !== FALSE) {
      $this
        ->assertResponse(200, t("Access to %name page (nid %nid) is allowed.", array(
        '%name' => $key,
        '%nid' => $page->nid,
      )));
    }
    else {
      $this
        ->assertResponse(403, t("Access to %name page (nid %nid) is denied.", array(
        '%name' => $key,
        '%nid' => $page->nid,
      )));
    }
  }

  // Programmatically enable v2 and add a specific configuration for v2t1.
  taxonomy_access_enable_vocab($this->vocabs['v2']->vid, DRUPAL_ANONYMOUS_RID);
  $term_config = _taxonomy_access_format_grant_record($this->terms['v2t1']->tid, DRUPAL_ANONYMOUS_RID, array(
    'view' => TAXONOMY_ACCESS_NODE_IGNORE,
  ));
  taxonomy_access_set_term_grants(array(
    $term_config,
  ));

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

  // Use the admin form to give anonymous view deny for the v2 default.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . '/role/' . DRUPAL_ANONYMOUS_RID . '/edit');
  $edit = array();
  $this
    ->configureFormRow($edit, $this->vocabs['v2']->vid, TAXONOMY_ACCESS_VOCABULARY_DEFAULT, TAXONOMY_ACCESS_NODE_DENY);
  $this
    ->drupalPost(NULL, $edit, 'Save all');
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . '/role/' . DRUPAL_ANONYMOUS_RID . '/edit');

  // Log out.
  $this
    ->drupalLogout();

  // Visit each page and verify whether access is allowed or denied.
  foreach ($this->pages as $key => $page) {
    $this
      ->drupalGet('node/' . $page->nid);
    switch (TRUE) {

      // If the page is tagged with v2t2, the v2 default is inherited: Deny.
      case strpos($key, 'v2t2') !== FALSE:
        $this
          ->assertResponse(403, t("Access to %name page (nid %nid) is denied.", array(
          '%name' => $key,
          '%nid' => $page->nid,
        )));
        break;

      // Otherwise, if the page is tagged with v1, it's allowed.
      case strpos($key, 'v1') !== FALSE:
        $this
          ->assertResponse(200, t("Access to %name page (nid %nid) is allowed.", array(
          '%name' => $key,
          '%nid' => $page->nid,
        )));
        break;

      // Access should be denied by default.
      default:
        $this
          ->assertResponse(403, t("Access to %name page (nid %nid) is denied.", array(
          '%name' => $key,
          '%nid' => $page->nid,
        )));
        break;
    }
  }

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

  // Use the form to change the configuration: Allow for v2; Deny for v1.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . '/role/' . DRUPAL_ANONYMOUS_RID . '/edit');
  $edit = array();
  $this
    ->configureFormRow($edit, $this->vocabs['v2']->vid, TAXONOMY_ACCESS_VOCABULARY_DEFAULT, TAXONOMY_ACCESS_NODE_ALLOW);
  $this
    ->configureFormRow($edit, $this->vocabs['v1']->vid, TAXONOMY_ACCESS_VOCABULARY_DEFAULT, TAXONOMY_ACCESS_NODE_DENY);
  $this
    ->drupalPost(NULL, $edit, 'Save all');

  // Log out.
  $this
    ->drupalLogout();

  // Visit each page and verify whether access is allowed or denied.
  foreach ($this->pages as $key => $page) {
    $this
      ->drupalGet('node/' . $page->nid);
    switch (TRUE) {

      // If the page is tagged with a v1 term, access should be denied.
      case strpos($key, 'v1') !== FALSE:
        $this
          ->assertResponse(403, t("Access to %name page (nid %nid) is denied.", array(
          '%name' => $key,
          '%nid' => $page->nid,
        )));
        break;

      // Otherwise, if the page is tagged with v2t2, the default is
      // inherited and access should be allowed.
      case strpos($key, 'v2t2') !== FALSE:
        $this
          ->assertResponse(200, t("Access to %name page (nid %nid) is allowed.", array(
          '%name' => $key,
          '%nid' => $page->nid,
        )));
        break;

      // Access should be denied by default.
      default:
        $this
          ->assertResponse(403, t("Access to %name page (nid %nid) is denied.", array(
          '%name' => $key,
          '%nid' => $page->nid,
        )));
        break;
    }
  }

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

  // Use the admin form to disable v1.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . '/role/' . DRUPAL_ANONYMOUS_RID . '/edit');
  $this
    ->clickLink(t('delete all v1 access rules'));
  $this
    ->assertText("Are you sure you want to delete all Taxonomy access rules for v1", t('Disable form for vocabulary loaded.'));
  $this
    ->drupalPost(NULL, array(), 'Delete all');

  // Log out.
  $this
    ->drupalLogout();

  // Visit each page and verify whether access is allowed or denied.
  foreach ($this->pages as $key => $page) {
    $this
      ->drupalGet('node/' . $page->nid);

    // If the page is tagged with v2t2, access should be allowed.
    if (strpos($key, 'v2t2') !== FALSE) {
      $this
        ->assertResponse(200, t("Access to %name page (nid %nid) is allowed.", array(
        '%name' => $key,
        '%nid' => $page->nid,
      )));
    }
    else {
      $this
        ->assertResponse(403, t("Access to %name page (nid %nid) is denied.", array(
        '%name' => $key,
        '%nid' => $page->nid,
      )));
    }
  }
}