diff --git a/token_example/src/Controller/TokenExampleController.php b/token_example/src/Controller/TokenExampleController.php
index 4c8b42d..ce14526 100644
--- a/token_example/src/Controller/TokenExampleController.php
+++ b/token_example/src/Controller/TokenExampleController.php
@@ -1,16 +1,16 @@
$this->tokenManager->replace("
This is sample text with simple token: [token_example:simple]
")
+ '#markup' => $this->tokenManager->replace("This is sample text with simple token: [token_example:simple]
"),
];
$build['with-type'] = [
- '#markup' => $this->tokenManager->replace("This is sample text with token of date type. Current timestamp is: [token_example:with-type]
")
+ '#markup' => $this->tokenManager->replace("This is sample text with token of date type. Current timestamp is: [token_example:with-type]
"),
];
$build['dynamic'] = [
- '#markup' => $this->tokenManager->replace("This is sample text with dynamic token with random number as suffix: [token_example:dynamic:bar]
")
+ '#markup' => $this->tokenManager->replace("This is sample text with dynamic token with random number as suffix: [token_example:dynamic:bar]
"),
];
$build['token_example_type'] = [
- '#markup' => $this->tokenManager->replace("Token example data type: [token_example_type:bar]
")
+ '#markup' => $this->tokenManager->replace("Token example data type: [token_example_type:bar]
"),
];
return $build;
diff --git a/token_example/src/Tests/TokenExampleTest.php b/token_example/src/Tests/TokenExampleTest.php
deleted file mode 100644
index 2fa1d80..0000000
--- a/token_example/src/Tests/TokenExampleTest.php
+++ /dev/null
@@ -1,44 +0,0 @@
-drupalCreateUser(array('access toolbar'));
- $this->drupalLogin($user);
-
- // Check that token_example module is registerd in example module tray.
- $this->assertRaw('');
- $this->assertLink('Token example');
-
- // Check for route registered by token_example module.
- $this->clickLink('Token example');
- $this->assertResponse(200, 'Path registered by token_example exist');
- }
-
-}
diff --git a/token_example/tests/src/Functional/TokenExampleTest.php b/token_example/tests/src/Functional/TokenExampleTest.php
new file mode 100644
index 0000000..73bba64
--- /dev/null
+++ b/token_example/tests/src/Functional/TokenExampleTest.php
@@ -0,0 +1,45 @@
+assertSession();
+
+ $this->drupalLogin(
+ $this->drupalCreateUser(['access content', 'access toolbar'])
+ );
+
+ $this->drupalGet(Url::fromRoute(''));
+ // Check that token_example module is registerd in example module tray.
+ $assert->responseContains('');
+ $assert->linkExists('Token example');
+
+ // Check for route registered by token_example module.
+ $this->drupalGet(Url::fromRoute('token_example.example'));
+ $assert->statusCodeEquals(200);
+ }
+
+}
diff --git a/token_example/tests/src/Kernel/ExampleTokensTest.php b/token_example/tests/src/Kernel/ExampleTokensTest.php
new file mode 100644
index 0000000..a4001e1
--- /dev/null
+++ b/token_example/tests/src/Kernel/ExampleTokensTest.php
@@ -0,0 +1,80 @@
+tokenManager = $this->container->get('token');
+ }
+
+ public function provideTokens() {
+ return [
+ ['foo', '[token_example:simple]'],
+ ['baz', '[token_example_type:bar]'],
+ ];
+ }
+
+ /**
+ * Test direct string replacement tokens.
+ *
+ * @dataProvider provideTokens
+ */
+ public function testReplacementTokens($expected, $token) {
+ $this->assertEquals($expected, $this->tokenManager->replace($token));
+ }
+
+ /**
+ * Test the with-type token, which adds a timestamp.
+ */
+ public function testWithType() {
+ $token = '[token_example:with-type]';
+ $replacement = $this->tokenManager->replace($token);
+ // Assert that we don't end up with the same string we started with. Since
+ // the result is a timestamp, we can't check it for accuracy, but we can
+ // check that it is numeric.
+ $this->assertNotEquals($replacement, $token);
+ $this->assertFalse(is_numeric($token));
+ $this->assertTrue(is_numeric($replacement));
+ }
+
+ /**
+ * Test the dynamic token which adds a random string to the given string.
+ */
+ public function testDynamic() {
+ $string = 'bar';
+ $token = "[token_example:dynamic:$string]";
+ $replacement = $this->tokenManager->replace($token);
+ // Assert that the replacement string is not the same, but starts with the
+ // original string.
+ $this->assertNotEquals($token, $replacement);
+ $this->assertEquals(0, strpos($replacement, $string));
+ }
+
+}
diff --git a/token_example/token_example.info.yml b/token_example/token_example.info.yml
index 0e2a9ec..ed8b1ca 100644
--- a/token_example/token_example.info.yml
+++ b/token_example/token_example.info.yml
@@ -4,4 +4,4 @@ description: 'An example module showing how to define and use tokens.'
package: Example modules
core: 8.x
dependencies:
- - examples
+ - examples:examples
diff --git a/token_example/token_example.module b/token_example/token_example.module
index 7d0944a..4238a6a 100644
--- a/token_example/token_example.module
+++ b/token_example/token_example.module
@@ -9,7 +9,13 @@ use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\RouteMatchInterface;
/**
- * Implements hook_help().
+ * @defgroup token_example Example: Token
+ * @ingroup examples
+ * @{
+ */
+
+/**
+ * Implements hook_help().
*/
function token_example_help($route_name, RouteMatchInterface $route_match) {
if ($route_name == 'help.page.token_example') {
@@ -42,17 +48,17 @@ function token_example_token_info() {
$token_example['with-type'] = [
'name' => t("Token with type"),
'description' => t("Token with type of value as UNIX timestamp."),
- 'type' => 'date'
+ 'type' => 'date',
];
$token_example['dynamic'] = [
'name' => t('Dynamic'),
'description' => t("A token with dynamic value"),
];
- return array(
+ return [
'types' => $types,
- 'tokens' => array('token_example' => $token_example),
- );
+ 'tokens' => ['token_example' => $token_example],
+ ];
}
/**
@@ -87,7 +93,7 @@ function token_example_tokens($type, $tokens, array $data, array $options, Bubbl
}
if ($type == 'token_example_type') {
- // generate replacement of type token_example.
+ // Generate replacement of type token_example.
$replacements = $token_service->generate('token_example_foo', $tokens, ['token_example_foo' => ['bar' => 'baz']], $options, $bubbleable_metadata);
}
@@ -102,3 +108,7 @@ function token_example_tokens($type, $tokens, array $data, array $options, Bubbl
return $replacements;
}
+
+/**
+ * @} End of "defgroup token_example".
+ */
diff --git a/token_example/token_example.routing.yml b/token_example/token_example.routing.yml
index bcd6be8..d60ca45 100644
--- a/token_example/token_example.routing.yml
+++ b/token_example/token_example.routing.yml
@@ -3,4 +3,4 @@ token_example.example:
defaults:
_controller: '\Drupal\token_example\Controller\TokenExampleController::getToken'
requirements:
- _access: 'TRUE'
+ _permission: 'access content'