953684193d3cbb4315f1717817ac1e8e2ce98086
[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', 'node'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     // Create a node type.
31     $this->drupalCreateContentType([
32       'type' => 'article',
33       'name' => 'Article',
34     ]);
35
36     $this->drupalPlaceBlock('local_actions_block');
37     $this->drupalPlaceBlock('page_title_block');
38   }
39
40   /**
41    * Tests the EntityViewMode user interface.
42    */
43   public function testEntityViewModeUI() {
44     // Test the listing page.
45     $this->drupalGet('admin/structure/display-modes/view');
46     $this->assertResponse(403);
47     $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
48     $this->drupalGet('admin/structure/display-modes/view');
49     $this->assertResponse(200);
50     $this->assertText(t('Add view mode'));
51     $this->assertLinkByHref('admin/structure/display-modes/view/add');
52     $this->assertLinkByHref('admin/structure/display-modes/view/add/entity_test');
53
54     $this->drupalGet('admin/structure/display-modes/view/add/entity_test_mulrev');
55     $this->assertResponse(404);
56
57     $this->drupalGet('admin/structure/display-modes/view/add');
58     $this->assertNoLink(t('Test entity - revisions and data table'), 'An entity type with no view builder cannot have view modes.');
59
60     // Test adding a view mode including dots in machine_name.
61     $this->clickLink(t('Test entity'));
62     $edit = [
63       'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
64       'label' => $this->randomString(),
65     ];
66     $this->drupalPostForm(NULL, $edit, t('Save'));
67     $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
68
69     // Test adding a view mode.
70     $edit = [
71       'id' => strtolower($this->randomMachineName()),
72       'label' => $this->randomString(),
73     ];
74     $this->drupalPostForm(NULL, $edit, t('Save'));
75     $this->assertRaw(t('Saved the %label view mode.', ['%label' => $edit['label']]));
76
77     // Test editing the view mode.
78     $this->drupalGet('admin/structure/display-modes/view/manage/entity_test.' . $edit['id']);
79
80     // Test that the link templates added by field_ui_entity_type_build() are
81     // generating valid routes.
82     $view_mode = EntityViewMode::load('entity_test.' . $edit['id']);
83     $this->assertEquals(Url::fromRoute('entity.entity_view_mode.collection')->toString(), $view_mode->toUrl('collection')->toString());
84     $this->assertEquals(Url::fromRoute('entity.entity_view_mode.add_form', ['entity_type_id' => $view_mode->getTargetType()])->toString(), $view_mode->toUrl('add-form')->toString());
85     $this->assertEquals(Url::fromRoute('entity.entity_view_mode.edit_form', ['entity_view_mode' => $view_mode->id()])->toString(), $view_mode->toUrl('edit-form')->toString());
86     $this->assertEquals(Url::fromRoute('entity.entity_view_mode.delete_form', ['entity_view_mode' => $view_mode->id()])->toString(), $view_mode->toUrl('delete-form')->toString());
87
88     // Test deleting the view mode.
89     $this->clickLink(t('Delete'));
90     $this->assertRaw(t('Are you sure you want to delete the view mode %label?', ['%label' => $edit['label']]));
91     $this->drupalPostForm(NULL, NULL, t('Delete'));
92     $this->assertRaw(t('The view mode %label has been deleted.', ['%label' => $edit['label']]));
93   }
94
95   /**
96    * Tests the EntityFormMode user interface.
97    */
98   public function testEntityFormModeUI() {
99     // Test the listing page.
100     $this->drupalGet('admin/structure/display-modes/form');
101     $this->assertResponse(403);
102     $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
103     $this->drupalGet('admin/structure/display-modes/form');
104     $this->assertResponse(200);
105     $this->assertText(t('Add form mode'));
106     $this->assertLinkByHref('admin/structure/display-modes/form/add');
107
108     $this->drupalGet('admin/structure/display-modes/form/add/entity_test_no_label');
109     $this->assertResponse(404);
110
111     $this->drupalGet('admin/structure/display-modes/form/add');
112     $this->assertNoLink(t('Entity Test without label'), 'An entity type with no form cannot have form modes.');
113
114     // Test adding a view mode including dots in machine_name.
115     $this->clickLink(t('Test entity'));
116     $edit = [
117       'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
118       'label' => $this->randomString(),
119     ];
120     $this->drupalPostForm(NULL, $edit, t('Save'));
121     $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');
122
123     // Test adding a form mode.
124     $edit = [
125       'id' => strtolower($this->randomMachineName()),
126       'label' => $this->randomString(),
127     ];
128     $this->drupalPostForm(NULL, $edit, t('Save'));
129     $this->assertRaw(t('Saved the %label form mode.', ['%label' => $edit['label']]));
130
131     // Test editing the form mode.
132     $this->drupalGet('admin/structure/display-modes/form/manage/entity_test.' . $edit['id']);
133
134     // Test that the link templates added by field_ui_entity_type_build() are
135     // generating valid routes.
136     $form_mode = EntityFormMode::load('entity_test.' . $edit['id']);
137     $this->assertEquals(Url::fromRoute('entity.entity_form_mode.collection')->toString(), $form_mode->toUrl('collection')->toString());
138     $this->assertEquals(Url::fromRoute('entity.entity_form_mode.add_form', ['entity_type_id' => $form_mode->getTargetType()])->toString(), $form_mode->toUrl('add-form')->toString());
139     $this->assertEquals(Url::fromRoute('entity.entity_form_mode.edit_form', ['entity_form_mode' => $form_mode->id()])->toString(), $form_mode->toUrl('edit-form')->toString());
140     $this->assertEquals(Url::fromRoute('entity.entity_form_mode.delete_form', ['entity_form_mode' => $form_mode->id()])->toString(), $form_mode->toUrl('delete-form')->toString());
141
142     // Test deleting the form mode.
143     $this->clickLink(t('Delete'));
144     $this->assertRaw(t('Are you sure you want to delete the form mode %label?', ['%label' => $edit['label']]));
145     $this->drupalPostForm(NULL, NULL, t('Delete'));
146     $this->assertRaw(t('The form mode %label has been deleted.', ['%label' => $edit['label']]));
147   }
148
149   /**
150    * Tests if view modes appear in alphabetical order by visible name.
151    *
152    * The machine name should not be used for sorting.
153    *
154    * @see https://www.drupal.org/node/2858569
155    */
156   public function testAlphabeticalDisplaySettings() {
157     $this->drupalLogin($this->drupalCreateUser([
158       'access administration pages',
159       'administer content types',
160       'administer display modes',
161       'administer nodes',
162       'administer node fields',
163       'administer node display',
164       'administer node form display',
165       'view the administration theme',
166     ]));
167     $this->drupalGet('admin/structure/types/manage/article/display');
168     // Verify that the order of view modes is alphabetical by visible label.
169     // Since the default view modes all have machine names which coincide with
170     // the English labels, they should appear in alphabetical order, by default
171     // if viewing the site in English and if no changes have been made. We will
172     // verify this first.
173     $page_text = $this->getTextContent();
174     $start = strpos($page_text, 'view modes');
175     $pos = $start;
176     $list = ['Full content', 'RSS', 'Search index', 'Search result', 'Teaser'];
177     foreach ($list as $name) {
178       $new_pos = strpos($page_text, $name, $start);
179       $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
180       $pos = $new_pos;
181     }
182     // Now that we have verified the original display order, we can change the
183     // label for one of the view modes. If we rename "Teaser" to "Breezer", it
184     // should appear as the first of the listed view modes:
185     // Set new values and enable test plugins.
186     $edit = [
187       'label' => 'Breezer',
188     ];
189     $this->drupalPostForm('admin/structure/display-modes/view/manage/node.teaser', $edit, 'Save');
190     $this->assertSession()->pageTextContains('Saved the Breezer view mode.');
191
192     // Re-open the display settings for the article content type and verify
193     // that changing "Teaser" to "Breezer" makes it appear before "Full
194     // content".
195     $this->drupalGet('admin/structure/types/manage/article/display');
196     $page_text = $this->getTextContent();
197     $start = strpos($page_text, 'view modes');
198     $pos = $start;
199     $list = ['Breezer', 'Full content'];
200     foreach ($list as $name) {
201       $new_pos = strpos($page_text, $name, $start);
202       $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
203       $pos = $new_pos;
204     }
205   }
206
207 }