From 3140144a1f01c6d6a375e17f7bda020534d61fc9 Mon Sep 17 00:00:00 2001 From: nate Date: Sun, 24 Feb 2008 02:47:10 +0000 Subject: [PATCH] Adding behavior test case and fixing validation behavior methods, fixes #4205 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6471 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 2 +- cake/tests/cases/libs/model/behavior.test.php | 249 ++++++++ .../cases/libs/model/behaviors/tree.test.php | 12 +- cake/tests/cases/libs/model/models.php | 550 ++++++++++++++++++ cake/tests/fixtures/callback_fixture.php | 51 ++ 5 files changed, 857 insertions(+), 7 deletions(-) create mode 100644 cake/tests/cases/libs/model/behavior.test.php create mode 100644 cake/tests/cases/libs/model/models.php create mode 100644 cake/tests/fixtures/callback_fixture.php diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 6ed304265..9134266d1 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -2050,7 +2050,7 @@ class Model extends Overloadable { } $data = $this->data; $methods = array_map('strtolower', get_class_methods($this)); - $behaviorMethods = $this->Behaviors->methods(); + $behaviorMethods = array_keys($this->Behaviors->methods()); if (isset($data[$this->alias])) { $data = $data[$this->alias]; diff --git a/cake/tests/cases/libs/model/behavior.test.php b/cake/tests/cases/libs/model/behavior.test.php new file mode 100644 index 000000000..bdc6c8ad1 --- /dev/null +++ b/cake/tests/cases/libs/model/behavior.test.php @@ -0,0 +1,249 @@ + 'testMethod', '/look for\s+(.+)/' => 'speakEnglish'); + + function setup(&$model, $config = array()) { + $this->settings[$model->alias] = array_merge(array('before' => 'on', 'after' => 'off'), $config); + } + + function beforeFind(&$model, $query) { + $settings = $this->settings[$model->alias]; + switch ($settings['before']) { + case 'on': + return false; + break; + case 'off': + return true; + break; + case 'test': + return null; + break; + case 'modify': + $query['fields'] = array('Apple.id', 'Apple.name', 'Apple.mytime'); + $query['recursive'] = -1; + return $query; + break; + } + } + + function afterFind(&$model, $results, $primary) { + $settings = $this->settings[$model->alias]; + switch ($settings['after']) { + case 'on': + return array(); + break; + case 'off': + return $results; + break; + case 'test': + return true; + break; + case 'test2': + return null; + break; + case 'modify': + return Set::extract($results, '{n}.Apple'); + break; + } + } + + function beforeValidate(&$model) { + $settings = $this->settings[$model->alias]; + if (!isset($settings['validate'])) { + return true; + } + switch ($settings['validate']) { + case 'on': + $model->invalidate('name'); + return true; + break; + case 'off': + return $results; + break; + case 'test': + return true; + break; + case 'test2': + return null; + break; + case 'modify': + return Set::extract($results, '{n}.Apple'); + break; + } + } + + function testMethod(&$model, $param = true) { + if ($param === true) { + return 'working'; + } + } + + function validateField(&$model, $field) { + return current($field) === 'Orange'; + } + + function speakEnglish(&$model, $method, $query) { + $method = preg_replace('/look for\s+/', 'Item.name = \'', $method); + $query = preg_replace('/^in\s+/', 'Location.name = \'', $query); + return $method . '\' AND ' . $query . '\''; + } +} + +class BehaviorTest extends CakeTestCase { + + var $fixtures = array('core.apple', 'core.sample'); + + function testBehaviorBinding() { + $this->model = new Apple(); + $this->assertIdentical($this->model->Behaviors->attached(), array()); + + $this->model->Behaviors->attach('Test', array('key' => 'value')); + $this->assertIdentical($this->model->Behaviors->attached(), array('Test')); + $this->assertEqual(strtolower(get_class($this->model->Behaviors->Test)), 'testbehavior'); + $this->assertEqual($this->model->Behaviors->Test->settings['Apple'], array('before' => 'on', 'after' => 'off', 'key' => 'value')); + $this->assertEqual(array_keys($this->model->Behaviors->Test->settings), array('Apple')); + + $this->assertIdentical($this->model->Sample->Behaviors->attached(), array()); + $this->model->Sample->Behaviors->attach('Test', array('key2' => 'value2')); + $this->assertIdentical($this->model->Sample->Behaviors->attached(), array('Test')); + $this->assertEqual($this->model->Behaviors->Test->settings['Sample'], array('before' => 'on', 'after' => 'off', 'key2' => 'value2')); + + $this->assertEqual(array_keys($this->model->Behaviors->Test->settings), array('Apple', 'Sample')); + $this->assertIdentical($this->model->Behaviors->Test->settings, $this->model->Sample->Behaviors->Test->settings); + + $this->model->Behaviors->attach('Test', array('key3' => 'value3', 'before' => 'off')); + $this->assertEqual($this->model->Behaviors->Test->settings['Apple'], array('before' => 'off', 'after' => 'off', 'key' => 'value', 'key3' => 'value3')); + $this->assertIdentical($this->model->Behaviors->Test->settings, $this->model->Sample->Behaviors->Test->settings); + } + + function testBehaviorToggling() { + $this->model = new Apple(); + $this->assertIdentical($this->model->Behaviors->enabled(), array()); + + $this->model->Behaviors->attach('Test', array('key' => 'value')); + $this->assertIdentical($this->model->Behaviors->enabled(), array('Test')); + + $this->model->Behaviors->disable('Test'); + $this->assertIdentical($this->model->Behaviors->attached(), array('Test')); + $this->assertIdentical($this->model->Behaviors->enabled(), array()); + + $this->model->Sample->Behaviors->attach('Test'); + $this->assertIdentical($this->model->Sample->Behaviors->enabled(), array('Test')); + $this->assertIdentical($this->model->Behaviors->enabled(), array()); + + $this->model->Behaviors->enable('Test'); + $this->assertIdentical($this->model->Behaviors->attached(), array('Test')); + $this->assertIdentical($this->model->Behaviors->enabled(), array('Test')); + + $this->model->Behaviors->disable('Test'); + $this->assertIdentical($this->model->Behaviors->enabled(), array()); + $this->model->Behaviors->attach('Test'); + $this->assertIdentical($this->model->Behaviors->enabled(), array('Test')); + } + + function testBehaviorFindCallbacks() { + $this->model = new Apple(); + $expected = $this->model->find('all'); + + $this->model->Behaviors->attach('Test'); + $this->assertIdentical($this->model->find('all'), null); + + $this->model->Behaviors->attach('Test', array('before' => false)); + $this->assertIdentical($this->model->find('all'), $expected); + + $this->model->Behaviors->attach('Test', array('before' => 'test')); + $this->assertIdentical($this->model->find('all'), $expected); + + $this->model->Behaviors->attach('Test', array('before' => 'modify')); + $expected2 = array( + array('Apple' => array('id' => '1', 'name' => 'Red Apple 1', 'mytime' => '22:57:17')), + array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')), + array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17')) + ); + $result = $this->model->find('all', array('conditions' => array('Apple.id' => '< 4'))); + $this->assertEqual($result, $expected2); + + $this->model->Behaviors->disable('Test'); + $result = $this->model->find('all'); + $this->assertEqual($result, $expected); + + $this->model->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on')); + $this->assertIdentical($this->model->find('all'), array()); + + $this->model->Behaviors->attach('Test', array('after' => 'off')); + $this->assertEqual($this->model->find('all'), $expected); + + $this->model->Behaviors->attach('Test', array('after' => 'test')); + $this->assertEqual($this->model->find('all'), $expected); + + $this->model->Behaviors->attach('Test', array('after' => 'test2')); + $this->assertEqual($this->model->find('all'), $expected); + + $this->model->Behaviors->attach('Test', array('after' => 'modify')); + $expected = array( + array('id' => '1', 'apple_id' => '2', 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'), + array('id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'), + array('id' => '3', 'apple_id' => '2', 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'), + array('id' => '4', 'apple_id' => '2', 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'), + array('id' => '5', 'apple_id' => '5', 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'), + array('id' => '6', 'apple_id' => '4', 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'), + array('id' => '7', 'apple_id' => '6', 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17') + ); + $this->assertEqual($this->model->find('all'), $expected); + } + + function testBehaviorValidateCallback() { + $this->model = new Apple(); + $this->model->Behaviors->attach('Test'); + $this->assertIdentical($this->model->validates(), true); + + $this->model->Behaviors->attach('Test', array('validate' => 'on')); + $this->assertIdentical($this->model->validates(), false); + $this->assertIdentical($this->model->validationErrors, array('name' => true)); + } + + function testBehaviorValidateMethods() { + $this->model = new Apple(); + $this->model->Behaviors->attach('Test'); + $this->model->validate['color'] = 'validateField'; + + $result = $this->model->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange')); + $this->assertEqual(array_keys($result['Apple']), array('name', 'color', 'modified', 'created')); + + $this->model->create(); + $result = $this->model->save(array('name' => 'Regular Apple', 'color' => 'Red')); + $this->assertFalse($result); + } + + function testBehaviorMethodDispatching() { + $this->model = new Apple(); + $this->model->Behaviors->attach('Test'); + + $expected = 'working'; + $this->assertEqual($this->model->testMethod(), $expected); + $this->assertEqual($this->model->Behaviors->dispatchMethod('testMethod'), $expected); + + $result = $this->model->Behaviors->dispatchMethod('wtf'); + $this->assertEqual($result, array('unhandled')); + + $result = $this->model->{'look for the remote'}('in the couch'); + $expected = "Item.name = 'the remote' AND Location.name = 'the couch'"; + $this->assertEqual($result, $expected); + } + + function tearDown() { + unset($this->model); + ClassRegistry::flush(); + } +} + +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/behaviors/tree.test.php b/cake/tests/cases/libs/model/behaviors/tree.test.php index 4d0f4534b..66e52666f 100644 --- a/cake/tests/cases/libs/model/behaviors/tree.test.php +++ b/cake/tests/cases/libs/model/behaviors/tree.test.php @@ -672,18 +672,18 @@ class NumberTreeCase extends CakeTestCase { $this->assertIdentical($validTree, true); } - function testRemoveLastTopParent () { + function testRemoveLastTopParent() { $this->NumberTree = & new NumberTree(); $this->NumberTree->__initialize(2, 2); $initialCount = $this->NumberTree->findCount(); - $initialTopNodes = $this->NumberTree->childcount(false); + $initialTopNodes = $this->NumberTree->childCount(false); $result = $this->NumberTree->findByName('1. Root'); $this->NumberTree->removeFromTree($result['NumberTree']['id']); $laterCount = $this->NumberTree->findCount(); - $laterTopNodes = $this->NumberTree->childcount(false); + $laterTopNodes = $this->NumberTree->childCount(false); $this->assertEqual($initialCount, $laterCount); $this->assertEqual($initialTopNodes, $laterTopNodes); @@ -754,11 +754,11 @@ class NumberTreeCase extends CakeTestCase { $data = $this->NumberTree->find(array('NumberTree.name' => '1. Root')); $this->NumberTree->id = $data['NumberTree']['id']; - $direct = $this->NumberTree->childcount(null, true); + $direct = $this->NumberTree->childCount(null, true); $this->assertEqual($direct, 2); $expects = $this->NumberTree->findCount() - 1; - $total = $this->NumberTree->childcount(); + $total = $this->NumberTree->childCount(); $this->assertEqual($total, 6); } @@ -781,7 +781,7 @@ class NumberTreeCase extends CakeTestCase { $data = $this->NumberTree->find(array('NumberTree.name' => '1.2.2')); $this->NumberTree->id= $data['NumberTree']['id']; - $result = $this->NumberTree->getpath(null, array('name')); + $result = $this->NumberTree->getPath(null, array('name')); $expects = array(array('NumberTree' => array('name' => '1. Root')), array('NumberTree' => array('name' => '1.2')), array('NumberTree' => array('name' => '1.2.2'))); diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php new file mode 100644 index 000000000..6e59c53fc --- /dev/null +++ b/cake/tests/cases/libs/model/models.php @@ -0,0 +1,550 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + * @since CakePHP(tm) v 1.2.0.6464 + * @version $Revision: 6441 $ + * @modifiedby $LastChangedBy: nate $ + * @lastmodified $Date: 2008-02-06 10:48:31 -0500 (Wed, 06 Feb 2008) $ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { + define('CAKEPHP_UNIT_TEST_EXECUTION', 1); +} + +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Test extends Model { + var $useTable = false; + var $name = 'Test'; + + var $_schema = array( + 'id'=> array('type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8', 'key'=>'primary'), + 'name'=> array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'email'=> array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + 'notes'=> array('type' => 'text', 'null' => '1', 'default' => 'write some notes here', 'length' => ''), + 'created'=> array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'updated'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); +} + +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class TestValidate extends Model { + var $useTable = false; + var $name = 'TestValidate'; + + var $_schema = array( + 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'title' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'body' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => ''), + 'number' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), + 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + 'modified' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + ); + + function validateNumber($value, $options) { + $options = am(array('min' => 0, 'max' => 100), $options); + $valid = ($value['number'] >= $options['min'] && $value['number'] <= $options['max']); + return $valid; + } + + function validateTitle($value) { + return (!empty($value) && strpos(low($value['title']), 'title-') === 0); + } +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class User extends CakeTestModel { + var $name = 'User'; + var $validate = array('user' => VALID_NOT_EMPTY, 'password' => VALID_NOT_EMPTY); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Article extends CakeTestModel { + var $name = 'Article'; + var $belongsTo = array('User'); + var $hasMany = array('Comment' => array('className'=>'Comment', 'dependent' => true)); + var $hasAndBelongsToMany = array('Tag'); + var $validate = array('user_id' => VALID_NUMBER, 'title' => array('allowEmpty' => false, 'rule' => VALID_NOT_EMPTY), 'body' => VALID_NOT_EMPTY); + + function titleDuplicate ($title) { + if ($title === 'My Article Title') { + return false; + } + return true; + } +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Article10 extends CakeTestModel { + var $name = 'Article10'; + var $useTable = 'articles'; + var $hasMany = array('Comment' => array('dependent' => true, 'exclusive' => true)); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class ArticleFeatured extends CakeTestModel { + var $name = 'ArticleFeatured'; + var $belongsTo = array('User', 'Category'); + var $hasOne = array('Featured'); + var $hasMany = array('Comment' => array('className'=>'Comment', 'dependent' => true)); + var $hasAndBelongsToMany = array('Tag'); + var $validate = array('user_id' => VALID_NUMBER, 'title' => VALID_NOT_EMPTY, 'body' => VALID_NOT_EMPTY); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Featured extends CakeTestModel { + var $name = 'Featured'; + var $belongsTo = array( + 'ArticleFeatured'=> array('className' => 'ArticleFeatured'), + 'Category'=> array('className' => 'Category') + ); +} + +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Tag extends CakeTestModel { + var $name = 'Tag'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class ArticlesTag extends CakeTestModel { + var $name = 'ArticlesTag'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class ArticleFeaturedsTag extends CakeTestModel { + var $name = 'ArticleFeaturedsTag'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Comment extends CakeTestModel { + var $name = 'Comment'; + var $belongsTo = array('Article', 'User'); + var $hasOne = array('Attachment' => array('className'=>'Attachment', 'dependent' => true)); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Attachment extends CakeTestModel { + var $name = 'Attachment'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Category extends CakeTestModel { + var $name = 'Category'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class CategoryThread extends CakeTestModel { + var $name = 'CategoryThread'; + var $belongsTo = array('ParentCategory' => array('className' => 'CategoryThread', 'foreignKey' => 'parent_id')); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Apple extends CakeTestModel { + var $name = 'Apple'; + var $validate = array('name' => VALID_NOT_EMPTY); + var $hasOne = array('Sample'); + var $hasMany = array('Child' => array('className' => 'Apple', 'dependent' => true)); + var $belongsTo = array('Parent' => array('className' => 'Apple', 'foreignKey' => 'apple_id')); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Sample extends CakeTestModel { + var $name = 'Sample'; + var $belongsTo = 'Apple'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class AnotherArticle extends CakeTestModel { + var $name = 'AnotherArticle'; + var $hasMany = 'Home'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Advertisement extends CakeTestModel { + var $name = 'Advertisement'; + var $hasMany = 'Home'; +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Home extends CakeTestModel { + var $name = 'Home'; + var $belongsTo = array('AnotherArticle', 'Advertisement'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Post extends CakeTestModel { + var $name = 'Post'; + var $belongsTo = array('Author'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Author extends CakeTestModel { + var $name = 'Author'; + var $hasMany = array('Post'); + + function afterFind($results) { + $results[0]['Author']['test'] = 'working'; + return $results; + } +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Project extends CakeTestModel { + var $name = 'Project'; + var $hasMany = array('Thread'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Thread extends CakeTestModel { + var $name = 'Thread'; + var $hasMany = array('Message'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Message extends CakeTestModel { + var $name = 'Message'; + var $hasOne = array('Bid'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Bid extends CakeTestModel { + var $name = 'Bid'; + var $belongsTo = array('Message'); +} +class NodeAfterFind extends CakeTestModel { + var $name = 'NodeAfterFind'; + var $validate = array('name' => VALID_NOT_EMPTY); + var $useTable = 'apples'; + var $hasOne = array('Sample' => array('className' => 'NodeAfterFindSample')); + var $hasMany = array('Child' => array('className' => 'NodeAfterFind', 'dependent' => true)); + var $belongsTo = array('Parent' => array('className' => 'NodeAfterFind', 'foreignKey' => 'apple_id')); + + function afterFind($results) { + return $results; + } +} +class NodeAfterFindSample extends CakeTestModel { + var $name = 'NodeAfterFindSample'; + var $useTable = 'samples'; + var $belongsTo = 'NodeAfterFind'; +} +class NodeNoAfterFind extends CakeTestModel { + var $name = 'NodeAfterFind'; + var $validate = array('name' => VALID_NOT_EMPTY); + var $useTable = 'apples'; + var $hasOne = array('Sample' => array('className' => 'NodeAfterFindSample')); + var $hasMany = array('Child' => array( 'className' => 'NodeAfterFind', 'dependent' => true)); + var $belongsTo = array('Parent' => array('className' => 'NodeAfterFind', 'foreignKey' => 'apple_id')); +} +class ModelA extends CakeTestModel { + var $name = 'ModelA'; + var $useTable = 'apples'; + var $hasMany = array('ModelB', 'ModelC'); +} +class ModelB extends CakeTestModel { + var $name = 'ModelB'; + var $useTable = 'messages'; + var $hasMany = array('ModelD'); +} +class ModelC extends CakeTestModel { + var $name = 'ModelC'; + var $useTable = 'bids'; + var $hasMany = array('ModelD'); +} +class ModelD extends CakeTestModel { + var $name = 'ModelD'; + var $useTable = 'threads'; +} +class Something extends CakeTestModel { + var $name = 'Something'; + var $hasAndBelongsToMany = array('SomethingElse' => array('with' => array('JoinThing' => array('doomed')))); +} +class SomethingElse extends CakeTestModel { + var $name = 'SomethingElse'; + var $hasAndBelongsToMany = array('Something' => array('with' => 'JoinThing')); +} +class JoinThing extends CakeTestModel { + var $name = 'JoinThing'; + var $belongsTo = array('Something', 'SomethingElse'); +} +class Portfolio extends CakeTestModel { + var $name = 'Portfolio'; + var $hasAndBelongsToMany = array('Item'); +} +class Item extends CakeTestModel { + var $name = 'Item'; + var $belongsTo = array('Syfile' => array('counterCache' => true)); + var $hasAndBelongsToMany = array('Portfolio'); +} +class ItemsPortfolio extends CakeTestModel { + var $name = 'ItemsPortfolio'; +} +class Syfile extends CakeTestModel { + var $name = 'Syfile'; + var $belongsTo = array('Image'); +} +class Image extends CakeTestModel { + var $name = 'Image'; +} +class DeviceType extends CakeTestModel { + var $name = 'DeviceType'; + var $order = array('DeviceType.order' => 'ASC'); + var $belongsTo = array( + 'DeviceTypeCategory', 'FeatureSet', 'ExteriorTypeCategory', + 'Image' => array('className' => 'Document'), + 'Extra1' => array('className' => 'Document'), + 'Extra2' => array('className' => 'Document')); + var $hasMany = array('Device' => array('order' => array('Device.id' => 'ASC'))); +} +class DeviceTypeCategory extends CakeTestModel { + var $name = 'DeviceTypeCategory'; +} +class FeatureSet extends CakeTestModel { + var $name = 'FeatureSet'; +} +class ExteriorTypeCategory extends CakeTestModel { + var $name = 'ExteriorTypeCategory'; + var $belongsTo = array('Image' => array('className' => 'Device')); +} +class Document extends CakeTestModel { + var $name = 'Document'; + var $belongsTo = array('DocumentDirectory'); +} +class Device extends CakeTestModel { + var $name = 'Device'; +} +class DocumentDirectory extends CakeTestModel { + var $name = 'DocumentDirectory'; +} +class PrimaryModel extends CakeTestModel { + var $name = 'PrimaryModel'; +} +class SecondaryModel extends CakeTestModel { + var $name = 'SecondaryModel'; +} +class JoinA extends CakeTestModel { + var $name = 'JoinA'; + var $hasAndBelongsToMany = array('JoinB', 'JoinC'); +} +class JoinB extends CakeTestModel { + var $name = 'JoinB'; + var $hasAndBelongsToMany = array('JoinA'); +} +class JoinC extends CakeTestModel { + var $name = 'JoinC'; + var $hasAndBelongsToMany = array('JoinA'); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class AssociationTest1 extends CakeTestModel { + var $useTable = 'join_as'; + var $name = 'AssociationTest1'; + + var $hasAndBelongsToMany = array('AssociationTest2' => array( + 'unique' => false, 'joinTable' => 'join_as_join_bs', 'foreignKey' => false + )); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class AssociationTest2 extends CakeTestModel { + var $useTable = 'join_bs'; + var $name = 'AssociationTest2'; + + var $hasAndBelongsToMany = array('AssociationTest1' => array( + 'unique' => false, 'joinTable' => 'join_as_join_bs' + )); +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class Callback extends CakeTestModel { + +} +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class ValidationTest extends CakeTestModel { + var $name = 'ValidationTest'; + var $useTable = false; + var $_schema = array(); + + var $validate = array( + 'title' => VALID_NOT_EMPTY, + 'published' => 'customValidationMethod', + 'body' => array( + VALID_NOT_EMPTY, + '/^.{5,}$/s' => 'no matchy', + '/^[0-9A-Za-z \\.]{1,}$/s' + ) + ); + + function customValidationMethod($data) { + return $data === 1; + } +} + +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.cases.libs.model + */ +class ValidationTest2 extends CakeTestModel { + var $name = 'ValidationTest2'; + var $useTable = false; + + var $validate = array( + 'title' => VALID_NOT_EMPTY, + 'published' => 'customValidationMethod', + 'body' => array( + VALID_NOT_EMPTY, + '/^.{5,}$/s' => 'no matchy', + '/^[0-9A-Za-z \\.]{1,}$/s' + ) + ); + + function customValidationMethod($data) { + return $data === 1; + } + + function schema() { + return array(); + } +} + +?> \ No newline at end of file diff --git a/cake/tests/fixtures/callback_fixture.php b/cake/tests/fixtures/callback_fixture.php new file mode 100644 index 000000000..d62a3da5f --- /dev/null +++ b/cake/tests/fixtures/callback_fixture.php @@ -0,0 +1,51 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake.tests + * @subpackage cake.tests.fixtures + * @since CakePHP(tm) v 1.2.0.4667 + * @version $Revision: 6354 $ + * @modifiedby $LastChangedBy: nate $ + * @lastmodified $Date: 2008-01-10 02:02:33 -0500 (Thu, 10 Jan 2008) $ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +/** + * Short description for class. + * + * @package cake.tests + * @subpackage cake.tests.fixtures + */ +class CallbackFixture extends CakeTestFixture { + var $name = 'Callback'; + var $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'user' => array('type' => 'string', 'null' => false), + 'password' => array('type' => 'string', 'null' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + var $records = array( + array('user' => 'user1', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'), + array('user' => 'user2', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'), + array('user' => 'user3', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'), + ); +} + +?> \ No newline at end of file