class UserRoleTest in Feeds 8.3
Same name in this branch
- 8.3 tests/src/Unit/Feeds/Target/UserRoleTest.php \Drupal\Tests\feeds\Unit\Feeds\Target\UserRoleTest
- 8.3 tests/src/Kernel/Feeds/Target/UserRoleTest.php \Drupal\Tests\feeds\Kernel\Feeds\Target\UserRoleTest
@coversDefaultClass \Drupal\feeds\Feeds\Target\UserRole @group feeds
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait- class \Drupal\Tests\feeds\Unit\FeedsUnitTestCase uses FeedsMockingTrait, FeedsReflectionTrait- class \Drupal\Tests\feeds\Unit\Feeds\Target\FieldTargetTestBase- class \Drupal\Tests\feeds\Unit\Feeds\Target\EntityReferenceTestBase- class \Drupal\Tests\feeds\Unit\Feeds\Target\ConfigEntityReferenceTestBase- class \Drupal\Tests\feeds\Unit\Feeds\Target\UserRoleTest
 
 
- class \Drupal\Tests\feeds\Unit\Feeds\Target\ConfigEntityReferenceTestBase
 
- class \Drupal\Tests\feeds\Unit\Feeds\Target\EntityReferenceTestBase
 
- class \Drupal\Tests\feeds\Unit\Feeds\Target\FieldTargetTestBase
 
- class \Drupal\Tests\feeds\Unit\FeedsUnitTestCase uses FeedsMockingTrait, FeedsReflectionTrait
Expanded class hierarchy of UserRoleTest
File
- tests/src/ Unit/ Feeds/ Target/ UserRoleTest.php, line 20 
Namespace
Drupal\Tests\feeds\Unit\Feeds\TargetView source
class UserRoleTest extends ConfigEntityReferenceTestBase {
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->transliteration
      ->transliterate('Bar', LanguageInterface::LANGCODE_DEFAULT, '_')
      ->willReturn('Bar');
    // Create a role.
    $foo_role = $this
      ->prophesize(RoleInterface::class);
    $foo_role
      ->label()
      ->willReturn('Foo');
    // Entity storage (needed for entity queries).
    $this->entityStorage = $this
      ->prophesize(RoleStorageInterface::class);
    $this->entityStorage
      ->loadMultiple()
      ->willReturn([
      RoleInterface::ANONYMOUS_ID => $this
        ->createMock(RoleInterface::class),
      RoleInterface::AUTHENTICATED_ID => $this
        ->createMock(RoleInterface::class),
      'foo' => $foo_role
        ->reveal(),
    ]);
    $this->entityTypeManager
      ->getStorage('user_role')
      ->willReturn($this->entityStorage);
    $this->typedConfigManager
      ->getDefinition('user.role.*')
      ->willReturn([
      'label' => 'User role settings',
      'mapping' => [
        'uuid' => [
          'type' => 'uuid',
          'label' => 'UUID',
        ],
        'id' => [
          'type' => 'string',
          'label' => 'ID',
        ],
        'label' => [
          'type' => 'label',
          'label' => 'Label',
        ],
      ],
    ]);
    $this
      ->buildContainer();
  }
  /**
   * {@inheritdoc}
   */
  protected function createTargetPluginInstance(array $configuration = []) {
    $configuration += [
      'feed_type' => $this
        ->createMock(FeedTypeInterface::class),
      'target_definition' => $this
        ->createTargetDefinitionMock(),
      'reference_by' => 'label',
    ];
    return new UserRole($configuration, 'user_role', [], $this->entityTypeManager
      ->reveal(), $this->entityFinder
      ->reveal(), $this->transliteration
      ->reveal(), $this->typedConfigManager
      ->reveal());
  }
  /**
   * {@inheritdoc}
   */
  protected function getTargetClass() {
    return UserRole::class;
  }
  /**
   * {@inheritdoc}
   */
  protected function getEntityStorageClass() {
    return RoleStorageInterface::class;
  }
  /**
   * {@inheritdoc}
   */
  protected function getReferencableEntityTypeId() {
    return 'user_role';
  }
  /**
   * {@inheritdoc}
   */
  protected function createReferencableEntityType() {
    $referenceable_entity_type = $this
      ->prophesize(ConfigEntityTypeInterface::class);
    $referenceable_entity_type
      ->entityClassImplements(ConfigEntityInterface::class)
      ->willReturn(TRUE)
      ->shouldBeCalled();
    $referenceable_entity_type
      ->getKey('label')
      ->willReturn('label');
    $referenceable_entity_type
      ->getConfigPrefix()
      ->willReturn('user.role');
    $this->entityTypeManager
      ->getDefinition('user_role')
      ->willReturn($referenceable_entity_type)
      ->shouldBeCalled();
    return $referenceable_entity_type;
  }
  /**
   * Tests finding a role by label.
   *
   * @covers ::prepareValue
   * @covers ::findEntity
   */
  public function testPrepareValue() {
    $this->entityFinder
      ->findEntities($this
      ->getReferencableEntityTypeId(), 'label', 'Foo')
      ->willReturn([
      'foo',
    ])
      ->shouldBeCalled();
    $method = $this
      ->getProtectedClosure($this
      ->createTargetPluginInstance(), 'prepareValue');
    $values = [
      'target_id' => 'Foo',
    ];
    $method(0, $values);
    $this
      ->assertSame($values, [
      'target_id' => 'foo',
    ]);
  }
  /**
   * Tests prepareValue() method without match.
   *
   * @covers ::prepareValue
   * @covers ::findEntity
   */
  public function testPrepareValueReferenceNotFound() {
    $this->entityFinder
      ->findEntities($this
      ->getReferencableEntityTypeId(), 'label', 'Bar')
      ->willReturn([])
      ->shouldBeCalled();
    $method = $this
      ->getProtectedClosure($this
      ->createTargetPluginInstance(), 'prepareValue');
    $values = [
      'target_id' => 'Bar',
    ];
    $this
      ->expectException(ReferenceNotFoundException::class);
    $this
      ->expectExceptionMessage("The role <em class=\"placeholder\">Bar</em> cannot be assigned because it does not exist.");
    $method(0, $values);
  }
  /**
   * Tests referencing a non-allowed role.
   *
   * @covers ::prepareValue
   * @covers ::findEntity
   */
  public function testPrepareValueNonAllowedRole() {
    $this->entityFinder
      ->findEntities($this
      ->getReferencableEntityTypeId(), 'label', 'Foo')
      ->willReturn([
      'foo',
    ])
      ->shouldBeCalled();
    // The 'Foo' role may not be used.
    $target_plugin = $this
      ->createTargetPluginInstance([
      'allowed_roles' => [
        'foo' => FALSE,
      ],
    ]);
    $method = $this
      ->getProtectedClosure($target_plugin, 'prepareValue');
    $values = [
      'target_id' => 'Foo',
    ];
    $this
      ->expectException(TargetValidationException::class, 'The role <em class=\\"placeholder\\">foo</em> may not be referenced.');
    $method(0, $values);
  }
  /**
   * Tests referencing a newly created role.
   *
   * @covers ::prepareValue
   * @covers ::findEntity
   * @covers ::createRole
   */
  public function testPrepareValueWithNewRole() {
    $this->entityFinder
      ->findEntities($this
      ->getReferencableEntityTypeId(), 'label', 'Bar')
      ->willReturn([])
      ->shouldBeCalled();
    $role = $this
      ->prophesize(RoleInterface::class);
    $role
      ->save()
      ->willReturn(TRUE);
    $role
      ->id()
      ->willReturn('bar');
    $this->entityStorage
      ->create([
      'id' => 'bar',
      'label' => 'Bar',
    ])
      ->willReturn($role
      ->reveal())
      ->shouldBeCalled();
    $target_plugin = $this
      ->createTargetPluginInstance([
      'autocreate' => TRUE,
    ]);
    $method = $this
      ->getProtectedClosure($target_plugin, 'prepareValue');
    $values = [
      'target_id' => 'Bar',
    ];
    $method(0, $values);
    $this
      ->assertSame($values, [
      'target_id' => 'bar',
    ]);
  }
  /**
   * Tests prepareValue() with passing a space as value.
   *
   * @covers ::prepareValue
   * @covers ::findEntity
   * @covers ::createRole
   */
  public function testPrepareValueEmptyFeedWithAutoCreateRole() {
    $target_plugin = $this
      ->createTargetPluginInstance([
      'autocreate' => TRUE,
    ]);
    $method = $this
      ->getProtectedClosure($target_plugin, 'prepareValue');
    $values = [
      'target_id' => ' ',
    ];
    $this
      ->expectException(EmptyFeedException::class);
    $method(0, $values);
  }
  /**
   * @covers ::getSummary
   */
  public function testGetSummary() {
    $expected = [
      'Reference by: <em class="placeholder">Label</em>',
      'Allowed roles: <em class="placeholder">Foo</em>',
      'Only assign existing roles',
      'Revoke roles: no',
    ];
    $summary = $this
      ->createTargetPluginInstance()
      ->getSummary();
    foreach ($summary as $key => $value) {
      $summary[$key] = (string) $value;
    }
    $this
      ->assertEquals($expected, $summary);
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| ConfigEntityReferenceTestBase:: | protected | property | The transliteration manager. | |
| ConfigEntityReferenceTestBase:: | protected | property | The manager for managing config schema type plugins. | |
| EntityReferenceTestBase:: | protected | property | The Feeds entity finder service. | |
| EntityReferenceTestBase:: | protected | property | The entity storage prophecy used in the test. | |
| EntityReferenceTestBase:: | protected | property | The entity type manager prophecy used in the test. | |
| EntityReferenceTestBase:: | protected | function | Builds the Drupal service container. | |
| EntityReferenceTestBase:: | protected | function | Creates a Feeds target definition mock. | |
| EntityReferenceTestBase:: | public | function | @covers ::prepareTarget Overrides FieldTargetTestBase:: | 1 | 
| EntityReferenceTestBase:: | public | function | Tests prepareValue() without passing values. | 1 | 
| FeedsMockingTrait:: | protected | function | Mocks an account object. | |
| FeedsMockingTrait:: | protected | function | Returns a mocked AccountSwitcher object. | |
| FeedsMockingTrait:: | protected | function | Returns a mocked feed entity. | |
| FeedsMockingTrait:: | protected | function | Returns a mocked feed type entity. | |
| FeedsMockingTrait:: | protected | function | Mocks a field definition. | 1 | 
| FeedsMockingTrait:: | protected | function | Mocks the file system. | |
| FeedsReflectionTrait:: | protected | function | Calls a protected method on the given object. | |
| FeedsReflectionTrait:: | protected | function | Gets a ReflectionMethod for a class method. | |
| FeedsReflectionTrait:: | protected | function | Returns a dynamically created closure for the object's method. | |
| FeedsReflectionTrait:: | protected | function | Sets a protected property. | |
| FeedsUnitTestCase:: | protected | function | Returns the absolute directory path of the Feeds module. | |
| FeedsUnitTestCase:: | protected | function | Defines stub constants. | |
| FeedsUnitTestCase:: | protected | function | Returns a mock stream wrapper manager. | |
| FeedsUnitTestCase:: | protected | function | Returns the absolute directory path of the resources folder. | |
| 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. | |
| 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. | |
| UserRoleTest:: | protected | function | Creates a referencable entity type instance. Overrides EntityReferenceTestBase:: | |
| UserRoleTest:: | protected | function | Creates a new target plugin instance. Overrides EntityReferenceTestBase:: | |
| UserRoleTest:: | protected | function | Returns the entity storage class name to use in this test. Overrides EntityReferenceTestBase:: | |
| UserRoleTest:: | protected | function | Returns the entity type machine name to use in this test. Overrides EntityReferenceTestBase:: | |
| UserRoleTest:: | protected | function | Returns the target class. Overrides FieldTargetTestBase:: | |
| UserRoleTest:: | public | function | Overrides ConfigEntityReferenceTestBase:: | |
| UserRoleTest:: | public | function | @covers ::getSummary | |
| UserRoleTest:: | public | function | Tests finding a role by label. | |
| UserRoleTest:: | public | function | Tests prepareValue() with passing a space as value. | |
| UserRoleTest:: | public | function | Tests referencing a non-allowed role. | |
| UserRoleTest:: | public | function | Tests prepareValue() method without match. | |
| UserRoleTest:: | public | function | Tests referencing a newly created role. | 
