1 common.test | CommonBackdropRenderTestCase::testBackdropRenderCache() |
Tests caching of render items.
File
- core/
modules/ simpletest/ tests/ common.test, line 2280 - Tests for common.inc functionality.
Class
- CommonBackdropRenderTestCase
- Tests for backdrop_render().
Code
function testBackdropRenderCache() {
// Force a request via GET.
$request_method = $_SERVER['REQUEST_METHOD'];
$_SERVER['REQUEST_METHOD'] = 'GET';
// Create an empty element.
$test_element = array(
'#cache' => array(
'cid' => 'render_cache_test',
),
'#markup' => '',
);
// Render the element and confirm that it goes through the rendering
// process (which will set $element['#printed']).
$element = $test_element;
backdrop_render($element);
$this->assertTrue(isset($element['#printed']), 'No cache hit');
// Render the element again and confirm that it is retrieved from the cache
// instead (so $element['#printed'] will not be set).
$element = $test_element;
backdrop_render($element);
$this->assertFalse(isset($element['#printed']), 'Cache hit');
// Test that user 1 does not share the cache with other users who have the
// same roles, even when BACKDROP_CACHE_PER_ROLE is used.
$user1 = user_load(1);
$first_authenticated_user = $this->backdropCreateUser();
$second_authenticated_user = $this->backdropCreateUser();
$user1->roles = array_intersect($user1->roles, array(BACKDROP_AUTHENTICATED_ROLE));
user_save($user1);
// Load all the accounts again, to make sure we have complete account
// objects.
$user1 = user_load(1);
$first_authenticated_user = user_load($first_authenticated_user->uid);
$second_authenticated_user = user_load($second_authenticated_user->uid);
$this->assertEqual($user1->roles, $first_authenticated_user->roles, 'User 1 has the same roles as an authenticated user.');
// Impersonate user 1 and render content that only user 1 should have
// permission to see.
$original_user = $GLOBALS['user'];
$original_session_state = backdrop_save_session();
backdrop_save_session(FALSE);
$GLOBALS['user'] = $user1;
$test_element = array(
'#cache' => array(
'keys' => array('test'),
'granularity' => BACKDROP_CACHE_PER_ROLE,
),
);
$element = $test_element;
$element['#markup'] = 'content for user 1';
$output = backdrop_render($element);
$this->assertEqual($output, 'content for user 1');
// Verify the cache is working by rendering the same element but with
// different markup passed in; the result should be the same.
$element = $test_element;
$element['#markup'] = 'should not be used';
$output = backdrop_render($element);
$this->assertEqual($output, 'content for user 1');
// Verify that the first authenticated user does not see the same content
// as user 1.
$GLOBALS['user'] = $first_authenticated_user;
$element = $test_element;
$element['#markup'] = 'content for authenticated users';
$output = backdrop_render($element);
$this->assertEqual($output, 'content for authenticated users');
// Verify that the second authenticated user shares the cache with the
// first authenticated user.
$GLOBALS['user'] = $second_authenticated_user;
$element = $test_element;
$element['#markup'] = 'should not be used';
$output = backdrop_render($element);
$this->assertEqual($output, 'content for authenticated users');
// Restore the original logged-in user.
$GLOBALS['user'] = $original_user;
backdrop_save_session($original_session_state);
// Restore the previous request method.
$_SERVER['REQUEST_METHOD'] = $request_method;
}