You are here

protected function IntegrationTest::checkUnsavedChanges in Search API 8

Tests whether unsaved fields changes work correctly.

1 call to IntegrationTest::checkUnsavedChanges()
IntegrationTest::testIntegerIndex in tests/src/Functional/IntegrationTest.php
Tests what happens when an index has an integer as id/label.

File

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

Class

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

Namespace

Drupal\Tests\search_api\Functional

Code

protected function checkUnsavedChanges() {
  $this
    ->addField('entity:node', 'changed', 'Changed');
  $this
    ->drupalGet($this
    ->getIndexPath('fields'));
  $this
    ->assertSession()
    ->pageTextContains('You have unsaved changes.');

  // Log in a different admin user.
  $this
    ->drupalLogin($this->adminUser2);

  // Construct the message that should be displayed.
  $username = [
    '#theme' => 'username',
    '#account' => $this->adminUser,
  ];
  $args = [
    '@user' => \Drupal::getContainer()
      ->get('renderer')
      ->renderPlain($username),
    ':url' => $this
      ->getIndex()
      ->toUrl('break-lock-form')
      ->toString(),
  ];
  $message = (string) new FormattableMarkup('This index is being edited by user @user, and is therefore locked from editing by others. This lock is @age old. Click here to <a href=":url">break this lock</a>.', $args);

  // Since we can't predict the age that will be shown, just check for
  // everything else.
  $message_parts = explode('@age', $message);
  $this
    ->drupalGet($this
    ->getIndexPath('fields/add/nojs'));
  $this
    ->assertSession()
    ->responseContains($message_parts[0]);
  $this
    ->assertSession()
    ->responseContains($message_parts[1]);
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//input[not(@disabled)]');
  $this
    ->drupalGet($this
    ->getIndexPath('fields/edit/rendered_item'));
  $this
    ->assertSession()
    ->responseContains($message_parts[0]);
  $this
    ->assertSession()
    ->responseContains($message_parts[1]);
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//input[not(@disabled)]');
  $this
    ->drupalGet($this
    ->getIndexPath('fields'));
  $this
    ->assertSession()
    ->responseContains($message_parts[0]);
  $this
    ->assertSession()
    ->responseContains($message_parts[1]);
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//input[not(@disabled)]');
  $this
    ->clickLink('break this lock');
  $this
    ->assertSession()
    ->responseContains(new FormattableMarkup('By breaking this lock, any unsaved changes made by @user will be lost.', $args));
  $this
    ->submitForm([], 'Break lock');
  $this
    ->assertSession()
    ->pageTextContains('The lock has been broken. You may now edit this search index.');

  // Make sure the field has not been added to the index.
  $index = $this
    ->getIndex(TRUE);
  $fields = $index
    ->getFields();
  $this
    ->assertArrayNotHasKey('changed', $fields);

  // Find the "Remove" link for the "title" field.
  $links = $this
    ->xpath('//a[@data-drupal-selector=:id]', [
    ':id' => 'edit-fields-title-remove',
  ]);
  $this
    ->assertNotEmpty($links, 'Found "Remove" link for title field');
  $this
    ->assertIsArray($links);
  $url_target = $this
    ->getAbsoluteUrl($links[0]
    ->getAttribute('href'));
  $this
    ->drupalGet($url_target);
  $this
    ->assertSession()
    ->pageTextContains('You have unsaved changes.');
  $this
    ->submitForm([], 'Cancel');
  $this
    ->assertArrayHasKey('title', $fields, 'The title field has not been removed from the index.');
}