View source
<?php
namespace Drupal\Tests\content_moderation\Unit;
use Drupal\content_moderation\Routing\ContentModerationRouteSubscriber;
use Drupal\Core\Entity\EntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ContentModerationRouteSubscriberTest extends UnitTestCase {
protected $routeSubscriber;
protected function setUp() : void {
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$this->routeSubscriber = new ContentModerationRouteSubscriber($entity_type_manager);
$this
->setupEntityTypes();
}
protected function setupEntityTypes() {
$definition = $this
->createMock(EntityTypeInterface::class);
$definition
->expects($this
->any())
->method('getClass')
->will($this
->returnValue(SimpleTestEntity::class));
$definition
->expects($this
->any())
->method('isRevisionable')
->willReturn(FALSE);
$revisionable_definition = $this
->createMock(EntityTypeInterface::class);
$revisionable_definition
->expects($this
->any())
->method('getClass')
->will($this
->returnValue(SimpleTestEntity::class));
$revisionable_definition
->expects($this
->any())
->method('isRevisionable')
->willReturn(TRUE);
$entity_types = [
'entity_test' => $definition,
'entity_test_rev' => $revisionable_definition,
];
$reflector = new \ReflectionProperty($this->routeSubscriber, 'moderatedEntityTypes');
$reflector
->setAccessible(TRUE);
$reflector
->setValue($this->routeSubscriber, $entity_types);
}
public function setLatestRevisionFlagTestCases() {
return [
'Entity parameter not on an entity form' => [
[],
[
'entity_test' => [
'type' => 'entity:entity_test_rev',
],
],
],
'Entity parameter on an entity form' => [
[
'_entity_form' => 'entity_test_rev.edit',
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
],
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
'load_latest_revision' => TRUE,
],
],
],
'Entity form with no operation' => [
[
'_entity_form' => 'entity_test_rev',
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
],
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
'load_latest_revision' => TRUE,
],
],
],
'Non-moderated entity form' => [
[
'_entity_form' => 'entity_test_mulrev',
],
[
'entity_test_mulrev' => [
'type' => 'entity:entity_test_mulrev',
],
],
],
'Multiple entity parameters on an entity form' => [
[
'_entity_form' => 'entity_test_rev.edit',
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
],
'node' => [
'type' => 'entity:node',
],
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
'load_latest_revision' => TRUE,
],
'node' => [
'type' => 'entity:node',
],
],
],
'Overridden load_latest_revision flag does not change' => [
[
'_entity_form' => 'entity_test_rev.edit',
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
'load_latest_revision' => FALSE,
],
],
],
'Non-revisionable entity type will not change' => [
[
'_entity_form' => 'entity_test.edit',
],
[
'entity_test' => [
'type' => 'entity:entity_test',
],
],
FALSE,
FALSE,
],
'Overridden load_latest_revision flag does not change with multiple parameters' => [
[
'_entity_form' => 'entity_test_rev.edit',
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
],
'node' => [
'type' => 'entity:node',
'load_latest_revision' => FALSE,
],
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
'load_latest_revision' => TRUE,
],
'node' => [
'type' => 'entity:node',
'load_latest_revision' => FALSE,
],
],
],
'Parameter without type is unchanged' => [
[
'_entity_form' => 'entity_test_rev.edit',
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
],
'unrelated_param' => [
'foo' => 'bar',
],
],
[
'entity_test_rev' => [
'type' => 'entity:entity_test_rev',
'load_latest_revision' => TRUE,
],
'unrelated_param' => [
'foo' => 'bar',
],
],
],
];
}
public function testSetLatestRevisionFlag($defaults, $parameters, $expected_parameters = FALSE) {
$route = new Route('/foo/{entity_test}', $defaults, [], [
'parameters' => $parameters,
]);
$route_collection = new RouteCollection();
$route_collection
->add('test', $route);
$event = new RouteBuildEvent($route_collection);
$this->routeSubscriber
->onAlterRoutes($event);
$this
->assertEquals($expected_parameters ?: $parameters, $route
->getOption('parameters'));
}
}
class SimpleTestEntity extends EntityBase {
}