Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / tests / src / Kernel / UrlTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Kernel;
4
5 use Drupal\Core\Url;
6 use Symfony\Component\HttpFoundation\Request;
7
8 /**
9  * Test generic url token replacements.
10  *
11  * @group token
12  */
13 class UrlTest extends KernelTestBase {
14
15   /**
16    * The token service.
17    *
18    * @var \Drupal\Core\Utility\Token
19    */
20   protected $token;
21
22   /**
23    * The current request stack.
24    *
25    * @var \Symfony\Component\HttpFoundation\RequestStack
26    */
27   protected $requestStack;
28
29   /**
30    * The current route match.
31    *
32    * @var \Drupal\Core\Routing\CurrentRouteMatch
33    */
34   protected $currentRouteMatch;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::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');
44   }
45
46   /**
47    * Test the url token replacements for current requests.
48    *
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.
53    */
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();
61
62       $this->assertEquals($expected_output, $this->token->replace($text, $data, $options));
63     }
64   }
65
66   /**
67    * Provides a list of results to expect for ::testRequestUrls().
68    *
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.
75    *
76    * @return array
77    *   The list of results to expect.
78    */
79   public function expectedCurrentRequestUrlResults() {
80     return [
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], ''],
85     ];
86   }
87
88 }