DisplayPageWebTest.php 6.18 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Functional\Plugin;
4

5
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
6
use Drupal\Tests\views\Functional\ViewTestBase;
7 8
use Drupal\views\Views;

9 10
/**
 * Tests the views page display plugin as webtest.
11 12
 *
 * @group views
13
 */
14
class DisplayPageWebTest extends ViewTestBase {
15

16 17
  use AssertPageCacheContextsAndTagsTrait;

18 19 20 21 22
  /**
   * Views used by this test.
   *
   * @var array
   */
23
  public static $testViews = ['test_page_display', 'test_page_display_arguments', 'test_page_display_menu', 'test_page_display_path'];
24

25 26 27 28 29
  /**
   * Modules to enable.
   *
   * @var array
   */
30
  protected static $modules = ['menu_ui', 'block', 'views_ui'];
31

32 33 34 35 36
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

37 38 39
  /**
   * {@inheritdoc}
   */
40
  protected function setUp($import_test_views = TRUE): void {
41
    parent::setUp($import_test_views);
42 43

    $this->enableViewsTestModule();
44
    $this->drupalPlaceBlock('local_tasks_block');
45 46 47 48 49 50 51
  }

  /**
   * Tests arguments.
   */
  public function testArguments() {
    $this->drupalGet('test_route_without_arguments');
52
    $this->assertSession()->statusCodeEquals(200);
53
    $result = $this->xpath('//span[@class="field-content"]');
54
    $this->assertCount(5, $result, 'All entries was returned');
55 56

    $this->drupalGet('test_route_without_arguments/1');
57
    $this->assertSession()->statusCodeEquals(404);
58 59

    $this->drupalGet('test_route_with_argument/1');
60
    $this->assertSession()->statusCodeEquals(200);
61
    $this->assertCacheContexts(['languages:language_interface', 'route', 'theme', 'url']);
62
    $result = $this->xpath('//span[@class="field-content"]');
63
    $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.');
64
    $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
65 66

    $this->drupalGet('test_route_with_suffix/1/suffix');
67
    $this->assertSession()->statusCodeEquals(200);
68
    $result = $this->xpath('//span[@class="field-content"]');
69
    $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.');
70
    $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
71 72

    $this->drupalGet('test_route_with_suffix_and_argument/1/suffix/2');
73
    $this->assertSession()->statusCodeEquals(200);
74
    $result = $this->xpath('//span[@class="field-content"]');
75
    $this->assertCount(0, $result, 'No result was returned.');
76 77

    $this->drupalGet('test_route_with_suffix_and_argument/1/suffix/1');
78
    $this->assertSession()->statusCodeEquals(200);
79
    $result = $this->xpath('//span[@class="field-content"]');
80
    $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.');
81
    $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
82 83

    $this->drupalGet('test_route_with_long_argument/1');
84
    $this->assertSession()->statusCodeEquals(200);
85
    $result = $this->xpath('//span[@class="field-content"]');
86
    $this->assertCount(1, $result, 'Ensure that just the filtered entry was returned.');
87
    $this->assertEqual($result[0]->getText(), 1, 'The passed ID was returned.');
88 89
  }

90 91 92 93
  /**
   * Tests menu settings of page displays.
   */
  public function testPageDisplayMenu() {
94
    // Check local tasks.
95
    $this->drupalGet('test_page_display_menu');
96
    $this->assertSession()->statusCodeEquals(200);
97
    $element = $this->xpath('//ul[contains(@class, :ul_class)]//a[contains(@class, :a_class)]/child::text()', [
98
      ':ul_class' => 'tabs primary',
99
      ':a_class' => 'is-active',
100
    ]);
101
    $this->assertEqual($element[0]->getText(), t('Test default tab'));
102
    $this->assertTitle('Test default page | Drupal');
103 104

    $this->drupalGet('test_page_display_menu/default');
105
    $this->assertSession()->statusCodeEquals(404);
106 107

    $this->drupalGet('test_page_display_menu/local');
108
    $this->assertSession()->statusCodeEquals(200);
109
    $element = $this->xpath('//ul[contains(@class, :ul_class)]//a[contains(@class, :a_class)]/child::text()', [
110
      ':ul_class' => 'tabs primary',
111
      ':a_class' => 'is-active',
112
    ]);
113
    $this->assertEqual($element[0]->getText(), t('Test local tab'));
114
    $this->assertTitle('Test local page | Drupal');
115 116 117 118 119 120 121

    // Check an ordinary menu link.
    $admin_user = $this->drupalCreateUser(['administer menu']);
    $this->drupalLogin($admin_user);
    $this->drupalPlaceBlock('system_menu_block:tools');
    $this->drupalGet('<front>');

122
    $menu_link = $this->cssSelect('nav.block-menu ul.menu a');
123
    $this->assertEqual($menu_link[0]->getText(), 'Test menu link');
124 125 126 127 128 129 130

    // Update the menu link.
    $this->drupalPostForm("admin/structure/menu/link/views_view:views.test_page_display_menu.page_3/edit", [
      'title' => 'New title',
    ], t('Save'));

    $this->drupalGet('<front>');
131
    $menu_link = $this->cssSelect('nav.block-menu ul.menu a');
132
    $this->assertEqual($menu_link[0]->getText(), 'New title');
133 134
  }

135 136 137 138 139 140 141 142
  /**
   * Tests the title is not displayed in the output.
   */
  public function testTitleOutput() {
    $this->drupalGet('test_page_display_200');

    $view = Views::getView('test_page_display');
    $xpath = $this->cssSelect('div.view:contains("' . $view->getTitle() . '")');
143
    $this->assertEmpty($xpath, 'The view title was not displayed in the view markup.');
144 145
  }

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  /**
   * Tests the views page path functionality.
   */
  public function testPagePaths() {
    $this->drupalLogin($this->rootUser);
    $this->assertPagePath('0');
    $this->assertPagePath('9999');
  }

  /**
   * Tests that we can successfully change a view page display path.
   *
   * @param string $path
   *   Path that will be set as the view page display path.
   *
alexpott's avatar
alexpott committed
161
   * @return bool
162 163 164 165 166 167 168 169 170 171
   *   Assertion result.
   */
  public function assertPagePath($path) {
    $view = Views::getView('test_page_display_path');
    $view->initDisplay('page_1');
    $view->displayHandlers->get('page_1')->overrideOption('path', $path);
    $view->save();
    $this->container->get('router.builder')->rebuild();
    // Check if we successfully changed the path.
    $this->drupalGet($path);
172
    $success = $this->assertSession()->statusCodeEquals(200);
173 174
    // Check if we don't get any error on the view edit page.
    $this->drupalGet('admin/structure/views/view/test_page_display_path');
175
    return $success && $this->assertSession()->statusCodeEquals(200);
176 177
  }

178
}