Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/_support/Models/UserTimestampModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Models;

use CodeIgniter\Model;

class UserTimestampModel extends Model
{
protected $table = 'user';
protected $allowedFields = [
'name',
'email',
'country',
];
protected $returnType = 'array';
protected $useSoftDeletes = true;
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
}
294 changes: 294 additions & 0 deletions tests/system/Models/TimestampModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Models;

use CodeIgniter\I18n\Time;
use Tests\Support\Entity\User;
use Tests\Support\Models\UserTimestampModel;

/**
* @group DatabaseLive
*
* @internal
*/
final class TimestampModelTest extends LiveModelTestCase
{
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = false;
protected $seed = '';

protected function tearDown(): void
{
parent::tearDown();

// Reset current time.
Time::setTestNow();
}

/**
* @return int|string Insert ID
*/
private function allowDatesPrepareOneRecord(array $data)
{
$this->createModel(UserTimestampModel::class);
$this->db->table('user')->truncate();

$this->model->setAllowedFields([
'name',
'email',
'country',
'created_at',
'updated_at',
'deleted_at',
]);

return $this->model->insert($data, true);
}

/**
* @return int|string Insert ID
*/
private function doNotAllowDatesPrepareOneRecord(array $data)
{
$this->createModel(UserTimestampModel::class);
$this->db->table('user')->truncate();

$this->model->setAllowedFields([
'name',
'email',
'country',
// no 'created_at',
// no 'updated_at',
'deleted_at',
]);

return $this->model->insert($data, true);
}

public function testDoNotAllowDatesInsertArrayWithoutDatesSetsTimestamp(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
// no created_at
// no updated_at
];
$id = $this->doNotAllowDatesPrepareOneRecord($data);

$user = $this->model->find($id);

$expected = '2023-11-25 12:00:00';
if ($this->db->DBDriver === 'SQLSRV') {
$expected .= '.000';
}
$this->assertSame($expected, $user['created_at']);
$this->assertSame($expected, $user['updated_at']);
}

public function testDoNotAllowDatesInsertArrayWithDatesSetsTimestamp(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
'created_at' => '2000-01-01 12:00:00',
'updated_at' => '2000-01-01 12:00:00',
];
$id = $this->doNotAllowDatesPrepareOneRecord($data);

$user = $this->model->find($id);

$expected = '2023-11-25 12:00:00';
if ($this->db->DBDriver === 'SQLSRV') {
$expected .= '.000';
}
$this->assertSame($expected, $user['created_at']);
$this->assertSame($expected, $user['updated_at']);
}

public function testDoNotAllowDatesUpdateArrayUpdatesUpdatedAt(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
'created_at' => '2000-01-01 12:00:00',
'updated_at' => '2000-01-01 12:00:00',
];
$id = $this->doNotAllowDatesPrepareOneRecord($data);

$user = $this->model->find($id);

$user['country'] = 'CA';
$this->model->update($user['id'], $user);

$user = $this->model->find($id);

$expected = '2023-11-25 12:00:00';
if ($this->db->DBDriver === 'SQLSRV') {
$expected .= '.000';
}
$this->assertSame($expected, $user['created_at']);
$this->assertSame($expected, $user['updated_at']);
}

public function testDoNotAllowDatesUpdateEntityUpdatesUpdatedAt(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
'created_at' => '2000-01-01 12:00:00',
'updated_at' => '2000-01-01 12:00:00',
];
$id = $this->doNotAllowDatesPrepareOneRecord($data);
$this->setPrivateProperty($this->model, 'returnType', User::class);
$this->setPrivateProperty($this->model, 'tempReturnType', User::class);

$user = $this->model->find($id);

$user->country = 'CA';
$this->model->update($user->id, $user);

$user = $this->model->find($id);

$this->assertSame('2023-11-25 12:00:00', (string) $user->created_at);
$this->assertSame('2023-11-25 12:00:00', (string) $user->updated_at);
}

/**
* We do not recommend to add timestamp fields to $allowedFields.
* If you want to add old data to these fields, use Query Builder.
*/
public function testAllowDatesInsertArrayWithoutDatesSetsTimestamp(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
// no created_at
// no updated_at
];
$id = $this->allowDatesPrepareOneRecord($data);

$user = $this->model->find($id);

$expected = '2023-11-25 12:00:00';
if ($this->db->DBDriver === 'SQLSRV') {
$expected .= '.000';
}
$this->assertSame($expected, $user['created_at']);
$this->assertSame($expected, $user['updated_at']);
}

/**
* We do not recommend to add timestamp fields to $allowedFields.
* If you want to add old data to these fields, use Query Builder.
*/
public function testAllowDatesInsertArrayWithDatesSetsTimestamp(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
'created_at' => '2000-01-01 12:00:00',
'updated_at' => '2000-01-01 12:00:00',
];
$id = $this->allowDatesPrepareOneRecord($data);

$user = $this->model->find($id);

$expected = '2000-01-01 12:00:00';
if ($this->db->DBDriver === 'SQLSRV') {
$expected .= '.000';
}
$this->assertSame($expected, $user['created_at']);
$this->assertSame($expected, $user['updated_at']);
}

/**
* We do not recommend to add timestamp fields to $allowedFields.
* If you want to add old data to these fields, use Query Builder.
*/
public function testAllowDatesUpdateArrayUpdatesUpdatedAt(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
'created_at' => '2000-01-01 12:00:00',
'updated_at' => '2000-01-01 12:00:00',
];
$id = $this->allowDatesPrepareOneRecord($data);

$user = $this->model->find($id);

$user['country'] = 'CA';
$this->model->update($user['id'], $user);

$user = $this->model->find($id);

$expected = '2000-01-01 12:00:00';
if ($this->db->DBDriver === 'SQLSRV') {
$expected .= '.000';
}
$this->assertSame($expected, $user['created_at']);
$this->assertSame($expected, $user['updated_at']);
}

/**
* We do not recommend to add timestamp fields to $allowedFields.
* If you want to add old data to these fields, use Query Builder.
*/
public function testAllowDatesUpdateEntityUpdatesUpdatedAt(): void
{
Time::setTestNow('2023-11-25 12:00:00');

$data = [
'name' => 'John Smith',
'email' => 'john@example.com',
'country' => 'US',
'created_at' => '2000-01-01 12:00:00',
'updated_at' => '2000-01-01 12:00:00',
];
$id = $this->allowDatesPrepareOneRecord($data);
$this->setPrivateProperty($this->model, 'returnType', User::class);
$this->setPrivateProperty($this->model, 'tempReturnType', User::class);

$user = $this->model->find($id);

$user->country = 'CA';
$this->model->update($user->id, $user);

$user = $this->model->find($id);

$this->assertSame('2000-01-01 12:00:00', (string) $user->created_at);
// The Entity has `updated_at` value, but it will be discarded because of onlyChanged.
$this->assertSame('2023-11-25 12:00:00', (string) $user->updated_at);
}
}