function BlockTest::testBlockRehash

Test _block_rehash().

File

drupal/core/modules/block/lib/Drupal/block/Tests/BlockTest.php, line 208
Contains \Drupal\block\Tests\BlockTest.

Class

BlockTest
Provides testing for basic block module functionality.

Namespace

Drupal\block\Tests

Code

function testBlockRehash() {
  module_enable(array(
    'block_test',
  ));
  $this
    ->assertTrue(module_exists('block_test'), 'Test block module enabled.');

  // Clear the block cache to load the block_test module's block definitions.
  $this->container
    ->get('plugin.manager.block')
    ->clearCachedDefinitions();

  // Add a test block.
  $block = array();
  $block['id'] = 'test_cache';
  $block['machine_name'] = strtolower($this
    ->randomName(8));
  $block['theme'] = config('system.theme')
    ->get('default');
  $block['region'] = 'header';
  $block = $this
    ->drupalPlaceBlock('test_cache', array(
    'region' => 'header',
  ));

  // Our test block's caching should default to DRUPAL_CACHE_PER_ROLE.
  $settings = $block
    ->get('settings');
  $this
    ->assertEqual($settings['cache'], DRUPAL_CACHE_PER_ROLE, 'Test block cache mode defaults to DRUPAL_CACHE_PER_ROLE.');

  // Disable caching for this block.
  $block
    ->getPlugin()
    ->setConfig('cache', DRUPAL_NO_CACHE);
  $block
    ->save();

  // Flushing all caches should call _block_rehash().
  $this
    ->resetAll();

  // Verify that block is updated with the new caching mode.
  $block = entity_load('block', $block
    ->id());
  $settings = $block
    ->get('settings');
  $this
    ->assertEqual($settings['cache'], DRUPAL_NO_CACHE, "Test block's database entry updated to DRUPAL_NO_CACHE.");
}