Files
opensourcepos/tests/Models/JobThrottleModelTest.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

93 lines
2.8 KiB
PHP

<?php
namespace Tests\Models;
use App\Models\JobThrottle;
use CodeIgniter\Database\Config;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
class JobThrottleModelTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $namespace = 'App';
private static bool $doneBootstrap = false;
protected JobThrottle $jobThrottle;
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->jobThrottle = model(JobThrottle::class);
$this->db->table('job_throttles')->truncate();
}
public function testExistsReturnsFalseForUnknownId(): void
{
$this->assertFalse($this->jobThrottle->exists(999999));
}
public function testSaveValueInsertsWhenThrottleDoesNotExist(): void
{
$this->assertSame(1, $this->jobThrottle->saveValue(['max_count' => 10, 'period' => 'hour'], 1));
$this->seeInDatabase('job_throttles', [
'throttle_id' => 1,
'max_count' => 10,
'period' => 'hour',
'deleted' => 0,
]);
}
public function testSaveValueUpdatesWhenThrottleExists(): void
{
$this->jobThrottle->saveValue(['max_count' => 10, 'period' => 'hour'], 1);
$this->assertTrue($this->jobThrottle->exists(1));
$this->assertSame(1, $this->jobThrottle->saveValue(['max_count' => 25, 'period' => 'day'], 1));
$this->seeInDatabase('job_throttles', [
'throttle_id' => 1,
'max_count' => 25,
'period' => 'day',
]);
$this->assertEquals(1, $this->db->table('job_throttles')->countAllResults());
}
public function testGetAllExcludesDeletedThrottles(): void
{
$this->jobThrottle->saveValue(['max_count' => 5, 'period' => 'minute'], 1);
$this->jobThrottle->saveValue(['max_count' => 15, 'period' => 'day'], 2);
$this->jobThrottle->delete(2);
$results = $this->jobThrottle->getAll()->getResultArray();
$this->assertCount(1, $results);
$this->assertSame('1', (string) $results[0]['throttle_id']);
}
public function testDeleteSoftDeletesThrottle(): void
{
$this->jobThrottle->saveValue(['max_count' => 5, 'period' => 'minute'], 1);
$this->assertTrue($this->jobThrottle->delete(1));
$this->seeInDatabase('job_throttles', ['throttle_id' => 1, 'deleted' => 1]);
$this->assertCount(0, $this->jobThrottle->getAll()->getResultArray());
}
}