From 5e368b8bf0c72a425a16f02c7802ee768575031e Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:01:27 +0530 Subject: [PATCH 01/22] Model typos changes --- system/Model.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/Model.php b/system/Model.php index 586e97f2b618..6c17be860a25 100644 --- a/system/Model.php +++ b/system/Model.php @@ -956,9 +956,9 @@ public function onlyDeleted() * @param null $data * @param boolean $returnSQL * - * @return boolean TRUE on success, FALSE on failure + * @return mixed */ - public function replace($data = null, bool $returnSQL = false): bool + public function replace($data = null, bool $returnSQL = false) { // Validate data before saving. if (! empty($data) && $this->skipValidation === false) @@ -1317,7 +1317,7 @@ public function setValidationMessage(string $field, array $fieldMessages) * Validate the data against the validation rules (or the validation group) * specified in the class property, $validationRules. * - * @param array $data + * @param array|object $data * * @return boolean */ From 067d0d534137a9f757ce6fd9505b33af2a8f319e Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:09:17 +0530 Subject: [PATCH 02/22] Corrected --- tests/_support/Models/EntityModel.php | 4 +++- tests/_support/Models/EventModel.php | 2 +- tests/_support/Models/JobModel.php | 6 +++++- tests/_support/Models/SecondaryModel.php | 2 +- tests/_support/Models/SimpleEntity.php | 2 ++ tests/_support/Models/UserModel.php | 2 +- tests/_support/Models/ValidErrorsModel.php | 2 +- tests/_support/Models/ValidModel.php | 2 +- 8 files changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/_support/Models/EntityModel.php b/tests/_support/Models/EntityModel.php index c6ab008a5021..4460242330bf 100644 --- a/tests/_support/Models/EntityModel.php +++ b/tests/_support/Models/EntityModel.php @@ -10,7 +10,9 @@ class EntityModel extends Model protected $useSoftDeletes = false; - protected $dateFormat = 'datetime'; + protected $dateFormat = 'int'; + + protected $deletedField = 'deleted'; protected $allowedFields = [ 'name', diff --git a/tests/_support/Models/EventModel.php b/tests/_support/Models/EventModel.php index 7059b89ce64e..3e27152af518 100644 --- a/tests/_support/Models/EventModel.php +++ b/tests/_support/Models/EventModel.php @@ -10,7 +10,7 @@ class EventModel extends Model protected $useSoftDeletes = false; - protected $dateFormat = 'integer'; + protected $dateFormat = 'int'; protected $allowedFields = [ 'name', diff --git a/tests/_support/Models/JobModel.php b/tests/_support/Models/JobModel.php index cdda38fba6e8..98c4ad28d94e 100644 --- a/tests/_support/Models/JobModel.php +++ b/tests/_support/Models/JobModel.php @@ -10,10 +10,14 @@ class JobModel extends Model protected $useSoftDeletes = false; - protected $dateFormat = 'integer'; + protected $dateFormat = 'int'; protected $allowedFields = [ 'name', 'description', ]; + + public $name = ''; + + public $description = ''; } diff --git a/tests/_support/Models/SecondaryModel.php b/tests/_support/Models/SecondaryModel.php index 6088bd669db5..18e0b0aa2ae4 100644 --- a/tests/_support/Models/SecondaryModel.php +++ b/tests/_support/Models/SecondaryModel.php @@ -12,7 +12,7 @@ class SecondaryModel extends Model protected $useSoftDeletes = false; - protected $dateFormat = 'integer'; + protected $dateFormat = 'int'; protected $allowedFields = [ 'key', diff --git a/tests/_support/Models/SimpleEntity.php b/tests/_support/Models/SimpleEntity.php index 88b3f92644b5..c96ef9e077d0 100644 --- a/tests/_support/Models/SimpleEntity.php +++ b/tests/_support/Models/SimpleEntity.php @@ -15,7 +15,9 @@ class SimpleEntity extends Entity protected $id; protected $name; protected $description; + protected $deleted; protected $created_at; + protected $updated_at; protected $_options = [ 'datamap' => [], diff --git a/tests/_support/Models/UserModel.php b/tests/_support/Models/UserModel.php index c84ca1af32df..7e4908762a4a 100644 --- a/tests/_support/Models/UserModel.php +++ b/tests/_support/Models/UserModel.php @@ -17,5 +17,5 @@ class UserModel extends Model protected $useSoftDeletes = true; - protected $dateFormat = 'integer'; + protected $dateFormat = 'int'; } diff --git a/tests/_support/Models/ValidErrorsModel.php b/tests/_support/Models/ValidErrorsModel.php index 95a2193511da..189ac63be124 100644 --- a/tests/_support/Models/ValidErrorsModel.php +++ b/tests/_support/Models/ValidErrorsModel.php @@ -10,7 +10,7 @@ class ValidErrorsModel extends Model protected $useSoftDeletes = false; - protected $dateFormat = 'integer'; + protected $dateFormat = 'int'; protected $allowedFields = [ 'name', diff --git a/tests/_support/Models/ValidModel.php b/tests/_support/Models/ValidModel.php index a25212094dd1..69842d8808dc 100644 --- a/tests/_support/Models/ValidModel.php +++ b/tests/_support/Models/ValidModel.php @@ -10,7 +10,7 @@ class ValidModel extends Model protected $useSoftDeletes = false; - protected $dateFormat = 'integer'; + protected $dateFormat = 'int'; protected $allowedFields = [ 'name', From e6d381e6327e3e7332527e21675cf8e333aa1a5c Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:12:10 +0530 Subject: [PATCH 03/22] Changed migration table structure --- .../20160428212500_Create_test_tables.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php b/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php index 2483c4b63c35..3912e9b439f9 100644 --- a/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php +++ b/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php @@ -50,9 +50,20 @@ public function up() 'type' => 'TEXT', 'null' => true, ], - 'created_at' => [ - 'type' => 'DATETIME', - 'null' => true, + 'deleted' => [ + 'type' => 'TINYINT', + 'constraint' => 1, + 'default' => '0', + ], + 'created_at' => [ + 'type' => 'INTEGER', + 'constraint' => 11, + 'null' => true, + ], + 'updated_at' => [ + 'type' => 'INTEGER', + 'constraint' => 11, + 'null' => true, ], ]); $this->forge->addKey('id', true); From d4f91874b84217a0fb2f6aaceb8cee64acf266f0 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:19:16 +0530 Subject: [PATCH 04/22] Changed existing model test according to corrected table structure --- tests/system/Database/Live/DbUtilsTest.php | 4 ++-- tests/system/Database/Live/GetTest.php | 2 +- tests/system/Database/Live/ModelTest.php | 15 +++++++-------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/system/Database/Live/DbUtilsTest.php b/tests/system/Database/Live/DbUtilsTest.php index c5b26ad6bf9b..f355d94084d3 100644 --- a/tests/system/Database/Live/DbUtilsTest.php +++ b/tests/system/Database/Live/DbUtilsTest.php @@ -188,7 +188,7 @@ public function testUtilsCSVFromResult() $data = array_filter(preg_split('/(\r\n|\n|\r)/', $data)); - $this->assertEquals('"1","Developer","Awesome job, but sometimes makes you bored",""', $data[1]); + $this->assertEquals('"1","Developer","Awesome job, but sometimes makes you bored","0","",""', $data[1]); } //-------------------------------------------------------------------- @@ -203,7 +203,7 @@ public function testUtilsXMLFromResult() $data = $util->getXMLFromResult($data); - $expected = '4MusicianOnly Coldplay can actually called Musician'; + $expected = '4MusicianOnly Coldplay can actually called Musician'; $actual = preg_replace('#\R+#', '', $data); $actual = preg_replace('/[ ]{2,}|[\t]/', '', $actual); diff --git a/tests/system/Database/Live/GetTest.php b/tests/system/Database/Live/GetTest.php index a1f3b45b4b3b..002d76861986 100644 --- a/tests/system/Database/Live/GetTest.php +++ b/tests/system/Database/Live/GetTest.php @@ -70,7 +70,7 @@ public function testGetFieldCount() ->get() ->getFieldCount(); - $this->assertEquals(4, $jobs); + $this->assertEquals(6, $jobs); } //-------------------------------------------------------------------- diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 98e38834660f..8dad7663578b 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -6,10 +6,7 @@ use CodeIgniter\Model; use CodeIgniter\Test\CIDatabaseTestCase; use CodeIgniter\Test\ReflectionHelper; -use Config\Database; use Config\Services; -use Config\Validation; -use Myth\Auth\Entities\User; use Tests\Support\Models\EntityModel; use Tests\Support\Models\EventModel; use Tests\Support\Models\JobModel; @@ -318,8 +315,8 @@ public function testSaveNewRecordArray() 'description' => 'That thing you do.', ]; - $result = $model->protect(false) - ->save($data); + $model->protect(false) + ->save($data); $this->seeInDatabase('job', ['name' => 'Apprentice']); } @@ -709,15 +706,17 @@ public function testCanCreateAndSaveEntityClasses() $this->assertEquals('Developer', $entity->name); $this->assertEquals('Awesome job, but sometimes makes you bored', $entity->description); + $time = time(); + $entity->name = 'Senior Developer'; - $entity->created_at = '2017-07-15'; + $entity->created_at = $time; $date = $this->getPrivateProperty($entity, 'created_at'); $this->assertInstanceOf(Time::class, $date); $this->assertTrue($model->save($entity)); - $this->seeInDatabase('job', ['name' => 'Senior Developer', 'created_at' => '2017-07-15 00:00:00']); + $this->seeInDatabase('job', ['name' => 'Senior Developer', 'created_at' => $time]); } /** @@ -980,7 +979,7 @@ public function testSelectAndEntitiesSaveOnlyChangedValues() $this->hasInDatabase('job', [ 'name' => 'Rocket Scientist', 'description' => 'Plays guitar for Queen', - 'created_at' => date('Y-m-d H:i:s'), + 'created_at' => time(), ]); $model = new EntityModel(); From 189ef4f80280b989fa7a6b8f755bef2e78c9d7d5 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:41:50 +0530 Subject: [PATCH 05/22] model test case --- tests/system/Database/Live/ModelTest.php | 103 +++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 8dad7663578b..4d8b541bfe42 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -34,6 +34,8 @@ protected function setUp() $this->model = new Model($this->db); } + //-------------------------------------------------------------------- + public function tearDown() { parent::tearDown(); @@ -174,6 +176,8 @@ public function testFindClearsBinds() $this->assertCount(1, $this->getPrivateProperty($query, 'binds')); } + //-------------------------------------------------------------------- + public function testFindAllReturnsAllRecords() { $model = new UserModel($this->db); @@ -265,6 +269,8 @@ public function testFirstRespectsSoftDeletes() $this->assertEquals(1, $user->id); } + //-------------------------------------------------------------------- + public function testFirstWithNoPrimaryKey() { $model = new SecondaryModel(); @@ -573,6 +579,8 @@ public function testValidationPlaceholdersSuccess() $this->assertTrue($model->validate($data)); } + //-------------------------------------------------------------------- + public function testValidationPlaceholdersFail() { $model = new ValidModel($this->db); @@ -586,6 +594,8 @@ public function testValidationPlaceholdersFail() $this->assertFalse($model->validate($data)); } + //-------------------------------------------------------------------- + public function testSkipValidation() { $model = new ValidModel($this->db); @@ -599,6 +609,8 @@ public function testSkipValidation() ->insert($data)); } + //-------------------------------------------------------------------- + public function testCleanValidationRemovesAllWhenNoDataProvided() { $model = new Model($this->db); @@ -614,6 +626,8 @@ public function testCleanValidationRemovesAllWhenNoDataProvided() $this->assertEmpty($rules); } + //-------------------------------------------------------------------- + public function testCleanValidationRemovesOnlyForFieldsNotProvided() { $model = new Model($this->db); @@ -634,6 +648,8 @@ public function testCleanValidationRemovesOnlyForFieldsNotProvided() $this->assertFalse(array_key_exists('name', $rules)); } + //-------------------------------------------------------------------- + public function testCleanValidationReturnsAllWhenAllExist() { $model = new Model($this->db); @@ -655,6 +671,8 @@ public function testCleanValidationReturnsAllWhenAllExist() $this->assertTrue(array_key_exists('name', $rules)); } + //-------------------------------------------------------------------- + public function testValidationPassesWithMissingFields() { $model = new ValidModel(); @@ -668,6 +686,8 @@ public function testValidationPassesWithMissingFields() $this->assertTrue($result); } + //-------------------------------------------------------------------- + public function testValidationWithGroupName() { $config = new \Config\Validation(); @@ -719,6 +739,8 @@ public function testCanCreateAndSaveEntityClasses() $this->seeInDatabase('job', ['name' => 'Senior Developer', 'created_at' => $time]); } + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/580 */ @@ -740,6 +762,8 @@ public function testPasswordsStoreCorrectly() $this->seeInDatabase('user', $data); } + //-------------------------------------------------------------------- + public function testInsertEvent() { $model = new EventModel(); @@ -757,6 +781,8 @@ public function testInsertEvent() $this->assertTrue($model->hasToken('afterInsert')); } + //-------------------------------------------------------------------- + public function testUpdateEvent() { $model = new EventModel(); @@ -775,6 +801,8 @@ public function testUpdateEvent() $this->assertTrue($model->hasToken('afterUpdate')); } + //-------------------------------------------------------------------- + public function testFindEvent() { $model = new EventModel(); @@ -784,6 +812,8 @@ public function testFindEvent() $this->assertTrue($model->hasToken('afterFind')); } + //-------------------------------------------------------------------- + public function testDeleteEvent() { $model = new EventModel(); @@ -793,6 +823,8 @@ public function testDeleteEvent() $this->assertTrue($model->hasToken('afterDelete')); } + //-------------------------------------------------------------------- + public function testSetWorksWithInsert() { $model = new EventModel(); @@ -813,6 +845,8 @@ public function testSetWorksWithInsert() ]); } + //-------------------------------------------------------------------- + public function testSetWorksWithUpdate() { $model = new EventModel(); @@ -839,6 +873,8 @@ public function testSetWorksWithUpdate() ]); } + //-------------------------------------------------------------------- + public function testSetWorksWithUpdateNoId() { $model = new EventModel(); @@ -867,6 +903,8 @@ public function testSetWorksWithUpdateNoId() ]); } + //-------------------------------------------------------------------- + public function testUpdateArray() { $model = new EventModel(); @@ -885,6 +923,8 @@ public function testUpdateArray() $this->seeInDatabase('user', ['id' => 2, 'name' => 'Foo Bar']); } + //-------------------------------------------------------------------- + public function testInsertBatchSuccess() { $job_data = [ @@ -905,6 +945,8 @@ public function testInsertBatchSuccess() $this->seeInDatabase('job', ['name' => 'Cab Driver']); } + //-------------------------------------------------------------------- + public function testInsertBatchValidationFail() { $job_data = [ @@ -924,6 +966,8 @@ public function testInsertBatchValidationFail() $this->assertTrue(isset($error['description'])); } + //-------------------------------------------------------------------- + public function testUpdateBatchSuccess() { $data = [ @@ -1015,6 +1059,8 @@ public function testSelectAndEntitiesSaveOnlyChangedValues() $this->assertEquals('Some guitar description', $job->description); } + //-------------------------------------------------------------------- + public function testUpdateNoPrimaryKey() { $model = new SecondaryModel(); @@ -1040,6 +1086,8 @@ public function testUpdateNoPrimaryKey() ]); } + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1617 */ @@ -1056,6 +1104,8 @@ public function testCountAllResultsRespectsSoftDeletes() $this->assertEquals(3, $model->countAllResults()); } + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1584 */ @@ -1081,6 +1131,8 @@ public function testUpdateWithValidation() $this->assertTrue($result); } + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1717 */ @@ -1095,6 +1147,8 @@ public function testRequiredWithValidationEmptyString() $this->assertFalse($model->insert($data)); } + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1717 */ @@ -1109,6 +1163,8 @@ public function testRequiredWithValidationNull() $this->assertFalse($model->insert($data)); } + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1717 */ @@ -1124,6 +1180,8 @@ public function testRequiredWithValidationTrue() $this->assertTrue($model->insert($data) !== false); } + //-------------------------------------------------------------------- + /** * @see https://github.com/codeigniter4/CodeIgniter4/issues/1574 */ @@ -1147,6 +1205,8 @@ public function testValidationIncludingErrors() $this->assertEquals('Minimum Length Error', $model->errors()['name']); } + //-------------------------------------------------------------------- + /** * @expectedException \CodeIgniter\Exceptions\ModelException * @expectedExceptionMessage `Tests\Support\Models\UserModel` model class does not specify a Primary Key. @@ -1158,4 +1218,47 @@ public function testThrowsWithNoPrimaryKey() $model->find(1); } + + //-------------------------------------------------------------------- + + public function testInsertID() + { + $model = new JobModel(); + + $data = [ + 'name' => 'Apprentice', + 'description' => 'That thing you do.', + ]; + + $model->protect(false) + ->save($data); + + $lastInsertId = $model->getInsertID(); + + $this->seeInDatabase('job', ['id' => $lastInsertId]); + } + + //-------------------------------------------------------------------- + + public function testSetTable() + { + $model = new JobModel(); + + $model->setTable('db_job'); + + $data = [ + 'name' => 'Apprentice', + 'description' => 'That thing you do.', + ]; + + $model->protect(false) + ->save($data); + + $lastInsertId = $model->getInsertID(); + + $this->seeInDatabase('job', ['id' => $lastInsertId]); + } + + //-------------------------------------------------------------------- + } From 4fdcec6bdc1cfd62f7baf45bef9d7fe6e64be606 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:53:45 +0530 Subject: [PATCH 06/22] model pagination test case --- tests/system/Database/Live/ModelTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 4d8b541bfe42..8bddfb5f4720 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1261,4 +1261,15 @@ public function testSetTable() //-------------------------------------------------------------------- + public function testPaginate() + { + $model = new ValidModel($this->db); + + $data = $model->paginate(); + + $this->assertEquals(4, count($data)); + } + + //-------------------------------------------------------------------- + } From e3641fa272c7ec362d69655bbf411f4a07a98241 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:54:40 +0530 Subject: [PATCH 07/22] model validation test case --- tests/system/Database/Live/ModelTest.php | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 8bddfb5f4720..5a2bf8578902 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1272,4 +1272,59 @@ public function testPaginate() //-------------------------------------------------------------------- + public function testValidationByObject() + { + $model = new ValidModel($this->db); + + $data = new class + { + public $name = ''; + public $id = ''; + public $token = ''; + }; + + $data->name = 'abc'; + $data->id = '13'; + $data->token = '13'; + + $this->assertTrue($model->validate($data)); + } + + //-------------------------------------------------------------------- + + public function testGetValidationRules() + { + $model = new JobModel($this->db); + + $this->setPrivateProperty($model, 'validationRules', ['description' => 'required']); + + $rules = $model->getValidationRules(); + + $this->assertEquals('required', $rules['description']); + } + + //-------------------------------------------------------------------- + + public function testGetValidationMessages() + { + $job_data = [ + [ + 'name' => 'Comedian', + 'description' => null, + ], + ]; + + $model = new JobModel($this->db); + + $this->setPrivateProperty($model, 'validationRules', ['description' => 'required']); + $this->setPrivateProperty($model, 'validationMessages', ['description' => 'Description field is required.']); + + $this->assertFalse($model->insertBatch($job_data)); + + $error = $model->getValidationMessages(); + $this->assertEquals('Description field is required.', $error['description']); + } + + //-------------------------------------------------------------------- + } From a404370971f979bb5bd30d101bc45053f6627e69 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:55:14 +0530 Subject: [PATCH 08/22] model getModelDetails test case --- tests/system/Database/Live/ModelTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 5a2bf8578902..2e18964cfe28 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1327,4 +1327,16 @@ public function testGetValidationMessages() //-------------------------------------------------------------------- + public function testGetGetModelDetails() + { + $model = new JobModel($this->db); + + $this->assertEquals('job', $model->table); + $this->assertEquals('id', $model->primaryKey); + $this->assertEquals('object', $model->returnType); + $this->assertNull($model->DBGroup); + } + + //-------------------------------------------------------------------- + } From 9ffb9dcc713058742f48eb416b52a938814b6e89 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:55:49 +0530 Subject: [PATCH 09/22] model save data test case --- tests/system/Database/Live/ModelTest.php | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 2e18964cfe28..78fc2f565155 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1339,4 +1339,38 @@ public function testGetGetModelDetails() //-------------------------------------------------------------------- + public function testSaveObject() + { + $model = new ValidModel($this->db); + + $testModel = new JobModel(); + + $testModel->name = 'my name'; + $testModel->description = 'some description'; + + $this->setPrivateProperty($model, 'useTimestamps', true); + + $model->insert($testModel); + + $lastInsertId = $model->getInsertID(); + + $this->seeInDatabase('job', ['id' => $lastInsertId]); + } + + //-------------------------------------------------------------------- + + public function testEmptySaveData() + { + $model = new JobModel(); + + $data = []; + + $data = $model->protect(false) + ->save($data); + + $this->assertTrue($data); + } + + //-------------------------------------------------------------------- + } From 1927bcf545d8ba4d6973bf291fcfebd14b303e36 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:56:37 +0530 Subject: [PATCH 10/22] model update by object test case --- tests/system/Database/Live/ModelTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 78fc2f565155..538995d20cd8 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1373,4 +1373,21 @@ public function testEmptySaveData() //-------------------------------------------------------------------- + public function testUpdateObject() + { + $model = new ValidModel($this->db); + + $testModel = new JobModel(); + + $testModel->name = 'my name'; + $testModel->description = 'some description'; + + $this->setPrivateProperty($model, 'useTimestamps', true); + + $model->update(1, $testModel); + + $this->seeInDatabase('job', ['id' => 1]); + } + + //-------------------------------------------------------------------- } From dbffad6c118b0b2470f1f68e2cad76a2e416b61f Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:57:17 +0530 Subject: [PATCH 11/22] model softDelete test case --- tests/system/Database/Live/ModelTest.php | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 538995d20cd8..e82bb8ad3b42 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1390,4 +1390,35 @@ public function testUpdateObject() } //-------------------------------------------------------------------- + + public function testDeleteWithSoftDelete() + { + $model = new JobModel(); + + $this->setPrivateProperty($model, 'useTimestamps', true); + $this->setPrivateProperty($model, 'useSoftDeletes', true); + + $model->delete(1); + + $this->seeInDatabase('job', ['id' => 1, 'deleted' => 1]); + } + + //-------------------------------------------------------------------- + + public function testPurgeDeletedWithSoftDeleteFalse() + { + $model = new JobModel(); + + $this->db->table('job') + ->where('id', 1) + ->update(['deleted' => 1]); + + $model->purgeDeleted(); + + $jobs = $model->findAll(); + + $this->assertCount(4, $jobs); + } + + //-------------------------------------------------------------------- } From ae63bc77fe7201bfc4ac7b0fa513196cc8021aed Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 12:57:57 +0530 Subject: [PATCH 12/22] model replace data test case --- tests/system/Database/Live/ModelTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index e82bb8ad3b42..ba8e576febe9 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1421,4 +1421,21 @@ public function testPurgeDeletedWithSoftDeleteFalse() } //-------------------------------------------------------------------- + + public function testReplaceObject() + { + $model = new ValidModel($this->db); + + $data = [ + 'id' => 1, + 'name' => 'my name', + 'description' => 'some description', + ]; + + $model->replace($data); + + $this->seeInDatabase('job', ['id' => 1, 'name' => 'my name']); + } + + //-------------------------------------------------------------------- } From 8462b7454abd8f553102b08ece2d06a5af926758 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 13:46:07 +0530 Subject: [PATCH 13/22] model replace validation test case --- tests/system/Database/Live/ModelTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index ba8e576febe9..3b0d1164f2fa 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1438,4 +1438,21 @@ public function testReplaceObject() } //-------------------------------------------------------------------- + + public function testGetValidationMessagesForReplace() + { + $job_data = [ + 'name' => 'Comedian', + 'description' => null, + ]; + + $model = new JobModel($this->db); + + $this->setPrivateProperty($model, 'validationRules', ['description' => 'required']); + + $this->assertFalse($model->replace($job_data)); + + $error = $model->errors(); + $this->assertTrue(isset($error['description'])); + } } From 26e65b85c5000f126522fce3ce1787296d6bfe1e Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 16:25:48 +0530 Subject: [PATCH 14/22] Changed table structure for forge coverage with all datetime format --- .../20160428212500_Create_test_tables.php | 16 ++++++++++++++++ tests/_support/Models/EventModel.php | 2 +- tests/_support/Models/UserModel.php | 8 +++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php b/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php index 3912e9b439f9..6bddd8ac4cd2 100644 --- a/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php +++ b/tests/_support/Database/Migrations/20160428212500_Create_test_tables.php @@ -31,6 +31,14 @@ public function up() 'constraint' => 1, 'default' => '0', ], + 'created_at' => [ + 'type' => 'DATETIME', + 'null' => true, + ], + 'updated_at' => [ + 'type' => 'DATETIME', + 'null' => true, + ], ]); $this->forge->addKey('id', true); $this->forge->createTable('user', true); @@ -96,6 +104,14 @@ public function up() 'type' => 'VARCHAR', 'constraint' => 40, ], + 'created_at' => [ + 'type' => 'DATE', + 'null' => true, + ], + 'updated_at' => [ + 'type' => 'DATE', + 'null' => true, + ], ]); $this->forge->addKey('id', true); $this->forge->createTable('empty', true); diff --git a/tests/_support/Models/EventModel.php b/tests/_support/Models/EventModel.php index 3e27152af518..e9df2ecd6166 100644 --- a/tests/_support/Models/EventModel.php +++ b/tests/_support/Models/EventModel.php @@ -10,7 +10,7 @@ class EventModel extends Model protected $useSoftDeletes = false; - protected $dateFormat = 'int'; + protected $dateFormat = 'datetime'; protected $allowedFields = [ 'name', diff --git a/tests/_support/Models/UserModel.php b/tests/_support/Models/UserModel.php index 7e4908762a4a..05018820f380 100644 --- a/tests/_support/Models/UserModel.php +++ b/tests/_support/Models/UserModel.php @@ -17,5 +17,11 @@ class UserModel extends Model protected $useSoftDeletes = true; - protected $dateFormat = 'int'; + protected $dateFormat = 'datetime'; + + public $name = ''; + + public $email = ''; + + public $country = ''; } From 0d1b6f239ef1786734ee60f5b4b5a82c05899d81 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 16:27:31 +0530 Subject: [PATCH 15/22] Forge coverage datetime formats test cases --- tests/system/Database/Live/ModelTest.php | 79 ++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 3b0d1164f2fa..591c36ba7a25 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -2,6 +2,7 @@ use CodeIgniter\Config\Config; use CodeIgniter\Database\Exceptions\DataException; +use CodeIgniter\Entity; use CodeIgniter\I18n\Time; use CodeIgniter\Model; use CodeIgniter\Test\CIDatabaseTestCase; @@ -1455,4 +1456,82 @@ public function testGetValidationMessagesForReplace() $error = $model->errors(); $this->assertTrue(isset($error['description'])); } + + //-------------------------------------------------------------------- + + public function testSaveNewEntityWithDateTime() + { + $entity = new class extends Entity{ + protected $id; + protected $name; + protected $email; + protected $country; + protected $deleted; + protected $created_at; + protected $updated_at; + + protected $_options = [ + 'datamap' => [], + 'dates' => [ + 'created_at', + 'updated_at', + 'deleted_at', + ], + 'casts' => [], + ]; + }; + $testModel = new UserModel(); + + $entity->name = 'Mark'; + $entity->email = 'mark@example.com'; + $entity->country = 'India'; + $entity->deleted = 0; + $entity->created_at = new Time('now'); + + $this->setPrivateProperty($testModel, 'useTimestamps', true); + + $this->assertTrue($testModel->save($entity)); + } + + //-------------------------------------------------------------------- + + public function testSaveNewEntityWithDate() + { + $entity = new class extends Entity + { + protected $id; + protected $name; + protected $created_at; + protected $updated_at; + protected $_options = [ + 'datamap' => [], + 'dates' => [ + 'created_at', + 'updated_at', + 'deleted_at', + ], + 'casts' => [], + ]; + }; + $testModel = new class extends Model + { + protected $table = 'empty'; + protected $allowedFields = [ + 'name', + ]; + protected $returnType = 'object'; + protected $useSoftDeletes = true; + protected $dateFormat = 'date'; + public $name = ''; + }; + + $entity->name = 'Mark'; + $entity->created_at = new Time('now'); + + $this->setPrivateProperty($testModel, 'useTimestamps', true); + + $this->assertTrue($testModel->save($entity)); + + $testModel->truncate(); + } } From ecd5b7960f7cce88efcadca5e2a3baa6dca12c2f Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sat, 13 Apr 2019 23:48:56 +0530 Subject: [PATCH 16/22] Migration code coverage test case --- .../Database/Migrations/MigrationTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/system/Database/Migrations/MigrationTest.php diff --git a/tests/system/Database/Migrations/MigrationTest.php b/tests/system/Database/Migrations/MigrationTest.php new file mode 100644 index 000000000000..14432484bb4a --- /dev/null +++ b/tests/system/Database/Migrations/MigrationTest.php @@ -0,0 +1,24 @@ +getDBGroup(); + + $this->assertEquals('default', $dbGroup); + } +} From 8fdb1251427502c1bd6000323a1cedf9a726201d Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 14 Apr 2019 00:04:28 +0530 Subject: [PATCH 17/22] Migration code coverage test case --- tests/system/Database/Migrations/MigrationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Database/Migrations/MigrationTest.php b/tests/system/Database/Migrations/MigrationTest.php index 14432484bb4a..b4d51bfc0f65 100644 --- a/tests/system/Database/Migrations/MigrationTest.php +++ b/tests/system/Database/Migrations/MigrationTest.php @@ -12,13 +12,13 @@ public function setUp() public function testDBGroup() { $migration = new class extends Migration { - protected $DBGroup = 'default'; + protected $DBGroup = 'tests'; function up(){} function down(){} }; $dbGroup = $migration->getDBGroup(); - $this->assertEquals('default', $dbGroup); + $this->assertEquals('tests', $dbGroup); } } From c015ef1d5759bd0189e133616b5c8280122c399a Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 14 Apr 2019 00:59:54 +0530 Subject: [PATCH 18/22] Entity and Model corrections --- system/Entity.php | 1 + system/Model.php | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/system/Entity.php b/system/Entity.php index 6a7472a55f93..312752ac4d2e 100644 --- a/system/Entity.php +++ b/system/Entity.php @@ -38,6 +38,7 @@ namespace CodeIgniter; +use CodeIgniter\Exceptions\EntityException; use CodeIgniter\I18n\Time; use CodeIgniter\Exceptions\CastException; diff --git a/system/Model.php b/system/Model.php index 6c17be860a25..30f4f2e9cde8 100644 --- a/system/Model.php +++ b/system/Model.php @@ -619,6 +619,11 @@ public function insert($data = null, bool $returnID = true) $this->tempData = []; } + if (empty($data)) + { + throw DataException::forEmptyDataset('insert'); + } + // If $data is using a custom class with public or protected // properties representing the table elements, we need to grab // them as an array. @@ -668,11 +673,6 @@ public function insert($data = null, bool $returnID = true) $data = $this->trigger('beforeInsert', ['data' => $data]); - if (empty($data)) - { - throw DataException::forEmptyDataset('insert'); - } - // Must use the set() method to ensure objects get converted to arrays $result = $this->builder() ->set($data['data'], '', $escape) @@ -749,6 +749,11 @@ public function update($id = null, $data = null): bool $this->tempData = []; } + if (empty($data)) + { + throw DataException::forEmptyDataset('update'); + } + // If $data is using a custom class with public or protected // properties representing the table elements, we need to grab // them as an array. @@ -790,11 +795,6 @@ public function update($id = null, $data = null): bool $data = $this->trigger('beforeUpdate', ['id' => $id, 'data' => $data]); - if (empty($data)) - { - throw DataException::forEmptyDataset('update'); - } - $builder = $this->builder(); if ($id) From 515b55d84c10564fda7a6494b2bdcb05e2a56fda Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 14 Apr 2019 01:01:16 +0530 Subject: [PATCH 19/22] Entity undefined property exception test case --- tests/system/Database/Live/ModelTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 591c36ba7a25..7b2b9fde056f 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -3,6 +3,7 @@ use CodeIgniter\Config\Config; use CodeIgniter\Database\Exceptions\DataException; use CodeIgniter\Entity; +use CodeIgniter\Exceptions\EntityException; use CodeIgniter\I18n\Time; use CodeIgniter\Model; use CodeIgniter\Test\CIDatabaseTestCase; @@ -1534,4 +1535,20 @@ public function testSaveNewEntityWithDate() $testModel->truncate(); } + + //-------------------------------------------------------------------- + + public function testUndefinedEntityPropertyException() + { + $entity = new class extends Entity + { + protected $id; + protected $name; + }; + + $this->expectException(EntityException::class); + $entity->undefinedProperty; + } + + //-------------------------------------------------------------------- } From 43451aa87697a5395f46ced48617883abf6d1aa7 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 14 Apr 2019 01:02:02 +0530 Subject: [PATCH 20/22] Insert and Update no data exception test case --- tests/system/Database/Live/ModelTest.php | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 7b2b9fde056f..165223d30b3a 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1551,4 +1551,39 @@ public function testUndefinedEntityPropertyException() } //-------------------------------------------------------------------- + + public function testInsertWithNoDataException() + { + $model = new UserModel(); + $data = []; + $this->expectException(DataException::class); + $this->expectExceptionMessage('There is no data to insert.'); + $model->insert($data); + } + + //-------------------------------------------------------------------- + + public function testUpdateWithNoDataException() + { + $model = new EventModel(); + + $data = [ + 'name' => 'Foo', + 'email' => 'foo@example.com', + 'country' => 'US', + 'deleted' => 0, + ]; + + $id = $model->insert($data); + + $data = []; + + $this->expectException(DataException::class); + $this->expectExceptionMessage('There is no data to update.'); + + $model->update($id, $data); + } + + //-------------------------------------------------------------------- + } From f00ee6b17ef39d169dccf18eeddae46685ed7070 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Sun, 14 Apr 2019 01:02:53 +0530 Subject: [PATCH 21/22] Invalid field and event exception test case --- tests/system/Database/Live/ModelTest.php | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 165223d30b3a..c208a418bf50 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1586,4 +1586,44 @@ public function testUpdateWithNoDataException() //-------------------------------------------------------------------- + public function testInvalidAllowedFieldException() + { + $model = new JobModel(); + + $data = [ + 'name' => 'Apprentice', + 'description' => 'That thing you do.', + ]; + + $this->setPrivateProperty($model,'allowedFields', []); + + $this->expectException(DataException::class); + $this->expectExceptionMessage('Allowed fields must be specified for model: Tests\Support\Models\JobModel'); + + $model->save($data); + } + + //-------------------------------------------------------------------- + + public function testInvalidEventException() + { + $model = new EventModel(); + + $data = [ + 'name' => 'Foo', + 'email' => 'foo@example.com', + 'country' => 'US', + 'deleted' => 0, + ]; + + $this->setPrivateProperty($model,'beforeInsert',['anotherBeforeInsertMethod']); + + $this->expectException(DataException::class); + $this->expectExceptionMessage('anotherBeforeInsertMethod is not a valid Model Event callback.'); + + $model->insert($data); + } + + //-------------------------------------------------------------------- + } From 70b0862132cce99343e174bb443d044bfb4f2528 Mon Sep 17 00:00:00 2001 From: Atish Amte Date: Wed, 17 Apr 2019 22:41:22 +0530 Subject: [PATCH 22/22] Updated ModelTest Case --- tests/system/Database/Live/ModelTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index c208a418bf50..167dd5c1f1d1 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -733,12 +733,13 @@ public function testCanCreateAndSaveEntityClasses() $entity->name = 'Senior Developer'; $entity->created_at = $time; - $date = $this->getPrivateProperty($entity, 'created_at'); - $this->assertInstanceOf(Time::class, $date); - $this->assertTrue($model->save($entity)); - $this->seeInDatabase('job', ['name' => 'Senior Developer', 'created_at' => $time]); + $result = $model->where('name', 'Senior Developer') + ->get() + ->getFirstRow(); + + $this->assertEquals(date('Y-m-d', $time), date('Y-m-d', $result->created_at)); } //--------------------------------------------------------------------