3 namespace Drupal\Tests\token\Kernel;
6 use Symfony\Component\HttpFoundation\Request;
9 * Test generic url token replacements.
13 class UrlTest extends KernelTestBase {
18 * @var \Drupal\Core\Utility\Token
23 * The current request stack.
25 * @var \Symfony\Component\HttpFoundation\RequestStack
27 protected $requestStack;
30 * The current route match.
32 * @var \Drupal\Core\Routing\CurrentRouteMatch
34 protected $currentRouteMatch;
39 protected function setUp() {
41 $this->token = $this->container->get('token');
42 $this->requestStack = $this->container->get('request_stack');
43 $this->currentRouteMatch = $this->container->get('current_route_match');
47 * Test the url token replacements for current requests.
49 * The method ::expectedCurrentRequestUrlResults() is not declared
50 * as a regular data provider, because it might use services from
51 * the global Drupal container, which is not initialized yet during
52 * the invocation of data providers.
54 public function testCurrentRequestUrls() {
55 foreach ($this->expectedCurrentRequestUrlResults() as $data_set) {
56 list ($request, $text, $data, $options, $expected_output) = $data_set;
57 // Set the request as the current one.
58 $this->requestStack->pop();
59 $this->requestStack->push($request);
60 $this->currentRouteMatch->resetRouteMatch();
62 $this->assertEquals($expected_output, $this->token->replace($text, $data, $options));
67 * Provides a list of results to expect for ::testRequestUrls().
69 * Each data set of this array holds the following order:
70 * - The request object to test for.
71 * - The input text as string.
72 * - The token data as array.
73 * - Further options for the token replacement as array.
74 * - The output to expect after token replacement.
77 * The list of results to expect.
79 public function expectedCurrentRequestUrlResults() {
81 [Request::createFromGlobals(), '[current-page:url]', [], [], Url::createFromRequest(Request::createFromGlobals())->setAbsolute()->toString()],
82 [Request::create('/should-not-exist'), '[current-page:url:path]', [], [], '/'],
83 [Request::create('/https://drupal.org/'), '[current-page:url:absolute]', [], [], '[current-page:url:absolute]'],
84 [Request::create('/https://drupal.org/'), '[current-page:url:absolute]', [], ['clear' => TRUE], ''],