3 namespace Drupal\Tests\pathauto\Kernel;
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests tokens provided by Pathauto.
13 class PathautoTokenTest extends KernelTestBase {
20 public static $modules = array('system', 'token', 'pathauto');
22 public function testPathautoTokens() {
24 $this->installConfig(array('pathauto'));
32 'join-path' => 'test-first-arg/array-value',
34 $data['array'] = $array;
35 $replacements = $this->assertTokens('array', $data, $tokens);
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');
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)
53 '[example:url-brief]',
54 '[example:login-url]',
55 '[example:login-url:relative]',
56 '[example:url:relative]',
60 '[example:path_part]',
61 '[example:something_url]',
64 foreach ($safe_tokens as $token) {
66 $token => 'this/is/a/path',
68 $alias_cleaner->cleanTokenValues($replacements);
69 $this->assertEquals('this/is/a/path', $replacements[$token], "Token $token cleaned.");
71 foreach ($unsafe_tokens as $token) {
73 $token => 'This is not a / path',
75 $alias_cleaner->cleanTokenValues($replacements);
76 $this->assertEquals('not-path', $replacements[$token], "Token $token not cleaned.");
81 * Function copied from TokenTestHelper::assertTokens().
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)));
92 elseif (!isset($replacements[$token])) {
93 $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
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)));
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)));
103 return $replacements;
106 public function mapTokenNames($type, array $tokens = array()) {
108 foreach ($tokens as $token) {
109 $return[$token] = "[$type:$token]";