d59bfca036236ee887646230cde194aa06d0cb79
[yaffs-website] / web / core / modules / content_moderation / tests / src / Unit / ContentModerationRouteSubscriberTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Unit;
4
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;
13
14 /**
15  * @coversDefaultClass \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
16  *
17  * @group content_moderation
18  */
19 class ContentModerationRouteSubscriberTest extends UnitTestCase {
20
21   /**
22    * The test content moderation route subscriber.
23    *
24    * @var \Drupal\content_moderation\Routing\ContentModerationRouteSubscriber
25    */
26   protected $routeSubscriber;
27
28   /**
29    * {@inheritdoc}
30    */
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();
36   }
37
38   /**
39    * Creates the entity manager mock returning entity type objects.
40    */
41   protected function setupEntityTypes() {
42     $definition = $this->createMock(EntityTypeInterface::class);
43     $definition->expects($this->any())
44       ->method('getClass')
45       ->will($this->returnValue(SimpleTestEntity::class));
46     $definition->expects($this->any())
47       ->method('isRevisionable')
48       ->willReturn(FALSE);
49     $revisionable_definition = $this->createMock(EntityTypeInterface::class);
50     $revisionable_definition->expects($this->any())
51       ->method('getClass')
52       ->will($this->returnValue(SimpleTestEntity::class));
53     $revisionable_definition->expects($this->any())
54       ->method('isRevisionable')
55       ->willReturn(TRUE);
56     $entity_types = [
57       'entity_test' => $definition,
58       'entity_test_rev' => $revisionable_definition,
59     ];
60
61     $reflector = new \ReflectionProperty($this->routeSubscriber, 'moderatedEntityTypes');
62     $reflector->setAccessible(TRUE);
63     $reflector->setValue($this->routeSubscriber, $entity_types);
64   }
65
66   /**
67    * Data provider for ::testSetLatestRevisionFlag.
68    */
69   public function setLatestRevisionFlagTestCases() {
70     return [
71       'Entity parameter not on an entity form' => [
72         [],
73         [
74           'entity_test' => [
75             'type' => 'entity:entity_test_rev',
76           ],
77         ],
78       ],
79       'Entity parameter on an entity form' => [
80         [
81           '_entity_form' => 'entity_test_rev.edit',
82         ],
83         [
84           'entity_test_rev' => [
85             'type' => 'entity:entity_test_rev',
86           ],
87         ],
88         [
89           'entity_test_rev' => [
90             'type' => 'entity:entity_test_rev',
91             'load_latest_revision' => TRUE,
92           ],
93         ],
94       ],
95       'Entity form with no operation' => [
96         [
97           '_entity_form' => 'entity_test_rev',
98         ],
99         [
100           'entity_test_rev' => [
101             'type' => 'entity:entity_test_rev',
102           ],
103         ],
104         [
105           'entity_test_rev' => [
106             'type' => 'entity:entity_test_rev',
107             'load_latest_revision' => TRUE,
108           ],
109         ],
110       ],
111       'Non-moderated entity form' => [
112         [
113           '_entity_form' => 'entity_test_mulrev',
114         ],
115         [
116           'entity_test_mulrev' => [
117             'type' => 'entity:entity_test_mulrev',
118           ],
119         ],
120       ],
121       'Multiple entity parameters on an entity form' => [
122         [
123           '_entity_form' => 'entity_test_rev.edit',
124         ],
125         [
126           'entity_test_rev' => [
127             'type' => 'entity:entity_test_rev',
128           ],
129           'node' => [
130             'type' => 'entity:node',
131           ],
132         ],
133         [
134           'entity_test_rev' => [
135             'type' => 'entity:entity_test_rev',
136             'load_latest_revision' => TRUE,
137           ],
138           'node' => [
139             'type' => 'entity:node',
140           ],
141         ],
142       ],
143       'Overridden load_latest_revision flag does not change' => [
144         [
145           '_entity_form' => 'entity_test_rev.edit',
146         ],
147         [
148           'entity_test_rev' => [
149             'type' => 'entity:entity_test_rev',
150             'load_latest_revision' => FALSE,
151           ],
152         ],
153       ],
154       'Non-revisionable entity type will not change' => [
155         [
156           '_entity_form' => 'entity_test.edit',
157         ],
158         [
159           'entity_test' => [
160             'type' => 'entity:entity_test',
161           ],
162         ],
163         FALSE,
164         FALSE,
165       ],
166       'Overridden load_latest_revision flag does not change with multiple parameters' => [
167         [
168           '_entity_form' => 'entity_test_rev.edit',
169         ],
170         [
171           'entity_test_rev' => [
172             'type' => 'entity:entity_test_rev',
173           ],
174           'node' => [
175             'type' => 'entity:node',
176             'load_latest_revision' => FALSE,
177           ],
178         ],
179         [
180           'entity_test_rev' => [
181             'type' => 'entity:entity_test_rev',
182             'load_latest_revision' => TRUE,
183           ],
184           'node' => [
185             'type' => 'entity:node',
186             'load_latest_revision' => FALSE,
187           ],
188         ],
189       ],
190     ];
191   }
192
193   /**
194    * Tests that the "load_latest_revision" flag is handled correctly.
195    *
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.
202    *
203    * @covers ::setLatestRevisionFlag
204    *
205    * @dataProvider setLatestRevisionFlagTestCases
206    */
207   public function testSetLatestRevisionFlag($defaults, $parameters, $expected_parameters = FALSE) {
208     $route = new Route('/foo/{entity_test}', $defaults, [], [
209       'parameters' => $parameters,
210     ]);
211
212     $route_collection = new RouteCollection();
213     $route_collection->add('test', $route);
214     $event = new RouteBuildEvent($route_collection);
215     $this->routeSubscriber->onAlterRoutes($event);
216
217     // If expected parameters have not been provided, assert they are unchanged.
218     $this->assertEquals($expected_parameters ?: $parameters, $route->getOption('parameters'));
219   }
220
221 }
222
223 /**
224  * A concrete entity.
225  */
226 class SimpleTestEntity extends Entity {
227 }