diff --git a/Model/Behavior/SoftDeleteBehavior.php b/Model/Behavior/SoftDeleteBehavior.php index afb655c..b2fa860 100644 --- a/Model/Behavior/SoftDeleteBehavior.php +++ b/Model/Behavior/SoftDeleteBehavior.php @@ -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) { @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -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); @@ -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)) { @@ -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; @@ -407,4 +415,4 @@ public function softDeleteAll(Model $model, $conditions = array()) { $this->delete($model, $result[$model->alias][$model->primaryKey]); } } -} +} \ No newline at end of file diff --git a/Test/Case/Model/Behavior/SoftDeleteTest.php b/Test/Case/Model/Behavior/SoftDeleteTest.php index fc5ed1d..c72ac05 100644 --- a/Test/Case/Model/Behavior/SoftDeleteTest.php +++ b/Test/Case/Model/Behavior/SoftDeleteTest.php @@ -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 */ @@ -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 @@ -70,6 +103,7 @@ class SoftDeleteTest extends CakeTestCase { */ public function setUp() { $this->Post = new SoftDeletedPost(); + $this->Article = new SoftDeletedArticle(); } /** @@ -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'])); + } +} \ No newline at end of file diff --git a/Test/Fixture/ArticleFixture.php b/Test/Fixture/ArticleFixture.php index 095d03e..71c51ea 100644 --- a/Test/Fixture/ArticleFixture.php +++ b/Test/Fixture/ArticleFixture.php @@ -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'); @@ -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') ); - } \ No newline at end of file diff --git a/Test/Fixture/AssetFixture.php b/Test/Fixture/AssetFixture.php index 0948f7a..c6dba07 100644 --- a/Test/Fixture/AssetFixture.php +++ b/Test/Fixture/AssetFixture.php @@ -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'), ); } \ No newline at end of file diff --git a/Test/Fixture/BArticleFixture.php b/Test/Fixture/BArticleFixture.php index 464fa2b..2708c96 100644 --- a/Test/Fixture/BArticleFixture.php +++ b/Test/Fixture/BArticleFixture.php @@ -24,9 +24,9 @@ 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'); @@ -34,37 +34,36 @@ class BArticleFixture extends CakeTestFixture { 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') ); -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/Test/Fixture/BinaryArticleFixture.php b/Test/Fixture/BinaryArticleFixture.php index 755f117..e9fb76d 100644 --- a/Test/Fixture/BinaryArticleFixture.php +++ b/Test/Fixture/BinaryArticleFixture.php @@ -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'), ); -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/Test/Fixture/CommentFixture.php b/Test/Fixture/CommentFixture.php index 08eb64c..ba06cc3 100755 --- a/Test/Fixture/CommentFixture.php +++ b/Test/Fixture/CommentFixture.php @@ -1,18 +1,22 @@ 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' - ); - -} -?> \ No newline at end of file + + 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' + ); +} \ No newline at end of file diff --git a/Test/Fixture/ContentFixture.php b/Test/Fixture/ContentFixture.php index 169a5b5..9c5855d 100644 --- a/Test/Fixture/ContentFixture.php +++ b/Test/Fixture/ContentFixture.php @@ -1,6 +1,12 @@ 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'), ); -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/Test/Fixture/ImageFixture.php b/Test/Fixture/ImageFixture.php index bd7320e..ecfdd94 100644 --- a/Test/Fixture/ImageFixture.php +++ b/Test/Fixture/ImageFixture.php @@ -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') ); } \ No newline at end of file diff --git a/Test/Fixture/LinkFixture.php b/Test/Fixture/LinkFixture.php index e7a68fb..4f41973 100644 --- a/Test/Fixture/LinkFixture.php +++ b/Test/Fixture/LinkFixture.php @@ -1,6 +1,13 @@ 11, 'url'=> 'http://cakephp.org'), - array('id' => 12, 'url'=> 'http://google.com'), - + array('id' => 11, 'url' => 'http://cakephp.org'), + array('id' => 12, 'url' => 'http://google.com'), ); - } \ No newline at end of file diff --git a/Test/Fixture/PostFixture.php b/Test/Fixture/PostFixture.php index 6b2e0c9..4fd4d13 100644 --- a/Test/Fixture/PostFixture.php +++ b/Test/Fixture/PostFixture.php @@ -26,7 +26,7 @@ class PostFixture extends CakeTestFixture { 'article_id' => array('type' => 'integer'), 'title' => array('type' => 'string', 'null' => false), 'deleted' => array('type' => 'boolean', 'null' => false, 'default' => '0'), - 'deleted_date' => 'datetime', + 'deleted_date' => 'datetime', 'created' => 'datetime', 'updated' => 'datetime'); @@ -62,6 +62,4 @@ class PostFixture extends CakeTestFixture { 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')); -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/Test/Fixture/ProductFixture.php b/Test/Fixture/ProductFixture.php index 280508e..8ab4a07 100644 --- a/Test/Fixture/ProductFixture.php +++ b/Test/Fixture/ProductFixture.php @@ -5,6 +5,7 @@ * @package cake * @subpackage cake.tests.fixtures */ + class ProductFixture extends CakeTestFixture { /** @@ -24,10 +25,9 @@ class ProductFixture extends CakeTestFixture { ); public $records = array( - array('id' => 1, 'name'=> 'Foot Ball DVD', 'description' =>'The best footie matches ever', 'published' => 0, 'publish_date' => null), - array('id' => 2, 'name'=> 'Cook like Jamie DVD', 'description' =>'Learn to cook like Jamie Oliver', 'published' => 1, 'publish_date' => null), - array('id' => 3, 'name'=> 'Utimte Fishing', 'description' =>'Where to Fish in the UK', 'published' => 0, 'publish_date' => '2004-06-19 21:05:31'), - array('id' => 4, 'name'=> 'Nigella from the Heart', 'description' =>'Nigella Eat your heart out', 'published' => 1, 'publish_date' => '2005-12-29 08:00:43'), + array('id' => 1, 'name' => 'Foot Ball DVD', 'description' => 'The best footie matches ever', 'published' => 0, 'publish_date' => null), + array('id' => 2, 'name' => 'Cook like Jamie DVD', 'description' => 'Learn to cook like Jamie Oliver', 'published' => 1, 'publish_date' => null), + array('id' => 3, 'name' => 'Utimte Fishing', 'description' => 'Where to Fish in the UK', 'published' => 0, 'publish_date' => '2004-06-19 21:05:31'), + array('id' => 4, 'name' => 'Nigella from the Heart', 'description' => 'Nigella Eat your heart out', 'published' => 1, 'publish_date' => '2005-12-29 08:00:43'), ); } -?> \ No newline at end of file diff --git a/Test/Fixture/UsersAddonFixture.php b/Test/Fixture/UsersAddonFixture.php index b05acbc..99dc4d5 100644 --- a/Test/Fixture/UsersAddonFixture.php +++ b/Test/Fixture/UsersAddonFixture.php @@ -1,5 +1,6 @@ array('type'=>'string', 'null' => false, 'length' => 36, 'key' => 'primary'), - 'addon_id' => array('type'=>'string', 'null' => false, 'length' => 36, 'key' => 'index'), - 'user_id' => array('type'=>'string', 'null' => false, 'length' => 36), - 'position' => array('type'=>'float', 'null' => false, 'default' => '1', 'length' => 4), - 'active' => array('type'=>'boolean', 'null' => false, 'default' => '0'), - 'created' => array('type'=>'datetime', 'null' => true, 'default' => NULL), - 'modified' => array('type'=>'datetime', 'null' => true, 'default' => NULL), + 'id' => array('type' => 'string', 'null' => false, 'length' => 36, 'key' => 'primary'), + 'addon_id' => array('type' => 'string', 'null' => false, 'length' => 36, 'key' => 'index'), + 'user_id' => array('type' => 'string', 'null' => false, 'length' => 36), + 'position' => array('type' => 'float', 'null' => false, 'default' => '1', 'length' => 4), + 'active' => array('type' => 'boolean', 'null' => false, 'default' => '0'), + 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'modified' => array('type' => 'datetime', 'null' => true, 'default' => null), 'indexes' => array( - 'PRIMARY' => array('column' => 'id', 'unique' => 1), + 'PRIMARY' => array('column' => 'id', 'unique' => 1), 'UNIQUE_ADDON_USER' => array('column' => array('addon_id', 'user_id'), 'unique' => 1)) ); + /** * Records * @@ -63,4 +67,4 @@ class UsersAddonFixture extends CakeTestFixture { 'active' => 1, 'created' => '2008-03-25 01:35:35', 'modified' => '2008-03-25 01:35:35')); -} +} \ No newline at end of file