Skip to content

Commit

Permalink
Merge pull request #3091 from michalsn/fix_insert_empty_object
Browse files Browse the repository at this point in the history
Fix - throw an exception on insert for an empty object
  • Loading branch information
michalsn authored Jun 12, 2020
2 parents 0e44da6 + 54745e2 commit f7dfb40
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 7 additions & 2 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
38 changes: 36 additions & 2 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ public function testUndefinedEntityPropertyReturnsNull()

//--------------------------------------------------------------------

public function testInsertWithNoDataException()
public function testInsertArrayWithNoDataException()
{
$model = new UserModel();
$data = [];
Expand All @@ -1809,7 +1809,7 @@ public function testInsertWithNoDataException()

//--------------------------------------------------------------------

public function testUpdateWithNoDataException()
public function testUpdateArrayWithNoDataException()
{
$model = new EventModel();

Expand All @@ -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();
Expand Down

0 comments on commit f7dfb40

Please sign in to comment.