You are here

public function AccessResultTest::testOrIf in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testOrIf()
  2. 9 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testOrIf()

@covers ::orIf

File

core/tests/Drupal/Tests/Core/Access/AccessResultTest.php, line 273
Contains \Drupal\Tests\Core\Access\AccessResultTest.

Class

AccessResultTest
@coversDefaultClass \Drupal\Core\Access\AccessResult @group Access

Namespace

Drupal\Tests\Core\Access

Code

public function testOrIf() {
  $neutral = AccessResult::neutral('neutral message');
  $neutral_other = AccessResult::neutral('other neutral message');
  $neutral_reasonless = AccessResult::neutral();
  $allowed = AccessResult::allowed();
  $forbidden = AccessResult::forbidden('forbidden message');
  $forbidden_other = AccessResult::forbidden('other forbidden message');
  $forbidden_reasonless = AccessResult::forbidden();
  $unused_access_result_due_to_lazy_evaluation = $this
    ->createMock('\\Drupal\\Core\\Access\\AccessResultInterface');
  $unused_access_result_due_to_lazy_evaluation
    ->expects($this
    ->never())
    ->method($this
    ->anything());

  // ALLOWED || ALLOWED === ALLOWED.
  $access = $allowed
    ->orIf($allowed);
  $this
    ->assertTrue($access
    ->isAllowed());
  $this
    ->assertFalse($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertDefaultCacheability($access);

  // ALLOWED || NEUTRAL === ALLOWED.
  $access = $allowed
    ->orIf($neutral);
  $this
    ->assertTrue($access
    ->isAllowed());
  $this
    ->assertFalse($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertDefaultCacheability($access);

  // ALLOWED || FORBIDDEN === FORBIDDEN.
  $access = $allowed
    ->orIf($forbidden);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertTrue($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

  // NEUTRAL || NEUTRAL === NEUTRAL.
  $access = $neutral
    ->orIf($neutral);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertFalse($access
    ->isForbidden());
  $this
    ->assertTrue($access
    ->isNeutral());
  $this
    ->assertEquals('neutral message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

  // Reason inheritance edge case: first reason is kept.
  $access = $neutral
    ->orIf($neutral_other);
  $this
    ->assertEquals('neutral message', $access
    ->getReason());
  $access = $neutral_other
    ->orIf($neutral);
  $this
    ->assertEquals('other neutral message', $access
    ->getReason());

  // Reason inheritance edge case: one of the operands is reasonless.
  $access = $neutral
    ->orIf($neutral_reasonless);
  $this
    ->assertEquals('neutral message', $access
    ->getReason());
  $access = $neutral_reasonless
    ->orIf($neutral);
  $this
    ->assertEquals('neutral message', $access
    ->getReason());
  $access = $neutral_reasonless
    ->orIf($neutral_reasonless);
  $this
    ->assertEquals('', $access
    ->getReason());

  // NEUTRAL || ALLOWED === ALLOWED.
  $access = $neutral
    ->orIf($allowed);
  $this
    ->assertTrue($access
    ->isAllowed());
  $this
    ->assertFalse($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertDefaultCacheability($access);

  // NEUTRAL || FORBIDDEN === FORBIDDEN.
  $access = $neutral
    ->orIf($forbidden);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertTrue($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

  // FORBIDDEN || ALLOWED === FORBIDDEN.
  $access = $forbidden
    ->orIf($allowed);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertTrue($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

  // FORBIDDEN || NEUTRAL === FORBIDDEN.
  $access = $forbidden
    ->orIf($neutral);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertTrue($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

  // FORBIDDEN || FORBIDDEN === FORBIDDEN.
  $access = $forbidden
    ->orIf($forbidden);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertTrue($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

  // Reason inheritance edge case: first reason is kept.
  $access = $forbidden
    ->orIf($forbidden_other);
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $access = $forbidden_other
    ->orIf($forbidden);
  $this
    ->assertEquals('other forbidden message', $access
    ->getReason());

  // Reason inheritance edge case: one of the operands is reasonless.
  $access = $forbidden
    ->orIf($forbidden_reasonless);
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $access = $forbidden_reasonless
    ->orIf($forbidden);
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $access = $forbidden_reasonless
    ->orIf($forbidden_reasonless);
  $this
    ->assertEquals('', $access
    ->getReason());

  // FORBIDDEN || * === FORBIDDEN.
  $access = $forbidden
    ->orIf($unused_access_result_due_to_lazy_evaluation);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertTrue($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);
}