df3134f7013fc3049df659c557323491d3de37e0
[yaffs-website] / web / core / modules / views_ui / tests / src / FunctionalJavascript / DisplayTest.php
1 <?php
2
3 namespace Drupal\Tests\views_ui\FunctionalJavascript;
4
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6 use Drupal\simpletest\NodeCreationTrait;
7 use Drupal\views\Entity\View;
8 use Drupal\views\Tests\ViewTestData;
9
10 /**
11  * Tests the display UI.
12  *
13  * @group views_ui
14  */
15 class DisplayTest extends WebDriverTestBase {
16
17   use NodeCreationTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = [
23     'block',
24     'contextual',
25     'node',
26     'views',
27     'views_ui',
28     'views_test_config',
29   ];
30
31   public static $testViews = ['test_content_ajax', 'test_display'];
32
33   /**
34    * {@inheritdoc}
35    */
36   public function setUp() {
37     parent::setUp();
38
39     ViewTestData::createTestViews(self::class, ['views_test_config']);
40
41     $admin_user = $this->drupalCreateUser([
42       'administer site configuration',
43       'administer views',
44       'administer nodes',
45       'access content overview',
46       'access contextual links',
47     ]);
48
49     // Disable automatic live preview to make the sequence of calls clearer.
50     \Drupal::configFactory()->getEditable('views.settings')->set('ui.always_live_preview', FALSE)->save();
51     $this->drupalLogin($admin_user);
52   }
53
54   /**
55    * Tests adding a display.
56    */
57   public function testAddDisplay() {
58     $this->drupalGet('admin/structure/views/view/test_content_ajax');
59     $page = $this->getSession()->getPage();
60
61     $page->find('css', '#views-display-menu-tabs .add')->click();
62
63     // Wait for the animation to complete.
64     $this->assertSession()->assertWaitOnAjaxRequest();
65
66     // Add the display.
67     $page->find('css', '#edit-displays-top-add-display-block')->click();
68
69     $element = $page->findById('views-display-menu-tabs')->findLink('Block');
70     $this->assertNotEmpty($element);
71   }
72
73   /**
74    * Tests contextual links on Views page displays.
75    */
76   public function testPageContextualLinks() {
77     $view = View::load('test_display');
78     $view->enable()->save();
79     $this->container->get('router.builder')->rebuildIfNeeded();
80
81     // Create node so the view has content and the contextual area is higher
82     // than 0 pixels.
83     $this->drupalCreateContentType(['type' => 'page']);
84     $this->createNode();
85
86     // When no "main content" block is placed, we find a contextual link
87     // placeholder for editing just the view.
88     $this->drupalGet('test-display');
89     $page = $this->getSession()->getPage();
90     $this->assertSession()->assertWaitOnAjaxRequest();
91
92     $selector = '.view-test-display';
93     $this->toggleContextualTriggerVisibility($selector);
94
95     $element = $this->getSession()->getPage()->find('css', $selector);
96     $element->find('css', '.contextual button')->press();
97
98     $contextual_container_id = 'entity.view.edit_form:view=test_display:location=page&name=test_display&display_id=page_1&langcode=en';
99     $contextual_container = $page->find('css', '[data-contextual-id="' . $contextual_container_id . '"]');
100     $this->assertNotEmpty($contextual_container);
101
102     $edit_link = $contextual_container->findLink('Edit view');
103     $this->assertNotEmpty($edit_link);
104
105     // When a "main content" is placed, we still find a contextual link
106     // placeholder for editing just the view (not the main content block).
107     // @see system_block_view_system_main_block_alter()
108     $this->drupalPlaceBlock('system_main_block', ['id' => 'main_content']);
109     $contextual_container = $page->find('css', '[data-contextual-id="' . $contextual_container_id . '"]');
110     $this->assertNotEmpty($contextual_container);
111   }
112
113   /**
114    * Toggles the visibility of a contextual trigger.
115    *
116    * @param string $selector
117    *   The selector for the element that contains the contextual Rink.
118    */
119   protected function toggleContextualTriggerVisibility($selector) {
120     // Hovering over the element itself with should be enough, but does not
121     // work. Manually remove the visually-hidden class.
122     $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').toggleClass('visually-hidden');");
123   }
124
125 }