DomainAccessContentUrlsTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Drupal\Tests\domain_access\Functional;
  3. use Drupal\Core\Url;
  4. use Drupal\Tests\domain\Functional\DomainTestBase;
  5. /**
  6. * Tests behavior for getting all URLs for an entity.
  7. *
  8. * @group domain_access
  9. */
  10. class DomainAccessContentUrlsTest extends DomainTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = ['domain', 'domain_access', 'field', 'node', 'user'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function setUp() {
  21. parent::setUp();
  22. // Create 4 domains.
  23. DomainTestBase::domainCreateTestDomains(4);
  24. }
  25. /**
  26. * Tests domain source URLs.
  27. */
  28. public function testDomainContentUrls() {
  29. // Create a node, assigned to a source domain.
  30. $id = 'one_example_com';
  31. $nodes_values = [
  32. 'type' => 'page',
  33. 'title' => 'foo',
  34. DOMAIN_ACCESS_FIELD => [
  35. 'example_com',
  36. 'one_example_com',
  37. 'two_example_com',
  38. ],
  39. DOMAIN_ACCESS_ALL_FIELD => 0,
  40. ];
  41. $node = $this->createNode($nodes_values);
  42. // Variables for our tests.
  43. $path = 'node/1';
  44. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  45. $route_name = 'entity.node.canonical';
  46. $route_parameters = ['node' => 1];
  47. $uri = 'entity:' . $path;
  48. $uri_path = '/' . $path;
  49. $expected = $uri_path;
  50. $options = [];
  51. // Get the link using Url::fromRoute().
  52. $url = URL::fromRoute($route_name, $route_parameters, $options)->toString();
  53. $this->assertTrue($url == $expected, 'fromRoute');
  54. // Get the link using Url::fromUserInput()
  55. $url = URL::fromUserInput($uri_path, $options)->toString();
  56. $this->assertTrue($url == $expected, 'fromUserInput');
  57. // Get the link using Url::fromUri()
  58. $url = URL::fromUri($uri, $options)->toString();
  59. $this->assertTrue($url == $expected, 'fromUri');
  60. // Get the path processor service.
  61. $paths = \Drupal::service('domain_access.manager');
  62. $urls = $paths->getContentUrls($node);
  63. $expected = [
  64. 'example_com' => $domains['example_com']->getPath() . 'node/1',
  65. $id => $domains[$id]->getPath() . 'node/1',
  66. 'two_example_com' => $domains['two_example_com']->getPath() . 'node/1',
  67. ];
  68. $this->assertTrue($expected == $urls);
  69. }
  70. }