View source
<?php
namespace Drupal\mongodb_block_ui\Tests;
class BlockCacheTest extends \DrupalWebTestCase {
protected $adminUser;
protected $normalUser;
protected $normalUserAlt;
public static function getInfo() {
return array(
'name' => 'Block caching',
'description' => 'Test block caching.',
'group' => 'MongoDB: Block',
);
}
public function setUp() {
parent::setUp('block_test');
$this->adminUser = $this
->drupalCreateUser(array(
'administer blocks',
'access administration pages',
));
$this
->drupalLogin($this->adminUser);
$this->normalUser = $this
->drupalCreateUser();
$this->normalUserAlt = $this
->drupalCreateUser();
user_save($this->normalUserAlt, array(
'roles' => $this->normalUser->roles,
));
$this->normalUserAlt->roles = $this->normalUser->roles;
variable_set('block_cache', TRUE);
$edit['block_test_test_cache[region]'] = 'sidebar_first';
$this
->drupalPost('admin/structure/block', $edit, t('Save blocks'));
}
public function testCachePerRole() {
$this
->setCacheMode(DRUPAL_CACHE_PER_ROLE);
$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.'));
$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.'));
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.'));
$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.'));
}
public function testCacheGlobal() {
$this
->setCacheMode(DRUPAL_CACHE_GLOBAL);
$current_content = $this
->randomName();
variable_set('block_test_content', $current_content);
$this
->drupalGet('');
$this
->assertText($current_content, t('Block content displays.'));
$old_content = $current_content;
$current_content = $this
->randomName();
variable_set('block_test_content', $current_content);
$this
->drupalLogout();
$this
->drupalGet('user');
$this
->assertText($old_content, t('Block content served from global cache.'));
}
public function testNoCache() {
$this
->setCacheMode(DRUPAL_NO_CACHE);
$current_content = $this
->randomName();
variable_set('block_test_content', $current_content);
$this
->drupalGet('');
$this
->assertText($current_content, t('Block content displays.'));
$current_content = $this
->randomName();
variable_set('block_test_content', $current_content);
$this
->drupalGet('');
$this
->assertText($current_content, t('DRUPAL_NO_CACHE prevents blocks from being cached.'));
}
public function testCachePerUser() {
$this
->setCacheMode(DRUPAL_CACHE_PER_USER);
$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.'));
$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 per-user cache.'));
$this
->drupalLogin($this->normalUserAlt);
$this
->drupalGet('');
$this
->assertText($current_content, t('Per-user block cache is not served for other users.'));
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('');
$this
->assertText($old_content, t('Per-user block cache is persistent.'));
}
public function testCachePerPage() {
$this
->setCacheMode(DRUPAL_CACHE_PER_PAGE);
$current_content = $this
->randomName();
variable_set('block_test_content', $current_content);
$this
->drupalGet('node');
$this
->assertText($current_content, t('Block content displays on the node page.'));
$old_content = $current_content;
$current_content = $this
->randomName();
variable_set('block_test_content', $current_content);
$this
->drupalGet('user');
$this
->assertNoText($old_content, t('Block content cached for the node page does not show up for the user page.'));
$this
->drupalGet('node');
$this
->assertText($old_content, t('Block content cached for the node page.'));
}
protected function setCacheMode($cache_mode) {
db_update('block')
->fields(array(
'cache' => $cache_mode,
))
->condition('module', 'block_test')
->execute();
$current_mode = db_query("SELECT cache FROM {block} WHERE module = 'block_test'")
->fetchField();
if ($current_mode != $cache_mode) {
$this
->fail(t('Unable to set cache mode to %mode. Current mode: %current_mode', array(
'%mode' => $cache_mode,
'%current_mode' => $current_mode,
)));
}
}
}