EntityRevisionParamConverterTest.php 2.5 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\Core\ParamConverter;

use Drupal\Core\Entity\EntityInterface;
6
use Drupal\Core\Entity\EntityRepositoryInterface;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\ParamConverter\EntityRevisionParamConverter;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;

/**
 * @coversDefaultClass \Drupal\Core\ParamConverter\EntityRevisionParamConverter
 * @group entity
 */
class EntityRevisionParamConverterTest extends UnitTestCase {

  /**
   * The tested entity revision param converter.
   *
   * @var \Drupal\entity\ParamConverter\EntityRevisionParamConverter
   */
  protected $converter;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

32 33 34 35
    $this->converter = new EntityRevisionParamConverter(
      $this->prophesize(EntityTypeManagerInterface::class)->reveal(),
      $this->prophesize(EntityRepositoryInterface::class)->reveal()
    );
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  }

  protected function getTestRoute() {
    $route = new Route('/test/{test_revision}');
    $route->setOption('parameters', [
      'test_revision' => [
        'type' => 'entity_revision:test',
      ],
    ]);
    return $route;
  }

  /**
   * @covers ::applies
   */
  public function testNonApplyingRoute() {
    $route = new Route('/test');
    $this->assertFalse($this->converter->applies([], 'test_revision', $route));
  }

  /**
   * @covers ::applies
   */
  public function testApplyingRoute() {
    $route = $this->getTestRoute();
    $this->assertTrue($this->converter->applies($route->getOption('parameters')['test_revision'], 'test_revision', $route));
  }

  /**
   * @covers ::convert
   */
  public function testConvert() {
    $entity = $this->prophesize(EntityInterface::class)->reveal();
    $storage = $this->prophesize(EntityStorageInterface::class);
    $storage->loadRevision(1)->willReturn($entity);

    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager->getStorage('test')->willReturn($storage->reveal());
74 75 76
    $entity_repository = $this->prophesize(EntityRepositoryInterface::class);
    $entity_repository->getTranslationFromContext($entity)->willReturn($entity);
    $converter = new EntityRevisionParamConverter($entity_type_manager->reveal(), $entity_repository->reveal());
77 78 79 80 81 82 83

    $route = $this->getTestRoute();
    $result = $converter->convert(1, $route->getOption('parameters')['test_revision'], 'test_revision', ['test_revision' => 1]);
    $this->assertSame($entity, $result);
  }

}