From 3dedabb1faf7e5e85bef1e74e4247d7eb5a56c02 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 25 Oct 2022 11:13:00 +1300 Subject: [PATCH] Add runAs to import job --- CRM/Import/Parser.php | 5 ++++- .../Import/Parser/ContributionTest.php | 21 ++++++++++++++++--- .../phpunit/CRMTraits/Import/ParserTrait.php | 3 +++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index dd5d6cf656..0ccb550ac7 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -667,7 +667,9 @@ abstract class CRM_Import_Parser implements UserJobInterface { public function queue(): void { $dataSource = $this->getDataSourceObject(); $totalRowCount = $totalRows = $dataSource->getRowCount(['new']); - $queue = Civi::queue('user_job_' . $this->getUserJobID(), ['type' => 'Sql', 'error' => 'abort', 'runner' => 'task', 'user_job_id' => $this->getUserJobID()]); + // The retry limit for the queue is set to 5 - allowing for a few deadlocks but we might consider + // making this configurable at some point. + $queue = Civi::queue('user_job_' . $this->getUserJobID(), ['type' => 'Sql', 'error' => 'abort', 'runner' => 'task', 'user_job_id' => $this->getUserJobID(), 'retry_limit' => 5]); UserJob::update(FALSE)->setValues(['queue_id.name' => 'user_job_' . $this->getUserJobID()])->addWhere('id', '=', $this->getUserJobID())->execute(); $offset = 0; $batchSize = 50; @@ -681,6 +683,7 @@ abstract class CRM_Import_Parser implements UserJobInterface { ['userJobID' => $this->getUserJobID(), 'limit' => $batchSize, 'offset' => 0], ts('Processed %1 rows out of %2', [1 => $offset + $batchSize, 2 => $totalRowCount]) ); + $task->runAs = ['contactId' => CRM_Core_Session::getLoggedInContactID(), 'domainId' => CRM_Core_Config::domainID()]; $queue->createItem($task); $totalRows -= $batchSize; $offset += $batchSize; diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 032aeb0cea..146ecb7784 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -32,6 +32,13 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { */ protected $entity = 'Contribution'; + /** + * Original value for background processing. + * + * @var bool + */ + protected $enableBackgroundQueueOriginalValue; + /** * These extensions are inactive at the start. They may be activated during the test. They should be deactivated at the end. * @@ -48,8 +55,9 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { protected function setUp(): void { parent::setUp(); - $origExtensions = array_column(CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'fullName'); - $this->assertEquals([], array_intersect($origExtensions, $this->toggleExts), 'These extensions may be enabled and disabled during the test. The start-state and end-state should be the same. It appears that we have an unexpected start-state. Perhaps another test left us with a weird start-state?'); + $originalExtensions = array_column(CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles(), 'fullName'); + $this->assertEquals([], array_intersect($originalExtensions, $this->toggleExts), 'These extensions may be enabled and disabled during the test. The start-state and end-state should be the same. It appears that we have an unexpected start-state. Perhaps another test left us with a weird start-state?'); + $this->enableBackgroundQueueOriginalValue = Civi::settings()->get('enableBackgroundQueue'); } /** @@ -68,6 +76,7 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { CRM_Extension_System::singleton()->getManager()->disable([$ext]); CRM_Extension_System::singleton()->getManager()->uninstall([$ext]); } + Civi::settings()->set('enableBackgroundQueue', $this->enableBackgroundQueueOriginalValue); parent::tearDown(); } @@ -223,9 +232,15 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { * These features are * - default_value for each field. * + * @dataProvider getBooleanDataProvider + * + * @param bool $isBackGroundProcessing + * * @throws \CRM_Core_Exception */ - public function testImportFromUserJobConfiguration(): void { + public function testImportFromUserJobConfiguration(bool $isBackGroundProcessing): void { + Civi::settings()->set('enableBackgroundQueue', $isBackGroundProcessing); + $this->createLoggedInUser(); $importMappings = [ ['name' => 'organization_name'], ['name' => 'legal_name'], diff --git a/tests/phpunit/CRMTraits/Import/ParserTrait.php b/tests/phpunit/CRMTraits/Import/ParserTrait.php index 338acd76b3..c28d26b49f 100644 --- a/tests/phpunit/CRMTraits/Import/ParserTrait.php +++ b/tests/phpunit/CRMTraits/Import/ParserTrait.php @@ -70,6 +70,9 @@ trait CRMTraits_Import_ParserTrait { catch (CRM_Core_Exception_PrematureExitException $e) { $queue = Civi::queue('user_job_' . $this->userJobID); $this->assertEquals(1, CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM civicrm_queue_item')); + $item = $queue->claimItem(0); + $this->assertEquals(['contactId' => CRM_Core_Session::getLoggedInContactID(), 'domainId' => CRM_Core_Config::domainID()], $item->data->runAs); + $queue->releaseItem($item); $runner = new CRM_Queue_Runner([ 'queue' => $queue, 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, -- 2.25.1