Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Entity / EntityUrlTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Entity;
4
5 use Drupal\Core\Entity\Entity;
6 use Drupal\Core\Entity\EntityMalformedException;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
10 use Drupal\Core\Entity\RevisionableInterface;
11 use Drupal\Core\Url;
12 use Drupal\Tests\UnitTestCase;
13
14 /**
15  * Tests URL handling of the \Drupal\Core\Entity\Entity class.
16  *
17  * @coversDefaultClass \Drupal\Core\Entity\Entity
18  *
19  * @group Entity
20  */
21 class EntityUrlTest extends UnitTestCase {
22
23   /**
24    * The entity manager mock used in this test.
25    *
26    * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityManagerInterface
27    */
28   protected $entityManager;
29
30   /**
31    * The ID of the entity type used in this test.
32    *
33    * @var string
34    */
35   protected $entityTypeId = 'test_entity';
36
37   /**
38    * The entity type mock used in this test.
39    *
40    * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeInterface
41    */
42   protected $entityType;
43
44   /**
45    * The ID of the entity used in this test.
46    *
47    * @var int
48    */
49   protected $entityId = 1;
50
51   /**
52    * The revision ID of the entity used in this test.
53    *
54    * @var int
55    */
56   protected $revisionId = 2;
57
58   /**
59    * The language code of the entity used in this test.
60    *
61    * @var string
62    */
63   protected $langcode = 'en';
64
65   /**
66    * Indicator for default revisions.
67    *
68    * @var true
69    */
70   const DEFAULT_REVISION = TRUE;
71
72   /**
73    * Indicator for non-default revisions.
74    *
75    * @var false
76    */
77   const NON_DEFAULT_REVISION = FALSE;
78
79   /**
80    * Indicator that the test entity type has no bundle key.
81    *
82    * @var false
83    */
84   const HAS_NO_BUNDLE_KEY = FALSE;
85
86   /**
87    * Indicator that the test entity type has a bundle key.
88    *
89    * @var true
90    */
91   const HAS_BUNDLE_KEY = TRUE;
92
93   /**
94    * Tests the toUrl() method without an entity ID.
95    *
96    * @covers ::toUrl
97    */
98   public function testToUrlNoId() {
99     $entity = $this->getEntity(Entity::class, []);
100
101     $this->setExpectedException(EntityMalformedException::class, 'The "' . $this->entityTypeId . '" entity cannot have a URI as it does not have an ID');
102     $entity->toUrl();
103   }
104
105   /**
106    * Tests the toUrl() method with simple link templates.
107    *
108    * @param string $link_template
109    *   The link template to test.
110    * @param string $expected_route_name
111    *   The expected route name of the generated URL.
112    *
113    * @dataProvider providerTestToUrlLinkTemplates
114    *
115    * @covers ::toUrl
116    * @covers ::linkTemplates
117    * @covers ::urlRouteParameters
118    */
119   public function testToUrlLinkTemplates($link_template, $expected_route_name) {
120     $values = ['id' => $this->entityId, 'langcode' => $this->langcode];
121     $entity = $this->getEntity(Entity::class, $values);
122     $this->registerLinkTemplate($link_template);
123
124     /** @var \Drupal\Core\Url $url */
125     $url = $entity->toUrl($link_template);
126     // The entity ID is the sole route parameter for the link templates tested
127     // here.
128     $this->assertUrl($expected_route_name, ['test_entity' => $this->entityId], $entity, TRUE, $url);
129   }
130
131   /**
132    * Provides data for testToUrlLinkTemplates().
133    *
134    * @return array
135    *   An array of test cases for testToUrlLinkTemplates().
136    */
137   public function providerTestToUrlLinkTemplates() {
138     $test_cases = [];
139
140     $test_cases['canonical'] = ['canonical', 'entity.test_entity.canonical'];
141     $test_cases['version-history'] = ['version-history', 'entity.test_entity.version_history'];
142     $test_cases['edit-form'] = ['edit-form', 'entity.test_entity.edit_form'];
143     $test_cases['delete-form'] = ['delete-form', 'entity.test_entity.delete_form'];
144     $test_cases['revision'] = ['revision', 'entity.test_entity.revision'];
145
146     return $test_cases;
147   }
148
149   /**
150    * Tests the toUrl() method with the 'revision' link template.
151    *
152    * @param bool $is_default_revision
153    *   Whether or not the mock entity should be the default revision.
154    * @param string $link_template
155    *   The link template to test.
156    * @param string $expected_route_name
157    *   The expected route name of the generated URL.
158    * @param array $expected_route_parameters
159    *   The expected route parameters of the generated URL.
160    *
161    * @dataProvider providerTestToUrlLinkTemplateRevision
162    *
163    * @covers ::toUrl
164    * @covers ::linkTemplates
165    * @covers ::urlRouteParameters
166    */
167   public function testToUrlLinkTemplateRevision($is_default_revision, $link_template, $expected_route_name, array $expected_route_parameters) {
168     $values = ['id' => $this->entityId, 'langcode' => $this->langcode];
169     $entity = $this->getEntity(RevisionableEntity::class, $values);
170     $entity->method('getRevisionId')->willReturn($this->revisionId);
171     $entity->method('isDefaultRevision')->willReturn($is_default_revision);
172     $this->registerLinkTemplate($link_template);
173     // Even though this is tested with both the 'canonical' and the 'revision'
174     // template registered with the entity, we always ask for the 'revision'
175     // link template, to test that it falls back to the 'canonical' link
176     // template in case of the default revision.
177     /** @var \Drupal\Core\Url $url */
178     $url = $entity->toUrl('revision');
179     $this->assertUrl($expected_route_name, $expected_route_parameters, $entity, TRUE, $url);
180
181   }
182
183   /**
184    * Provides data for testToUrlLinkTemplateRevision().
185    *
186    * @return array
187    *   An array of test cases for testToUrlLinkTemplateRevision().
188    */
189   public function providerTestToUrlLinkTemplateRevision() {
190     $test_cases = [];
191
192     $route_parameters = ['test_entity' => $this->entityId];
193     $test_cases['default_revision'] = [static::DEFAULT_REVISION, 'canonical', 'entity.test_entity.canonical', $route_parameters];
194     // Add the revision ID to the expected route parameters.
195     $route_parameters['test_entity_revision'] = $this->revisionId;
196     $test_cases['non_default_revision'] = [static::NON_DEFAULT_REVISION, 'revision', 'entity.test_entity.revision', $route_parameters];
197
198     return $test_cases;
199   }
200
201   /**
202    * Tests the toUrl() method with link templates without an entity ID.
203    *
204    * @param string $link_template
205    *   The link template to test.
206    * @param string $expected_route_name
207    *   The expected route name of the generated URL.
208    *
209    * @dataProvider providerTestToUrlLinkTemplateNoId
210    *
211    * @covers ::toUrl
212    * @covers ::linkTemplates
213    * @covers ::urlRouteParameters
214    */
215   public function testToUrlLinkTemplateNoId($link_template, $expected_route_name) {
216     $entity = $this->getEntity(Entity::class, ['id' => $this->entityId]);
217     $this->registerLinkTemplate($link_template);
218
219     /** @var \Drupal\Core\Url $url */
220     $url = $entity->toUrl($link_template);
221     $this->assertUrl($expected_route_name, [], $entity, FALSE, $url);
222   }
223
224   /**
225    * Provides data for testToUrlLinkTemplateNoId().
226    *
227    * @return array
228    *   An array of test cases for testToUrlLinkTemplateNoId().
229    */
230   public function providerTestToUrlLinkTemplateNoId() {
231     $test_cases = [];
232
233     $test_cases['collection'] = ['collection', 'entity.test_entity.collection'];
234     $test_cases['add-page'] = ['add-page', 'entity.test_entity.add_page'];
235
236     return $test_cases;
237   }
238
239   /**
240    * Tests the toUrl() method with the 'revision' link template.
241    *
242    * @param bool $has_bundle_key
243    *   Whether or not the mock entity type should have a bundle key.
244    * @param string|null $bundle_entity_type
245    *   The ID of the bundle entity type of the mock entity type, or NULL if the
246    *   mock entity type should not have a bundle entity type.
247    * @param string $bundle_key
248    *   The bundle key of the mock entity type or FALSE if the entity type should
249    *   not have a bundle key.
250    * @param array $expected_route_parameters
251    *   The expected route parameters of the generated URL.
252    *
253    * @dataProvider providerTestToUrlLinkTemplateAddForm
254    *
255    * @covers ::toUrl
256    * @covers ::linkTemplates
257    * @covers ::urlRouteParameters
258    */
259   public function testToUrlLinkTemplateAddForm($has_bundle_key, $bundle_entity_type, $bundle_key, $expected_route_parameters) {
260     $values = ['id' => $this->entityId, 'langcode' => $this->langcode];
261     $entity = $this->getEntity(Entity::class, $values);
262     $this->entityType->hasKey('bundle')->willReturn($has_bundle_key);
263     $this->entityType->getBundleEntityType()->willReturn($bundle_entity_type);
264     $this->entityType->getKey('bundle')->willReturn($bundle_key);
265     $link_template = 'add-form';
266     $this->registerLinkTemplate($link_template);
267
268     /** @var \Drupal\Core\Url $url */
269     $url = $entity->toUrl($link_template);
270     $this->assertUrl('entity.test_entity.add_form', $expected_route_parameters, $entity, FALSE, $url);
271   }
272
273   /**
274    * Provides data for testToUrlLinkTemplateAddForm().
275    *
276    * @return array
277    *   An array of test cases for testToUrlLinkTemplateAddForm().
278    */
279   public function providerTestToUrlLinkTemplateAddForm() {
280     $test_cases = [];
281
282     $route_parameters = [];
283     $test_cases['no_bundle_key'] = [static::HAS_NO_BUNDLE_KEY, NULL, FALSE, $route_parameters];
284
285     $route_parameters = ['type' => $this->entityTypeId];
286     $test_cases['bundle_entity_type'] = [static::HAS_BUNDLE_KEY, 'type', FALSE, $route_parameters];
287     $test_cases['bundle_key'] = [static::HAS_BUNDLE_KEY, NULL, 'type', $route_parameters];
288
289     return $test_cases;
290   }
291
292   /**
293    * Tests the toUrl() method with neither link templates nor a URI callback.
294    *
295    * @param array $bundle_info
296    *   An array of bundle info to register.
297    * @param string $uri_callback
298    *   The entity type URI callback to register.
299    *
300    * @dataProvider providerTestToUrlUriCallbackUndefined
301    *
302    * @covers ::toUrl
303    * @covers ::linkTemplates
304    */
305   public function testToUrlUriCallbackUndefined(array $bundle_info, $uri_callback) {
306     $entity = $this->getEntity(Entity::class, ['id' => $this->entityId]);
307
308     $this->registerBundleInfo($bundle_info);
309     $this->entityType->getUriCallback()->willReturn($uri_callback);
310
311     $link_template = 'canonical';
312     $this->setExpectedException(UndefinedLinkTemplateException::class, "No link template '$link_template' found for the '$this->entityTypeId' entity type");
313     $entity->toUrl($link_template);
314   }
315
316   /**
317    * Provides data for testToUrlUriCallbackUndefined().
318    *
319    * @return array
320    *   An array of test cases for testToUrlUriCallbackUndefined().
321    */
322   public function providerTestToUrlUriCallbackUndefined() {
323     $test_cases = [];
324
325     $test_cases['no_callback'] = [[], NULL];
326     $test_cases['uri_callback'] = [[], 'not_a_callable'];
327     $test_cases['bundle_uri_callback'] = [['uri_callback' => 'not_a_callable'], NULL];
328
329     return $test_cases;
330   }
331
332   /**
333    * Tests the toUrl() method with a URI callback.
334    *
335    * @param array $bundle_info
336    *   An array of bundle info to register.
337    * @param string $uri_callback
338    *   The entity type URI callback to register.
339    *
340    * @covers ::toUrl
341    * @covers ::linkTemplates
342    *
343    * @dataProvider providerTestToUrlUriCallback
344    */
345   public function testToUrlUriCallback(array $bundle_info, $uri_callback) {
346     $entity = $this->getEntity(Entity::class, ['id' => $this->entityId, 'langcode' => $this->langcode]);
347
348     $this->registerBundleInfo($bundle_info);
349     $this->entityType->getUriCallback()->willReturn($uri_callback);
350
351     /** @var \Drupal\Core\Url $url */
352     $url = $entity->toUrl('canonical');
353     $this->assertUrl('<none>', [], $entity, TRUE, $url);
354   }
355
356   /**
357    * Provides data for testToUrlUriCallback().
358    *
359    * @return array
360    *   An array of test cases for testToUrlUriCallback().
361    */
362   public function providerTestToUrlUriCallback() {
363     $test_cases = [];
364
365     $uri_callback = function () {
366       return Url::fromRoute('<none>');
367     };
368     $test_cases['uri_callback'] = [[], $uri_callback];
369     $test_cases['bundle_uri_callback'] = [['uri_callback' => $uri_callback], NULL];
370
371     return $test_cases;
372   }
373
374   /**
375    * Tests the urlInfo() method.
376    *
377    * @param string $rel
378    *   The link relation to test.
379    * @param array $options
380    *   An array of URL options to test with.
381    *
382    * @covers ::urlInfo
383    *
384    * @dataProvider providerTestUrlInfo
385    */
386   public function testUrlInfo($rel, $options) {
387     $entity = $this->getEntity(Entity::class, [], ['toUrl']);
388     $entity->expects($this->once())
389       ->method('toUrl')
390       ->with($rel, $options);
391
392     $entity->urlInfo($rel, $options);
393   }
394
395   /**
396    * Provides data for testUrlInfo().
397    *
398    * @return array
399    *   An array of test cases for testUrlInfo().
400    */
401   public function providerTestUrlInfo() {
402     $test_cases = [];
403
404     $test_cases['default'] = ['canonical', []];
405     $test_cases['with_option'] = ['canonical', ['absolute' => TRUE]];
406     $test_cases['revision'] = ['revision', []];
407
408     return $test_cases;
409   }
410
411   /**
412    * Tests the url() method without an entity ID.
413    *
414    * @param string $rel
415    *   The link relation to test.
416    *
417    * @covers ::url
418    * @covers ::hasLinkTemplate
419    * @covers ::linkTemplates
420    *
421    * @dataProvider providerTestUrl
422    */
423   public function testUrlEmpty($rel) {
424     $entity = $this->getEntity(Entity::class, []);
425     $this->assertEquals('', $entity->url($rel));
426   }
427
428   /**
429    * Provides data for testUrlEmpty().
430    *
431    * @return array
432    *   An array of test cases for testUrlEmpty().
433    */
434   public function providerTestUrlEmpty() {
435     $test_cases = [];
436
437     $test_cases['default'] = ['canonical', []];
438     $test_cases['revision'] = ['revision', []];
439
440     return $test_cases;
441   }
442
443   /**
444    * Tests the url() method.
445    *
446    * @param string $rel
447    *   The link relation to test.
448    * @param array $options
449    *   An array of URL options to call url() with.
450    * @param array $default_options
451    *   An array of URL options that toUrl() should generate.
452    * @param array $expected_options
453    *   An array of combined URL options that should be set on the final URL.
454    *
455    * @covers ::url
456    * @covers ::hasLinkTemplate
457    * @covers ::linkTemplates
458    *
459    * @dataProvider providerTestUrl
460    */
461   public function testUrl($rel, $options, $default_options, $expected_options) {
462     $entity = $this->getEntity(Entity::class, ['id' => $this->entityId], ['toUrl']);
463     $this->registerLinkTemplate($rel);
464
465     $uri = $this->prophesize(Url::class);
466     $uri->getOptions()->willReturn($default_options);
467     $uri->setOptions($expected_options)->shouldBeCalled();
468
469     $url_string = "/test-entity/{$this->entityId}/$rel";
470     $uri->toString()->willReturn($url_string);
471
472     $entity->expects($this->once())
473       ->method('toUrl')
474       ->with($rel)
475       ->willReturn($uri->reveal());
476
477     $this->assertEquals($url_string, $entity->url($rel, $options));
478   }
479
480   /**
481    * Provides data for testUrl().
482    *
483    * @return array
484    *   An array of test cases for testUrl().
485    */
486   public function providerTestUrl() {
487     $test_cases = [];
488
489     $test_cases['default'] = ['canonical', [], [], []];
490     $test_cases['revision'] = ['revision', [], [], []];
491     $test_cases['option'] = ['canonical', ['absolute' => TRUE], [], ['absolute' => TRUE]];
492     $test_cases['default_option'] = ['canonical', [], ['absolute' => TRUE], ['absolute' => TRUE]];
493     $test_cases['option_merge'] = ['canonical', ['absolute' => TRUE], ['entity_type' => $this->entityTypeId], ['absolute' => TRUE, 'entity_type' => $this->entityTypeId]];
494     $test_cases['option_override'] = ['canonical', ['absolute' => TRUE], ['absolute' => FALSE], ['absolute' => TRUE]];
495
496     return $test_cases;
497   }
498
499   /**
500    * Returns a mock entity for testing.
501    *
502    * @param string $class
503    *   The class name to mock. Should be \Drupal\Core\Entity\Entity or a
504    *   subclass.
505    * @param array $values
506    *   An array of entity values to construct the mock entity with.
507    * @param array $methods
508    *   (optional) An array of additional methods to mock on the entity object.
509    *   The getEntityType() and entityManager() methods are always mocked.
510    *
511    * @return \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject
512    */
513   protected function getEntity($class, array $values, array $methods = []) {
514     $methods = array_merge($methods, ['getEntityType', 'entityManager']);
515
516     // Prophecy does not allow prophesizing abstract classes while actually
517     // calling their code. We use Prophecy below because that allows us to
518     // add method prophecies later while still revealing the prophecy now.
519     $entity = $this->getMockBuilder($class)
520       ->setConstructorArgs([$values, $this->entityTypeId])
521       ->setMethods($methods)
522       ->getMockForAbstractClass();
523
524     $this->entityType = $this->prophesize(EntityTypeInterface::class);
525     $this->entityType->getLinkTemplates()->willReturn([]);
526     $this->entityType->getKey('langcode')->willReturn(FALSE);
527     $entity->method('getEntityType')->willReturn($this->entityType->reveal());
528
529     $this->entityManager = $this->prophesize(EntityManagerInterface::class);
530     $entity->method('entityManager')->willReturn($this->entityManager->reveal());
531
532     return $entity;
533   }
534
535   /**
536    * Asserts that a given URL object matches the expectations.
537    *
538    * @param string $expected_route_name
539    *   The expected route name of the generated URL.
540    * @param array $expected_route_parameters
541    *   The expected route parameters of the generated URL.
542    * @param \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject $entity
543    *   The entity that is expected to be set as a URL option.
544    * @param bool $has_language
545    *   Whether or not the URL is expected to have a language option.
546    * @param \Drupal\Core\Url $url
547    *   The URL option to make the assertions on.
548    */
549   protected function assertUrl($expected_route_name, array $expected_route_parameters, $entity, $has_language, Url $url) {
550     $this->assertEquals($expected_route_name, $url->getRouteName());
551     $this->assertEquals($expected_route_parameters, $url->getRouteParameters());
552     $this->assertEquals($this->entityTypeId, $url->getOption('entity_type'));
553     $this->assertEquals($entity, $url->getOption('entity'));
554     if ($has_language) {
555       $this->assertEquals($this->langcode, $url->getOption('language')->getId());
556     }
557     else {
558       $this->assertNull($url->getOption('language'));
559     }
560   }
561
562   /**
563    * Registers a link template for the mock entity.
564    *
565    * @param string $link_template
566    *   The link template to register.
567    */
568   protected function registerLinkTemplate($link_template) {
569     $link_templates = [
570       // The path is actually never used because we never invoke the URL
571       // generator but perform assertions on the URL object directly.
572       $link_template => "/test-entity/{test_entity}/$link_template",
573     ];
574     $this->entityType->getLinkTemplates()->willReturn($link_templates);
575   }
576
577   /**
578    * Registers bundle information for the mock entity type.
579    *
580    * @param array $bundle_info
581    *   The bundle information to register.
582    */
583   protected function registerBundleInfo($bundle_info) {
584     $this->entityManager
585       ->getBundleInfo($this->entityTypeId)
586       ->willReturn([$this->entityTypeId => $bundle_info]);
587   }
588
589 }
590
591 abstract class RevisionableEntity extends Entity implements RevisionableInterface {}