You are here

public function BlockCacheTest::testCachePerRole in MongoDB 7

Test DRUPAL_CACHE_PER_ROLE.

File

mongodb_block_ui/src/Tests/BlockCacheTest.php, line 71

Class

BlockCacheTest
Test block caching.

Namespace

Drupal\mongodb_block_ui\Tests

Code

public function testCachePerRole() {
  $this
    ->setCacheMode(DRUPAL_CACHE_PER_ROLE);

  // Enable our test block. Set some content for it to display.
  $current_content = $this
    ->randomName();
  variable_set('block_test_content', $current_content);
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('');
  $this
    ->assertText($current_content, t('Block content displays.'));

  // Change the content, but the cached copy should still be served.
  $old_content = $current_content;
  $current_content = $this
    ->randomName();
  variable_set('block_test_content', $current_content);
  $this
    ->drupalGet('');
  $this
    ->assertText($old_content, t('Block is served from the cache.'));

  // Clear the cache and verify that the stale data is no longer there.
  cache_clear_all();
  $this
    ->drupalGet('');
  $this
    ->assertNoText($old_content, t('Block cache clear removes stale cache data.'));
  $this
    ->assertText($current_content, t('Fresh block content is displayed after clearing the cache.'));

  // Test whether the cached data is served for the correct users.
  $old_content = $current_content;
  $current_content = $this
    ->randomName();
  variable_set('block_test_content', $current_content);
  $this
    ->drupalLogout();
  $this
    ->drupalGet('');
  $this
    ->assertNoText($old_content, t('Anonymous user does not see content cached per-role for normal user.'));
  $this
    ->drupalLogin($this->normalUserAlt);
  $this
    ->drupalGet('');
  $this
    ->assertText($old_content, t('User with the same roles sees per-role cached content.'));
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('');
  $this
    ->assertNoText($old_content, t('Admin user does not see content cached per-role for normal user.'));
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('');
  $this
    ->assertText($old_content, t('Block is served from the per-role cache.'));
}