You are here

public function LengthIndicatorTest::testLengthIndicatorSettings in Length Indicator 8

Tests the length indicator widget settings form.

File

tests/src/FunctionalJavascript/LengthIndicatorTest.php, line 165

Class

LengthIndicatorTest
Tests the UI for entity displays.

Namespace

Drupal\Tests\length_indicator\FunctionalJavascript

Code

public function testLengthIndicatorSettings() {
  $this
    ->drupalGet('entity_test/structure/entity_test/form-display');
  $this
    ->xpath('//input[@data-drupal-selector="edit-fields-string-settings-edit"]')[0]
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $html_output = '<hr />Ending URL: ' . $this
    ->getSession()
    ->getCurrentUrl();
  $html_output .= '<hr />' . $this
    ->getSession()
    ->getPage()
    ->getContent();
  $html_output .= $this
    ->getHtmlOutputHeaders();
  $this
    ->htmlOutput($html_output);

  // Have to click the field for #states to work.
  $this
    ->getSession()
    ->getPage()
    ->checkField('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator]');

  // Default values.
  $this
    ->assertSession()
    ->fieldValueEquals('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][optimin]', '10');
  $this
    ->assertSession()
    ->fieldValueEquals('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][optimax]', '15');
  $this
    ->assertSession()
    ->fieldValueEquals('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][tolerance]', '5');

  // Form error when min is greater than max and tolerance is greater than
  // min.
  $this
    ->getSession()
    ->getPage()
    ->fillField('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][optimin]', '20');
  $this
    ->getSession()
    ->getPage()
    ->fillField('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][tolerance]', '21');
  $this
    ->xpath('//input[@data-drupal-selector="edit-fields-string-settings-edit-form-actions-save-settings"]')[0]
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Optimum maximum has to be greater than the optimum minimum');
  $this
    ->assertSession()
    ->pageTextContains('Tolerance has to be smaller than the optimum minimum');

  // Fill out the form with some non-default values.
  $this
    ->getSession()
    ->getPage()
    ->fillField('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][optimin]', '15');
  $this
    ->getSession()
    ->getPage()
    ->fillField('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][optimax]', '30');
  $this
    ->getSession()
    ->getPage()
    ->fillField('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator_opt][tolerance]', '6');
  $this
    ->xpath('//input[@data-drupal-selector="edit-fields-string-settings-edit-form-actions-save-settings"]')[0]
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // This saves the form display which will validate our configuration schema.
  $this
    ->xpath('//input[@data-drupal-selector="edit-submit"]')[0]
    ->click();

  // Check the values.
  $form_display = EntityFormDisplay::load('entity_test.entity_test.default');
  $expected = [
    'indicator' => TRUE,
    'indicator_opt' => [
      'optimin' => 15,
      'optimax' => 30,
      'tolerance' => 6,
    ],
  ];
  $this
    ->assertEquals($expected, $form_display
    ->getRenderer('string')
    ->getThirdPartySettings('length_indicator'));
}