public function RestfulRenderCacheTestCase::testPageCache in RESTful 7.2
Same name and namespace in other branches
- 7 tests/RestfulRenderCacheTestCase.test \RestfulRenderCacheTestCase::testPageCache()
Tests for SA 154563.
File
- tests/
RestfulRenderCacheTestCase.test, line 195 - Contains RestfulRenderCacheTestCase
Class
- RestfulRenderCacheTestCase
- Class RestfulRenderCacheTestCase.
Code
public function testPageCache() {
// Enable page cache.
variable_set('cache', TRUE);
variable_set('restful_page_cache', TRUE);
// Make anonymous users not to be able to access content.
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
'access content' => FALSE,
));
// Create a new article.
$settings = array(
'type' => 'article',
);
$settings['title'] = 'Node title';
$node = $this
->drupalCreateNode($settings);
$path = 'api/v1.0/test_articles/' . $node->nid;
$url = url($path, array(
'absolute' => TRUE,
));
$cache = _cache_get_object('cache_page');
// Create a user that can access content.
$account = $this
->drupalCreateUser(array(
'access content',
));
// 1. Test the cookie authentication.
// Log in the user (creating the cookie).
$this
->drupalLogin($account);
// Access the created article creating a page cache entry.
$response = $this
->httpRequest($path);
$this
->assertEqual($response['code'], 200, 'Access granted for logged in user.');
if (!drupal_is_cli()) {
// Make sure that there is not a page cache entry.
$this
->assertFalse($cache
->get($url), 'A page cache entry was not created for a authenticated user.');
}
// Log out the user.
$this
->drupalLogout();
// Try to access the cached resource.
$response = $this
->httpRequest($path);
// The user should get a 401.
$this
->assertEqual($response['code'], 401, 'Access denied for anonymous user.');
// Clear the cache, since anonymous requests get cached. Requests with basic
// authentication will pick that cached version. This is a know issue and we
// accept it as a lesser evil.
$cache
->clear($url);
// 2. Test the basic authentication.
$response = $this
->httpRequest($path, RequestInterface::METHOD_GET, NULL, array(
'Authorization' => 'Basic ' . drupal_base64_encode($account->name . ':' . $account->pass_raw),
));
$this
->assertEqual($response['code'], 200, 'Access granted for logged in user.');
if (!drupal_is_cli()) {
// Make sure that there is a page cache entry.
$this
->assertFalse($cache
->get($url), 'A page cache entry was not created with basic auth.');
}
// Try to access the cached resource.
$response = $this
->httpRequest($path);
// The user should get a 401.
$this
->assertEqual($response['code'], 401, 'Access denied for anonymous user.');
if (!drupal_is_cli()) {
// Make sure that there is not a page cache entry.
$this
->assertFalse($cache
->get($url), 'A page cache entry was created for an anonymous user.');
}
// Remove the cache entry.
$cache
->clear($url);
// 3. Test that when restful_page_cache is off there is no page cache.
variable_set('restful_page_cache', FALSE);
// Try to access the cached resource as anonymous users.
$this
->httpRequest($path);
if (!drupal_is_cli()) {
$this
->assertFalse($cache
->get($url), 'A page cache entry was not created for an anonymous users when restful_page_cache is off.');
}
}