You are here

public function KeyValueTest::testExpiration in MongoDB 8

Tests data expiration.

See also

\Drupal\system\Tests\KeyValueStore\DatabaseStorageExpirableTest::testExpiration().

File

src/Tests/KeyValueTest.php, line 148
Contains Drupal\mongodb\Tests\KeyValueTest.

Class

KeyValueTest
Tests the key-value MongoDB storage.

Namespace

Drupal\mongodb\Tests

Code

public function testExpiration() {
  $stores = $this
    ->createStorage();
  $day = 604800;

  // Set an item to expire in the past and another without an expiration.
  $stores[0]
    ->setWithExpire('yesterday', 'all my troubles seemed so far away', -1 * $day);
  $stores[0]
    ->set('troubles', 'here to stay');

  // Only the non-expired item should be returned.
  $this
    ->assertFalse($stores[0]
    ->get('yesterday'));
  $this
    ->assertIdentical($stores[0]
    ->get('troubles'), 'here to stay');
  $this
    ->assertIdentical(count($stores[0]
    ->getMultiple(array(
    'yesterday',
    'troubles',
  ))), 1);

  // Store items set to expire in the past in various ways.
  $stores[0]
    ->setWithExpire($this
    ->randomName(), $this->objects[0], -7 * $day);
  $stores[0]
    ->setWithExpireIfNotExists($this
    ->randomName(), $this->objects[1], -5 * $day);
  $stores[0]
    ->setMultipleWithExpire(array(
    $this
      ->randomName() => $this->objects[2],
    $this
      ->randomName() => $this->objects[3],
  ), -3 * $day);
  $stores[0]
    ->setWithExpireIfNotExists('yesterday', "you'd forgiven me", -1 * $day);
  $stores[0]
    ->setWithExpire('still', "'til we say we're sorry", 2 * $day);

  // Ensure only non-expired items are retrived.
  $all = $stores[0]
    ->getAll();
  $this
    ->assertIdentical(count($all), 2);
  foreach (array(
    'troubles',
    'still',
  ) as $key) {
    $this
      ->assertTrue(!empty($all[$key]));
  }
}