Browse Source

Adding test to make sure Table::delete() is first cascaded to associations for which cascadeCallbacks is true

PGBarrow 11 years ago
parent
commit
308501396a

+ 46 - 0
tests/Fixture/GroupsFixture.php

@@ -0,0 +1,46 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\Fixture;
+
+use Cake\TestSuite\Fixture\TestFixture;
+
+/**
+ * Class GroupsFixture
+ *
+ */
+class GroupsFixture extends TestFixture
+{
+
+    /**
+     * fields property
+     *
+     * @var array
+     */
+    public $fields = [
+        'id' => ['type' => 'integer'],
+        'title' => ['type' => 'string'],
+        '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
+    ];
+
+    /**
+     * records property
+     *
+     * @var array
+     */
+    public $records = [
+        ['title' => 'foo'],
+        ['title' => 'bar'],
+    ];
+}

+ 47 - 0
tests/Fixture/GroupsMembersFixture.php

@@ -0,0 +1,47 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\Fixture;
+
+use Cake\TestSuite\Fixture\TestFixture;
+
+/**
+ * Class GroupsMembersFixture
+ *
+ */
+class GroupsMembersFixture extends TestFixture
+{
+
+    /**
+     * fields property
+     *
+     * @var array
+     */
+    public $fields = [
+        'id' => ['type' => 'integer'],
+        'group_id' => ['type' => 'integer'],
+        'member_id' => ['type' => 'integer'],
+        '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
+    ];
+
+    /**
+     * records property
+     *
+     * @var array
+     */
+    public $records = [
+        ['group_id' => 1, 'member_id' => 1],
+        ['group_id' => 2, 'member_id' => 1],
+    ];
+}

+ 45 - 0
tests/Fixture/MembersFixture.php

@@ -0,0 +1,45 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
+ * @since         1.2.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\Fixture;
+
+use Cake\TestSuite\Fixture\TestFixture;
+
+/**
+ * Class MembersFixture
+ *
+ */
+class MembersFixture extends TestFixture
+{
+
+    /**
+     * fields property
+     *
+     * @var array
+     */
+    public $fields = [
+        'id' => ['type' => 'integer'],
+        'group_count' => ['type' => 'integer'],
+        '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
+    ];
+
+    /**
+     * records property
+     *
+     * @var array
+     */
+    public $records = [
+        ['group_count' => 2],
+    ];
+}

+ 17 - 0
tests/TestCase/ORM/TableTest.php

@@ -56,6 +56,9 @@ class TableTest extends TestCase
         'core.tags',
         'core.articles_tags',
         'core.site_articles',
+        'core.members',
+        'core.groups',
+        'core.groups_members',
     ];
 
     /**
@@ -2465,6 +2468,20 @@ class TableTest extends TestCase
         $this->assertNull($query->all()->first(), 'Should not find any rows.');
     }
 
+    public function testDeleteAssociationsCascadingCallbacksOrder()
+    {
+        $Members = TableRegistry::get('Members');
+
+        $member = $Members->get(1);
+        $this->assertEquals(2, $member->group_count);
+
+        $group = $Members->Groups->get(1);
+        $Members->Groups->delete($group);
+
+        $member = $Members->get(1);
+        $this->assertEquals(1, $member->group_count);
+    }
+
     /**
      * Test delete callbacks
      *

+ 31 - 0
tests/test_app/TestApp/Model/Table/GroupsMembersTable.php

@@ -0,0 +1,31 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Model\Table;
+
+use Cake\ORM\Table;
+
+/**
+ * Article table class
+ *
+ */
+class GroupsMembersTable extends Table
+{
+
+    public function initialize(array $config)
+    {
+        $this->belongsTo('Members');
+
+        $this->addBehavior('CounterCache', ['Members' => ['group_count']]);
+    }
+
+}

+ 34 - 0
tests/test_app/TestApp/Model/Table/GroupsTable.php

@@ -0,0 +1,34 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Model\Table;
+
+use Cake\ORM\Table;
+
+/**
+ * Article table class
+ *
+ */
+class GroupsTable extends Table
+{
+
+    public function initialize(array $config)
+    {
+        $this->belongsToMany('Members');
+
+        $this->hasMany('GroupsMembers', [
+            'dependent' => true,
+            'cascadeCallbacks' => true
+        ]);
+    }
+
+}

+ 29 - 0
tests/test_app/TestApp/Model/Table/MembersTable.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Model\Table;
+
+use Cake\ORM\Table;
+
+/**
+ * Article table class
+ *
+ */
+class MembersTable extends Table
+{
+
+    public function initialize(array $config)
+    {
+        $this->belongsToMany('Groups');
+    }
+
+}