EntityDisplayModeTest.php 9.02 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\field_ui\Functional;
4

5
6
7
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Url;
8
use Drupal\Tests\BrowserTestBase;
9
10

/**
11
12
 * Tests the entity display modes UI.
 *
13
 * @group field_ui
14
 */
15
class EntityDisplayModeTest extends BrowserTestBase {
16
17
18
19

  /**
   * Modules to enable.
   *
20
   * @var string[]
21
   */
22
  protected static $modules = ['block', 'entity_test', 'field_ui', 'node'];
23

24
25
26
27
28
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

29
30
31
  /**
   * {@inheritdoc}
   */
32
  protected function setUp(): void {
33
34
    parent::setUp();

35
36
37
38
39
40
    // Create a node type.
    $this->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);

41
    $this->drupalPlaceBlock('local_actions_block');
42
    $this->drupalPlaceBlock('page_title_block');
43
  }
44
45
46
47
48
49
50

  /**
   * Tests the EntityViewMode user interface.
   */
  public function testEntityViewModeUI() {
    // Test the listing page.
    $this->drupalGet('admin/structure/display-modes/view');
51
    $this->assertSession()->statusCodeEquals(403);
52
    $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
53
    $this->drupalGet('admin/structure/display-modes/view');
54
    $this->assertSession()->statusCodeEquals(200);
55
    $this->assertText(t('Add view mode'));
56
    $this->assertLinkByHref('admin/structure/display-modes/view/add');
57
    $this->assertLinkByHref('admin/structure/display-modes/view/add/entity_test');
58

59
    $this->drupalGet('admin/structure/display-modes/view/add/entity_test_mulrev');
60
    $this->assertSession()->statusCodeEquals(404);
61
62

    $this->drupalGet('admin/structure/display-modes/view/add');
63
    $this->assertSession()->linkNotExists('Test entity - revisions and data table', 'An entity type with no view builder cannot have view modes.');
64

65
    // Test adding a view mode including dots in machine_name.
66
    $this->clickLink(t('Test entity'));
67
    $edit = [
68
      'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
69
      'label' => $this->randomString(),
70
    ];
71
72
73
74
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');

    // Test adding a view mode.
75
    $edit = [
76
      'id' => strtolower($this->randomMachineName()),
77
      'label' => $this->randomString(),
78
    ];
79
    $this->drupalPostForm(NULL, $edit, t('Save'));
80
    $this->assertRaw(t('Saved the %label view mode.', ['%label' => $edit['label']]));
81
82

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

85
86
87
88
89
90
91
92
    // 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());

93
    // Test deleting the view mode.
94
    $this->clickLink(t('Delete'));
95
    $this->assertRaw(t('Are you sure you want to delete the view mode %label?', ['%label' => $edit['label']]));
96
    $this->drupalPostForm(NULL, NULL, t('Delete'));
97
    $this->assertRaw(t('The view mode %label has been deleted.', ['%label' => $edit['label']]));
98
99
100
101
102
103
104
105
  }

  /**
   * Tests the EntityFormMode user interface.
   */
  public function testEntityFormModeUI() {
    // Test the listing page.
    $this->drupalGet('admin/structure/display-modes/form');
106
    $this->assertSession()->statusCodeEquals(403);
107
    $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
108
    $this->drupalGet('admin/structure/display-modes/form');
109
    $this->assertSession()->statusCodeEquals(200);
110
    $this->assertText(t('Add form mode'));
111
    $this->assertLinkByHref('admin/structure/display-modes/form/add');
112

113
    $this->drupalGet('admin/structure/display-modes/form/add/entity_test_no_label');
114
    $this->assertSession()->statusCodeEquals(404);
115
116

    $this->drupalGet('admin/structure/display-modes/form/add');
117
    $this->assertSession()->linkNotExists('Entity Test without label', 'An entity type with no form cannot have form modes.');
118

119
    // Test adding a view mode including dots in machine_name.
120
    $this->clickLink(t('Test entity'));
121
    $edit = [
122
      'id' => strtolower($this->randomMachineName()) . '.' . strtolower($this->randomMachineName()),
123
      'label' => $this->randomString(),
124
    ];
125
126
127
128
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw('The machine-readable name must contain only lowercase letters, numbers, and underscores.');

    // Test adding a form mode.
129
    $edit = [
130
      'id' => strtolower($this->randomMachineName()),
131
      'label' => $this->randomString(),
132
    ];
133
    $this->drupalPostForm(NULL, $edit, t('Save'));
134
    $this->assertRaw(t('Saved the %label form mode.', ['%label' => $edit['label']]));
135
136
137
138

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

139
140
141
142
143
144
145
146
    // 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());

147
    // Test deleting the form mode.
148
    $this->clickLink(t('Delete'));
149
    $this->assertRaw(t('Are you sure you want to delete the form mode %label?', ['%label' => $edit['label']]));
150
    $this->drupalPostForm(NULL, NULL, t('Delete'));
151
    $this->assertRaw(t('The form mode %label has been deleted.', ['%label' => $edit['label']]));
152
153
  }

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  /**
   * Tests if view modes appear in alphabetical order by visible name.
   *
   * The machine name should not be used for sorting.
   *
   * @see https://www.drupal.org/node/2858569
   */
  public function testAlphabeticalDisplaySettings() {
    $this->drupalLogin($this->drupalCreateUser([
      'access administration pages',
      'administer content types',
      'administer display modes',
      'administer nodes',
      'administer node fields',
      'administer node display',
      'administer node form display',
      'view the administration theme',
    ]));
    $this->drupalGet('admin/structure/types/manage/article/display');
    // Verify that the order of view modes is alphabetical by visible label.
    // Since the default view modes all have machine names which coincide with
    // the English labels, they should appear in alphabetical order, by default
    // if viewing the site in English and if no changes have been made. We will
    // verify this first.
    $page_text = $this->getTextContent();
    $start = strpos($page_text, 'view modes');
    $pos = $start;
    $list = ['Full content', 'RSS', 'Search index', 'Search result', 'Teaser'];
    foreach ($list as $name) {
      $new_pos = strpos($page_text, $name, $start);
      $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
      $pos = $new_pos;
    }
    // Now that we have verified the original display order, we can change the
    // label for one of the view modes. If we rename "Teaser" to "Breezer", it
    // should appear as the first of the listed view modes:
    // Set new values and enable test plugins.
    $edit = [
      'label' => 'Breezer',
    ];
    $this->drupalPostForm('admin/structure/display-modes/view/manage/node.teaser', $edit, 'Save');
    $this->assertSession()->pageTextContains('Saved the Breezer view mode.');

    // Re-open the display settings for the article content type and verify
    // that changing "Teaser" to "Breezer" makes it appear before "Full
    // content".
    $this->drupalGet('admin/structure/types/manage/article/display');
    $page_text = $this->getTextContent();
    $start = strpos($page_text, 'view modes');
    $pos = $start;
    $list = ['Breezer', 'Full content'];
    foreach ($list as $name) {
      $new_pos = strpos($page_text, $name, $start);
      $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page');
      $pos = $new_pos;
    }
  }

212
}