class EntityPermissionsFormTest

Same name and namespace in other branches
  1. 11.x core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php \Drupal\Tests\user\Unit\Form\EntityPermissionsFormTest
  2. 10 core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php \Drupal\Tests\user\Unit\Form\EntityPermissionsFormTest
  3. 9 core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php \Drupal\Tests\user\Unit\Form\EntityPermissionsFormTest

Tests the permissions administration form for a bundle.

Attributes

#[CoversClass(EntityPermissionsForm::class)] #[Group('user')] #[IgnoreDeprecations]

Hierarchy

Expanded class hierarchy of EntityPermissionsFormTest

File

core/modules/user/tests/src/Unit/Form/EntityPermissionsFormTest.php, line 29

Namespace

Drupal\Tests\user\Unit\Form
View source
class EntityPermissionsFormTest extends UnitTestCase {
  
  /**
   * Tests generating the permissions list.
   *
   * This is really a test of the protected method permissionsByProvider(). Call
   * the public method access() with an object $bundle, so that it skips most of
   * the logic in that function.
   *
   * @param string $dependency_name
   *   The test permission has a direct dependency on this.
   * @param bool $found
   *   TRUE if there is a permission to be managed by the form.
   *
   * @legacy-covers \Drupal\user\Form\EntityPermissionsForm::access
   * @legacy-covers \Drupal\user\Form\EntityPermissionsForm::permissionsByProvider
   */
  public function testPermissionsByProvider(string $dependency_name, bool $found) : void {
    // Mock the constructor parameters.
    $prophecy = $this->prophesize(PermissionHandlerInterface::class);
    $prophecy->getPermissions()
      ->willReturn([
      'permission name' => [
        'title' => 'permission display name',
        'provider' => 'some module',
        'dependencies' => [
          'config' => [
            $dependency_name,
          ],
        ],
      ],
    ]);
    $permission_handler = $prophecy->reveal();
    $role_storage = $this->prophesize(RoleStorageInterface::class)
      ->reveal();
    $module_handler = $this->prophesize(ModuleHandlerInterface::class)
      ->reveal();
    $module_extension_list = $this->prophesize(ModuleExtensionList::class)
      ->reveal();
    $prophecy = $this->prophesize(ConfigManagerInterface::class);
    $prophecy->findConfigEntityDependencies('config', [
      'node.type.article',
    ])
      ->willReturn([
      new ConfigEntityDependency('core.entity_view_display.node.article.full'),
      new ConfigEntityDependency('field.field.node.article.body'),
    ]);
    $config_manager = $prophecy->reveal();
    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->getPermissionGranularity()
      ->willReturn('entity_type');
    $entity_type = $prophecy->reveal();
    $prophecy = $this->prophesize(EntityTypeManagerInterface::class);
    $prophecy->getDefinition('entity_type')
      ->willReturn($entity_type);
    $entity_type_manager = $prophecy->reveal();
    $bundle_form = new EntityPermissionsForm($permission_handler, $role_storage, $module_handler, $config_manager, $entity_type_manager, $module_extension_list);
    // Mock the method parameters.
    $route = new Route('some.path');
    $route_match = $this->prophesize(RouteMatchInterface::class)
      ->reveal();
    $prophecy = $this->prophesize(EntityTypeInterface::class);
    $prophecy->getBundleOf()
      ->willReturn('entity_type');
    $bundle_type = $prophecy->reveal();
    $prophecy = $this->prophesize(EntityInterface::class);
    $prophecy->getEntityType()
      ->willReturn($bundle_type);
    $prophecy->getConfigDependencyName()
      ->willReturn('node.type.article');
    $bundle = $prophecy->reveal();
    $access_actual = $bundle_form->access($route, $route_match, $bundle);
    $this->assertEquals($found ? AccessResult::allowed() : AccessResult::neutral(), $access_actual);
    $this->expectUserDeprecationMessage('Drupal\\user\\Form\\EntityPermissionsForm::access() is deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use a permissions check on the route definition instead. See https://www.drupal.org/node/3384745');
  }
  
  /**
   * Provides data for the testPermissionsByProvider method.
   *
   * @return array
   *   An array of test data.
   */
  public static function providerTestPermissionsByProvider() {
    return [
      'direct dependency' => [
        'node.type.article',
        TRUE,
      ],
      'indirect dependency' => [
        'core.entity_view_display.node.article.full',
        TRUE,
      ],
      'not a dependency' => [
        'node.type.page',
        FALSE,
      ],
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCaseTrait::$root protected property The Drupal root directory.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::getDrupalRoot Deprecated protected static function Returns the Drupal root directory. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
DrupalTestCaseTrait::setUpRoot final protected function Ensure that the $root property is set initially.
EntityPermissionsFormTest::providerTestPermissionsByProvider public static function Provides data for the testPermissionsByProvider method.
EntityPermissionsFormTest::testPermissionsByProvider public function Tests generating the permissions list.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
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::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 361
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.