WidgetTest.php in Range 8
File
tests/src/Kernel/Widget/WidgetTest.php
View source
<?php
namespace Drupal\Tests\range\Kernel\Widget;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\range\Traits\RangeTestTrait;
use Drupal\entity_test\Entity\EntityTest;
class WidgetTest extends KernelTestBase {
use RangeTestTrait;
protected static $modules = [
'system',
'field',
'text',
'entity_test',
'user',
'range',
];
protected $fieldType;
protected $fieldName;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'system',
]);
$this
->installConfig([
'field',
]);
$this
->installConfig([
'text',
]);
$this
->installConfig([
'range',
]);
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('user');
}
public function testFieldWidget() {
foreach ($this
->getRangeFieldTypes() as $this->fieldType) {
$this->fieldName = $this
->getFieldName($this->fieldType);
$this
->createField($this->fieldType);
$this
->createFormDisplay();
$widget_settings = $this
->getWidgetSettings();
$this
->setFormDisplayComponent($this->fieldType, $widget_settings);
$entity = EntityTest::create([]);
$content = $this
->renderEntityForm($entity);
$this
->assertFieldByXPath($this
->constructRangeFieldXpath('from'));
$this
->assertFieldByXPath($this
->constructRangeFieldXpath('to'));
$field_settings = $this
->getFieldSettings($this->fieldType);
$this
->assertStringContainsString($widget_settings['label']['from'], $content);
$this
->assertStringContainsString($widget_settings['label']['to'], $content);
$this
->assertStringContainsString($field_settings['field']['prefix'] . $field_settings['from']['prefix'], $content);
$this
->assertStringContainsString($field_settings['from']['suffix'], $content);
$this
->assertStringContainsString($field_settings['to']['prefix'], $content);
$this
->assertStringContainsString($field_settings['to']['suffix'] . $field_settings['field']['suffix'], $content);
$this
->deleteField();
}
}
protected function constructRangeFieldXpath($name) {
$field_settings = $this
->getFieldSettings($this->fieldType);
$widget_settings = $this
->getWidgetSettings();
$xpath = '//input[@name=:name][@type=:type][@min=:min][@max=:max][@step=:step][@placeholder=:placeholder]';
return $this
->buildXPathQuery($xpath, [
':name' => "{$this->fieldName}[0][{$name}]",
':type' => 'number',
':min' => $field_settings['min'],
':max' => $field_settings['max'],
':step' => $this
->getExpectedStepValue(),
':placeholder' => $widget_settings['placeholder'][$name],
]);
}
protected function renderEntityForm(FieldableEntityInterface $entity) {
$form = $this->container
->get('entity.form_builder')
->getForm($entity, 'default');
return $this
->render($form);
}
private function getExpectedStepValue() {
switch ($this->fieldType) {
case 'range_integer':
return '1';
case 'range_float':
return 'any';
case 'range_decimal':
return '0.0001';
}
}
}