diff --git a/src/Tests/HooksTest.php b/src/Tests/HooksTest.php index 9e1a1a4..bc66fff 100644 --- a/src/Tests/HooksTest.php +++ b/src/Tests/HooksTest.php @@ -2,13 +2,11 @@ /** * @file - * Contains \Drupal\search_api\Tests\IntegrationTest. + * Contains \Drupal\search_api\Tests\HooksTest. */ namespace Drupal\search_api\Tests; -use Drupal\Component\Utility\Unicode; - /** * Tests integration of hooks. * @@ -22,13 +20,6 @@ class HooksTest extends WebTestBase { public static $modules = array('node', 'search_api', 'search_api_test_backend', 'search_api_test_views', 'search_api_test_hooks'); /** - * The id of the server. - * - * @var string - */ - protected $serverId; - - /** * The id of the index. * * @var string @@ -40,16 +31,18 @@ class HooksTest extends WebTestBase { */ public function testHooks() { // Create some nodes. - $this->drupalCreateNode(array('type' => 'page')); - $this->drupalCreateNode(array('type' => 'page')); - $this->drupalCreateNode(array('type' => 'page')); + $this->drupalCreateNode(array('type' => 'page', 'title' => 'node - 1')); + $this->drupalCreateNode(array('type' => 'page', 'title' => 'node - 2')); + $this->drupalCreateNode(array('type' => 'page', 'title' => 'node - 3')); + $this->drupalCreateNode(array('type' => 'page', 'title' => 'node - 4')); // Log in, so we can test all the things. $this->drupalLogin($this->adminUser); // Create an index and server to work with. - $this->createServer(); - $this->createIndex(); + $this->getTestServer(); + $index = $this->getTestIndex(); + $this->indexId = $index->id(); // hook_search_api_backend_info_alter was triggered. $this->drupalGet('admin/config/search/search-api/add-server'); @@ -66,9 +59,10 @@ class HooksTest extends WebTestBase { $this->drupalGet($this->getIndexPath()); $this->drupalPostForm(NULL, [], $this->t('Index now')); - // hook_search_api_index_items_alter was triggered. + // hook_search_api_index_items_alter was triggered, this removed node:1. + // hook_search_api_query_TAG_alter was triggered, this removed node:3. $this->assertText('There are 2 items indexed on the server for this index.'); - $this->assertText('Successfully indexed 3 items.'); + $this->assertText('Successfully indexed 4 items.'); $this->assertText('Stormy'); // hook_search_api_items_indexed was triggered. @@ -91,53 +85,8 @@ class HooksTest extends WebTestBase { $this->assertText('Funky blue note'); // hook_search_api_results_alter was triggered. $this->assertText('Stepping into tomorrow'); - // hook_search_api_views_query_alter was triggered. - $this->assertText('Andrew hill break'); - } - - /** - * Creates a search server. - */ - protected function createServer() { - $this->serverId = Unicode::strtolower($this->randomMachineName()); - - $this->drupalGet('admin/config/search/search-api/add-server'); - - $edit = array( - 'name' => 'Search API test server', - 'id' => $this->serverId, - 'status' => 1, - 'description' => 'A server used for testing.', - 'backend' => 'search_api_test_backend', - ); - - $this->drupalPostForm(NULL, $edit, $this->t('Save')); - - $this->assertText($this->t('The server was successfully saved.')); - $this->assertUrl('admin/config/search/search-api/server/' . $this->serverId, array(), 'Correct redirect to server page.'); - } - - /** - * Creates an index. - */ - protected function createIndex() { - $this->drupalGet('admin/config/search/search-api/add-index'); - - $this->indexId = 'test_index'; - - $edit = array( - 'name' => 'Search API test index', - 'id' => $this->indexId, - 'status' => 1, - 'description' => 'An index used for testing.', - 'server' => $this->serverId, - 'datasources[]' => array('entity:node'), - ); - - $this->drupalPostForm(NULL, $edit, $this->t('Save')); - - $this->assertText($this->t('The index was successfully saved.')); - $this->assertUrl($this->getIndexPath(), array(), 'Correct redirect to index page.'); + // hook_search_api_results_TAG_alter was triggered. + $this->assertText('Llama'); } /** diff --git a/tests/search_api_test_hooks/search_api_test_hooks.module b/tests/search_api_test_hooks/search_api_test_hooks.module index 44a830f..d400acd 100644 --- a/tests/search_api_test_hooks/search_api_test_hooks.module +++ b/tests/search_api_test_hooks/search_api_test_hooks.module @@ -46,6 +46,7 @@ function search_api_test_hooks_search_api_items_indexed(\Drupal\search_api\Index */ function search_api_test_hooks_search_api_query_alter(\Drupal\search_api\Query\QueryInterface &$query) { drupal_set_message('Funky blue note'); + $query->addTag('andrew_hill'); } /** @@ -56,13 +57,6 @@ function search_api_test_hooks_search_api_results_alter(\Drupal\search_api\Query } /** - * Implements hook_search_api_views_query_alter. - */ -function search_api_test_hooks_search_api_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\search_api\Query\QueryInterface &$query) { - drupal_set_message('Andrew hill break'); -} - -/** * Implements hook_search_api_index_reindex. */ function search_api_test_hooks_search_api_index_reindex(\Drupal\search_api\IndexInterface $index, $clear = FALSE) { @@ -85,3 +79,26 @@ function search_api_test_hooks_search_api_field_type_mapping_alter(array &$mappi $mapping['datetime_iso8601'] = NULL; $mapping['timestamp'] = NULL; } + +/** + * Implements hook_search_api_query_TAG_alter. + */ +function search_api_test_hooks_search_api_query_andrew_hill_alter(\Drupal\search_api\Query\QueryInterface &$query) { + // Exclude the node with ID 2 from the search results. + $fields = $query->getIndex()->getFields(); + foreach ($query->getIndex()->getDatasources() as $datasource_id => $datasource) { + if ($datasource->getEntityTypeId() == 'node') { + $field = \Drupal\search_api\Utility::createCombinedId($datasource_id, 'nid'); + if (isset($fields[$field])) { + $query->addCondition($field, 2, '<>'); + } + } + } +} + +/** + * Implements hook_search_api_results_TAG_alter. + */ +function search_api_test_hooks_search_api_results_andrew_hill_alter(\Drupal\search_api\Query\ResultSetInterface &$results) { + drupal_set_message('Llama'); +}