 .../lib/Drupal/taxonomy/Tests/TermTest.php         |   99 ++++++++++++++++++++
 1 file changed, 99 insertions(+)

diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php
index a84f400..70c752f 100644
--- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\taxonomy\Tests;
 
+use Drupal\Core\Cache\Cache;
 use Drupal\Component\Utility\Json;
 use Drupal\Component\Utility\Tags;
 use Drupal\Core\Field\FieldDefinitionInterface;
@@ -543,4 +544,102 @@ function testReSavingTags() {
     $this->assertRaw($term->label(), 'Term is displayed after saving the node with no changes.');
   }
 
+  /**
+   * Tests cache tags presence and invalidation.
+   *
+   * Tests the following cache tags:
+   * - "taxonomy_term_view:1"
+   * - "taxonomy_term:<term ID>"
+   */
+  public function testCacheTagsPresenceAndInvalidation() {
+    // Enable page caching.
+    $config = \Drupal::config('system.performance');
+    $config->set('cache.page.use_internal', 1);
+    $config->set('cache.page.max_age', 300);
+    $config->save();
+
+    // Page caching only works for anonymous users.
+    $this->drupalLogout();
+
+    // Create a "Camelids" vocabulary
+    $vocabulary = entity_create('taxonomy_vocabulary',  array(
+      'name' => 'camelids',
+      'vid' => 'camelids',
+    ));
+    $vocabulary->save();
+
+    // Create a "Llama" taxonomy term.
+    $term = entity_create('taxonomy_term', array(
+      'name' => 'Llama',
+      'vid' => $vocabulary->id(),
+    ));
+    $term->save();
+    $term_path = 'taxonomy/term/' . $term->id();
+
+    // Prime the page cache.
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
+
+    // Verify a cache hit, but also the presence of the correct cache tags.
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
+    $cid_parts = array(url($term_path, array('absolute' => TRUE)), 'html');
+    $cid = sha1(implode(':', $cid_parts));
+    $cache_entry = \Drupal::cache('page')->get($cid);
+    $this->assertIdentical($cache_entry->tags, array('content:1', 'taxonomy_term_view:1', 'taxonomy_term:' . $term->id()));
+
+    // Also verify the existence of an entity render cache entry.
+    $cid = 'entity_view:taxonomy_term:' . $term->id() . ':full:stark:r.anonymous';
+    $cache_entry = \Drupal::cache()->get($cid);
+    $this->assertIdentical($cache_entry->tags, array('taxonomy_term_view:1', 'taxonomy_term:' . $term->id()));
+
+    // The "Llama" term is modified.
+    $term->description = 'The name "llama" was adopted by European settlers from native Peruvians.';
+    $term->save();
+
+    // Verify that after modifying the term, there is a cache miss.
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
+
+    // Verify a cache hit.
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
+
+    // Verify that after modifying the Term entity's default display, there is
+    // a cache miss.
+    $entity_display = entity_create('entity_display', array(
+      'targetEntityType' => 'taxonomy_term',
+      'bundle' => 'camelids',
+      'mode' => 'default',
+      'status' => TRUE,
+    ));
+    $entity_display->save();
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
+
+    // Verify a cache hit.
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
+
+    // Verify that after invalidating the taxonomy term's cache tag directly,
+    // there is a cache miss.
+    Cache::invalidateTags(array('taxonomy_term' => array($term->id())));
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
+
+    // Verify a cache hit.
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
+
+    // Verify that after invalidating the generic taxonomy term view cache tag
+    // directly, there is a cache miss.
+    Cache::invalidateTags(array('taxonomy_term_view' => TRUE));
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
+
+    // Verify a cache hit.
+    $this->drupalGet($term_path);
+    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
+  }
+
 }
