2ebc35061782bebc71f06d3f3dabd6afeeeb7463
[yaffs-website] / web / core / modules / system / src / Tests / Entity / EntityFormTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Entity;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Tests the entity form.
10  *
11  * @group Entity
12  */
13 class EntityFormTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['entity_test', 'language'];
21
22   protected function setUp() {
23     parent::setUp();
24     $web_user = $this->drupalCreateUser(['administer entity_test content', 'view test entity']);
25     $this->drupalLogin($web_user);
26
27     // Add a language.
28     ConfigurableLanguage::createFromLangcode('ro')->save();
29   }
30
31   /**
32    * Tests basic form CRUD functionality.
33    */
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);
38     }
39   }
40
41   /**
42    * Tests basic multilingual form CRUD functionality.
43    */
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);
48     }
49   }
50
51   /**
52    * Tests hook_entity_form_display_alter().
53    *
54    * @see entity_test_entity_form_display_alter()
55    */
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.');
60   }
61
62   /**
63    * Executes the form CRUD tests for the given entity type.
64    *
65    * @param string $entity_type
66    *   The entity type to run the tests with.
67    */
68   protected function doTestFormCRUD($entity_type) {
69     $name1 = $this->randomMachineName(8);
70     $name2 = $this->randomMachineName(10);
71
72     $edit = [
73       'name[0][value]' => $name1,
74       'field_test_text[0][value]' => $this->randomMachineName(16),
75     ];
76
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]));
80
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]));
88
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]));
94   }
95
96   /**
97    * Executes the multilingual form CRUD tests for the given entity type ID.
98    *
99    * @param string $entity_type_id
100    *   The ID of entity type to run the tests with.
101    */
102   protected function doTestMultilingualFormCRUD($entity_type_id) {
103     $name1 = $this->randomMachineName(8);
104     $name1_ro = $this->randomMachineName(9);
105     $name2_ro = $this->randomMachineName(11);
106
107     $edit = [
108       'name[0][value]' => $name1,
109       'field_test_text[0][value]' => $this->randomMachineName(16),
110     ];
111
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]));
115
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]));
121
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]));
127
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]));
134   }
135
136   /**
137    * Loads a test entity by name always resetting the storage cache.
138    */
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;
146   }
147
148   /**
149    * Checks that validation handlers works as expected.
150    */
151   public function testValidationHandlers() {
152     /** @var \Drupal\Core\State\StateInterface $state */
153     $state = $this->container->get('state');
154
155     // Check that from-level validation handlers can be defined and can alter
156     // the form array.
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.');
160
161     // Check that defining a button-level validation handler causes an exception
162     // to be thrown.
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.');
166   }
167
168 }