You are here

public function EntityDisplayModeTest::testEntityViewModeUI in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_ui/tests/src/Functional/EntityDisplayModeTest.php \Drupal\Tests\field_ui\Functional\EntityDisplayModeTest::testEntityViewModeUI()

Tests the EntityViewMode user interface.

File

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

Class

EntityDisplayModeTest
Tests the entity display modes UI.

Namespace

Drupal\Tests\field_ui\Functional

Code

public function testEntityViewModeUI() {

  // Test the listing page.
  $this
    ->drupalGet('admin/structure/display-modes/view');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'administer display modes',
  ]));
  $this
    ->drupalGet('admin/structure/display-modes/view');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains('Add view mode');
  $this
    ->assertSession()
    ->linkByHrefExists('admin/structure/display-modes/view/add');
  $this
    ->assertSession()
    ->linkByHrefExists('admin/structure/display-modes/view/add/entity_test');
  $this
    ->drupalGet('admin/structure/display-modes/view/add/entity_test_mulrev');
  $this
    ->assertSession()
    ->statusCodeEquals(404);
  $this
    ->drupalGet('admin/structure/display-modes/view/add');
  $this
    ->assertSession()
    ->linkNotExists('Test entity - revisions and data table', 'An entity type with no view builder cannot have view modes.');

  // Test adding a view mode including dots in machine_name.
  $this
    ->clickLink('Test entity');
  $edit = [
    'id' => strtolower($this
      ->randomMachineName()) . '.' . strtolower($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 view mode.
  $edit = [
    'id' => strtolower($this
      ->randomMachineName()),
    'label' => $this
      ->randomString(),
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains("Saved the {$edit['label']} view mode.");

  // Test editing the view mode.
  $this
    ->drupalGet('admin/structure/display-modes/view/manage/entity_test.' . $edit['id']);

  // Test that the link templates added by field_ui_entity_type_build() are
  // generating valid routes.
  $view_mode = EntityViewMode::load('entity_test.' . $edit['id']);
  $this
    ->assertEquals(Url::fromRoute('entity.entity_view_mode.collection')
    ->toString(), $view_mode
    ->toUrl('collection')
    ->toString());
  $this
    ->assertEquals(Url::fromRoute('entity.entity_view_mode.add_form', [
    'entity_type_id' => $view_mode
      ->getTargetType(),
  ])
    ->toString(), $view_mode
    ->toUrl('add-form')
    ->toString());
  $this
    ->assertEquals(Url::fromRoute('entity.entity_view_mode.edit_form', [
    'entity_view_mode' => $view_mode
      ->id(),
  ])
    ->toString(), $view_mode
    ->toUrl('edit-form')
    ->toString());
  $this
    ->assertEquals(Url::fromRoute('entity.entity_view_mode.delete_form', [
    'entity_view_mode' => $view_mode
      ->id(),
  ])
    ->toString(), $view_mode
    ->toUrl('delete-form')
    ->toString());

  // Test deleting the view mode.
  $this
    ->clickLink('Delete');
  $this
    ->assertSession()
    ->pageTextContains("Are you sure you want to delete the view mode {$edit['label']}?");
  $this
    ->submitForm([], 'Delete');
  $this
    ->assertSession()
    ->pageTextContains("The view mode {$edit['label']} has been deleted.");
}