You are here

class PermissionsGeneratorTest in Entity Construction Kit (ECK) 8

Tests the form element implementation.

@group eck

Hierarchy

Expanded class hierarchy of PermissionsGeneratorTest

File

tests/src/Unit/PermissionsGeneratorTest.php, line 12

Namespace

Drupal\Tests\eck\Unit
View source
class PermissionsGeneratorTest extends UnitTestBase {

  /**
   * The subject under test.
   *
   * @var \Drupal\eck\PermissionsGenerator
   */
  private $sut;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->sut = $this
      ->createNewSubjectUnderTest();
  }

  /**
   * Creates a PermissionsGenerator to be used in the tests.
   *
   * @return \Drupal\eck\PermissionsGenerator
   *   The created PermissionsGenerator instance.
   */
  private function createNewSubjectUnderTest() {
    $permissionsGenerator = new PermissionsGenerator();
    $permissionsGenerator
      ->setStringTranslation($this
      ->getStringTranslationStub());
    return $permissionsGenerator;
  }

  /**
   * Tests that no permissions are created if no entity types are defined.
   *
   * @test
   */
  public function generatesNoPermissionsIfNoEntityTypesAreDefined() {
    $this
      ->assertSame([], $this->sut
      ->entityPermissions());
  }

  /**
   * Tests permission creation for a single entity type.
   *
   * @test
   */
  public function givenSingleEntityTypeGeneratesCorrectPermissions() {
    $this
      ->addEntityToStorage($this
      ->createEckEntityType('entity_type'));
    $permissions = $this->sut
      ->entityPermissions();
    $this
      ->assertCreatePermission($permissions);
    $this
      ->assertGlobalPermissions($permissions);
    $this
      ->assertOwnerPermissions($permissions);
  }

  /**
   * Tests permission creation for an entity type with an author field.
   *
   * @test
   */
  public function givenSingleEntityTypeWithAuthorFieldGeneratesCorrectPermissions() {
    $this
      ->addEntityToStorage($this
      ->createEckEntityType('entity_type', [
      'uid' => TRUE,
    ]));
    $permissions = $this->sut
      ->entityPermissions();
    $this
      ->assertCreatePermission($permissions);
    $this
      ->assertGlobalPermissions($permissions);
    $this
      ->assertOwnerPermissions($permissions);
  }

  /**
   * Tests permission creation for entity types with mixed settings.
   *
   * @test
   */
  public function givenMultipleEntityTypesWithMixedSettingsGeneratesCorrectPermissions() {
    $this
      ->addEntityToStorage($this
      ->createEckEntityType('entity_type'));
    $this
      ->addEntityToStorage($this
      ->createEckEntityType('another_type', [
      'uid' => TRUE,
    ]));
    $permissions = $this->sut
      ->entityPermissions();
    $this
      ->assertCreatePermission($permissions);
    $this
      ->assertGlobalPermissions($permissions);
    $this
      ->assertOwnerPermissions($permissions);
  }

  /**
   * Asserts that the correct create permission is returned.
   */
  protected function assertCreatePermission($permissions) {
    foreach ($this->entities as $id => $entity) {
      $this
        ->assertArrayHasKey("create {$id} entities", $permissions);
    }
  }

  /**
   * Asserts that the correct global permissions are returned.
   */
  protected function assertGlobalPermissions($permissions) {
    foreach ($this->entities as $id => $entity) {
      $this
        ->assertArrayHasKey("edit any {$id} entities", $permissions);
      $this
        ->assertArrayHasKey("delete any {$id} entities", $permissions);
      $this
        ->assertArrayHasKey("view any {$id} entities", $permissions);
    }
  }

  /**
   * Asserts that the correct owner permissions are returned.
   */
  protected function assertOwnerPermissions($permissions) {
    foreach ($this->entities as $id => $entity) {

      /** @var \Drupal\eck\Entity\EckEntityType $entity */
      if ($entity
        ->hasAuthorField()) {
        $this
          ->assertArrayHasKey("edit own {$id} entities", $permissions);
        $this
          ->assertArrayHasKey("delete own {$id} entities", $permissions);
        $this
          ->assertArrayHasKey("view own {$id} entities", $permissions);
      }
      else {
        $this
          ->assertArrayNotHasKey("edit own {$id} entities", $permissions);
        $this
          ->assertArrayNotHasKey("delete own {$id} entities", $permissions);
        $this
          ->assertArrayNotHasKey("view own {$id} entities", $permissions);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PermissionsGeneratorTest::$sut private property The subject under test.
PermissionsGeneratorTest::assertCreatePermission protected function Asserts that the correct create permission is returned.
PermissionsGeneratorTest::assertGlobalPermissions protected function Asserts that the correct global permissions are returned.
PermissionsGeneratorTest::assertOwnerPermissions protected function Asserts that the correct owner permissions are returned.
PermissionsGeneratorTest::createNewSubjectUnderTest private function Creates a PermissionsGenerator to be used in the tests.
PermissionsGeneratorTest::generatesNoPermissionsIfNoEntityTypesAreDefined public function Tests that no permissions are created if no entity types are defined.
PermissionsGeneratorTest::givenMultipleEntityTypesWithMixedSettingsGeneratesCorrectPermissions public function Tests permission creation for entity types with mixed settings.
PermissionsGeneratorTest::givenSingleEntityTypeGeneratesCorrectPermissions public function Tests permission creation for a single entity type.
PermissionsGeneratorTest::givenSingleEntityTypeWithAuthorFieldGeneratesCorrectPermissions public function Tests permission creation for an entity type with an author field.
PermissionsGeneratorTest::setUp protected function Overrides UnitTestBase::setUp
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestBase::$entities protected property The entities.
UnitTestBase::$services private property The services.
UnitTestBase::addEntityToStorage protected function Adds an entity to the mock storage.
UnitTestBase::assertArrayKeysEqual protected function Asserts that the array keys of an array equal the expected keys.
UnitTestBase::containerMockGetServiceCallback public function Callback for the get method on the mocked service container.
UnitTestBase::createEckEntityType protected function Creates a test entity type.
UnitTestBase::createLanguageManagerMock protected function Creates the language manager mock.
UnitTestBase::entityStorageLoadMultiple public function Callback for entity storage load multiple.
UnitTestBase::getEntityManagerMock private function Retrieves the entity manager mock.
UnitTestBase::getEntityStorageMock private function Retrieves the entity storage mock.
UnitTestBase::getEntityTypeManagerMock private function Retrieves the entity type manager mock.
UnitTestBase::getEntityTypeRepositoryMock private function Retrieves the entity type repository mock.
UnitTestBase::getNewUserMock private function Creates and returns a mocked user.
UnitTestBase::prepareContainer private function Prepares a mocked service container.
UnitTestBase::registerServiceWithContainerMock protected function Registers a (mocked) service with the mocked service container.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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.