You are here

function ABTPermissionGrantsTestCase::accessGrants in Access By Term 7

1 call to ABTPermissionGrantsTestCase::accessGrants()
ABTPermissionGrantsTestCase::testGrants in ./abt.test

File

./abt.test, line 350

Class

ABTPermissionGrantsTestCase

Code

function accessGrants($authenticateUsers = true) {
  $flag_map[$this->fields[0]['field_name']] = array(
    'v' => 1,
    'u' => 1,
    'd' => 1,
  );
  $flag_map[$this->fields[1]['field_name']] = array(
    'v' => 1,
    'u' => 0,
    'd' => 0,
  );
  $flag_map[$this->fields[2]['field_name']] = array(
    'v' => 0,
    'u' => 1,
    'd' => 0,
  );
  $flag_map[$this->fields[3]['field_name']] = array(
    'v' => 0,
    'u' => 0,
    'd' => 1,
  );
  $flag_map[$this->fields[4]['field_name']] = array(
    'v' => 1,
    'u' => 0,
    'd' => 1,
  );
  for ($x = 0; $x < count($this->fields); $x++) {
    $field_name = $this->fields[$x]['field_name'];
    $v = $flag_map[$field_name]['v'];
    $u = $flag_map[$field_name]['u'];
    $d = $flag_map[$field_name]['d'];

    // loop trough terms and create users
    $user_map = array();
    $node_map = array();
    $expect_map = array();
    for ($i = 0; $i < count($this->terms); $i++) {
      $fields = field_read_fields();
      foreach ($fields as $field_name => $field) {
        unset($field['settings']['abt_map']);
        field_update_field($field);
      }

      // clean slate (for good measure)
      $field = field_info_field($field_name);
      $field_abt_map = array(
        'ctrl_view_access' => $v,
        'ctrl_update_access' => $u,
        'ctrl_delete_access' => $d,
      );
      $field['settings']['abt_map'] = $field_abt_map;
      field_update_field($field);
      node_access_rebuild();
      $term_list_ordered = $this->terms[$i];

      // Make sure we have some nodes and users without access flags.
      array_push($term_list_ordered, (object) array(
        'tid' => FALSE,
      ));
      array_unshift($term_list_ordered, (object) array(
        'tid' => FALSE,
      ));
      $term_list_reversed = array_reverse($term_list_ordered);
      for ($j = 0; $j < count($term_list_ordered); $j++) {
        $user_map[$i][$j] = $this
          ->drupalCreateUser();
        $node_map[$i][$j] = $this
          ->drupalCreateNode(array(
          'type' => $this->content_type->type,
        ));
        $usr = user_load($user_map[$i][$j]->uid);
        $nde = node_load($node_map[$i][$j]->nid);
        $term_list_ordered[$j]->tid !== FALSE && ($nde->{$field_name}['und'][0]['tid'] = $term_list_ordered[$j]->tid);
        $term_list_reversed[$j]->tid !== FALSE && ($usr->{$field_name}['und'][0]['tid'] = $term_list_reversed[$j]->tid);
        user_save($usr);
        node_save($nde);

        /*
         * Blocks bellow basicly say:
         * if either node or user lacks access flag = expect "access denied".
         */

        // User has no access tags && Node has access tags
        if ($term_list_reversed[$j]->tid === FALSE && $term_list_ordered[$j]->tid !== FALSE) {
          $expect_access[$i][$j] = FALSE;

          // User has no access tags && Node has no access tags
        }
        elseif ($term_list_reversed[$j]->tid === FALSE && $term_list_ordered[$j]->tid === FALSE) {
          $expect_access[$i][$j] = FALSE;

          // User has access tags && Node has no access tags
        }
        elseif ($term_list_reversed[$j]->tid !== FALSE && $term_list_ordered[$j]->tid === FALSE) {
          $expect_access[$i][$j] = FALSE;
        }
        else {
          $expect_access[$i][$j] = $term_list_reversed[$j]->tid <= $term_list_ordered[$j]->tid;
        }
        $authenticateUsers && $this
          ->drupalLogin($user_map[$i][$j]);
        $this
          ->drupalGet('node/' . $node_map[$i][$j]->nid);
        $text_to_expect = $authenticateUsers && $v == 1 && $expect_access[$i][$j] ? $nde->title : t('Access denied');
        $msg = $authenticateUsers && $v == 1 && $expect_access[$i][$j] ? t('View: access allowed') : t('View: access denied');
        $this
          ->assertText($text_to_expect, $msg . ' (' . $v . $u . $d . ') user-tid:' . ($term_list_reversed[$j]->tid === FALSE ? 'none' : $term_list_reversed[$j]->tid) . ' <= node-tid:' . ($term_list_ordered[$j]->tid === FALSE ? 'none' : $term_list_ordered[$j]->tid));
        $this
          ->drupalGet('node/' . $node_map[$i][$j]->nid . '/edit');
        $text_to_expect = $authenticateUsers && $u == 1 && $expect_access[$i][$j] ? $nde->title : t('Access denied');
        $msg = $authenticateUsers && $u == 1 && $expect_access[$i][$j] ? t('Update: access allowed') : t('Update: access denied');
        $this
          ->assertText($text_to_expect, $msg . ' (' . $v . $u . $d . ') user-tid:' . ($term_list_reversed[$j]->tid === FALSE ? 'none' : $term_list_reversed[$j]->tid) . ' <= node-tid:' . ($term_list_ordered[$j]->tid === FALSE ? 'none' : $term_list_ordered[$j]->tid));
        $this
          ->drupalGet('node/' . $node_map[$i][$j]->nid . '/delete');
        $text_to_expect = $authenticateUsers && $d == 1 && $expect_access[$i][$j] ? $nde->title : t('Access denied');
        $msg = $authenticateUsers && $d == 1 && $expect_access[$i][$j] ? t('Delete: access allowed') : t('Delete: access denied');
        $this
          ->assertText($text_to_expect, $msg . ' (' . $v . $u . $d . ') user-tid:' . ($term_list_reversed[$j]->tid === FALSE ? 'none' : $term_list_reversed[$j]->tid) . ' <= node-tid:' . ($term_list_ordered[$j]->tid === FALSE ? 'none' : $term_list_ordered[$j]->tid));
        $authenticateUsers && $this
          ->drupalLogout($user_map[$i][$j]);
      }
    }
  }
}