From f97d2fa5a3d7a132341fa355f6ee018760b13229 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Wed, 9 Apr 2014 20:17:40 -0700
Subject: [PATCH] Issue #2236851 by alexpott | Berdir: Views blocks are missing
 dependency on view.

---
 .../views/Plugin/Derivative/ViewsBlock.php    |   5 +
 .../Derivative/ViewsExposedFilterBlock.php    |   5 +
 .../Tests/Plugin/BlockDependenciesTest.php    | 123 ++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 core/modules/views/lib/Drupal/views/Tests/Plugin/BlockDependenciesTest.php

diff --git a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php
index c1d753befbdc..be44dca3e27a 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsBlock.php
@@ -102,6 +102,11 @@ public function getDerivativeDefinitions($base_plugin_definition) {
           $this->derivatives[$delta] = array(
             'category' => $display->getOption('block_category'),
             'admin_label' => $desc,
+            'config_dependencies' => array(
+              'entity' => array(
+                $view->getConfigDependencyName(),
+              )
+            )
           );
           $this->derivatives[$delta] += $base_plugin_definition;
         }
diff --git a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsExposedFilterBlock.php b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsExposedFilterBlock.php
index f077d8ee3e9e..2dd5f69ae047 100644
--- a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsExposedFilterBlock.php
+++ b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsExposedFilterBlock.php
@@ -93,6 +93,11 @@ public function getDerivativeDefinitions($base_plugin_definition) {
             $desc = t('Exposed form: @view-@display_id', array('@view' => $view->id(), '@display_id' => $display->display['id']));
             $this->derivatives[$delta] = array(
               'admin_label' => $desc,
+              'config_dependencies' => array(
+                'entity' => array(
+                  $view->getConfigDependencyName(),
+                )
+              )
             );
             $this->derivatives[$delta] += $base_plugin_definition;
           }
diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/BlockDependenciesTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/BlockDependenciesTest.php
new file mode 100644
index 000000000000..42db1e08e5d0
--- /dev/null
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/BlockDependenciesTest.php
@@ -0,0 +1,123 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\views\Tests\Plugin\BlockDependenciesTest.
+ */
+
+namespace Drupal\views\Tests\Plugin;
+
+use Drupal\views\Tests\ViewUnitTestBase;
+
+/**
+ * Tests exposed views derived blocks have the correct config dependencies.
+ */
+class BlockDependenciesTest extends ViewUnitTestBase {
+
+  /**
+   * Views used by this test.
+   *
+   * @var array
+   */
+  public static $testViews = array('test_exposed_block');
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array('node', 'block');
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Views block config dependencies',
+      'description' => 'Test views block config dependencies functionality.',
+      'group' => 'Views Plugins',
+    );
+  }
+
+  /**
+   * Tests that exposed filter blocks have the correct dependencies.
+   *
+   * @see \Drupal\views\Plugin\Derivative\ViewsExposedFilterBlock::getDerivativeDefinitions()
+   */
+  public function testExposedBlock() {
+    $block = $this->createBlock('views_exposed_filter_block:test_exposed_block-page_1');
+    $dependencies = $block->calculateDependencies();
+    $expected = array(
+      'entity' => array('views.view.test_exposed_block'),
+      'module' => array('views'),
+      'theme' => array('stark')
+    );
+    $this->assertIdentical($expected, $dependencies);
+  }
+
+  /**
+   * Tests that exposed filter blocks have the correct dependencies.
+   *
+   * @see \Drupal\views\Plugin\Derivative\ViewsBlock::getDerivativeDefinitions()
+   */
+  public function testViewsBlock() {
+    $block = $this->createBlock('views_block:content_recent-block_1');
+    $dependencies = $block->calculateDependencies();
+    $expected = array(
+      'entity' => array('views.view.content_recent'),
+      'module' => array('views'),
+      'theme' => array('stark')
+    );
+    $this->assertIdentical($expected, $dependencies);
+  }
+
+  /**
+   * Creates a block instance based on default settings.
+   *
+   * @param string $plugin_id
+   *   The plugin ID of the block type for this block instance.
+   * @param array $settings
+   *   (optional) An associative array of settings for the block entity.
+   *   Override the defaults by specifying the key and value in the array, for
+   *   example:
+   *   @code
+   *     $this->createBlock('system_powered_by_block', array(
+   *       'label' => t('Hello, world!'),
+   *     ));
+   *   @endcode
+   *   The following defaults are provided:
+   *   - label: Random string.
+   *   - id: Random string.
+   *   - region: 'sidebar_first'.
+   *   - theme: The default theme.
+   *   - visibility: Empty array.
+   *   - cache: array('max_age' => 0).
+   *
+   * @return \Drupal\block\Entity\Block
+   *   The block entity.
+   */
+  protected function createBlock($plugin_id, array $settings = array()) {
+    $settings += array(
+      'plugin' => $plugin_id,
+      'region' => 'sidebar_first',
+      'id' => strtolower($this->randomName(8)),
+      'theme' => \Drupal::config('system.theme')->get('default'),
+      'label' => $this->randomName(8),
+      'visibility' => array(),
+      'weight' => 0,
+      'cache' => array(
+        'max_age' => 0,
+      ),
+    );
+    foreach (array('region', 'id', 'theme', 'plugin', 'visibility', 'weight') as $key) {
+      $values[$key] = $settings[$key];
+      // Remove extra values that do not belong in the settings array.
+      unset($settings[$key]);
+    }
+    $values['settings'] = $settings;
+    $block = entity_create('block', $values);
+    $block->save();
+    return $block;
+  }
+
+}
-- 
GitLab