You are here

protected function UrlTest::setUp in Drupal 8

Overrides UnitTestCase::setUp

File

core/tests/Drupal/Tests/Core/UrlTest.php, line 73
Contains \Drupal\Tests\Core\UrlTest.

Class

UrlTest
@coversDefaultClass \Drupal\Core\Url @group UrlTest

Namespace

Drupal\Tests\Core

Code

protected function setUp() {
  parent::setUp();
  $map = [];
  $map[] = [
    'view.frontpage.page_1',
    [],
    [],
    FALSE,
    '/node',
  ];
  $map[] = [
    'node_view',
    [
      'node' => '1',
    ],
    [],
    FALSE,
    '/node/1',
  ];
  $map[] = [
    'node_edit',
    [
      'node' => '2',
    ],
    [],
    FALSE,
    '/node/2/edit',
  ];
  $this->map = $map;
  $alias_map = [
    // Set up one proper alias that can be resolved to a system path.
    [
      'node-alias-test',
      NULL,
      FALSE,
      'node',
    ],
    // Passing in anything else should return the same string.
    [
      'node',
      NULL,
      FALSE,
      'node',
    ],
    [
      'node/1',
      NULL,
      FALSE,
      'node/1',
    ],
    [
      'node/2/edit',
      NULL,
      FALSE,
      'node/2/edit',
    ],
    [
      'non-existent',
      NULL,
      FALSE,
      'non-existent',
    ],
  ];

  // $this->map has $collect_bubbleable_metadata = FALSE; also generate the
  // $collect_bubbleable_metadata = TRUE case for ::generateFromRoute().
  $generate_from_route_map = [];
  foreach ($this->map as $values) {
    $generate_from_route_map[] = $values;
    $generate_from_route_map[] = [
      $values[0],
      $values[1],
      $values[2],
      TRUE,
      (new GeneratedUrl())
        ->setGeneratedUrl($values[4]),
    ];
  }
  $this->urlGenerator = $this
    ->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
  $this->urlGenerator
    ->expects($this
    ->any())
    ->method('generateFromRoute')
    ->will($this
    ->returnValueMap($generate_from_route_map));
  $this->pathAliasManager = $this
    ->createMock('Drupal\\path_alias\\AliasManagerInterface');
  $this->pathAliasManager
    ->expects($this
    ->any())
    ->method('getPathByAlias')
    ->will($this
    ->returnValueMap($alias_map));
  $this->router = $this
    ->createMock('Drupal\\Tests\\Core\\Routing\\TestRouterInterface');
  $this->pathValidator = $this
    ->createMock('Drupal\\Core\\Path\\PathValidatorInterface');
  $this->container = new ContainerBuilder();
  $this->container
    ->set('router.no_access_checks', $this->router);
  $this->container
    ->set('url_generator', $this->urlGenerator);
  $this->container
    ->set('path_alias.manager', $this->pathAliasManager);
  $this->container
    ->set('path.validator', $this->pathValidator);
  \Drupal::setContainer($this->container);
}