diff --git a/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php b/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
index c3e7af6..d698c4e 100644
--- a/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
@@ -2,8 +2,10 @@
 
 namespace Drupal\KernelTests\Core\Theme;
 
+use Drupal\Component\Utility\Crypt;
 use Drupal\Component\Utility\Html;
 use Drupal\Core\Site\Settings;
+use Drupal\Core\Template\TwigPhpStorageCache;
 use Drupal\KernelTests\KernelTestBase;
 
 /**
@@ -87,7 +89,7 @@ public function testInlineTemplate() {
 
     $cache = $environment->getCache();
     $class = $environment->getTemplateClass($name);
-    $expected = $prefix . '_inline-template' . '_' . hash('sha256', $class);
+    $expected = $prefix . '_inline-template_' . substr(Crypt::hashBase64($class), 0, TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH);
     $this->assertEqual($expected, $cache->generateKey($name, $class));
   }
 
@@ -116,6 +118,19 @@ public function testCacheFilename() {
     // static cache.
     $environment = \Drupal::service('twig');
 
+    // A template basename greater than the constant
+    // TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH should get truncated.
+    $cache = $environment->getCache();
+    $long_name = 'core/modules/system/templates/block--system-messages-block.html.twig';
+    $this->assertGreaterThan(TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH, strlen(basename($long_name)));
+    $class = $environment->getTemplateClass($long_name);
+    $key = $cache->generateKey($long_name, $class);
+    $hash = $this->container->getParameter('twig_extension_hash');
+    // The key should consist of the hash, two underscores, and two strings
+    // truncated to length TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH.
+    $expected = strlen($hash) + 2 + 2 * TwigPhpStorageCache::SUFFIX_SUBSTRING_LENGTH;
+    $this->assertEquals($expected, strlen($key));
+
     $original_filename = $environment->getCacheFilename('core/modules/system/templates/container.html.twig');
     \Drupal::getContainer()->set('twig', NULL);
 
