From d2bb0d7b998a05abf8609956074846b78d7bcd96 Mon Sep 17 00:00:00 2001 From: Joe Williams Date: Wed, 5 Nov 2025 13:59:53 -0800 Subject: [PATCH] Unit Test Updates - Removed `testHandlesEmptyTokenTree` (it was the same as `testIgnoresTextWithoutPercentSign`). - Added tests for special characters, bad token/date formats, and basic security. - The tests for bad date formats still fail with the updated `render` function. --- tests/libraries/Token_libTest.php | 117 ++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 15 deletions(-) diff --git a/tests/libraries/Token_libTest.php b/tests/libraries/Token_libTest.php index 5b7f5828e..f4335938f 100644 --- a/tests/libraries/Token_libTest.php +++ b/tests/libraries/Token_libTest.php @@ -7,16 +7,16 @@ use PHPUnit\Framework\TestCase; { private token_lib $tokenLib; - private function testHelper(string $tokenText): void + private function testHelper(string $tokenText, bool $save = true): void { $tokens = []; - error_log("-----\nTesting string '$tokenText' with tokens " . implode(", ", $tokens)); + error_log("Testing string '$tokenText' with tokens " . implode(", ", $tokens)); - $currentResult = $this->tokenLib->render($tokenText, $tokens); - error_log("current: $currentResult\n"); + $currentResult = $this->tokenLib->render($tokenText, $tokens, $save); + error_log("current: \"$currentResult\""); - $newResult = $this->tokenLib->renderUpdated($tokenText, $tokens); - error_log("new: $newResult\n\-----\n"); + $newResult = $this->tokenLib->renderUpdated($tokenText, $tokens, $save); + error_log("new: \"$newResult\"\n"); $this->assertEquals($currentResult, $newResult); } @@ -47,13 +47,13 @@ use PHPUnit\Framework\TestCase; { $tokenText = 'Invoice {CO} on %mm-%dd-%yyyy'; $tokens = []; - error_log("-----\nTesting string '$tokenText' with tokens " . implode(", ", $tokens)); + error_log("Testing string '$tokenText' with tokens " . implode(", ", $tokens)); $currentResult = $this->tokenLib->render($tokenText, $tokens); - error_log("current: $currentResult\n"); + error_log("current: \"$currentResult\""); $newResult = $this->tokenLib->renderUpdated($tokenText, $tokens); - error_log("new: $newResult\n\-----\n"); + error_log("new: \"$newResult\"\n"); $this->assertNotEquals($currentResult, $newResult); } @@ -68,18 +68,105 @@ use PHPUnit\Framework\TestCase; $this->testHelper(''); } - public function testHandlesEmptyTokenTree(): void - { - $this->testHelper('No tokens here'); - } - public function testHandlesNonexistentTokens(): void { $this->testHelper('{INVALID}'); } - public function testComplexInvoiceTemplate(): void + public function testHandlesComplexInvoiceTemplate(): void { $this->testHelper('Invoice #{CO:6} - %B %d, %Y - Customer: {CUSTOMER}'); } + + public function testHandlesNewLines(): void + { + $this->testHelper("Invoice {CO}\nDate: %Y-%m-%d"); + $this->testHelper("Invoice {CO}\r\nDate: %Y-%m-%d"); + $this->testHelper("Invoice {CO}\rDate: %Y-%m-%d"); + } + + public function testHandlesTabs(): void + { + $this->testHelper("Invoice\t{CO}\tCustomer\t{CUSTOMER}"); + } + + public function testHandlesNewLinesAndTabs(): void + { + $this->testHelper("Invoice\n\t{CO}\n\tCustomer\n\t{CUSTOMER}"); + } + + public function testHandlesSpecialCharacters(): void + { + $this->testHelper("Invoice #{CO} @ $100 & tax!"); + } + + public function testHandlesUnicode() + { + $this->testHelper("客户 {CUSTOMER} - 发票 {CO}"); + } + + public function testHandlesNestedBraces(): void + { + $this->testHelper("Invoice {{CO}} Date: %Y-%m-%d"); + } + + public function testHandlesUnclosedBraces(): void + { + $this->testHelper("Invoice {CO Date: %Y-%m-%d"); + } + + public function testHandlesUnopenedBraces(): void + { + $this->testHelper("Invoice CO} Date: %Y-%m-%d"); + } + + public function testHandlesDateAtStart(): void + { + $this->testHelper('%Y-%m-%d Invoice {CO}'); + } + + public function testHandlesHtmlTags(): void + { + // if your IDE complains about CO not being defined, ignore it + $this->testHelper(htmlentities("")); + } + + public function testHandlesSqlInjectionAttempt(): void + { + $this->testHelper("'; DROP TABLE--{CO}"); + } + + public function testHandlesVeryLongString(): void + { + // TODO: This test still fails + $this->testHelper(str_repeat('buffer ', 500) . '%Y-%m-%d Invoice {CO}' . str_repeat('buffer ', 500)); + } + + public function testHandlesMultipleDates(): void + { + // TODO: This test still fails + $this->testHelper('%Y-%m-%d Invoice {CO} - %Y-%m-%d'); + } + + public function testHandlesNotDatePercentTokens(): void + { + // TODO: This test still fails + $this->testHelper('Discount: 50%'); + } + + public function testHandlesBadDateFormats(): void + { + // TODO: This test still fails + $this->testHelper("%-%-%"); + $this->testHelper("%Y-%q-%bad"); + $this->testHelper("%a%"); + } + + public function testSaveParameter(): void + { + $this->testHelper('{CO}', false); + $this->testHelper('Plain text', false); + $this->testHelper('Date: %Y-%m-%d', false); + $this->testHelper('Invoice #{CO:6} - %B %d, %Y - Customer: {CUSTOMER}', false); + } }