You are here

public function GenericCacheBackendUnitTestBase::testGetMultiple in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase::testGetMultiple()

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

File

core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php, line 290

Class

GenericCacheBackendUnitTestBase
Tests any cache backend.

Namespace

Drupal\KernelTests\Core\Cache

Code

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

  // Set numerous testing keys.
  $long_cid = str_repeat('a', 300);
  $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);
  $backend
    ->set($long_cid, 300);

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

  // Test return - ensure it contains existing cache ids.
  $this
    ->assertArrayHasKey('test2', $ret, "Existing cache id test2 is set.");
  $this
    ->assertArrayHasKey('test3', $ret, "Existing cache id test3 is set.");
  $this
    ->assertArrayHasKey('test6', $ret, "Existing cache id test6 is set.");
  $this
    ->assertArrayHasKey('test7', $ret, "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
    ->assertGreaterThanOrEqual(REQUEST_TIME, $ret['test2']->created);
  $this
    ->assertLessThanOrEqual(round(microtime(TRUE), 3), $ret['test2']->created);
  $this
    ->assertEquals(Cache::PERMANENT, $ret['test2']->expire, '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
    ->assertSame(3, $ret['test2']->data, "Existing cache id test2 has the correct value.");
  $this
    ->assertSame(5, $ret['test3']->data, "Existing cache id test3 has the correct value.");
  $this
    ->assertSame(13, $ret['test6']->data, "Existing cache id test6 has the correct value.");
  $this
    ->assertSame(17, $ret['test7']->data, "Existing cache id test7 has the correct value.");

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

  // Test $cids array - ensure it does not contain cache id's that exist.
  $this
    ->assertNotContains('test2', $cids, "Existing cache id test2 is not in cids array.");
  $this
    ->assertNotContains('test3', $cids, "Existing cache id test3 is not in cids array.");
  $this
    ->assertNotContains('test6', $cids, "Existing cache id test6 is not in cids array.");
  $this
    ->assertNotContains('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
    ->assertArrayHasKey('test2', $ret, "Existing cache id test2 is set");
  $this
    ->assertArrayHasKey('test7', $ret, "Existing cache id test7 is set");
  $this
    ->assertArrayHasKey('test19', $ret, "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
    ->assertSame(3, $ret['test2']->data, "Existing cache id test2 has the correct value.");
  $this
    ->assertSame(17, $ret['test7']->data, "Existing cache id test7 has the correct value.");
  $this
    ->assertSame(57, $ret['test19']->data, "Added cache id test19 has the correct value.");

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

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

  // Test with a long $cid and non-numeric array key.
  $cids = [
    'key:key' => $long_cid,
  ];
  $return = $backend
    ->getMultiple($cids);
  $this
    ->assertEquals(300, $return[$long_cid]->data);
  $this
    ->assertTrue(empty($cids));
}