Files
opensourcepos/tests/Models/ItemTest.php
T
objecttothisandClaude Sonnet 5 600417bfdf refactor: extract PSR-12 renames and independent bugfixes from plugin system PR (#4784)
build/refactor/fix: tighten tooling, rename accessors to camelCase, and correct model bugs

* build: tighten gitignore, lockfile, and script config
- gitignore /build and /.php-cs-fixer.cache
- allow core-js@3.50.0 postinstall script in package.json

* refactor: rename snake_case data accessors to camelCase
Rename get_info, get_multiple_info, get_sale_items, get_sale_payments,
and save_customer across Customer, Employee, Item, Sale, and Supplier
models to PSR-12 camelCase, updating every call site in controllers,
libraries, and views.

* test: update calls to match renamed camelCase accessors
Update HomeTest, ItemsControllerTest, and ItemTest to call
getInfo()/getMultipleInfo() instead of the old snake_case model methods.

* fix: correct independent bugs in Item, Item_taxes, and Attribute models
- Item::delete()/delete_list(): let $this->db->transStatus() alone decide
  success instead of accumulating a bitwise-AND over intermediate results,
  which could report failure (and trigger error toasts) even when the
  delete had actually completed
- Item_taxes: correct $table property from 'item_taxes' to 'items_taxes'
- Attribute::getDefinitionNames(): pass an array to
  QueryBuilder->whereNotIn(), which requires one

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-28 13:00:30 +04:00

104 lines
3.3 KiB
PHP

<?php
namespace Tests\Models;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use App\Models\Item;
use Tests\Support\DatabaseBootstrapTrait;
use Tests\Support\ItemFixtureTrait;
class ItemTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use DatabaseBootstrapTrait;
use ItemFixtureTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $namespace = null;
private const LOCATION_ID = 1;
protected function setUp(): void
{
$this->resetDatabaseOnce();
parent::setUp();
}
public function testSuggestionsColumnValidationRejectsSQLInjection(): void
{
// These malicious values should not be in the allowed columns list
$malicious_columns = [
'name, SLEEP(5)',
'name; DROP TABLE items',
"name' OR '1'='1",
'name UNION SELECT * FROM users',
'name--comment'
];
foreach ($malicious_columns as $malicious) {
$this->assertNotContains($malicious, Item::ALLOWED_SUGGESTIONS_COLUMNS,
"Malicious input should not be in allowed columns: $malicious");
}
}
public function testSuggestionsColumnValidationAcceptsValidColumns(): void
{
// Valid columns should be preserved
$valid_columns = ['name', 'item_number', 'description', 'cost_price', 'unit_price'];
foreach ($valid_columns as $column) {
$this->assertContains($column, Item::ALLOWED_SUGGESTIONS_COLUMNS,
"Valid column should be in allowed list: $column");
}
}
public function testSuggestionsColumnValidationHandlesEmptyString(): void
{
// Empty string should only be allowed for second and third columns
$this->assertContains('', Item::ALLOWED_SUGGESTIONS_COLUMNS_WITH_EMPTY,
'Empty string should be allowed for optional columns');
}
public function testSuggestionsColumnValidationConstantsAreConsistent(): void
{
// Ensure the constants are properly defined
$this->assertCount(5, Item::ALLOWED_SUGGESTIONS_COLUMNS,
'Should have exactly 5 allowed columns');
$this->assertCount(6, Item::ALLOWED_SUGGESTIONS_COLUMNS_WITH_EMPTY,
'Should have exactly 6 allowed columns including empty');
// Ensure empty string is only in WITH_EMPTY variant
$this->assertNotContains('', Item::ALLOWED_SUGGESTIONS_COLUMNS,
'Empty string should not be in required columns list');
}
public function testGetMultipleInfo_ReturnsEmptyResultWhenLocationIdIsNull(): void
{
$itemId = $this->createTestItem();
$itemModel = model(Item::class);
$result = $itemModel->getMultipleInfo([$itemId], null);
$this->assertSame(0, $result->getNumRows());
}
public function testGetMultipleInfo_ReturnsRowForMatchingLocation(): void
{
$itemId = $this->createTestItem();
\Config\Database::connect()->table('item_quantities')->insert([
'item_id' => $itemId,
'location_id' => self::LOCATION_ID,
'quantity' => 5,
]);
$itemModel = model(Item::class);
$result = $itemModel->getMultipleInfo([$itemId], self::LOCATION_ID);
$this->assertSame(1, $result->getNumRows());
}
}