Skip to content

Commit

Permalink
Test drop logic on post-import error
Browse files Browse the repository at this point in the history
  • Loading branch information
chri5tia committed Oct 25, 2024
1 parent b3d2f80 commit 65ef768
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public static function create(ContainerInterface $container, array $configuratio
*/
public function processItem($data) {
$postImportResult = $this->postImportProcessItem($data);
$drop_config = $this->config->get('datastore.settings')->get('drop_datastore_on_post_import_error');
$drop_config = $this->config->get('drop_datastore_on_post_import_error');

if ($postImportResult->getPostImportStatus() === 'done') {
$this->invalidateCacheTags(DataResource::buildUniqueIdentifier(
$data->getIdentifier(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ public function testPostImportProcessItemResourceNoLongerExists() {
$this->assertEmpty($errors);
}

// /**
// * Test postImportProcessItem() halts and logs a message if a resource has changed.
// */
/**
* Test postImportProcessItem() halts and logs a message if a resource has changed.
*/
public function testPostImportProcessItemResourceChanged() {
$resource_a = new DataResource('test.csv', 'text/csv');

Expand Down Expand Up @@ -194,9 +194,9 @@ public function testPostImportProcessItemResourceChanged() {
$this->assertEmpty($errors);
}

// /**
// * Test postImportProcessItem() logs errors encountered in processors.
// */
/**
* Test postImportProcessItem() logs errors encountered in processors.
*/
public function testPostImportProcessItemProcessorError() {
$resource = new DataResource('test.csv', 'text/csv');

Expand Down Expand Up @@ -228,6 +228,107 @@ public function testPostImportProcessItemProcessorError() {
$this->assertEquals($errors[0], 'Test Error');
}

/**
* Test 1: Verify Datastore Drop on Post-Import Error (with drop_config enabled)
*/
public function testDatastoreDropOnPostImportError() {
$resource = new DataResource('test.csv', 'text/csv');
$resource_processor = (new Chain($this))
->add(ResourceProcessorInterface::class, 'process', new \Exception('Test Error'))
->getMock();

$container_chain = $this->getContainerChain()
->add(ResourceProcessorCollector::class, 'getResourceProcessors', [$resource_processor])
->add(ResourceMapper::class, 'get', $resource);
\Drupal::setContainer($container_chain->getMock());

$dictionaryEnforcer = PostImportResourceProcessor::create(
$container_chain->getMock(), [], '', ['cron' => ['lease_time' => 10800]]
);
$postImportResult = $dictionaryEnforcer->postImportProcessItem($resource);

$this->assertEquals($resource->getIdentifier(), $postImportResult->getResourceIdentifier());
$this->assertEquals($resource->getVersion(), $postImportResult->getResourceVersion());
$this->assertEquals('error', $postImportResult->getPostImportStatus());
$this->assertEquals('Test Error', $postImportResult->getPostImportMessage());
}

/**
* Test 2: Verify Logging on Successful Datastore Drop
*/
public function testLoggingOnSuccessfulDatastoreDrop() {
$resource = new DataResource('test.csv', 'text/csv');
$resource_processor = (new Chain($this))
->add(ResourceProcessorInterface::class, 'process')
->getMock();

$container_chain = $this->getContainerChain()
->add(ResourceProcessorCollector::class, 'getResourceProcessors', [$resource_processor])
->add(ResourceMapper::class, 'get', $resource);
\Drupal::setContainer($container_chain->getMock());

$dictionaryEnforcer = PostImportResourceProcessor::create(
$container_chain->getMock(), [], '', ['cron' => ['lease_time' => 10800]]
);
$postImportResult = $dictionaryEnforcer->postImportProcessItem($resource);

$this->assertEquals($resource->getIdentifier(), $postImportResult->getResourceIdentifier());
$this->assertEquals($resource->getVersion(), $postImportResult->getResourceVersion());
}

/**
* Test 3: Verify Exception Handling When Datastore Drop Fails
*/
public function testExceptionHandlingWhenDatastoreDropFails() {
$resource = new DataResource('test.csv', 'text/csv');
$resource_processor = (new Chain($this))
->add(ResourceProcessorInterface::class, 'process', new \Exception('Test Error'))
->getMock();

$container_chain = $this->getContainerChain()
->add(ResourceProcessorCollector::class, 'getResourceProcessors', [$resource_processor])
->add(ResourceMapper::class, 'get', $resource);
\Drupal::setContainer($container_chain->getMock());

$dictionaryEnforcer = PostImportResourceProcessor::create(
$container_chain->getMock(), [], '', ['cron' => ['lease_time' => 10800]]
);
$postImportResult = $dictionaryEnforcer->postImportProcessItem($resource);

$this->assertEquals($resource->getIdentifier(), $postImportResult->getResourceIdentifier());
$this->assertEquals($resource->getVersion(), $postImportResult->getResourceVersion());
$this->assertEquals('error', $postImportResult->getPostImportStatus());
$this->assertEquals('Test Error', $postImportResult->getPostImportMessage());
}

/**
* Test 4: Verify No Datastore Drop When drop_config is Disabled
*/
public function testNoDatastoreDropWhenDropConfigIsDisabled() {
$resource = new DataResource('test.csv', 'text/csv');
$resource_processor = (new Chain($this))
->add(ResourceProcessorInterface::class, 'process')
->getMock();

$datastoreService = $this->createMock(DatastoreService::class);
$datastoreService->expects($this->never())
->method('drop');

$container_chain = $this->getContainerChain()
->add(ResourceProcessorCollector::class, 'getResourceProcessors', [$resource_processor])
->add(ResourceMapper::class, 'get', $resource);
\Drupal::setContainer($container_chain->getMock());

$dictionaryEnforcer = PostImportResourceProcessor::create(
$container_chain->getMock(), [], '', ['cron' => ['lease_time' => 10800]]
);

$postImportResult = $dictionaryEnforcer->postImportProcessItem($resource);

$this->assertEquals('done', $postImportResult->getPostImportStatus());
$this->assertEquals(NULL, $postImportResult->getPostImportMessage());
}

/**
* Get container chain.
*/
Expand Down

0 comments on commit 65ef768

Please sign in to comment.