You are here

private function WorkbenchAccessMenuTestCase::assertMenuUpdateNodes in Workbench Access 7

Helper function for testing node access over the menu hierarchy.

Parameters

$account: A user account object.

$nid: The node id to test for access control.

$depth: The menu depth of the node being tested.

$expected_count: A count of the expected returns based on the known children of this node. See the setup in $this->node_titles.

1 call to WorkbenchAccessMenuTestCase::assertMenuUpdateNodes()
WorkbenchAccessMenuTestCase::testWorkbenchAccessMenu in tests/workbench_access.test

File

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

Class

WorkbenchAccessMenuTestCase

Code

private function assertMenuUpdateNodes($account, $nid, $depth, $expected_count) {

  // Since we change conditions, reset node access.
  drupal_static_reset('node_access');

  // Confirm the account isn't assigned any sections.
  $account = user_load($account->uid, TRUE);
  $this
    ->assertTrue(empty($account->workbench_access), t('Test user not assigned any sections.'));

  // Assign section
  $mlid = db_query("SELECT link_title, mlid FROM {menu_links} WHERE link_path = :link_path", array(
    ':link_path' => 'node/' . $nid,
  ))
    ->fetchObject();
  if (empty($account->workbench_access[$mlid->mlid])) {
    workbench_access_user_section_save($account->uid, $mlid->mlid, 'menu');
  }
  $account = user_load($account->uid, TRUE);
  $this
    ->assertTrue(array_key_exists($mlid->mlid, $account->workbench_access), t('Test user assigned section :section.', array(
    ':section' => $mlid->link_title,
  )));

  // Get an array of editable nodes.
  // Use db_select because of the variable depth.
  $query = db_select('menu_links', 'ml');
  $query
    ->condition('p' . $depth, $mlid->mlid);
  $query
    ->addExpression("SUBSTRING(ml.link_path, 6, (LENGTH(ml.link_path) - 1))", 'nid');
  $count = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertTrue($count == $expected_count, t('!mlids menu items found associated with a !depth-level deep node.', array(
    '!mlids' => $expected_count,
    '!depth' => $depth,
  )));
  $editable_nids = $query
    ->execute()
    ->fetchCol();
  sort($editable_nids);

  // Check update permission on editable nodes.
  foreach ($editable_nids as $nid) {
    $node = node_load($nid, NULL, TRUE);
    $result = node_access('update', $node, $account);
    $this
      ->assertTrue($result, t('Page update allowed on :title.', array(
      ':title' => $node->title,
    )));
  }

  // Get an array of non-editable nodes.
  $non_editable_nids = db_query("SELECT nid FROM {node} WHERE nid NOT IN (:nids)", array(
    ':nids' => $editable_nids,
  ))
    ->fetchCol();

  // Check update permission on non-editable nodes.
  foreach ($non_editable_nids as $nid) {
    $node = node_load($nid, null, TRUE);
    $result = node_access('update', $node, $account);
    $this
      ->assertTrue(!$result, t('Page update disallowed on :title.', array(
      ':title' => $node->title,
    )));
  }

  // Revoke assigned section
  workbench_access_user_section_delete($account->uid, $mlid->mlid, 'menu');
  $account = user_load($account->uid, TRUE);
  $this
    ->assertTrue(empty($account->workbench_access), t('Test user removed from section :section.', array(
    ':section' => $mlid->link_title,
  )));
}