class PermissionsGeneratorTest in Entity Construction Kit (ECK) 8
Tests the form element implementation.
@group eck
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait- class \Drupal\Tests\eck\Unit\UnitTestBase- class \Drupal\Tests\eck\Unit\PermissionsGeneratorTest
 
 
- class \Drupal\Tests\eck\Unit\UnitTestBase
Expanded class hierarchy of PermissionsGeneratorTest
File
- tests/src/ Unit/ PermissionsGeneratorTest.php, line 12 
Namespace
Drupal\Tests\eck\UnitView 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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| PermissionsGeneratorTest:: | private | property | The subject under test. | |
| PermissionsGeneratorTest:: | protected | function | Asserts that the correct create permission is returned. | |
| PermissionsGeneratorTest:: | protected | function | Asserts that the correct global permissions are returned. | |
| PermissionsGeneratorTest:: | protected | function | Asserts that the correct owner permissions are returned. | |
| PermissionsGeneratorTest:: | private | function | Creates a PermissionsGenerator to be used in the tests. | |
| PermissionsGeneratorTest:: | public | function | Tests that no permissions are created if no entity types are defined. | |
| PermissionsGeneratorTest:: | public | function | Tests permission creation for entity types with mixed settings. | |
| PermissionsGeneratorTest:: | public | function | Tests permission creation for a single entity type. | |
| PermissionsGeneratorTest:: | public | function | Tests permission creation for an entity type with an author field. | |
| PermissionsGeneratorTest:: | protected | function | Overrides UnitTestBase:: | |
| PhpunitCompatibilityTrait:: | public | function | Returns a mock object for the specified class using the available method. | |
| PhpunitCompatibilityTrait:: | public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
| UnitTestBase:: | protected | property | The entities. | |
| UnitTestBase:: | private | property | The services. | |
| UnitTestBase:: | protected | function | Adds an entity to the mock storage. | |
| UnitTestBase:: | protected | function | Asserts that the array keys of an array equal the expected keys. | |
| UnitTestBase:: | public | function | Callback for the get method on the mocked service container. | |
| UnitTestBase:: | protected | function | Creates a test entity type. | |
| UnitTestBase:: | protected | function | Creates the language manager mock. | |
| UnitTestBase:: | public | function | Callback for entity storage load multiple. | |
| UnitTestBase:: | private | function | Retrieves the entity manager mock. | |
| UnitTestBase:: | private | function | Retrieves the entity storage mock. | |
| UnitTestBase:: | private | function | Retrieves the entity type manager mock. | |
| UnitTestBase:: | private | function | Retrieves the entity type repository mock. | |
| UnitTestBase:: | private | function | Creates and returns a mocked user. | |
| UnitTestBase:: | private | function | Prepares a mocked service container. | |
| UnitTestBase:: | protected | function | Registers a (mocked) service with the mocked service container. | |
| UnitTestCase:: | protected | property | The random generator. | |
| UnitTestCase:: | protected | property | The app root. | 1 | 
| UnitTestCase:: | protected | function | Asserts if two arrays are equal by sorting them first. | |
| UnitTestCase:: | protected | function | Mocks a block with a block plugin. | 1 | 
| UnitTestCase:: | protected | function | Returns a stub class resolver. | |
| UnitTestCase:: | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase:: | public | function | Returns a stub config storage that returns the supplied configuration. | |
| UnitTestCase:: | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase:: | protected | function | Gets the random generator for the utility methods. | |
| UnitTestCase:: | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase:: | public | function | Generates a unique random string containing letters and numbers. | 
