You are here

function AclTest::testNodeAclAddRemoveFromNode in ACL 8

Includes acl_node_add_acl, acl_node_remove_acl, acl_node_clear_acls

File

src/Tests/AclTest.php, line 132

Class

AclTest
Test case for ACL module.

Namespace

Drupal\acl\Tests

Code

function testNodeAclAddRemoveFromNode() {

  // Add nodes.
  $node1 = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    'promote' => 0,
  ));
  $this
    ->assertTrue((bool) Node::load($node1
    ->id()), t('Page node created.'));
  $node2 = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    'promote' => 0,
  ));
  $this
    ->assertTrue((bool) Node::load($node2
    ->id()), t('Page node created.'));
  $node3 = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    'promote' => 0,
  ));
  $this
    ->assertTrue((bool) Node::load($node3
    ->id()), t('Page node created.'));

  // Create an ACL and add nodes.
  $acl_id1 = acl_create_acl('test3', 'test', 1);
  $this
    ->assertNotNull($acl_id1, t('ACL ID was created.'), $group = 'ACL');

  // Add two nodes.
  $query = \Drupal::database()
    ->select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->condition('nid', array(
    $node1
      ->id(),
    $node2
      ->id(),
  ), 'IN');
  acl_add_nodes($query, $acl_id1, 1, 1, 1);
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id1)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 2, t("2 nodes under control ({$count})."), $group = 'ACL');

  // Add a third node.
  acl_node_add_acl($node3
    ->id(), $acl_id1, 1, 1, 1);
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id1)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 3, t('3 nodes under control.'), $group = 'ACL');

  // Add the second node again.
  acl_node_add_acl($node2
    ->id(), $acl_id1, 1, 1, 1);
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id1)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 3, t('Still only 3 nodes under control.'), $group = 'ACL');

  // Remove the second node again.
  acl_node_remove_acl($node2
    ->id(), $acl_id1);
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id1)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 2, t('2 nodes left under control.'), $group = 'ACL');

  // Remove the second node again.
  acl_node_remove_acl($node2
    ->id(), $acl_id1);
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id1)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 2, t('Still 2 nodes left under control.'), $group = 'ACL');

  // Create another ACL and add nodes.
  $acl_id2 = acl_create_acl('test3', 'test', 2);
  $this
    ->assertNotNull($acl_id2, t('ACL ID was created.'), $group = 'ACL');
  acl_node_add_acl($node1
    ->id(), $acl_id2, 1, 1, 1);
  acl_node_add_acl($node2
    ->id(), $acl_id2, 1, 1, 1);
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id2)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 2, t('2 nodes under control.'), $group = 'ACL');

  // Remove a node (which has two ACLs).
  acl_node_clear_acls($node1
    ->id(), 'test3');
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id1)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 1, t('1 node left under control.'), $group = 'ACL');
  $count = \Drupal::database()
    ->select('acl_node')
    ->condition('acl_id', $acl_id2)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 1, t('1 node left under control.'), $group = 'ACL');
}