You are here

public function TokenTest::testGetInfo in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testGetInfo()
  2. 10 core/tests/Drupal/Tests/Core/Utility/TokenTest.php \Drupal\Tests\Core\Utility\TokenTest::testGetInfo()

@covers ::getInfo

File

core/tests/Drupal/Tests/Core/Utility/TokenTest.php, line 106

Class

TokenTest
@coversDefaultClass \Drupal\Core\Utility\Token @group Utility

Namespace

Drupal\Tests\Core\Utility

Code

public function testGetInfo() {
  $token_info = [
    'types' => [
      'foo' => [
        'name' => $this
          ->randomMachineName(),
      ],
    ],
  ];
  $this->language
    ->expects($this
    ->atLeastOnce())
    ->method('getId')
    ->will($this
    ->returnValue($this
    ->randomMachineName()));
  $this->languageManager
    ->expects($this
    ->once())
    ->method('getCurrentLanguage')
    ->with(LanguageInterface::TYPE_CONTENT)
    ->will($this
    ->returnValue($this->language));

  // The persistent cache must only be hit once, after which the info is
  // cached statically.
  $this->cache
    ->expects($this
    ->once())
    ->method('get');
  $this->cache
    ->expects($this
    ->once())
    ->method('set')
    ->with('token_info:' . $this->language
    ->getId(), $token_info);
  $this->moduleHandler
    ->expects($this
    ->once())
    ->method('invokeAll')
    ->with('token_info')
    ->will($this
    ->returnValue($token_info));
  $this->moduleHandler
    ->expects($this
    ->once())
    ->method('alter')
    ->with('token_info', $token_info);

  // Get the information for the first time. The cache should be checked, the
  // hooks invoked, and the info should be set to the cache should.
  $this->token
    ->getInfo();

  // Get the information for the second time. The data must be returned from
  // the static cache, so the persistent cache must not be accessed and the
  // hooks must not be invoked.
  $this->token
    ->getInfo();
}