View source
<?php
namespace Drupal\Tests\system\Functional\Entity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class EntityFormTest extends BrowserTestBase {
public static $modules = [
'entity_test',
'language',
];
protected $defaultTheme = 'stark';
protected function setUp() {
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 testEntityFormDisplayAlter() {
$this
->drupalGet('entity_test/add');
$altered_field = $this
->xpath('//input[@name="field_test_text[0][value]" and @size="42"]');
$this
->assertCount(1, $altered_field, 'The altered field has the correct size value.');
}
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
->drupalPostForm($entity_type . '/add', $edit, t('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
->drupalPostForm($entity_type . '/manage/' . $entity
->id() . '/edit', $edit, t('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
->assertNotEqual($entity->name->value, $name1, new FormattableMarkup('%entity_type: The entity name has been modified.', [
'%entity_type' => $entity_type,
]));
$this
->drupalGet($entity_type . '/manage/' . $entity
->id() . '/edit');
$this
->clickLink(t('Delete'));
$this
->drupalPostForm(NULL, [], t('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
->drupalPostForm($entity_type_id . '/add', $edit, t('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
->assertEqual($translated_entity->name->value, $name1_ro, new FormattableMarkup('%entity_type: The translation has been added.', [
'%entity_type' => $entity_type_id,
]));
$edit['name[0][value]'] = $name2_ro;
$this
->drupalPostForm('ro/' . $entity_type_id . '/manage/' . $entity
->id() . '/edit', $edit, t('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
->assertEqual($translated_entity->name->value, $name2_ro, 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(t('Delete'));
$this
->drupalPostForm(NULL, [], t('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
->drupalPostForm('entity_test/add', [], '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
->drupalPostForm('entity_test/add', [], 'Save');
$this
->assertEqual($state
->get('entity_test.form.save.exception'), 'Drupal\\Core\\Entity\\EntityStorageException: Entity validation was skipped.', 'Button-level validation handlers behave correctly.');
}
}