diff --git a/search_api.api.php b/search_api.api.php index 87e25ce..4c515da 100644 --- a/search_api.api.php +++ b/search_api.api.php @@ -78,7 +78,7 @@ function hook_search_api_processor_info_alter(array &$processors) { */ function hook_search_api_data_type_info_alter(array &$data_type_definitions) { if (isset($data_type_definitions['text'])) { - $data_type_definitions['text'] = t('Parsed text'); + $data_type_definitions['text']['label'] = t('Parsed text'); } } diff --git a/src/Tests/HooksTest.php b/src/Tests/HooksTest.php new file mode 100644 index 0000000..5ec28a5 --- /dev/null +++ b/src/Tests/HooksTest.php @@ -0,0 +1,170 @@ +indexStorage = \Drupal::entityManager()->getStorage('search_api_index'); + } + + /** + * Tests various operations via the Search API's admin UI. + */ + public function testHooks() { + $server_add_form = $this->urlGenerator->generateFromRoute('entity.search_api_server.add_form', array(), array('absolute' => TRUE)); + $index_add_form = $this->urlGenerator->generateFromRoute('entity.search_api_index.add_form', array(), array('absolute' => TRUE)); + + // Create some nodes. + $this->drupalCreateNode(array('type' => 'page')); + $this->drupalCreateNode(array('type' => 'page')); + $this->drupalCreateNode(array('type' => 'page')); + + // 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(); + + // hook_search_api_backend_info_alter was triggered. + $this->drupalGet($server_add_form); + $this->assertText('Slims return'); + + // hook_search_api_datasource_info_alter was triggered. + $this->drupalGet($index_add_form); + $this->assertText('Distant land'); + + // hook_search_api_processor_info_alter was triggered. + $this->drupalGet($this->getIndexPath('processors')); + $this->assertText('Mystic bounce'); + + // hook_search_api_index_items_alter was triggered. + $this->drupalGet($this->getIndexPath()); + $this->drupalPostForm(NULL, [], $this->t('Index now')); + $this->assertText('Stormy'); + // hook_search_api_items_indexed was triggered. + $this->assertText('Please set me at ease'); + + // hook_search_api_index_reindex was triggered. + $this->drupalGet($this->getIndexPath('reindex')); + $this->drupalPostForm(NULL, [], $this->t('Confirm')); + $this->assertText('Montara'); + + // hook_search_api_data_type_info_alter was triggered. + $this->drupalGet($this->getIndexPath('fields')); + $this->assertText('Peace/Dolphin dance'); + + $this->drupalGet('search-api-test-fulltext'); + // hook_search_api_query_alter was triggered. + $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()); + $settings_path = $this->urlGenerator->generateFromRoute('entity.search_api_server.add_form', array(), array('absolute' => TRUE)); + + $this->drupalGet($settings_path); + + $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() { + $settings_path = $this->urlGenerator->generateFromRoute('entity.search_api_index.add_form', array(), array('absolute' => TRUE)); + + $this->drupalGet($settings_path); + + $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.'); + } + + /** + * Returns the system path for the test index. + * + * @param string|null $tab + * (optional) If set, the path suffix for a specific index tab. + * + * @return string + * A system path. + */ + protected function getIndexPath($tab = NULL) { + $path = 'admin/config/search/search-api/index/' . $this->indexId; + if ($tab) { + $path .= "/$tab"; + } + return $path; + } + +} diff --git a/tests/search_api_test_hooks/search_api_test_hooks.info.yml b/tests/search_api_test_hooks/search_api_test_hooks.info.yml new file mode 100644 index 0000000..f05b43b --- /dev/null +++ b/tests/search_api_test_hooks/search_api_test_hooks.info.yml @@ -0,0 +1,8 @@ +name: 'Search API Hooks Test' +type: module +description: 'Support module for Search API tests, tests all the hooks.' +package: Testing +dependencies: + - search_api:search_api +core: 8.x +#hidden: true diff --git a/tests/search_api_test_hooks/search_api_test_hooks.module b/tests/search_api_test_hooks/search_api_test_hooks.module new file mode 100644 index 0000000..82c3cbf --- /dev/null +++ b/tests/search_api_test_hooks/search_api_test_hooks.module @@ -0,0 +1,82 @@ +