You are here

public function AccessResultTest::testAndIf 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::testAndIf()
  2. 9 core/tests/Drupal/Tests/Core/Access/AccessResultTest.php \Drupal\Tests\Core\Access\AccessResultTest::testAndIf()

@covers ::andIf

File

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

Class

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

Namespace

Drupal\Tests\Core\Access

Code

public function testAndIf() {
  $neutral = AccessResult::neutral('neutral message');
  $allowed = AccessResult::allowed();
  $forbidden = AccessResult::forbidden('forbidden message');
  $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
    ->andIf($allowed);
  $this
    ->assertTrue($access
    ->isAllowed());
  $this
    ->assertFalse($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertDefaultCacheability($access);

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

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

  // NEUTRAL && ALLOW == NEUTRAL
  $access = $neutral
    ->andIf($allowed);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertFalse($access
    ->isForbidden());
  $this
    ->assertTrue($access
    ->isNeutral());
  $this
    ->assertEquals('neutral message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

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

  // NEUTRAL && FORBIDDEN === FORBIDDEN.
  $access = $neutral
    ->andIf($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
    ->andif($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
    ->andif($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
    ->andif($forbidden);
  $this
    ->assertFalse($access
    ->isAllowed());
  $this
    ->assertTrue($access
    ->isForbidden());
  $this
    ->assertFalse($access
    ->isNeutral());
  $this
    ->assertEquals('forbidden message', $access
    ->getReason());
  $this
    ->assertDefaultCacheability($access);

  // FORBIDDEN && * === FORBIDDEN: lazy evaluation verification.
  $access = $forbidden
    ->andIf($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);
}