You are here

class QuickEditEntityFieldAccessCheckTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/quickedit/tests/src/Unit/Access/QuickEditEntityFieldAccessCheckTest.php \Drupal\Tests\quickedit\Unit\Access\QuickEditEntityFieldAccessCheckTest
  2. 9 core/modules/quickedit/tests/src/Unit/Access/QuickEditEntityFieldAccessCheckTest.php \Drupal\Tests\quickedit\Unit\Access\QuickEditEntityFieldAccessCheckTest

@coversDefaultClass \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheck @group Access @group quickedit

Hierarchy

Expanded class hierarchy of QuickEditEntityFieldAccessCheckTest

File

core/modules/quickedit/tests/src/Unit/Access/QuickEditEntityFieldAccessCheckTest.php, line 17

Namespace

Drupal\Tests\quickedit\Unit\Access
View source
class QuickEditEntityFieldAccessCheckTest extends UnitTestCase {

  /**
   * The tested access checker.
   *
   * @var \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheck
   */
  protected $editAccessCheck;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->editAccessCheck = new QuickEditEntityFieldAccessCheck();
    $cache_contexts_manager = $this
      ->prophesize(CacheContextsManager::class);
    $cache_contexts_manager
      ->assertValidTokens()
      ->willReturn(TRUE);
    $cache_contexts_manager
      ->reveal();
    $container = new Container();
    $container
      ->set('cache_contexts_manager', $cache_contexts_manager);
    \Drupal::setContainer($container);
  }

  /**
   * Provides test data for testAccess().
   *
   * @see \Drupal\Tests\edit\Unit\quickedit\Access\QuickEditEntityFieldAccessCheckTest::testAccess()
   */
  public function providerTestAccess() {
    $data = [];
    $data[] = [
      TRUE,
      TRUE,
      AccessResult::allowed(),
    ];
    $data[] = [
      FALSE,
      TRUE,
      AccessResult::neutral(),
    ];
    $data[] = [
      TRUE,
      FALSE,
      AccessResult::neutral(),
    ];
    $data[] = [
      FALSE,
      FALSE,
      AccessResult::neutral(),
    ];
    return $data;
  }

  /**
   * Tests the method for checking access to routes.
   *
   * @param bool $entity_is_editable
   *   Whether the subject entity is editable.
   * @param bool $field_storage_is_accessible
   *   Whether the user has access to the field storage entity.
   * @param \Drupal\Core\Access\AccessResult $expected_result
   *   The expected result of the access call.
   *
   * @dataProvider providerTestAccess
   */
  public function testAccess($entity_is_editable, $field_storage_is_accessible, AccessResult $expected_result) {
    $entity = $this
      ->createMockEntity();
    $entity
      ->expects($this
      ->any())
      ->method('access')
      ->willReturn(AccessResult::allowedIf($entity_is_editable)
      ->cachePerPermissions());
    $field_storage = $this
      ->createMock('Drupal\\field\\FieldStorageConfigInterface');
    $field_storage
      ->expects($this
      ->any())
      ->method('access')
      ->willReturn(AccessResult::allowedIf($field_storage_is_accessible));
    $expected_result
      ->cachePerPermissions();
    $field_name = 'valid';
    $entity_with_field = clone $entity;
    $entity_with_field
      ->expects($this
      ->any())
      ->method('get')
      ->with($field_name)
      ->will($this
      ->returnValue($field_storage));
    $entity_with_field
      ->expects($this
      ->once())
      ->method('hasTranslation')
      ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
      ->will($this
      ->returnValue(TRUE));
    $account = $this
      ->createMock('Drupal\\Core\\Session\\AccountInterface');
    $access = $this->editAccessCheck
      ->access($entity_with_field, $field_name, LanguageInterface::LANGCODE_NOT_SPECIFIED, $account);
    $this
      ->assertEquals($expected_result, $access);
  }

  /**
   * Tests checking access to routes that result in AccessResult::isForbidden().
   *
   * @dataProvider providerTestAccessForbidden
   */
  public function testAccessForbidden($field_name, $langcode) {
    $account = $this
      ->createMock('Drupal\\Core\\Session\\AccountInterface');
    $entity = $this
      ->createMockEntity();
    $this
      ->assertEquals(AccessResult::forbidden(), $this->editAccessCheck
      ->access($entity, $field_name, $langcode, $account));
  }

  /**
   * Provides test data for testAccessForbidden.
   */
  public function providerTestAccessForbidden() {
    $data = [];

    // Tests the access method without a field_name.
    $data[] = [
      NULL,
      LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ];

    // Tests the access method with a non-existent field.
    $data[] = [
      'not_valid',
      LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ];

    // Tests the access method without a langcode.
    $data[] = [
      'valid',
      NULL,
    ];

    // Tests the access method with an invalid langcode.
    $data[] = [
      'valid',
      'xx-lolspeak',
    ];
    return $data;
  }

  /**
   * Returns a mock entity.
   *
   * @return \Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected function createMockEntity() {
    $entity = $this
      ->getMockBuilder('Drupal\\entity_test\\Entity\\EntityTest')
      ->disableOriginalConstructor()
      ->getMock();
    $entity
      ->expects($this
      ->any())
      ->method('hasTranslation')
      ->willReturnMap([
      [
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
        TRUE,
      ],
      [
        'xx-lolspeak',
        FALSE,
      ],
    ]);
    $entity
      ->expects($this
      ->any())
      ->method('hasField')
      ->willReturnMap([
      [
        'valid',
        TRUE,
      ],
      [
        'not_valid',
        FALSE,
      ],
    ]);
    return $entity;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
QuickEditEntityFieldAccessCheckTest::$editAccessCheck protected property The tested access checker.
QuickEditEntityFieldAccessCheckTest::createMockEntity protected function Returns a mock entity.
QuickEditEntityFieldAccessCheckTest::providerTestAccess public function Provides test data for testAccess().
QuickEditEntityFieldAccessCheckTest::providerTestAccessForbidden public function Provides test data for testAccessForbidden.
QuickEditEntityFieldAccessCheckTest::setUp protected function Overrides UnitTestCase::setUp
QuickEditEntityFieldAccessCheckTest::testAccess public function Tests the method for checking access to routes.
QuickEditEntityFieldAccessCheckTest::testAccessForbidden public function Tests checking access to routes that result in AccessResult::isForbidden().
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function