From 609cb323d41081affa61299d4a1af8a7b2cf6a0f Mon Sep 17 00:00:00 2001 From: Ollama Date: Fri, 13 Mar 2026 21:35:31 +0000 Subject: [PATCH] Fix: Pass parameter to generate() and add composite format tests - Fixed bug where render() was not passing caller-supplied to generate(), causing ad-hoc tokens to be ignored - Added %F (yyyy-MM-dd) and %D (MM/dd/yy) composite date formats to the IntlDateFormatter pattern map - Added test coverage for composite date format directives (%F, %D, %T, %R) --- app/Libraries/Token_lib.php | 8 ++++--- tests/Libraries/Token_libTest.php | 36 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/Libraries/Token_lib.php b/app/Libraries/Token_lib.php index 4cc218c8d..cd5b72352 100644 --- a/app/Libraries/Token_lib.php +++ b/app/Libraries/Token_lib.php @@ -20,7 +20,9 @@ class Token_lib '%b' => 'MMM', '%B' => 'MMMM', '%d' => 'dd', + '%D' => 'MM/dd/yy', '%e' => 'd', + '%F' => 'yyyy-MM-dd', '%j' => 'D', '%m' => 'MM', '%U' => 'w', @@ -71,7 +73,7 @@ class Token_lib $token_values = []; $tokens_to_replace = []; - $this->generate($token_tree, $tokens_to_replace, $token_values, $save); + $this->generate($token_tree, $tokens, $tokens_to_replace, $token_values, $save); return str_replace($tokens_to_replace, $token_values, $tokened_text); } @@ -192,10 +194,10 @@ class Token_lib return $results; } - private function generate(array $used_tokens, array &$tokens_to_replace, array &$token_values, bool $save = true): void + private function generate(array $used_tokens, array $tokens, array &$tokens_to_replace, array &$token_values, bool $save = true): void { foreach ($used_tokens as $token_code => $token_info) { - $token_value = $this->resolve_token($token_code, [], $save); + $token_value = $this->resolve_token($token_code, $tokens, $save); foreach ($token_info as $length => $token_spec) { $tokens_to_replace[] = $token_spec; diff --git a/tests/Libraries/Token_libTest.php b/tests/Libraries/Token_libTest.php index 003e1a5b8..8a5a52f17 100644 --- a/tests/Libraries/Token_libTest.php +++ b/tests/Libraries/Token_libTest.php @@ -228,4 +228,40 @@ class Token_libTest extends CIUnitTestCase $this->assertArrayHasKey('token1', $result); $this->assertArrayHasKey('token2', $result); } + + public function testRenderReplacesCompositeDirectivePercentF(): void + { + $input = 'Date: %F'; + $result = $this->tokenLib->render($input, [], false); + $this->assertNotEmpty($result); + $this->assertStringNotContainsString('%F', $result); + $this->assertMatchesRegularExpression('/Date: \d{4}-\d{2}-\d{2}/', $result); + } + + public function testRenderReplacesCompositeDirectivePercentD(): void + { + $input = 'Date: %D'; + $result = $this->tokenLib->render($input, [], false); + $this->assertNotEmpty($result); + $this->assertStringNotContainsString('%D', $result); + $this->assertMatchesRegularExpression('/Date: \d{2}\/\d{2}\/\d{2}/', $result); + } + + public function testRenderHandlesPercentT(): void + { + $input = 'Time: %T'; + $result = $this->tokenLib->render($input, [], false); + $this->assertNotEmpty($result); + $this->assertStringNotContainsString('%T', $result); + $this->assertMatchesRegularExpression('/Time: \d{2}:\d{2}:\d{2}/', $result); + } + + public function testRenderHandlesPercentR(): void + { + $input = 'Time: %R'; + $result = $this->tokenLib->render($input, [], false); + $this->assertNotEmpty($result); + $this->assertStringNotContainsString('%R', $result); + $this->assertMatchesRegularExpression('/Time: \d{2}:\d{2}/', $result); + } } \ No newline at end of file