You are here

public function TaxonomyAccessConfigTest::testGlobalDefaultConfig in Taxonomy Access Control 7

Tests configuring a global default.

Verifies that:

  • Access is updated for all nodes when there are no other configurations.
  • Access is updated for the correct nodes when there are specific term and vocabulary configurations.

File

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

Class

TaxonomyAccessConfigTest
Tests the module's configuration forms.

Code

public function testGlobalDefaultConfig() {

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

  // Use the admin form to give anonymous view allow in the global default.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . '/role/' . DRUPAL_ANONYMOUS_RID . '/edit');
  $edit = array();
  $this
    ->configureFormRow($edit, TAXONOMY_ACCESS_GLOBAL_DEFAULT, TAXONOMY_ACCESS_VOCABULARY_DEFAULT, TAXONOMY_ACCESS_NODE_ALLOW);
  $this
    ->drupalPost(NULL, $edit, 'Save all');

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

  // 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,
    )));
  }

  // Add some specific configurations programmatically.
  // Set the v1 default to view allow.
  $default_config = _taxonomy_access_format_grant_record($this->vocabs['v1']->vid, DRUPAL_ANONYMOUS_RID, array(
    'view' => TAXONOMY_ACCESS_NODE_ALLOW,
  ), TRUE);
  taxonomy_access_set_default_grants(array(
    $default_config,
  ));

  // Set v1t1 and v2t1 to view allow.
  $term_configs = array();
  foreach (array(
    'v1t1',
    'v2t1',
  ) as $name) {
    $term_configs[] = _taxonomy_access_format_grant_record($this->terms[$name]->vid, DRUPAL_ANONYMOUS_RID, array(
      'view' => TAXONOMY_ACCESS_NODE_ALLOW,
    ));
  }
  taxonomy_access_set_term_grants($term_configs);

  // This leaves articles and the v2t2 page controlled by the global default.
  // Log in as the administrator.
  $this
    ->drupalLogin($this->users['site_admin']);

  // Use the admin form to give anonymous view deny in the global default.
  $this
    ->drupalGet(TAXONOMY_ACCESS_CONFIG . '/role/' . DRUPAL_ANONYMOUS_RID . '/edit');
  $edit = array();
  $this
    ->configureFormRow($edit, TAXONOMY_ACCESS_GLOBAL_DEFAULT, TAXONOMY_ACCESS_VOCABULARY_DEFAULT, TAXONOMY_ACCESS_NODE_DENY);
  $this
    ->drupalPost(NULL, $edit, 'Save all');

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

  // Visit each artile and verify that access is 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,
    )));
  }

  // Visit each page.
  foreach ($this->pages as $key => $page) {
    $this
      ->drupalGet('node/' . $page->nid);
    switch (TRUE) {

      // If the page has no tags, access should be denied.
      case $key == 'no_tags':

      // If the page is tagged with v2t2, access should be denied.
      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, access should be allowed.
      default:
        $this
          ->assertResponse(200, t("Access to %name page (nid %nid) is allowed.", array(
          '%name' => $key,
          '%nid' => $page->nid,
        )));
        break;
    }
  }
}