From 54745e2dc518b5ad71b5e4661f91ef9e89387c3d Mon Sep 17 00:00:00 2001 From: michalsn Date: Fri, 12 Jun 2020 17:37:38 +0200 Subject: [PATCH] Throw an exception on insert for an empty object --- system/Model.php | 9 ++++-- tests/system/Database/Live/ModelTest.php | 38 ++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/system/Model.php b/system/Model.php index 5543a262a374..c2dd089953e1 100644 --- a/system/Model.php +++ b/system/Model.php @@ -695,6 +695,11 @@ public function insert($data = null, bool $returnID = true) $data = (array) $data; } + if (empty($data)) + { + throw DataException::forEmptyDataset('insert'); + } + // Validate data before saving. if ($this->skipValidation === false) { @@ -1439,7 +1444,7 @@ public function cleanRules(bool $choice = false) public function validate($data): bool { $rules = $this->getValidationRules(); - + if ($this->skipValidation === true || empty($rules) || empty($data)) { return true; @@ -1580,7 +1585,7 @@ protected function fillPlaceholders(array $rules, array $data): array public function getValidationRules(array $options = []): array { $rules = $this->validationRules; - + // ValidationRules can be either a string, which is the group name, // or an array of rules. if (is_string($rules)) diff --git a/tests/system/Database/Live/ModelTest.php b/tests/system/Database/Live/ModelTest.php index 9228936337bb..75be8a8ef813 100644 --- a/tests/system/Database/Live/ModelTest.php +++ b/tests/system/Database/Live/ModelTest.php @@ -1798,7 +1798,7 @@ public function testUndefinedEntityPropertyReturnsNull() //-------------------------------------------------------------------- - public function testInsertWithNoDataException() + public function testInsertArrayWithNoDataException() { $model = new UserModel(); $data = []; @@ -1809,7 +1809,7 @@ public function testInsertWithNoDataException() //-------------------------------------------------------------------- - public function testUpdateWithNoDataException() + public function testUpdateArrayWithNoDataException() { $model = new EventModel(); @@ -1832,6 +1832,40 @@ public function testUpdateWithNoDataException() //-------------------------------------------------------------------- + public function testInsertObjectWithNoDataException() + { + $model = new UserModel(); + $data = new \stdClass(); + $this->expectException(DataException::class); + $this->expectExceptionMessage('There is no data to insert.'); + $model->insert($data); + } + + //-------------------------------------------------------------------- + + public function testUpdateObjectWithNoDataException() + { + $model = new EventModel(); + + $data = (object) [ + 'name' => 'Foo', + 'email' => 'foo@example.com', + 'country' => 'US', + 'deleted' => 0, + ]; + + $id = $model->insert($data); + + $data = new \stdClass(); + + $this->expectException(DataException::class); + $this->expectExceptionMessage('There is no data to update.'); + + $model->update($id, $data); + } + + //-------------------------------------------------------------------- + public function testInvalidAllowedFieldException() { $model = new JobModel();