Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / aggregator / tests / src / Functional / ItemCacheTagsTest.php
1 <?php
2
3 namespace Drupal\Tests\aggregator\Functional;
4
5 use Drupal\aggregator\Entity\Feed;
6 use Drupal\aggregator\Entity\Item;
7 use Drupal\Core\Cache\CacheBackendInterface;
8 use Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase;
9 use Drupal\user\Entity\Role;
10 use Drupal\user\RoleInterface;
11
12 /**
13  * Tests the Item entity's cache tags.
14  *
15  * @group aggregator
16  */
17 class ItemCacheTagsTest extends EntityCacheTagsTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['aggregator'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     // Give anonymous users permission to access feeds, so that we can verify
31     // the cache tags of cached versions of feed items.
32     $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
33     $user_role->grantPermission('access news feeds');
34     $user_role->save();
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function createEntity() {
41     // Create a "Camelids" feed.
42     $feed = Feed::create([
43       'title' => 'Camelids',
44       'url' => 'https://groups.drupal.org/not_used/167169',
45       'refresh' => 900,
46       'checked' => 1389919932,
47       'description' => 'Drupal Core Group feed',
48     ]);
49     $feed->save();
50
51     // Create a "Llama" aggregator feed item.
52     $item = Item::create([
53       'fid' => $feed->id(),
54       'title' => t('Llama'),
55       'path' => 'https://www.drupal.org/',
56     ]);
57     $item->save();
58
59     return $item;
60   }
61
62   /**
63    * Tests that when creating a feed item, the feed tag is invalidated.
64    */
65   public function testEntityCreation() {
66     // Create a cache entry that is tagged with a feed cache tag.
67     \Drupal::cache('render')->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $this->entity->getCacheTags());
68
69     // Verify a cache hit.
70     $this->verifyRenderCache('foo', ['aggregator_feed:1']);
71
72     // Now create a feed item in that feed.
73     Item::create([
74       'fid' => $this->entity->getFeedId(),
75       'title' => t('Llama 2'),
76       'path' => 'https://groups.drupal.org/',
77     ])->save();
78
79     // Verify a cache miss.
80     $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new feed item invalidates the cache tag of the feed.');
81   }
82
83 }