3 namespace Drupal\Tests\content_moderation\Unit;
5 use Drupal\content_moderation\Routing\ContentModerationRouteSubscriber;
6 use Drupal\Core\Entity\Entity;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Routing\RouteBuildEvent;
10 use Drupal\Tests\UnitTestCase;
11 use Symfony\Component\Routing\Route;
12 use Symfony\Component\Routing\RouteCollection;
15 * @coversDefaultClass \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
17 * @group content_moderation
19 class ContentModerationRouteSubscriberTest extends UnitTestCase {
22 * The test content moderation route subscriber.
24 * @var \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
26 protected $routeSubscriber;
31 protected function setUp() {
32 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
33 $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
34 $this->routeSubscriber = new ContentModerationRouteSubscriber($entity_type_manager);
35 $this->setupEntityTypes();
39 * Creates the entity manager mock returning entity type objects.
41 protected function setupEntityTypes() {
42 $definition = $this->createMock(EntityTypeInterface::class);
43 $definition->expects($this->any())
45 ->will($this->returnValue(SimpleTestEntity::class));
46 $definition->expects($this->any())
47 ->method('isRevisionable')
49 $revisionable_definition = $this->createMock(EntityTypeInterface::class);
50 $revisionable_definition->expects($this->any())
52 ->will($this->returnValue(SimpleTestEntity::class));
53 $revisionable_definition->expects($this->any())
54 ->method('isRevisionable')
57 'entity_test' => $definition,
58 'entity_test_rev' => $revisionable_definition,
61 $reflector = new \ReflectionProperty($this->routeSubscriber, 'moderatedEntityTypes');
62 $reflector->setAccessible(TRUE);
63 $reflector->setValue($this->routeSubscriber, $entity_types);
67 * Data provider for ::testSetLatestRevisionFlag.
69 public function setLatestRevisionFlagTestCases() {
71 'Entity parameter not on an entity form' => [
75 'type' => 'entity:entity_test_rev',
79 'Entity parameter on an entity form' => [
81 '_entity_form' => 'entity_test_rev.edit',
84 'entity_test_rev' => [
85 'type' => 'entity:entity_test_rev',
89 'entity_test_rev' => [
90 'type' => 'entity:entity_test_rev',
91 'load_latest_revision' => TRUE,
95 'Entity form with no operation' => [
97 '_entity_form' => 'entity_test_rev',
100 'entity_test_rev' => [
101 'type' => 'entity:entity_test_rev',
105 'entity_test_rev' => [
106 'type' => 'entity:entity_test_rev',
107 'load_latest_revision' => TRUE,
111 'Non-moderated entity form' => [
113 '_entity_form' => 'entity_test_mulrev',
116 'entity_test_mulrev' => [
117 'type' => 'entity:entity_test_mulrev',
121 'Multiple entity parameters on an entity form' => [
123 '_entity_form' => 'entity_test_rev.edit',
126 'entity_test_rev' => [
127 'type' => 'entity:entity_test_rev',
130 'type' => 'entity:node',
134 'entity_test_rev' => [
135 'type' => 'entity:entity_test_rev',
136 'load_latest_revision' => TRUE,
139 'type' => 'entity:node',
143 'Overridden load_latest_revision flag does not change' => [
145 '_entity_form' => 'entity_test_rev.edit',
148 'entity_test_rev' => [
149 'type' => 'entity:entity_test_rev',
150 'load_latest_revision' => FALSE,
154 'Non-revisionable entity type will not change' => [
156 '_entity_form' => 'entity_test.edit',
160 'type' => 'entity:entity_test',
166 'Overridden load_latest_revision flag does not change with multiple parameters' => [
168 '_entity_form' => 'entity_test_rev.edit',
171 'entity_test_rev' => [
172 'type' => 'entity:entity_test_rev',
175 'type' => 'entity:node',
176 'load_latest_revision' => FALSE,
180 'entity_test_rev' => [
181 'type' => 'entity:entity_test_rev',
182 'load_latest_revision' => TRUE,
185 'type' => 'entity:node',
186 'load_latest_revision' => FALSE,
194 * Tests that the "load_latest_revision" flag is handled correctly.
196 * @param array $defaults
197 * The route defaults.
198 * @param array $parameters
199 * The route parameters.
200 * @param array|bool $expected_parameters
201 * (optional) The expected route parameters. Defaults to FALSE.
203 * @covers ::setLatestRevisionFlag
205 * @dataProvider setLatestRevisionFlagTestCases
207 public function testSetLatestRevisionFlag($defaults, $parameters, $expected_parameters = FALSE) {
208 $route = new Route('/foo/{entity_test}', $defaults, [], [
209 'parameters' => $parameters,
212 $route_collection = new RouteCollection();
213 $route_collection->add('test', $route);
214 $event = new RouteBuildEvent($route_collection);
215 $this->routeSubscriber->onAlterRoutes($event);
217 // If expected parameters have not been provided, assert they are unchanged.
218 $this->assertEquals($expected_parameters ?: $parameters, $route->getOption('parameters'));
226 class SimpleTestEntity extends Entity {