class EntityRevisionParamConverterTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/ParamConverter/EntityRevisionParamConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityRevisionParamConverterTest
  2. 10 core/tests/Drupal/Tests/Core/ParamConverter/EntityRevisionParamConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityRevisionParamConverterTest
  3. 9 core/tests/Drupal/Tests/Core/ParamConverter/EntityRevisionParamConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityRevisionParamConverterTest
  4. 8.9.x core/tests/Drupal/Tests/Core/ParamConverter/EntityRevisionParamConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityRevisionParamConverterTest

Tests Drupal\Core\ParamConverter\EntityRevisionParamConverter.

Attributes

#[CoversClass(EntityRevisionParamConverter::class)] #[Group('entity')]

Hierarchy

Expanded class hierarchy of EntityRevisionParamConverterTest

File

core/tests/Drupal/Tests/Core/ParamConverter/EntityRevisionParamConverterTest.php, line 22

Namespace

Drupal\Tests\Core\ParamConverter
View source
class EntityRevisionParamConverterTest extends UnitTestCase {
  
  /**
   * The tested entity revision param converter.
   *
   * @var \Drupal\Core\ParamConverter\EntityRevisionParamConverter
   */
  protected $converter;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->converter = new EntityRevisionParamConverter($this->prophesize(EntityTypeManagerInterface::class)
      ->reveal(), $this->prophesize(EntityRepositoryInterface::class)
      ->reveal());
  }
  protected function getTestRoute() : Route {
    $route = new Route('/test/{test_revision}');
    $route->setOption('parameters', [
      'test_revision' => [
        'type' => 'entity_revision:test',
      ],
    ]);
    return $route;
  }
  
  /**
   * Tests non applying route.
   *
   * @legacy-covers ::applies
   */
  public function testNonApplyingRoute() : void {
    $route = new Route('/test');
    $this->assertFalse($this->converter
      ->applies([], 'test_revision', $route));
  }
  
  /**
   * Tests applying route.
   *
   * @legacy-covers ::applies
   */
  public function testApplyingRoute() : void {
    $route = $this->getTestRoute();
    $this->assertTrue($this->converter
      ->applies($route->getOption('parameters')['test_revision'], 'test_revision', $route));
  }
  
  /**
   * Tests the convert() method.
   */
  public function testConvert($value, array $definition, array $defaults, $expected_result) : void {
    $storage = $this->prophesize(RevisionableStorageInterface::class);
    $storage->loadRevision('valid_id')
      ->willReturn((object) [
      'revision_id' => 'valid_id',
    ]);
    $storage->loadRevision('invalid_id')
      ->willReturn(NULL);
    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager->getStorage('entity_test')
      ->willReturn($storage->reveal());
    $entity_repository = $this->prophesize(EntityRepositoryInterface::class);
    $converter = new EntityRevisionParamConverter($entity_type_manager->reveal(), $entity_repository->reveal());
    $result = $converter->convert($value, $definition, 'test_revision', $defaults);
    $this->assertEquals($expected_result, $result);
  }
  
  /**
   * Provides test data for testConvert.
   */
  public static function providerTestConvert() : array {
    $data = [];
    // Existing entity type.
    $data[] = [
      'valid_id',
      [
        'type' => 'entity_revision:entity_test',
      ],
      [
        'test_revision' => 'valid_id',
      ],
      (object) [
        'revision_id' => 'valid_id',
      ],
    ];
    // Invalid ID.
    $data[] = [
      'invalid_id',
      [
        'type' => 'entity_revision:entity_test',
      ],
      [
        'test_revision' => 'invalid_id',
      ],
      NULL,
    ];
    // Entity type placeholder.
    $data[] = [
      'valid_id',
      [
        'type' => 'entity_revision:{entity_type}',
      ],
      [
        'test_revision' => 'valid_id',
        'entity_type' => 'entity_test',
      ],
      (object) [
        'revision_id' => 'valid_id',
      ],
    ];
    return $data;
  }
  
  /**
   * Tests the convert() method with an invalid entity type ID.
   */
  public function testConvertWithInvalidEntityType() : void {
    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager->getStorage('invalid_entity_type_id')
      ->willThrow(new InvalidPluginDefinitionException('invalid_entity_type_id'));
    $entity_repository = $this->prophesize(EntityRepositoryInterface::class);
    $converter = new EntityRevisionParamConverter($entity_type_manager->reveal(), $entity_repository->reveal());
    $this->expectException(InvalidPluginDefinitionException::class);
    $converter->convert('valid_id', [
      'type' => 'entity_revision:invalid_entity_type_id',
    ], 'foo', [
      'foo' => 'valid_id',
    ]);
  }
  
  /**
   * Tests the convert() method with an invalid dynamic entity type ID.
   */
  public function testConvertWithInvalidType() : void {
    $this->expectException(ParamNotConvertedException::class);
    $this->expectExceptionMessage('The type definition "entity_revision_{entity_type_id}" is invalid. The expected format is "entity_revision:<entity_type_id>".');
    $this->converter
      ->convert('valid_id', [
      'type' => 'entity_revision_{entity_type_id}',
    ], 'foo', [
      'foo' => 'valid_id',
    ]);
  }
  
  /**
   * Tests the convert() method with an invalid dynamic entity type ID.
   */
  public function testConvertWithInvalidDynamicEntityType() : void {
    $this->expectException(ParamNotConvertedException::class);
    $this->expectExceptionMessage('The "foo" parameter was not converted because the "invalid_entity_type_id" parameter is missing.');
    $this->converter
      ->convert('valid_id', [
      'type' => 'entity_revision:{invalid_entity_type_id}',
    ], 'foo', [
      'foo' => 'valid_id',
    ]);
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution.
EntityRevisionParamConverterTest::$converter protected property The tested entity revision param converter.
EntityRevisionParamConverterTest::getTestRoute protected function
EntityRevisionParamConverterTest::providerTestConvert public static function Provides test data for testConvert.
EntityRevisionParamConverterTest::setUp protected function Overrides UnitTestCase::setUp
EntityRevisionParamConverterTest::testApplyingRoute public function Tests applying route.
EntityRevisionParamConverterTest::testConvert public function Tests the convert() method.
EntityRevisionParamConverterTest::testConvertWithInvalidDynamicEntityType public function Tests the convert() method with an invalid dynamic entity type ID.
EntityRevisionParamConverterTest::testConvertWithInvalidEntityType public function Tests the convert() method with an invalid entity type ID.
EntityRevisionParamConverterTest::testConvertWithInvalidType public function Tests the convert() method with an invalid dynamic entity type ID.
EntityRevisionParamConverterTest::testNonApplyingRoute public function Tests non applying route.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
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::$root protected property The app root.
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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
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.