function EntityDisplayModeTest::testEntityFormModeUI

Same name in other branches
  1. 9 core/modules/field_ui/tests/src/Functional/EntityDisplayModeTest.php \Drupal\Tests\field_ui\Functional\EntityDisplayModeTest::testEntityFormModeUI()
  2. 8.9.x core/modules/field_ui/tests/src/Functional/EntityDisplayModeTest.php \Drupal\Tests\field_ui\Functional\EntityDisplayModeTest::testEntityFormModeUI()
  3. 10 core/modules/field_ui/tests/src/Functional/EntityDisplayModeTest.php \Drupal\Tests\field_ui\Functional\EntityDisplayModeTest::testEntityFormModeUI()

Tests the EntityFormMode user interface.

File

core/modules/field_ui/tests/src/Functional/EntityDisplayModeTest.php, line 105

Class

EntityDisplayModeTest
Tests the entity display modes UI.

Namespace

Drupal\Tests\field_ui\Functional

Code

public function testEntityFormModeUI() : void {
    // Test the listing page.
    $this->drupalGet('admin/structure/display-modes/form');
    $this->assertSession()
        ->statusCodeEquals(403);
    $this->drupalLogin($this->drupalCreateUser([
        'administer display modes',
    ]));
    $this->drupalGet('admin/structure/display-modes/form');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('Add form mode');
    $this->assertSession()
        ->linkByHrefExists('admin/structure/display-modes/form/add');
    $this->drupalGet('admin/structure/display-modes/form/add/entity_test_no_label');
    $this->assertSession()
        ->statusCodeEquals(404);
    $this->drupalGet('admin/structure/display-modes/form/add');
    $this->assertSession()
        ->linkNotExists('Entity Test without label', 'An entity type with no form cannot have form modes.');
    // Test adding a view mode including dots in machine_name.
    $this->clickLink('Test entity');
    // Check if 'Name' field is required.
    $this->assertTrue($this->getSession()
        ->getPage()
        ->findField('label')
        ->hasClass('required'));
    $edit = [
        'id' => $this->randomMachineName() . '.' . $this->randomMachineName(),
        'label' => $this->randomString(),
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
    // Test adding a form mode.
    $edit = [
        'id' => $this->randomMachineName(),
        'label' => $this->randomString(),
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains("Saved the {$edit['label']} form mode.");
    // Test editing the form mode.
    $this->drupalGet('admin/structure/display-modes/form/manage/entity_test.' . $edit['id']);
    // Test that the link templates added by field_ui_entity_type_build() are
    // generating valid routes.
    $form_mode = EntityFormMode::load('entity_test.' . $edit['id']);
    $this->assertEquals(Url::fromRoute('entity.entity_form_mode.collection')->toString(), $form_mode->toUrl('collection')
        ->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_form_mode.add_form', [
        'entity_type_id' => $form_mode->getTargetType(),
    ])
        ->toString(), $form_mode->toUrl('add-form')
        ->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_form_mode.edit_form', [
        'entity_form_mode' => $form_mode->id(),
    ])
        ->toString(), $form_mode->toUrl('edit-form')
        ->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_form_mode.delete_form', [
        'entity_form_mode' => $form_mode->id(),
    ])
        ->toString(), $form_mode->toUrl('delete-form')
        ->toString());
    // Test deleting the form mode.
    $this->clickLink('Delete');
    $this->assertSession()
        ->pageTextContains("Are you sure you want to delete the form mode {$edit['label']}?");
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->pageTextContains("The form mode {$edit['label']} has been deleted.");
}

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