You are here

RestfulRenderCacheTestCase.test in RESTful 7

Same filename and directory in other branches
  1. 7.2 tests/RestfulRenderCacheTestCase.test

Contains RestfulRenderCacheTestCase

File

tests/RestfulRenderCacheTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulRenderCacheTestCase
 */
class RestfulRenderCacheTestCase extends \RestfulCurlBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Render Cache',
      'description' => 'Test the render cache capabilities.',
      'group' => 'RESTful',
    );
  }
  function setUp() {
    parent::setUp('restful_test');
  }

  /**
   * Test creating an entity (POST method).
   */
  function testCreateEntity() {
    $settings = array(
      'type' => 'article',
    );
    $account = $this
      ->drupalCreateUser();
    $num_articles = 3;
    for ($index = 0; $index < $num_articles; $index++) {
      $settings['title'] = 'Node title ' . $index;
      $node = $this
        ->drupalCreateNode($settings);
      $nodes[$node->nid] = $node;
    }
    $handler = restful_get_restful_handler('test_articles', 1, 1);
    $handler
      ->setAccount($account);
    $cache = $handler
      ->getCacheController();

    // Make sure the cache is activated.
    $cache_info = $handler
      ->getPluginKey('render_cache');
    $this
      ->assertTrue($cache_info['render'], 'Cache render is activated');

    // Empty the cache.
    $cache
      ->clear('*', TRUE);
    $this
      ->assertTrue($cache
      ->isEmpty(), 'Cache render is empty.');

    // Test that cache is being generated correctly.
    // Get the articles.
    drupal_static_reset('RestfulBase::getVersionFromRequest');
    $pre_cache_results = $handler
      ->get();

    // Make sure some cache entries are generated.
    $this
      ->assertFalse($cache
      ->isEmpty(), 'Cache render is being populated.');

    // Get the cached results.
    $post_cache_results = $handler
      ->get();

    // Make sure the cached results are the same as the non-cached results.
    $this
      ->assertEqual($pre_cache_results[0], $post_cache_results[0], 'Cached data is consistent.');

    // Get a cached result directly from the cache backend.
    $node = reset($nodes);
    $cid = 'v1.1::test_articles::uu' . $account->uid . '::pa' . 'et:node::ei:' . $node->nid;
    $record = $cache
      ->get($cid);
    $this
      ->assertEqual($pre_cache_results[0], $record->data, 'Data in cache bins is correct.');

    // Test that invalidation is clearing cache records.
    // 1. Update node and watch cache vanish.
    // Make sure the cache is activated.
    $request = array(
      'fields' => 'id,label',
    );
    $handler
      ->get('', $request);
    $cid_w_request = $cid . '::fi:id,label';
    $this
      ->assertTrue($cache_info['simple_invalidate'], 'Cache render simple invalidation is activated');
    $node->title .= ' updated';
    node_save($node);
    $record = $cache
      ->get($cid);
    $this
      ->assertFalse($record, 'Cache record cleared correctly.');
    $record = $cache
      ->get($cid_w_request);
    $this
      ->assertFalse($record, 'Cache record cleared correctly with request params.');
    $this
      ->assertFalse($cache
      ->isEmpty(), 'Remaining cache items are intact after updating entity.');

    // Regenerate cache for $node.
    $handler
      ->get($node->nid);
    $handler
      ->get($node->nid, $request);
    $record = $cache
      ->get($cid);
    $this
      ->assertNotNull($record->data, 'Cache is being generated for non-list requests.');
    $record = $cache
      ->get($cid_w_request);
    $this
      ->assertNotNull($record->data, 'Cache is being generated for non-list requests with request params.');

    // 2. Update the user account. All cache records should be invalidated.
    $account->name .= ' updated';
    user_save($account);
    $this
      ->assertTrue($cache
      ->isEmpty(), 'Cache bin is emptied after updating a user.');

    // Test cache segmentation with request params.
    // Rebuild caches.
    $cache
      ->clear('*', TRUE);
    $results_wo_request = $handler
      ->get();
    $results_w_request = $handler
      ->get('', $request);

    // Check that the cached results for the entity without request array are
    // different than with the request array.
    $cid_wo_request = $cid;
    $record = $cache
      ->get($cid_w_request);

    // Check that the stored data for both cache ids exists and is different.
    $this
      ->assertNotEqual($record->data, $cache
      ->get($cid_wo_request)->data, 'Cache with request params is different.');

    // Make sure that we are not storing the same data than before.
    $this
      ->assertNotEqual($results_wo_request[0], $record->data, 'Cache with request is not overwritten.');

    // Assure that the stored cache corresponds to the returned rendered entity.
    $this
      ->assertEqual($results_w_request[0], $record->data, 'Cache with request is correctly stored.');

    // Test helper functions.
    foreach ($nodes as $nid => $entity) {

      // Populate the cache.
      $handler
        ->get($nid);
      $handler
        ->get($nid, $request);
    }

    // Testing clearResourceRenderedCacheEntity.
    $handler
      ->clearResourceRenderedCacheEntity($node->nid);
    $record = $cache
      ->get($cid);
    $this
      ->assertFalse($record, 'The entity cache for the current resource has been deleted.');
    $record = $cache
      ->get($cid_w_request);
    $this
      ->assertFalse($record, 'The entity cache for the current resource has been deleted.');
    $this
      ->assertFalse($cache
      ->isEmpty(), 'Other entities keep the cache.');

    // Testing clearResourceRenderedCache
    $handler
      ->clearResourceRenderedCache();
    foreach ($nodes as $nid => $entity) {
      $cid = 'v1.1::test_articles::uu' . $account->uid . '::pa' . 'et:node::ei:' . $nid;
      $record = $cache
        ->get($cid);
      $this
        ->assertFalse($record, 'The entity cache for the current resource has been deleted.');
      $cid_w_request = $cid . '::fi:id,label';
      $record = $cache
        ->get($cid_w_request);
      $this
        ->assertFalse($record, 'The entity cache for the current resource has been deleted.');
    }

    // Test PER_ROLE granularity.
    $cache
      ->clear('*', TRUE);
    $cache_info = $handler
      ->getPluginKey('render_cache');
    $cache_info['granularity'] = DRUPAL_CACHE_PER_ROLE;
    $handler
      ->setPluginKey('render_cache', $cache_info);
    $handler
      ->get('');
    $role_cid = 'v1.1::test_articles::ur' . implode(',', array_keys($account->roles)) . '::pa' . 'et:node::ei:' . $node->nid;
    $record = $cache
      ->get($role_cid);
    $this
      ->assertTrue($record->data, 'Cache key contains role information.');
  }

  /**
   * Tests for SA 154563.
   */
  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.');

    // Pages are not cached if the request is originated in CLI.
    // @see drupal_page_is_cacheable()
    if (!drupal_is_cli()) {

      // Make sure that there is not a page cache entry.
      $this
        ->assertFalse($cache
        ->get($url), 'There should not be a cache entry 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.');
    if (!drupal_is_cli()) {

      // Make sure that there is a page cache entry.
      $this
        ->assertTrue($cache
        ->get($url), 'A page cache entry was created for an anonymous user.');
    }

    // Remove the cache entry.
    $cache
      ->clear($url);

    // 2. Test the basic authentication.
    $response = $this
      ->httpRequest($path, \RestfulInterface::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
        ->assertTrue($cache
        ->get($url), 'A page cache entry was created for an anonymous user.');
    }

    // 3. Test that when restful_page_cache is off there is no page cache.
    // Remove the cache entry.
    $cache
      ->clear($url);
    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.');
    }
  }

}

Classes

Namesort descending Description
RestfulRenderCacheTestCase @file Contains RestfulRenderCacheTestCase