From 0a38107ea08fdfa802eace3528b274edd5e4725d Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sun, 18 Jan 2015 10:39:36 +0000
Subject: [PATCH] Issue #2407107 by jan.stoeckler:
 Drupal\user\Plugin\views\filter\Roles should implement
 calculateDependencies()

---
 .../user/src/Plugin/views/filter/Roles.php    | 50 +++++++++++++
 .../Tests/Views/HandlerFilterRolesTest.php    | 72 +++++++++++++++++++
 2 files changed, 122 insertions(+)
 create mode 100644 core/modules/user/src/Tests/Views/HandlerFilterRolesTest.php

diff --git a/core/modules/user/src/Plugin/views/filter/Roles.php b/core/modules/user/src/Plugin/views/filter/Roles.php
index 014425df520a..f878785a3af3 100644
--- a/core/modules/user/src/Plugin/views/filter/Roles.php
+++ b/core/modules/user/src/Plugin/views/filter/Roles.php
@@ -7,7 +7,9 @@
 
 namespace Drupal\user\Plugin\views\filter;
 
+use Drupal\user\RoleStorageInterface;
 use Drupal\views\Plugin\views\filter\ManyToOne;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Filter handler for user roles.
@@ -18,6 +20,42 @@
  */
 class Roles extends ManyToOne {
 
+  /**
+   * The role storage.
+   *
+   * @var \Drupal\user\RoleStorageInterface
+   */
+  protected $roleStorage;
+
+  /**
+   * Constructs a Roles object.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin_id for the plugin instance.
+   * @param mixed $plugin_definition
+   *   The plugin implementation definition.
+   * @param \Drupal\user\RoleStorageInterface $role_storage
+   *   The role storage.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, RoleStorageInterface $role_storage) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->roleStorage = $role_storage;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static(
+      $configuration,
+      $plugin_id,
+      $plugin_definition,
+      $container->get('entity.manager')->getStorage('user_role')
+    );
+  }
+
   public function getValueOptions() {
     $this->valueOptions = user_role_names(TRUE);
     unset($this->valueOptions[DRUPAL_AUTHENTICATED_RID]);
@@ -33,4 +71,16 @@ function operators() {
     return $operators;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function calculateDependencies() {
+    $dependencies = array();
+    foreach ($this->value as $role_id) {
+      $role = $this->roleStorage->load($role_id);
+      $dependencies[$role->getConfigDependencyKey()][] = $role->getConfigDependencyName();
+    }
+    return $dependencies;
+  }
+
 }
diff --git a/core/modules/user/src/Tests/Views/HandlerFilterRolesTest.php b/core/modules/user/src/Tests/Views/HandlerFilterRolesTest.php
new file mode 100644
index 000000000000..6ed889baf628
--- /dev/null
+++ b/core/modules/user/src/Tests/Views/HandlerFilterRolesTest.php
@@ -0,0 +1,72 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\user\Tests\Views\HandlerFilterRolesTest.
+ */
+
+namespace Drupal\user\Tests\Views;
+
+use Drupal\Component\Utility\String;
+use Drupal\user\Entity\Role;
+use Drupal\user\Tests\Views\UserUnitTestBase;
+use Drupal\views\Entity\View;
+use Drupal\views\Views;
+
+/**
+ * Tests the roles filter handler.
+ *
+ * @group user
+ *
+ * @see \Drupal\user\Plugin\views\filter\Roles
+ */
+class HandlerFilterRolesTest extends UserUnitTestBase {
+
+  /**
+   * Views used by this test.
+   *
+   * @var array
+   */
+  public static $testViews = array('test_user_name');
+
+  /**
+   * Tests that role filter dependencies are calculated correctly.
+   */
+  public function testDependencies() {
+    $role = Role::create(['id' => 'test_user_role']);
+    $role->save();
+    $view = View::load('test_user_name');
+    $expected = [
+      'module' => ['user'],
+    ];
+    $this->assertEqual($expected, $view->getDependencies());
+
+    $display = &$view->getDisplay('default');
+    $display['display_options']['filters']['roles_target_id'] = [
+      'id' => 'roles_target_id',
+      'table' => 'user__roles',
+      'field' => 'roles_target_id',
+      'value' => [
+        'test_user_role' => 'test_user_role',
+      ],
+      'plugin_id' => 'user_roles',
+    ];
+    $view->save();
+    $expected['config'][] = 'user.role.test_user_role';
+    $this->assertEqual($expected, $view->getDependencies());
+
+    $view = View::load('test_user_name');
+    $display = &$view->getDisplay('default');
+    $display['display_options']['filters']['roles_target_id'] = [
+      'id' => 'roles_target_id',
+      'table' => 'user__roles',
+      'field' => 'roles_target_id',
+      'value' => [],
+      'plugin_id' => 'user_roles',
+    ];
+    $view->save();
+    unset($expected['config']);
+    $this->assertEqual($expected, $view->getDependencies());
+  }
+
+}
-- 
GitLab