Beispiel #1
0
 /**
  * Test that tokens are generated only for content entities.
  */
 public function testContentEntityOnlyTokens()
 {
     // Verify that type and token info for a config entity is not generated.
     $this->assertNull($this->tokenService->getTokenInfo('user_role', 'original'));
     $this->assertNull($this->tokenService->getTokenInfo('user_role', 'url'));
     $this->assertNull($this->tokenService->getTypeInfo('user_role'));
 }
 /**
  * Test invalid tokens.
  */
 public function testGetInvalidTokens()
 {
     $tests = array();
     $tests[] = array('valid tokens' => array('[node:title]', '[node:created:short]', '[node:created:custom:invalid]', '[node:created:custom:mm-YYYY]', '[node:colons:in:name]', '[site:name]', '[site:slogan]', '[current-date:short]', '[current-user:uid]', '[current-user:ip-address]'), 'invalid tokens' => array('[node:title:invalid]', '[node:created:invalid]', '[node:created:short:invalid]', '[node:colons:in:name:invalid]', '[invalid:title]', '[site:invalid]', '[user:ip-address]', '[user:uid]', '[comment:cid]', '[node:tnid]', '[node:type]', '[node:type-name]', '[date:short]'), 'types' => array('node'));
     $tests[] = array('valid tokens' => array('[node:title]', '[node:created:short]', '[node:created:custom:invalid]', '[node:created:custom:mm-YYYY]', '[node:colons:in:name]', '[site:name]', '[site:slogan]', '[user:uid]', '[current-date:short]', '[current-user:uid]'), 'invalid tokens' => array('[node:title:invalid]', '[node:created:invalid]', '[node:created:short:invalid]', '[node:colons:in:name:invalid]', '[invalid:title]', '[site:invalid]', '[user:ip-address]', '[comment:cid]', '[node:tnid]', '[node:type]', '[node:type-name]'), 'types' => array('all'));
     foreach ($tests as $test) {
         $tokens = array_merge($test['valid tokens'], $test['invalid tokens']);
         shuffle($tokens);
         $invalid_tokens = $this->tokenService->getInvalidTokensByContext(implode(' ', $tokens), $test['types']);
         sort($invalid_tokens);
         sort($test['invalid tokens']);
         $this->assertEqual($invalid_tokens, $test['invalid tokens'], 'Invalid tokens detected properly: ' . implode(', ', $invalid_tokens));
     }
 }
 /**
  * Generate a token tree.
  *
  * @param string $token_type
  *   The token type.
  * @param array $options
  *   An associative array of additional options. See documentation for
  *   TreeBuilderInterface::buildTree() for more information.
  *
  * @return array
  *   The token data for the specified $token_type.
  *
  * @internal
  */
 protected function getTokenData($token_type, array $options)
 {
     $options += ['parents' => []];
     $info = $this->tokenService->getInfo();
     if ($options['depth'] <= 0 || !isset($info['types'][$token_type]) || !isset($info['tokens'][$token_type])) {
         return [];
     }
     $tree = [];
     foreach ($info['tokens'][$token_type] as $token => $token_info) {
         // Build the raw token string.
         $token_parents = $options['parents'];
         if (empty($token_parents)) {
             // If the parents array is currently empty, assume the token type is its
             // parent.
             $token_parents[] = $token_type;
         } elseif (in_array($token, array_slice($token_parents, 1), TRUE)) {
             // Prevent duplicate recursive tokens. For example, this will prevent
             // the tree from generating the following tokens or deeper:
             // [comment:parent:parent]
             // [comment:parent:root:parent]
             continue;
         }
         $token_parents[] = $token;
         if (!empty($token_info['dynamic'])) {
             $token_parents[] = '?';
         }
         $raw_token = '[' . implode(':', $token_parents) . ']';
         $tree[$raw_token] = $token_info;
         $tree[$raw_token]['raw token'] = $raw_token;
         // Add the token's real name (leave out the base token type).
         $tree[$raw_token]['token'] = implode(':', array_slice($token_parents, 1));
         // Add the token's parent as its raw token value.
         if (!empty($options['parents'])) {
             $tree[$raw_token]['parent'] = '[' . implode(':', $options['parents']) . ']';
         }
         // Fetch the child tokens.
         if (!empty($token_info['type'])) {
             $child_options = $options;
             $child_options['depth']--;
             $child_options['parents'] = $token_parents;
             $tree[$raw_token]['children'] = $this->getTokenData($token_info['type'], $child_options);
         }
     }
     return $tree;
 }