Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional empty checks after field protection for update/insert. #4195

Merged
merged 4 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,13 @@ public function insert($data = null, bool $returnID = true)
// strip out created_at values.
$data = $this->doProtectFields($data);

// doProtectFields() can further remove elements from
// $data so we need to check for empty dataset again
if (empty($data))
{
throw DataException::forEmptyDataset('insert');
}

// Set created_at and updated_at with same time
$date = $this->setDate();

Expand Down Expand Up @@ -866,6 +873,13 @@ public function update($id = null, $data = null): bool
// strip out updated_at values.
$data = $this->doProtectFields($data);

// doProtectFields() can further remove elements from
// $data so we need to check for empty dataset again
if (empty($data))
{
throw DataException::forEmptyDataset('update');
}

if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data))
{
$data[$this->updatedField] = $this->setDate();
Expand Down
41 changes: 40 additions & 1 deletion tests/system/Models/InsertModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function testInsertBatchNewEntityWithDateTime(): void
$this->assertSame(2, $this->model->insertBatch([$entity, $entity]));
}

public function testInsertArrayWithDataException(): void
public function testInsertArrayWithNoDataException(): void
{
$this->expectException(DataException::class);
$this->expectExceptionMessage('There is no data to insert.');
Expand All @@ -193,6 +193,45 @@ public function testInsertObjectWithNoDataException(): void
$this->createModel(UserModel::class)->insert($data);
}

public function testInsertArrayWithNoDataExceptionNoAllowedData(): void
{
$this->expectException(DataException::class);
$this->expectExceptionMessage('There is no data to insert.');
$this->createModel(UserModel::class)->insert(['thisKeyIsNotAllowed' => 'Bar']);
}

public function testInsertEntityWithNoDataExceptionNoAllowedData(): void
{
$this->createModel(UserModel::class);

$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' => [],
];
};

$entity->fill(['thisKeyIsNotAllowed' => 'Bar']);

$this->expectException(DataException::class);
$this->expectExceptionMessage('There is no data to insert.');
$this->model->insert($entity);
}

public function testUseAutoIncrementSetToFalseInsertException(): void
{
$this->expectException(DataException::class);
Expand Down
67 changes: 63 additions & 4 deletions tests/system/Models/UpdateModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,26 @@ public function testUpdateObjectWithDataException(): void
$this->createModel(EventModel::class);

$data = (object) [
'name' => 'Foo',
'email' => 'foo@example.com',
'country' => 'US',
'deleted' => 0,
];
sfadschm marked this conversation as resolved.
Show resolved Hide resolved

$id = $this->model->insert($data);

$data = new stdClass();

$this->expectException(DataException::class);
$this->expectExceptionMessage('There is no data to update.');
$this->model->update($id, $data);
}

public function testUpdateArrayWithDataExceptionNoAllowedFields(): void
{
$this->createModel(EventModel::class);

$data = [
'name' => 'Foo',
'email' => 'foo@example.com',
'country' => 'US',
Expand All @@ -268,11 +288,50 @@ public function testUpdateObjectWithDataException(): void

$id = $this->model->insert($data);

$data = new stdClass();
$this->expectException(DataException::class);
$this->expectExceptionMessage('There is no data to update.');
$this->model->update($id, ['thisKeyIsNotAllowed' => 'Bar']);
}

public function testUpdateWithEntityNoAllowedFields(): void
{
$this->createModel(UserModel::class);

$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' => [],
];
};

$entity->id = 1;
$entity->name = 'Jones Martin';
$entity->country = 'India';
$entity->deleted = 0;

$id = $this->model->insert($entity);

$entity->syncOriginal();

$entity->fill(['thisKeyIsNotAllowed' => 'Bar']);

$this->expectException(DataException::class);
$this->expectExceptionMessage('There is no data to update.');
$this->model->update($id, $data);
$this->model->update($id, $entity);
}

public function testUseAutoIncrementSetToFalseUpdate(): void
Expand Down Expand Up @@ -301,9 +360,9 @@ public function testUpdateWithSetAndEscape(): void
$this->assertTrue($this->model->set('country', '2+2', false)->set('email', '1+1')->update(1, $userData));

$this->seeInDatabase('user', [
'name' => 'Scott',
'name' => 'Scott',
'country' => '4',
'email' => '1+1',
'email' => '1+1',
]);
}
}