Loading core/lib/Drupal/Core/Access/AccessResult.php +10 −4 Original line number Diff line number Diff line Loading @@ -71,13 +71,16 @@ public static function forbidden($reason = NULL) { * * @param bool $condition * The condition to evaluate. * @param string|null $reason * (optional) The reason why access is neutral. Intended for developers, * hence not translatable. * * @return \Drupal\Core\Access\AccessResult * If $condition is TRUE, isAllowed() will be TRUE, otherwise isNeutral() * will be TRUE. */ public static function allowedIf($condition) { return $condition ? static::allowed() : static::neutral(); public static function allowedIf(bool $condition, ?string $reason = NULL) { return $condition ? static::allowed() : static::neutral($reason); } /** Loading @@ -88,13 +91,16 @@ public static function allowedIf($condition) { * @param string|null $reason * (optional) The reason why access is forbidden. Intended for developers, * hence not translatable. * @param string|null $neutralReason * (optional) The reason why access is neutral. Intended for developers, * hence not translatable. * * @return \Drupal\Core\Access\AccessResult * If $condition is TRUE, isForbidden() will be TRUE, otherwise isNeutral() * will be TRUE. */ public static function forbiddenIf($condition, $reason = NULL) { return $condition ? static::forbidden($reason) : static::neutral(); public static function forbiddenIf(bool $condition, ?string $reason = NULL, ?string $neutralReason = NULL) { return $condition ? static::forbidden($reason) : static::neutral($neutralReason); } /** Loading core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +25 −0 Original line number Diff line number Diff line Loading @@ -137,6 +137,31 @@ public function testAccessForbiddenReason(): void { $verify($b, $reason); } /** * Tests access neutral reason. * * @legacy-covers ::neutral */ public function testAccessNeutralReason(): void { $verify = function (AccessResult $access, $reason): void { $this->assertInstanceOf(AccessResultReasonInterface::class, $access); $this->assertSame($reason, $access->getReason()); }; $b = AccessResult::neutral(); $verify($b, ''); $reason = $this->getRandomGenerator()->string(); $b = AccessResult::neutral($reason); $verify($b, $reason); $b = AccessResult::forbiddenIf(FALSE, '', $reason); $verify($b, $reason); $b = AccessResult::allowedIf(FALSE, $reason); $verify($b, $reason); } /** * Tests access conditionally allowed. * Loading core/tests/Drupal/Tests/Core/Entity/Access/EntityFormDisplayAccessControlHandlerTest.php +3 −0 Original line number Diff line number Diff line Loading @@ -103,6 +103,7 @@ protected function setUp(): void { ->method('hasPermission') ->willReturnMap([ ['administer foobar form display', TRUE], ['Llama', FALSE], ]); $this->member ->method('id') Loading @@ -113,6 +114,8 @@ protected function setUp(): void { ->method('hasPermission') ->willReturnMap([ ['Llama', TRUE], ['administer foobar form display', FALSE], ['administer foobar display', FALSE], ]); $this->parentMember ->method('id') Loading core/tests/Drupal/Tests/Core/Entity/Access/EntityViewDisplayAccessControlHandlerTest.php +1 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ protected function setUp(): void { ->method('hasPermission') ->willReturnMap([ ['administer foobar display', TRUE], ['Llama', FALSE], ]); $this->member ->method('id') Loading Loading
core/lib/Drupal/Core/Access/AccessResult.php +10 −4 Original line number Diff line number Diff line Loading @@ -71,13 +71,16 @@ public static function forbidden($reason = NULL) { * * @param bool $condition * The condition to evaluate. * @param string|null $reason * (optional) The reason why access is neutral. Intended for developers, * hence not translatable. * * @return \Drupal\Core\Access\AccessResult * If $condition is TRUE, isAllowed() will be TRUE, otherwise isNeutral() * will be TRUE. */ public static function allowedIf($condition) { return $condition ? static::allowed() : static::neutral(); public static function allowedIf(bool $condition, ?string $reason = NULL) { return $condition ? static::allowed() : static::neutral($reason); } /** Loading @@ -88,13 +91,16 @@ public static function allowedIf($condition) { * @param string|null $reason * (optional) The reason why access is forbidden. Intended for developers, * hence not translatable. * @param string|null $neutralReason * (optional) The reason why access is neutral. Intended for developers, * hence not translatable. * * @return \Drupal\Core\Access\AccessResult * If $condition is TRUE, isForbidden() will be TRUE, otherwise isNeutral() * will be TRUE. */ public static function forbiddenIf($condition, $reason = NULL) { return $condition ? static::forbidden($reason) : static::neutral(); public static function forbiddenIf(bool $condition, ?string $reason = NULL, ?string $neutralReason = NULL) { return $condition ? static::forbidden($reason) : static::neutral($neutralReason); } /** Loading
core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +25 −0 Original line number Diff line number Diff line Loading @@ -137,6 +137,31 @@ public function testAccessForbiddenReason(): void { $verify($b, $reason); } /** * Tests access neutral reason. * * @legacy-covers ::neutral */ public function testAccessNeutralReason(): void { $verify = function (AccessResult $access, $reason): void { $this->assertInstanceOf(AccessResultReasonInterface::class, $access); $this->assertSame($reason, $access->getReason()); }; $b = AccessResult::neutral(); $verify($b, ''); $reason = $this->getRandomGenerator()->string(); $b = AccessResult::neutral($reason); $verify($b, $reason); $b = AccessResult::forbiddenIf(FALSE, '', $reason); $verify($b, $reason); $b = AccessResult::allowedIf(FALSE, $reason); $verify($b, $reason); } /** * Tests access conditionally allowed. * Loading
core/tests/Drupal/Tests/Core/Entity/Access/EntityFormDisplayAccessControlHandlerTest.php +3 −0 Original line number Diff line number Diff line Loading @@ -103,6 +103,7 @@ protected function setUp(): void { ->method('hasPermission') ->willReturnMap([ ['administer foobar form display', TRUE], ['Llama', FALSE], ]); $this->member ->method('id') Loading @@ -113,6 +114,8 @@ protected function setUp(): void { ->method('hasPermission') ->willReturnMap([ ['Llama', TRUE], ['administer foobar form display', FALSE], ['administer foobar display', FALSE], ]); $this->parentMember ->method('id') Loading
core/tests/Drupal/Tests/Core/Entity/Access/EntityViewDisplayAccessControlHandlerTest.php +1 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ protected function setUp(): void { ->method('hasPermission') ->willReturnMap([ ['administer foobar display', TRUE], ['Llama', FALSE], ]); $this->member ->method('id') Loading