Commit 9f8f38d8 authored by catch's avatar catch
Browse files

Issue #2681947 by vasi1186, dan2k3k4, Leksat, Lendude, raman.b, yogeshmpawar,...

Issue #2681947 by vasi1186, dan2k3k4, Leksat, Lendude, raman.b, yogeshmpawar, quietone, keszthelyi, Abhijith S, pameeela, claudiu.cristea, alexpott, NickDickinsonWilde: Allow 'Put the exposed form in a block' setting to be used with views block displays
parent 257efe54
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -369,13 +369,10 @@ public function preBlockBuild(ViewsBlock $block) {
  }

  /**
   * Block views use exposed widgets only if AJAX is set.
   * {@inheritdoc}
   */
  public function usesExposed() {
    if ($this->ajaxEnabled()) {
      return parent::usesExposed();
    }
    return FALSE;
  public function usesExposedFormInBlock() {
    return TRUE;
  }

  /**
+17 −0
Original line number Diff line number Diff line
@@ -58,3 +58,20 @@ display:
    display_title: Page
    id: page_1
    position: 0
  block_1:
    display_plugin: block
    id: block_1
    display_title: Block
    position: 2
    display_options:
      display_extenders: {  }
      exposed_block: true
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }
+57 −7
Original line number Diff line number Diff line
@@ -38,16 +38,26 @@ class ExposedFormTest extends ViewTestBase {
   */
  protected $defaultTheme = 'classy';

  /**
   * Nodes to test.
   *
   * @var \Drupal\node\NodeInterface[]
   */
  protected $nodes = [];

  protected function setUp($import_test_views = TRUE): void {
    parent::setUp($import_test_views);

    $this->enableViewsTestModule();

    $this->drupalCreateContentType(['type' => 'article']);
    $this->drupalCreateContentType(['type' => 'page']);

    $this->nodes = [];
    // Create some random nodes.
    for ($i = 0; $i < 5; $i++) {
      $this->drupalCreateNode(['type' => 'article']);
      $this->nodes[] = $this->drupalCreateNode(['type' => 'article']);
      $this->nodes[] = $this->drupalCreateNode(['type' => 'page']);
    }
  }

@@ -198,12 +208,13 @@ public function testResetButton() {

  /**
   * Tests the exposed block functionality.
   *
   * @dataProvider providerTestExposedBlock
   */
  public function testExposedBlock() {
    $this->drupalCreateContentType(['type' => 'page']);
  public function testExposedBlock($display) {
    $view = Views::getView('test_exposed_block');
    $view->setDisplay('page_1');
    $block = $this->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-page_1');
    $view->setDisplay($display);
    $block = $this->drupalPlaceBlock('views_exposed_filter_block:test_exposed_block-' . $display);

    // Set label to display on the exposed filter form block.
    $block->getPlugin()->setConfigurationValue('label_display', TRUE);
@@ -248,11 +259,33 @@ public function testExposedBlock() {
    // Test that the correct option is selected after form submission.
    $this->assertCacheContext('url');
    $this->assertTrue($this->assertSession()->optionExists('Content: Type', 'All')->isSelected());
    foreach (['All', 'article', 'page'] as $argument) {
      $this->drupalGet('test_exposed_block', ['query' => ['type' => $argument]]);
    $arguments = [
      'All' => ['article', 'page'],
      'article' => ['article'],
      'page' => ['page'],
    ];
    foreach ($arguments as $argument => $bundles) {
      $elements[0]->find('css', 'select')->selectOption($argument);
      $elements[0]->findButton('Apply')->click();
      $this->assertCacheContext('url');
      $this->assertTrue($this->assertSession()->optionExists('Content: Type', $argument)->isSelected());
      $this->assertNodesExist($bundles);
    }
    $elements[0]->findButton('Reset')->click();
    $this->assertNodesExist($arguments['All']);
  }

  /**
   * Data provider for testing different types of displays.
   *
   * @return array
   *   Array of display names to test.
   */
  public function providerTestExposedBlock() {
    return [
      'page_display' => ['page_1'],
      'block_display' => ['block_1'],
    ];
  }

  /**
@@ -435,4 +468,21 @@ public function testExposedFilterPagination() {
    $this->assertSession()->fieldValueEquals('created[max]', '+1 month');
  }

  /**
   * Asserts that nodes of only given bundles exist.
   *
   * @param array $bundles
   *   Bundles of nodes.
   */
  protected function assertNodesExist(array $bundles) {
    foreach ($this->nodes as $node) {
      if (in_array($node->bundle(), $bundles)) {
        $this->assertSession()->pageTextContains($node->label());
      }
      else {
        $this->assertSession()->pageTextNotContains($node->label());
      }
    }
  }

}