<?php
// $Id: apachesolr_nodeaccess.test,v 1.1.2.3 2010/05/01 04:42:17 jpmckinney Exp $

class DrupalApacheSolrNodeAccess extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => 'Node Access',
      'description' => 'Test Access Control',
      'group' => 'ApacheSolr'
    );
  }

  function setUp() {
    parent::setUp('nodeaccess', 'apachesolr', 'apachesolr_search', 'apachesolr_nodeaccess');

     // Create a basic user, which is subject to moderation.
    $permissions = array(
      'access content',
      'create page content',
      'edit own page content',
      'create story content',
      'edit own story content',
    );
    $this->basic_user = $this->drupalCreateUser($permissions);
  }

  function testIndexing() {
    $basic_user = $this->basic_user;
    // Login as basic user to perform initial content creation.
    $this->drupalLogin($basic_user);

    //Create 2 nodes
    $edit = array();
    $edit['title'] = $this->randomName(32);
    $edit['body']  = $this->randomName(32);
    $role_restricted_node = $this->drupalCreateNode($edit);

    $edit = array();
    $edit['title'] = $this->randomName(32);
    $edit['body']  = $this->randomName(32);
    $author_restricted_node = $this->drupalCreateNode($edit);

    $this->drupalLogout();

    $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), 'nodeaccess_rid');

    $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), 'nodeaccess_author');

    $include_path = get_include_path();
    set_include_path('./'. drupal_get_path('module', 'apachesolr') .'/SolrPhpClient/');
    include_once('Apache/Solr/Service.php');
    set_include_path($include_path);

    $document = new Apache_Solr_Document();
    apachesolr_nodeaccess_apachesolr_update_index($document, $role_restricted_node, 'apachesolr_search');
    $field = 'nodeaccess_' . apachesolr_site_hash() . '_nodeaccess_rid';
    $this->assertEqual($document->{$field}[0], $assigned_role, 'Solr Document being indexed is restricted by the proper role');

    $document = new Apache_Solr_Document();
    apachesolr_nodeaccess_apachesolr_update_index($document, $author_restricted_node, 'apachesolr_search');
    $field = 'nodeaccess_' . apachesolr_site_hash() . '_nodeaccess_author';
    $this->assertEqual($document->{$field}[0], $basic_user->uid, 'Solr Document being indexed is restricted by the proper author');
  }

  function testQuery() {
    $basic_user = $this->basic_user;
    // Login as basic user
    $this->drupalLogin($basic_user);

    include_once drupal_get_path('module', 'apachesolr') .'/Solr_Base_Query.php';
    $query = apachesolr_current_query();
    $params = array();

    $subquery = _apachesolr_nodeaccess_build_subquery($basic_user);

    $roles = array_keys($basic_user->roles);
    $assigned_role = end($roles);

    $expected_criterion = array(
      'nodeaccess_all' => 0,
      'nodeaccess_' . apachesolr_site_hash() . '_all' => 0,
      'nodeaccess_' . apachesolr_site_hash() . '_nodeaccess_rid' => array(2, $assigned_role),
      'nodeaccess_' . apachesolr_site_hash() . '_nodeaccess_uid' => $basic_user->uid,
      'nodeaccess_' . apachesolr_site_hash() . '_nodeaccess_author' => $basic_user->uid,
    );

    $fields = $subquery->get_filters();

    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) . ']');
  }
}
