You are here

protected function IntegrationTest::createIndex in Search API 8

Tests creating a search index via the UI.

1 call to IntegrationTest::createIndex()
IntegrationTest::testFramework in tests/src/Functional/IntegrationTest.php
Tests various operations via the Search API's admin UI.

File

tests/src/Functional/IntegrationTest.php, line 277

Class

IntegrationTest
Tests the overall functionality of the Search API framework and admin UI.

Namespace

Drupal\Tests\search_api\Functional

Code

protected function createIndex() {
  $settings_path = 'admin/config/search/search-api/add-index';
  $this->indexId = 'test_index';
  $index_description = 'An >index< used for &! tęsting.';
  $index_name = 'Search >API< test &!^* index';
  $index_datasource = 'entity:node';
  $this
    ->drupalGet($settings_path);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextNotContains('No UI datasource');
  $this
    ->assertSession()
    ->pageTextNotContains('No UI tracker');

  // Make sure plugin labels are only escaped when necessary.
  $this
    ->assertHtmlEscaped('"Test" tracker');
  $this
    ->assertHtmlEscaped('&quot;String label&quot; test tracker');
  $this
    ->assertHtmlEscaped('"Test" datasource');

  // Make sure datasource and tracker plugin descriptions are displayed.
  $dummy_index = Index::create();
  foreach ([
    'createDatasourcePlugins',
    'createTrackerPlugins',
  ] as $method) {

    /** @var \Drupal\search_api\Plugin\IndexPluginInterface[] $plugins */
    $plugins = \Drupal::getContainer()
      ->get('search_api.plugin_helper')
      ->{$method}($dummy_index);
    foreach ($plugins as $plugin) {
      if ($plugin
        ->isHidden()) {
        continue;
      }
      $description = Utility::escapeHtml($plugin
        ->getDescription());
      $this
        ->assertSession()
        ->responseContains($description);
    }
  }

  // Test form validation (required fields).
  $edit = [
    'status' => 1,
    'description' => $index_description,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Index name field is required.');
  $this
    ->assertSession()
    ->pageTextContains('Machine-readable name field is required.');
  $this
    ->assertSession()
    ->pageTextContains('Datasources field is required.');
  $edit = [
    'name' => $index_name,
    'id' => $this->indexId,
    'status' => 1,
    'description' => $index_description,
    'server' => $this->serverId,
    'datasources[' . $index_datasource . ']' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Please configure the used datasources.');
  $this
    ->submitForm([], 'Save');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $this
    ->assertSession()
    ->addressEquals($this
    ->getIndexPath());
  $this
    ->assertHtmlEscaped($index_name);
  $this
    ->drupalGet($this
    ->getIndexPath('edit'));
  $this
    ->assertHtmlEscaped($index_name);
  $index = $this
    ->getIndex(TRUE);
  $this
    ->assertInstanceOf(IndexInterface::class, $index, 'Index was correctly created.');
  $this
    ->assertEquals($edit['name'], $index
    ->label(), 'Name correctly inserted.');
  $this
    ->assertEquals($edit['id'], $index
    ->id(), 'Index ID correctly inserted.');
  $this
    ->assertTrue($index
    ->status(), 'Index status correctly inserted.');
  $this
    ->assertEquals($edit['description'], $index
    ->getDescription(), 'Index ID correctly inserted.');
  $this
    ->assertEquals($edit['server'], $index
    ->getServerId(), 'Index server ID correctly inserted.');
  $this
    ->assertEquals($index_datasource, $index
    ->getDatasourceIds()[0], 'Index datasource id correctly inserted.');

  // Test the "Save and add fields" button.
  $index2_id = 'test_index2';
  $edit['id'] = $index2_id;
  unset($edit['server']);
  $this
    ->drupalGet($settings_path);
  $this
    ->submitForm($edit, 'Save and add fields');
  $this
    ->assertSession()
    ->pageTextContains('Please configure the used datasources.');
  $this
    ->submitForm([], 'Save and add fields');
  $this
    ->assertSession()
    ->pageTextContains('The index was successfully saved.');
  $this->indexStorage
    ->resetCache([
    $index2_id,
  ]);
  $index = $this->indexStorage
    ->load($index2_id);
  $this
    ->assertSession()
    ->addressEquals($index
    ->toUrl('add-fields'));
  $this
    ->drupalGet('admin/config/search/search-api');
  $this
    ->assertHtmlEscaped($index_name);
  $this
    ->assertHtmlEscaped($index_description);
}