Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / tests / src / Kernel / PathautoTokenTest.php
1 <?php
2
3 namespace Drupal\Tests\pathauto\Kernel;
4
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests tokens provided by Pathauto.
10  *
11  * @group pathauto
12  */
13 class PathautoTokenTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = array('system', 'token', 'pathauto');
21
22   public function testPathautoTokens() {
23
24     $this->installConfig(array('pathauto'));
25
26     $array = array(
27       'test first arg',
28       'The Array / value',
29     );
30
31     $tokens = array(
32       'join-path' => 'test-first-arg/array-value',
33     );
34     $data['array'] = $array;
35     $replacements = $this->assertTokens('array', $data, $tokens);
36
37     // Ensure that the cleanTokenValues() method does not alter this token value.
38     /* @var \Drupal\pathauto\AliasCleanerInterface $alias_cleaner */
39     $alias_cleaner = \Drupal::service('pathauto.alias_cleaner');
40     $alias_cleaner->cleanTokenValues($replacements, $data, array());
41     $this->assertEqual($replacements['[array:join-path]'], 'test-first-arg/array-value');
42
43     // Test additional token cleaning and its configuration.
44     $safe_tokens = $this->config('pathauto.settings')->get('safe_tokens');
45     $safe_tokens[] = 'safe';
46     $this->config('pathauto.settings')
47       ->set('safe_tokens', $safe_tokens)
48       ->save();
49
50     $safe_tokens = [
51       '[example:path]',
52       '[example:url]',
53       '[example:url-brief]',
54       '[example:login-url]',
55       '[example:login-url:relative]',
56       '[example:url:relative]',
57       '[example:safe]',
58     ];
59     $unsafe_tokens = [
60       '[example:path_part]',
61       '[example:something_url]',
62       '[example:unsafe]',
63     ];
64     foreach ($safe_tokens as $token) {
65       $replacements = [
66         $token => 'this/is/a/path',
67       ];
68       $alias_cleaner->cleanTokenValues($replacements);
69       $this->assertEquals('this/is/a/path', $replacements[$token], "Token $token cleaned.");
70     }
71     foreach ($unsafe_tokens as $token) {
72       $replacements = [
73         $token => 'This is not a / path',
74       ];
75       $alias_cleaner->cleanTokenValues($replacements);
76       $this->assertEquals('not-path', $replacements[$token], "Token $token not cleaned.");
77     }
78   }
79
80   /**
81    * Function copied from TokenTestHelper::assertTokens().
82    */
83   public function assertTokens($type, array $data, array $tokens, array $options = array()) {
84     $input = $this->mapTokenNames($type, array_keys($tokens));
85     $bubbleable_metadata = new BubbleableMetadata();
86     $replacements = \Drupal::token()->generate($type, $input, $data, $options, $bubbleable_metadata);
87     foreach ($tokens as $name => $expected) {
88       $token = $input[$name];
89       if (!isset($expected)) {
90         $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
91       }
92       elseif (!isset($replacements[$token])) {
93         $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
94       }
95       elseif (!empty($options['regex'])) {
96         $this->assertTrue(preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
97       }
98       else {
99         $this->assertIdentical($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
100       }
101     }
102
103     return $replacements;
104   }
105
106   public function mapTokenNames($type, array $tokens = array()) {
107     $return = array();
108     foreach ($tokens as $token) {
109       $return[$token] = "[$type:$token]";
110     }
111     return $return;
112   }
113
114 }