Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 12 additions & 5 deletions core/MY_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,23 @@ public function update_all($data)
*/
public function delete($id)
{
$this->trigger('before_delete', $id);

$this->_database->where($this->primary_key, $id);

if ($this->soft_delete)
{
$result = $this->_database->update($this->_table, array( $this->soft_delete_key => TRUE ));
$data = $this->as_array()
->get($id);

$data = $this->trigger('before_delete', $data);

$data = array_merge($data, array($this->soft_delete_key => TRUE));

$result = $this->update($id, $data);
}
else
{
$this->trigger('before_delete', $id);

$this->_database->where($this->primary_key, $id);

$result = $this->_database->delete($this->_table);
}

Expand Down
74 changes: 62 additions & 12 deletions tests/MY_Model_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,14 +557,39 @@ public function test_soft_delete()
$this->model = new Soft_delete_model();
$this->model->_database = $this->getMock('MY_Model_Mock_DB');

$this->model->_database->expects($this->at(0))
->method('where')
->with($this->equalTo('deleted'), FALSE);

$this->model->_database->expects($this->at(1))
->method('where')
->with('id', 2)
->will($this->returnValue($this->model->_database));

$fake_data = array('fake_record_here' => 'fake data');

$this->model->_database->expects($this->once())
->method('where')
->with($this->equalTo('id'), $this->equalTo(2))
->will($this->returnValue($this->model->_database));
->method('row_array')
->will($this->returnValue($fake_data));

$this->_expect_get();

$this->model->_database->expects($this->at(4))
->method('where')
->with('id', 2)
->will($this->returnValue($this->model->_database));

$fake_data = array_merge($fake_data, array('deleted' => TRUE));

$this->model->_database->expects($this->once())
->method('update')
->with($this->equalTo('records'), $this->equalTo(array( 'deleted' => TRUE )))
->will($this->returnValue(TRUE));
->method('set')
->with($fake_data)
->will($this->returnValue($this->model->_database));

$this->model->_database->expects($this->once())
->method('update')
->with('records')
->will($this->returnValue(TRUE));

$this->assertEquals($this->model->delete(2), TRUE);
}
Expand All @@ -573,15 +598,40 @@ public function test_soft_delete_custom_key()
{
$this->model = new Soft_delete_model('record_deleted');
$this->model->_database = $this->getMock('MY_Model_Mock_DB');

$this->model->_database->expects($this->at(0))
->method('where')
->with($this->equalTo('record_deleted'), FALSE);

$this->model->_database->expects($this->at(1))
->method('where')
->with('id', 2)
->will($this->returnValue($this->model->_database));

$fake_data = array('fake_record_here' => 'fake data');

$this->model->_database->expects($this->once())
->method('where')
->with($this->equalTo('id'), $this->equalTo(2))
->will($this->returnValue($this->model->_database));
->method('row_array')
->will($this->returnValue($fake_data));

$this->_expect_get();

$this->model->_database->expects($this->at(4))
->method('where')
->with('id', 2)
->will($this->returnValue($this->model->_database));

$fake_data = array_merge($fake_data, array('record_deleted' => TRUE));

$this->model->_database->expects($this->once())
->method('update')
->with($this->equalTo('records'), $this->equalTo(array( 'record_deleted' => TRUE )))
->will($this->returnValue(TRUE));
->method('set')
->with($fake_data)
->will($this->returnValue($this->model->_database));

$this->model->_database->expects($this->once())
->method('update')
->with('records')
->will($this->returnValue(TRUE));

$this->assertEquals($this->model->delete(2), TRUE);
}
Expand Down