You are here

function DrupalApacheSolrNodeAccess::testIndexing in Apache Solr Search 6.3

Same name and namespace in other branches
  1. 8 apachesolr_access/tests/apachesolr_access.test \DrupalApacheSolrNodeAccess::testIndexing()
  2. 5 contrib/apachesolr_nodeaccess/tests/apachesolr_nodeaccess.test \DrupalApacheSolrNodeAccess::testIndexing()
  3. 6 contrib/apachesolr_nodeaccess/tests/apachesolr_nodeaccess.test \DrupalApacheSolrNodeAccess::testIndexing()
  4. 6.2 contrib/apachesolr_nodeaccess/tests/apachesolr_nodeaccess.test \DrupalApacheSolrNodeAccess::testIndexing()
  5. 7 apachesolr_access/tests/apachesolr_access.test \DrupalApacheSolrNodeAccess::testIndexing()

File

apachesolr_access/tests/apachesolr_access.test, line 41
Unit tests for the access control functionalities that are added by apachesolr_access.

Class

DrupalApacheSolrNodeAccess
@file Unit tests for the access control functionalities that are added by apachesolr_access.

Code

function testIndexing() {
  $basic_user = $this->basic_user;

  // Login as basic user to perform initial content creation.

  //Create 2 nodes
  $edit = array();
  $edit['uid'] = $basic_user->uid;
  $role_restricted_node = $this
    ->drupalCreateNode($edit);
  $edit = array();
  $edit['uid'] = $basic_user->uid;
  $author_restricted_node = $this
    ->drupalCreateNode($edit);

  // Delete the generic node access grant for all nodes.
  db_query("DELETE FROM {node_access} WHERE nid = 0");
  $roles = array_keys($basic_user->roles);

  // The assigned role will be the last in the array.
  $assigned_role = end($roles);
  $role_grant = array(
    'gid' => $assigned_role,
    'realm' => 'nodeaccess_rid',
    'grant_view' => '1',
    'grant_update' => '0',
    'grant_delete' => '0',
  );
  node_access_write_grants($role_restricted_node, array(
    $role_grant,
  ));
  $author_grant = array(
    'gid' => $basic_user->uid,
    'realm' => 'nodeaccess_author',
    'grant_view' => '1',
    'grant_update' => '0',
    'grant_delete' => '0',
  );
  node_access_write_grants($author_restricted_node, array(
    $author_grant,
  ));

  // This loads the document class too.
  $env_id = apachesolr_default_environment(NULL, TRUE);
  $solr = apachesolr_get_solr($env_id);
  $document = new ApacheSolrDocument();
  apachesolr_access_apachesolr_index_document_build_node($document, $role_restricted_node, $env_id);
  $field = 'access_node_' . apachesolr_site_hash() . '_nodeaccess_rid';
  $this
    ->assertEqual($document->{$field}[0], $assigned_role, 'Solr Document being indexed is restricted by the proper role');
  $this
    ->drupalGet('node');
  $document = new ApacheSolrDocument();
  apachesolr_access_apachesolr_index_document_build_node($document, $author_restricted_node, $env_id);
  $field = 'access_node_' . apachesolr_site_hash() . '_nodeaccess_author';
  $this
    ->assertEqual($document->{$field}[0], $basic_user->uid, 'Solr Document being indexed is restricted by the proper author');
  $expected_criterion = array(
    'access__all' => 0,
    'access_node_' . apachesolr_site_hash() . '_all' => 0,
    // The anode_access_test module writes this - copied from core 7.
    'access_node_' . apachesolr_site_hash() . '_node_access_test_author' => $basic_user->uid,
  );

  // Test addition of filters to query.
  $subquery = apachesolr_access_build_subquery($basic_user);
  $fields = $subquery
    ->getFilters();
  foreach ($fields as $field) {
    if (is_array($expected_criterion[$field['#name']])) {
      $this
        ->assertTrue(in_array($field['#value'], $expected_criterion[$field['#name']]), t('Expected node access grant @name == @value found', array(
        '@name' => $field['#name'],
        '@value' => $field['#value'],
      )));

      //This is sorta a bug
      $found_criterion[$field['#name']] = $expected_criterion[$field['#name']];
    }
    else {
      $this
        ->assertEqual($field['#value'], $expected_criterion[$field['#name']], t('Expected node access grant @name == @value found', array(
        '@name' => $field['#name'],
        '@value' => $field['#value'],
      )));
      $found_criterion[$field['#name']] = $expected_criterion[$field['#name']];
    }
  }
  $this
    ->assertEqual($expected_criterion, $found_criterion, 'All Criteria was accounted for in fields. If not accounted for, Unaccounted Criteria [' . var_export(array_diff($expected_criterion, $found_criterion), 1) . ']');

  // Run a query through the MLT code to be sure access filters are added.
  $solr = new DummySolr($url = NULL, $env_id);
  $settings = apachesolr_search_mlt_block_defaults();

  // Dummy value
  $id = apachesolr_document_id($author_restricted_node->nid);
  session_save_session(FALSE);
  $GLOBALS['user'] = $basic_user;
  $response = apachesolr_search_mlt_suggestions($settings, $id, $solr);
  $search = $solr
    ->getLastSearch();

  // Should only be one fq
  $this
    ->assertEqual(count($search['params']['fq']), 1, 'One fq param found');

  // Do some manipulation to avoid having to guess the order.
  $filter = trim(end($search['params']['fq']), ')(');
  $parts = explode(' OR ', $filter);
  $this
    ->assertEqual(count($expected_criterion), count($parts), 'Number of parts is the same as the number of critera');
  foreach ($expected_criterion as $k => $v) {
    $this
      ->assertTrue(in_array("{$k}:{$v}", $parts), "Filter {$k}:{$v} found in the parts");
  }

  // Test reset of index position.
  $this
    ->drupalLogin($this->admin_user);
  $env_id = apachesolr_default_environment(NULL, TRUE);
  apachesolr_set_last_index_position($env_id, 'node', 1, 1);
  $empty = serialize(array());
  $value = db_result(db_query("SELECT value FROM {apachesolr_environment_variable} WHERE env_id = '%s' AND name = '%s'", $env_id, 'apachesolr_index_last'));
  $this
    ->assertNotEqual($value, $empty, 'value is not empty array');
  $this
    ->drupalPost('admin/content/node-settings', array(), t('Rebuild permissions'));
  $value = db_result(db_query("SELECT value FROM {apachesolr_environment_variable} WHERE env_id = '%s' AND name = '%s'", $env_id, 'apachesolr_index_last'));
  $this
    ->assertEqual($value, $empty, 'value is empty array');
}