apachesolr_access.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @file
  4. * Unit tests for the access control functionalities that are added by
  5. * apachesolr_access.
  6. */
  7. class DrupalApacheSolrNodeAccess extends DrupalWebTestCase {
  8. /**
  9. * Gets Information about the DrupalApacheSolrNodeAccess test
  10. *
  11. * @return array
  12. * Information such as name, description and group it belongs to
  13. */
  14. public static function getInfo() {
  15. return array(
  16. 'name' => 'Node Access',
  17. 'description' => 'Test Access Control',
  18. 'group' => 'ApacheSolr'
  19. );
  20. }
  21. /**
  22. * Defines what is required to start the DrupalApacheSolrNodeAccess test.
  23. */
  24. function setUp() {
  25. parent::setUp('node_access_test', 'apachesolr', 'apachesolr_search', 'apachesolr_access');
  26. // Create a basic user, which is subject to moderation.
  27. $permissions = array(
  28. 'access content',
  29. 'create page content',
  30. 'edit own page content',
  31. 'create article content',
  32. 'edit own article content',
  33. );
  34. $this->basic_user = $this->drupalCreateUser($permissions);
  35. // Create an admin user.
  36. $permissions = array(
  37. 'access content',
  38. 'search content',
  39. 'administer nodes',
  40. 'administer search',
  41. 'access administration pages',
  42. );
  43. $this->admin_user = $this->drupalCreateUser($permissions);
  44. }
  45. /**
  46. * Tests indexing and check if it adds the correct grants for those specific users
  47. */
  48. function testIndexing() {
  49. $basic_user = $this->basic_user;
  50. // Login as basic user to perform initial content creation.
  51. //Create 2 nodes
  52. $edit = array();
  53. $edit['uid'] = $basic_user->uid;
  54. $role_restricted_node = $this->drupalCreateNode($edit);
  55. $edit = array();
  56. $edit['uid'] = $basic_user->uid;
  57. $author_restricted_node = $this->drupalCreateNode($edit);
  58. // Delete the generic node access grant for all nodes.
  59. db_delete('node_access')->condition('nid', '0')->execute();
  60. $roles = array_keys($basic_user->roles);
  61. // The assigned role will be the last in the array.
  62. $assigned_role = end($roles);
  63. $role_grant = array(
  64. 'gid' => $assigned_role,
  65. 'realm' => 'nodeaccess_rid',
  66. 'grant_view' => '1',
  67. 'grant_update' => '0',
  68. 'grant_delete' => '0',
  69. );
  70. node_access_write_grants($role_restricted_node, array($role_grant));
  71. $author_grant = array(
  72. 'gid' => $basic_user->uid,
  73. 'realm' => 'nodeaccess_author',
  74. 'grant_view' => '1',
  75. 'grant_update' => '0',
  76. 'grant_delete' => '0',
  77. );
  78. node_access_write_grants($author_restricted_node, array($author_grant));
  79. // This loads the document class too.
  80. $env_id = apachesolr_default_environment();
  81. $solr = apachesolr_get_solr($env_id);
  82. $document = new ApacheSolrDocument();
  83. apachesolr_access_apachesolr_index_document_build_node($document, $role_restricted_node, $env_id);
  84. $field = 'access_node_' . apachesolr_site_hash() . '_nodeaccess_rid';
  85. $this->assertEqual($document->{$field}[0], $assigned_role, 'Solr Document being indexed is restricted by the proper role' . print_r(db_query('SELECT * FROM {node_access}')->fetchAllAssoc('nid'), 1));
  86. $this->drupalGet('node');
  87. $document = new ApacheSolrDocument();
  88. apachesolr_access_apachesolr_index_document_build_node($document, $author_restricted_node, $env_id);
  89. $field = 'access_node_' . apachesolr_site_hash() . '_nodeaccess_author';
  90. $this->assertEqual($document->{$field}[0], $basic_user->uid, 'Solr Document being indexed is restricted by the proper author');
  91. $expected_criterion = array(
  92. 'access__all' => 0,
  93. 'access_node_' . apachesolr_site_hash() . '_all' => 0,
  94. // The node_access_test module writes this as of core 7.3.
  95. 'access_node_' . apachesolr_site_hash() . '_node_access_test_author' => $basic_user->uid,
  96. );
  97. // Test addition of filters to query.
  98. $subquery = apachesolr_access_build_subquery($basic_user);
  99. $fields = $subquery->getFilters();
  100. foreach ($fields as $field) {
  101. if (is_array($expected_criterion[$field['#name']])) {
  102. $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'])));
  103. //This is sorta a bug
  104. $found_criterion[$field['#name']] = $expected_criterion[$field['#name']];
  105. }
  106. else {
  107. $this->assertEqual($field['#value'], $expected_criterion[$field['#name']], t('Expected node access grant @name == @value found', array('@name' => $field['#name'], '@value' => $field['#value'])));
  108. $found_criterion[$field['#name']] = $expected_criterion[$field['#name']];
  109. }
  110. }
  111. $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) . ']');
  112. // Run a query through the MLT code to be sure access filters are added.
  113. $solr = new DummySolr($url = NULL, $env_id);
  114. $settings = apachesolr_search_mlt_block_defaults();
  115. // Dummy value
  116. $id = apachesolr_document_id($author_restricted_node->nid);
  117. drupal_save_session(false);
  118. $GLOBALS['user'] = $basic_user;
  119. $response = apachesolr_search_mlt_suggestions($settings, $id, $solr);
  120. $search = $solr->getLastSearch();
  121. // Should only be one fq
  122. $this->assertEqual(count($search['params']['fq']), 1, 'One fq param found');
  123. // Do some manipulation to avoid having to guess the order.
  124. $filter = trim(end($search['params']['fq']), ')(');
  125. $parts = explode(' OR ', $filter);
  126. $this->assertEqual(count($expected_criterion), count($parts), 'Number of parts is the same as the number of critera');
  127. foreach ($expected_criterion as $k => $v) {
  128. $this->assertTrue(in_array("$k:$v", $parts), "Filter $k:$v found in the parts");
  129. }
  130. // Test reset of index position.
  131. $this->drupalLogin($this->admin_user);
  132. $env_id = apachesolr_default_environment();
  133. apachesolr_set_last_index_position($env_id, 'node', 1, 1);
  134. $empty = serialize(array());
  135. $value = db_query('SELECT value FROM {apachesolr_environment_variable} WHERE env_id = :env_id AND name = :name',
  136. array(':env_id' => $env_id, ':name' => 'apachesolr_index_last'))->fetchField();
  137. $this->assertNotEqual($value, $empty, 'value is not empty array');
  138. $this->drupalPost('admin/reports/status/rebuild', array(), t('Rebuild permissions'));
  139. $value = db_query('SELECT value FROM {apachesolr_environment_variable} WHERE env_id = :env_id AND name = :name',
  140. array(':env_id' => $env_id, ':name' => 'apachesolr_index_last'))->fetchField();
  141. $this->assertEqual($value, $empty, 'value is empty array');
  142. }
  143. }