mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-09-13 13:57:34 -04:00
* fix(tests): resolve all phpunit failures (#4626) Bring the phpunit suite from 153 failures to 0 (281 tests passing): - Employee: decouple grants block from save_value success; restructure save_employee new-employee + disallowed-grants early return - Sale: unify sales_payments_temp schema (add sale_cash_refund, reference_code) so both creators produce an identical superset table - Employees controller: provide placeholder password/hash in testing env so new-employee insert succeeds and grant logic is testable - TestDatabaseBootstrapSeeder: reset shared connection table-name cache after bootstrap reset to avoid stale listTables()/tableExists() results - Config: fix postSaveLocale validation rule syntax - Test data: use unique employee usernames to avoid UNIQUE constraint collisions latching strict-mode transStatus=false on the shared conn - Various test-file and language-string corrections * test: consolidate employee fixtures in shared trait Route test employee creation through a single EmployeeFixtureTrait that delegates to Employee::save_employee(), so fixtures exercise the same production code path instead of raw DB inserts. Removes six near-duplicate helpers across EmployeeTest, SalesControllerTest, and EmployeesControllerTest while preserving each test's specific grant set. Closes a piece of the fixture-scattering flagged in #4626. Closes #4626 * test: add global DROP/CREATE grant and commit theme fixtures * fix(ci): remove redundant symlink step, set working encryption key * fix(ci): run phpunit with --no-coverage to avoid no-driver warning * fix: address code review findings - Config: restore strict locale validation (min required|integer|>0) and fix max cross-field check with a new gte_field rule (CI4's greater_than_equal_to[field] does not resolve the field value) - Tests: assert rejection for non-numeric/zero/negative/min>max limits - .env.example: remove shared hard-coded encryption.key (auto-generates); document Docker env-var usage - phpunit.yml: scope CREATE/DROP grant to ospos_test.* and provision a per-run encryption key as an env var * feat: support ENCRYPTION_KEY env var for encryption key Read ENCRYPTION_KEY as a fallback for the encryption key when the config value is empty. This is a supported, reliable path for Docker / container deploys and CI, avoiding reliance on the raw dotted encryption.key env var. * fix: align Summary_report temp tables with Sale temp table schema Summary_report created sales_items_taxes_temp and sales_payments_temp with fewer columns than the canonical create_temp_table() in Sale.php. A later reader expecting those columns hit a schema-mismatch SQL error on the shared temp tables. Add internal_tax/sales_tax (sales_items_taxes_temp) and reference_code (sales_payments_temp) so all creators emit the identical column set.
298 lines
11 KiB
PHP
298 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Tests\Controllers;
|
|
|
|
use CodeIgniter\Database\Config;
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use CodeIgniter\Test\DatabaseTestTrait;
|
|
use CodeIgniter\Test\FeatureTestTrait;
|
|
use App\Models\Customer;
|
|
use App\Models\Employee;
|
|
|
|
class CustomersCsvImportTest extends CIUnitTestCase
|
|
{
|
|
use DatabaseTestTrait;
|
|
use FeatureTestTrait;
|
|
|
|
protected $migrate = true;
|
|
protected $migrateOnce = true;
|
|
protected $refresh = false;
|
|
protected $namespace = null;
|
|
|
|
protected Customer $customer;
|
|
protected Employee $employee;
|
|
|
|
private static bool $doneBootstrap = false;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
// Reset the test database to a clean schema on the first test in this
|
|
// class so leftover customers with the same emails from prior runs
|
|
// cannot be rejected as duplicates by the import (check_email_exists).
|
|
if (self::$doneBootstrap === false) {
|
|
Config::seeder($this->DBGroup)->call('App\Database\Seeds\TestDatabaseBootstrapSeeder');
|
|
Config::connect($this->DBGroup)->close();
|
|
|
|
self::$doneBootstrap = true;
|
|
}
|
|
|
|
parent::setUp();
|
|
// Reset any stale transaction state left on the shared in-process DB
|
|
// connection by a previous test.
|
|
$this->db->resetTransStatus();
|
|
|
|
$this->customer = model(Customer::class);
|
|
$this->employee = model(Employee::class);
|
|
|
|
helper('test');
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
parent::tearDown();
|
|
}
|
|
|
|
protected function loginAsEmployee(): void
|
|
{
|
|
$this->withSession(['person_id' => 1, 'menu_group' => 'office']);
|
|
}
|
|
|
|
protected function createCsvFile(array $rows): string
|
|
{
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'csv_test_');
|
|
|
|
$handle = fopen($tempFile, 'w');
|
|
foreach ($rows as $row) {
|
|
fputcsv($handle, $row);
|
|
}
|
|
fclose($handle);
|
|
|
|
return $tempFile;
|
|
}
|
|
|
|
protected function findCustomerByEmail(string $email): ?array
|
|
{
|
|
return $this->customer
|
|
->select('customers.*, people.*')
|
|
->join('people', 'people.person_id = customers.person_id')
|
|
->where('people.email', $email)
|
|
->first();
|
|
}
|
|
|
|
protected function findCustomersByEmailLike(string $needle): ?array
|
|
{
|
|
return $this->customer
|
|
->select('customers.*, people.*')
|
|
->join('people', 'people.person_id = customers.person_id')
|
|
->where('people.email LIKE', '%' . $needle . '%')
|
|
->first();
|
|
}
|
|
|
|
public function testValidEmailIsAccepted(): void
|
|
{
|
|
$this->loginAsEmployee();
|
|
|
|
$csvContent = [
|
|
['First Name', 'Last Name', 'Gender', 'Consent', 'Email', 'Phone', 'Address 1', 'Address 2', 'City', 'State', 'Zip', 'Country', 'Comments', 'Company', 'Account Number', 'Discount', 'Discount Type', 'Taxable'],
|
|
['John', 'Doe', '1', '1', 'john.doe@example.com', '555-1234', '123 Main St', '', 'Springfield', 'IL', '62701', 'US', '', '', '', '', '', '']
|
|
];
|
|
|
|
$tempFile = $this->createCsvFile($csvContent);
|
|
|
|
$_FILES['file_path'] = [
|
|
'name' => 'test.csv',
|
|
'type' => 'text/csv',
|
|
'tmp_name' => $tempFile,
|
|
'error' => UPLOAD_ERR_OK,
|
|
'size' => filesize($tempFile)
|
|
];
|
|
|
|
$result = $this->post('/customers/importCsvFile');
|
|
|
|
$result->assertOK();
|
|
$resultBody = json_decode($result->getJSON(), true);
|
|
$this->assertTrue($resultBody['success'], 'Import should fully succeed');
|
|
|
|
$importedCustomer = $this->findCustomerByEmail('john.doe@example.com');
|
|
$this->assertNotNull($importedCustomer);
|
|
|
|
unlink($tempFile);
|
|
}
|
|
|
|
public function testInvalidEmailIsRejected(): void
|
|
{
|
|
$this->loginAsEmployee();
|
|
|
|
$csvContent = [
|
|
['First Name', 'Last Name', 'Gender', 'Consent', 'Email', 'Phone', 'Address 1', 'Address 2', 'City', 'State', 'Zip', 'Country', 'Comments', 'Company', 'Account Number', 'Discount', 'Discount Type', 'Taxable'],
|
|
['John', 'Doe', '1', '1', 'not-an-email', '555-1234', '123 Main St', '', 'Springfield', 'IL', '62701', 'US', '', '', '', '', '', '']
|
|
];
|
|
|
|
$tempFile = $this->createCsvFile($csvContent);
|
|
|
|
$_FILES['file_path'] = [
|
|
'name' => 'test.csv',
|
|
'type' => 'text/csv',
|
|
'tmp_name' => $tempFile,
|
|
'error' => UPLOAD_ERR_OK,
|
|
'size' => filesize($tempFile)
|
|
];
|
|
|
|
$result = $this->post('/customers/importCsvFile');
|
|
|
|
$result->assertOK();
|
|
|
|
$resultBody = json_decode($result->getJSON(), true);
|
|
$this->assertFalse($resultBody['success'], 'Import should fail for invalid email');
|
|
$this->assertStringContainsString('Row 1', $resultBody['message'], 'Error message should reference failing row');
|
|
$this->assertStringContainsString('Invalid email format', $resultBody['message'], 'Error message should mention email validation');
|
|
|
|
$importedCustomer = $this->findCustomerByEmail('not-an-email');
|
|
$this->assertNull($importedCustomer, 'Customer with invalid email should not be imported');
|
|
|
|
unlink($tempFile);
|
|
}
|
|
|
|
public function testXssPayloadInEmailIsSanitized(): void
|
|
{
|
|
$this->loginAsEmployee();
|
|
|
|
$maliciousEmail = '<script>alert("xss")</script>@example.com';
|
|
|
|
$csvContent = [
|
|
['First Name', 'Last Name', 'Gender', 'Consent', 'Email', 'Phone', 'Address 1', 'Address 2', 'City', 'State', 'Zip', 'Country', 'Comments', 'Company', 'Account Number', 'Discount', 'Discount Type', 'Taxable'],
|
|
['John', 'Doe', '1', '1', $maliciousEmail, '555-1234', '123 Main St', '', 'Springfield', 'IL', '62701', 'US', '', '', '', '', '', '']
|
|
];
|
|
|
|
$tempFile = $this->createCsvFile($csvContent);
|
|
|
|
$_FILES['file_path'] = [
|
|
'name' => 'test.csv',
|
|
'type' => 'text/csv',
|
|
'tmp_name' => $tempFile,
|
|
'error' => UPLOAD_ERR_OK,
|
|
'size' => filesize($tempFile)
|
|
];
|
|
|
|
$result = $this->post('/customers/importCsvFile');
|
|
|
|
$result->assertOK();
|
|
|
|
$importedCustomer = $this->findCustomersByEmailLike('example.com');
|
|
|
|
$this->assertNotNull($importedCustomer, 'Customer should be imported after sanitization');
|
|
$this->assertStringNotContainsString('<script>', $importedCustomer['email'], 'Script tags should be removed');
|
|
$this->assertStringNotContainsString('</script>', $importedCustomer['email'], 'Script tags should be removed');
|
|
|
|
unlink($tempFile);
|
|
}
|
|
|
|
public function testMixedValidAndInvalidEmails(): void
|
|
{
|
|
$this->loginAsEmployee();
|
|
|
|
$csvContent = [
|
|
['First Name', 'Last Name', 'Gender', 'Consent', 'Email', 'Phone', 'Address 1', 'Address 2', 'City', 'State', 'Zip', 'Country', 'Comments', 'Company', 'Account Number', 'Discount', 'Discount Type', 'Taxable'],
|
|
['Valid', 'User', '1', '1', 'valid@example.com', '555-1111', '123 Main St', '', 'City1', 'ST', '12345', 'US', '', '', '', '', '', ''],
|
|
['Invalid', 'User', '1', '1', 'invalid-email', '555-2222', '456 Oak Ave', '', 'City2', 'ST', '23456', 'US', '', '', '', '', '', ''],
|
|
['Another', 'Valid', '1', '1', 'another@example.com', '555-3333', '789 Pine Rd', '', 'City3', 'ST', '34567', 'US', '', '', '', '', '', '']
|
|
];
|
|
|
|
$tempFile = $this->createCsvFile($csvContent);
|
|
|
|
$_FILES['file_path'] = [
|
|
'name' => 'test.csv',
|
|
'type' => 'text/csv',
|
|
'tmp_name' => $tempFile,
|
|
'error' => UPLOAD_ERR_OK,
|
|
'size' => filesize($tempFile)
|
|
];
|
|
|
|
$result = $this->post('/customers/importCsvFile');
|
|
|
|
$result->assertOK();
|
|
|
|
$validCustomer1 = $this->findCustomerByEmail('valid@example.com');
|
|
$this->assertNotNull($validCustomer1, 'Valid customer should be imported');
|
|
|
|
$validCustomer2 = $this->findCustomerByEmail('another@example.com');
|
|
$this->assertNotNull($validCustomer2, 'Another valid customer should be imported');
|
|
|
|
$invalidCustomer = $this->findCustomerByEmail('invalid-email');
|
|
$this->assertNull($invalidCustomer, 'Invalid email customer should not be imported');
|
|
|
|
unlink($tempFile);
|
|
}
|
|
|
|
public function testEmailWithSpecialCharactersIsSanitized(): void
|
|
{
|
|
$this->loginAsEmployee();
|
|
|
|
$emailWithSpecialChars = 'test"user@example.com';
|
|
$csvContent = [
|
|
['First Name', 'Last Name', 'Gender', 'Consent', 'Email', 'Phone', 'Address 1', 'Address 2', 'City', 'State', 'Zip', 'Country', 'Comments', 'Company', 'Account Number', 'Discount', 'Discount Type', 'Taxable'],
|
|
['Test', 'User', '1', '1', $emailWithSpecialChars, '555-1234', '123 Main St', '', 'Springfield', 'IL', '62701', 'US', '', '', '', '', '', '']
|
|
];
|
|
|
|
$tempFile = $this->createCsvFile($csvContent);
|
|
|
|
$_FILES['file_path'] = [
|
|
'name' => 'test.csv',
|
|
'type' => 'text/csv',
|
|
'tmp_name' => $tempFile,
|
|
'error' => UPLOAD_ERR_OK,
|
|
'size' => filesize($tempFile)
|
|
];
|
|
|
|
$result = $this->post('/customers/importCsvFile');
|
|
|
|
$result->assertOK();
|
|
|
|
$importedCustomer = $this->findCustomersByEmailLike('example.com');
|
|
|
|
$this->assertNotNull($importedCustomer, 'Sanitized email should be imported');
|
|
$this->assertStringNotContainsString('"', $importedCustomer['email'], 'Quote characters should be sanitized');
|
|
|
|
unlink($tempFile);
|
|
}
|
|
|
|
public function testEmptyEmailIsAccepted(): void
|
|
{
|
|
$this->loginAsEmployee();
|
|
|
|
// Empty email should be allowed - customers may not have email addresses
|
|
$csvContent = [
|
|
['First Name', 'Last Name', 'Gender', 'Consent', 'Email', 'Phone', 'Address 1', 'Address 2', 'City', 'State', 'Zip', 'Country', 'Comments', 'Company', 'Account Number', 'Discount', 'Discount Type', 'Taxable'],
|
|
['Empty', 'Mail', '1', '1', '', '555-1234', '123 Main St', '', 'Springfield', 'IL', '62701', 'US', '', '', '', '', '', '']
|
|
];
|
|
|
|
$tempFile = $this->createCsvFile($csvContent);
|
|
|
|
$_FILES['file_path'] = [
|
|
'name' => 'test.csv',
|
|
'type' => 'text/csv',
|
|
'tmp_name' => $tempFile,
|
|
'error' => UPLOAD_ERR_OK,
|
|
'size' => filesize($tempFile)
|
|
];
|
|
|
|
$result = $this->post('/customers/importCsvFile');
|
|
|
|
$result->assertOK();
|
|
|
|
$resultBody = json_decode($result->getJSON(), true);
|
|
$this->assertTrue($resultBody['success'], 'Import should succeed with empty email');
|
|
|
|
// Find customer by name since email is empty
|
|
$importedCustomer = $this->customer->select('customers.*, people.*')
|
|
->join('people', 'people.person_id = customers.person_id')
|
|
->where('first_name', 'Empty')
|
|
->where('last_name', 'Mail')
|
|
->first();
|
|
|
|
$this->assertNotNull($importedCustomer, 'Customer with empty email should be imported');
|
|
$this->assertEquals('', $importedCustomer['email'], 'Email should be empty string');
|
|
|
|
unlink($tempFile);
|
|
}
|
|
} |