You are here

public function AreaDisplayLinkTest::testAreaDisplayLink in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/AreaDisplayLinkTest.php \Drupal\Tests\views\Kernel\Handler\AreaDisplayLinkTest::testAreaDisplayLink()
  2. 10 core/modules/views/tests/src/Kernel/Handler/AreaDisplayLinkTest.php \Drupal\Tests\views\Kernel\Handler\AreaDisplayLinkTest::testAreaDisplayLink()

Tests the views area display_link handler.

File

core/modules/views/tests/src/Kernel/Handler/AreaDisplayLinkTest.php, line 129

Class

AreaDisplayLinkTest
Tests the core views_handler_area_display_link handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testAreaDisplayLink() {
  $view = Views::getView('test_view');

  // Assert only path-based displays are available in the display link
  // settings form.
  $view
    ->setDisplay('page_1');
  $this
    ->assertFormOptions($view, 'display_link_1');
  $this
    ->assertFormOptions($view, 'display_link_2');
  $view
    ->setDisplay('page_2');
  $this
    ->assertFormOptions($view, 'display_link_1');
  $this
    ->assertFormOptions($view, 'display_link_2');
  $view
    ->setDisplay('block_1');
  $this
    ->assertFormOptions($view, 'display_link_1');
  $this
    ->assertFormOptions($view, 'display_link_2');

  // Assert the links are rendered correctly for all displays.
  $this
    ->assertRenderedDisplayLinks($view, 'page_1');
  $this
    ->assertRenderedDisplayLinks($view, 'page_2');
  $this
    ->assertRenderedDisplayLinks($view, 'block_1');

  // Assert some special request parameters are filtered from the display
  // links.
  $request_stack = new RequestStack();
  $request_stack
    ->push(Request::create('page_1', 'GET', [
    'name' => 'John',
    'sort_by' => 'created',
    'sort_order' => 'ASC',
    'page' => 1,
    'keep' => 'keep',
    'keep_another' => 1,
    'view_name' => 1,
    'view_display_id' => 1,
    'view_args' => 1,
    'view_path' => 1,
    'view_dom_id' => 1,
    'pager_element' => 1,
    'view_base_path' => 1,
    AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER => 1,
    FormBuilderInterface::AJAX_FORM_REQUEST => 1,
    MainContentViewSubscriber::WRAPPER_FORMAT => 1,
  ]));
  $this->container
    ->set('request_stack', $request_stack);
  $view
    ->destroy();
  $view
    ->setDisplay('page_1');
  $view
    ->setCurrentPage(2);
  $this
    ->executeView($view, [
    1,
  ]);
  $this
    ->assertSame('<a href="/page_1/1?name=John&amp;sort_by=created&amp;sort_order=ASC&amp;keep=keep&amp;keep_another=1&amp;page=1" class="views-display-link views-display-link-page_1 is-active">Page 1</a>', $this
    ->renderDisplayLink($view, 'display_link_1'));
  $this
    ->assertSame('<a href="/page_2/1?name=John&amp;sort_by=created&amp;sort_order=ASC&amp;keep=keep&amp;keep_another=1&amp;page=1" class="views-display-link views-display-link-page_2">Page 2</a>', $this
    ->renderDisplayLink($view, 'display_link_2'));

  // Assert the validation adds warning messages when a display link is added
  // to a display with different filter criteria, sort criteria, pager
  // settings or contextual filters. Since all options are added to the
  // default display there currently should be no warning messages.
  $this
    ->assertNoWarningMessages($view);

  // Assert the message are shown when changing the filter criteria of page_1.
  $filters = [
    'name' => [
      'id' => 'name',
      'table' => 'views_test_data',
      'field' => 'name',
      'relationship' => 'none',
      'operator' => '=',
      'value' => '',
      'exposed' => TRUE,
      'expose' => [
        'identifier' => 'name',
        'label' => 'Name',
      ],
    ],
  ];
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('filters', $filters);
  $this
    ->assertWarningMessages($view, [
    'filters',
  ]);

  // Assert no messages are added after the default display is changed with
  // the same options.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', $filters);
  $this
    ->assertNoWarningMessages($view);

  // Assert the message are shown when changing the sort criteria of page_1.
  $sorts = [
    'created' => [
      'id' => 'created',
      'table' => 'views_test_data',
      'field' => 'created',
      'relationship' => 'none',
      'order' => 'DESC',
      'exposed' => TRUE,
    ],
  ];
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('sorts', $sorts);
  $this
    ->assertWarningMessages($view, [
    'sorts',
  ]);

  // Assert no messages are added after the default display is changed with
  // the same options.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('sorts', $sorts);
  $this
    ->assertNoWarningMessages($view);

  // Assert the message are shown when changing the sort criteria of page_1.
  $pager = [
    'type' => 'full',
    'options' => [
      'items_per_page' => 10,
    ],
  ];
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('pager', $pager);
  $this
    ->assertWarningMessages($view, [
    'pager',
  ]);

  // Assert no messages are added after the default display is changed with
  // the same options.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('pager', $pager);
  $this
    ->assertNoWarningMessages($view);

  // Assert the message are shown when changing the contextual filters of
  // page_1.
  $arguments = [
    'id' => [
      'id' => 'id',
      'table' => 'views_test_data',
      'field' => 'id',
      'relationship' => 'none',
    ],
  ];
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('arguments', $arguments);
  $this
    ->assertWarningMessages($view, [
    'arguments',
  ]);

  // Assert no messages are added after the default display is changed with
  // the same options.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('arguments', $arguments);
  $this
    ->assertNoWarningMessages($view);

  // Assert an error is shown when the display ID is not set.
  $display_link = [
    'display_link_3' => [
      'id' => 'display_link_3',
      'table' => 'views',
      'field' => 'display_link',
      'display_id' => '',
      'label' => 'Empty',
      'plugin_id' => 'display_link',
    ],
  ];
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('header', $display_link);
  $view
    ->destroy();
  $view
    ->setDisplay('page_1');
  $errors = $view
    ->validate();
  $this
    ->assertCount(1, $errors);
  $this
    ->assertCount(1, $errors['page_1']);
  $this
    ->assertSame('<em class="placeholder">Page 1</em>: The link in the <em class="placeholder">header</em> area has no configured display.', $errors['page_1'][0]
    ->__toString());

  // Assert an error is shown when linking to a display ID that doesn't exist.
  $display_link['display_link_3']['display_id'] = 'non-existent';
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('header', $display_link);
  $view
    ->destroy();
  $view
    ->setDisplay('page_1');
  $errors = $view
    ->validate();
  $this
    ->assertCount(1, $errors);
  $this
    ->assertCount(1, $errors['page_1']);
  $this
    ->assertSame('<em class="placeholder">Page 1</em>: The link in the <em class="placeholder">header</em> area points to the <em class="placeholder">non-existent</em> display which no longer exists.', $errors['page_1'][0]
    ->__toString());

  // Assert an error is shown when linking to a display without a path.
  $display_link['display_link_3']['display_id'] = 'block_1';
  $view->displayHandlers
    ->get('page_1')
    ->overrideOption('header', $display_link);
  $view
    ->destroy();
  $view
    ->setDisplay('page_1');
  $errors = $view
    ->validate();
  $this
    ->assertCount(1, $errors);
  $this
    ->assertCount(1, $errors['page_1']);
  $this
    ->assertSame('<em class="placeholder">Page 1</em>: The link in the <em class="placeholder">header</em> area points to the <em class="placeholder">Block 1</em> display which does not have a path.', $errors['page_1'][0]
    ->__toString());
}