View source
<?php
namespace Drupal\Tests\search_api\Functional;
use Behat\Mink\Element\NodeElement;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\search_api\Entity\Index;
use Drupal\search_api_test_bulk_form\TypedData\FooDataDefinition;
use Drupal\Tests\BrowserTestBase;
class SearchApiBulkFormTest extends BrowserTestBase {
protected static $modules = [
'search_api_test_bulk_form',
];
protected $defaultTheme = 'stark';
protected $index;
protected function setUp() {
parent::setUp();
$this->index = Index::load('test_index');
$this
->createIndexedContent();
$this
->drupalLogin($this
->createUser([
'view test entity',
]));
}
public function testBulkForm() {
$this
->drupalGet('/search-api-test-bulk-form');
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->assertCheckboxExistsInRow('entity:entity_test/1:en');
$this
->assertCheckboxExistsInRow('entity:entity_test/2:en');
$this
->assertCheckboxExistsInRow('entity:entity_test_string_id/1:und');
$this
->assertCheckboxExistsInRow('entity:entity_test_string_id/2:und');
$this
->assertCheckboxNotExistsInRow('search_api_test/1:en');
$this
->assertCheckboxNotExistsInRow('search_api_test/2:en');
$assert
->fieldExists('search_api_bulk_form[1]');
$assert
->fieldExists('search_api_bulk_form[2]');
$assert
->fieldExists('search_api_bulk_form[3]');
$assert
->fieldNotExists('search_api_bulk_form[4]');
$assert
->fieldNotExists('search_api_bulk_form[5]');
$this
->checkCheckboxInRow('entity:entity_test/1:en');
$this
->checkCheckboxInRow('entity:entity_test/2:en');
$page
->selectFieldOption('Action', 'Search API test bulk form action: entity_test');
$page
->pressButton('Apply to selected items');
$assert
->pageTextContains('Search API test bulk form action: entity_test was applied to 2 items.');
$this
->assertActionsApplied([
[
'search_api_test_bulk_form_entity_test',
'entity_test',
'1',
],
[
'search_api_test_bulk_form_entity_test',
'entity_test',
'2',
],
]);
$this
->checkCheckboxInRow('entity:entity_test/1:en');
$this
->checkCheckboxInRow('entity:entity_test/2:en');
$this
->checkCheckboxInRow('entity:entity_test_string_id/2:und');
$page
->selectFieldOption('Action', 'Search API test bulk form action: entity_test');
$page
->pressButton('Apply to selected items');
$assert
->pageTextContains('Search API test bulk form action: entity_test was applied to 2 items.');
$entity = EntityTestStringId::load(2);
$assert
->pageTextContains("Row {$entity->label()} removed from selection as it's not compatible with Search API test bulk form action: entity_test action.");
$this
->assertActionsApplied([
[
'search_api_test_bulk_form_entity_test',
'entity_test',
'1',
],
[
'search_api_test_bulk_form_entity_test',
'entity_test',
'2',
],
]);
$this
->checkCheckboxInRow('entity:entity_test/1:en');
$this
->checkCheckboxInRow('entity:entity_test/2:en');
$page
->selectFieldOption('Action', 'Search API test bulk form action: entity_test_string_id');
$page
->pressButton('Apply to selected items');
$entity1 = EntityTest::load(1);
$entity2 = EntityTest::load(2);
$assert
->pageTextContains("Rows {$entity1->label()}, {$entity2->label()} removed from selection as they are not compatible with Search API test bulk form action: entity_test_string_id action.");
$assert
->pageTextContains("No items selected.");
$this
->assertActionsApplied([]);
}
protected function createIndexedContent() {
$foo_data_definition = FooDataDefinition::create()
->setMainPropertyName('foo')
->setLabel('Foo');
$search_api_test_values = [];
for ($i = 1; $i <= 2; $i++) {
$entity = EntityTest::create([
'name' => $this
->randomString(),
]);
$entity
->save();
$entity = EntityTestStringId::create([
'id' => "{$i}",
'name' => $this
->randomString(),
]);
$entity
->save();
$foo = \Drupal::getContainer()
->get('typed_data_manager')
->createInstance('map', [
'data_definition' => $foo_data_definition,
'name' => NULL,
'parent' => NULL,
]);
$foo
->set('foo', $this
->randomMachineName());
$search_api_test_values["{$i}:en"] = $foo;
}
$state = \Drupal::state();
$state
->set('search_api_test.datasource.return.loadMultiple', $search_api_test_values);
$state
->set('search_api_test.datasource.return.getItemLanguage', 'en');
$state
->set('search_api_test.datasource.return.getPropertyDefinitions', [
'foo' => $foo_data_definition,
]);
$this->index
->trackItemsInserted('search_api_test', array_keys($search_api_test_values));
$this->index
->indexItems();
$query_helper = \Drupal::getContainer()
->get('search_api.query_helper');
$query = $query_helper
->createQuery($this->index);
$results = $query
->execute()
->getResultItems();
$this
->assertCount(6, $results);
$this
->assertArrayHasKey('entity:entity_test/1:en', $results);
$this
->assertArrayHasKey('entity:entity_test/2:en', $results);
$this
->assertArrayHasKey('entity:entity_test_string_id/1:und', $results);
$this
->assertArrayHasKey('entity:entity_test_string_id/2:und', $results);
$this
->assertArrayHasKey('search_api_test/1:en', $results);
$this
->assertArrayHasKey('search_api_test/2:en', $results);
}
protected function assertActionsApplied(array $expected_actions) {
$actual_actions = \Drupal::state()
->get('search_api_test_bulk_form', []);
$this
->assertSame($expected_actions, $actual_actions);
\Drupal::state()
->delete('search_api_test_bulk_form');
}
protected function checkCheckboxInRow(string $text) {
$row = $this
->getRowContainingText($text);
$checkbox = $row
->find('css', 'input[type="checkbox"]');
$this
->assertNotNull($checkbox);
$checkbox
->check();
}
protected function assertCheckboxExistsInRow(string $text) {
$row = $this
->getRowContainingText($text);
$this
->assertNotNull($row
->find('css', 'input[type="checkbox"]'));
}
protected function assertCheckboxNotExistsInRow(string $text) {
$row = $this
->getRowContainingText($text);
$this
->assertNull($row
->find('css', 'input[type="checkbox"]'));
}
protected function getRowContainingText(string $text) : NodeElement {
$rows = $this
->getSession()
->getPage()
->findAll('css', 'tr');
$this
->assertNotEmpty($rows, 'No rows found on the page.');
$found = FALSE;
foreach ($rows as $row) {
if (strpos($row
->getText(), $text) !== FALSE) {
$found = TRUE;
break;
}
}
$this
->assertTrue($found, "No row with text \"{$text}\" found on the page.");
return $row;
}
}