8381ea7c64e2b07e93d4e2b6bd1ba0bdc8872004
[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 () { return Url::fromRoute('<none>'); };
366     $test_cases['uri_callback'] = [[], $uri_callback];
367     $test_cases['bundle_uri_callback'] = [['uri_callback' => $uri_callback], NULL];
368
369     return $test_cases;
370   }
371
372   /**
373    * Tests the urlInfo() method.
374    *
375    * @param string $rel
376    *   The link relation to test.
377    * @param array $options
378    *   An array of URL options to test with.
379    *
380    * @covers ::urlInfo
381    *
382    * @dataProvider providerTestUrlInfo
383    */
384   public function testUrlInfo($rel, $options) {
385     $entity = $this->getEntity(Entity::class, [], ['toUrl']);
386     $entity->expects($this->once())
387       ->method('toUrl')
388       ->with($rel, $options);
389
390     $entity->urlInfo($rel, $options);
391   }
392
393   /**
394    * Provides data for testUrlInfo().
395    *
396    * @return array
397    *   An array of test cases for testUrlInfo().
398    */
399   public function providerTestUrlInfo() {
400     $test_cases = [];
401
402     $test_cases['default'] = ['canonical', []];
403     $test_cases['with_option'] = ['canonical', ['absolute' => TRUE]];
404     $test_cases['revision'] = ['revision', []];
405
406     return $test_cases;
407   }
408
409   /**
410    * Tests the url() method without an entity ID.
411    *
412    * @param string $rel
413    *   The link relation to test.
414    *
415    * @covers ::url
416    * @covers ::hasLinkTemplate
417    * @covers ::linkTemplates
418    *
419    * @dataProvider providerTestUrl
420    */
421   public function testUrlEmpty($rel) {
422     $entity = $this->getEntity(Entity::class, []);
423     $this->assertEquals('', $entity->url($rel));
424   }
425
426   /**
427    * Provides data for testUrlEmpty().
428    *
429    * @return array
430    *   An array of test cases for testUrlEmpty().
431    */
432   public function providerTestUrlEmpty() {
433     $test_cases = [];
434
435     $test_cases['default'] = ['canonical', []];
436     $test_cases['revision'] = ['revision', []];
437
438     return $test_cases;
439   }
440
441   /**
442    * Tests the url() method.
443    *
444    * @param string $rel
445    *   The link relation to test.
446    * @param array $options
447    *   An array of URL options to call url() with.
448    * @param array $default_options
449    *   An array of URL options that toUrl() should generate.
450    * @param array $expected_options
451    *   An array of combined URL options that should be set on the final URL.
452    *
453    * @covers ::url
454    * @covers ::hasLinkTemplate
455    * @covers ::linkTemplates
456    *
457    * @dataProvider providerTestUrl
458    */
459   public function testUrl($rel, $options, $default_options, $expected_options) {
460     $entity = $this->getEntity(Entity::class, ['id' => $this->entityId], ['toUrl']);
461     $this->registerLinkTemplate($rel);
462
463     $uri = $this->prophesize(Url::class);
464     $uri->getOptions()->willReturn($default_options);
465     $uri->setOptions($expected_options)->shouldBeCalled();
466
467     $url_string = "/test-entity/{$this->entityId}/$rel";
468     $uri->toString()->willReturn($url_string);
469
470     $entity->expects($this->once())
471       ->method('toUrl')
472       ->with($rel)
473       ->willReturn($uri->reveal());
474
475     $this->assertEquals($url_string, $entity->url($rel, $options));
476   }
477
478   /**
479    * Provides data for testUrl().
480    *
481    * @return array
482    *   An array of test cases for testUrl().
483    */
484   public function providerTestUrl() {
485     $test_cases = [];
486
487     $test_cases['default'] = ['canonical', [], [], []];
488     $test_cases['revision'] = ['revision', [], [], []];
489     $test_cases['option'] = ['canonical', ['absolute' => TRUE], [], ['absolute' => TRUE]];
490     $test_cases['default_option'] = ['canonical', [], ['absolute' => TRUE], ['absolute' => TRUE]];
491     $test_cases['option_merge'] = ['canonical', ['absolute' => TRUE], ['entity_type' => $this->entityTypeId], ['absolute' => TRUE, 'entity_type' => $this->entityTypeId]];
492     $test_cases['option_override'] = ['canonical', ['absolute' => TRUE], ['absolute' => FALSE], ['absolute' => TRUE]];
493
494     return $test_cases;
495   }
496
497   /**
498    * Returns a mock entity for testing.
499    *
500    * @param string $class
501    *   The class name to mock. Should be \Drupal\Core\Entity\Entity or a
502    *   subclass.
503    * @param array $values
504    *   An array of entity values to construct the mock entity with.
505    * @param array $methods
506    *   (optional) An array of additional methods to mock on the entity object.
507    *   The getEntityType() and entityManager() methods are always mocked.
508    *
509    * @return \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject
510    */
511   protected function getEntity($class, array $values, array $methods = []) {
512     $methods = array_merge($methods, ['getEntityType', 'entityManager']);
513
514     // Prophecy does not allow prophesizing abstract classes while actually
515     // calling their code. We use Prophecy below because that allows us to
516     // add method prophecies later while still revealing the prophecy now.
517     $entity = $this->getMockBuilder($class)
518       ->setConstructorArgs([$values, $this->entityTypeId])
519       ->setMethods($methods)
520       ->getMockForAbstractClass();
521
522     $this->entityType = $this->prophesize(EntityTypeInterface::class);
523     $this->entityType->getLinkTemplates()->willReturn([]);
524     $this->entityType->getKey('langcode')->willReturn(FALSE);
525     $entity->method('getEntityType')->willReturn($this->entityType->reveal());
526
527     $this->entityManager = $this->prophesize(EntityManagerInterface::class);
528     $entity->method('entityManager')->willReturn($this->entityManager->reveal());
529
530     return $entity;
531   }
532
533   /**
534    * Asserts that a given URL object matches the expectations.
535    *
536    * @param string $expected_route_name
537    *   The expected route name of the generated URL.
538    * @param array $expected_route_parameters
539    *   The expected route parameters of the generated URL.
540    * @param \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject $entity
541    *   The entity that is expected to be set as a URL option.
542    * @param bool $has_language
543    *   Whether or not the URL is expected to have a language option.
544    * @param \Drupal\Core\Url $url
545    *   The URL option to make the assertions on.
546    */
547   protected function assertUrl($expected_route_name, array $expected_route_parameters, $entity, $has_language, Url $url) {
548     $this->assertEquals($expected_route_name, $url->getRouteName());
549     $this->assertEquals($expected_route_parameters, $url->getRouteParameters());
550     $this->assertEquals($this->entityTypeId, $url->getOption('entity_type'));
551     $this->assertEquals($entity, $url->getOption('entity'));
552     if ($has_language) {
553       $this->assertEquals($this->langcode, $url->getOption('language')->getId());
554     }
555     else {
556       $this->assertNull($url->getOption('language'));
557     }
558   }
559
560   /**
561    * Registers a link template for the mock entity.
562    *
563    * @param string $link_template
564    *   The link template to register.
565    */
566   protected function registerLinkTemplate($link_template) {
567     $link_templates = [
568       // The path is actually never used because we never invoke the URL
569       // generator but perform assertions on the URL object directly.
570       $link_template => "/test-entity/{test_entity}/$link_template",
571     ];
572     $this->entityType->getLinkTemplates()->willReturn($link_templates);
573   }
574
575   /**
576    * Registers bundle information for the mock entity type.
577    *
578    * @param array $bundle_info
579    *   The bundle information to register.
580    */
581   protected function registerBundleInfo($bundle_info) {
582     $this->entityManager
583       ->getBundleInfo($this->entityTypeId)
584       ->willReturn([$this->entityTypeId => $bundle_info])
585     ;
586   }
587
588 }
589
590 abstract class RevisionableEntity extends Entity implements RevisionableInterface {}