You are here

public function TaxonomyAccessNodeGrantTest::setUp in Taxonomy Access Control 7

Sets up a Drupal site for running functional and integration tests.

Generates a random database prefix and installs Drupal with the specified installation profile in DrupalWebTestCase::$profile into the prefixed database. Afterwards, installs any additional modules specified by the test.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Parameters

...: List of modules to enable for the duration of the test. This can be either a single array or a variable number of string arguments.

Overrides TaxonomyAccessTestCase::setUp

See also

DrupalWebTestCase::prepareDatabasePrefix()

DrupalWebTestCase::changeDatabasePrefix()

DrupalWebTestCase::prepareEnvironment()

File

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

Class

TaxonomyAccessNodeGrantTest
Tests node access for all possible grant combinations.

Code

public function setUp() {
  parent::setUp();

  // Configure roles with no additional permissions.
  foreach ($this->role_config as $role_name => $permissions) {
    $this->roles[$role_name] = $this
      ->drupalCreateRole(array(), $role_name);
  }
  $node_grants = array(
    'view',
    'update',
    'delete',
  );

  // Set up our testing taxonomy.
  // We will create 4 vocabularies: a, i, d, and nc
  // These names indicate what grant the vocab. default will have for view.
  // (NC means the vocab default is not configured.)
  $grant_types = array(
    'a' => array(),
    'i' => array(),
    'd' => array(),
    'nc' => array(),
  );

  // View alone can be used to test V/U/D because the logic is identical.
  foreach ($node_grants as $grant) {
    $grant_types['a'][$grant] = TAXONOMY_ACCESS_NODE_ALLOW;
    $grant_types['i'][$grant] = TAXONOMY_ACCESS_NODE_IGNORE;
    $grant_types['d'][$grant] = TAXONOMY_ACCESS_NODE_DENY;
  }

  // Each vocabulary will have four parent terms in the same fashion:
  // a_parent, i_parent, d_parent, and nc_parent.
  // Each of these_parent terms will have children in each class, as well:
  // a_child, i_child, d_child, and nc_child.
  // So, each vocab looks something like:
  // - a_parent
  // - - a_child
  // - - i_child
  // - - d_child
  // - - nc_child
  // - i_parent
  // - - a_child
  // - - i_child
  // - - d_child
  // - - nc_child
  // - d_parent
  // - - a_child
  // - - i_child
  // - - d_child
  // - - nc_child
  // - nc_parent
  // - - a_child
  // - - i_child
  // - - d_child
  // - - nc_child
  $term_rows = array();
  $default_rows = array();
  $this->setUpAssertions = array();

  // Configure terms, vocabularies, and grants.
  foreach ($grant_types as $vocab_name => $default_grants) {

    // Create the vocabulary.
    $vocab_name = "v" . $vocab_name;
    $this->vocabs[$vocab_name] = array();
    $this->vocabs[$vocab_name]['vocab'] = parent::createVocab($vocab_name);
    $this->vocabs[$vocab_name]['terms'] = array();
    $vocab = $this->vocabs[$vocab_name]['vocab'];

    // Add a field for the vocabulary to pages.
    $this
      ->createField($vocab_name);

    // Configure default grants for the vocabulary for each role.
    if (!empty($default_grants)) {
      foreach ($this->roles as $name => $role) {
        $default_rows[] = _taxonomy_access_format_grant_record($vocab->vid, $role, $default_grants, TRUE);
        $this->setUpAssertions[] = array(
          'grant' => $default_grants['view'],
          'query' => 'SELECT grant_view FROM {taxonomy_access_default} WHERE vid = :vid AND rid = :rid',
          'args' => array(
            ':vid' => $vocab->vid,
            ':rid' => $role,
          ),
          'message' => t('Configured default grants for vocab %vocab, role %role', array(
            '%vocab' => $vocab->machine_name,
            '%role' => $name,
          )),
        );
      }
    }

    // Create terms.
    foreach ($grant_types as $parent_name => $parent_grants) {

      // Create parent term.
      $parent_name = $vocab_name . "__" . $parent_name . "_parent";
      $this->vocabs[$vocab_name]['terms'][$parent_name] = parent::createTerm($parent_name, $vocab);
      $parent_id = $this->vocabs[$vocab_name]['terms'][$parent_name]->tid;

      // Configure grants for the parent term for each role.
      if (!empty($parent_grants)) {
        foreach ($this->roles as $name => $role) {
          $term_rows[] = _taxonomy_access_format_grant_record($parent_id, $role, $parent_grants);
          $this->setUpAssertions[] = array(
            'grant' => $parent_grants['view'],
            'query' => 'SELECT grant_view FROM {taxonomy_access_term} WHERE tid = :tid AND rid = :rid',
            'args' => array(
              ':tid' => $parent_id,
              ':rid' => $role,
            ),
            'message' => t('Configured grants for term %term, role %role', array(
              '%term' => $parent_name,
              '%role' => $name,
            )),
          );
        }
      }

      // Create child terms.
      foreach ($grant_types as $child_name => $child_grants) {
        $child_name = $parent_name . "__" . $child_name . "_child";
        $this->vocabs[$vocab_name]['terms'][$child_name] = parent::createTerm($child_name, $vocab, $parent_id);
        $child_id = $this->vocabs[$vocab_name]['terms'][$child_name]->tid;

        // Configure grants for the child term for each role.
        if (!empty($child_grants)) {
          foreach ($this->roles as $name => $role) {
            $term_rows[] = _taxonomy_access_format_grant_record($child_id, $role, $child_grants);
            $this->setUpAssertions[] = array(
              'grant' => $child_grants['view'],
              'query' => 'SELECT grant_view FROM {taxonomy_access_term} WHERE tid = :tid AND rid = :rid',
              'args' => array(
                ':tid' => $child_id,
                ':rid' => $role,
              ),
              'message' => t('Configured grants for term %term, role %role', array(
                '%term' => $child_name,
                '%role' => $name,
              )),
            );
          }
        }
      }
    }
  }

  // Set the grants.
  taxonomy_access_set_default_grants($default_rows);
  taxonomy_access_set_term_grants($term_rows);
}