Skip to content

Commit 1695369

Browse files
authored
Merge pull request #4195 from sfadschm/fix-model-empty-check
Add additional empty checks after field protection for update/insert.
2 parents 3d78c33 + 570951b commit 1695369

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

system/BaseModel.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,13 @@ public function insert($data = null, bool $returnID = true)
728728
// strip out created_at values.
729729
$data = $this->doProtectFields($data);
730730

731+
// doProtectFields() can further remove elements from
732+
// $data so we need to check for empty dataset again
733+
if (empty($data))
734+
{
735+
throw DataException::forEmptyDataset('insert');
736+
}
737+
731738
// Set created_at and updated_at with same time
732739
$date = $this->setDate();
733740

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

876+
// doProtectFields() can further remove elements from
877+
// $data so we need to check for empty dataset again
878+
if (empty($data))
879+
{
880+
throw DataException::forEmptyDataset('update');
881+
}
882+
869883
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data))
870884
{
871885
$data[$this->updatedField] = $this->setDate();

tests/system/Models/InsertModelTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function testInsertBatchNewEntityWithDateTime(): void
178178
$this->assertSame(2, $this->model->insertBatch([$entity, $entity]));
179179
}
180180

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

196+
public function testInsertArrayWithNoDataExceptionNoAllowedData(): void
197+
{
198+
$this->expectException(DataException::class);
199+
$this->expectExceptionMessage('There is no data to insert.');
200+
$this->createModel(UserModel::class)->insert(['thisKeyIsNotAllowed' => 'Bar']);
201+
}
202+
203+
public function testInsertEntityWithNoDataExceptionNoAllowedData(): void
204+
{
205+
$this->createModel(UserModel::class);
206+
207+
$entity = new class extends Entity
208+
{
209+
protected $id;
210+
protected $name;
211+
protected $email;
212+
protected $country;
213+
protected $deleted;
214+
protected $created_at;
215+
protected $updated_at;
216+
217+
protected $_options = [
218+
'datamap' => [],
219+
'dates' => [
220+
'created_at',
221+
'updated_at',
222+
'deleted_at',
223+
],
224+
'casts' => [],
225+
];
226+
};
227+
228+
$entity->fill(['thisKeyIsNotAllowed' => 'Bar']);
229+
230+
$this->expectException(DataException::class);
231+
$this->expectExceptionMessage('There is no data to insert.');
232+
$this->model->insert($entity);
233+
}
234+
196235
public function testUseAutoIncrementSetToFalseInsertException(): void
197236
{
198237
$this->expectException(DataException::class);

tests/system/Models/UpdateModelTest.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,65 @@ public function testUpdateObjectWithDataException(): void
275275
$this->model->update($id, $data);
276276
}
277277

278+
public function testUpdateArrayWithDataExceptionNoAllowedFields(): void
279+
{
280+
$this->createModel(EventModel::class);
281+
282+
$data = [
283+
'name' => 'Foo',
284+
'email' => 'foo@example.com',
285+
'country' => 'US',
286+
'deleted' => 0,
287+
];
288+
289+
$id = $this->model->insert($data);
290+
291+
$this->expectException(DataException::class);
292+
$this->expectExceptionMessage('There is no data to update.');
293+
$this->model->update($id, ['thisKeyIsNotAllowed' => 'Bar']);
294+
}
295+
296+
public function testUpdateWithEntityNoAllowedFields(): void
297+
{
298+
$this->createModel(UserModel::class);
299+
300+
$entity = new class extends Entity
301+
{
302+
protected $id;
303+
protected $name;
304+
protected $email;
305+
protected $country;
306+
protected $deleted;
307+
protected $created_at;
308+
protected $updated_at;
309+
310+
protected $_options = [
311+
'datamap' => [],
312+
'dates' => [
313+
'created_at',
314+
'updated_at',
315+
'deleted_at',
316+
],
317+
'casts' => [],
318+
];
319+
};
320+
321+
$entity->id = 1;
322+
$entity->name = 'Jones Martin';
323+
$entity->country = 'India';
324+
$entity->deleted = 0;
325+
326+
$id = $this->model->insert($entity);
327+
328+
$entity->syncOriginal();
329+
330+
$entity->fill(['thisKeyIsNotAllowed' => 'Bar']);
331+
332+
$this->expectException(DataException::class);
333+
$this->expectExceptionMessage('There is no data to update.');
334+
$this->model->update($id, $entity);
335+
}
336+
278337
public function testUseAutoIncrementSetToFalseUpdate(): void
279338
{
280339
$key = 'key';
@@ -301,9 +360,9 @@ public function testUpdateWithSetAndEscape(): void
301360
$this->assertTrue($this->model->set('country', '2+2', false)->set('email', '1+1')->update(1, $userData));
302361

303362
$this->seeInDatabase('user', [
304-
'name' => 'Scott',
363+
'name' => 'Scott',
305364
'country' => '4',
306-
'email' => '1+1',
365+
'email' => '1+1',
307366
]);
308367
}
309368
}

0 commit comments

Comments
 (0)