1 node.test NodeAccessRecordsUnitTest::testNodeAccessRecords()

Creates a node and tests the creation of node access rules.

File

core/modules/node/tests/node.test, line 1588
Tests for node.module.

Class

NodeAccessRecordsUnitTest
Tests hook_node_access_records() functionality.

Code

function testNodeAccessRecords() {
  // Create a post node.
  $node1 = $this->backdropCreateNode(array('type' => 'post'));
  $this->assertTrue(node_load($node1->nid), 'Post node created.');

  // Check to see if grants added by node_test_node_access_records made it in.
  $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node1->nid))->fetchAll();
  $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
  $this->assertEqual($records[0]->realm, 'test_post_realm', 'Grant with post_realm acquired for node without alteration.');
  $this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');

  // Create an unpromoted "Page" node.
  $node2 = $this->backdropCreateNode(array('type' => 'page', 'promote' => 0));
  $this->assertTrue(node_load($node2->nid), 'Unpromoted page node created.');

  // Check to see if grants added by node_test_node_access_records made it in.
  $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node2->nid))->fetchAll();
  $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
  $this->assertEqual($records[0]->realm, 'test_page_realm', 'Grant with page_realm acquired for node without alteration.');
  $this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');

  // Create an unpromoted, unpublished "Page" node.
  $node3 = $this->backdropCreateNode(array('type' => 'page', 'promote' => 0, 'status' => 0));
  $this->assertTrue(node_load($node3->nid), 'Unpromoted, unpublished page node created.');

  // Check to see if grants added by node_test_node_access_records made it in.
  $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node3->nid))->fetchAll();
  $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
  $this->assertEqual($records[0]->realm, 'test_page_realm', 'Grant with page_realm acquired for node without alteration.');
  $this->assertEqual($records[0]->gid, 1, 'Grant with gid = 1 acquired for node without alteration.');

  // Create a promoted "Page" node.
  $node4 = $this->backdropCreateNode(array('type' => 'page', 'promote' => 1));
  $this->assertTrue(node_load($node4->nid), 'Promoted page node created.');

  // Check to see if grant added by node_test_node_access_records was altered
  // by node_test_node_access_records_alter.
  $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node4->nid))->fetchAll();
  $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
  $this->assertEqual($records[0]->realm, 'test_alter_realm', 'Altered grant with alter_realm acquired for node.');
  $this->assertEqual($records[0]->gid, 2, 'Altered grant with gid = 2 acquired for node.');

  // Check to see if we can alter grants with hook_node_grants_alter().
  $operations = array('view', 'update', 'delete');
  // Create a user that is allowed to access content.
  $web_user = $this->backdropCreateUser(array('access content'));
  foreach ($operations as $op) {
    $grants = node_test_node_grants($op, $web_user);
    $altered_grants = $grants;
    backdrop_alter('node_grants', $altered_grants, $web_user, $op);
    $this->assertNotEqual($grants, $altered_grants, format_string('Altered the %op grant for a user.', array('%op' => $op)));
  }

  // Check that core does not grant access to an unpublished node when an
  // empty $grants array is returned.
  $node6 = $this->backdropCreateNode(array('status' => 0, 'disable_node_access' => TRUE));
  $records = db_query('SELECT realm, gid FROM {node_access} WHERE nid = :nid', array(':nid' => $node6->nid))->fetchAll();
  $this->assertEqual(count($records), 0, 'Returned no records for unpublished node.');
}