Skip to content

Commit 8a7a08f

Browse files
committed
Tests for prevent soft-delete all records when no conditions were set
1 parent 7e6e293 commit 8a7a08f

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

tests/system/Database/Live/ModelTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,63 @@ public function testOnlyDeleted()
486486

487487
$this->assertCount(1, $users);
488488
}
489+
/**
490+
* If where condition is set, beyond the value was empty (0,'', NULL, etc.),
491+
* Exception should not be thrown because condition was explicity set
492+
*
493+
* @dataProvider emptyPkValues
494+
* @return void
495+
*/
496+
public function testDontThrowExceptionWhenSoftDeleteConditionIsSetWithEmptyValue($emptyValue)
497+
{
498+
$model = new UserModel();
499+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
500+
$model->where('id', $emptyValue)->delete();
501+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
502+
unset($model);
503+
} //--------------------------------------------------------------------
504+
505+
/**
506+
* @expectedException \CodeIgniter\Database\Exceptions\DatabaseException
507+
* @expectedExceptionMessage Deletes are not allowed unless they contain a "where" or "like" clause.
508+
* @dataProvider emptyPkValues
509+
* @return void
510+
*/
511+
public function testThrowExceptionWhenSoftDeleteParamIsEmptyValue($emptyValue)
512+
{
513+
$model = new UserModel();
514+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
515+
$model->delete($emptyValue);
516+
}
489517

490518
//--------------------------------------------------------------------
491519

520+
/**
521+
* @expectedException \CodeIgniter\Database\Exceptions\DatabaseException
522+
* @expectedExceptionMessage Deletes are not allowed unless they contain a "where" or "like" clause.
523+
* @dataProvider emptyPkValues
524+
* @return void
525+
*/
526+
public function testDontDeleteRowsWhenSoftDeleteParamIsEmpty($emptyValue)
527+
{
528+
$model = new UserModel();
529+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
530+
$model->delete($emptyValue);
531+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
532+
unset($model);
533+
}
534+
535+
public function emptyPkValues()
536+
{
537+
return [
538+
[0],
539+
[null],
540+
['0'],
541+
[false],
542+
];
543+
}
544+
//--------------------------------------------------------------------
545+
492546
public function testChunk()
493547
{
494548
$model = new UserModel();

0 commit comments

Comments
 (0)