You are here

public function NestedFormTest::testNestedEntityFormEntityLevelValidation in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/NestedFormTest.php \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()

Tests entity level validation within subforms.

File

core/modules/field/tests/src/Functional/NestedFormTest.php, line 181

Class

NestedFormTest
Tests field elements in nested forms.

Namespace

Drupal\Tests\field\Functional

Code

public function testNestedEntityFormEntityLevelValidation() {

  // Create two entities.
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test_constraints');
  $entity_1 = $storage
    ->create();
  $entity_1
    ->save();
  $entity_2 = $storage
    ->create();
  $entity_2
    ->save();
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Display the 'combined form'.
  $this
    ->drupalGet("test-entity-constraints/nested/{$entity_1->id()}/{$entity_2->id()}");
  $assert_session
    ->hiddenFieldValueEquals('entity_2[changed]', REQUEST_TIME);

  // Submit the form and check that the entities are updated accordingly.
  $assert_session
    ->hiddenFieldExists('entity_2[changed]')
    ->setValue(REQUEST_TIME - 86400);
  $page
    ->pressButton(t('Save'));
  $elements = $this
    ->cssSelect('.entity-2.error');
  $this
    ->assertCount(1, $elements, 'The whole nested entity form has been correctly flagged with an error class.');
}