You are here

public function DatabaseStorageExpirableTest::_testExpiration in Service Container 7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/service_container/Tests/KeyValue/DatabaseStorageExpirableTest.php \Drupal\service_container\Tests\KeyValue\DatabaseStorageExpirableTest::_testExpiration()

Tests data expiration.

1 call to DatabaseStorageExpirableTest::_testExpiration()
DatabaseStorageExpirableTest::testKeyValueStore in lib/Drupal/service_container/Tests/KeyValue/DatabaseStorageExpirableTest.php

File

lib/Drupal/service_container/Tests/KeyValue/DatabaseStorageExpirableTest.php, line 141
Contains Drupal\system\Tests\KeyValueStore\DatabaseStorageExpirableTest.

Class

DatabaseStorageExpirableTest
Tests the key-value database storage.

Namespace

Drupal\service_container\Tests\KeyValue

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]
    ->has('yesterday'));
  $this
    ->assertFalse($stores[0]
    ->get('yesterday'));
  $this
    ->assertTrue($stores[0]
    ->has('troubles'));
  $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]));
  }
}