SelectWidgetTest.php in Typed Data API enhancements 8
File
tests/src/Functional/TypedDataFormWidget/SelectWidgetTest.php
View source
<?php
namespace Drupal\Tests\typed_data\Functional\TypedDataFormWidget;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\ListDataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
class SelectWidgetTest extends FormWidgetBrowserTestBase {
protected $widget;
protected static $modules = [
'text',
];
protected function setUp() : void {
parent::setUp();
$this->widget = $this
->getFormWidgetManager()
->createInstance('select');
}
public function testIsApplicable() {
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('any')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('binary')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('boolean')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('datetime_iso8601')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('duration_iso8601')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('email')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('float')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('integer')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('string')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('timespan')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('timestamp')));
$this
->assertFalse($this->widget
->isApplicable(DataDefinition::create('uri')));
$this
->assertFalse($this->widget
->isApplicable(ListDataDefinition::create('string')));
$this
->assertFalse($this->widget
->isApplicable(MapDataDefinition::create()));
$this
->assertTrue($this->widget
->isApplicable(DataDefinition::create('filter_format')));
}
public function testFormEditing() {
$context_definition = ContextDefinition::create('filter_format')
->setLabel('Filter format')
->setDescription('Some example selection.');
$this->container
->get('state')
->set('typed_data_widgets.definition', $context_definition);
$this
->drupalLogin($this
->createUser([], NULL, TRUE));
$path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget
->getPluginId();
$this
->drupalGet($path);
$assert = $this
->assertSession();
$assert
->elementTextContains('css', 'label[for=edit-data-value]', $context_definition
->getLabel());
$assert
->elementTextContains('css', 'div[id=edit-data-value--description]', $context_definition
->getDescription());
$assert
->fieldValueEquals('data[value]', $context_definition
->getDefaultValue());
$this
->getSession()
->getPage()
->selectFieldOption('data[value]', 'plain_text');
$this
->pressButton('Submit');
$this
->drupalGet($path);
$assert
->fieldValueEquals('data[value]', 'plain_text');
}
public function testValidation() {
$context_definition = ContextDefinition::create('filter_format')
->setLabel('Filter format')
->setDescription('Some example selection.')
->setRequired(TRUE);
$this->container
->get('state')
->set('typed_data_widgets.definition', $context_definition);
$this
->drupalLogin($this
->createUser([], NULL, TRUE));
$path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget
->getPluginId();
$this
->drupalGet($path);
$this
->fillField('data[value]', '');
$this
->pressButton('Submit');
$assert = $this
->assertSession();
$assert
->fieldExists('data[value]')
->hasClass('error');
$this
->drupalGet($path);
$assert
->fieldValueEquals('data[value]', $context_definition
->getDefaultValue());
}
}