Verified Commit 06b4b895 authored by godotislate's avatar godotislate
Browse files

feat: #3566611 It's not always possible to add a reason to neutral access results

By: prudloff
By: godotislate
parent 2d81003d
Loading
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -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);
  }

  /**
@@ -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);
  }

  /**
+25 −0
Original line number Diff line number Diff line
@@ -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.
   *
+3 −0
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ protected function setUp(): void {
      ->method('hasPermission')
      ->willReturnMap([
        ['administer foobar form display', TRUE],
        ['Llama', FALSE],
      ]);
    $this->member
      ->method('id')
@@ -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')
+1 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ protected function setUp(): void {
      ->method('hasPermission')
      ->willReturnMap([
        ['administer foobar display', TRUE],
        ['Llama', FALSE],
      ]);
    $this->member
      ->method('id')