function GarbageCollectionTest::testGarbageCollection
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php \Drupal\KernelTests\Core\KeyValueStore\GarbageCollectionTest::testGarbageCollection()
- 8.9.x core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php \Drupal\KernelTests\Core\KeyValueStore\GarbageCollectionTest::testGarbageCollection()
- 10 core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php \Drupal\KernelTests\Core\KeyValueStore\GarbageCollectionTest::testGarbageCollection()
Tests garbage collection.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ KeyValueStore/ GarbageCollectionTest.php, line 27
Class
- GarbageCollectionTest
- Tests garbage collection for the expirable key-value database storage.
Namespace
Drupal\KernelTests\Core\KeyValueStoreCode
public function testGarbageCollection() : void {
$collection = $this->randomMachineName();
$connection = Database::getConnection();
$store = new DatabaseStorageExpirable($collection, new PhpSerialize(), $connection, \Drupal::time());
// Insert some items and confirm that they're set.
for ($i = 0; $i <= 3; $i++) {
$store->setWithExpire('key_' . $i, $this->randomObject(), rand(500, 100000));
}
$this->assertCount(4, $store->getAll(), 'Four items were written to the storage.');
// Manually expire the data.
for ($i = 0; $i <= 3; $i++) {
$connection->merge('key_value_expire')
->keys([
'name' => 'key_' . $i,
'collection' => $collection,
])
->fields([
'expire' => \Drupal::time()->getRequestTime() - 1,
])
->execute();
}
// Perform a new set operation and then trigger garbage collection.
$store->setWithExpire('autumn', 'winter', rand(500, 1000000));
system_cron();
// Query the database and confirm that the stale records were deleted.
$result = $connection->select('key_value_expire', 'kvp')
->fields('kvp', [
'name',
])
->condition('collection', $collection)
->execute()
->fetchAll();
$this->assertCount(1, $result, 'Only one item remains after garbage collection');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.