GarbageCollectionTest.php in Zircon Profile 8
File
core/modules/system/src/Tests/KeyValueStore/GarbageCollectionTest.php
View source
<?php
namespace Drupal\system\Tests\KeyValueStore;
use Drupal\Component\Serialization\PhpSerialize;
use Drupal\Core\Database\Database;
use Drupal\Core\KeyValueStore\DatabaseStorageExpirable;
use Drupal\simpletest\KernelTestBase;
class GarbageCollectionTest extends KernelTestBase {
public static $modules = array(
'system',
);
protected function setUp() {
parent::setUp();
$this
->installSchema('system', array(
'key_value_expire',
'flood',
'queue',
));
}
public function testGarbageCollection() {
$collection = $this
->randomMachineName();
$store = new DatabaseStorageExpirable($collection, new PhpSerialize(), Database::getConnection());
for ($i = 0; $i <= 3; $i++) {
$store
->setWithExpire('key_' . $i, $this
->randomObject(), rand(500, 100000));
}
$this
->assertIdentical(sizeof($store
->getAll()), 4, 'Four items were written to the storage.');
for ($i = 0; $i <= 3; $i++) {
db_merge('key_value_expire')
->keys(array(
'name' => 'key_' . $i,
'collection' => $collection,
))
->fields(array(
'expire' => REQUEST_TIME - 1,
))
->execute();
}
$store
->setWithExpire('autumn', 'winter', rand(500, 1000000));
system_cron();
$result = db_query('SELECT name, value FROM {key_value_expire} WHERE collection = :collection', array(
':collection' => $collection,
))
->fetchAll();
$this
->assertIdentical(count($result), 1, 'Only one item remains after garbage collection');
}
}