You are here

function RestfulRenderCacheTestCase::testCreateEntity in RESTful 7

Test creating an entity (POST method).

File

tests/RestfulRenderCacheTestCase.test, line 25
Contains RestfulRenderCacheTestCase

Class

RestfulRenderCacheTestCase
@file Contains RestfulRenderCacheTestCase

Code

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.');
}