diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php index c0a427eec1731a1ccb8a46f161cbd4d8e3e81684..9f651bdc7b8150113b5d07f68352e2683262f6d5 100644 --- a/core/.phpstan-baseline.php +++ b/core/.phpstan-baseline.php @@ -1643,18 +1643,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, diff --git a/core/modules/user/src/Plugin/views/filter/Roles.php b/core/modules/user/src/Plugin/views/filter/Roles.php index b8576621914efa2bdb6a5ce2c0fa4e2ef10a00e7..8accb75d54a2dd52404276ba0e570277ed7b3d39 100644 --- a/core/modules/user/src/Plugin/views/filter/Roles.php +++ b/core/modules/user/src/Plugin/views/filter/Roles.php @@ -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; diff --git a/core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php b/core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php index a7ab12de744a5b1d69135d7a6035094e03596e2a..caf2001b6cee76ea0ad30c90d2846df63f348a1f 100644 --- a/core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php +++ b/core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php @@ -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(); }