updated contrib modules : pathauto, token, entity_api, search_api, redirect, features

This commit is contained in:
2018-09-12 15:05:44 +02:00
parent b8e8cc8cee
commit a4746432c7
281 changed files with 4839 additions and 2453 deletions
@@ -2,7 +2,6 @@
namespace Drupal\token\Controller;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Controller\ControllerBase;
use Drupal\token\TreeBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -48,9 +47,9 @@ class TokenAutocompleteController extends ControllerBase {
public function autocomplete($token_type, $filter, Request $request) {
$filter = substr($filter, strrpos($filter, '['));
$matches = array();
$matches = [];
if (!Unicode::strlen($filter)) {
if (!mb_strlen($filter)) {
$matches["[{$token_type}:"] = 0;
}
else {
@@ -7,14 +7,14 @@ use Drupal\Core\Controller\ControllerBase;
/**
* Clears cache for tokens.
*/
class TokenCacheController extends ControllerBase {
class TokenCacheController extends ControllerBase {
/**
* Clear caches and redirect back to the frontpage.
*/
public function flush() {
token_clear_cache();
drupal_set_message(t('Token registry caches cleared.'));
$this->messenger()->addMessage($this->t('Token registry caches cleared.'));
return $this->redirect('<front>');
}
@@ -71,4 +71,5 @@ class RouteSubscriber extends RouteSubscriberBase {
$events[RoutingEvents::ALTER] = array('onAlterRoutes', 100);
return $events;
}
}
@@ -1,6 +1,7 @@
<?php
namespace Drupal\token\Tests;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
@@ -21,38 +22,38 @@ class TokenBlockTest extends TokenTestBase {
/**
* {@inheritdoc}
*/
public function setUp($modules = array()) {
public function setUp($modules = []) {
parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('access content', 'administer blocks'));
$this->admin_user = $this->drupalCreateUser(['access content', 'administer blocks']);
$this->drupalLogin($this->admin_user);
}
public function testBlockTitleTokens() {
$label = 'tokenblock';
$bundle = BlockContentType::create(array(
$bundle = BlockContentType::create([
'id' => $label,
'label' => $label,
'revision' => FALSE
));
]);
$bundle->save();
$block_content = BlockContent::create(array(
$block_content = BlockContent::create([
'type' => $label,
'label' => '[current-page:title] block title',
'info' => 'Test token title block',
'body[value]' => 'This is the test token title block.',
));
]);
$block_content->save();
$block = $this->drupalPlaceBlock('block_content:' . $block_content->uuid(), array(
$block = $this->drupalPlaceBlock('block_content:' . $block_content->uuid(), [
'label' => '[user:name]',
));
$this->drupalGet($block->urlInfo());
]);
$this->drupalGet($block->toUrl());
// Ensure that the link to available tokens is present and correctly
// positioned.
$this->assertLink('Browse available tokens.');
$this->assertText('This field supports tokens. Browse available tokens.');
$this->drupalPostForm(NULL, array(), t('Save block'));
$this->drupalPostForm(NULL, [], t('Save block'));
// Ensure token validation is working on the block.
$this->assertText('Title is using the following invalid tokens: [user:name].');
@@ -63,8 +64,8 @@ class TokenBlockTest extends TokenTestBase {
$block->save();
// Ensure that tokens are not double-escaped when output as a block title.
$this->drupalCreateContentType(array('type' => 'page'));
$node = $this->drupalCreateNode(array('title' => "Site's first node"));
$this->drupalCreateContentType(['type' => 'page']);
$node = $this->drupalCreateNode(['title' => "Site's first node"]);
$this->drupalGet('node/' . $node->id());
// The apostraphe should only be escaped once.
$this->assertRaw("Site&#039;s first node block title");
@@ -16,19 +16,19 @@ class TokenCurrentPageTest extends TokenTestBase {
*
* @var array
*/
public static $modules = array('node');
public static $modules = ['node'];
function testCurrentPageTokens() {
$tokens = array(
$tokens = [
'[current-page:title]' => t('Log in'),
'[current-page:url]' => Url::fromRoute('user.login', [], array('absolute' => TRUE))->toString(),
'[current-page:url:absolute]' => Url::fromRoute('user.login', [], array('absolute' => TRUE))->toString(),
'[current-page:url]' => Url::fromRoute('user.login', [], ['absolute' => TRUE])->toString(),
'[current-page:url:absolute]' => Url::fromRoute('user.login', [], ['absolute' => TRUE])->toString(),
'[current-page:url:relative]' => Url::fromRoute('user.login')->toString(),
'[current-page:url:path]' => '/user/login',
'[current-page:url:args:value:0]' => 'user',
'[current-page:url:args:value:1]' => 'login',
'[current-page:url:args:value:2]' => NULL,
'[current-page:url:unaliased]' => Url::fromRoute('user.login', [], array('absolute' => TRUE, 'alias' => TRUE))->toString(),
'[current-page:url:unaliased]' => Url::fromRoute('user.login', [], ['absolute' => TRUE, 'alias' => TRUE])->toString(),
'[current-page:page-number]' => 1,
'[current-page:query:foo]' => NULL,
'[current-page:query:bar]' => NULL,
@@ -36,20 +36,20 @@ class TokenCurrentPageTest extends TokenTestBase {
'[current-page:arg:0]' => 'user',
'[current-page:arg:1]' => 'login',
'[current-page:arg:2]' => NULL,
);
];
$this->assertPageTokens('user/login', $tokens);
$this->drupalCreateContentType(array('type' => 'page'));
$node = $this->drupalCreateNode(array('title' => 'Node title', 'path' => array('alias' => '/node-alias')));
$tokens = array(
$this->drupalCreateContentType(['type' => 'page']);
$node = $this->drupalCreateNode(['title' => 'Node title', 'path' => ['alias' => '/node-alias']]);
$tokens = [
'[current-page:title]' => 'Node title',
'[current-page:url]' => $node->url('canonical', array('absolute' => TRUE)),
'[current-page:url:absolute]' => $node->url('canonical', array('absolute' => TRUE)),
'[current-page:url:relative]' => $node->url(),
'[current-page:url]' => $node->toUrl('canonical', ['absolute' => TRUE])->toString(),
'[current-page:url:absolute]' => $node->toUrl('canonical', ['absolute' => TRUE])->toString(),
'[current-page:url:relative]' => $node->toUrl()->toString(),
'[current-page:url:alias]' => '/node-alias',
'[current-page:url:args:value:0]' => 'node-alias',
'[current-page:url:args:value:1]' => NULL,
'[current-page:url:unaliased]' => $node->url('canonical', array('absolute' => TRUE, 'alias' => TRUE)),
'[current-page:url:unaliased]' => $node->toUrl('canonical', ['absolute' => TRUE, 'alias' => TRUE])->toString(),
'[current-page:url:unaliased:args:value:0]' => 'node',
'[current-page:url:unaliased:args:value:1]' => $node->id(),
'[current-page:url:unaliased:args:value:2]' => NULL,
@@ -60,7 +60,7 @@ class TokenCurrentPageTest extends TokenTestBase {
'[current-page:arg:0]' => 'node',
'[current-page:arg:1]' => 1,
'[current-page:arg:2]' => NULL,
);
$this->assertPageTokens("/node/{$node->id()}", $tokens, array(), array('url_options' => array('query' => array('foo' => 'bar'))));
];
$this->assertPageTokens("/node/{$node->id()}", $tokens, [], ['url_options' => ['query' => ['foo' => 'bar']]]);
}
}
@@ -2,6 +2,8 @@
namespace Drupal\token\Tests;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
@@ -41,50 +43,50 @@ class TokenFieldUiTest extends TokenTestBase {
]);
$node_type->save();
entity_create('field_storage_config', array(
FieldStorageConfig::create([
'field_name' => 'field_body',
'entity_type' => 'node',
'type' => 'text_with_summary',
))->save();
entity_create('field_config', array(
])->save();
FieldConfig::create([
'field_name' => 'field_body',
'label' => 'Body',
'entity_type' => 'node',
'bundle' => 'article',
))->save();
entity_create('field_storage_config', array(
])->save();
FieldStorageConfig::create([
'field_name' => 'field_image',
'entity_type' => 'node',
'type' => 'image',
))->save();
entity_create('field_config', array(
])->save();
FieldConfig::create([
'field_name' => 'field_image',
'label' => 'Image',
'entity_type' => 'node',
'bundle' => 'article',
))->save();
entity_create('field_storage_config', array(
])->save();
FieldStorageConfig::create([
'field_name' => 'field_image_2',
'entity_type' => 'node',
'type' => 'image',
))->save();
entity_create('field_config', array(
])->save();
FieldConfig::create([
'field_name' => 'field_image_2',
'label' => 'Image 2',
'entity_type' => 'node',
'bundle' => 'article',
))->save();
entity_create('field_storage_config', array(
])->save();
FieldStorageConfig::create([
'field_name' => 'multivalued_field_image',
'entity_type' => 'node',
'type' => 'image',
))->save();
entity_create('field_config', array(
])->save();
FieldConfig::create([
'field_name' => 'multivalued_field_image',
'label' => 'Multivalued field image',
'entity_type' => 'node',
'bundle' => 'article',
))->save();
])->save();
entity_get_form_display('node', 'article', 'default')
->setComponent('field_body', [
@@ -35,11 +35,11 @@ class TokenMenuTest extends TokenTestBase {
// Make sure we have a body field on the node type.
$this->drupalCreateContentType(['type' => 'page']);
// Add a menu.
$menu = entity_create('menu', array(
$menu = Menu::create([
'id' => 'main-menu',
'label' => 'Main menu',
'description' => 'The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.',
));
]);
$menu->save();
// Place the menu block.
@@ -47,25 +47,25 @@ class TokenMenuTest extends TokenTestBase {
// Add a root link.
/** @var \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $root_link */
$root_link = entity_create('menu_link_content', array(
$root_link = MenuLinkContent::create([
'link' => ['uri' => 'internal:/admin'],
'title' => 'Administration',
'menu_name' => 'main-menu',
));
]);
$root_link->save();
// Add another link with the root link as the parent.
/** @var \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $parent_link */
$parent_link = entity_create('menu_link_content', array(
$parent_link = MenuLinkContent::create([
'link' => ['uri' => 'internal:/admin/config'],
'title' => 'Configuration',
'menu_name' => 'main-menu',
'parent' => $root_link->getPluginId(),
));
]);
$parent_link->save();
// Test menu link tokens.
$tokens = array(
$tokens = [
'id' => $parent_link->getPluginId(),
'title' => 'Configuration',
'menu' => 'Main menu',
@@ -73,13 +73,13 @@ class TokenMenuTest extends TokenTestBase {
'menu:machine-name' => $menu->id(),
'menu:description' => 'The <em>Main</em> menu is used on many sites to show the major sections of the site, often in a top navigation bar.',
'menu:menu-link-count' => '2',
'menu:edit-url' => Url::fromRoute('entity.menu.edit_form', ['menu' => 'main-menu'], array('absolute' => TRUE))->toString(),
'url' => Url::fromRoute('system.admin_config', [], array('absolute' => TRUE))->toString(),
'url:absolute' => Url::fromRoute('system.admin_config', [], array('absolute' => TRUE))->toString(),
'url:relative' => Url::fromRoute('system.admin_config', [], array('absolute' => FALSE))->toString(),
'menu:edit-url' => Url::fromRoute('entity.menu.edit_form', ['menu' => 'main-menu'], ['absolute' => TRUE])->toString(),
'url' => Url::fromRoute('system.admin_config', [], ['absolute' => TRUE])->toString(),
'url:absolute' => Url::fromRoute('system.admin_config', [], ['absolute' => TRUE])->toString(),
'url:relative' => Url::fromRoute('system.admin_config', [], ['absolute' => FALSE])->toString(),
'url:path' => '/admin/config',
'url:alias' => '/admin/config',
'edit-url' => Url::fromRoute('entity.menu_link_content.canonical', ['menu_link_content' => $parent_link->id()], array('absolute' => TRUE))->toString(),
'edit-url' => Url::fromRoute('entity.menu_link_content.canonical', ['menu_link_content' => $parent_link->id()], ['absolute' => TRUE])->toString(),
'parent' => 'Administration',
'parent:id' => $root_link->getPluginId(),
'parent:title' => 'Administration',
@@ -92,32 +92,32 @@ class TokenMenuTest extends TokenTestBase {
'root:id' => $root_link->getPluginId(),
'root:parent' => NULL,
'root:root' => NULL,
);
$this->assertTokens('menu-link', array('menu-link' => $parent_link), $tokens);
];
$this->assertTokens('menu-link', ['menu-link' => $parent_link], $tokens);
// Add a node.
$node = $this->drupalCreateNode();
// Allow main menu for this node type.
//$this->config('menu.entity.node.' . $node->getType())->set('available_menus', array('main-menu'))->save();
//$this->config('menu.entity.node.' . $node->getType())->set('available_menus', ['main-menu'])->save();
// Add a node menu link.
/** @var \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent $node_link */
$node_link = entity_create('menu_link_content', array(
'link' => ['uri' =>'entity:node/' . $node->id()],
$node_link = MenuLinkContent::create([
'link' => ['uri' => 'entity:node/' . $node->id()],
'title' => 'Node link',
'parent' => $parent_link->getPluginId(),
'menu_name' => 'main-menu',
));
]);
$node_link->save();
// Test [node:menu] tokens.
$tokens = array(
$tokens = [
'menu-link' => 'Node link',
'menu-link:id' => $node_link->getPluginId(),
'menu-link:title' => 'Node link',
'menu-link:menu' => 'Main menu',
'menu-link:url' => $node->url('canonical', ['absolute' => TRUE]),
'menu-link:url' => $node->toUrl('canonical', ['absolute' => TRUE])->toString(),
'menu-link:url:path' => '/node/' . $node->id(),
'menu-link:edit-url' => $node_link->url('edit-form', ['absolute' => TRUE]),
'menu-link:parent' => 'Configuration',
@@ -127,12 +127,12 @@ class TokenMenuTest extends TokenTestBase {
'menu-link:parents:keys' => $root_link->getPluginId() . ', ' . $parent_link->getPluginId(),
'menu-link:root' => 'Administration',
'menu-link:root:id' => $root_link->getPluginId(),
);
$this->assertTokens('node', array('node' => $node), $tokens);
];
$this->assertTokens('node', ['node' => $node], $tokens);
// Reload the node which will not have $node->menu defined and re-test.
$loaded_node = Node::load($node->id());
$this->assertTokens('node', array('node' => $loaded_node), $tokens);
$this->assertTokens('node', ['node' => $loaded_node], $tokens);
// Regression test for http://drupal.org/node/1317926 to ensure the
// original node object is not changed when calling menu_node_prepare().
@@ -148,11 +148,11 @@ class TokenMenuTest extends TokenTestBase {
'access administration pages',
]));
// Setup node type menu options.
$edit = array(
$edit = [
'menu_options[main-menu]' => 1,
'menu_options[main]' => 1,
'menu_parent' => 'main-menu:',
);
];
$this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
// Use a menu-link token in the body.
@@ -186,7 +186,7 @@ class TokenMenuTest extends TokenTestBase {
$select = reset($selects);
$options = $this->getAllOptions($select);
// Filter to items with title containing 'Test preview'.
$options = array_filter($options, function(\SimpleXMLElement $item) {
$options = array_filter($options, function (\SimpleXMLElement $item) {
return strpos((string) $item[0], 'Test preview') !== FALSE;
});
$this->assertEqual(1, count($options));
@@ -195,14 +195,14 @@ class TokenMenuTest extends TokenTestBase {
'body[0][value]' => 'This is a [node:menu-link:parent:url:path] token to the menu link parent',
'menu[enabled]' => 1,
'menu[title]' => 'Child link',
'menu[menu_parent]' => 'main-menu:' . $parent_link->getPluginId(),
'menu[menu_parent]' => 'main-menu:' . $parent_link->getPluginId(),
], t('Save'));
$node = $this->drupalGetNodeByTitle('Node menu title parent path test');
$this->assertEqual('This is a /admin/config token to the menu link parent', $node->body->value);
// Now edit the node and update the parent and title.
$this->drupalPostForm('node/' . $node->id() . '/edit', [
'menu[menu_parent]' => 'main-menu:' . $node_link->getPluginId(),
'menu[menu_parent]' => 'main-menu:' . $node_link->getPluginId(),
'title[0][value]' => 'Node menu title edit parent path test',
'body[0][value]' => 'This is a [node:menu-link:parent:url:path] token to the menu link parent',
], t('Save'));
@@ -218,7 +218,7 @@ class TokenMenuTest extends TokenTestBase {
$select = reset($selects);
$options = $this->getAllOptions($select);
// Filter to items with title containing 'Test preview'.
$options = array_filter($options, function(\SimpleXMLElement $item) {
$options = array_filter($options, function (\SimpleXMLElement $item) {
return strpos((string) $item[0], 'Child link') !== FALSE;
});
$this->assertEqual(1, count($options));
@@ -238,7 +238,7 @@ class TokenMenuTest extends TokenTestBase {
'body[0][value]' => 'This is a [node:menu-link:parent:url:path] token to the menu link parent',
'menu[enabled]' => 1,
'menu[title]' => 'Child link',
'menu[menu_parent]' => 'main-menu:' . $parent_link->getPluginId(),
'menu[menu_parent]' => 'main-menu:' . $parent_link->getPluginId(),
], t('Save'));
$node = $this->drupalGetNodeByTitle('Node menu adding menu later test', TRUE);
$this->assertEqual('This is a /admin/config token to the menu link parent', $node->body->value);
@@ -264,14 +264,14 @@ class TokenMenuTest extends TokenTestBase {
$this->assertText('page ' . $node_title . ' has been created');
$node = $this->drupalGetNodeByTitle($node_title);
$menu_ui_link1 = entity_create('menu_link_content', [
$menu_ui_link1 = MenuLinkContent::create([
'link' => ['uri' => 'entity:node/' . $node->id()],
'title' => 'menu link 1 provided by menu ui',
'menu_name' => 'main-menu',
]);
$menu_ui_link1->save();
$menu_ui_link2 = entity_create('menu_link_content', [
$menu_ui_link2 = MenuLinkContent::create([
'link' => ['uri' => 'entity:node/' . $node->id()],
'title' => 'menu link 2 provided by menu ui',
'menu_name' => 'main-menu',
@@ -306,7 +306,7 @@ class TokenMenuTest extends TokenTestBase {
]);
$node_type->save();
$permissions = array(
$permissions = [
'access administration pages',
'administer content translation',
'administer content types',
@@ -316,18 +316,18 @@ class TokenMenuTest extends TokenTestBase {
'edit any article content',
'translate any entity',
'administer menu',
);
];
$this->drupalLogin($this->drupalCreateUser($permissions));
// Enable translation for articles and menu links.
$this->drupalGet('admin/config/regional/content-language');
$edit = array(
$edit = [
'entity_types[node]' => TRUE,
'entity_types[menu_link_content]' => TRUE,
'settings[node][article][translatable]' => TRUE,
'settings[node][article][fields][title]' => TRUE,
'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
);
];
$this->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->assertText('Settings successfully updated.');
@@ -411,10 +411,10 @@ class TokenMenuTest extends TokenTestBase {
// - parent
// - child-1
// - child-1-1
Menu::create(array(
Menu::create([
'id' => 'menu_test',
'label' => 'Test menu',
))->save();
])->save();
$base_options = [
'provider' => 'menu_test',
'menu_name' => 'menu_test',
@@ -16,6 +16,6 @@ abstract class TokenTestBase extends WebTestBase {
*
* @var array
*/
public static $modules = array('path', 'token', 'token_module_test');
public static $modules = ['path', 'token', 'token_module_test'];
}
@@ -11,57 +11,57 @@ use Drupal\Core\Render\BubbleableMetadata;
*/
trait TokenTestTrait {
function assertToken($type, array $data, $token, $expected, array $options = array()) {
return $this->assertTokens($type, $data, array($token => $expected), $options);
function assertToken($type, array $data, $token, $expected, array $options = []) {
return $this->assertTokens($type, $data, [$token => $expected], $options);
}
function assertTokens($type, array $data, array $tokens, array $options = array()) {
function assertTokens($type, array $data, array $tokens, array $options = []) {
$input = $this->mapTokenNames($type, array_keys($tokens));
$bubbleable_metadata = new BubbleableMetadata();
$replacements = \Drupal::token()->generate($type, $input, $data, $options, $bubbleable_metadata);
foreach ($tokens as $name => $expected) {
$token = $input[$name];
if (!isset($expected)) {
$this->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
$this->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", ['@type' => $type, '@token' => $token]));
}
elseif (!isset($replacements[$token])) {
$this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
$this->fail(t("Token value for @token was not generated.", ['@type' => $type, '@token' => $token]));
}
elseif (!empty($options['regex'])) {
$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)));
$this->assertTrue(preg_match('/^' . $expected . '$/', $replacements[$token]), t("Token value for @token was '@actual', matching regular expression pattern '@expected'.", ['@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected]));
}
else {
$this->assertEqual($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
$this->assertEqual($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", ['@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected]));
}
}
return $replacements;
}
function mapTokenNames($type, array $tokens = array()) {
$return = array();
function mapTokenNames($type, array $tokens = []) {
$return = [];
foreach ($tokens as $token) {
$return[$token] = "[$type:$token]";
}
return $return;
}
function assertNoTokens($type, array $data, array $tokens, array $options = array()) {
function assertNoTokens($type, array $data, array $tokens, array $options = []) {
$input = $this->mapTokenNames($type, $tokens);
$bubbleable_metadata = new BubbleableMetadata();
$replacements = \Drupal::token()->generate($type, $input, $data, $options, $bubbleable_metadata);
foreach ($tokens as $name) {
$token = $input[$name];
$this->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
$this->assertTrue(!isset($replacements[$token]), t("Token value for @token was not generated.", ['@type' => $type, '@token' => $token]));
}
}
function saveAlias($source, $alias, $language = Language::LANGCODE_NOT_SPECIFIED) {
$alias = array(
$alias = [
'source' => $source,
'alias' => $alias,
'language' => $language,
);
];
\Drupal::service('path.alias_storage')->save($alias['source'], $alias['alias']);
return $alias;
}
@@ -74,22 +74,22 @@ trait TokenTestTrait {
/**
* Make a page request and test for token generation.
*/
function assertPageTokens($url, array $tokens, array $data = array(), array $options = array()) {
function assertPageTokens($url, array $tokens, array $data = [], array $options = []) {
if (empty($tokens)) {
return TRUE;
}
$token_page_tokens = array(
$token_page_tokens = [
'tokens' => $tokens,
'data' => $data,
'options' => $options,
);
];
\Drupal::state()->set('token_page_tokens', $token_page_tokens);
$options += array('url_options' => array());
$options += ['url_options' => []];
$this->drupalGet($url, $options['url_options']);
$this->refreshVariables();
$result = \Drupal::state()->get('token_page_tokens', array());
$result = \Drupal::state()->get('token_page_tokens', []);
if (!isset($result['values']) || !is_array($result['values'])) {
return $this->fail('Failed to generate tokens.');
@@ -97,13 +97,13 @@ trait TokenTestTrait {
foreach ($tokens as $token => $expected) {
if (!isset($expected)) {
$this->assertTrue(!isset($result['values'][$token]) || $result['values'][$token] === $token, t("Token value for @token was not generated.", array('@token' => $token)));
$this->assertTrue(!isset($result['values'][$token]) || $result['values'][$token] === $token, t("Token value for @token was not generated.", ['@token' => $token]));
}
elseif (!isset($result['values'][$token])) {
$this->fail(t('Failed to generate token @token.', array('@token' => $token)));
$this->fail(t('Failed to generate token @token.', ['@token' => $token]));
}
else {
$this->assertIdentical($result['values'][$token], (string) $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@token' => $token, '@actual' => $result['values'][$token], '@expected' => $expected)));
$this->assertIdentical($result['values'][$token], (string) $expected, t("Token value for @token was '@actual', expected value '@expected'.", ['@token' => $token, '@actual' => $result['values'][$token], '@expected' => $expected]));
}
}
}
@@ -16,7 +16,7 @@ class TokenURLTest extends TokenTestBase {
*
* @var array
*/
public static $modules = array('node');
public static $modules = ['node'];
/**
* {@inheritdoc}
@@ -27,25 +27,25 @@ class TokenURLTest extends TokenTestBase {
}
function testURLTokens() {
$url = new Url('entity.node.canonical', array('node' => 1));
$tokens = array(
$url = new Url('entity.node.canonical', ['node' => 1]);
$tokens = [
'absolute' => $url->setAbsolute()->toString(),
'relative' => $url->setAbsolute(FALSE)->toString(),
'path' => '/first-node',
'brief' => preg_replace(array('!^https?://!', '!/$!'), '', $url->setAbsolute()->toString()),
'brief' => preg_replace(['!^https?://!', '!/$!'], '', $url->setAbsolute()->toString()),
'args:value:0' => 'first-node',
'args:value:1' => NULL,
'args:value:N' => NULL,
'unaliased' => $url->setAbsolute()->setOption('alias', TRUE)->toString(),
'unaliased:relative' => $url->setAbsolute(FALSE)->setOption('alias', TRUE)->toString(),
'unaliased:path' => '/node/1',
'unaliased:brief' => preg_replace(array('!^https?://!', '!/$!'), '', $url->setAbsolute()->setOption('alias', TRUE)->toString()),
'unaliased:brief' => preg_replace(['!^https?://!', '!/$!'], '', $url->setAbsolute()->setOption('alias', TRUE)->toString()),
'unaliased:args:value:0' => 'node',
'unaliased:args:value:1' => '1',
'unaliased:args:value:2' => NULL,
// Deprecated tokens.
'alias' => '/first-node',
);
$this->assertTokens('url', array('url' => new Url('entity.node.canonical', array('node' => 1))), $tokens);
];
$this->assertTokens('url', ['url' => new Url('entity.node.canonical', ['node' => 1])], $tokens);
}
}
@@ -2,6 +2,7 @@
namespace Drupal\token\Tests;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\field\Entity\FieldStorageConfig;
@@ -24,7 +25,7 @@ class TokenUserTest extends TokenTestBase {
*
* @var array
*/
public static $modules = array('token_user_picture');
public static $modules = ['token_user_picture'];
/**
* {@inheritdoc}
@@ -32,10 +33,17 @@ class TokenUserTest extends TokenTestBase {
public function setUp() {
parent::setUp();
$this->account = $this->drupalCreateUser(['administer users', 'administer account settings', 'access content']);
$this->account = $this->drupalCreateUser([
'administer users',
'administer account settings',
'access content',
]);
$this->drupalLogin($this->account);
}
/**
* Tests the user releated tokens.
*/
public function testUserTokens() {
// Enable user pictures.
\Drupal::state()->set('user_pictures', 1);
@@ -49,7 +57,7 @@ class TokenUserTest extends TokenTestBase {
// Add a user picture to the account.
$image = current($this->drupalGetTestFiles('image'));
$edit = array('files[user_picture_0]' => drupal_realpath($image->uri));
$edit = ['files[user_picture_0]' => \Drupal::service('file_system')->realpath($image->uri)];
$this->drupalPostForm('user/' . $this->account->id() . '/edit', $edit, t('Save'));
$storage = \Drupal::entityTypeManager()->getStorage('user');
@@ -65,14 +73,14 @@ class TokenUserTest extends TokenTestBase {
];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$user_tokens = array(
$user_tokens = [
'picture' => $renderer->renderPlain($picture),
'picture:fid' => $this->account->user_picture->target_id,
'picture:size-raw' => 125,
'ip-address' => NULL,
'roles' => implode(', ', $this->account->getRoles()),
);
$this->assertTokens('user', array('user' => $this->account), $user_tokens);
];
$this->assertTokens('user', ['user' => $this->account], $user_tokens);
// Remove the simpletest-created user role.
$roles = $this->account->getRoles();
@@ -84,29 +92,32 @@ class TokenUserTest extends TokenTestBase {
$storage->resetCache();
$this->account = $storage->load($this->account->id());
$user_tokens = array(
$user_tokens = [
'picture' => NULL,
'picture:fid' => NULL,
'ip-address' => NULL,
'roles' => 'authenticated',
'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID,
);
$this->assertTokens('user', array('user' => $this->account), $user_tokens);
'roles:keys' => AccountInterface::AUTHENTICATED_ROLE,
];
$this->assertTokens('user', ['user' => $this->account], $user_tokens);
// The ip address token should work for the current user token type.
$tokens = array(
$tokens = [
'ip-address' => \Drupal::request()->getClientIp(),
);
$this->assertTokens('current-user', array(), $tokens);
];
$this->assertTokens('current-user', [], $tokens);
$anonymous = new AnonymousUserSession();
$tokens = array(
$tokens = [
'roles' => 'anonymous',
'roles:keys' => (string) DRUPAL_ANONYMOUS_RID,
);
$this->assertTokens('user', array('user' => $anonymous), $tokens);
'roles:keys' => AccountInterface::ANONYMOUS_ROLE,
];
$this->assertTokens('user', ['user' => $anonymous], $tokens);
}
/**
* Test user account settings.
*/
public function testUserAccountSettings() {
$this->drupalGet('admin/config/people/accounts');
$this->assertText('The list of available tokens that can be used in e-mails is provided below.');
@@ -137,7 +137,7 @@ class TreeTest extends TokenTestBase {
protected function getTokenTreeUrl($options = []) {
$this->drupalGet('token_module_test/browse');
$this->assertTitle('Available Tokens | Drupal');
$links = $this->xpath('//a[contains(@href, :href)]/@href', array(':href' => 'token/tree'));
$links = $this->xpath('//a[contains(@href, :href)]/@href', [':href' => 'token/tree']);
$link = $this->getAbsoluteUrl((string) current($links));
if (!empty($options)) {
$options = Json::encode($options);
@@ -65,9 +65,9 @@ class Token extends TokenBase implements TokenInterface {
}
$this->tokenInfo = $token_info;
$this->cache->set($cache_id, $this->tokenInfo, CacheBackendInterface::CACHE_PERMANENT, array(
$this->cache->set($cache_id, $this->tokenInfo, CacheBackendInterface::CACHE_PERMANENT, [
static::TOKEN_INFO_CACHE_TAG,
));
]);
}
}
@@ -136,7 +136,7 @@ class Token extends TokenBase implements TokenInterface {
*/
function getInvalidTokens($type, $tokens) {
$token_info = $this->getInfo();
$invalid_tokens = array();
$invalid_tokens = [];
foreach ($tokens as $token => $full_token) {
if (isset($token_info['tokens'][$type][$token])) {
@@ -162,7 +162,7 @@ class Token extends TokenBase implements TokenInterface {
}
else {
// Recursively check the chained tokens.
$sub_tokens = $this->findWithPrefix(array($token => $full_token), $parts[0]);
$sub_tokens = $this->findWithPrefix([$token => $full_token], $parts[0]);
$invalid_tokens = array_merge($invalid_tokens, $this->getInvalidTokens($sub_token_info['type'], $sub_tokens));
}
}
@@ -184,7 +184,7 @@ class Token extends TokenBase implements TokenInterface {
$valid_types = array_merge($valid_types, $this->getGlobalTokenTypes());
}
$invalid_tokens = array();
$invalid_tokens = [];
$value_tokens = is_string($value) ? $this->scan($value) : $value;
foreach ($value_tokens as $type => $tokens) {
@@ -61,12 +61,12 @@ class TreeBuilder implements TreeBuilderInterface {
$token_types = array_merge($token_types, $this->tokenService->getGlobalTokenTypes());
}
$element = array(
/*'#cache' => array(
'cid' => 'tree-rendered:' . hash('sha256', serialize(array('token_types' => $token_types, 'global_types' => NULL) + $variables)),
'tags' => array(Token::TOKEN_INFO_CACHE_TAG),
),*/
);
$element = [
/*'#cache' => [
'cid' => 'tree-rendered:' . hash('sha256', serialize(['token_types' => $token_types, 'global_types' => NULL] + $variables)),
'tags' => [Token::TOKEN_INFO_CACHE_TAG],
],*/
];
// @todo Find a way to use the render cache for this.
/*if ($cached_output = token_render_cache_get($element)) {