View source
<?php
namespace Drupal\Tests\length_indicator\FunctionalJavascript;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class LengthIndicatorTest extends WebDriverTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'length_indicator',
'field_ui',
'entity_test',
];
protected function setUp() {
parent::setUp();
$this
->createField('string', 'string_textfield');
$this
->createField('string_long', 'string_textarea');
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'view test entity',
'administer entity_test content',
'administer entity_test fields',
'administer entity_test display',
'administer entity_test form display',
'view the administration theme',
]));
}
protected function createField($field_type, $widget_type) {
$field_name = $field_type;
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => $field_type,
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
'label' => $this
->randomMachineName() . '_label',
])
->save();
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($field_name, [
'type' => $widget_type,
'settings' => [
'placeholder' => 'A placeholder on ' . $widget_type,
],
])
->save();
}
public function testLengthIndicator() {
$entity = EntityTest::create([
'name' => 'The name for this entity',
'string' => [
[
'value' => 'A value in a string field',
],
],
'string_long' => [
[
'value' => 'A value in a string_long field',
],
],
]);
$entity
->save();
$form_display = EntityFormDisplay::load('entity_test.entity_test.default');
$values = $form_display
->getComponent('string');
$values['third_party_settings']['length_indicator'] = [
'indicator' => TRUE,
'indicator_opt' => [
'optimin' => 15,
'optimax' => 30,
'tolerance' => 6,
],
];
$form_display
->setComponent('string', $values);
$values = $form_display
->getComponent('string_long');
$values['third_party_settings']['length_indicator'] = [
'indicator' => TRUE,
'indicator_opt' => [
'optimin' => 100,
'optimax' => 300,
'tolerance' => 40,
],
];
$form_display
->setComponent('string_long', $values);
$form_display
->save();
$this
->drupalGet('entity_test/manage/1/edit');
$this
->assertActiveElement('string', 'good');
$this
->assertActiveElement('string', 'bad', '');
$this
->assertActiveElement('string', 'bad', $this
->randomString(8));
$this
->assertActiveElement('string', 'ok', $this
->randomString(9));
$this
->assertActiveElement('string', 'ok', $this
->randomString(14));
$this
->assertActiveElement('string', 'good', $this
->randomString(15));
$this
->assertActiveElement('string', 'good', $this
->randomString(30));
$this
->assertActiveElement('string', 'ok', $this
->randomString(31));
$this
->assertActiveElement('string', 'ok', $this
->randomString(36));
$this
->assertActiveElement('string', 'bad', $this
->randomString(37));
$this
->assertActiveElement('string_long', 'bad');
$this
->assertActiveElement('string', 'bad', '');
$this
->assertActiveElement('string_long', 'bad', $this
->randomString(59));
$this
->assertActiveElement('string_long', 'ok', $this
->randomString(60));
$this
->assertActiveElement('string_long', 'ok', $this
->randomString(99));
$this
->assertActiveElement('string_long', 'good', $this
->randomString(100));
$this
->assertActiveElement('string_long', 'good', $this
->randomString(300));
$this
->assertActiveElement('string_long', 'ok', $this
->randomString(301));
$this
->assertActiveElement('string_long', 'ok', $this
->randomString(340));
$this
->assertActiveElement('string_long', 'bad', $this
->randomString(341));
}
protected function assertActiveElement($field_name, $class_modifier, $value = NULL) {
if (!is_null($value)) {
$this
->getSession()
->getPage()
->fillField($field_name . '[0][value]', $value);
}
$active_elements = $this
->xpath('//*[@id="edit-' . str_replace('_', '-', $field_name) . '-wrapper"]/div[2]/span[contains(@class, "is-active")]');
$this
->assertCount(1, $active_elements);
$this
->assertContains("length-indicator__indicator--{$class_modifier}", $active_elements[0]
->getAttribute('class'), "{$field_name} field's active indicator has class modifier '{$class_modifier}'");
}
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);
$this
->getSession()
->getPage()
->checkField('fields[string][settings_edit_form][third_party_settings][length_indicator][indicator]');
$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');
$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');
$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
->xpath('//input[@data-drupal-selector="edit-submit"]')[0]
->click();
$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'));
}
}