You are here

public function LinkGeneratorTest::testGenerateActive in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateActive()

Tests the active class on the link method.

See also

\Drupal\Core\Utility\LinkGenerator::generate()

File

core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php, line 379
Contains \Drupal\Tests\Core\Utility\LinkGeneratorTest.

Class

LinkGeneratorTest
@coversDefaultClass \Drupal\Core\Utility\LinkGenerator @group Utility

Namespace

Drupal\Tests\Core\Utility

Code

public function testGenerateActive() {
  $this->urlGenerator
    ->expects($this
    ->exactly(5))
    ->method('generateFromRoute')
    ->willReturnCallback(function ($name, $parameters = array(), $options = array(), $collect_bubbleable_metadata = FALSE) {
    switch ($name) {
      case 'test_route_1':
        return (new GeneratedUrl())
          ->setGeneratedUrl('/test-route-1');
      case 'test_route_3':
        return (new GeneratedUrl())
          ->setGeneratedUrl('/test-route-3');
      case 'test_route_4':
        if ($parameters['object'] == '1') {
          return (new GeneratedUrl())
            ->setGeneratedUrl('/test-route-4/1');
        }
    }
  });
  $this->urlGenerator
    ->expects($this
    ->exactly(4))
    ->method('getPathFromRoute')
    ->will($this
    ->returnValueMap(array(
    array(
      'test_route_1',
      array(),
      'test-route-1',
    ),
    array(
      'test_route_3',
      array(),
      'test-route-3',
    ),
    array(
      'test_route_4',
      array(
        'object' => '1',
      ),
      'test-route-4/1',
    ),
  )));
  $this->moduleHandler
    ->expects($this
    ->exactly(5))
    ->method('alter');

  // Render a link.
  $url = new Url('test_route_1', array(), array(
    'set_active_class' => TRUE,
  ));
  $url
    ->setUrlGenerator($this->urlGenerator);
  $result = $this->linkGenerator
    ->generate('Test', $url);
  $this
    ->assertLink(array(
    'attributes' => array(
      'data-drupal-link-system-path' => 'test-route-1',
    ),
  ), $result);

  // Render a link with the set_active_class option disabled.
  $url = new Url('test_route_1', array(), array(
    'set_active_class' => FALSE,
  ));
  $url
    ->setUrlGenerator($this->urlGenerator);
  $result = $this->linkGenerator
    ->generate('Test', $url);
  $this
    ->assertNoXPathResults('//a[@data-drupal-link-system-path="test-route-1"]', $result);

  // Render a link with an associated language.
  $url = new Url('test_route_1', array(), array(
    'language' => new Language(array(
      'id' => 'de',
    )),
    'set_active_class' => TRUE,
  ));
  $url
    ->setUrlGenerator($this->urlGenerator);
  $result = $this->linkGenerator
    ->generate('Test', $url);
  $this
    ->assertLink(array(
    'attributes' => array(
      'data-drupal-link-system-path' => 'test-route-1',
      'hreflang' => 'de',
    ),
  ), $result);

  // Render a link with a query parameter.
  $url = new Url('test_route_3', array(), array(
    'query' => array(
      'value' => 'example_1',
    ),
    'set_active_class' => TRUE,
  ));
  $url
    ->setUrlGenerator($this->urlGenerator);
  $result = $this->linkGenerator
    ->generate('Test', $url);
  $this
    ->assertLink(array(
    'attributes' => array(
      'data-drupal-link-system-path' => 'test-route-3',
      'data-drupal-link-query' => '{"value":"example_1"}',
    ),
  ), $result);

  // Render a link with route parameters and a query parameter.
  $url = new Url('test_route_4', array(
    'object' => '1',
  ), array(
    'query' => array(
      'value' => 'example_1',
    ),
    'set_active_class' => TRUE,
  ));
  $url
    ->setUrlGenerator($this->urlGenerator);
  $result = $this->linkGenerator
    ->generate('Test', $url);
  $this
    ->assertLink(array(
    'attributes' => array(
      'data-drupal-link-system-path' => 'test-route-4/1',
      'data-drupal-link-query' => '{"value":"example_1"}',
    ),
  ), $result);
}