View source
<?php
namespace Drupal\Tests\text\Functional;
use Drupal\Component\Utility\Html;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\field\Functional\String\StringFieldTest;
use Drupal\Tests\TestFileCreationTrait;
class TextFieldTest extends StringFieldTest {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected $adminUser;
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer filters',
]);
}
public function testTextFieldValidation() {
$max_length = 3;
$field_name = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'text',
'settings' => [
'max_length' => $max_length,
],
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
])
->save();
$entity = EntityTest::create();
for ($i = 0; $i <= $max_length + 2; $i++) {
$entity->{$field_name}->value = str_repeat('x', $i);
$violations = $entity->{$field_name}
->validate();
if ($i <= $max_length) {
$this
->assertCount(0, $violations, "Length {$i} does not cause validation error when max_length is {$max_length}");
}
else {
$this
->assertCount(1, $violations, "Length {$i} causes validation error when max_length is {$max_length}");
}
}
}
public function testRequiredLongTextWithFileUpload() {
$text_field_name = 'text_long';
$field_storage = FieldStorageConfig::create([
'field_name' => $text_field_name,
'entity_type' => 'entity_test',
'type' => 'text_with_summary',
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
'label' => $this
->randomMachineName() . '_label',
'required' => TRUE,
])
->save();
$file_field_name = 'file_field';
$field_storage = FieldStorageConfig::create([
'field_name' => $file_field_name,
'entity_type' => 'entity_test',
'type' => 'file',
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
'label' => $this
->randomMachineName() . '_label',
])
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($text_field_name, [
'type' => 'text_textarea_with_summary',
])
->setComponent($file_field_name, [
'type' => 'file_generic',
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($text_field_name)
->setComponent($file_field_name)
->save();
$test_file = current($this
->drupalGetTestFiles('text'));
$edit['files[file_field_0]'] = \Drupal::service('file_system')
->realpath($test_file->uri);
$this
->drupalGet('entity_test/add');
$this
->submitForm($edit, 'Upload');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'text_long[0][value]' => 'Long text',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('entity_test/1');
$this
->assertSession()
->pageTextContains('Long text');
}
public function testTextfieldWidgets() {
$this
->_testTextfieldWidgets('text', 'text_textfield');
$this
->_testTextfieldWidgets('text_long', 'text_textarea');
}
public function testTextfieldWidgetsFormatted() {
$this
->_testTextfieldWidgetsFormatted('text', 'text_textfield');
$this
->_testTextfieldWidgetsFormatted('text_long', 'text_textarea');
}
public function _testTextfieldWidgetsFormatted($field_type, $widget_type) {
$renderer = $this->container
->get('renderer');
$field_name = mb_strtolower($this
->randomMachineName());
$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();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => $widget_type,
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($field_name)
->save();
$this
->drupalLogin($this->adminUser);
foreach (filter_formats() as $format) {
if (!$format
->isFallbackFormat()) {
$this
->drupalGet('admin/config/content/formats/manage/' . $format
->id() . '/disable');
$this
->submitForm([], 'Disable');
}
}
$this
->drupalLogin($this->webUser);
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$field_name}[0][value]", '');
$this
->assertSession()
->fieldNotExists("{$field_name}[0][format]");
$value = '<em>' . $this
->randomMachineName() . '</em>';
$edit = [
"{$field_name}[0][value]" => $value,
];
$this
->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
$entity = EntityTest::load($id);
$display = $display_repository
->getViewDisplay($entity
->getEntityTypeId(), $entity
->bundle(), 'full');
$content = $display
->build($entity);
$rendered_entity = \Drupal::service('renderer')
->renderRoot($content);
$this
->assertStringNotContainsString($value, (string) $rendered_entity);
$this
->assertStringContainsString(Html::escape($value), (string) $rendered_entity);
$this
->drupalLogin($this->adminUser);
$edit = [
'format' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
];
$this
->drupalGet('admin/config/content/formats/add');
$this
->submitForm($edit, 'Save configuration');
filter_formats_reset();
$format = FilterFormat::load($edit['format']);
$format_id = $format
->id();
$permission = $format
->getPermissionName();
$roles = $this->webUser
->getRoles();
$rid = $roles[0];
user_role_grant_permissions($rid, [
$permission,
]);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('entity_test/manage/' . $id . '/edit');
$this
->assertSession()
->fieldExists("{$field_name}[0][value]");
$this
->assertSession()
->fieldExists("{$field_name}[0][format]");
$edit = [
"{$field_name}[0][format]" => $format_id,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('entity_test ' . $id . ' has been updated.');
$this->container
->get('entity_type.manager')
->getStorage('entity_test')
->resetCache([
$id,
]);
$entity = EntityTest::load($id);
$display = $display_repository
->getViewDisplay($entity
->getEntityTypeId(), $entity
->bundle(), 'full');
$content = $display
->build($entity);
$rendered_entity = \Drupal::service('renderer')
->renderRoot($content);
$this
->assertStringContainsString($value, (string) $rendered_entity);
}
}