78764fb32da41ef93dc74a49505964eaa55aaa65
[yaffs-website] / web / core / modules / field_ui / tests / src / Functional / EntityDisplayModeTest.php
1 <?php
2
3 namespace Drupal\Tests\field_ui\Functional;
4
5 use Drupal\Core\Entity\Entity\EntityFormMode;
6 use Drupal\Core\Entity\Entity\EntityViewMode;
7 use Drupal\Core\Url;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests the entity display modes UI.
12  *
13  * @group field_ui
14  */
15 class EntityDisplayModeTest extends BrowserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var string[]
21    */
22   public static $modules = ['block', 'entity_test', 'field_ui'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $this->drupalPlaceBlock('local_actions_block');
31     $this->drupalPlaceBlock('page_title_block');
32   }
33
34   /**
35    * Tests the EntityViewMode user interface.
36    */
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');
47
48     $this->drupalGet('admin/structure/display-modes/view/add/entity_test_mulrev');
49     $this->assertResponse(404);
50
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.');
53
54     // Test adding a view mode including dots in machine_name.
55     $this->clickLink(t('Test entity'));
56     $edit = [
57       'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
58       'label' => $this->randomString(),
59     ];
60     $this->drupalPostForm(NULL, $edit, t('Save'));
61     $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
62
63     // Test adding a view mode.
64     $edit = [
65       'id' => strtolower($this->randomMachineName()),
66       'label' => $this->randomString(),
67     ];
68     $this->drupalPostForm(NULL, $edit, t('Save'));
69     $this->assertRaw(t('Saved the %label view mode.', ['%label' => $edit['label']]));
70
71     // Test editing the view mode.
72     $this->drupalGet('admin/structure/display-modes/view/manage/entity_test.' . $edit['id']);
73
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());
81
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']]));
87   }
88
89   /**
90    * Tests the EntityFormMode user interface.
91    */
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');
101
102     $this->drupalGet('admin/structure/display-modes/form/add/entity_test_no_label');
103     $this->assertResponse(404);
104
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.');
107
108     // Test adding a view mode including dots in machine_name.
109     $this->clickLink(t('Test entity'));
110     $edit = [
111       'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
112       'label' => $this->randomString(),
113     ];
114     $this->drupalPostForm(NULL, $edit, t('Save'));
115     $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
116
117     // Test adding a form mode.
118     $edit = [
119       'id' => strtolower($this->randomMachineName()),
120       'label' => $this->randomString(),
121     ];
122     $this->drupalPostForm(NULL, $edit, t('Save'));
123     $this->assertRaw(t('Saved the %label form mode.', ['%label' => $edit['label']]));
124
125     // Test editing the form mode.
126     $this->drupalGet('admin/structure/display-modes/form/manage/entity_test.' . $edit['id']);
127
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());
135
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']]));
141   }
142
143 }