View source
<?php
namespace Drupal\Tests\system\Functional\Entity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class EntityFormTest extends BrowserTestBase {
protected static $modules = [
'entity_test',
'language',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'administer entity_test content',
'view test entity',
]);
$this
->drupalLogin($web_user);
ConfigurableLanguage::createFromLangcode('ro')
->save();
}
public function testFormCRUD() {
foreach (entity_test_entity_types() as $entity_type) {
$this
->doTestFormCRUD($entity_type);
}
}
public function testMultilingualFormCRUD() {
foreach (entity_test_entity_types(ENTITY_TEST_TYPES_MULTILINGUAL) as $entity_type) {
$this
->doTestMultilingualFormCRUD($entity_type);
}
}
public function testEntityFormModeAlter() {
EntityFormMode::create([
'id' => 'entity_test.compact',
'targetEntityType' => 'entity_test',
])
->save();
EntityFormDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'compact',
'status' => TRUE,
])
->removeComponent('field_test_text')
->save();
$entity1 = EntityTest::create([
'name' => $this
->randomString(),
]);
$entity1
->save();
$this
->drupalGet($entity1
->toUrl('edit-form'));
$this
->assertSession()
->elementExists('css', 'input[name="field_test_text[0][value]"]');
$entity2 = EntityTest::create([
'name' => 'compact_form_mode',
]);
$entity2
->save();
$this
->drupalGet($entity2
->toUrl('edit-form'));
$this
->assertSession()
->elementNotExists('css', 'input[name="field_test_text[0][value]"]');
}
public function testEntityFormDisplayAlter() {
$this
->drupalGet('entity_test/add');
$altered_field = $this
->assertSession()
->fieldExists('field_test_text[0][value]');
$this
->assertEquals(42, $altered_field
->getAttribute('size'));
}
protected function doTestFormCRUD($entity_type) {
$name1 = $this
->randomMachineName(8);
$name2 = $this
->randomMachineName(10);
$edit = [
'name[0][value]' => $name1,
'field_test_text[0][value]' => $this
->randomMachineName(16),
];
$this
->drupalGet($entity_type . '/add');
$this
->submitForm($edit, 'Save');
$entity = $this
->loadEntityByName($entity_type, $name1);
$this
->assertNotNull($entity, new FormattableMarkup('%entity_type: Entity found in the database.', [
'%entity_type' => $entity_type,
]));
$edit['name[0][value]'] = $name2;
$this
->drupalGet($entity_type . '/manage/' . $entity
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$entity = $this
->loadEntityByName($entity_type, $name1);
$this
->assertNull($entity, new FormattableMarkup('%entity_type: The entity has been modified.', [
'%entity_type' => $entity_type,
]));
$entity = $this
->loadEntityByName($entity_type, $name2);
$this
->assertNotNull($entity, new FormattableMarkup('%entity_type: Modified entity found in the database.', [
'%entity_type' => $entity_type,
]));
$this
->assertNotEquals($name1, $entity->name->value, new FormattableMarkup('%entity_type: The entity name has been modified.', [
'%entity_type' => $entity_type,
]));
$this
->drupalGet($entity_type . '/manage/' . $entity
->id() . '/edit');
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete');
$entity = $this
->loadEntityByName($entity_type, $name2);
$this
->assertNull($entity, new FormattableMarkup('%entity_type: Entity not found in the database.', [
'%entity_type' => $entity_type,
]));
}
protected function doTestMultilingualFormCRUD($entity_type_id) {
$name1 = $this
->randomMachineName(8);
$name1_ro = $this
->randomMachineName(9);
$name2_ro = $this
->randomMachineName(11);
$edit = [
'name[0][value]' => $name1,
'field_test_text[0][value]' => $this
->randomMachineName(16),
];
$this
->drupalGet($entity_type_id . '/add');
$this
->submitForm($edit, 'Save');
$entity = $this
->loadEntityByName($entity_type_id, $name1);
$this
->assertNotNull($entity, new FormattableMarkup('%entity_type: Entity found in the database.', [
'%entity_type' => $entity_type_id,
]));
$entity
->addTranslation('ro', [
'name' => $name1_ro,
])
->save();
$translated_entity = $this
->loadEntityByName($entity_type_id, $name1)
->getTranslation('ro');
$this
->assertEquals($name1_ro, $translated_entity->name->value, new FormattableMarkup('%entity_type: The translation has been added.', [
'%entity_type' => $entity_type_id,
]));
$edit['name[0][value]'] = $name2_ro;
$this
->drupalGet('ro/' . $entity_type_id . '/manage/' . $entity
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$translated_entity = $this
->loadEntityByName($entity_type_id, $name1)
->getTranslation('ro');
$this
->assertNotNull($translated_entity, new FormattableMarkup('%entity_type: Modified translation found in the database.', [
'%entity_type' => $entity_type_id,
]));
$this
->assertEquals($name2_ro, $translated_entity->name->value, new FormattableMarkup('%entity_type: The name of the translation has been modified.', [
'%entity_type' => $entity_type_id,
]));
$this
->drupalGet('ro/' . $entity_type_id . '/manage/' . $entity
->id() . '/edit');
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete Romanian translation');
$entity = $this
->loadEntityByName($entity_type_id, $name1);
$this
->assertNotNull($entity, new FormattableMarkup('%entity_type: The original entity still exists.', [
'%entity_type' => $entity_type_id,
]));
$this
->assertFalse($entity
->hasTranslation('ro'), new FormattableMarkup('%entity_type: Entity translation does not exist anymore.', [
'%entity_type' => $entity_type_id,
]));
}
protected function loadEntityByName($entity_type, $name) {
$entity_storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$entity_storage
->resetCache();
$entities = $entity_storage
->loadByProperties([
'name' => $name,
]);
return $entities ? current($entities) : NULL;
}
public function testValidationHandlers() {
$state = $this->container
->get('state');
$state
->set('entity_test.form.validate.test', 'form-level');
$this
->drupalGet('entity_test/add');
$this
->submitForm([], 'Save');
$this
->assertTrue($state
->get('entity_test.form.validate.result'), 'Form-level validation handlers behave correctly.');
$state
->set('entity_test.form.validate.test', 'button-level');
$this
->drupalGet('entity_test/add');
$this
->submitForm([], 'Save');
$this
->assertEquals('Drupal\\Core\\Entity\\EntityStorageException: Entity validation was skipped.', $state
->get('entity_test.form.save.exception'), 'Button-level validation handlers behave correctly.');
}
}