View source
<?php
namespace Drupal\Tests\queue_unique\Kernel;
use Drupal\Core\Database\IntegrityConstraintViolationException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use Drupal\queue_unique\UniqueDatabaseQueue;
class UniqueQueueTest extends KernelTestBase {
protected static $modules = [
'queue_unique',
];
public function register(ContainerBuilder $container) {
parent::register($container);
$this->container
->setParameter('install_profile', 'testing');
}
public function testQueueIsUnique() {
$queue_factory = $this->container
->get('queue_unique.database');
$queue = $queue_factory
->get('queue');
self::assertInstanceOf(UniqueDatabaseQueue::class, $queue);
$examples = [
1,
'1',
[
'a' => 1,
'b' => [
'x',
],
],
(object) [
'a' => 1,
'b' => [
'x',
],
],
];
$other_data = [
'z',
];
foreach ($examples as $data) {
$item_id = $queue
->createItem($data);
self::assertNotFalse($item_id);
self::assertEquals(1, $queue
->numberOfItems());
$duplicate_id = $queue
->createItem($data);
self::assertFalse($duplicate_id);
self::assertEquals(1, $queue
->numberOfItems());
$item = $queue
->claimItem();
$queue
->deleteItem($item);
$item_id = $queue
->createItem($data);
self::assertNotFalse($item_id);
self::assertEquals(1, $queue
->numberOfItems());
$item_id = $queue
->createItem($other_data);
self::assertNotFalse($item_id);
self::assertEquals(2, $queue
->numberOfItems());
$queue
->deleteQueue();
}
foreach ($examples as $data) {
$item_id = $queue
->createItem($data);
self::assertNotFalse($item_id);
}
self::assertEquals(count($examples), $queue
->numberOfItems());
}
public function testUpdateHook8001() {
module_load_install('queue_unique');
$database_schema = $this->container
->get('database')
->schema();
self::assertFalse($database_schema
->tableExists(UniqueDatabaseQueue::TABLE_NAME));
$message = queue_unique_update_8001();
self::assertStringContainsString('Queue table does not exist', $message);
$old_schema_definition = $this
->oldSchemaDefinition();
$database_schema
->createTable(UniqueDatabaseQueue::TABLE_NAME, $old_schema_definition);
self::assertTrue($database_schema
->tableExists(UniqueDatabaseQueue::TABLE_NAME));
$message = queue_unique_update_8001();
self::assertStringContainsString('Table dropped', $message);
self::assertFalse($database_schema
->tableExists(UniqueDatabaseQueue::TABLE_NAME));
$database_schema
->createTable(UniqueDatabaseQueue::TABLE_NAME, $old_schema_definition);
self::assertTrue($database_schema
->tableExists(UniqueDatabaseQueue::TABLE_NAME));
$names = [
'testq1',
'myotherq2',
];
$examples = [
1,
'1',
[
'a' => 1,
'b' => [
'x',
],
],
(object) [
'a' => 1,
'b' => [
'x',
],
],
];
foreach ($names as $name) {
foreach ($examples as $data) {
$this
->createItemMd5($name, $data);
}
}
$connection = $this->container
->get('database');
$query = $connection
->select(UniqueDatabaseQueue::TABLE_NAME)
->countQuery();
$count = $query
->execute()
->fetchField();
self::assertEquals(count($names) * count($examples), $count);
$message = queue_unique_update_8001();
$expected = "Migrated {$count} items from the old queue table to the new table.";
self::assertEquals($expected, $message);
$queue_factory = $this->container
->get('queue_unique.database');
foreach ($names as $name) {
$queue = $queue_factory
->get($name);
self::assertEquals(count($examples), $queue
->numberOfItems());
foreach ($examples as $data) {
$duplicate_id = $queue
->createItem($data);
self::assertFalse($duplicate_id);
$item = $queue
->claimItem();
self::assertEquals($data, $item->data);
}
}
}
protected function createItemMd5($name, $data) {
$connection = $this->container
->get('database');
try {
$query = $connection
->insert(UniqueDatabaseQueue::TABLE_NAME)
->fields([
'name' => $name,
'data' => serialize($data),
'created' => time(),
'md5' => substr(hash('sha512', $name . serialize($data)), 0, 32),
]);
return $query
->execute();
} catch (IntegrityConstraintViolationException $e) {
return FALSE;
}
}
protected function oldSchemaDefinition() {
$db_queue = $this->container
->get('queue.database')
->get('dummy');
return array_merge_recursive($db_queue
->schemaDefinition(), [
'fields' => [
'md5' => [
'type' => 'char',
'length' => 32,
'not null' => TRUE,
],
],
'unique keys' => [
'unique' => [
'md5',
],
],
]);
}
}