function NestedFormTest::testNestedEntityFormEntityLevelValidation

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/NestedFormTest.php \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()
  2. 8.9.x core/modules/field/tests/src/Functional/NestedFormTest.php \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()
  3. 11.x 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 204

Class

NestedFormTest
Tests field elements in nested forms.

Namespace

Drupal\Tests\field\Functional

Code

public function testNestedEntityFormEntityLevelValidation() : void {
    // 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]', (string) \Drupal::time()->getRequestTime());
    // Submit the form and check that the entities are updated accordingly.
    $assert_session->hiddenFieldExists('entity_2[changed]')
        ->setValue(\Drupal::time()->getRequestTime() - 86400);
    $page->pressButton('Save');
    $elements = $this->cssSelect('.entity-2.error');
    $this->assertCount(1, $elements, 'The whole nested entity form has been correctly flagged with an error class.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.