From 08a4d0bf63d8c095e4a967c52503379a3d3568c3 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Sat, 3 Mar 2012 23:45:45 +0100 Subject: [PATCH] Avoid unsetting Model's $findMethods when defining a custom find method in AppModel, also add tests for custom finds --- lib/Cake/Model/Model.php | 2 + lib/Cake/Test/Case/Model/ModelReadTest.php | 20 +++++++ lib/Cake/Test/Case/Model/models.php | 70 ++++++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 246b9f91a..7a5574116 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -699,6 +699,8 @@ class Model extends Object implements CakeEventListener { } $this->_mergeVars($merge, 'AppModel'); } + $this->_mergeVars(array('findMethods'), 'Model'); + $this->Behaviors = new BehaviorCollection(); if ($this->useTable !== false) { diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 5292670dc..ff756de86 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -7826,4 +7826,24 @@ class ModelReadTest extends BaseModelTest { $this->assertTrue(is_array($result) && !empty($result)); } + +/** + * test custom find method + * + * @return void + */ + public function testfindCustom() { + $this->loadFixtures('Article'); + $Article = new CustomArticle(); + $data = array('user_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N'); + $Article->create($data); + $Article->save(); + $this->assertEquals(4, $Article->id); + + $result = $Article->find('published'); + $this->assertEquals(3, count($result)); + + $result = $Article->find('unPublished'); + $this->assertEquals(1, count($result)); + } } diff --git a/lib/Cake/Test/Case/Model/models.php b/lib/Cake/Test/Case/Model/models.php index bd9d34afc..7cf58dcca 100644 --- a/lib/Cake/Test/Case/Model/models.php +++ b/lib/Cake/Test/Case/Model/models.php @@ -18,6 +18,41 @@ * @since CakePHP(tm) v 1.2.0.6464 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::uses('Model', 'Model'); +/** + * AppModel class + * + * @package Cake.Test.Case.Model + */ +class AppModel extends Model { + +/** + * findMethods property + * + * @var array + */ + public $findMethods = array('published' => true); + +/** + * useDbConfig property + * + * @var array + */ + public $useDbConfig = 'test'; + +/** + * _findPublished custom find + * + * @return array + */ + public function _findPublished($state, $query, $results = array()) { + if ($state === 'before') { + $query['conditions']['published'] = 'Y'; + return $query; + } + return $results; + } +} /** * Test class @@ -4689,3 +4724,38 @@ class ArmorsPlayer extends CakeTestModel { public $useDbConfig = 'test_database_three'; } + +/** + * CustomArticle class + * + * @package Cake.Test.Case.Model + */ +class CustomArticle extends AppModel { +/** + * useTable property + * + * @var string + */ + public $useTable = 'articles'; + +/** + * findMethods property + * + * @var array + */ + public $findMethods = array('unPublished' => true); + +/** + * _findUnPublished custom find + * + * @return array + */ + public function _findUnPublished($state, $query, $results = array()) { + if ($state === 'before') { + $query['conditions']['published'] = 'N'; + return $query; + } + return $results; + } + +} \ No newline at end of file