diff --git a/modules/common/common.services.yml b/modules/common/common.services.yml index 10a3cd4c4b..24c4f7f0ad 100644 --- a/modules/common/common.services.yml +++ b/modules/common/common.services.yml @@ -43,6 +43,10 @@ services: - [setResourceMapper, ['@?dkan.metastore.resource_mapper']] - [setImportInfo, ['@?dkan.datastore.import_info']] + dkan.common.logger_channel: + parent: logger.channel_base + arguments: [ 'dkan' ] + plugin.manager.dkan_api_docs: class: \Drupal\common\Plugin\DkanApiDocsPluginManager parent: default_plugin_manager diff --git a/modules/common/src/LoggerTrait.php b/modules/common/src/LoggerTrait.php index 21422b48e7..b1b0461ec8 100644 --- a/modules/common/src/LoggerTrait.php +++ b/modules/common/src/LoggerTrait.php @@ -7,6 +7,8 @@ /** * DKAN logger channel trait. + * + * @deprecated Use an injected logger service instead. */ trait LoggerTrait { diff --git a/modules/datastore/datastore.services.yml b/modules/datastore/datastore.services.yml index ce35058910..aa7072f9f1 100644 --- a/modules/datastore/datastore.services.yml +++ b/modules/datastore/datastore.services.yml @@ -50,14 +50,14 @@ services: - '@dkan.metastore.reference_lookup' - '@dkan.metastore.storage' - '@dkan.datastore.service' - calls: - - [setLoggerFactory, ['@logger.factory']] + - '@dkan.datastore.logger_channel' dkan.datastore.service.factory.import: class: \Drupal\datastore\Service\Factory\ImportServiceFactory arguments: - '@dkan.datastore.import_job_store_factory' - '@dkan.datastore.database_table_factory' + - '@dkan.datastore.logger_channel' dkan.datastore.logger_channel: parent: logger.channel_base @@ -81,6 +81,7 @@ services: class: \Drupal\datastore\Storage\DatabaseTableFactory arguments: - '@dkan.datastore.database' + - '@dkan.datastore.logger_channel' dkan.datastore.sql_endpoint.service: class: \Drupal\datastore\SqlEndpoint\DatastoreSqlEndpointService diff --git a/modules/datastore/modules/datastore_mysql_import/datastore_mysql_import.services.yml b/modules/datastore/modules/datastore_mysql_import/datastore_mysql_import.services.yml index bf6d0fcc73..8d918cb413 100644 --- a/modules/datastore/modules/datastore_mysql_import/datastore_mysql_import.services.yml +++ b/modules/datastore/modules/datastore_mysql_import/datastore_mysql_import.services.yml @@ -1,11 +1,14 @@ services: - dkan.datastore.service.factory.import: + dkan.datastore_mysql_import.service.factory.import: + decorates: dkan.datastore.service.factory.import class: \Drupal\datastore_mysql_import\Factory\MysqlImportFactory arguments: - '@dkan.datastore.import_job_store_factory' - '@dkan.datastore_mysql_import.database_table_factory' + - '@dkan.datastore.logger_channel' dkan.datastore_mysql_import.database_table_factory: class: \Drupal\datastore_mysql_import\Storage\MySqlDatabaseTableFactory arguments: - '@dkan.datastore.database' + - '@dkan.datastore.logger_channel' diff --git a/modules/datastore/modules/datastore_mysql_import/src/Factory/MysqlImportFactory.php b/modules/datastore/modules/datastore_mysql_import/src/Factory/MysqlImportFactory.php index cfe5767d8a..530cc06267 100644 --- a/modules/datastore/modules/datastore_mysql_import/src/Factory/MysqlImportFactory.php +++ b/modules/datastore/modules/datastore_mysql_import/src/Factory/MysqlImportFactory.php @@ -7,6 +7,7 @@ use Drupal\datastore\Storage\ImportJobStoreFactory; use Drupal\datastore_mysql_import\Service\MysqlImport; use Drupal\datastore_mysql_import\Storage\MySqlDatabaseTableFactory; +use Psr\Log\LoggerInterface; /** * Mysql importer factory. @@ -27,12 +28,24 @@ class MysqlImportFactory implements ImportFactoryInterface { */ private $databaseTableFactory; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor. */ - public function __construct(ImportJobStoreFactory $importJobStoreFactory, MySqlDatabaseTableFactory $databaseTableFactory) { + public function __construct( + ImportJobStoreFactory $importJobStoreFactory, + MySqlDatabaseTableFactory $databaseTableFactory, + LoggerInterface $loggerChannel + ) { $this->importJobStoreFactory = $importJobStoreFactory; $this->databaseTableFactory = $databaseTableFactory; + $this->logger = $loggerChannel; } /** @@ -46,7 +59,12 @@ public function getInstance(string $identifier, array $config = []) { throw new \Exception("config['resource'] is required"); } - $importer = new ImportService($resource, $this->importJobStoreFactory, $this->databaseTableFactory); + $importer = new ImportService( + $resource, + $this->importJobStoreFactory, + $this->databaseTableFactory, + $this->logger + ); $importer->setImporterClass(MysqlImport::class); return $importer; } diff --git a/modules/datastore/modules/datastore_mysql_import/src/Storage/MySqlDatabaseTableFactory.php b/modules/datastore/modules/datastore_mysql_import/src/Storage/MySqlDatabaseTableFactory.php index 54a9f6af16..9f85f55e29 100644 --- a/modules/datastore/modules/datastore_mysql_import/src/Storage/MySqlDatabaseTableFactory.php +++ b/modules/datastore/modules/datastore_mysql_import/src/Storage/MySqlDatabaseTableFactory.php @@ -13,7 +13,7 @@ class MySqlDatabaseTableFactory extends DatabaseTableFactory { * {@inheritDoc} */ protected function getDatabaseTable($resource) { - return new MySqlDatabaseTable($this->connection, $resource); + return new MySqlDatabaseTable($this->connection, $resource, $this->logger); } } diff --git a/modules/datastore/src/Service/Factory/ImportServiceFactory.php b/modules/datastore/src/Service/Factory/ImportServiceFactory.php index c5347edcc8..27e55be7ad 100644 --- a/modules/datastore/src/Service/Factory/ImportServiceFactory.php +++ b/modules/datastore/src/Service/Factory/ImportServiceFactory.php @@ -5,6 +5,7 @@ use Drupal\datastore\Service\ImportService; use Drupal\datastore\Storage\DatabaseTableFactory; use Drupal\datastore\Storage\ImportJobStoreFactory; +use Psr\Log\LoggerInterface; /** * Create an importer object for a given resource. @@ -25,12 +26,24 @@ class ImportServiceFactory implements ImportFactoryInterface { */ private $databaseTableFactory; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor. */ - public function __construct(ImportJobStoreFactory $importJobStoreFactory, DatabaseTableFactory $databaseTableFactory) { + public function __construct( + ImportJobStoreFactory $importJobStoreFactory, + DatabaseTableFactory $databaseTableFactory, + LoggerInterface $loggerChannel + ) { $this->importJobStoreFactory = $importJobStoreFactory; $this->databaseTableFactory = $databaseTableFactory; + $this->logger = $loggerChannel; } /** @@ -40,7 +53,12 @@ public function __construct(ImportJobStoreFactory $importJobStoreFactory, Databa */ public function getInstance(string $identifier, array $config = []) { if ($resource = $config['resource'] ?? FALSE) { - return new ImportService($resource, $this->importJobStoreFactory, $this->databaseTableFactory); + return new ImportService( + $resource, + $this->importJobStoreFactory, + $this->databaseTableFactory, + $this->logger + ); } throw new \Exception("config['resource'] is required"); } diff --git a/modules/datastore/src/Service/ImportService.php b/modules/datastore/src/Service/ImportService.php index 267a0f55df..a89c097c18 100644 --- a/modules/datastore/src/Service/ImportService.php +++ b/modules/datastore/src/Service/ImportService.php @@ -3,14 +3,14 @@ namespace Drupal\datastore\Service; use CsvParser\Parser\Csv; -use Drupal\datastore\Plugin\QueueWorker\ImportJob; -use Drupal\common\EventDispatcherTrait; -use Drupal\common\LoggerTrait; use Drupal\common\DataResource; +use Drupal\common\EventDispatcherTrait; +use Drupal\datastore\Plugin\QueueWorker\ImportJob; use Drupal\datastore\Storage\DatabaseTable; use Drupal\datastore\Storage\DatabaseTableFactory; use Drupal\datastore\Storage\ImportJobStoreFactory; use Procrastinator\Result; +use Psr\Log\LoggerInterface; /** * Datastore importer. @@ -20,7 +20,7 @@ * as a property. */ class ImportService { - use LoggerTrait; + use EventDispatcherTrait; /** @@ -76,6 +76,13 @@ class ImportService { */ private ?ImportJob $importJob = NULL; + /** + * Logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Create a resource service instance. * @@ -85,11 +92,19 @@ class ImportService { * Import jobstore factory. * @param \Drupal\datastore\Storage\DatabaseTableFactory $databaseTableFactory * Database Table factory. + * @param \Psr\Log\LoggerInterface $loggerChannel + * DKAN logger channel service. */ - public function __construct(DataResource $resource, ImportJobStoreFactory $importJobStoreFactory, DatabaseTableFactory $databaseTableFactory) { + public function __construct( + DataResource $resource, + ImportJobStoreFactory $importJobStoreFactory, + DatabaseTableFactory $databaseTableFactory, + LoggerInterface $loggerChannel + ) { $this->resource = $resource; $this->importJobStoreFactory = $importJobStoreFactory; $this->databaseTableFactory = $databaseTableFactory; + $this->logger = $loggerChannel; } /** @@ -117,8 +132,7 @@ public function import() { if ($result->getStatus() === Result::ERROR) { $datastore_resource = $this->getResource()->getDatastoreResource(); - $this->setLoggerFactory(\Drupal::service('logger.factory')); - $this->error('Error importing resource id:%id path:%path message:%message', [ + $this->logger->error('Error importing resource id:%id path:%path message:%message', [ '%id' => $datastore_resource->getId(), '%path' => $datastore_resource->getFilePath(), '%message' => $result->getError(), diff --git a/modules/datastore/src/Service/ResourceLocalizer.php b/modules/datastore/src/Service/ResourceLocalizer.php index 3f204c919b..351f3f98f1 100644 --- a/modules/datastore/src/Service/ResourceLocalizer.php +++ b/modules/datastore/src/Service/ResourceLocalizer.php @@ -5,7 +5,6 @@ use Contracts\FactoryInterface; use Drupal\common\DataResource; use Drupal\common\EventDispatcherTrait; -use Drupal\common\LoggerTrait; use Drupal\common\UrlHostTokenResolver; use Drupal\common\Util\DrupalFiles; use Drupal\Core\File\FileSystemInterface; @@ -21,7 +20,6 @@ */ class ResourceLocalizer { - use LoggerTrait; use EventDispatcherTrait; /** diff --git a/modules/datastore/src/Service/ResourcePurger.php b/modules/datastore/src/Service/ResourcePurger.php index 6b3e0d7669..6d632d811d 100644 --- a/modules/datastore/src/Service/ResourcePurger.php +++ b/modules/datastore/src/Service/ResourcePurger.php @@ -4,22 +4,19 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; - -use Drupal\common\LoggerTrait; -use Drupal\common\DataResource; use Drupal\Core\Entity\EntityPublishedInterface; +use Drupal\common\DataResource; use Drupal\datastore\DatastoreService; use Drupal\metastore\ReferenceLookupInterface; use Drupal\metastore\Storage\DataFactory; use Drupal\node\NodeInterface; - +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Resource purger service. */ class ResourcePurger implements ContainerInjectionInterface { - use LoggerTrait; /** * The datastore.settings config. @@ -49,6 +46,13 @@ class ResourcePurger implements ContainerInjectionInterface { */ private $storage; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructs a ResourcePurger object. * @@ -60,12 +64,21 @@ class ResourcePurger implements ContainerInjectionInterface { * The dkan.metastore.storage service. * @param \Drupal\datastore\DatastoreService $datastore * The dkan.datastore.service service. + * @param \Psr\Log\LoggerInterface $loggerChannel + * DKAN logger channel service. */ - public function __construct(ConfigFactoryInterface $configFactory, ReferenceLookupInterface $referenceLookup, DataFactory $dataFactory, DatastoreService $datastore) { + public function __construct( + ConfigFactoryInterface $configFactory, + ReferenceLookupInterface $referenceLookup, + DataFactory $dataFactory, + DatastoreService $datastore, + LoggerInterface $loggerChannel + ) { $this->config = $configFactory->get('datastore.settings'); $this->referenceLookup = $referenceLookup; $this->storage = $dataFactory->getInstance('dataset'); $this->datastore = $datastore; + $this->logger = $loggerChannel; } /** @@ -76,7 +89,8 @@ public static function create(ContainerInterface $container) { $container->get('config.factory'), $container->get('dkan.metastore.reference_lookup'), $container->get('dkan.metastore.storage'), - $container->get('dkan.datastore.service') + $container->get('dkan.datastore.service'), + $container->get('dkan.datastore.logger_channel') ); } @@ -147,7 +161,7 @@ private function queue(array $uuids, bool $prior) { 'uuids' => $uuids, 'prior' => $prior, ]); - $this->notice('Queued resource purging with queueId:%queueId uuids:%uuids', [ + $this->logger->notice('Queued resource purging with queueId:%queueId uuids:%uuids', [ '%queueId' => $queueId, '%uuids' => implode(', ', $uuids), ]); @@ -184,7 +198,7 @@ public function purgeHelper(int $vid, string $uuid, bool $prior) { $this->purge($vid, $uuid, $prior); } catch (\Exception $e) { - $this->error("Error purging uuid {$uuid}, revision id {$vid}: " . $e->getMessage()); + $this->logger->error("Error purging uuid {$uuid}, revision id {$vid}: " . $e->getMessage()); } } @@ -387,7 +401,7 @@ private function removeResourceLocalizer(string $id, string $version) { $this->datastore->getResourceLocalizer()->remove($id, $version); } catch (\Exception $e) { - $this->error("Error removing resource localizer id {$id}, version {$version}: " . $e->getMessage()); + $this->logger->error("Error removing resource localizer id {$id}, version {$version}: " . $e->getMessage()); } } @@ -404,7 +418,7 @@ private function removeDatastoreStorage(string $id, string $version) { $this->datastore->getStorage($id, $version)->destruct(); } catch (\Exception $e) { - $this->error("Error deleting datastore id {$id}, version {$version}: " . $e->getMessage()); + $this->logger->error("Error deleting datastore id {$id}, version {$version}: " . $e->getMessage()); } } diff --git a/modules/datastore/src/Storage/DatabaseTable.php b/modules/datastore/src/Storage/DatabaseTable.php index c96bea8b86..bc6cfa4370 100755 --- a/modules/datastore/src/Storage/DatabaseTable.php +++ b/modules/datastore/src/Storage/DatabaseTable.php @@ -2,10 +2,10 @@ namespace Drupal\datastore\Storage; -use Drupal\common\LoggerTrait; -use Drupal\common\Storage\AbstractDatabaseTable; use Drupal\Core\Database\Connection; +use Drupal\common\Storage\AbstractDatabaseTable; use Drupal\datastore\DatastoreResource; +use Psr\Log\LoggerInterface; /** * Database storage object. @@ -14,8 +14,6 @@ */ class DatabaseTable extends AbstractDatabaseTable implements \JsonSerializable { - use LoggerTrait; - /** * Datastore resource object. * @@ -23,6 +21,13 @@ class DatabaseTable extends AbstractDatabaseTable implements \JsonSerializable { */ private $resource; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor method. * @@ -30,12 +35,19 @@ class DatabaseTable extends AbstractDatabaseTable implements \JsonSerializable { * Drupal database connection object. * @param \Drupal\datastore\DatastoreResource $resource * A resource. + * @param \Psr\Log\LoggerInterface $loggerChannel + * DKAN logger channel service. */ - public function __construct(Connection $connection, DatastoreResource $resource) { + public function __construct( + Connection $connection, + DatastoreResource $resource, + LoggerInterface $loggerChannel + ) { // Set resource before calling the parent constructor. The parent calls // getTableName which we implement and needs the resource to operate. $this->resource = $resource; $this->connection = $connection; + $this->logger = $loggerChannel; if ($this->tableExist($this->getTableName())) { $this->setSchemaFromTable(); @@ -89,7 +101,7 @@ public function getTableName() { protected function prepareData(string $data, string $id = NULL): array { $decoded = json_decode($data); if ($decoded === NULL) { - $this->log( + $this->logger->log( 'datastore_import', 'Error decoding id:@id, data: @data.', ['@id' => $id, '@data' => $data] @@ -97,7 +109,7 @@ protected function prepareData(string $data, string $id = NULL): array { throw new \Exception('Import for ' . $id . ' error when decoding ' . $data); } elseif (!is_array($decoded)) { - $this->log( + $this->logger->log( 'datastore_import', 'Array expected while decoding id:@id, data: @data.', ['@id' => $id, '@data' => $data] diff --git a/modules/datastore/src/Storage/DatabaseTableFactory.php b/modules/datastore/src/Storage/DatabaseTableFactory.php index ca319f70a5..2ffa5ac67a 100644 --- a/modules/datastore/src/Storage/DatabaseTableFactory.php +++ b/modules/datastore/src/Storage/DatabaseTableFactory.php @@ -4,6 +4,7 @@ use Contracts\FactoryInterface; use Drupal\Core\Database\Connection; +use Psr\Log\LoggerInterface; /** * DatabaseTable data object factory. @@ -17,11 +18,22 @@ class DatabaseTableFactory implements FactoryInterface { */ protected $connection; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + protected LoggerInterface $logger; + /** * Constructor. */ - public function __construct(Connection $connection) { + public function __construct( + Connection $connection, + LoggerInterface $loggerChannel + ) { $this->connection = $connection; + $this->logger = $loggerChannel; } /** @@ -44,7 +56,7 @@ public function getInstance(string $identifier, array $config = []) { * Protected. */ protected function getDatabaseTable($resource) { - return new DatabaseTable($this->connection, $resource); + return new DatabaseTable($this->connection, $resource, $this->logger); } } diff --git a/modules/datastore/tests/src/Kernel/Service/ImportServiceTest.php b/modules/datastore/tests/src/Kernel/Service/ImportServiceTest.php index d588705026..763a352071 100644 --- a/modules/datastore/tests/src/Kernel/Service/ImportServiceTest.php +++ b/modules/datastore/tests/src/Kernel/Service/ImportServiceTest.php @@ -52,6 +52,7 @@ public function testImport() { new DataResource('abc.txt', 'text/csv'), $this->container->get('dkan.datastore.import_job_store_factory'), $this->container->get('dkan.datastore.database_table_factory'), + $this->container->get('dkan.datastore.logger_channel') ]) ->getMock(); $import_service->method('getImporter') @@ -83,7 +84,7 @@ public function testLogImportError() { $logger_factory = $this->createMock(LoggerChannelFactory::class); $logger_factory->expects($this->once()) ->method('get') - ->with('dkan') + ->with('datastore') ->willReturn($logger); $this->container->set('logger.factory', $logger_factory); diff --git a/modules/datastore/tests/src/Kernel/Storage/DatabaseTableTest.php b/modules/datastore/tests/src/Kernel/Storage/DatabaseTableTest.php index 6d83573dff..9e5f46cb84 100644 --- a/modules/datastore/tests/src/Kernel/Storage/DatabaseTableTest.php +++ b/modules/datastore/tests/src/Kernel/Storage/DatabaseTableTest.php @@ -14,6 +14,7 @@ * @covers \Drupal\datastore\Storage\DatabaseTable * @coversDefaultClass \Drupal\datastore\Storage\DatabaseTable * + * @group dkan * @group datastore * @group kernel */ diff --git a/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php b/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php index e88a7c23e3..eeca736965 100644 --- a/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php +++ b/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php @@ -2,31 +2,34 @@ namespace Drupal\Tests\datastore\Unit\Controller; -use Drupal\datastore\DatastoreResource; -use Drupal\common\DatasetInfo; use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; -use Drupal\sqlite\Driver\Database\sqlite\Connection as SqliteConnection; -use MockChain\Options; -use Drupal\datastore\DatastoreService; -use PHPUnit\Framework\TestCase; -use Symfony\Component\DependencyInjection\Container; -use MockChain\Chain; +use Drupal\common\DatasetInfo; use Drupal\datastore\Controller\QueryController; +use Drupal\datastore\DatastoreResource; +use Drupal\datastore\DatastoreService; use Drupal\datastore\Service\Query; use Drupal\datastore\Storage\SqliteDatabaseTable; use Drupal\metastore\MetastoreApiResponse; use Drupal\metastore\NodeWrapper\Data; use Drupal\metastore\NodeWrapper\NodeDataFactory; use Drupal\metastore\Storage\DataFactory; +use Drupal\sqlite\Driver\Database\sqlite\Connection as SqliteConnection; use Ilbee\CSVResponse\CSVResponse as CsvResponse; +use MockChain\Chain; +use MockChain\Options; +use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; /** - * + * @group dkan + * @group datastore + * @group unit */ class QueryControllerTest extends TestCase { @@ -498,7 +501,11 @@ public function mockDatastoreTable() { $connection->query("INSERT INTO `datastore_2` VALUES ($row[0], '$row[1]', $row[2]);"); } - $storage = new SqliteDatabaseTable($connection, new DatastoreResource("2", "data.csv", "text/csv")); + $storage = new SqliteDatabaseTable( + $connection, + new DatastoreResource("2", "data.csv", "text/csv"), + $this->createStub(LoggerInterface::class) + ); $storage->setSchema([ 'fields' => [ 'record_number' => ['type' => 'int', 'not null' => TRUE], diff --git a/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php b/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php index ebe985a782..47fb6b10cd 100644 --- a/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php +++ b/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php @@ -2,34 +2,36 @@ namespace Drupal\Tests\datastore\Unit\Controller; -use Drupal\datastore\DatastoreResource; -use Drupal\common\DatasetInfo; use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; -use Drupal\sqlite\Driver\Database\sqlite\Connection as SqliteConnection; +use Drupal\Core\Database\Query\Select; +use Drupal\Tests\common\Unit\Connection; +use Drupal\common\DatasetInfo; use Drupal\datastore\Controller\QueryController; -use MockChain\Options; -use Drupal\datastore\DatastoreService; -use PHPUnit\Framework\TestCase; -use Symfony\Component\DependencyInjection\Container; -use MockChain\Chain; use Drupal\datastore\Controller\QueryDownloadController; +use Drupal\datastore\DatastoreResource; +use Drupal\datastore\DatastoreService; use Drupal\datastore\Service\Query; use Drupal\datastore\Storage\SqliteDatabaseTable; use Drupal\metastore\MetastoreApiResponse; use Drupal\metastore\NodeWrapper\Data; use Drupal\metastore\NodeWrapper\NodeDataFactory; use Drupal\metastore\Storage\DataFactory; +use Drupal\sqlite\Driver\Database\sqlite\Connection as SqliteConnection; +use MockChain\Chain; +use MockChain\Options; +use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; -use Drupal\Core\Database\Query\Select; -use Drupal\Tests\common\Unit\Connection; /** * @group dkan * @group datastore + * @group unit */ class QueryDownloadControllerTest extends TestCase { @@ -454,7 +456,11 @@ public function mockDatastoreTable($connection, $id, $csvFile, $fields) { $connection->query("INSERT INTO `datastore_$id` VALUES ($valuesStr);"); } - $storage = new SqliteDatabaseTable($connection, new DatastoreResource($id, "data-$id.csv", "text/csv")); + $storage = new SqliteDatabaseTable( + $connection, + new DatastoreResource($id, "data-$id.csv", "text/csv"), + $this->createStub(LoggerInterface::class) + ); $storage->setSchema([ 'fields' => $fields, ]); diff --git a/modules/datastore/tests/src/Unit/Service/Factory/ImportServiceFactoryTest.php b/modules/datastore/tests/src/Unit/Service/Factory/ImportServiceFactoryTest.php index c63c924545..9b10f09940 100644 --- a/modules/datastore/tests/src/Unit/Service/Factory/ImportServiceFactoryTest.php +++ b/modules/datastore/tests/src/Unit/Service/Factory/ImportServiceFactoryTest.php @@ -2,10 +2,11 @@ namespace Drupal\Tests\datastore\Unit\Service\Factory; -use Drupal\datastore\Storage\DatabaseTableFactory; use Drupal\datastore\Service\Factory\ImportServiceFactory; +use Drupal\datastore\Storage\DatabaseTableFactory; use Drupal\datastore\Storage\ImportJobStoreFactory; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; /** * @covers \Drupal\datastore\Service\Factory\ImportServiceFactory @@ -13,6 +14,7 @@ * * @group dkan * @group datastore + * @group unit */ class ImportServiceFactoryTest extends TestCase { @@ -26,7 +28,8 @@ public function testGetInstanceException() { ->getMock(), $this->getMockBuilder(DatabaseTableFactory::class) ->disableOriginalConstructor() - ->getMock() + ->getMock(), + $this->createStub(LoggerInterface::class) ); $this->expectException(\Exception::class); diff --git a/modules/datastore/tests/src/Unit/Service/ResourcePurgerTest.php b/modules/datastore/tests/src/Unit/Service/ResourcePurgerTest.php index ae9d36fb61..fb2921f01e 100644 --- a/modules/datastore/tests/src/Unit/Service/ResourcePurgerTest.php +++ b/modules/datastore/tests/src/Unit/Service/ResourcePurgerTest.php @@ -18,11 +18,12 @@ use MockChain\Chain; use MockChain\Options; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; /** - * Class ResourcePurgerTest - * - * @package Drupal\Tests\datastore\Service + * @group dkan + * @group datastore + * @group unit */ class ResourcePurgerTest extends TestCase { @@ -99,6 +100,7 @@ private function getCommonChain() { ->add('dkan.metastore.reference_lookup', ReferenceLookupInterface::class) ->add('dkan.metastore.storage', DataFactory::class) ->add('dkan.datastore.service', DatastoreService::class) + ->add('dkan.datastore.logger_channel', LoggerInterface::class) ->index(0); return (new Chain($this)) diff --git a/modules/datastore/tests/src/Unit/Storage/DatabaseTableFactoryTest.php b/modules/datastore/tests/src/Unit/Storage/DatabaseTableFactoryTest.php index 6822e42a55..a09f7c8701 100644 --- a/modules/datastore/tests/src/Unit/Storage/DatabaseTableFactoryTest.php +++ b/modules/datastore/tests/src/Unit/Storage/DatabaseTableFactoryTest.php @@ -2,16 +2,19 @@ namespace Drupal\Tests\datastore\Unit\Storage; -use Drupal\datastore\DatastoreResource; use Drupal\Core\Database\Connection; -use MockChain\Chain; +use Drupal\datastore\DatastoreResource; use Drupal\datastore\Storage\DatabaseTable; use Drupal\datastore\Storage\DatabaseTableFactory; use Drupal\indexer\IndexManager; +use MockChain\Chain; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; /** - * + * @group dkan + * @group datastore + * @group unit */ class DatabaseTableFactoryTest extends TestCase { @@ -28,7 +31,10 @@ public function test() { ->getMock(); $builder = $this->getMockBuilder(DatabaseTableFactory::class); - $factory = $builder->setConstructorArgs([$connection]) + $factory = $builder->setConstructorArgs([ + $connection, + $this->createStub(LoggerInterface::class + )]) ->onlyMethods(["getDatabaseTable"]) ->getMock(); diff --git a/modules/datastore/tests/src/Unit/Storage/DatabaseTableTest.php b/modules/datastore/tests/src/Unit/Storage/DatabaseTableTest.php index c1c8e146cc..81e9f93f46 100644 --- a/modules/datastore/tests/src/Unit/Storage/DatabaseTableTest.php +++ b/modules/datastore/tests/src/Unit/Storage/DatabaseTableTest.php @@ -2,21 +2,24 @@ namespace Drupal\Tests\datastore\Storage; -use Drupal\datastore\DatastoreResource; use Drupal\Core\Database\Connection; use Drupal\Core\Database\DatabaseExceptionWrapper; use Drupal\Core\Database\Query\Insert; use Drupal\Core\Database\Query\Select; -use Drupal\mysql\Driver\Database\mysql\Schema; use Drupal\Core\Database\StatementWrapper; use Drupal\common\Storage\Query; +use Drupal\datastore\DatastoreResource; +use Drupal\datastore\Storage\DatabaseTable; +use Drupal\mysql\Driver\Database\mysql\Schema; use MockChain\Chain; use MockChain\Sequence; -use Drupal\datastore\Storage\DatabaseTable; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; /** - * + * @group dkan + * @group datastore + * @group unit */ class DatabaseTableTest extends TestCase { @@ -27,7 +30,8 @@ public function testConstruction() { $databaseTable = new DatabaseTable( $this->getConnectionChain()->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->assertTrue(is_object($databaseTable)); } @@ -40,7 +44,8 @@ public function testGetSchema() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $schema = $databaseTable->getSchema(); @@ -105,7 +110,8 @@ public function testRetrieveAll() { $databaseTable = new DatabaseTable( $connection, - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->assertEquals([], $databaseTable->retrieveAll()); } @@ -127,7 +133,8 @@ public function testStore() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->assertEquals("1", $databaseTable->store('["Gerardo", "Gonzalez"]', "1")); } @@ -149,7 +156,8 @@ public function testStoreFieldCountException() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->expectExceptionMessageMatches("/The number of fields and data given do not match:/"); $this->assertEquals("1", $databaseTable->store('["Foobar"]', "1")); @@ -172,7 +180,8 @@ public function testStoreMultiple() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $data = [ '["Gerardo", "Gonzalez"]', @@ -199,7 +208,8 @@ public function testStoreMultipleFieldCountException() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $data = [ '["One"]', @@ -223,7 +233,8 @@ public function testCount() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->assertEquals(1, $databaseTable->count()); } @@ -241,7 +252,8 @@ public function testGetSummary() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $actual = json_decode(json_encode( @@ -262,7 +274,8 @@ public function testDestruct() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $databaseTable->destruct(); $this->assertTrue(TRUE); @@ -286,7 +299,8 @@ public function testPrepareDataJsonDecodeNull() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->expectExceptionMessage('Import for 1 returned an error when preparing table header: {"foo":"bar"}'); $this->assertEquals("1", $databaseTable->store('{"foo":"bar"}', "1")); @@ -309,7 +323,8 @@ public function testPrepareDataNonArray() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->expectExceptionMessage("Import for 1 error when decoding foobar"); $this->assertEquals("1", $databaseTable->store("foobar", "1")); @@ -330,7 +345,8 @@ public function testQuery() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->assertEquals([], $databaseTable->query($query)); @@ -350,7 +366,8 @@ public function testQueryExceptionDatabaseInternalError() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->expectExceptionMessage("Database internal error."); @@ -371,7 +388,8 @@ public function testQueryColumnNotFound() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->expectExceptionMessage("Column not found"); @@ -392,7 +410,8 @@ public function testNoFulltextIndexFound() { $databaseTable = new DatabaseTable( $connectionChain->getMock(), - $this->getResource() + $this->getResource(), + $this->createStub(LoggerInterface::class) ); $this->expectExceptionMessage("You have attempted a fulltext match against a column that is not indexed for fulltext searching"); @@ -405,17 +424,17 @@ public function testNoFulltextIndexFound() { private function getConnectionChain() { $fieldInfo = [ (object) [ - 'Field' => "record_number", + 'Field' => "record_number", 'Type' => "int(10)", 'Extra' => "auto_increment", ], (object) [ - 'Field' => "first_name", + 'Field' => "first_name", 'Type' => "varchar(10)" ], (object) [ - 'Field' => - "last_name", + 'Field' => + "last_name", 'Type' => 'text' ] ]; diff --git a/modules/harvest/harvest.services.yml b/modules/harvest/harvest.services.yml index a25cb0f633..0140b56111 100644 --- a/modules/harvest/harvest.services.yml +++ b/modules/harvest/harvest.services.yml @@ -5,8 +5,8 @@ services: - '@dkan.harvest.storage.database_table' - '@dkan.metastore.service' - '@dkan.harvest.harvest_plan_repository' + - '@dkan.harvest.logger_channel' calls: - - [ setLoggerFactory, [ '@logger.factory' ] ] - [ setEntityTypeManager, [ '@entity_type.manager' ] ] dkan.harvest.utility: class: Drupal\harvest\HarvestUtility diff --git a/modules/harvest/src/HarvestService.php b/modules/harvest/src/HarvestService.php index 5193ddf45a..096a30b51e 100644 --- a/modules/harvest/src/HarvestService.php +++ b/modules/harvest/src/HarvestService.php @@ -4,11 +4,11 @@ use Contracts\FactoryInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\common\LoggerTrait; use Drupal\harvest\Entity\HarvestPlanRepository; use Drupal\metastore\MetastoreService; use Harvest\ETL\Factory; use Harvest\Harvester; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -19,7 +19,6 @@ */ class HarvestService implements ContainerInjectionInterface { - use LoggerTrait; use OrphanDatasetsProcessor; /** @@ -45,6 +44,13 @@ class HarvestService implements ContainerInjectionInterface { */ private HarvestPlanRepository $harvestPlanRepository; + /** + * DKAN logger channel. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Create. * @@ -54,7 +60,8 @@ public static function create(ContainerInterface $container) { return new self( $container->get('dkan.harvest.storage.database_table'), $container->get('dkan.metastore.service'), - $container->get('dkan.harvest.harvest_plan_repository') + $container->get('dkan.harvest.harvest_plan_repository'), + $container->get('dkan.harvest.logger_channel') ); } @@ -64,11 +71,13 @@ public static function create(ContainerInterface $container) { public function __construct( FactoryInterface $storeFactory, MetastoreService $metastore, - HarvestPlanRepository $harvestPlansRepository + HarvestPlanRepository $harvestPlansRepository, + LoggerInterface $loggerChannel ) { $this->storeFactory = $storeFactory; $this->metastore = $metastore; $this->harvestPlanRepository = $harvestPlansRepository; + $this->logger = $loggerChannel; } /** @@ -318,7 +327,7 @@ protected function setDatasetStatus($runInfoStatus, string $datasetId, string $m $this->metastore->$method('dataset', $datasetId); } catch (\Exception $e) { - $this->error("Error applying method {$method} to dataset {$datasetId}: {$e->getMessage()}"); + $this->logger->error("Error applying method {$method} to dataset {$datasetId}: {$e->getMessage()}"); return FALSE; } } diff --git a/modules/harvest/tests/src/Unit/HarvestServiceTest.php b/modules/harvest/tests/src/Unit/HarvestServiceTest.php index ba8465829c..7fee23eabb 100644 --- a/modules/harvest/tests/src/Unit/HarvestServiceTest.php +++ b/modules/harvest/tests/src/Unit/HarvestServiceTest.php @@ -4,20 +4,18 @@ use Drupal\Component\DependencyInjection\Container; use Drupal\Core\Entity\EntityTypeManager; -use Drupal\Core\Logger\LoggerChannelFactory; -use Drupal\Core\Logger\LoggerChannelInterface; -use Drupal\harvest\Entity\HarvestPlanRepository; use Drupal\Tests\common\Traits\ServiceCheckTrait; use Drupal\datastore\Storage\DatabaseTable; +use Drupal\harvest\Entity\HarvestPlanRepository; use Drupal\harvest\HarvestService; use Drupal\harvest\Storage\DatabaseTableFactory; use Drupal\metastore\MetastoreService; -use Drupal\node\NodeStorage; use Harvest\Harvester; use MockChain\Chain; use MockChain\Options; use MockChain\Sequence; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; /** * @covers \Drupal\harvest\HarvestService @@ -32,8 +30,6 @@ class HarvestServiceTest extends TestCase { use ServiceCheckTrait; - private $storageFactory; - /** * */ @@ -45,7 +41,8 @@ public function testGetHarvestPlan() { $service = new HarvestService( $this->createStub(DatabaseTableFactory::class), $this->createStub(MetastoreService::class), - $planRepository + $planRepository, + $this->createStub(LoggerInterface::class) ); $plan = $service->getHarvestPlan('test'); $this->assertEquals('Hello', $plan); @@ -71,6 +68,7 @@ public function testGetHarvestRunInfo() { $storeFactory, $this->getMetastoreMockChain(), $this->createStub(HarvestPlanRepository::class), + $this->createStub(LoggerInterface::class), ]) ->onlyMethods(['getDkanHarvesterInstance']) ->getMock(); @@ -90,20 +88,10 @@ private function getMetastoreMockChain() { ->getMock(); } - /** - * Private. - */ - private function getEntityTypeManagerMockChain() { - return (new Chain($this)) - ->add(EntityTypeManager::class, 'getStorage', NodeStorage::class) - ->getMock(); - } - /** * */ public function testPublish() { - $datasetUuids = ['abcd-1001', 'abcd-1002', 'abcd-1003', 'abcd-1004']; $lastRunInfo = (new Sequence()) ->add(json_encode((object) [ @@ -130,15 +118,13 @@ public function testPublish() { ->add(TRUE); $logger = (new Chain($this)) - ->add(LoggerChannelFactory::class, 'get', LoggerChannelInterface::class) - ->add(LoggerChannelInterface::class, 'error', NULL, 'error'); + ->add(LoggerInterface::class, 'error', NULL, 'error'); - $container = $this->getCommonMockChain() + $container = $this->getCommonMockChain($logger->getMock()) ->add(DatabaseTable::class, "retrieve", $lastRunInfo) ->add(MetastoreService::class, 'publish', $metastorePublicationResults); $service = HarvestService::create($container->getMock()); - $service->setLoggerFactory($logger->getMock()); $result = $service->publish('1'); $this->assertEquals(['abcd-1003'], $result); @@ -176,17 +162,14 @@ public function testArchive() { ->add(new \Exception('FooBar')) // abcd-1003 should be archived without issue. ->add(TRUE); - $logger = (new Chain($this)) - ->add(LoggerChannelFactory::class, 'get', LoggerChannelInterface::class) - ->add(LoggerChannelInterface::class, 'error', NULL, 'error'); + ->add(LoggerInterface::class, 'error', NULL, 'error'); - $container = $this->getCommonMockChain() + $container = $this->getCommonMockChain($logger->getMock()) ->add(DatabaseTable::class, "retrieve", $lastRunInfo) ->add(MetastoreService::class, 'archive', $metastoreArchiveResults); $service = HarvestService::create($container->getMock()); - $service->setLoggerFactory($logger->getMock()); $result = $service->archive('1'); $this->assertEquals(['abcd-1003'], $result); @@ -220,14 +203,22 @@ public function testGetOrphansFromCompleteHarvest() { $this->assertEquals(['4'], array_values($removedIds)); } - private function getCommonMockChain() { + private function getCommonMockChain($logger = NULL) { $options = (new Options()) ->add('dkan.harvest.storage.database_table', DatabaseTableFactory::class) ->add('dkan.metastore.service', MetastoreService::class) ->add('entity_type.manager', EntityTypeManager::class) - ->add('dkan.harvest.harvest_plan_repository', HarvestPlanRepository::class) - ->index(0); + ->add('dkan.harvest.harvest_plan_repository', HarvestPlanRepository::class); + + if ($logger) { + $options->add('dkan.harvest.logger_channel', $logger) + ->index(0); + } + else { + $options->add('dkan.harvest.logger_channel', LoggerInterface::class) + ->index(0); + } return (new Chain($this)) ->add(Container::class, 'get', $options) diff --git a/modules/harvest/tests/src/Unit/WebServiceApiTest.php b/modules/harvest/tests/src/Unit/WebServiceApiTest.php index 5ccb8e8303..aae7a6b612 100644 --- a/modules/harvest/tests/src/Unit/WebServiceApiTest.php +++ b/modules/harvest/tests/src/Unit/WebServiceApiTest.php @@ -2,21 +2,22 @@ namespace Drupal\Tests\harvest\Unit; +use Contracts\Mock\Storage\MemoryFactory; +use Drupal\Component\DependencyInjection\Container; +use Drupal\Tests\common\Traits\ServiceCheckTrait; use Drupal\harvest\Entity\HarvestPlanRepository; +use Drupal\harvest\HarvestService; +use Drupal\harvest\WebServiceApi; use Drupal\metastore\MetastoreService; -use Drupal\Tests\common\Traits\ServiceCheckTrait; -use MockChain\Options; -use Drupal\Component\DependencyInjection\Container; use MockChain\Chain; +use MockChain\Options; +use PHPUnit\Framework\TestCase; use Procrastinator\Result; -use Symfony\Component\HttpFoundation\Request; +use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; -use Contracts\Mock\Storage\MemoryFactory; -use Drupal\harvest\HarvestService; -use PHPUnit\Framework\TestCase; -use Drupal\harvest\WebServiceApi; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * @covers \Drupal\harvest\WebServiceApi @@ -65,7 +66,8 @@ public function containerGet($input) { return new HarvestService( new MemoryFactory(), $this->getMetastoreMockChain(), - $this->getHarvestEntityRepositoryMock() + $this->getHarvestEntityRepositoryMock(), + $this->createStub(LoggerInterface::class) ); break; diff --git a/modules/metastore/metastore.services.yml b/modules/metastore/metastore.services.yml index 7261aec005..80641f9247 100644 --- a/modules/metastore/metastore.services.yml +++ b/modules/metastore/metastore.services.yml @@ -5,6 +5,7 @@ services: - '@dkan.metastore.schema_retriever' - '@dkan.metastore.storage' - '@dkan.metastore.valid_metadata' + - '@dkan.common.logger_channel' dkan.metastore.lifecycle: class: \Drupal\metastore\LifeCycle\LifeCycle @@ -34,6 +35,7 @@ services: arguments: - '@entity_type.manager' - '@config.factory' + - '@dkan.common.logger_channel' dkan.metastore.referencer: class: \Drupal\metastore\Reference\Referencer @@ -43,16 +45,14 @@ services: - '@dkan.metastore.url_generator' - '@http_client' - '@file.mime_type.guesser.extension' - calls: - - [setLoggerFactory, ['@logger.factory']] + - '@dkan.common.logger_channel' dkan.metastore.dereferencer: class: \Drupal\metastore\Reference\Dereferencer arguments: - '@config.factory' - '@dkan.metastore.storage' - calls: - - [setLoggerFactory, ['@logger.factory']] + - '@dkan.common.logger_channel' dkan.metastore.orphan_checker: class: \Drupal\metastore\Reference\OrphanChecker @@ -70,6 +70,7 @@ services: class: \Drupal\metastore\Storage\ResourceMapperDatabaseTable arguments: - '@database' + - '@dkan.common.logger_channel' dkan.metastore.event_subscriber: class: \Drupal\metastore\EventSubscriber\MetastoreSubscriber diff --git a/modules/metastore/src/MetastoreService.php b/modules/metastore/src/MetastoreService.php index 849d2dde1e..4fbd9788ee 100644 --- a/modules/metastore/src/MetastoreService.php +++ b/modules/metastore/src/MetastoreService.php @@ -4,13 +4,13 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\common\EventDispatcherTrait; -use Drupal\common\LoggerTrait; use Drupal\metastore\Exception\CannotChangeUuidException; use Drupal\metastore\Exception\ExistingObjectException; use Drupal\metastore\Exception\MissingObjectException; use Drupal\metastore\Exception\UnmodifiedObjectException; use Drupal\metastore\Storage\DataFactory; use Drupal\metastore\Storage\MetastoreStorageInterface; +use Psr\Log\LoggerInterface; use RootedData\RootedJsonData; use Rs\Json\Merge\Patch; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -20,7 +20,6 @@ */ class MetastoreService implements ContainerInjectionInterface { use EventDispatcherTrait; - use LoggerTrait; const EVENT_DATA_GET = 'dkan_metastore_data_get'; const EVENT_DATA_GET_ALL = 'dkan_metastore_data_get_all'; @@ -35,7 +34,7 @@ class MetastoreService implements ContainerInjectionInterface { /** * Storage factory. * - * @var \Drupal\metastore\Storage\MetastoreStorageFactoryInterface + * @var \Drupal\metastore\Storage\DataFactory */ private $storageFactory; @@ -53,26 +52,40 @@ class MetastoreService implements ContainerInjectionInterface { */ private $validMetadataFactory; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Inherited. * * {@inheritDoc} */ public static function create(ContainerInterface $container) { - return new MetastoreService( + return new static( $container->get('dkan.metastore.schema_retriever'), $container->get('dkan.metastore.storage'), - $container->get('dkan.metastore.valid_metadata') + $container->get('dkan.metastore.valid_metadata'), + $container->get('dkan.common.logger_channel') ); } /** * Constructor. */ - public function __construct(SchemaRetriever $schemaRetriever, DataFactory $factory, ValidMetadataFactory $validMetadataFactory) { + public function __construct( + SchemaRetriever $schemaRetriever, + DataFactory $factory, + ValidMetadataFactory $validMetadataFactory, + LoggerInterface $loggerChannel + ) { $this->schemaRetriever = $schemaRetriever; $this->storageFactory = $factory; $this->validMetadataFactory = $validMetadataFactory; + $this->logger = $loggerChannel; } /** @@ -203,7 +216,7 @@ function ($jsonString) use ($schema_id) { }); } catch (\Exception $e) { - $this->log('metastore', 'A JSON string failed validation.', + $this->logger->log('metastore', 'A JSON string failed validation.', ['@schema_id' => $schema_id, '@json' => $jsonString] ); return NULL; @@ -549,7 +562,7 @@ public static function removeReferences(RootedJsonData $object, $prefix = "%"): * Metadata. Can be a RootedJsonData object, a stdObject or JSON string. * * @return string - * An md5 hash of the normalized metadata. + * An MD5 hash of the normalized metadata. * * @todo This should probably be somewhere else. */ diff --git a/modules/metastore/src/NodeWrapper/Data.php b/modules/metastore/src/NodeWrapper/Data.php index 75b35d925f..905e41297c 100644 --- a/modules/metastore/src/NodeWrapper/Data.php +++ b/modules/metastore/src/NodeWrapper/Data.php @@ -2,10 +2,9 @@ namespace Drupal\metastore\NodeWrapper; -use Drupal\common\Exception\DataNodeLifeCycleEntityValidationException; -use Drupal\common\LoggerTrait; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManager; +use Drupal\common\Exception\DataNodeLifeCycleEntityValidationException; use Drupal\metastore\MetastoreItemInterface; use Drupal\node\Entity\Node; @@ -13,7 +12,6 @@ * MetastoreItem object that wraps a data node, provides additional methods. */ class Data implements MetastoreItemInterface { - use LoggerTrait; /** * Node. @@ -32,14 +30,14 @@ class Data implements MetastoreItemInterface { /** * Entity Type Manager. * - * @var Drupal\Core\Entity\EntityTypeManager + * @var \Drupal\Core\Entity\EntityTypeManager */ private $entityTypeManager; /** * Entity Node Storage. * - * @var Drupal\Core\Entity\EntityStorageInterface + * @var \Drupal\Core\Entity\EntityStorageInterface */ private $nodeStorage; diff --git a/modules/metastore/src/Plugin/QueueWorker/OrphanReferenceProcessor.php b/modules/metastore/src/Plugin/QueueWorker/OrphanReferenceProcessor.php index 51a3476933..cf698bddb3 100644 --- a/modules/metastore/src/Plugin/QueueWorker/OrphanReferenceProcessor.php +++ b/modules/metastore/src/Plugin/QueueWorker/OrphanReferenceProcessor.php @@ -4,13 +4,12 @@ namespace Drupal\metastore\Plugin\QueueWorker; -use Drupal\common\LoggerTrait; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Queue\QueueWorkerBase; -use Drupal\node\NodeStorageInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\common\EventDispatcherTrait; use Drupal\metastore\ReferenceLookupInterface; +use Drupal\node\NodeStorageInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Verifies if a dataset property reference is orphaned, then deletes it. @@ -24,7 +23,7 @@ * @codeCoverageIgnore */ class OrphanReferenceProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface { - use LoggerTrait; + use EventDispatcherTrait; const EVENT_ORPHANING_DISTRIBUTION = 'metastore_orphaning_distribution'; @@ -82,7 +81,6 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('dkan.common.node_storage'), $container->get('dkan.metastore.reference_lookup') ); - $me->setLoggerFactory($container->get('logger.factory')); return $me; } diff --git a/modules/metastore/src/Plugin/QueueWorker/OrphanResourceRemover.php b/modules/metastore/src/Plugin/QueueWorker/OrphanResourceRemover.php index 33ac924251..5b03ab01a8 100644 --- a/modules/metastore/src/Plugin/QueueWorker/OrphanResourceRemover.php +++ b/modules/metastore/src/Plugin/QueueWorker/OrphanResourceRemover.php @@ -2,7 +2,6 @@ namespace Drupal\metastore\Plugin\QueueWorker; -use Drupal\common\LoggerTrait; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Queue\QueueWorkerBase; use Drupal\metastore\ResourceMapper; @@ -22,7 +21,6 @@ * @codeCoverageIgnore */ class OrphanResourceRemover extends QueueWorkerBase implements ContainerFactoryPluginInterface { - use LoggerTrait; /** * Resource mapper service. diff --git a/modules/metastore/src/Reference/Dereferencer.php b/modules/metastore/src/Reference/Dereferencer.php index f495307841..017e5ab2ee 100644 --- a/modules/metastore/src/Reference/Dereferencer.php +++ b/modules/metastore/src/Reference/Dereferencer.php @@ -4,7 +4,8 @@ use Contracts\FactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\common\LoggerTrait; +use Psr\Log\LoggerInterface; + use Drupal\metastore\Exception\MissingObjectException; /** @@ -12,7 +13,6 @@ */ class Dereferencer { use HelperTrait; - use LoggerTrait; /** * Storage factory interface service. @@ -21,16 +21,28 @@ class Dereferencer { */ private $storageFactory; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor. */ - public function __construct(ConfigFactoryInterface $configService, FactoryInterface $storageFactory) { + public function __construct( + ConfigFactoryInterface $configService, + FactoryInterface $storageFactory, + LoggerInterface $loggerChannel + ) { $this->setConfigService($configService); $this->storageFactory = $storageFactory; + $this->logger = $loggerChannel; } /** - * Replaces value references in a dataset with with their actual values. + * Replaces value references in a dataset with their actual values. * * @param object $data * The json metadata object. @@ -91,7 +103,7 @@ private function dereferencePropertyUuid(string $property_id, $uuid) { return $this->dereferenceSingle($property_id, $uuid); } else { - $this->log('value_referencer', 'Unexpected data type when dereferencing property_id: @property_id with uuid: @uuid', + $this->logger->log('value_referencer', 'Unexpected data type when dereferencing property_id: @property_id with uuid: @uuid', [ '@property_id' => $property_id, '@uuid' => var_export($uuid, TRUE), @@ -156,7 +168,7 @@ private function dereferenceSingle(string $property_id, string $uuid) { // If a property node was not found, it most likely means it was deleted // while still being referenced. - $this->log( + $this->logger->log( 'value_referencer', 'Property @property_id reference @uuid not found', [ diff --git a/modules/metastore/src/Reference/ReferenceLookup.php b/modules/metastore/src/Reference/ReferenceLookup.php index df5c52ce3c..20d40e62e2 100644 --- a/modules/metastore/src/Reference/ReferenceLookup.php +++ b/modules/metastore/src/Reference/ReferenceLookup.php @@ -2,14 +2,12 @@ namespace Drupal\metastore\Reference; -use Drupal\common\LoggerTrait; -use Drupal\metastore\Factory\MetastoreItemFactoryInterface; -use Drupal\metastore\ReferenceLookupInterface; - use Contracts\FactoryInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\metastore\Factory\MetastoreItemFactoryInterface; +use Drupal\metastore\ReferenceLookupInterface; use RootedData\RootedJsonData; /** @@ -17,7 +15,6 @@ */ class ReferenceLookup implements ReferenceLookupInterface { use HelperTrait; - use LoggerTrait; /** * Metastore Storage service. diff --git a/modules/metastore/src/Reference/Referencer.php b/modules/metastore/src/Reference/Referencer.php index e78ee33df6..be56c33f6f 100644 --- a/modules/metastore/src/Reference/Referencer.php +++ b/modules/metastore/src/Reference/Referencer.php @@ -3,17 +3,16 @@ namespace Drupal\metastore\Reference; use Contracts\FactoryInterface; -use Drupal\common\DataResource; -use Drupal\common\LoggerTrait; -use Drupal\common\UrlHostTokenResolver; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\StreamWrapper\StreamWrapperManager; +use Drupal\common\DataResource; +use Drupal\common\UrlHostTokenResolver; use Drupal\metastore\Exception\AlreadyRegistered; use Drupal\metastore\MetastoreService; use Drupal\metastore\ResourceMapper; - use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; +use Psr\Log\LoggerInterface; use Symfony\Component\Mime\MimeTypeGuesserInterface; /** @@ -21,7 +20,6 @@ */ class Referencer { use HelperTrait; - use LoggerTrait; /** * Default Mime Type to use when mime type detection fails. @@ -58,6 +56,13 @@ class Referencer { */ protected $mimeTypeGuesser; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor. * @@ -71,20 +76,23 @@ class Referencer { * Guzzle http client. * @param \Symfony\Component\Mime\MimeTypeGuesserInterface $mimeTypeGuesser * The MIME type guesser. + * @param \Psr\Log\LoggerInterface $loggerChannel + * DKAN logger channel service. */ public function __construct( ConfigFactoryInterface $configService, FactoryInterface $storageFactory, MetastoreUrlGenerator $metastoreUrlGenerator, Client $httpClient, - MimeTypeGuesserInterface $mimeTypeGuesser + MimeTypeGuesserInterface $mimeTypeGuesser, + LoggerInterface $loggerChannel ) { $this->setConfigService($configService); $this->storageFactory = $storageFactory; - $this->setLoggerFactory(\Drupal::service('logger.factory')); $this->metastoreUrlGenerator = $metastoreUrlGenerator; $this->httpClient = $httpClient; $this->mimeTypeGuesser = $mimeTypeGuesser; + $this->logger = $loggerChannel; } /** @@ -180,7 +188,7 @@ private function referenceSingle(string $property_id, $value) { return $uuid; } else { - $this->log( + $this->logger->log( 'value_referencer', 'Neither found an existing nor could create a new reference for property_id: @property_id with value: @value', [ @@ -318,6 +326,8 @@ private function handleExistingResource(DataResource $existing, string $mimeType /** * Private. + * + * @todo Inject this service. */ private function getFileMapper(): ResourceMapper { return \Drupal::service('dkan.metastore.resource_mapper'); @@ -339,7 +349,7 @@ private function getLocalMimeType(string $downloadUrl): ?string { // If we couldn't find a mime type, log an error notifying the user. if (is_null($mime_type)) { $filename = basename($downloadUrl); - $this->log('value_referencer', 'Unable to determine mime type of file with name "@name".', [ + $this->logger->log('value_referencer', 'Unable to determine mime type of file with name "@name".', [ '@name' => $filename, ]); } diff --git a/modules/metastore/src/ResourceMapper.php b/modules/metastore/src/ResourceMapper.php index 6271799b9f..76f359c377 100644 --- a/modules/metastore/src/ResourceMapper.php +++ b/modules/metastore/src/ResourceMapper.php @@ -29,6 +29,8 @@ class ResourceMapper { * Database storage service. * * @var \Drupal\common\Storage\DatabaseTableInterface + * + * @todo Deprecate/remove this form of storage. */ private $store; diff --git a/modules/metastore/src/Storage/Data.php b/modules/metastore/src/Storage/Data.php index d2a2f81c37..d72403ff1b 100644 --- a/modules/metastore/src/Storage/Data.php +++ b/modules/metastore/src/Storage/Data.php @@ -2,7 +2,6 @@ namespace Drupal\metastore\Storage; -use Drupal\common\LoggerTrait; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityPublishedInterface; @@ -12,6 +11,7 @@ use Drupal\metastore\Exception\MissingObjectException; use Drupal\metastore\MetastoreService; use Drupal\workflows\WorkflowInterface; +use Psr\Log\LoggerInterface; /** * Abstract metastore storage class, for using Drupal entities. @@ -20,8 +20,6 @@ */ abstract class Data implements MetastoreEntityStorageInterface { - use LoggerTrait; - /** * Entity type manager. * @@ -92,14 +90,27 @@ abstract class Data implements MetastoreEntityStorageInterface { */ protected $configFactory; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor. */ - public function __construct(string $schemaId, EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory) { + public function __construct( + string $schemaId, + EntityTypeManagerInterface $entityTypeManager, + ConfigFactoryInterface $config_factory, + LoggerInterface $loggerChannel + ) { $this->entityTypeManager = $entityTypeManager; $this->entityStorage = $this->entityTypeManager->getStorage($this->entityType); $this->schemaId = $schemaId; $this->configFactory = $config_factory; + $this->logger = $loggerChannel; } /** @@ -459,7 +470,7 @@ private function htmlPurifier(string $input) { // Ensure the tmp cache directory exists. if (!is_dir($cache_dir) && !mkdir($cache_dir)) { - $this->log('metastore', 'Failed to create cache directory for HTML purifier'); + $this->logger->log('metastore', 'Failed to create cache directory for HTML purifier'); } else { $config['Cache.SerializerPath'] = $cache_dir; diff --git a/modules/metastore/src/Storage/DataFactory.php b/modules/metastore/src/Storage/DataFactory.php index e7fb337f59..d9b2cdd091 100644 --- a/modules/metastore/src/Storage/DataFactory.php +++ b/modules/metastore/src/Storage/DataFactory.php @@ -5,6 +5,7 @@ use Contracts\FactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManager; +use Psr\Log\LoggerInterface; /** * Data factory. @@ -32,12 +33,24 @@ class DataFactory implements FactoryInterface { */ protected $configFactory; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor. */ - public function __construct(EntityTypeManager $entityTypeManager, ConfigFactoryInterface $config_factory) { + public function __construct( + EntityTypeManager $entityTypeManager, + ConfigFactoryInterface $config_factory, + LoggerInterface $loggerChannel + ) { $this->entityTypeManager = $entityTypeManager; $this->configFactory = $config_factory; + $this->logger = $loggerChannel; } /** @@ -88,7 +101,12 @@ private function getEntityTypeBySchema(string $schema_id) : string { * Storage object. */ protected function createNodeInstance(string $identifier) { - return new NodeData($identifier, $this->entityTypeManager, $this->configFactory); + return new NodeData( + $identifier, + $this->entityTypeManager, + $this->configFactory, + $this->logger + ); } /** diff --git a/modules/metastore/src/Storage/NodeData.php b/modules/metastore/src/Storage/NodeData.php index 72c8c49afc..d8265f192e 100644 --- a/modules/metastore/src/Storage/NodeData.php +++ b/modules/metastore/src/Storage/NodeData.php @@ -4,6 +4,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Psr\Log\LoggerInterface; /** * Node Data. @@ -13,14 +14,19 @@ class NodeData extends Data { /** * NodeData constructor. */ - public function __construct(string $schemaId, EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory) { + public function __construct( + string $schemaId, + EntityTypeManagerInterface $entityTypeManager, + ConfigFactoryInterface $config_factory, + LoggerInterface $loggerChannel + ) { $this->entityType = 'node'; $this->bundle = 'data'; - $this->bundleKey = "type"; - $this->labelKey = "title"; - $this->schemaIdField = "field_data_type"; - $this->metadataField = "field_json_metadata"; - parent::__construct($schemaId, $entityTypeManager, $config_factory); + $this->bundleKey = 'type'; + $this->labelKey = 'title'; + $this->schemaIdField = 'field_data_type'; + $this->metadataField = 'field_json_metadata'; + parent::__construct($schemaId, $entityTypeManager, $config_factory, $loggerChannel); } /** diff --git a/modules/metastore/src/Storage/ResourceMapperDatabaseTable.php b/modules/metastore/src/Storage/ResourceMapperDatabaseTable.php index 32f159987d..e69333e713 100644 --- a/modules/metastore/src/Storage/ResourceMapperDatabaseTable.php +++ b/modules/metastore/src/Storage/ResourceMapperDatabaseTable.php @@ -2,16 +2,19 @@ namespace Drupal\metastore\Storage; -use Drupal\common\LoggerTrait; -use Drupal\common\Storage\AbstractDatabaseTable; use Drupal\Core\Database\Connection; +use Drupal\common\Storage\AbstractDatabaseTable; use Psr\Log\LogLevel; +use Psr\Log\LoggerInterface; /** * Database storage object. + * + * @deprecated Use resource_mapping entity type instead. + * + * @see \Drupal\metastore\Entity\ResourceMapping */ class ResourceMapperDatabaseTable extends AbstractDatabaseTable { - use LoggerTrait; /** * Resource mapper database table schema. @@ -20,11 +23,22 @@ class ResourceMapperDatabaseTable extends AbstractDatabaseTable { */ protected $schema; + /** + * DKAN logger channel service. + * + * @var \Psr\Log\LoggerInterface + */ + private LoggerInterface $logger; + /** * Constructor. */ - public function __construct(Connection $connection) { + public function __construct( + Connection $connection, + LoggerInterface $loggerChannel + ) { parent::__construct($connection); + $this->logger = $loggerChannel; $schema = []; @@ -91,7 +105,7 @@ protected function prepareData(string $data, string $id = NULL): array { } if ($decoded === NULL) { - $this->log( + $this->logger->log( 'dkan_metastore_filemapper', "Error decoding id:@id, data: @data.", ['@id' => $id, '@data' => $data], @@ -100,7 +114,7 @@ protected function prepareData(string $data, string $id = NULL): array { throw new \Exception("Import for {$id} error when decoding {$data}"); } elseif (!is_object($decoded)) { - $this->log( + $this->logger->log( 'dkan_metastore_filemapper', "Object expected while decoding id:@id, data: @data.", ['@id' => $id, '@data' => $data], diff --git a/modules/metastore/tests/src/Unit/MetastoreControllerTest.php b/modules/metastore/tests/src/Unit/MetastoreControllerTest.php index 21a0f1e1d2..852b7b58d0 100644 --- a/modules/metastore/tests/src/Unit/MetastoreControllerTest.php +++ b/modules/metastore/tests/src/Unit/MetastoreControllerTest.php @@ -5,32 +5,34 @@ use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\metastore\Controller\MetastoreController; use Drupal\metastore\DatasetApiDocs; use Drupal\metastore\Exception\ExistingObjectException; use Drupal\metastore\Exception\MissingObjectException; use Drupal\metastore\Exception\UnmodifiedObjectException; -use Drupal\metastore\ValidMetadataFactory; -use Drupal\metastore\Storage\Data; -use Drupal\metastore\MetastoreService; -use Drupal\metastore\Controller\MetastoreController; use Drupal\metastore\MetastoreApiResponse; +use Drupal\metastore\MetastoreService; use Drupal\metastore\NodeWrapper\Data as NodeWrapperData; use Drupal\metastore\NodeWrapper\NodeDataFactory; use Drupal\metastore\SchemaRetriever; +use Drupal\metastore\Storage\Data; use Drupal\metastore\Storage\NodeData; -use Drupal\Core\Entity\EntityStorageInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Entity\Query\QueryInterface; - +use Drupal\metastore\ValidMetadataFactory; use MockChain\Chain; use MockChain\Options; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; use RootedData\RootedJsonData; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; /** - * + * @group dkan + * @group metastore + * @group unit */ class MetastoreControllerTest extends TestCase { @@ -131,7 +133,7 @@ public function testGet() { $configFactoryMock = (new Chain($this)) ->add(ConfigFactoryInterface::class, 'get', $immutableConfig) ->getMock(); - $nodeDataMock = new NodeData($schema_id, $entityTypeManagerMock, $configFactoryMock); + $nodeDataMock = new NodeData($schema_id, $entityTypeManagerMock, $configFactoryMock, $this->createStub(LoggerInterface::class)); $container = $this->getCommonMockChain() ->add(MetastoreService::class, 'getStorage', $nodeDataMock) ->getMock(); diff --git a/modules/metastore/tests/src/Unit/MetastoreServiceTest.php b/modules/metastore/tests/src/Unit/MetastoreServiceTest.php index 588b5971dd..ffb78ec53b 100644 --- a/modules/metastore/tests/src/Unit/MetastoreServiceTest.php +++ b/modules/metastore/tests/src/Unit/MetastoreServiceTest.php @@ -2,27 +2,31 @@ namespace Drupal\Tests\metastore\Unit; -use Drupal\common\Events\Event; -use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Component\DependencyInjection\Container; +use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; +use Drupal\common\Events\Event; +use Drupal\Core\Logger\LoggerChannelInterface; use Drupal\metastore\Exception\ExistingObjectException; use Drupal\metastore\Exception\MissingObjectException; use Drupal\metastore\Exception\UnmodifiedObjectException; -use Drupal\metastore\ValidMetadataFactory; use Drupal\metastore\MetastoreService; use Drupal\metastore\SchemaRetriever; use Drupal\metastore\Storage\DataFactory; use Drupal\metastore\Storage\MetastoreStorageInterface; use Drupal\metastore\Storage\NodeData; - +use Drupal\metastore\ValidMetadataFactory; use MockChain\Chain; +use MockChain\Options; use MockChain\Sequence; use PHPUnit\Framework\TestCase; -use MockChain\Options; use RootedData\RootedJsonData; /** * @coversDefaultClass Drupal\metastore\MetastoreService + * + * @group dkan + * @group metastore + * @group unit */ class MetastoreServiceTest extends TestCase { @@ -406,6 +410,7 @@ public static function getCommonMockChain(TestCase $case, Options $services = nu ->add('dkan.metastore.storage', DataFactory::class) ->add('event_dispatcher', ContainerAwareEventDispatcher::class) ->add('dkan.metastore.valid_metadata', ValidMetadataFactory::class) + ->add('dkan.common.logger_channel', LoggerChannelInterface::class) ->index(0); return (new Chain($case)) diff --git a/modules/metastore/tests/src/Unit/Reference/DereferencerTest.php b/modules/metastore/tests/src/Unit/Reference/DereferencerTest.php index f5bef3da18..4f20b3ec7d 100644 --- a/modules/metastore/tests/src/Unit/Reference/DereferencerTest.php +++ b/modules/metastore/tests/src/Unit/Reference/DereferencerTest.php @@ -13,9 +13,12 @@ use MockChain\Chain; use MockChain\Sequence; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; /** - * + * @group dkan + * @group metastore + * @group unit */ class DereferencerTest extends TestCase { @@ -42,7 +45,7 @@ public function testDereference() { ->add(QueueFactory::class) ->getMock(); - $valueReferencer = new Dereferencer($configService, $storageFactory); + $valueReferencer = new Dereferencer($configService, $storageFactory, $this->createStub(LoggerInterface::class)); $referenced = $valueReferencer->dereference((object) ['publisher' => $uuid]); $this->assertTrue(is_object($referenced)); @@ -63,7 +66,7 @@ public function testDereferenceDeletedReference() { $uuidService = new Uuid5(); $uuid = $uuidService->generate('dataset', "some value"); - $valueReferencer = new Dereferencer($configService, $storageFactory); + $valueReferencer = new Dereferencer($configService, $storageFactory, $this->createStub(LoggerInterface::class)); $referenced = $valueReferencer->dereference((object) ['distribution' => $uuid]); $this->assertEmpty((array) $referenced); @@ -96,7 +99,7 @@ public function testDereferenceMultiple() { ->add(QueueFactory::class) ->getMock(); - $valueReferencer = new Dereferencer($configService, $storageFactory); + $valueReferencer = new Dereferencer($configService, $storageFactory, $this->createStub(LoggerInterface::class)); $referenced = $valueReferencer->dereference((object) ['keyword' => ['123456789', '987654321']]); $this->assertTrue(is_object($referenced)); diff --git a/modules/metastore/tests/src/Unit/Reference/ReferencerTest.php b/modules/metastore/tests/src/Unit/Reference/ReferencerTest.php index edc111470f..71ab811666 100644 --- a/modules/metastore/tests/src/Unit/Reference/ReferencerTest.php +++ b/modules/metastore/tests/src/Unit/Reference/ReferencerTest.php @@ -8,30 +8,24 @@ use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\File\FileSystem; -use Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser; use Drupal\Core\Logger\LoggerChannelFactory; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\StreamWrapper\StreamWrapperManager; - -use Drupal\common\DataResource; -use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\metastore\DataDictionary\DataDictionaryDiscovery; use Drupal\metastore\Exception\MissingObjectException; +use Drupal\metastore\MetastoreService; use Drupal\metastore\Reference\MetastoreUrlGenerator; use Drupal\metastore\Reference\Referencer; use Drupal\metastore\ResourceMapper; -use Drupal\metastore\MetastoreService; use Drupal\metastore\Storage\DataFactory; use Drupal\metastore\Storage\NodeData; -use Drupal\metastore\Storage\ResourceMapperDatabaseTable; use Drupal\node\Entity\Node; use Drupal\node\NodeStorage; - use GuzzleHttp\Client; -use GuzzleHttp\Exception\ConnectException; use MockChain\Chain; use MockChain\Options; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; use RootedData\RootedJsonData; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\HttpFoundation\Request; @@ -130,7 +124,8 @@ private function mockReferencer($existing = TRUE) { $storageFactory, $urlGenerator, new Client(), - $mimeTypeGuesser + $mimeTypeGuesser, + $this->createStub(LoggerInterface::class) ); } @@ -410,7 +405,8 @@ public function getMimeType() { return ReferencerTest::MIME_TYPE; } $storageFactory, $urlGenerator, new Client(), - $mimeTypeGuesser + $mimeTypeGuesser, + $this->createStub(LoggerInterface::class) ); // Test Mime Type detection using the resource `mediaType` property. @@ -475,7 +471,8 @@ public function testDistributionHandlingDataDict($distribution, $describedBy) { $storageFactory, $urlGenerator, $http_client, - $mimeTypeGuesser + $mimeTypeGuesser, + $this->createStub(LoggerInterface::class) ); if ($describedBy instanceof \Exception) { diff --git a/modules/metastore/tests/src/Unit/Storage/DataTest.php b/modules/metastore/tests/src/Unit/Storage/DataTest.php index c2f685d846..3ce6eebb68 100644 --- a/modules/metastore/tests/src/Unit/Storage/DataTest.php +++ b/modules/metastore/tests/src/Unit/Storage/DataTest.php @@ -12,11 +12,12 @@ use Drupal\node\NodeStorage; use MockChain\Chain; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; /** - * Class DataTest - * - * @package Drupal\Tests\metastore\Storage + * @group dkan + * @group metastore + * @group unit */ class DataTest extends TestCase { @@ -40,7 +41,7 @@ public function testGetStorageNode() { ->add(ConfigFactoryInterface::class, 'get', $immutableConfig) ->getMock(); - $data = new NodeData('dataset', $this->getEtmChain()->getMock(), $configFactoryMock); + $data = new NodeData('dataset', $this->getEtmChain()->getMock(), $configFactoryMock, $this->createStub(LoggerInterface::class)); $this->assertInstanceOf(NodeStorage::class, $data->getEntityStorage()); } @@ -57,7 +58,7 @@ public function testPublishDatasetNotFound() { ->getMock(); $this->expectExceptionMessage('Error: 1 not found.'); - $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock); + $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock, $this->createStub(LoggerInterface::class)); $nodeData->publish('1'); } @@ -76,7 +77,7 @@ public function testPublishDraftDataset() { ->add(ConfigFactoryInterface::class, 'get', $immutableConfig) ->getMock(); - $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock); + $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock, $this->createStub(LoggerInterface::class)); $result = $nodeData->publish('1'); $this->assertEquals(TRUE, $result); } @@ -94,7 +95,7 @@ public function testPublishDatasetAlreadyPublished() { ->add(ConfigFactoryInterface::class, 'get', $immutableConfig) ->getMock(); - $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock); + $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock, $this->createStub(LoggerInterface::class)); $result = $nodeData->publish('1'); $this->assertEquals(FALSE, $result); } @@ -132,7 +133,7 @@ public function testCount(): void { ->getMock(); // Create Data object. - $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock); + $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock, $this->createStub(LoggerInterface::class)); // Ensure count matches return value. $this->assertEquals($count, $nodeData->count()); } @@ -167,7 +168,7 @@ public function uuid() { ->getMock(); // Create Data object. - $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock); + $nodeData = new NodeData('dataset', $etmMock, $configFactoryMock, $this->createStub(LoggerInterface::class)); // Ensure the returned uuids match those belonging to the generated nodes. $this->assertEquals($uuids, $nodeData->retrieveIds(1, 5)); }