View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\mongodb_storage\Kernel;
use Drupal\Core\KeyValueStore\DatabaseStorage;
use Drupal\Core\KeyValueStore\DatabaseStorageExpirable;
use Drupal\mongodb\MongoDb;
use Drupal\mongodb_storage\Install\SqlImport;
use Drupal\mongodb_storage\KeyValueExpirableFactory;
use Drupal\mongodb_storage\KeyValueFactory;
use Drupal\mongodb_storage\Storage;
class SqlImportTest extends KeyValueTestBase {
const IMPORT_OUTPUT = SqlImport::KVP_TABLE . PHP_EOL . SqlImport::KVE_TABLE . PHP_EOL;
protected static $modules = [
'system',
MongoDb::MODULE,
Storage::MODULE,
];
protected $database;
protected $sqlImport;
public function setUp() : void {
parent::setUp();
$this->database = $this->container
->get('database');
$this->sqlImport = $this->container
->get(Storage::SERVICE_SQL_IMPORT);
}
protected function countTable(string $name) : int {
$count = (int) $this->database
->select($name)
->countQuery()
->execute()
->fetchField();
return $count;
}
protected function getKvCollectionNames(string $prefix) : array {
$cursor = $this->container
->get(MongoDb::SERVICE_DB_FACTORY)
->get(KeyValueFactory::DB_KEYVALUE)
->listCollections();
$result = [];
$len = strlen($prefix);
foreach ($cursor as $collection) {
if (strpos($name = $collection
->getName(), $prefix) === 0) {
$result[] = substr($name, $len);
}
}
sort($result);
return $result;
}
public function testImportService() {
$this
->assertInstanceOf(SqlImport::class, $this->sqlImport, "SQL import service is available");
$this
->assertTrue(method_exists($this->sqlImport, 'import'));
}
public function testImport() {
$this
->expectOutputString(self::IMPORT_OUTPUT);
$this->sqlImport
->import();
}
public function importProvider() : array {
return [
[
SqlImport::KVP_TABLE,
Storage::SERVICE_KV,
KeyValueFactory::COLLECTION_PREFIX,
],
[
SqlImport::KVE_TABLE,
Storage::SERVICE_KVE,
KeyValueExpirableFactory::COLLECTION_PREFIX,
],
];
}
public function testImportActual(string $table, string $service, string $prefix) {
$columns = [];
switch ($table) {
case SqlImport::KVE_TABLE:
$columns = array_keys(DatabaseStorageExpirable::schemaDefinition()['fields']);
break;
case SqlImport::KVP_TABLE:
$columns = array_keys(DatabaseStorage::schemaDefinition()['fields']);
break;
default:
$this
->fail("Unexpected table requested: {$table}.");
}
$actualPreDbCount = $this
->countTable($table);
$this
->assertEquals(0, $actualPreDbCount);
$actualPreMgCount = count($this
->getKvCollectionNames($prefix));
$this
->assertEquals(0, $actualPreMgCount);
$rowCount = mt_rand(1, 100);
$rows = [];
$collection = $this
->randomMachineName();
for ($i = 0; $i < $rowCount; $i++) {
if (mt_rand(0, 10) >= 8) {
$collection = $this
->randomMachineName();
}
$name = $this
->randomMachineName();
$value = serialize($this
->randomString(1024));
$row = [
$collection,
$name,
$value,
];
if (count($columns) === 4) {
$row[] = time() + 180;
}
$rows[] = $row;
}
$expectedCollections = [];
foreach ($rows as $row) {
$expectedCollections[$row[0]][$row[1]] = unserialize($row[2]);
}
ksort($expectedCollections);
foreach ($expectedCollections as $name => &$values) {
ksort($values);
}
$insert = $this->database
->insert($table)
->fields($columns);
foreach ($rows as $row) {
$insert
->values($row);
}
$insert
->execute();
$this
->expectOutputString(self::IMPORT_OUTPUT);
$this->sqlImport
->import();
$keyValue = $this->container
->get($service);
$mongoCollections = $this
->getKvCollectionNames($prefix);
$this
->assertEquals(array_keys($expectedCollections), $mongoCollections, "Collection names match");
foreach ($expectedCollections as $collectionName => $expected) {
$all = $keyValue
->get($collectionName)
->getAll();
ksort($all);
$this
->assertEquals($expected, $all);
}
}
}