3 namespace Drupal\Tests\system\Functional\Entity;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests the entity form.
13 class EntityFormTest extends BrowserTestBase {
20 public static $modules = ['entity_test', 'language'];
22 protected function setUp() {
24 $web_user = $this->drupalCreateUser(['administer entity_test content', 'view test entity']);
25 $this->drupalLogin($web_user);
28 ConfigurableLanguage::createFromLangcode('ro')->save();
32 * Tests basic form CRUD functionality.
34 public function testFormCRUD() {
35 // All entity variations have to have the same results.
36 foreach (entity_test_entity_types() as $entity_type) {
37 $this->doTestFormCRUD($entity_type);
42 * Tests basic multilingual form CRUD functionality.
44 public function testMultilingualFormCRUD() {
45 // All entity variations have to have the same results.
46 foreach (entity_test_entity_types(ENTITY_TEST_TYPES_MULTILINGUAL) as $entity_type) {
47 $this->doTestMultilingualFormCRUD($entity_type);
52 * Tests hook_entity_form_display_alter().
54 * @see entity_test_entity_form_display_alter()
56 public function testEntityFormDisplayAlter() {
57 $this->drupalGet('entity_test/add');
58 $altered_field = $this->xpath('//input[@name="field_test_text[0][value]" and @size="42"]');
59 $this->assertTrue(count($altered_field) === 1, 'The altered field has the correct size value.');
63 * Executes the form CRUD tests for the given entity type.
65 * @param string $entity_type
66 * The entity type to run the tests with.
68 protected function doTestFormCRUD($entity_type) {
69 $name1 = $this->randomMachineName(8);
70 $name2 = $this->randomMachineName(10);
73 'name[0][value]' => $name1,
74 'field_test_text[0][value]' => $this->randomMachineName(16),
77 $this->drupalPostForm($entity_type . '/add', $edit, t('Save'));
78 $entity = $this->loadEntityByName($entity_type, $name1);
79 $this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', ['%entity_type' => $entity_type]));
81 $edit['name[0][value]'] = $name2;
82 $this->drupalPostForm($entity_type . '/manage/' . $entity->id() . '/edit', $edit, t('Save'));
83 $entity = $this->loadEntityByName($entity_type, $name1);
84 $this->assertFalse($entity, format_string('%entity_type: The entity has been modified.', ['%entity_type' => $entity_type]));
85 $entity = $this->loadEntityByName($entity_type, $name2);
86 $this->assertTrue($entity, format_string('%entity_type: Modified entity found in the database.', ['%entity_type' => $entity_type]));
87 $this->assertNotEqual($entity->name->value, $name1, format_string('%entity_type: The entity name has been modified.', ['%entity_type' => $entity_type]));
89 $this->drupalGet($entity_type . '/manage/' . $entity->id() . '/edit');
90 $this->clickLink(t('Delete'));
91 $this->drupalPostForm(NULL, [], t('Delete'));
92 $entity = $this->loadEntityByName($entity_type, $name2);
93 $this->assertFalse($entity, format_string('%entity_type: Entity not found in the database.', ['%entity_type' => $entity_type]));
97 * Executes the multilingual form CRUD tests for the given entity type ID.
99 * @param string $entity_type_id
100 * The ID of entity type to run the tests with.
102 protected function doTestMultilingualFormCRUD($entity_type_id) {
103 $name1 = $this->randomMachineName(8);
104 $name1_ro = $this->randomMachineName(9);
105 $name2_ro = $this->randomMachineName(11);
108 'name[0][value]' => $name1,
109 'field_test_text[0][value]' => $this->randomMachineName(16),
112 $this->drupalPostForm($entity_type_id . '/add', $edit, t('Save'));
113 $entity = $this->loadEntityByName($entity_type_id, $name1);
114 $this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', ['%entity_type' => $entity_type_id]));
116 // Add a translation to the newly created entity without using the Content
117 // translation module.
118 $entity->addTranslation('ro', ['name' => $name1_ro])->save();
119 $translated_entity = $this->loadEntityByName($entity_type_id, $name1)->getTranslation('ro');
120 $this->assertEqual($translated_entity->name->value, $name1_ro, format_string('%entity_type: The translation has been added.', ['%entity_type' => $entity_type_id]));
122 $edit['name[0][value]'] = $name2_ro;
123 $this->drupalPostForm('ro/' . $entity_type_id . '/manage/' . $entity->id() . '/edit', $edit, t('Save'));
124 $translated_entity = $this->loadEntityByName($entity_type_id, $name1)->getTranslation('ro');
125 $this->assertTrue($translated_entity, format_string('%entity_type: Modified translation found in the database.', ['%entity_type' => $entity_type_id]));
126 $this->assertEqual($translated_entity->name->value, $name2_ro, format_string('%entity_type: The name of the translation has been modified.', ['%entity_type' => $entity_type_id]));
128 $this->drupalGet('ro/' . $entity_type_id . '/manage/' . $entity->id() . '/edit');
129 $this->clickLink(t('Delete'));
130 $this->drupalPostForm(NULL, [], t('Delete Romanian translation'));
131 $entity = $this->loadEntityByName($entity_type_id, $name1);
132 $this->assertNotNull($entity, format_string('%entity_type: The original entity still exists.', ['%entity_type' => $entity_type_id]));
133 $this->assertFalse($entity->hasTranslation('ro'), format_string('%entity_type: Entity translation does not exist anymore.', ['%entity_type' => $entity_type_id]));
137 * Loads a test entity by name always resetting the storage cache.
139 protected function loadEntityByName($entity_type, $name) {
140 // Always load the entity from the database to ensure that changes are
141 // correctly picked up.
142 $entity_storage = $this->container->get('entity.manager')->getStorage($entity_type);
143 $entity_storage->resetCache();
144 $entities = $entity_storage->loadByProperties(['name' => $name]);
145 return $entities ? current($entities) : NULL;
149 * Checks that validation handlers works as expected.
151 public function testValidationHandlers() {
152 /** @var \Drupal\Core\State\StateInterface $state */
153 $state = $this->container->get('state');
155 // Check that from-level validation handlers can be defined and can alter
157 $state->set('entity_test.form.validate.test', 'form-level');
158 $this->drupalPostForm('entity_test/add', [], 'Save');
159 $this->assertTrue($state->get('entity_test.form.validate.result'), 'Form-level validation handlers behave correctly.');
161 // Check that defining a button-level validation handler causes an exception
163 $state->set('entity_test.form.validate.test', 'button-level');
164 $this->drupalPostForm('entity_test/add', [], 'Save');
165 $this->assertEqual($state->get('entity_test.form.save.exception'), 'Drupal\Core\Entity\EntityStorageException: Entity validation was skipped.', 'Button-level validation handlers behave correctly.');