Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.
Merged
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
28 changes: 18 additions & 10 deletions Model/Behavior/SoftDeleteBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function beforeFind(Model $model, $query) {
* Check if a record exists for the given id
*
* @param Model $model
* @param id
* @param $id
* @return mixed
*/
public function existsAndNotDeleted(Model $model, $id) {
Expand All @@ -126,8 +126,8 @@ public function existsAndNotDeleted(Model $model, $id) {
* Before delete callback
*
* @param Model $model
* @param boolean $cascade
* @return boolean
* @param bool $cascade
* @return bool
*/
public function beforeDelete(Model $model, $cascade = true) {
$runtime = $this->runtime[$model->alias];
Expand All @@ -144,8 +144,8 @@ public function beforeDelete(Model $model, $cascade = true) {
* Mark record as deleted
*
* @param object $model
* @param integer $id
* @return boolean
* @param int $id
* @return bool
*/
public function delete($model, $id) {
$runtime = $this->runtime[$model->alias];
Expand Down Expand Up @@ -188,8 +188,8 @@ public function delete($model, $id) {
* Mark record as not deleted
*
* @param object $model
* @param integer $id
* @return boolean
* @param int $id
* @return bool
*/
public function undelete($model, $id) {
$runtime = $this->runtime[$model->alias];
Expand Down Expand Up @@ -248,7 +248,7 @@ public function softDelete($model, $active) {
*
* @param object $model
* @param mixed $expiration anything parseable by strtotime(), by default '-90 days'
* @return integer
* @return int
*/
public function purgeDeletedCount($model, $expiration = '-90 days') {
$runtime = $this->runtime[$model->alias];
Expand All @@ -268,7 +268,7 @@ public function purgeDeletedCount($model, $expiration = '-90 days') {
*
* @param object $model
* @param mixed $expiration anything parseable by strtotime(), by default '-90 days'
* @return boolean if there were some outdated records
* @return bool if there were some outdated records
*/
public function purgeDeleted($model, $expiration = '-90 days') {
$this->softDelete($model, false);
Expand Down Expand Up @@ -335,6 +335,7 @@ protected function _normalizeFields($model, $settings = array()) {
*
* @param Model $model
* @param mixed $active
* @return void
*/
protected function _softDeleteAssociations(Model $model, $active) {
if (empty($model->belongsTo)) {
Expand All @@ -345,6 +346,13 @@ protected function _softDeleteAssociations(Model $model, $active) {
$parentModels = array_keys($model->belongsTo);

foreach ($parentModels as $parentModel) {
list($plugin, $modelClass) = pluginSplit($parentModel, true);
App::uses($modelClass, $plugin . 'Model');
if (!class_exists($modelClass)) {
throw new MissingModelException(array('class' => $modelClass));
}
$model->{$parentModel} = new $parentModel(null, null, $model->useDbConfig);

foreach (array('hasOne', 'hasMany') as $assocType) {
if (empty($model->{$parentModel}->{$assocType})) {
continue;
Expand Down Expand Up @@ -407,4 +415,4 @@ public function softDeleteAll(Model $model, $conditions = array()) {
$this->delete($model, $result[$model->alias][$model->primaryKey]);
}
}
}
}
62 changes: 60 additions & 2 deletions Test/Case/Model/Behavior/SoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ class SoftDeletedPost extends CakeTestModel {
public $alias = 'Post';
}

/**
* SoftDeletedPost
*
* @package utils
* @subpackage utils.tests.cases.behaviors
*/
class SoftDeletedArticle extends CakeTestModel {

/**
* Use Table
*
* @var string
*/
public $useTable = 'articles';

/**
* Behaviors
*
* @var array
*/
public $actsAs = array('Utils.SoftDelete');

/**
* Alias
*
* @var string
*/
public $alias = 'Article';
}

/**
* SoftDelete Test case
*/
Expand All @@ -61,7 +91,10 @@ class SoftDeleteTest extends CakeTestCase {
*
* @var array
*/
public $fixtures = array('plugin.utils.post');
public $fixtures = array(
'plugin.utils.post',
'plugin.utils.article'
);

/**
* Creates the model instance
Expand All @@ -70,6 +103,7 @@ class SoftDeleteTest extends CakeTestCase {
*/
public function setUp() {
$this->Post = new SoftDeletedPost();
$this->Article = new SoftDeletedArticle();
}

/**
Expand Down Expand Up @@ -236,4 +270,28 @@ public function testSoftDeleteAll() {
$this->assertEquals($result, $expected);
}

}
public function testModelHasManyRelation() {
$this->Post->bindModel(array(
'belongsTo' => array(
'Article' => array(
'foreignKey' => 'article_id'
)
)
));

$this->Article->bindModel(array(
'hasMany' => array(
'Post' => array(
'foreignKey' => 'article_id'
)
)
));

$result = $this->Post->delete(1);
$this->Post->Behaviors->unload('SoftDelete');
$this->assertFalse($result);
$data = $this->Post->read(null, 1);
$this->assertEquals($data['Post']['deleted'], true);
$this->assertTrue(!empty($data['Post']['deleted_date']));
}
}
9 changes: 5 additions & 4 deletions Test/Fixture/ArticleFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ArticleFixture extends CakeTestFixture {
'slug' => array('type' => 'string', 'null' => true),
'tiny_slug' => array('type' => 'string', 'null' => true),
'position' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 10),
'deleted' => array('type' => 'boolean', 'null' => false, 'default' => '0'),
'deleted_date' => 'datetime',
'created' => 'datetime',
'updated' => 'datetime');

Expand All @@ -34,9 +36,8 @@ class ArticleFixture extends CakeTestFixture {
* @var array
*/
public $records = array(
array('id' => 1, 'title' => 'First Article', 'slug' => 'first_article', 'tiny_slug' => '0', 'position' => 1, 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
array('id' => 2, 'title' => 'Second Article', 'slug' => 'second_article', 'tiny_slug' => '1', 'position' => 2, 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
array('id' => 3, 'title' => 'Third Article', 'slug' => 'third_article', 'tiny_slug' => '2', 'position' => 3, 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')
array('id' => 1, 'title' => 'First Article', 'slug' => 'first_article', 'tiny_slug' => '0', 'position' => 1, 'deleted' => 0, 'deleted_date' => null, 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
array('id' => 2, 'title' => 'Second Article', 'slug' => 'second_article', 'tiny_slug' => '1', 'position' => 2, 'deleted' => 0, 'deleted_date' => null, 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
array('id' => 3, 'title' => 'Third Article', 'slug' => 'third_article', 'tiny_slug' => '2', 'position' => 3, 'deleted' => 0, 'deleted_date' => null, 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')
);

}
8 changes: 4 additions & 4 deletions Test/Fixture/AssetFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class AssetFixture extends CakeTestFixture {
);

public $records = array(
array('id' => 1, 'title'=> 'soccuer image', 'description'=> 'amazing shot...'),
array('id' => 2, 'title'=> 'animal image', 'description'=> 'very disturbing'),
array('id' => 11, 'title'=> 'home page link', 'description' => 'link back to home page'),
array('id' => 12, 'title'=> 'google', 'description' => 'Google is the search engine'),
array('id' => 1, 'title' => 'soccuer image', 'description' => 'amazing shot...'),
array('id' => 2, 'title' => 'animal image', 'description' => 'very disturbing'),
array('id' => 11, 'title' => 'home page link', 'description' => 'link back to home page'),
array('id' => 12, 'title' => 'google', 'description' => 'Google is the search engine'),
);

}
47 changes: 23 additions & 24 deletions Test/Fixture/BArticleFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,46 @@ class BArticleFixture extends CakeTestFixture {
public $fields = array(
'id' => array('type' => 'integer', 'null' => false, 'length' => 11, 'key' => 'primary'),
'title' => array('type' => 'string', 'null' => false),
'parent_id' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 36),
'lft' => array('type' => 'integer', 'null' => false, 'default' => NULL),
'rght' => array('type' => 'integer', 'null' => false, 'default' => NULL),
'parent_id' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 36),
'lft' => array('type' => 'integer', 'null' => false, 'default' => null),
'rght' => array('type' => 'integer', 'null' => false, 'default' => null),
'created' => 'datetime',
'modified' => 'datetime');

public $records = array(
array(
'id' => 1,
'title' => 'First article',
'parent_id' => NULL,
'parent_id' => null,
'lft' => 65537,
'rght' => 65542,
'created' => '2010-02-03 16:44:34',
'modified' => '2010-02-03 16:44:34',
),
array(
'id' => 2,
'title' => 'First article - child 1',
'parent_id' => 1,
'lft' => 65538,
'rght' => 65541,
'created' => '2010-02-03 17:07:06',
'modified' => '2010-02-03 17:07:06',
),
array(
'id' => 3,
'title' => 'First article - child 1 - subchild 1',
'parent_id' => 2,
'lft' => 65539,
'rght' => 65540,
'created' => '2010-02-03 17:42:27',
'modified' => '2010-02-03 17:42:27'),
array(
'id' => 2,
'title' => 'First article - child 1',
'parent_id' => 1,
'lft' => 65538,
'rght' => 65541,
'created' => '2010-02-03 17:07:06',
'modified' => '2010-02-03 17:07:06',
),
array(
'id' => 3,
'title' => 'First article - child 1 - subchild 1',
'parent_id' => 2,
'lft' => 65539,
'rght' => 65540,
'created' => '2010-02-03 17:42:27',
'modified' => '2010-02-03 17:42:27'),
array(
'id' => 4, 'title' => 'Second article',
'parent_id' => NULL,
'parent_id' => null,
'lft' => 131073,
'rght' => 131074,
'created' => '2010-02-03 17:46:47',
'modified' => '2010-02-03 17:46:47')
);

}
?>
}
3 changes: 1 addition & 2 deletions Test/Fixture/BinaryArticleFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ class BinaryArticleFixture extends CakeTestFixture {
array('id' => 5, 'parent_id' => 3, 'title' => 'Forth Article', 'position' => 256, 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
array('id' => 6, 'parent_id' => 3, 'title' => 'Fifth Article', 'position' => 512, 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
);
}
?>
}
36 changes: 20 additions & 16 deletions Test/Fixture/CommentFixture.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<?php
// For the inheritable behavior
/**
* Short description for class.
*
* @package cake
* @subpackage cake.tests.fixtures
*/
class CommentFixture extends CakeTestFixture {
var $name = 'Comment';

var $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'content_id' => array('type' => 'integer', 'null' => false),
'body' => 'text',
'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
'permalink' => array('type' => 'string'),
'parent_id' => array('type' => 'integer'),
'created' => 'datetime',
'updated' => 'datetime'
);

}
?>

public $name = 'Comment';

public $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'content_id' => array('type' => 'integer', 'null' => false),
'body' => 'text',
'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
'permalink' => array('type' => 'string'),
'parent_id' => array('type' => 'integer'),
'created' => 'datetime',
'updated' => 'datetime'
);
}
21 changes: 13 additions & 8 deletions Test/Fixture/ContentFixture.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php
// For the inheritable behavior
/**
* Short description for class.
*
* @package cake
* @subpackage cake.tests.fixtures
*/
class ContentFixture extends CakeTestFixture {

public $name = 'Content';

public $fields = array(
Expand All @@ -19,16 +25,15 @@ class ContentFixture extends CakeTestFixture {
public $records = array(

/* Articles */
array('id' => 1, 'parent_id' => 0, 'type'=>'Article', 'title'=> 'Unearthed rare monster in london', 'body'=> 'very strange discovery...', 'permalink'=> 'unearthed-rare-monster-in-london'),
array('id' => 2, 'parent_id' => 0, 'type'=>'Article', 'title'=> 'about us', 'body'=> 'history of our company', 'permalink'=> 'about-us'),
array('id' => 1, 'parent_id' => 0, 'type' => 'Article', 'title' => 'Unearthed rare monster in london', 'body' => 'very strange discovery...', 'permalink' => 'unearthed-rare-monster-in-london'),
array('id' => 2, 'parent_id' => 0, 'type' => 'Article', 'title' => 'about us', 'body' => 'history of our company', 'permalink' => 'about-us'),


/* Pages */
array('id' => 100, 'parent_id' => 0, 'type' => 'Page', 'title' => 'Home page', 'body'=>'welcome to my site', 'permalink'=>''),
array('id' => 101, 'parent_id' => 100, 'type'=>'Page', 'title'=> 'Frequent Asked Questions', 'body'=> 'questions and more...', 'permalink'=> 'faq'),
array('id' => 102, 'parent_id' => 101, 'type'=>'Page', 'title'=> 'about us', 'body'=> 'CakePHP is a MVC PHP framework that aids development of... ', 'permalink'=> 'about-us'),
array('id' => 100, 'parent_id' => 0, 'type' => 'Page', 'title' => 'Home page', 'body' => 'welcome to my site', 'permalink' => ''),
array('id' => 101, 'parent_id' => 100, 'type' => 'Page', 'title' => 'Frequent Asked Questions', 'body' => 'questions and more...', 'permalink' => 'faq'),
array('id' => 102, 'parent_id' => 101, 'type' => 'Page', 'title' => 'about us', 'body' => 'CakePHP is a MVC PHP framework that aids development of... ', 'permalink' => 'about-us'),

);

}
?>
}
4 changes: 2 additions & 2 deletions Test/Fixture/ImageFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class ImageFixture extends CakeTestFixture {
* @var array
*/
public $records = array(
array('id' => 1, 'file_name'=> 'soccer_worldcup.jpg', 'file_size' =>' 53422', 'content_type' => 'image/jpeg'),
array('id' => 2, 'file_name'=> 'dog.png', 'file_size'=>'431234', 'content_type'=>'image/png')
array('id' => 1, 'file_name' => 'soccer_worldcup.jpg', 'file_size' => ' 53422', 'content_type' => 'image/jpeg'),
array('id' => 2, 'file_name' => 'dog.png', 'file_size' => '431234', 'content_type' => 'image/png')
);

}
Loading