You are here

public function AckNodeAccessTest::assertTaxonomyAccess in Access Control Kit 7

Utility function to run taxonomy-related access tests.

Parameters

string $scheme_machine_name: The access scheme machine name.

2 calls to AckNodeAccessTest::assertTaxonomyAccess()
AckNodeAccessTest::testFieldAccess in ack_node/ack_node.test
Test controlling access by a field value.
AckNodeAccessTest::testTaxonomyAccess in ack_node/ack_node.test
Test controlling access with the taxonomy index.

File

ack_node/ack_node.test, line 156
Tests for the ACK node module.

Class

AckNodeAccessTest
Tests the node access functions.

Code

public function assertTaxonomyAccess($scheme_machine_name) {
  $scheme_field = 'ack_' . $scheme_machine_name;
  $scheme_url = str_replace('_', '-', $scheme_machine_name);

  // Test node access without any grants.
  drupal_static_reset();
  $this
    ->assertFalse(node_access('create', 'article', $this->ackUser), 'User does not have access to create a node.');
  $this
    ->assertFalse(node_access('update', $this->node, $this->ackUser), 'User does not have access to edit the test node.');
  $this
    ->assertFalse(node_access('delete', $this->node, $this->ackUser), 'User does not have access to delete the test node.');

  // Grant access and retest.
  $edit = array(
    'user' => $this->ackUser->name,
    'role' => $this->ackRoleAny->rid,
    $scheme_field . '[und][1]' => TRUE,
  );
  $this
    ->drupalPost('admin/access/add/' . $scheme_url, $edit, 'Save');
  drupal_static_reset();
  $this
    ->assertFalse(node_access('create', 'article', $this->ackUser), 'User does not have access to create a node.');
  $this
    ->assertTrue(node_access('update', $this->node, $this->ackUser), 'User has access to edit the test node.');
  $this
    ->assertTrue(node_access('delete', $this->node, $this->ackUser), 'User has access to delete the test node.');

  // Change access and retest.
  $edit = array(
    $scheme_field . '[und][1]' => FALSE,
    $scheme_field . '[und][2]' => TRUE,
  );
  $this
    ->drupalPost('admin/access/grant/1/edit', $edit, 'Save');
  drupal_static_reset();
  $this
    ->assertFalse(node_access('create', 'article', $this->ackUser), 'User does not have access to create a node.');
  $this
    ->assertFalse(node_access('update', $this->node, $this->ackUser), 'User does not have access to edit the test node.');
  $this
    ->assertFalse(node_access('delete', $this->node, $this->ackUser), 'User does not have access to delete the test node.');

  // Revoke access and retest.
  $edit = array();
  $this
    ->drupalPost('admin/access/grant/1/delete', $edit, 'Delete');
  drupal_static_reset();
  $this
    ->assertFalse(node_access('create', 'article', $this->ackUser), 'User does not have access to create a node.');
  $this
    ->assertFalse(node_access('update', $this->node, $this->ackUser), 'User does not have access to edit the test node.');
  $this
    ->assertFalse(node_access('delete', $this->node, $this->ackUser), 'User does not have access to delete the test node.');

  // Grant access through the other role and retest.
  $edit = array(
    'user' => $this->ackUser->name,
    'role' => $this->ackRoleOwn->rid,
    $scheme_field . '[und][1]' => TRUE,
  );
  $this
    ->drupalPost('admin/access/add/' . $scheme_url, $edit, 'Save');
  drupal_static_reset();
  $this
    ->assertTrue(node_access('create', 'article', $this->ackUser), 'User has access to create a node.');
  $this
    ->assertFalse(node_access('update', $this->node, $this->ackUser), 'User does not have access to edit the test node.');
  $this
    ->assertFalse(node_access('delete', $this->node, $this->ackUser), 'User does not have access to delete the test node.');

  // Change the node's owner and retest.
  $this->node->uid = $this->ackUser->uid;
  node_save($this->node);
  drupal_static_reset();
  $this
    ->assertTrue(node_access('create', 'article', $this->ackUser), 'User has access to create a node.');
  $this
    ->assertTrue(node_access('update', $this->node, $this->ackUser), 'User has access to edit the test node.');
  $this
    ->assertTrue(node_access('delete', $this->node, $this->ackUser), 'User has access to delete the test node.');

  // Change access and retest.
  $edit = array(
    $scheme_field . '[und][1]' => FALSE,
    $scheme_field . '[und][2]' => TRUE,
  );
  $this
    ->drupalPost('admin/access/grant/2/edit', $edit, 'Save');
  drupal_static_reset();
  $this
    ->assertTrue(node_access('create', 'article', $this->ackUser), 'User has access to create a node.');
  $this
    ->assertFalse(node_access('update', $this->node, $this->ackUser), 'User does not have access to edit the test node.');
  $this
    ->assertFalse(node_access('delete', $this->node, $this->ackUser), 'User does not have access to delete the test node.');

  // Log in as the ACK user and test realm filtering on the node form.
  $this
    ->drupalLogin($this->ackUser);
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertFieldChecked('edit-field-tags-und-2', 'The only available realm is preselected.');
  $disabled_field = $this
    ->xpath('//input[@id=:id and @disabled="disabled"]', array(
    ':id' => 'edit-field-tags-und-2',
  ));
  $this
    ->assertTrue($disabled_field, 'The realm field is disabled.');
  $this
    ->assertNoFieldById('edit-field-tags-und-1', '', 'The inaccessible realm option was removed.');
  $edit = array(
    'title' => $this
      ->randomName(),
    'body[und][0][value]' => $this
      ->randomName(),
  );
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertText('Article ' . $edit['title'] . ' has been created.', 'User is able to create content in the assigned realm.');
  $this
    ->drupalGet('node/2/edit');
  $this
    ->assertTitle('Edit Article ' . $edit['title'] . ' | Drupal', 'User is able to edit own content in the assigned realm.');
  $this
    ->assertFieldChecked('edit-field-tags-und-2', 'The accessible realm is selected.');
  $disabled_field = $this
    ->xpath('//input[@id=:id and @disabled="disabled"]', array(
    ':id' => 'edit-field-tags-und-2',
  ));
  $this
    ->assertTrue($disabled_field, 'The accessible realm option is disabled.');
  $this
    ->assertNoFieldChecked('edit-field-tags-und-1', 'The inaccessible realm is not selected.');
  $disabled_field = $this
    ->xpath('//input[@id=:id and @disabled="disabled"]', array(
    ':id' => 'edit-field-tags-und-1',
  ));
  $this
    ->assertTrue($disabled_field, 'The inaccessible realm option is disabled.');
  $edit['body[und][0][value]'] = $this
    ->randomName();
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertText('Article ' . $edit['title'] . ' has been updated.', 'User is able to update content in the assigned realm.');

  // Log in as a user with global node access and retest.
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'create article content',
    'edit own article content',
  ));
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('node/add/article');
  $disabled_field = $this
    ->xpath('//input[@id=:id and @disabled="disabled"]', array(
    ':id' => 'edit-field-tags-und-2',
  ));
  $this
    ->assertFalse($disabled_field, 'The realm field is not disabled.');
  $this
    ->assertNoFieldChecked('edit-field-tags-und-1');
  $this
    ->assertNoFieldChecked('edit-field-tags-und-2', 'The realm was not preselected.');
  $edit = array(
    'title' => $this
      ->randomName(),
    'body[und][0][value]' => $this
      ->randomName(),
    'field_tags[und][2]' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertText('Article ' . $edit['title'] . ' has been created.', 'User is able to create content.');
  $this
    ->drupalGet('node/3/edit');
  $this
    ->assertTitle('Edit Article ' . $edit['title'] . ' | Drupal', 'User is able to edit own content.');
  $disabled_field = $this
    ->xpath('//input[@id=:id and @disabled="disabled"]', array(
    ':id' => 'edit-field-tags-und-2',
  ));
  $this
    ->assertFalse($disabled_field, 'The realm field is not disabled.');
  $this
    ->assertNoFieldChecked('edit-field-tags-und-1');
  $this
    ->assertFieldChecked('edit-field-tags-und-2', 'The realm is selected.');
  $edit['body[und][0][value]'] = $this
    ->randomName();
  $this
    ->drupalPost(NULL, $edit, 'Save');
  $this
    ->assertText('Article ' . $edit['title'] . ' has been updated.', 'User is able to update content.');
}