View source
<?php
namespace Drupal\Tests\search_api\Functional;
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\ServerInterface;
class OverviewPageTest extends SearchApiBrowserTestBase {
public static $modules = [
'block',
];
protected $overviewPageUrl;
public function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this->overviewPageUrl = 'admin/config/search/search-api';
}
public function testOverviewPage() {
$this
->checkServerAndIndexCreation();
$this
->checkServerAndIndexStatusChanges();
$this
->checkOperations();
$this
->checkOverviewPermissions();
}
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',
],
];
Block::create([
'id' => 'local_actions',
'theme' => $this->defaultTheme,
'weight' => -20,
'plugin' => 'local_actions_block',
'region' => 'content',
])
->save();
$this
->drupalGet($this->overviewPageUrl);
$this
->assertLocalAction($actions);
$this
->assertSession()
->pageTextNotContains($server_name);
$this
->assertSession()
->pageTextNotContains($index_name);
$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');
$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.');
\Drupal::getContainer()
->get('search_api.task_manager')
->addTask('deleteItems', $server, $index, [
'',
]);
$this
->drupalGet($this->overviewPageUrl);
$this
->assertSession()
->linkExists('Execute pending tasks', 0);
}
protected function checkServerAndIndexStatusChanges() {
$server = $this
->getTestServer();
$this
->assertEntityStatusChange($server);
$this
->getTestIndex()
->delete();
$index = $this
->getTestIndex();
$this
->assertEntityStatusChange($index);
$server
->setStatus(FALSE)
->save();
$this
->drupalGet($this->overviewPageUrl);
$server_class = Html::cleanCssIdentifier($server
->getEntityTypeId() . '-' . $server
->id());
$index_class = Html::cleanCssIdentifier($index
->getEntityTypeId() . '-' . $index
->id());
$servers = $this
->xpath('//tr[contains(@class,"' . $server_class . '") and contains(@class, "search-api-list-disabled")]');
$this
->assertNotEmpty($servers, 'The server has been disabled.');
$indexes = $this
->xpath('//tr[contains(@class,"' . $index_class . '") and contains(@class, "search-api-list-disabled")]');
$this
->assertNotEmpty($indexes, 'The index has been disabled.');
$this
->clickLink('Enable', 1);
$this
->drupalGet($this->overviewPageUrl);
$indexes = $this
->xpath('//tr[contains(@class,"' . $index_class . '") and contains(@class, "search-api-list-disabled")]');
$this
->assertNotEmpty($indexes, 'The index could not be enabled.');
$server
->setStatus(TRUE)
->save();
$this
->drupalGet($this->overviewPageUrl);
$this
->clickLink('Enable');
$this
->drupalGet($this->overviewPageUrl);
$indexes = $this
->xpath('//tr[contains(@class,"' . $index_class . '") and contains(@class, "search-api-list-enabled")]');
$this
->assertNotEmpty($indexes, 'The index has been enabled.');
$index2 = Index::create([
'id' => 'test_index_2',
'name' => 'WebTest index 2',
'datasource_settings' => [
'entity:node' => [],
],
]);
$index2
->save();
$this
->assertFalse($index2
->status(), 'The newly created index without a server is disabled by default.');
$index2
->setStatus(TRUE)
->save();
$this
->assertFalse($index2
->status(), 'The newly created index without a server cannot be enabled.');
}
protected function assertEntityStatusChange($entity) {
$this
->drupalGet($this->overviewPageUrl);
$row_class = Html::cleanCssIdentifier($entity
->getEntityTypeId() . '-' . $entity
->id());
$rows = $this
->xpath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-enabled")]');
$this
->assertNotEmpty($rows, 'The newly created entity is enabled by default.');
$this
->clickLink('Disable', $entity instanceof ServerInterface ? 0 : 1);
$this
->submitForm([], 'Disable');
$rows = $this
->xpath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-disabled")]');
$this
->assertNotEmpty($rows, 'The entity has been disabled.');
$this
->clickLink('Enable');
$this
->drupalGet($this->overviewPageUrl);
$rows = $this
->xpath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-enabled")]');
$this
->assertNotEmpty($rows, 'The entity has benn enabled.');
}
protected function checkOperations() {
$server = $this
->getTestServer();
$this
->drupalGet($this->overviewPageUrl);
$basic_url = $this->urlGenerator
->generateFromRoute('entity.search_api_server.canonical', [
'search_api_server' => $server
->id(),
]);
$destination = "?destination=" . $this->urlGenerator
->generateFromRoute('search_api.overview');
$this
->assertSession()
->responseContains("<a href=\"{$basic_url}/edit{$destination}\">Edit</a>");
$this
->assertSession()
->responseContains("<a href=\"{$basic_url}/disable{$destination}\">Disable</a>");
$this
->assertSession()
->responseContains("<a href=\"{$basic_url}/delete{$destination}\">Delete</a>");
$this
->assertSession()
->responseNotContains("<a href=\"{$basic_url}/enable{$destination}\">Enable</a>");
$server
->setStatus(FALSE)
->save();
$this
->drupalGet($this->overviewPageUrl);
$params = $destination ? "{$destination}&token=" : '?token=';
$this
->assertSession()
->responseContains("<a href=\"{$basic_url}/enable{$params}");
$this
->assertSession()
->responseNotContains("<a href=\"{$basic_url}/disable{$destination}\">Disable</a>");
}
protected function checkOverviewPermissions() {
$this
->drupalGet('admin/config');
$this
->assertSession()
->pageTextContains('Search API');
$this
->drupalGet($this->overviewPageUrl);
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->unauthorizedUser);
$this
->drupalGet($this->overviewPageUrl);
$this
->assertSession()
->statusCodeEquals(403);
}
protected function assertLocalAction(array $actions) {
$elements = $this
->xpath('//a[contains(@class, :class)]', [
':class' => 'button-action',
]);
$index = 0;
foreach ($actions as $action) {
list($url, $title) = $action;
$this
->assertSession()
->responseMatches('@<a [^>]*class="[^"]*button-action[^"]*"[^>]*>' . preg_quote($title, '@') . '</@');
$this
->assertEquals($url
->toString(), $elements[$index++]
->getAttribute('href'));
}
}
}