public function GenericCacheBackendUnitTestBase::testGetMultiple

Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().

File

drupal/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php, line 221
Definition of Drupal\system\Tests\Cache\GenericCacheBackendUnitTestBase.

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\system\Tests\Cache

Code

public function testGetMultiple() {
  $backend = $this
    ->getCacheBackend();

  // Set numerous testing keys.
  $backend
    ->set('test1', 1);
  $backend
    ->set('test2', 3);
  $backend
    ->set('test3', 5);
  $backend
    ->set('test4', 7);
  $backend
    ->set('test5', 11);
  $backend
    ->set('test6', 13);
  $backend
    ->set('test7', 17);

  // Mismatch order for harder testing.
  $reference = array(
    'test3',
    'test7',
    'test21',
    // Cid does not exist.
    'test6',
    'test19',
    // Cid does not exist until added before second getMultiple().
    'test2',
  );
  $cids = $reference;
  $ret = $backend
    ->getMultiple($cids);

  // Test return - ensure it contains existing cache ids.
  $this
    ->assert(isset($ret['test2']), "Existing cache id test2 is set.");
  $this
    ->assert(isset($ret['test3']), "Existing cache id test3 is set.");
  $this
    ->assert(isset($ret['test6']), "Existing cache id test6 is set.");
  $this
    ->assert(isset($ret['test7']), "Existing cache id test7 is set.");

  // Test return - ensure that objects has expected properties.
  $this
    ->assertTrue($ret['test2']->valid, 'Item is marked as valid.');
  $this
    ->assertEqual($ret['test2']->created, REQUEST_TIME, 'Created time is correct.');
  $this
    ->assertEqual($ret['test2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Expire time is correct.');

  // Test return - ensure it does not contain nonexistent cache ids.
  $this
    ->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set.");
  $this
    ->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set.");

  // Test values.
  $this
    ->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
  $this
    ->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value.");
  $this
    ->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value.");
  $this
    ->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");

  // Test $cids array - ensure it contains cache id's that do not exist.
  $this
    ->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array.");
  $this
    ->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");

  // Test $cids array - ensure it does not contain cache id's that exist.
  $this
    ->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
  $this
    ->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array.");
  $this
    ->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array.");
  $this
    ->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");

  // Test a second time after deleting and setting new keys which ensures that
  // if the backend uses statics it does not cause unexpected results.
  $backend
    ->delete('test3');
  $backend
    ->delete('test6');
  $backend
    ->set('test19', 57);
  $cids = $reference;
  $ret = $backend
    ->getMultiple($cids);

  // Test return - ensure it contains existing cache ids.
  $this
    ->assert(isset($ret['test2']), "Existing cache id test2 is set");
  $this
    ->assert(isset($ret['test7']), "Existing cache id test7 is set");
  $this
    ->assert(isset($ret['test19']), "Added cache id test19 is set");

  // Test return - ensure it does not contain nonexistent cache ids.
  $this
    ->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set");
  $this
    ->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set");
  $this
    ->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set");

  // Test values.
  $this
    ->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
  $this
    ->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
  $this
    ->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value.");

  // Test $cids array - ensure it contains cache id's that do not exist.
  $this
    ->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array.");
  $this
    ->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array.");
  $this
    ->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");

  // Test $cids array - ensure it does not contain cache id's that exist.
  $this
    ->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
  $this
    ->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
  $this
    ->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array.");
}