UnroutedUrlAssemblerTest.php 6.36 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->getMock('Drupal\Core\PathProcessor\OutboundPathProcessorInterface');
54
    $this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor);
55 56 57 58 59 60 61 62 63 64
  }

  /**
   * @covers ::assemble
   * @expectedException \InvalidArgumentException
   */
  public function testAssembleWithNeitherExternalNorDomainLocalUri() {
    $this->unroutedUrlAssembler->assemble('wrong-url');
  }

65 66 67 68 69 70 71 72
  /**
   * @covers ::assemble
   * @expectedException \InvalidArgumentException
   */
  public function testAssembleWithLeadingSlash() {
    $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
   $this->setupRequestStack(FALSE);
81
   $this->assertEquals($expected, $this->unroutedUrlAssembler->assemble($uri, $options));
82 83
   $generated_url = $this->unroutedUrlAssembler->assemble($uri, $options, TRUE);
   $this->assertEquals($expected, $generated_url->getGeneratedUrl());
84
   $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
      ['//www.drupal.org', [], '//www.drupal.org'],
100 101 102 103 104 105 106 107 108 109
    ];
  }

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

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

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

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

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

157
    $result = $this->unroutedUrlAssembler->assemble('base:test-uri', ['path_processing' => TRUE]);
158
    $this->assertEquals('/test-other-uri', $result);
159 160 161 162 163 164

    $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);
165 166 167 168 169 170 171 172 173
  }

  /**
   * Setups the request stack for a given subdir.
   *
   * @param string $subdir
   *   The wanted subdir.
   */
  protected function setupRequestStack($subdir) {
174 175 176 177 178 179 180
    $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',
181
        'SCRIPT_FILENAME' => $this->root . '/index.php',
182 183
        'SERVER_NAME' => 'http://www.example.com',
      ];
184
      $request = Request::create('/subdir/');
185 186 187 188 189 190 191 192 193
    }
    else {
      $request = Request::create('/');
    }
    $request->server->add($server);
    $this->requestStack->push($request);
  }

}