You are here

protected function OverviewPageTest::checkServerAndIndexCreation in Search API 8

Tests the creation of a server and an index.

1 call to OverviewPageTest::checkServerAndIndexCreation()
OverviewPageTest::testOverviewPage in tests/src/Functional/OverviewPageTest.php
Tests various scenarios for the overview page.

File

tests/src/Functional/OverviewPageTest.php, line 58

Class

OverviewPageTest
Tests the Search API overview page.

Namespace

Drupal\Tests\search_api\Functional

Code

protected function checkServerAndIndexCreation() {
  $server_name = 'WebTest server';
  $index_name = 'WebTest index';
  $actions = [
    [
      Url::fromRoute('entity.search_api_server.add_form'),
      'Add server',
    ],
    [
      Url::fromRoute('entity.search_api_index.add_form'),
      'Add index',
    ],
  ];

  // Enable the "Local actions" block so we can verify which local actions are
  // displayed.
  Block::create([
    'id' => 'local_actions',
    'theme' => $this->defaultTheme,
    'weight' => -20,
    'plugin' => 'local_actions_block',
    'region' => 'content',
  ])
    ->save();

  // Make sure the overview is empty.
  $this
    ->drupalGet($this->overviewPageUrl);
  $this
    ->assertLocalAction($actions);
  $this
    ->assertSession()
    ->pageTextNotContains($server_name);
  $this
    ->assertSession()
    ->pageTextNotContains($index_name);

  // Test whether a newly created server appears on the overview page.
  $server = $this
    ->getTestServer();
  $this
    ->drupalGet($this->overviewPageUrl);
  $this
    ->assertSession()
    ->pageTextContains($server_name);
  $this
    ->assertSession()
    ->responseContains($server
    ->get('description'));
  $server_class = Html::cleanCssIdentifier($server
    ->getEntityTypeId() . '-' . $server
    ->id());
  $servers = $this
    ->xpath('//tr[contains(@class,"' . $server_class . '") and contains(@class, "search-api-list-enabled")]');
  $this
    ->assertNotEmpty($servers, 'Server is in proper table');

  // Test whether a newly created index appears on the overview page.
  $index = $this
    ->getTestIndex();
  $this
    ->drupalGet($this->overviewPageUrl);
  $this
    ->assertSession()
    ->pageTextContains($index_name);
  $this
    ->assertSession()
    ->responseContains($index
    ->get('description'));
  $index_class = Html::cleanCssIdentifier($index
    ->getEntityTypeId() . '-' . $index
    ->id());
  $fields = $this
    ->xpath('//tr[contains(@class,"' . $index_class . '") and contains(@class, "search-api-list-enabled")]');
  $this
    ->assertNotEmpty($fields, 'Index is in proper table');
  $this
    ->assertSession()
    ->linkNotExists('Execute pending tasks', 'No pending tasks to execute.');

  // Tests that the "Execute pending tasks" local action is correctly
  // displayed when there are pending tasks.
  \Drupal::getContainer()
    ->get('search_api.task_manager')
    ->addTask('deleteItems', $server, $index, [
    '',
  ]);
  $this
    ->drupalGet($this->overviewPageUrl);
  $this
    ->assertSession()
    ->linkExists('Execute pending tasks', 0);
}