3 namespace Drupal\Tests\field_ui\Functional;
5 use Drupal\Core\Entity\Entity\EntityFormMode;
6 use Drupal\Core\Entity\Entity\EntityViewMode;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the entity display modes UI.
15 class EntityDisplayModeTest extends BrowserTestBase {
22 public static $modules = ['block', 'entity_test', 'field_ui'];
27 protected function setUp() {
30 $this->drupalPlaceBlock('local_actions_block');
31 $this->drupalPlaceBlock('page_title_block');
35 * Tests the EntityViewMode user interface.
37 public function testEntityViewModeUI() {
38 // Test the listing page.
39 $this->drupalGet('admin/structure/display-modes/view');
40 $this->assertResponse(403);
41 $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
42 $this->drupalGet('admin/structure/display-modes/view');
43 $this->assertResponse(200);
44 $this->assertText(t('Add view mode'));
45 $this->assertLinkByHref('admin/structure/display-modes/view/add');
46 $this->assertLinkByHref('admin/structure/display-modes/view/add/entity_test');
48 $this->drupalGet('admin/structure/display-modes/view/add/entity_test_mulrev');
49 $this->assertResponse(404);
51 $this->drupalGet('admin/structure/display-modes/view/add');
52 $this->assertNoLink(t('Test entity - revisions and data table'), 'An entity type with no view builder cannot have view modes.');
54 // Test adding a view mode including dots in machine_name.
55 $this->clickLink(t('Test entity'));
57 'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
58 'label' => $this->randomString(),
60 $this->drupalPostForm(NULL, $edit, t('Save'));
61 $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
63 // Test adding a view mode.
65 'id' => strtolower($this->randomMachineName()),
66 'label' => $this->randomString(),
68 $this->drupalPostForm(NULL, $edit, t('Save'));
69 $this->assertRaw(t('Saved the %label view mode.', ['%label' => $edit['label']]));
71 // Test editing the view mode.
72 $this->drupalGet('admin/structure/display-modes/view/manage/entity_test.' . $edit['id']);
74 // Test that the link templates added by field_ui_entity_type_build() are
75 // generating valid routes.
76 $view_mode = EntityViewMode::load('entity_test.' . $edit['id']);
77 $this->assertEquals(Url::fromRoute('entity.entity_view_mode.collection')->toString(), $view_mode->toUrl('collection')->toString());
78 $this->assertEquals(Url::fromRoute('entity.entity_view_mode.add_form', ['entity_type_id' => $view_mode->getTargetType()])->toString(), $view_mode->toUrl('add-form')->toString());
79 $this->assertEquals(Url::fromRoute('entity.entity_view_mode.edit_form', ['entity_view_mode' => $view_mode->id()])->toString(), $view_mode->toUrl('edit-form')->toString());
80 $this->assertEquals(Url::fromRoute('entity.entity_view_mode.delete_form', ['entity_view_mode' => $view_mode->id()])->toString(), $view_mode->toUrl('delete-form')->toString());
82 // Test deleting the view mode.
83 $this->clickLink(t('Delete'));
84 $this->assertRaw(t('Are you sure you want to delete the view mode %label?', ['%label' => $edit['label']]));
85 $this->drupalPostForm(NULL, NULL, t('Delete'));
86 $this->assertRaw(t('The view mode %label has been deleted.', ['%label' => $edit['label']]));
90 * Tests the EntityFormMode user interface.
92 public function testEntityFormModeUI() {
93 // Test the listing page.
94 $this->drupalGet('admin/structure/display-modes/form');
95 $this->assertResponse(403);
96 $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
97 $this->drupalGet('admin/structure/display-modes/form');
98 $this->assertResponse(200);
99 $this->assertText(t('Add form mode'));
100 $this->assertLinkByHref('admin/structure/display-modes/form/add');
102 $this->drupalGet('admin/structure/display-modes/form/add/entity_test_no_label');
103 $this->assertResponse(404);
105 $this->drupalGet('admin/structure/display-modes/form/add');
106 $this->assertNoLink(t('Entity Test without label'), 'An entity type with no form cannot have form modes.');
108 // Test adding a view mode including dots in machine_name.
109 $this->clickLink(t('Test entity'));
111 'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
112 'label' => $this->randomString(),
114 $this->drupalPostForm(NULL, $edit, t('Save'));
115 $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
117 // Test adding a form mode.
119 'id' => strtolower($this->randomMachineName()),
120 'label' => $this->randomString(),
122 $this->drupalPostForm(NULL, $edit, t('Save'));
123 $this->assertRaw(t('Saved the %label form mode.', ['%label' => $edit['label']]));
125 // Test editing the form mode.
126 $this->drupalGet('admin/structure/display-modes/form/manage/entity_test.' . $edit['id']);
128 // Test that the link templates added by field_ui_entity_type_build() are
129 // generating valid routes.
130 $form_mode = EntityFormMode::load('entity_test.' . $edit['id']);
131 $this->assertEquals(Url::fromRoute('entity.entity_form_mode.collection')->toString(), $form_mode->toUrl('collection')->toString());
132 $this->assertEquals(Url::fromRoute('entity.entity_form_mode.add_form', ['entity_type_id' => $form_mode->getTargetType()])->toString(), $form_mode->toUrl('add-form')->toString());
133 $this->assertEquals(Url::fromRoute('entity.entity_form_mode.edit_form', ['entity_form_mode' => $form_mode->id()])->toString(), $form_mode->toUrl('edit-form')->toString());
134 $this->assertEquals(Url::fromRoute('entity.entity_form_mode.delete_form', ['entity_form_mode' => $form_mode->id()])->toString(), $form_mode->toUrl('delete-form')->toString());
136 // Test deleting the form mode.
137 $this->clickLink(t('Delete'));
138 $this->assertRaw(t('Are you sure you want to delete the form mode %label?', ['%label' => $edit['label']]));
139 $this->drupalPostForm(NULL, NULL, t('Delete'));
140 $this->assertRaw(t('The form mode %label has been deleted.', ['%label' => $edit['label']]));