View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
class EntityFormTest extends WebTestBase {
public static $modules = array(
'entity_test',
'language',
);
protected function setUp() {
parent::setUp();
$web_user = $this
->drupalCreateUser(array(
'administer entity_test content',
'view test entity',
));
$this
->drupalLogin($web_user);
ConfigurableLanguage::createFromLangcode('ro')
->save();
}
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);
}
}
function testEntityFormDisplayAlter() {
$this
->drupalGet('entity_test/add');
$altered_field = $this
->xpath('//input[@name="field_test_text[0][value]" and @size="42"]');
$this
->assertTrue(count($altered_field) === 1, 'The altered field has the correct size value.');
}
protected function doTestFormCRUD($entity_type) {
$name1 = $this
->randomMachineName(8);
$name2 = $this
->randomMachineName(10);
$edit = array(
'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
->assertTrue($entity, format_string('%entity_type: Entity found in the database.', array(
'%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
->assertFalse($entity, format_string('%entity_type: The entity has been modified.', array(
'%entity_type' => $entity_type,
)));
$entity = $this
->loadEntityByName($entity_type, $name2);
$this
->assertTrue($entity, format_string('%entity_type: Modified entity found in the database.', array(
'%entity_type' => $entity_type,
)));
$this
->assertNotEqual($entity->name->value, $name1, format_string('%entity_type: The entity name has been modified.', array(
'%entity_type' => $entity_type,
)));
$this
->drupalGet($entity_type . '/manage/' . $entity
->id() . '/edit');
$this
->clickLink(t('Delete'));
$this
->drupalPostForm(NULL, array(), t('Delete'));
$entity = $this
->loadEntityByName($entity_type, $name2);
$this
->assertFalse($entity, format_string('%entity_type: Entity not found in the database.', array(
'%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 = array(
'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
->assertTrue($entity, format_string('%entity_type: Entity found in the database.', array(
'%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, format_string('%entity_type: The translation has been added.', array(
'%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
->assertTrue($translated_entity, format_string('%entity_type: Modified translation found in the database.', array(
'%entity_type' => $entity_type_id,
)));
$this
->assertEqual($translated_entity->name->value, $name2_ro, format_string('%entity_type: The name of the translation has been modified.', array(
'%entity_type' => $entity_type_id,
)));
$this
->drupalGet('ro/' . $entity_type_id . '/manage/' . $entity
->id() . '/edit');
$this
->clickLink(t('Delete'));
$this
->drupalPostForm(NULL, array(), t('Delete Romanian translation'));
$entity = $this
->loadEntityByName($entity_type_id, $name1);
$this
->assertNotNull($entity, format_string('%entity_type: The original entity still exists.', array(
'%entity_type' => $entity_type_id,
)));
$this
->assertFalse($entity
->hasTranslation('ro'), format_string('%entity_type: Entity translation does not exist anymore.', array(
'%entity_type' => $entity_type_id,
)));
}
protected function loadEntityByName($entity_type, $name) {
$entity_storage = $this->container
->get('entity.manager')
->getStorage($entity_type);
$entity_storage
->resetCache();
$entities = $entity_storage
->loadByProperties(array(
'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.');
}
}