EntityDisplayModeTest.php 8.81 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
  public static $modules = ['block', 'entity_test', 'field_ui', 'node'];
23 24 25 26 27 28 29

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

30 31 32 33 34 35
    // Create a node type.
    $this->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);

36
    $this->drupalPlaceBlock('local_actions_block');
37
    $this->drupalPlaceBlock('page_title_block');
38
  }
39 40 41 42 43 44 45 46

  /**
   * Tests the EntityViewMode user interface.
   */
  public function testEntityViewModeUI() {
    // Test the listing page.
    $this->drupalGet('admin/structure/display-modes/view');
    $this->assertResponse(403);
47
    $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
48 49
    $this->drupalGet('admin/structure/display-modes/view');
    $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 56 57
    $this->assertResponse(404);

    $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 67 68 69
    $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.
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 81 82 83 84 85 86 87
    // 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());

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 97 98 99 100 101
  }

  /**
   * Tests the EntityFormMode user interface.
   */
  public function testEntityFormModeUI() {
    // Test the listing page.
    $this->drupalGet('admin/structure/display-modes/form');
    $this->assertResponse(403);
102
    $this->drupalLogin($this->drupalCreateUser(['administer display modes']));
103 104
    $this->drupalGet('admin/structure/display-modes/form');
    $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 110 111
    $this->assertResponse(404);

    $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 121 122 123
    $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.
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 132 133

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

134 135 136 137 138 139 140 141
    // 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());

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 151 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
  /**
   * 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;
    }
  }

207
}