Files
opensourcepos/tests/Libraries/ImportBatchServiceTest.php
T
objecttothis 90e68714f0 feat(jobs): add batch-level import context and queue pause support
- Add import_batch_contexts table plus ImportBatchContext model so
  per-batch data (e.g. Items CSV attribute definitions) is stored
  once and referenced by context_id, instead of duplicated into
  every queued row's job payload.
- Extend queue_jobs, queue_jobs_failed, and new queue_paused_jobs
  tables with batch_id/context_id columns and FKs (ON DELETE
  RESTRICT) back to import_batch_contexts, so context rows cannot
  be purged while any job still references them.
- Introduce ImportBatchService.pushRow() to centralize queuing a
  batch row with batch/context stamping and priority; update
  Customers and Items controllers to use it instead of calling
  importBatch->create() and queue->push() separately.
- Add queue_paused_jobs table and Jobs::postPauseJobs()/pauseQueues()
  to move pending jobs out of queue_jobs into a paused table per
  queue (all core queues or a selected subset), leaving in-flight
  jobs untouched (issue #3833 pause/resume design).
- Change Jobs::getEditJob()/postSaveJob() routes to accept separate
  source/id path segments instead of a combined uid string.
- Add test coverage for ImportBatchContext, ImportBatchService, and
  ImportBatch (including purge-retains-while-referenced guards).

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-10-01 16:07:04 +04:00

105 lines
3.5 KiB
PHP

<?php
namespace Tests\Libraries;
use App\Libraries\ImportBatchService;
use CodeIgniter\Database\Config;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
class ImportBatchServiceTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $namespace = 'App';
private static bool $doneBootstrap = false;
protected ImportBatchService $importBatchService;
protected function setUp(): void
{
if (self::$doneBootstrap === false) {
Config::seeder($this->DBGroup)->call('App\Database\Seeds\TestDatabaseBootstrapSeeder');
Config::connect($this->DBGroup)->close();
self::$doneBootstrap = true;
}
parent::setUp();
$this->importBatchService = service('importBatch');
$this->db->table('queue_jobs')->truncate();
$this->db->table('import_batch_contexts')->where('id >', 0)->delete();
$this->db->table('import_batches')->truncate();
}
public function testCreateWithoutContextStoresNoContextRow(): void
{
$batchId = $this->importBatchService->create('customers', 1);
$this->seeInDatabase('import_batches', ['id' => $batchId, 'type' => 'customers']);
$this->assertNull($this->importBatchService->getContext($batchId));
}
public function testCreateWithContextStoresRetrievableContext(): void
{
$context = ['definition_names' => ['1' => 'Color'], 'attribute_data' => []];
$batchId = $this->importBatchService->create('items', 1, $context);
$this->assertSame($context, $this->importBatchService->getContext($batchId));
}
public function testPushRowStampsBatchIdOnQueuedJob(): void
{
$batchId = $this->importBatchService->create('customers', 1);
$this->importBatchService->pushRow('imports', 'customer_import', [
'batch_id' => $batchId,
'row' => ['First Name' => 'Jane'],
'employee_id' => 1,
], $batchId, 'low');
$this->seeInDatabase('queue_jobs', ['queue' => 'imports', 'batch_id' => $batchId]);
}
public function testPushRowStampsContextIdWhenBatchHasContext(): void
{
$batchId = $this->importBatchService->create('items', 1, ['definition_names' => []]);
$this->importBatchService->pushRow('imports', 'item_import', [
'batch_id' => $batchId,
'row' => [],
'employee_id' => 1,
], $batchId, 'low');
$contextRow = $this->db->table('import_batch_contexts')->where('batch_id', $batchId)->get()->getRowArray();
$this->seeInDatabase('queue_jobs', ['queue' => 'imports', 'batch_id' => $batchId, 'context_id' => $contextRow['id']]);
}
public function testPushRowLeavesContextIdNullWhenBatchHasNoContext(): void
{
$batchId = $this->importBatchService->create('customers', 1);
$this->importBatchService->pushRow('imports', 'customer_import', [
'batch_id' => $batchId,
'row' => [],
'employee_id' => 1,
], $batchId, 'low');
$this->seeInDatabase('queue_jobs', ['queue' => 'imports', 'batch_id' => $batchId, 'context_id' => null]);
}
public function testIncrementReportsBatchCompletion(): void
{
$batchId = $this->importBatchService->create('customers', 1);
$this->assertTrue($this->importBatchService->increment($batchId, false));
}
}