Verified Commit f7ddabe7 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3427171 by samit.310@gmail.com, mondrake, longwave, smustgrave, Spokje,...

Issue #3427171 by samit.310@gmail.com, mondrake, longwave, smustgrave, Spokje, catch, quietone: Replace calls to ::expectWarning*() in Drupal\Tests\user\Kernel\Views\HandlerFilterRolesTest

(cherry picked from commit d7086912)
parent 28bd49e5
Loading
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -1898,18 +1898,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/user/tests/src/Functional/UserRegistrationRestTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Call to deprecated method expectWarning\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
	'count' => 1,
	'path' => __DIR__ . '/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Call to deprecated method expectWarningMessage\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\:
https\\://github\\.com/sebastianbergmann/phpunit/issues/5062$#',
	'count' => 1,
	'path' => __DIR__ . '/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Variable \\$result in isset\\(\\) always exists and is not nullable\\.$#',
	'count' => 1,
+21 −12
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
use Drupal\user\RoleStorageInterface;
use Drupal\views\Attribute\ViewsFilter;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -16,13 +17,6 @@
#[ViewsFilter("user_roles")]
class Roles extends ManyToOne {

  /**
   * The role storage.
   *
   * @var \Drupal\user\RoleStorageInterface
   */
  protected $roleStorage;

  /**
   * Constructs a Roles object.
   *
@@ -32,12 +26,23 @@ class Roles extends ManyToOne {
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\user\RoleStorageInterface $role_storage
   * @param \Drupal\user\RoleStorageInterface $roleStorage
   *   The role storage.
   * @param \Psr\Log\LoggerInterface|null $logger
   *   The logger service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RoleStorageInterface $role_storage) {
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    protected readonly RoleStorageInterface $roleStorage,
    protected ?LoggerInterface $logger,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->roleStorage = $role_storage;
    if (!$logger) {
      @trigger_error('Calling ' . __METHOD__ . '() without the $logger argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3427368', E_USER_DEPRECATED);
      $this->logger = \Drupal::service('logger.channel.default');
    }
  }

  /**
@@ -48,7 +53,8 @@ public static function create(ContainerInterface $container, array $configuratio
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager')->getStorage('user_role')
      $container->get('entity_type.manager')->getStorage('user_role'),
      $container->get('logger.channel.default'),
    );
  }

@@ -101,7 +107,10 @@ public function calculateDependencies() {
        $dependencies[$role->getConfigDependencyKey()][] = $role->getConfigDependencyName();
      }
      else {
        trigger_error("The {$role_id} role does not exist. You should review and fix the configuration of the {$this->view->id()} view.", E_USER_WARNING);
        $this->logger->warning("View %view depends on role %role, but the role does not exist.", [
          '%view' => $this->view->id(),
          '%role' => $role_id,
        ]);
      }
    }
    return $dependencies;
+18 −2
Original line number Diff line number Diff line
@@ -4,9 +4,12 @@

namespace Drupal\Tests\user\Kernel\Views;

use Drupal\Core\Logger\RfcLogLevel;
use Drupal\user\Entity\Role;
use Drupal\views\Entity\View;
use Drupal\views\Views;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;

/**
 * Tests the roles filter handler.
@@ -101,6 +104,11 @@ public function testDependencies() {
   * Tests that a warning is triggered if the filter references a missing role.
   */
  public function testMissingRole() {
    $logger = $this->prophesize(LoggerInterface::class);
    $this->container->get('logger.factory')
      ->get('system')
      ->addLogger($logger->reveal());

    $role = Role::create(['id' => 'test_user_role', 'label' => 'Test user role']);
    $role->save();
    /** @var \Drupal\views\Entity\View $view */
@@ -116,8 +124,16 @@ public function testMissingRole() {
    // Ensure no warning is triggered before the role is deleted.
    $view->calculateDependencies();
    $role->delete();
    $this->expectWarning();
    $this->expectWarningMessage('The test_user_role role does not exist. You should review and fix the configuration of the test_user_name view.');

    // Recalculate after role deletion.
    $logger->log(
      RfcLogLevel::WARNING,
      'View %view depends on role %role, but the role does not exist.',
      Argument::allOf(
        Argument::withEntry('%view', 'test_user_name'),
        Argument::withEntry('%role', 'test_user_role'),
      )
    )->shouldBeCalled();
    $view->calculateDependencies();
  }