You are here

public function SearchApiBulkFormTest::testBulkForm in Search API 8

Tests the Views bulk form.

File

tests/src/Functional/SearchApiBulkFormTest.php, line 52

Class

SearchApiBulkFormTest
Tests the Search API bulk form Views field plugin.

Namespace

Drupal\Tests\search_api\Functional

Code

public function testBulkForm() {
  $this
    ->drupalGet('/search-api-test-bulk-form');
  $assert = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();

  // Check that only entity-based datasource rows have checkboxes. The view
  // is sorted by item ID, so the following assertions are safe.
  $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]');

  // Check two entity_test rows with a compatible action.
  $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',
    ],
  ]);

  // Check two entity_test rows with a compatible action and one that is not
  // compatible with the applied action.
  $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.');

  // Check that the incompatible row was not executed.
  $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',
    ],
  ]);

  // Use the other action on an exclusive entity_test list.
  $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.");

  // The form didn't pass validation.
  $assert
    ->pageTextContains("No items selected.");
  $this
    ->assertActionsApplied([]);
}