You are here

public function HooksTest::testHooks in Search API 8

Tests various operations via the Search API's admin UI.

File

tests/src/Functional/HooksTest.php, line 79

Class

HooksTest
Tests integration of hooks.

Namespace

Drupal\Tests\search_api\Functional

Code

public function testHooks() {

  // hook_search_api_backend_info_alter() was invoked.
  $this
    ->drupalGet('admin/config/search/search-api/add-server');
  $this
    ->assertSession()
    ->pageTextContains('Slims return');

  // hook_search_api_datasource_info_alter() was invoked.
  $this
    ->drupalGet('admin/config/search/search-api/add-index');
  $this
    ->assertSession()
    ->pageTextContains('Distant land');

  // hook_search_api_tracker_info_alter() was invoked.
  $this
    ->assertSession()
    ->pageTextContains('Good luck');

  // hook_search_api_processor_info_alter() was invoked.
  $this
    ->drupalGet($this
    ->getIndexPath('processors'));
  $this
    ->assertSession()
    ->pageTextContains('Mystic bounce');

  // hook_search_api_parse_mode_info_alter was invoked.
  $definition = \Drupal::getContainer()
    ->get('plugin.manager.search_api.parse_mode')
    ->getDefinition('direct');
  $this
    ->assertEquals('Song for My Father', $definition['label']);

  // Saving the index should trigger the processor's preIndexSave() method.
  $this
    ->submitForm([], 'Save');
  $processor_methods = $this
    ->getCalledMethods('processor');
  $this
    ->assertEquals([
    'preIndexSave',
  ], $processor_methods);
  $this
    ->drupalGet($this
    ->getIndexPath());

  // Duplication on value 'Index now' with summary.
  $this
    ->submitForm([], 'Index now');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('Successfully indexed 4 items.');

  // During indexing, alterIndexedItems() and preprocessIndexItems() should be
  // called on the processor.
  $processor_methods = $this
    ->getCalledMethods('processor');
  $expected = [
    'alterIndexedItems',
    'preprocessIndexItems',
  ];
  $this
    ->assertEquals($expected, $processor_methods);

  // hook_search_api_index_items_alter() was invoked, this removed node:1.
  $this
    ->assertSession()
    ->pageTextContains('There are 2 items indexed on the server for this index.');
  $this
    ->assertSession()
    ->pageTextContains('Stormy');

  // hook_search_api_items_indexed() was invoked.
  $this
    ->assertSession()
    ->pageTextContains('Please set me at ease');

  // hook_search_api_index_reindex() was invoked.
  $this
    ->drupalGet($this
    ->getIndexPath('reindex'));
  $this
    ->submitForm([], 'Confirm');
  $this
    ->assertSession()
    ->pageTextContains('Montara');

  // hook_search_api_data_type_info_alter() was invoked.
  $this
    ->drupalGet($this
    ->getIndexPath('fields'));
  $this
    ->assertSession()
    ->pageTextContains('Peace/Dolphin dance');

  // The implementation of hook_search_api_field_type_mapping_alter() has
  // removed all dates, so we can't see any timestamp anymore in the page.
  $url_options['query']['datasource'] = 'entity:node';
  $this
    ->drupalGet($this
    ->getIndexPath('fields/add/nojs'), $url_options);
  $this
    ->assertSession()
    ->pageTextContains('Add fields to index');
  $this
    ->assertSession()
    ->pageTextNotContains('timestamp');
  $this
    ->drupalGet('search-api-test');
  $this
    ->assertSession()
    ->pageTextContains('Search id: views_page:search_api_test_view__page_1');

  // hook_search_api_query_alter() was invoked.
  $this
    ->assertSession()
    ->pageTextContains('Funky blue note');

  // hook_search_api_query_TAG_alter() was invoked.
  $this
    ->assertSession()
    ->pageTextContains('Freeland');

  // hook_search_api_results_alter() was invoked.
  $this
    ->assertSession()
    ->pageTextContains('Stepping into tomorrow');

  // hook_search_api_results_TAG_alter() was invoked.
  $this
    ->assertSession()
    ->pageTextContains('Llama');

  // The query alter methods of the processor were called.
  $processor_methods = $this
    ->getCalledMethods('processor');
  $expected = [
    'preprocessSearchQuery',
    'postprocessSearchResults',
  ];
  $this
    ->assertEquals($expected, $processor_methods);

  // hook_search_api_server_features_alter() is triggered.
  $this
    ->assertTrue($this->server
    ->supportsFeature('welcome_to_the_jungle'));
  $displays = \Drupal::getContainer()
    ->get('plugin.manager.search_api.display')
    ->getInstances();

  // hook_search_api_displays_alter was invoked.
  $display_label = $displays['views_page:search_api_test_view__page_1']
    ->label();
  $this
    ->assertEquals('Some funny label for testing', $display_label);
}