diff --git a/system/BaseModel.php b/system/BaseModel.php index 64d083fe4d2b..ad79f6f42ec6 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -1425,9 +1425,11 @@ protected function doProtectFieldsForInsert(array $row): array */ protected function setDate(?int $userDate = null) { - $currentDate = $userDate ?? Time::now()->getTimestamp(); + if ($userDate === null && $this->dateFormat === 'datetime') { + return $this->timeToDate(Time::now()); + } - return $this->intToDate($currentDate); + return $this->intToDate($userDate ?? Time::now()->getTimestamp()); } /** diff --git a/tests/system/CLI/CommandsTest.php b/tests/system/CLI/CommandsTest.php index 34bb0601da65..cd284960909d 100644 --- a/tests/system/CLI/CommandsTest.php +++ b/tests/system/CLI/CommandsTest.php @@ -13,7 +13,9 @@ namespace CodeIgniter\CLI; +use App\Commands\ListCommands as AppListCommands; use CodeIgniter\Autoloader\FileLocatorInterface; +use CodeIgniter\Commands\ListCommands; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; use Config\Services; @@ -42,22 +44,6 @@ protected function resetAll(): void CLI::reset(); } - private function copyAppListCommands(): void - { - if (! is_dir(APPPATH . 'Commands')) { - mkdir(APPPATH . 'Commands'); - } - - copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php'); - } - - private function deleteAppListCommands(): void - { - if (is_file(APPPATH . 'Commands/ListCommands.php')) { - unlink(APPPATH . 'Commands/ListCommands.php'); - } - } - public function testRunOnUnknownCommand(): void { $commands = new Commands(); @@ -166,13 +152,22 @@ public function testDiscoverCommandsWithNoFiles(): void public function testDiscoveredCommandsCanBeOverridden(): void { - $this->copyAppListCommands(); + // The fixture sits outside any PSR-4 root, so the autoloader cannot load it. + require_once SUPPORTPATH . '_command/ListCommands.php'; + + $files = [ + SUPPORTPATH . '_command/ListCommands.php' => AppListCommands::class, + SYSTEMPATH . 'Commands/ListCommands.php' => ListCommands::class, + ]; + + $locator = $this->createMock(FileLocatorInterface::class); + $locator->method('listFiles')->with('Commands/')->willReturn(array_keys($files)); + $locator->method('findQualifiedNameFromPath')->willReturnCallback(static fn (string $file): string => $files[$file]); + Services::injectMock('locator', $locator); - command('list'); + (new Commands())->run('list', []); $this->assertStringContainsString('This is App\Commands\ListCommands', $this->getStreamFilterBuffer()); $this->assertStringNotContainsString('Displays basic usage information.', $this->getStreamFilterBuffer()); - - $this->deleteAppListCommands(); } } diff --git a/tests/system/Models/GeneralModelTest.php b/tests/system/Models/GeneralModelTest.php index bd9f2d14109e..eb72f6aec5eb 100644 --- a/tests/system/Models/GeneralModelTest.php +++ b/tests/system/Models/GeneralModelTest.php @@ -15,8 +15,11 @@ use CodeIgniter\Database\BaseConnection; use CodeIgniter\Exceptions\BadMethodCallException; +use CodeIgniter\I18n\Time; use CodeIgniter\Model; use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\Mock\MockConnection; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use Tests\Support\Models\JobModel; use Tests\Support\Models\UserModel; @@ -39,6 +42,7 @@ protected function tearDown(): void { parent::tearDown(); $this->resetServices(); + Time::setTestNow(); } /** @@ -111,6 +115,28 @@ public function testSetAllowedFields(): void $this->assertSame($allowed2, $this->getPrivateProperty($model, 'allowedFields')); } + #[DataProvider('provideCurrentTimestampPreservesSubseconds')] + public function testCurrentTimestampPreservesSubseconds(string $format, string $expected): void + { + Time::setTestNow('2024-07-09 09:13:34.654321'); + + $db = new MockConnection(['dateFormat' => ['datetime' => $format]]); + $model = $this->createModel(UserModel::class, $db); + $date = self::getPrivateMethodInvoker($model, 'setDate'); + + $this->assertSame($expected, $date()); + } + + /** + * @return iterable + */ + public static function provideCurrentTimestampPreservesSubseconds(): iterable + { + yield 'milliseconds' => ['Y-m-d H:i:s.v', '2024-07-09 09:13:34.654']; + + yield 'microseconds' => ['Y-m-d H:i:s.u', '2024-07-09 09:13:34.654321']; + } + public function testBuilderUsesModelTable(): void { $builder = $this->createModel(UserModel::class)->builder(); diff --git a/user_guide_src/source/changelogs/v4.7.5.rst b/user_guide_src/source/changelogs/v4.7.5.rst index 1f10e03cdf21..83e0bf47742a 100644 --- a/user_guide_src/source/changelogs/v4.7.5.rst +++ b/user_guide_src/source/changelogs/v4.7.5.rst @@ -40,6 +40,7 @@ Bugs Fixed ********** - **Autoloader:** Fixed a bug where ``FileLocatorCached::deleteCache()`` left the deleted data in memory, so it could be written back to the cache file on shutdown. ``spark optimize`` and ``spark cache:clear`` now clear the shared locator's cache instead of a separate instance. +- **BaseModel:** Fixed a bug where auto-generated ``created_at``/``updated_at`` timestamps always rendered ``.000000`` for a ``'datetime'`` ``$dateFormat`` whose connection ``dateFormat`` mask includes ``.v``/``.u``, instead of the real sub-second value. - **CLI:** Fixed a bug where pressing backspace in a ``CLI::prompt()`` erased the prompt text when the ``readline`` extension is enabled. The prompt is now passed to ``readline()`` so line redraws repaint it. ANSI color codes in the prompt (e.g., option defaults) are wrapped in readline's non-printing markers under GNU readline so cursor positioning stays accurate. On Windows, where the ``readline`` extension is built on WinEditLine, the prompt is written to STDOUT first because WinEditLine reports no library version and prints ANSI sequences literally.