OverrideDisplaysTest.php 8.52 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views_ui\Functional;
4 5 6

/**
 * Tests that displays can be correctly overridden via the user interface.
7 8
 *
 * @group views_ui
9
 */
10 11
class OverrideDisplaysTest extends UITestBase {

12 13 14 15 16
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

17 18
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
19 20 21 22

    $this->drupalPlaceBlock('page_title_block');
  }

23 24 25
  /**
   * Tests that displays can be overridden via the UI.
   */
26
  public function testOverrideDisplays() {
27 28
    // Create a basic view that shows all content, with a page and a block
    // display.
29 30
    $view['label'] = $this->randomMachineName(16);
    $view['id'] = strtolower($this->randomMachineName(16));
31
    $view['page[create]'] = 1;
32
    $view['page[path]'] = $this->randomMachineName(16);
33 34
    $view['block[create]'] = 1;
    $view_path = $view['page[path]'];
35
    $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
36 37 38 39 40

    // Configure its title. Since the page and block both started off with the
    // same (empty) title in the views wizard, we expect the wizard to have set
    // things up so that they both inherit from the default display, and we
    // therefore only need to change that to have it take effect for both.
41
    $edit = [];
42
    $edit['title'] = $original_title = $this->randomMachineName(16);
43
    $edit['override[dropdown]'] = 'default';
44
    $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_1/title", $edit, t('Apply'));
45
    $this->drupalPostForm("admin/structure/views/view/{$view['id']}/edit/page_1", [], t('Save'));
46 47 48

    // Add a node that will appear in the view, so that the block will actually
    // be displayed.
49
    $this->drupalCreateContentType(['type' => 'page']);
50 51
    $this->drupalCreateNode();

52
    // Make sure the title appears in the page.
53
    $this->drupalGet($view_path);
54
    $this->assertSession()->statusCodeEquals(200);
55
    $this->assertText($original_title);
56

57
    // Confirm that the view block is available in the block administration UI.
58
    $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
59
    $this->clickLink('Place block');
60
    $this->assertText($view['label']);
61 62

    // Place the block.
63
    $this->drupalPlaceBlock("views_block:{$view['id']}-block_1");
64 65

    // Make sure the title appears in the block.
66 67 68
    $this->drupalGet('');
    $this->assertText($original_title);

69 70
    // Change the title for the page display only, and make sure that the
    // original title still appears on the page.
71
    $edit = [];
72
    $edit['title'] = $new_title = $this->randomMachineName(16);
73
    $edit['override[dropdown]'] = 'page_1';
74
    $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_1/title", $edit, t('Apply'));
75
    $this->drupalPostForm("admin/structure/views/view/{$view['id']}/edit/page_1", [], t('Save'));
76
    $this->drupalGet($view_path);
77
    $this->assertSession()->statusCodeEquals(200);
78 79 80 81 82 83 84
    $this->assertText($new_title);
    $this->assertText($original_title);
  }

  /**
   * Tests that the wizard correctly sets up default and overridden displays.
   */
85
  public function testWizardMixedDefaultOverriddenDisplays() {
86 87 88 89
    // Create a basic view with a page, block, and feed. Give the page and feed
    // identical titles, but give the block a different one, so we expect the
    // page and feed to inherit their titles from the default display, but the
    // block to override it.
90 91
    $view['label'] = $this->randomMachineName(16);
    $view['id'] = strtolower($this->randomMachineName(16));
92
    $view['page[create]'] = 1;
93 94
    $view['page[title]'] = $this->randomMachineName(16);
    $view['page[path]'] = $this->randomMachineName(16);
95
    $view['page[feed]'] = 1;
96
    $view['page[feed_properties][path]'] = $this->randomMachineName(16);
97
    $view['block[create]'] = 1;
98
    $view['block[title]'] = $this->randomMachineName(16);
99
    $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
100 101 102

    // Add a node that will appear in the view, so that the block will actually
    // be displayed.
103
    $this->drupalCreateContentType(['type' => 'page']);
104 105 106 107 108
    $this->drupalCreateNode();

    // Make sure that the feed, page and block all start off with the correct
    // titles.
    $this->drupalGet($view['page[path]']);
109
    $this->assertSession()->statusCodeEquals(200);
110 111 112
    $this->assertText($view['page[title]']);
    $this->assertNoText($view['block[title]']);
    $this->drupalGet($view['page[feed_properties][path]']);
113
    $this->assertSession()->statusCodeEquals(200);
114 115
    $this->assertText($view['page[title]']);
    $this->assertNoText($view['block[title]']);
116

117
    // Confirm that the block is available in the block administration UI.
118
    $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
119
    $this->clickLink('Place block');
120
    $this->assertText($view['label']);
121

122 123 124
    // Put the block into the first sidebar region, and make sure it will not
    // display on the view's page display (since we will be searching for the
    // presence/absence of the view's title in both the page and the block).
125 126 127
    $this->drupalPlaceBlock("views_block:{$view['id']}-block_1", [
      'visibility' => [
        'request_path' => [
128
          'pages' => '/' . $view['page[path]'],
129
          'negate' => TRUE,
130 131 132
        ],
      ],
    ]);
133

134 135 136 137 138 139
    $this->drupalGet('');
    $this->assertText($view['block[title]']);
    $this->assertNoText($view['page[title]']);

    // Edit the page and change the title. This should automatically change
    // the feed's title also, but not the block.
140
    $edit = [];
141
    $edit['title'] = $new_default_title = $this->randomMachineName(16);
142
    $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/page_1/title", $edit, t('Apply'));
143
    $this->drupalPostForm("admin/structure/views/view/{$view['id']}/edit/page_1", [], t('Save'));
144
    $this->drupalGet($view['page[path]']);
145
    $this->assertSession()->statusCodeEquals(200);
146 147
    $this->assertText($new_default_title);
    $this->assertNoText($view['page[title]']);
148
    $this->assertNoText($view['block[title]']);
149
    $this->drupalGet($view['page[feed_properties][path]']);
150
    $this->assertSession()->statusCodeEquals(200);
151 152 153 154 155 156
    $this->assertText($new_default_title);
    $this->assertNoText($view['page[title]']);
    $this->assertNoText($view['block[title]']);
    $this->drupalGet('');
    $this->assertNoText($new_default_title);
    $this->assertNoText($view['page[title]']);
157
    $this->assertText($view['block[title]']);
158 159 160

    // Edit the block and change the title. This should automatically change
    // the block title only, and leave the defaults alone.
161
    $edit = [];
162
    $edit['title'] = $new_block_title = $this->randomMachineName(16);
163
    $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/block_1/title", $edit, t('Apply'));
164
    $this->drupalPostForm("admin/structure/views/view/{$view['id']}/edit/block_1", [], t('Save'));
165
    $this->drupalGet($view['page[path]']);
166
    $this->assertSession()->statusCodeEquals(200);
167 168
    $this->assertText($new_default_title);
    $this->drupalGet($view['page[feed_properties][path]']);
169
    $this->assertSession()->statusCodeEquals(200);
170 171 172 173 174 175 176 177 178 179
    $this->assertText($new_default_title);
    $this->assertNoText($new_block_title);
    $this->drupalGet('');
    $this->assertText($new_block_title);
    $this->assertNoText($view['block[title]']);
  }

  /**
   * Tests that the revert to all displays select-option works as expected.
   */
180
  public function testRevertAllDisplays() {
181 182
    // Create a basic view with a page, block.
    // Because there is both a title on page and block we expect the title on
183
    // the block be overridden.
184 185
    $view['label'] = $this->randomMachineName(16);
    $view['id'] = strtolower($this->randomMachineName(16));
186
    $view['page[create]'] = 1;
187 188
    $view['page[title]'] = $this->randomMachineName(16);
    $view['page[path]'] = $this->randomMachineName(16);
189
    $view['block[create]'] = 1;
190
    $view['block[title]'] = $this->randomMachineName(16);
191
    $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
192

193 194
    // Revert the title of the block to the default ones, but submit some new
    // values to be sure that the new value is not stored.
195
    $edit = [];
196
    $edit['title'] = $this->randomMachineName();
197 198
    $edit['override[dropdown]'] = 'default_revert';

199
    $this->drupalPostForm("admin/structure/views/nojs/display/{$view['id']}/block_1/title", $edit, t('Apply'));
200
    $this->drupalPostForm("admin/structure/views/view/{$view['id']}/edit/block_1", [], t('Save'));
201 202 203
    $this->assertText($view['page[title]']);
  }

204
}