mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-11-05 05:53:18 -05:00
Refactor Stock_location model to camelCase naming and introduce
is_default and sort_order fields with full migration, UI, and
item-sync support. Multiple follow-up fixes consolidated.
Database
- Add is_default (TINYINT, backfilled to oldest non-deleted row) and
sort_order (nullable unsigned TINYINT unique, dense 0..N-1) columns
via new migration; MySQL 5.7-compatible backfill using user variables
instead of ROW_NUMBER(); down() drops both columns
Model
- Rename show_locations → showLocations (camelCase convention)
- Add getDefaultLocationId(), setDefaultLocation(), saveSortOrder();
validate location existence before setting as default
- delete() compacts sort_order via two-pass CASE update to avoid
collisions after prior reorders
- Atomic sort_order assignment via explicit SQL COUNT query
Controller / Config
- Modularize save logic into sanitizeSubmittedLocations(),
deleteRemovedLocations(), saveSubmittedLocations(),
saveLocationOrderAndDefault(); wrap in single transaction
- Support stock_location_new inputs for new-row creation
- Resolve new-* ID placeholders to real IDs post-save
- Abort loop on save failure; default missing POST arrays to []
- Validate that at least one location array is non-empty
Views
- Add Default column with center-aligned radio buttons; disable for
unsaved rows; use attr() instead of data() for location IDs
- Escape dynamic location IDs against XSS in all affected partials
- form_count_details.php: fall back to default location when current
is undefined; guard quantity input similarly
- Add aria-label to default radio inputs
Libraries / Items
- Widen Item_lib::get_item_location() and get_stock_destination() to
nullable return types
- Guard null location_id in Items::getRow() and getGenerateBarcodes()
- Item::get_multiple_info(): accept ?int $location_id; short-circuit
with where('item_id', -1) instead of always-false where('1','0')
i18n
- Add stock_location_default, stock_location_default_header,
stock_location_name_header across all locale files
Tests
- Stock_locationTest: getDefaultLocationId, setDefaultLocation,
saveSortOrder, delete compaction, saveValue; rename to
testMethod_Scenario convention
- Add ConfigControllerTest / StockLocationsControllerTest with
AdminAuthTrait covering CRUD, reorder, default assignment, and
malformed/empty/blank payload validation
- DatabaseBootstrapTrait-based setup for ItemTest and Stock_locationTest
Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
104 lines
3.3 KiB
PHP
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->get_multiple_info([$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->get_multiple_info([$itemId], self::LOCATION_ID);
|
|
|
|
$this->assertSame(1, $result->getNumRows());
|
|
}
|
|
}
|