Files
opensourcepos/tests/Models/ImportBatchContextTest.php
T
objecttothis c365e6e6dd 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-07 17:55:22 +04:00

86 lines
2.7 KiB
PHP

<?php
namespace Tests\Models;
use App\Models\ImportBatchContext;
use CodeIgniter\Database\Config;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
class ImportBatchContextTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $namespace = 'App';
private static bool $doneBootstrap = false;
protected ImportBatchContext $importBatchContext;
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->importBatchContext = model(ImportBatchContext::class);
$this->db->table('queue_jobs')->where('context_id >', 0)->delete();
$this->db->table('import_batch_contexts')->where('id >', 0)->delete();
}
public function testCreateStoresJsonEncodedContextAndReturnsId(): void
{
$context = ['definition_names' => ['1' => 'Color'], 'attribute_data' => []];
$id = $this->importBatchContext->create('batch-a', $context);
$this->assertGreaterThan(0, $id);
$this->seeInDatabase('import_batch_contexts', ['id' => $id, 'batch_id' => 'batch-a']);
}
public function testGetByBatchIdReturnsDecodedContext(): void
{
$context = ['definition_names' => ['1' => 'Color'], 'attribute_data' => ['Color' => ['definition_type' => 'TEXT']]];
$this->importBatchContext->create('batch-b', $context);
$result = $this->importBatchContext->getByBatchId('batch-b');
$this->assertSame($context, $result);
}
public function testGetByBatchIdReturnsNullWhenNoContextStored(): void
{
$this->assertNull($this->importBatchContext->getByBatchId('nonexistent-batch'));
}
public function testGetIdForBatchReturnsStoredId(): void
{
$id = $this->importBatchContext->create('batch-c', ['definition_names' => []]);
$this->assertSame($id, $this->importBatchContext->getIdForBatch('batch-c'));
}
public function testGetIdForBatchReturnsNullWhenNoContextStored(): void
{
$this->assertNull($this->importBatchContext->getIdForBatch('nonexistent-batch'));
}
public function testDeleteForBatchRemovesRow(): void
{
$this->importBatchContext->create('batch-d', ['definition_names' => []]);
$this->importBatchContext->deleteForBatch('batch-d');
$this->dontSeeInDatabase('import_batch_contexts', ['batch_id' => 'batch-d']);
}
}