Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ResourcePurgerTest to BTB #4246

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,72 @@

namespace Drupal\Tests\datastore\Functional\Service;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\common\Traits\CleanUp;
use Drupal\Tests\common\Traits\GetDataTrait;
use Drupal\Tests\common\Traits\QueueRunnerTrait;
use Drupal\Tests\metastore\Unit\MetastoreServiceTest;

use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
* Test ResourcePurger service.
*
* @coversDefaultClass \Drupal\datastore\Service\ResourcePurger
*
* @group dkan
* @group datastore
* @group functional
* @group btb
*/
class ResourcePurgerTest extends ExistingSiteBase {
class ResourcePurgerTest extends BrowserTestBase {
use GetDataTrait;
use CleanUp;
use QueueRunnerTrait;

protected static $modules = [
'datastore',
'metastore',
'node',
];

protected $defaultTheme = 'stark';

/**
* DKAN dataset storage service.
*
* @var \Drupal\metastore\Storage\NodeData
*/
protected $datasetStorage;

/**
* DKAN datastore service.
*
* @var \Drupal\datastore\DatastoreService
*/
protected $datastore;

/**
* DKAN metastore service.
*
* @var \Drupal\metastore\MetastoreService
*/
protected $metastore;

/**
* The Drupal Core Queue service.
*
* @var \Drupal\Core\Queue\QueueFactory
*/
protected $queue;

/**
* The Drupal Core queue worker manager service.
*
* @var \Drupal\Core\Queue\QueueWorkerManager
*/
protected $queueWorkerManager;

/**
* DKAN resource purger service.
*
* @var \Drupal\datastore\Service\ResourcePurger
*/
protected $resourcePurger;

/**
* The ValidMetadataFactory class used for testing.
*
* @var \Drupal\metastore\ValidMetadataFactory|\PHPUnit\Framework\MockObject\MockObject
*/
protected $validMetadataFactory;

/**
* The Config Factory
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $config;

public function setUp(): void {
parent::setUp();

// Prepare environment.
$this->removeHarvests();
$this->removeAllNodes();
$this->removeAllMappedFiles();
$this->removeAllFileFetchingJobs();
$this->flushQueues();
$this->removeFiles();
$this->removeDatastoreTables();

// Initialize services.
$this->datasetStorage = \Drupal::service('dkan.metastore.storage')->getInstance('dataset');
$this->datastore = \Drupal::service('dkan.datastore.service');
$this->metastore = \Drupal::service('dkan.metastore.service');
$this->queue = \Drupal::service('queue');
$this->queueWorkerManager = \Drupal::service('plugin.manager.queue_worker');
$this->resourcePurger = \Drupal::service('dkan.datastore.service.resource_purger');
$this->datasetStorage = $this->container->get('dkan.metastore.storage')->getInstance('dataset');
$this->metastore = $this->container->get('dkan.metastore.service');
$this->validMetadataFactory = MetastoreServiceTest::getValidMetadataFactory($this);
$this->config = \Drupal::service('config.factory');
}

/**
* Test deleting a dataset doesn't delete other datasets sharing a resource.
*/
public function testDatasetsWithSharedResourcesAreNotDeletedPrematurely(): void {
// Make sure the default moderation state is "published".
$workflowConfig = $this->config->getEditable('workflows.workflow.dkan_publishing');
$defaultModerationState = $workflowConfig->get('type_settings.default_moderation_state');
$workflowConfig->set('type_settings.default_moderation_state', 'published')->save();
$this->config('workflows.workflow.dkan_publishing')
->set('type_settings.default_moderation_state', 'published')
->save();

// Create 2 datasets with the same resource, and change the resource of one.
$dataset = $this->validMetadataFactory->get($this->getDataset(123, 'Test #1', ['district_centerpoints_small.csv']), 'dataset');
Expand All @@ -125,13 +85,14 @@ public function testDatasetsWithSharedResourcesAreNotDeletedPrematurely(): void

// Ensure calling the resource purger on the updated dataset does not delete
// the previously shared resource.
$this->resourcePurger->schedule([123], FALSE);
$this->container->get('dkan.datastore.service.resource_purger')
->schedule([123], FALSE);
$resources = $this->getResourcesForDataset(456);
$resource = reset($resources);
$this->assertNotEmpty($this->datastore->getStorage($resource->identifier, $resource->version));

// Set default moderation state back to default.
$workflowConfig->set('type_settings.default_moderation_state', $defaultModerationState)->save();
$this->assertNotEmpty(
$this->container->get('dkan.datastore.service')
->getStorage($resource->identifier, $resource->version)
);
}

/**
Expand Down