You are here

function WorkbenchAccessTokenTestCase::testTaxonomyTokens in Workbench Access 7

File

tests/workbench_access.test, line 865
Test file for Workbench Access.

Class

WorkbenchAccessTokenTestCase

Code

function testTaxonomyTokens() {
  $assigned_node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  module_load_include('inc', 'workbench_access', 'admin');
  workbench_access_example_taxonomy();
  $assigned_node = node_load($assigned_node->nid);

  // Fetch a list of additional sections to be used in the tests.
  $terms = taxonomy_get_term_by_name('Exhibits');
  $terms += taxonomy_get_term_by_name('Library');
  $terms += taxonomy_get_term_by_name('Gift Shop');

  // Test tokens for a node that has been assigned to the Museum section only.
  $tokens = array(
    'workbench-access-sections' => 'Museum',
    'workbench-access-sections:keys' => 'workbench_access',
    'workbench-access-sections:count' => 1,
  );
  $this
    ->assertTokens('node', array(
    'node' => $assigned_node,
  ), $tokens);
  $assigned_node->workbench_access = array_keys($terms);
  node_save($assigned_node);

  // Test tokens now that the node has multiple section assignments.
  $assigned_node = node_load($assigned_node->nid);
  $tokens = array(
    'workbench-access-sections' => 'Exhibits, Library, Gift Shop',
    'workbench-access-sections:keys' => implode(', ', array_keys($terms)),
    'workbench-access-sections:count' => 3,
    'workbench-access-sections:first' => 'Exhibits',
    'workbench-access-sections:last' => 'Gift Shop',
  );
  $this
    ->assertTokens('node', array(
    'node' => $assigned_node,
  ), $tokens);

  // Test tokens for a node that has not been assigned to any sections.
  $unassigned_node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $tokens = array(
    'workbench-access-sections' => 'Unassigned',
    'workbench-access-sections:keys' => NULL,
    'workbench-access-sections:count' => NULL,
  );
  $this
    ->assertTokens('node', array(
    'node' => $unassigned_node,
  ), $tokens);

  // Test tokens for a node that is not under section access control.
  $unassigned_node = $this
    ->drupalCreateNode(array(
    'type' => 'page',
  ));
  $tokens = array(
    'workbench-access-sections' => NULL,
    'workbench-access-sections:keys' => NULL,
    'workbench-access-sections:count' => NULL,
  );
  $this
    ->assertTokens('node', array(
    'node' => $unassigned_node,
  ), $tokens);

  // Test tokens for a user that has been assigned to the Museum section only.
  $admin_user = user_load(1);
  $tokens = array(
    'workbench-access-sections' => 'Museum',
    'workbench-access-sections:keys' => 'workbench_access',
    'workbench-access-sections:count' => 1,
  );
  $this
    ->assertTokens('user', array(
    'user' => $admin_user,
  ), $tokens);

  // Change the sections for the user.
  foreach ($terms as $term) {
    workbench_access_user_section_save($admin_user->uid, $term->tid, 'taxonomy');
  }
  workbench_access_user_section_delete($admin_user->uid, 'workbench_access', 'taxonomy');

  // Test tokens now that the user has multiple section assignments.
  $admin_user = user_load(1);
  $tokens = array(
    'workbench-access-sections' => 'Exhibits, Library, Gift Shop',
    'workbench-access-sections:keys' => implode(', ', array_keys($terms)),
    'workbench-access-sections:count' => 3,
    'workbench-access-sections:first' => 'Exhibits',
    'workbench-access-sections:last' => 'Gift Shop',
  );
  $this
    ->assertTokens('user', array(
    'user' => $admin_user,
  ), $tokens);

  // Test tokens for a user that is not assigned to any sections.
  $unassigned_user = $this
    ->drupalCreateUser();
  $tokens = array(
    'workbench-access-sections' => NULL,
    'workbench-access-sections:keys' => NULL,
    'workbench-access-sections:count' => NULL,
  );
  $this
    ->assertTokens('user', array(
    'user' => $unassigned_user,
  ), $tokens);

  // Test the site-wide access scheme tokens.
  $tokens = array(
    'workbench-access-scheme' => t('Taxonomy'),
    'workbench-access-scheme:name' => t('Taxonomy'),
    'workbench-access-scheme:machine-name' => 'taxonomy',
    'workbench-access-scheme:description' => t('Uses taxonomy vocabularies for assigning hierarchical access control.'),
  );
  $this
    ->assertTokens('site', array(), $tokens);

  // Change the site-wide scheme to menu.module.
  variable_set('workbench_access', 'menu');
  $tokens = array(
    'workbench-access-scheme' => t('Menu'),
    'workbench-access-scheme:name' => t('Menu'),
    'workbench-access-scheme:machine-name' => 'menu',
    'workbench-access-scheme:description' => t('Uses the menu system for assigning hierarchical access control.'),
  );
  $this
    ->assertTokens('site', array(), $tokens);
}