Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Commit fd69d2b

Browse files
author
Cake Development Corporation
committed
Merge pull request #139 from omnuvito/issue/138
Issue #138 Fixing SoftDeleteBehavior belongsTo when import users on fixture
2 parents 4e729c1 + d1fdbf7 commit fd69d2b

File tree

13 files changed

+176
-96
lines changed

13 files changed

+176
-96
lines changed

Model/Behavior/SoftDeleteBehavior.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function beforeFind(Model $model, $query) {
102102
* Check if a record exists for the given id
103103
*
104104
* @param Model $model
105-
* @param id
105+
* @param $id
106106
* @return mixed
107107
*/
108108
public function existsAndNotDeleted(Model $model, $id) {
@@ -126,8 +126,8 @@ public function existsAndNotDeleted(Model $model, $id) {
126126
* Before delete callback
127127
*
128128
* @param Model $model
129-
* @param boolean $cascade
130-
* @return boolean
129+
* @param bool $cascade
130+
* @return bool
131131
*/
132132
public function beforeDelete(Model $model, $cascade = true) {
133133
$runtime = $this->runtime[$model->alias];
@@ -144,8 +144,8 @@ public function beforeDelete(Model $model, $cascade = true) {
144144
* Mark record as deleted
145145
*
146146
* @param object $model
147-
* @param integer $id
148-
* @return boolean
147+
* @param int $id
148+
* @return bool
149149
*/
150150
public function delete($model, $id) {
151151
$runtime = $this->runtime[$model->alias];
@@ -188,8 +188,8 @@ public function delete($model, $id) {
188188
* Mark record as not deleted
189189
*
190190
* @param object $model
191-
* @param integer $id
192-
* @return boolean
191+
* @param int $id
192+
* @return bool
193193
*/
194194
public function undelete($model, $id) {
195195
$runtime = $this->runtime[$model->alias];
@@ -248,7 +248,7 @@ public function softDelete($model, $active) {
248248
*
249249
* @param object $model
250250
* @param mixed $expiration anything parseable by strtotime(), by default '-90 days'
251-
* @return integer
251+
* @return int
252252
*/
253253
public function purgeDeletedCount($model, $expiration = '-90 days') {
254254
$runtime = $this->runtime[$model->alias];
@@ -268,7 +268,7 @@ public function purgeDeletedCount($model, $expiration = '-90 days') {
268268
*
269269
* @param object $model
270270
* @param mixed $expiration anything parseable by strtotime(), by default '-90 days'
271-
* @return boolean if there were some outdated records
271+
* @return bool if there were some outdated records
272272
*/
273273
public function purgeDeleted($model, $expiration = '-90 days') {
274274
$this->softDelete($model, false);
@@ -335,6 +335,7 @@ protected function _normalizeFields($model, $settings = array()) {
335335
*
336336
* @param Model $model
337337
* @param mixed $active
338+
* @return void
338339
*/
339340
protected function _softDeleteAssociations(Model $model, $active) {
340341
if (empty($model->belongsTo)) {
@@ -345,6 +346,13 @@ protected function _softDeleteAssociations(Model $model, $active) {
345346
$parentModels = array_keys($model->belongsTo);
346347

347348
foreach ($parentModels as $parentModel) {
349+
list($plugin, $modelClass) = pluginSplit($parentModel, true);
350+
App::uses($modelClass, $plugin . 'Model');
351+
if (!class_exists($modelClass)) {
352+
throw new MissingModelException(array('class' => $modelClass));
353+
}
354+
$model->{$parentModel} = new $parentModel(null, null, $model->useDbConfig);
355+
348356
foreach (array('hasOne', 'hasMany') as $assocType) {
349357
if (empty($model->{$parentModel}->{$assocType})) {
350358
continue;
@@ -407,4 +415,4 @@ public function softDeleteAll(Model $model, $conditions = array()) {
407415
$this->delete($model, $result[$model->alias][$model->primaryKey]);
408416
}
409417
}
410-
}
418+
}

Test/Case/Model/Behavior/SoftDeleteTest.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,36 @@ class SoftDeletedPost extends CakeTestModel {
5151
public $alias = 'Post';
5252
}
5353

54+
/**
55+
* SoftDeletedPost
56+
*
57+
* @package utils
58+
* @subpackage utils.tests.cases.behaviors
59+
*/
60+
class SoftDeletedArticle extends CakeTestModel {
61+
62+
/**
63+
* Use Table
64+
*
65+
* @var string
66+
*/
67+
public $useTable = 'articles';
68+
69+
/**
70+
* Behaviors
71+
*
72+
* @var array
73+
*/
74+
public $actsAs = array('Utils.SoftDelete');
75+
76+
/**
77+
* Alias
78+
*
79+
* @var string
80+
*/
81+
public $alias = 'Article';
82+
}
83+
5484
/**
5585
* SoftDelete Test case
5686
*/
@@ -61,7 +91,10 @@ class SoftDeleteTest extends CakeTestCase {
6191
*
6292
* @var array
6393
*/
64-
public $fixtures = array('plugin.utils.post');
94+
public $fixtures = array(
95+
'plugin.utils.post',
96+
'plugin.utils.article'
97+
);
6598

6699
/**
67100
* Creates the model instance
@@ -70,6 +103,7 @@ class SoftDeleteTest extends CakeTestCase {
70103
*/
71104
public function setUp() {
72105
$this->Post = new SoftDeletedPost();
106+
$this->Article = new SoftDeletedArticle();
73107
}
74108

75109
/**
@@ -236,4 +270,28 @@ public function testSoftDeleteAll() {
236270
$this->assertEquals($result, $expected);
237271
}
238272

239-
}
273+
public function testModelHasManyRelation() {
274+
$this->Post->bindModel(array(
275+
'belongsTo' => array(
276+
'Article' => array(
277+
'foreignKey' => 'article_id'
278+
)
279+
)
280+
));
281+
282+
$this->Article->bindModel(array(
283+
'hasMany' => array(
284+
'Post' => array(
285+
'foreignKey' => 'article_id'
286+
)
287+
)
288+
));
289+
290+
$result = $this->Post->delete(1);
291+
$this->Post->Behaviors->unload('SoftDelete');
292+
$this->assertFalse($result);
293+
$data = $this->Post->read(null, 1);
294+
$this->assertEquals($data['Post']['deleted'], true);
295+
$this->assertTrue(!empty($data['Post']['deleted_date']));
296+
}
297+
}

Test/Fixture/ArticleFixture.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class ArticleFixture extends CakeTestFixture {
2525
'slug' => array('type' => 'string', 'null' => true),
2626
'tiny_slug' => array('type' => 'string', 'null' => true),
2727
'position' => array('type' => 'integer', 'null' => false, 'default' => '0', 'length' => 10),
28+
'deleted' => array('type' => 'boolean', 'null' => false, 'default' => '0'),
29+
'deleted_date' => 'datetime',
2830
'created' => 'datetime',
2931
'updated' => 'datetime');
3032

@@ -34,9 +36,8 @@ class ArticleFixture extends CakeTestFixture {
3436
* @var array
3537
*/
3638
public $records = array(
37-
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'),
38-
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'),
39-
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')
39+
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'),
40+
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'),
41+
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')
4042
);
41-
4243
}

Test/Fixture/AssetFixture.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class AssetFixture extends CakeTestFixture {
3030
);
3131

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

3939
}

Test/Fixture/BArticleFixture.php

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,46 @@ class BArticleFixture extends CakeTestFixture {
2424
public $fields = array(
2525
'id' => array('type' => 'integer', 'null' => false, 'length' => 11, 'key' => 'primary'),
2626
'title' => array('type' => 'string', 'null' => false),
27-
'parent_id' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 36),
28-
'lft' => array('type' => 'integer', 'null' => false, 'default' => NULL),
29-
'rght' => array('type' => 'integer', 'null' => false, 'default' => NULL),
27+
'parent_id' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 36),
28+
'lft' => array('type' => 'integer', 'null' => false, 'default' => null),
29+
'rght' => array('type' => 'integer', 'null' => false, 'default' => null),
3030
'created' => 'datetime',
3131
'modified' => 'datetime');
3232

3333
public $records = array(
3434
array(
3535
'id' => 1,
3636
'title' => 'First article',
37-
'parent_id' => NULL,
37+
'parent_id' => null,
3838
'lft' => 65537,
3939
'rght' => 65542,
4040
'created' => '2010-02-03 16:44:34',
4141
'modified' => '2010-02-03 16:44:34',
4242
),
43-
array(
44-
'id' => 2,
45-
'title' => 'First article - child 1',
46-
'parent_id' => 1,
47-
'lft' => 65538,
48-
'rght' => 65541,
49-
'created' => '2010-02-03 17:07:06',
50-
'modified' => '2010-02-03 17:07:06',
51-
),
52-
array(
53-
'id' => 3,
54-
'title' => 'First article - child 1 - subchild 1',
55-
'parent_id' => 2,
56-
'lft' => 65539,
57-
'rght' => 65540,
58-
'created' => '2010-02-03 17:42:27',
59-
'modified' => '2010-02-03 17:42:27'),
43+
array(
44+
'id' => 2,
45+
'title' => 'First article - child 1',
46+
'parent_id' => 1,
47+
'lft' => 65538,
48+
'rght' => 65541,
49+
'created' => '2010-02-03 17:07:06',
50+
'modified' => '2010-02-03 17:07:06',
51+
),
52+
array(
53+
'id' => 3,
54+
'title' => 'First article - child 1 - subchild 1',
55+
'parent_id' => 2,
56+
'lft' => 65539,
57+
'rght' => 65540,
58+
'created' => '2010-02-03 17:42:27',
59+
'modified' => '2010-02-03 17:42:27'),
6060
array(
6161
'id' => 4, 'title' => 'Second article',
62-
'parent_id' => NULL,
62+
'parent_id' => null,
6363
'lft' => 131073,
6464
'rght' => 131074,
6565
'created' => '2010-02-03 17:46:47',
6666
'modified' => '2010-02-03 17:46:47')
6767
);
6868

69-
}
70-
?>
69+
}

Test/Fixture/BinaryArticleFixture.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,4 @@ class BinaryArticleFixture extends CakeTestFixture {
4242
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'),
4343
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'),
4444
);
45-
}
46-
?>
45+
}

Test/Fixture/CommentFixture.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<?php
2-
// For the inheritable behavior
2+
/**
3+
* Short description for class.
4+
*
5+
* @package cake
6+
* @subpackage cake.tests.fixtures
7+
*/
38
class CommentFixture extends CakeTestFixture {
4-
var $name = 'Comment';
5-
6-
var $fields = array(
7-
'id' => array('type' => 'integer', 'key' => 'primary'),
8-
'content_id' => array('type' => 'integer', 'null' => false),
9-
'body' => 'text',
10-
'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
11-
'permalink' => array('type' => 'string'),
12-
'parent_id' => array('type' => 'integer'),
13-
'created' => 'datetime',
14-
'updated' => 'datetime'
15-
);
16-
17-
}
18-
?>
9+
10+
public $name = 'Comment';
11+
12+
public $fields = array(
13+
'id' => array('type' => 'integer', 'key' => 'primary'),
14+
'content_id' => array('type' => 'integer', 'null' => false),
15+
'body' => 'text',
16+
'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
17+
'permalink' => array('type' => 'string'),
18+
'parent_id' => array('type' => 'integer'),
19+
'created' => 'datetime',
20+
'updated' => 'datetime'
21+
);
22+
}

Test/Fixture/ContentFixture.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
2-
// For the inheritable behavior
2+
/**
3+
* Short description for class.
4+
*
5+
* @package cake
6+
* @subpackage cake.tests.fixtures
7+
*/
38
class ContentFixture extends CakeTestFixture {
9+
410
public $name = 'Content';
511

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

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

2531

2632
/* Pages */
27-
array('id' => 100, 'parent_id' => 0, 'type' => 'Page', 'title' => 'Home page', 'body'=>'welcome to my site', 'permalink'=>''),
28-
array('id' => 101, 'parent_id' => 100, 'type'=>'Page', 'title'=> 'Frequent Asked Questions', 'body'=> 'questions and more...', 'permalink'=> 'faq'),
29-
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'),
33+
array('id' => 100, 'parent_id' => 0, 'type' => 'Page', 'title' => 'Home page', 'body' => 'welcome to my site', 'permalink' => ''),
34+
array('id' => 101, 'parent_id' => 100, 'type' => 'Page', 'title' => 'Frequent Asked Questions', 'body' => 'questions and more...', 'permalink' => 'faq'),
35+
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'),
3036

3137
);
3238

33-
}
34-
?>
39+
}

Test/Fixture/ImageFixture.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ImageFixture extends CakeTestFixture {
3232
* @var array
3333
*/
3434
public $records = array(
35-
array('id' => 1, 'file_name'=> 'soccer_worldcup.jpg', 'file_size' =>' 53422', 'content_type' => 'image/jpeg'),
36-
array('id' => 2, 'file_name'=> 'dog.png', 'file_size'=>'431234', 'content_type'=>'image/png')
35+
array('id' => 1, 'file_name' => 'soccer_worldcup.jpg', 'file_size' => ' 53422', 'content_type' => 'image/jpeg'),
36+
array('id' => 2, 'file_name' => 'dog.png', 'file_size' => '431234', 'content_type' => 'image/png')
3737
);
3838

3939
}

0 commit comments

Comments
 (0)