UnroutedUrlAssemblerTest.php 6.95 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\Core\Utility;

5
use Drupal\Core\GeneratedUrl;
6
use Drupal\Core\Render\BubbleableMetadata;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
use Drupal\Core\Utility\UnroutedUrlAssembler;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * @coversDefaultClass \Drupal\Core\Utility\UnroutedUrlAssembler
 * @group Utility
 */
class UnroutedUrlAssemblerTest extends UnitTestCase {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The mocked config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
33
   * The tested unrouted URL assembler.
34 35 36 37 38
   *
   * @var \Drupal\Core\Utility\UnroutedUrlAssembler
   */
  protected $unroutedUrlAssembler;

39 40 41 42 43 44 45
  /**
   * The mocked outbound path processor.
   *
   * @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pathProcessor;

46 47 48 49 50 51 52
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->requestStack = new RequestStack();
53
    $this->pathProcessor = $this->createMock('Drupal\Core\PathProcessor\OutboundPathProcessorInterface');
54
    $this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor);
55 56 57 58 59 60
  }

  /**
   * @covers ::assemble
   */
  public function testAssembleWithNeitherExternalNorDomainLocalUri() {
61
    $this->expectException(\InvalidArgumentException::class);
62 63 64
    $this->unroutedUrlAssembler->assemble('wrong-url');
  }

65 66 67 68
  /**
   * @covers ::assemble
   */
  public function testAssembleWithLeadingSlash() {
69
    $this->expectException(\InvalidArgumentException::class);
70 71 72
    $this->unroutedUrlAssembler->assemble('/drupal.org');
  }

73 74 75 76 77 78 79
  /**
   * @covers ::assemble
   * @covers ::buildExternalUrl
   *
   * @dataProvider providerTestAssembleWithExternalUrl
   */
  public function testAssembleWithExternalUrl($uri, array $options, $expected) {
80 81 82 83 84
    $this->setupRequestStack(FALSE);
    $this->assertEquals($expected, $this->unroutedUrlAssembler->assemble($uri, $options));
    $generated_url = $this->unroutedUrlAssembler->assemble($uri, $options, TRUE);
    $this->assertEquals($expected, $generated_url->getGeneratedUrl());
    $this->assertInstanceOf('\Drupal\Core\Render\BubbleableMetadata', $generated_url);
85 86 87 88 89 90 91 92 93 94 95 96 97 98
  }

  /**
   * Provides test data for testAssembleWithExternalUrl
   */
  public function providerTestAssembleWithExternalUrl() {
    return [
      ['http://example.com/test', [], 'http://example.com/test'],
      ['http://example.com/test', ['fragment' => 'example'], 'http://example.com/test#example'],
      ['http://example.com/test', ['fragment' => 'example'], 'http://example.com/test#example'],
      ['http://example.com/test', ['query' => ['foo' => 'bar']], 'http://example.com/test?foo=bar'],
      ['http://example.com/test', ['https' => TRUE], 'https://example.com/test'],
      ['https://example.com/test', ['https' => FALSE], 'http://example.com/test'],
      ['https://example.com/test?foo=1#bar', [], 'https://example.com/test?foo=1#bar'],
99
      'override-query' => ['https://example.com/test?foo=1#bar', ['query' => ['foo' => 2]], 'https://example.com/test?foo=2#bar'],
100 101
      'override-query-merge' => ['https://example.com/test?foo=1#bar', ['query' => ['bar' => 2]], 'https://example.com/test?foo=1&bar=2#bar'],
      'override-deep-query-merge' => ['https://example.com/test?foo=1#bar', ['query' => ['bar' => ['baz' => 'foo']]], 'https://example.com/test?foo=1&bar%5Bbaz%5D=foo#bar'],
102
      'override-fragment' => ['https://example.com/test?foo=1#bar', ['fragment' => 'baz'], 'https://example.com/test?foo=1#baz'],
103
      ['//www.drupal.org', [], '//www.drupal.org'],
104 105 106 107 108 109 110 111 112 113
    ];
  }

  /**
   * @covers ::assemble
   * @covers::buildLocalUrl
   *
   * @dataProvider providerTestAssembleWithLocalUri
   */
  public function testAssembleWithLocalUri($uri, array $options, $subdir, $expected) {
114 115 116 117 118 119 120 121 122 123
    $this->setupRequestStack($subdir);

    $this->assertEquals($expected, $this->unroutedUrlAssembler->assemble($uri, $options));
  }

  /**
   * @return array
   */
  public function providerTestAssembleWithLocalUri() {
    return [
124 125
      ['base:example', [], FALSE, '/example'],
      ['base:example', ['query' => ['foo' => 'bar']], FALSE, '/example?foo=bar'],
126 127
      ['base:example', ['query' => ['foo' => '"bar"']], FALSE, '/example?foo=%22bar%22'],
      ['base:example', ['query' => ['foo' => '"bar"', 'zoo' => 'baz']], FALSE, '/example?foo=%22bar%22&zoo=baz'],
128
      ['base:example', ['fragment' => 'example'], FALSE, '/example#example'],
129 130
      ['base:example', [], TRUE, '/subdir/example'],
      ['base:example', ['query' => ['foo' => 'bar']], TRUE, '/subdir/example?foo=bar'],
131
      ['base:example', ['fragment' => 'example'], TRUE, '/subdir/example#example'],
132
      ['base:/drupal.org', [], FALSE, '/drupal.org'],
133 134 135 136 137 138 139 140 141 142
    ];
  }

  /**
   * @covers ::assemble
   */
  public function testAssembleWithNotEnabledProcessing() {
    $this->setupRequestStack(FALSE);
    $this->pathProcessor->expects($this->never())
      ->method('processOutbound');
143
    $result = $this->unroutedUrlAssembler->assemble('base:test-uri', []);
144 145 146 147 148 149 150 151
    $this->assertEquals('/test-uri', $result);
  }

  /**
   * @covers ::assemble
   */
  public function testAssembleWithEnabledProcessing() {
    $this->setupRequestStack(FALSE);
152
    $this->pathProcessor->expects($this->exactly(2))
153
      ->method('processOutbound')
154
      ->willReturnCallback(function ($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
155 156
        if ($bubbleable_metadata) {
          $bubbleable_metadata->setCacheContexts(['some-cache-context']);
157 158 159 160
        }
        return 'test-other-uri';
      });

161
    $result = $this->unroutedUrlAssembler->assemble('base:test-uri', ['path_processing' => TRUE]);
162
    $this->assertEquals('/test-other-uri', $result);
163 164 165 166 167 168

    $result = $this->unroutedUrlAssembler->assemble('base:test-uri', ['path_processing' => TRUE], TRUE);
    $expected_generated_url = new GeneratedUrl();
    $expected_generated_url->setGeneratedUrl('/test-other-uri')
      ->setCacheContexts(['some-cache-context']);
    $this->assertEquals($expected_generated_url, $result);
169 170 171 172 173 174 175 176 177
  }

  /**
   * Setups the request stack for a given subdir.
   *
   * @param string $subdir
   *   The wanted subdir.
   */
  protected function setupRequestStack($subdir) {
178 179 180 181 182 183 184
    $server = [];
    if ($subdir) {
      // Setup a fake request which looks like a Drupal installed under the
      // subdir "subdir" on the domain www.example.com.
      // To reproduce the values install Drupal like that and use a debugger.
      $server = [
        'SCRIPT_NAME' => '/subdir/index.php',
185
        'SCRIPT_FILENAME' => $this->root . '/index.php',
186 187
        'SERVER_NAME' => 'http://www.example.com',
      ];
188
      $request = Request::create('/subdir/');
189 190 191 192 193 194 195 196 197
    }
    else {
      $request = Request::create('/');
    }
    $request->server->add($server);
    $this->requestStack->push($request);
  }

}