From e382f53eadebbca303735438a1a31b4bb8b3e14a Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 23 Jul 2009 13:31:16 +0000 Subject: [PATCH 1/7] Fixing duplicated test case class name. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8249 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/tests/cases/console/libs/api.test.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cake/tests/cases/console/libs/api.test.php b/cake/tests/cases/console/libs/api.test.php index 62b316ee0..dbaa34fa6 100644 --- a/cake/tests/cases/console/libs/api.test.php +++ b/cake/tests/cases/console/libs/api.test.php @@ -1,9 +1,7 @@ Date: Thu, 23 Jul 2009 13:35:14 +0000 Subject: [PATCH 2/7] Removing use of e() from core classes. Refs #6525 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8250 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo/dbo_oracle.php | 6 +++--- cake/libs/model/datasources/dbo_source.php | 6 +++--- cake/libs/view/helpers/ajax.php | 7 +++---- cake/libs/view/layouts/xml/default.ctp | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_oracle.php b/cake/libs/model/datasources/dbo/dbo_oracle.php index f7ec6f06e..92ffc76dd 100644 --- a/cake/libs/model/datasources/dbo/dbo_oracle.php +++ b/cake/libs/model/datasources/dbo/dbo_oracle.php @@ -952,11 +952,11 @@ class DboOracle extends DboSource { if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) { if (!isset($resultSet) || !is_array($resultSet)) { if (Configure::read() > 0) { - e('
' . sprintf(__('SQL Error in model %s:', true), $model->alias) . ' '); + echo '
' . sprintf(__('SQL Error in model %s:', true), $model->alias) . ' '; if (isset($this->error) && $this->error != null) { - e($this->error); + echo $this->error; } - e('
'); + echo '
'; } return null; } diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 8264081d6..318593f95 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -742,11 +742,11 @@ class DboSource extends DataSource { if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) { if (!isset($resultSet) || !is_array($resultSet)) { if (Configure::read() > 0) { - e('
' . sprintf(__('SQL Error in model %s:', true), $model->alias) . ' '); + echo '
' . sprintf(__('SQL Error in model %s:', true), $model->alias) . ' '; if (isset($this->error) && $this->error != null) { - e($this->error); + echo $this->error; } - e('
'); + echo '
'; } return null; } diff --git a/cake/libs/view/helpers/ajax.php b/cake/libs/view/helpers/ajax.php index 97d5c9580..ed0de9782 100644 --- a/cake/libs/view/helpers/ajax.php +++ b/cake/libs/view/helpers/ajax.php @@ -958,7 +958,7 @@ class AjaxHelper extends AppHelper { $keys = array_keys($this->__ajaxBuffer); if (count($divs) == 1 && in_array($divs[0], $keys)) { - e($this->__ajaxBuffer[$divs[0]]); + echo $this->__ajaxBuffer[$divs[0]]; } else { foreach ($this->__ajaxBuffer as $key => $val) { if (in_array($key, $divs)) { @@ -969,14 +969,13 @@ class AjaxHelper extends AppHelper { $out .= 'for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string"'; $out .= ' && $(n)) Element.update($(n), unescape(decodeURIComponent('; $out .= '__ajaxUpdater__[n]))); }'; - e($this->Javascript->codeBlock($out, false)); + echo $this->Javascript->codeBlock($out, false); } $scripts = $this->Javascript->getCache(); if (!empty($scripts)) { - e($this->Javascript->codeBlock($scripts, false)); + echo $this->Javascript->codeBlock($scripts, false); } - $this->_stop(); } } diff --git a/cake/libs/view/layouts/xml/default.ctp b/cake/libs/view/layouts/xml/default.ctp index c68870298..566ca2158 100644 --- a/cake/libs/view/layouts/xml/default.ctp +++ b/cake/libs/view/layouts/xml/default.ctp @@ -1,2 +1,2 @@ -header()); ?> +header(); ?> \ No newline at end of file From 30281d6c59cf537693e988a06a3d22784ad07052 Mon Sep 17 00:00:00 2001 From: gwoo Date: Thu, 23 Jul 2009 19:28:46 +0000 Subject: [PATCH 3/7] fixes #6506, notice on 5.2.10 for habtm save with empty value git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8251 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index cf50f8f6c..58fa4c826 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -372,7 +372,7 @@ class Model extends Overloadable { } elseif ($table) { $this->useTable = $table; } - + if ($ds !== null) { $this->useDbConfig = $ds; } @@ -827,7 +827,7 @@ class Model extends Overloadable { $type = $this->getColumnType($field); if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) { - $useNewDate = (isset($data['year']) || isset($data['month']) || + $useNewDate = (isset($data['year']) || isset($data['month']) || isset($data['day']) || isset($data['hour']) || isset($data['minute'])); $dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec'); @@ -1343,7 +1343,7 @@ class Model extends Overloadable { unset($values); } elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { $newData[] = $row; - } elseif (isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { + } elseif (isset($row[$join]) && isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { $newData[] = $row[$join]; } } From 8f61c666c25f2753c698f280593d060a0d371443 Mon Sep 17 00:00:00 2001 From: DarkAngelBGE Date: Thu, 23 Jul 2009 20:51:24 +0000 Subject: [PATCH 4/7] Removing usage of r() from core git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8252 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/console/libs/api.php | 2 +- cake/console/libs/shell.php | 4 ++-- cake/libs/view/helpers/time.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cake/console/libs/api.php b/cake/console/libs/api.php index 95897c0c2..8d6c4078d 100644 --- a/cake/console/libs/api.php +++ b/cake/console/libs/api.php @@ -202,7 +202,7 @@ class ApiShell extends Shell { if (strpos($method, '__') === false && $method[0] != '_') { $parsed[$method] = array( - 'comment' => r(array('/*', '*/', '*'), '', trim($result[1][$key])), + 'comment' => str_replace(array('/*', '*/', '*'), '', trim($result[1][$key])), 'method' => $method, 'parameters' => trim($result[3][$key]) ); diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index bac6c347b..99613bf26 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -488,8 +488,8 @@ class Shell extends Object { */ function shortPath($file) { $shortPath = str_replace(ROOT, null, $file); - $shortPath = str_replace('..'.DS, '', $shortPath); - return r(DS.DS, DS, $shortPath); + $shortPath = str_replace('..' . DS, '', $shortPath); + return str_replace(DS . DS, DS, $shortPath); } /** * Checks for Configure::read('Routing.admin') and forces user to input it if not enabled diff --git a/cake/libs/view/helpers/time.php b/cake/libs/view/helpers/time.php index 066d97906..e2a8c723a 100644 --- a/cake/libs/view/helpers/time.php +++ b/cake/libs/view/helpers/time.php @@ -481,7 +481,7 @@ class TimeHelper extends AppHelper { * @return bool */ function wasWithinLast($timeInterval, $dateString, $userOffset = null) { - $tmp = r(' ', '', $timeInterval); + $tmp = str_replace(' ', '', $timeInterval); if (is_numeric($tmp)) { $timeInterval = $tmp . ' ' . __('days', true); } From 9eaad7528fc1efb09c2420c9e77ae75cb39ed1b4 Mon Sep 17 00:00:00 2001 From: DarkAngelBGE Date: Thu, 23 Jul 2009 21:04:40 +0000 Subject: [PATCH 5/7] Removing usage of up() and low() from core git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8253 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/console/libs/api.php | 2 +- cake/console/libs/shell.php | 14 ++++++------- cake/console/libs/tasks/controller.php | 28 +++++++++++++------------- cake/console/libs/tasks/db_config.php | 2 +- cake/console/libs/tasks/model.php | 16 +++++++-------- cake/console/libs/tasks/plugin.php | 4 ++-- cake/console/libs/tasks/project.php | 6 +++--- cake/console/libs/tasks/view.php | 12 +++++------ 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/cake/console/libs/api.php b/cake/console/libs/api.php index 8d6c4078d..fb529e439 100644 --- a/cake/console/libs/api.php +++ b/cake/console/libs/api.php @@ -66,7 +66,7 @@ class ApiShell extends Shell { return $this->help(); } - $type = low($this->args[0]); + $type = strtolower($this->args[0]); if (isset($this->paths[$type])) { $path = $this->paths[$type]; diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index 99613bf26..47bbfac1f 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -149,10 +149,10 @@ class Shell extends Object { ClassRegistry::map($this->name, $this->alias); if (!PHP5 && isset($this->args[0])) { - if (strpos($this->name, low(Inflector::camelize($this->args[0]))) !== false) { + if (strpos($this->name, strtolower(Inflector::camelize($this->args[0]))) !== false) { $dispatch->shiftArgs(); } - if (low($this->command) == low(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) { + if (strtolower($this->command) == strtolower(Inflector::variable($this->args[0])) && method_exists($this, $this->command)) { $dispatch->shiftArgs(); } } @@ -329,7 +329,7 @@ class Shell extends Object { } } if (is_array($options)) { - while ($in == '' || ($in && (!in_array(low($in), $options) && !in_array(up($in), $options)) && !in_array($in, $options))) { + while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) { $in = $this->Dispatch->getInput($prompt, $options, $default); } } @@ -427,10 +427,10 @@ class Shell extends Object { $this->out("\n" . sprintf(__("Creating file %s", true), $path)); if (is_file($path) && $this->interactive === true) { $key = $this->in(__("File exists, overwrite?", true). " {$path}", array('y', 'n', 'q'), 'n'); - if (low($key) == 'q') { + if (strtolower($key) == 'q') { $this->out(__("Quitting.", true) ."\n"); exit; - } elseif (low($key) != 'y') { + } elseif (strtolower($key) != 'y') { $this->out(__("Skip", true) ." {$path}\n"); return false; } @@ -472,7 +472,7 @@ class Shell extends Object { return true; } $unitTest = $this->in('SimpleTest is not installed. Do you want to bake unit test files anyway?', array('y','n'), 'y'); - $result = low($unitTest) == 'y' || low($unitTest) == 'yes'; + $result = strtolower($unitTest) == 'y' || strtolower($unitTest) == 'yes'; if ($result) { $this->out("\nYou can download SimpleTest from http://simpletest.org", true); @@ -528,7 +528,7 @@ class Shell extends Object { * @access protected */ function _controllerPath($name) { - return low(Inflector::underscore($name)); + return strtolower(Inflector::underscore($name)); } /** * Creates the proper controller plural name for the specified controller class name diff --git a/cake/console/libs/tasks/controller.php b/cake/console/libs/tasks/controller.php index bb7928db2..2531dacca 100644 --- a/cake/console/libs/tasks/controller.php +++ b/cake/console/libs/tasks/controller.php @@ -120,7 +120,7 @@ class ControllerTask extends Shell { $this->out("Baking {$controllerName}Controller"); $this->hr(); - $controllerFile = low(Inflector::underscore($controllerName)); + $controllerFile = strtolower(Inflector::underscore($controllerName)); $question[] = __("Would you like to build your controller interactively?", true); if (file_exists($this->path . $controllerFile .'_controller.php')) { @@ -128,29 +128,29 @@ class ControllerTask extends Shell { } $doItInteractive = $this->in(join("\n", $question), array('y','n'), 'y'); - if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') { + if (strtolower($doItInteractive) == 'y' || strtolower($doItInteractive) == 'yes') { $this->interactive = true; $wannaUseScaffold = $this->in(__("Would you like to use scaffolding?", true), array('y','n'), 'n'); - if (low($wannaUseScaffold) == 'n' || low($wannaUseScaffold) == 'no') { + if (strtolower($wannaUseScaffold) == 'n' || strtolower($wannaUseScaffold) == 'no') { $wannaDoScaffolding = $this->in(__("Would you like to include some basic class methods (index(), add(), view(), edit())?", true), array('y','n'), 'n'); - if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') { + if (strtolower($wannaDoScaffolding) == 'y' || strtolower($wannaDoScaffolding) == 'yes') { $wannaDoAdmin = $this->in(__("Would you like to create the methods for admin routing?", true), array('y','n'), 'n'); } $wannaDoHelpers = $this->in(__("Would you like this controller to use other helpers besides HtmlHelper and FormHelper?", true), array('y','n'), 'n'); - if (low($wannaDoHelpers) == 'y' || low($wannaDoHelpers) == 'yes') { + if (strtolower($wannaDoHelpers) == 'y' || strtolower($wannaDoHelpers) == 'yes') { $helpersList = $this->in(__("Please provide a comma separated list of the other helper names you'd like to use.\nExample: 'Ajax, Javascript, Time'", true)); $helpersListTrimmed = str_replace(' ', '', $helpersList); $helpers = explode(',', $helpersListTrimmed); } $wannaDoComponents = $this->in(__("Would you like this controller to use any components?", true), array('y','n'), 'n'); - if (low($wannaDoComponents) == 'y' || low($wannaDoComponents) == 'yes') { + if (strtolower($wannaDoComponents) == 'y' || strtolower($wannaDoComponents) == 'yes') { $componentsList = $this->in(__("Please provide a comma separated list of the component names you'd like to use.\nExample: 'Acl, Security, RequestHandler'", true)); $componentsListTrimmed = str_replace(' ', '', $componentsList); $components = explode(',', $componentsListTrimmed); @@ -163,20 +163,20 @@ class ControllerTask extends Shell { } else { $wannaDoScaffolding = $this->in(__("Would you like to include some basic class methods (index(), add(), view(), edit())?", true), array('y','n'), 'y'); - if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') { + if (strtolower($wannaDoScaffolding) == 'y' || strtolower($wannaDoScaffolding) == 'yes') { $wannaDoAdmin = $this->in(__("Would you like to create the methods for admin routing?", true), array('y','n'), 'y'); } } $admin = false; - if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) { + if ((strtolower($wannaDoAdmin) == 'y' || strtolower($wannaDoAdmin) == 'yes')) { $admin = $this->getAdmin(); } - if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') { - $actions = $this->bakeActions($controllerName, null, in_array(low($wannaUseSession), array('y', 'yes'))); + if (strtolower($wannaDoScaffolding) == 'y' || strtolower($wannaDoScaffolding) == 'yes') { + $actions = $this->bakeActions($controllerName, null, in_array(strtolower($wannaUseSession), array('y', 'yes'))); if ($admin) { - $actions .= $this->bakeActions($controllerName, $admin, in_array(low($wannaUseSession), array('y', 'yes'))); + $actions .= $this->bakeActions($controllerName, $admin, in_array(strtolower($wannaUseSession), array('y', 'yes'))); } } @@ -187,7 +187,7 @@ class ControllerTask extends Shell { $this->hr(); $this->out("Controller Name: $controllerName"); - if (low($wannaUseScaffold) == 'y' || low($wannaUseScaffold) == 'yes') { + if (strtolower($wannaUseScaffold) == 'y' || strtolower($wannaUseScaffold) == 'yes') { $this->out(" var \$scaffold;"); $actions = 'scaffold'; } @@ -218,7 +218,7 @@ class ControllerTask extends Shell { $this->hr(); $looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y'); - if (low($looksGood) == 'y' || low($looksGood) == 'yes') { + if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') { $baked = $this->bake($controllerName, $actions, $helpers, $components, $uses); if ($baked && $this->_checkUnitTest()) { $this->bakeTest($controllerName); @@ -408,7 +408,7 @@ class ControllerTask extends Shell { $out .= "class $controllerName" . "Controller extends {$this->plugin}AppController {\n\n"; $out .= "\tvar \$name = '$controllerName';\n"; - if (low($actions) == 'scaffold') { + if (strtolower($actions) == 'scaffold') { $out .= "\tvar \$scaffold;\n"; } else { if (count($uses)) { diff --git a/cake/console/libs/tasks/db_config.php b/cake/console/libs/tasks/db_config.php index fcd71052e..d2f05393b 100644 --- a/cake/console/libs/tasks/db_config.php +++ b/cake/console/libs/tasks/db_config.php @@ -240,7 +240,7 @@ class DbConfigTask extends Shell { $this->hr(); $looksGood = $this->in('Look okay?', array('y', 'n'), 'y'); - if (low($looksGood) == 'y' || low($looksGood) == 'yes') { + if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') { return $config; } return false; diff --git a/cake/console/libs/tasks/model.php b/cake/console/libs/tasks/model.php index fc03da64b..2c98659da 100644 --- a/cake/console/libs/tasks/model.php +++ b/cake/console/libs/tasks/model.php @@ -112,11 +112,11 @@ class ModelTask extends Shell { $tableIsGood = $this->in(__('Do you want to use this table?', true), array('y','n'), 'y'); } - if (low($tableIsGood) == 'n' || low($tableIsGood) == 'no') { + if (strtolower($tableIsGood) == 'n' || strtolower($tableIsGood) == 'no') { $useTable = $this->in(__('What is the name of the table (enter "null" to use NO table)?', true)); } - while ($tableIsGood == false && low($useTable) != 'null') { + while ($tableIsGood == false && strtolower($useTable) != 'null') { if (is_array($this->__tables) && !in_array($useTable, $this->__tables)) { $fullTableName = $db->fullTableName($useTable, false); $this->out($fullTableName . ' does not exist.'); @@ -144,12 +144,12 @@ class ModelTask extends Shell { } } - if (array_search($useTable, $this->__tables) !== false && (low($wannaDoValidation) == 'y' || low($wannaDoValidation) == 'yes')) { + if (array_search($useTable, $this->__tables) !== false && (strtolower($wannaDoValidation) == 'y' || strtolower($wannaDoValidation) == 'yes')) { $validate = $this->doValidation($tempModel); } $wannaDoAssoc = $this->in(__('Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)?', true), array('y','n'), 'y'); - if ((low($wannaDoAssoc) == 'y' || low($wannaDoAssoc) == 'yes')) { + if ((strtolower($wannaDoAssoc) == 'y' || strtolower($wannaDoAssoc) == 'yes')) { $associations = $this->doAssociations($tempModel); } @@ -201,7 +201,7 @@ class ModelTask extends Shell { $this->hr(); $looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y'); - if (low($looksGood) == 'y' || low($looksGood) == 'yes') { + if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') { if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $useDbConfig)) { if ($this->_checkUnitTest()) { $this->bakeTest($currentModelName, $useTable, $associations); @@ -401,7 +401,7 @@ class ModelTask extends Shell { $prompt = "{$model->name} {$type} {$associations[$type][$i]['alias']}"; $response = $this->in("{$prompt}?", array('y','n'), 'y'); - if ('n' == low($response) || 'no' == low($response)) { + if ('n' == strtolower($response) || 'no' == strtolower($response)) { unset($associations[$type][$i]); } else { if ($model->name === $associations[$type][$i]['alias']) { @@ -414,7 +414,7 @@ class ModelTask extends Shell { $alternateAlias = $this->in(sprintf(__('This is a self join. Use %s as the alias', true), $alias), array('y', 'n'), 'y'); - if ('n' == low($alternateAlias) || 'no' == low($alternateAlias)) { + if ('n' == strtolower($alternateAlias) || 'no' == strtolower($alternateAlias)) { $associations[$type][$i]['alias'] = $this->in(__('Specify an alternate alias.', true)); } else { $associations[$type][$i]['alias'] = $alias; @@ -429,7 +429,7 @@ class ModelTask extends Shell { $wannaDoMoreAssoc = $this->in(__('Would you like to define some additional model associations?', true), array('y','n'), 'n'); - while ((low($wannaDoMoreAssoc) == 'y' || low($wannaDoMoreAssoc) == 'yes')) { + while ((strtolower($wannaDoMoreAssoc) == 'y' || strtolower($wannaDoMoreAssoc) == 'yes')) { $assocs = array(1 => 'belongsTo', 2 => 'hasOne', 3 => 'hasMany', 4 => 'hasAndBelongsToMany'); $bad = true; while ($bad) { diff --git a/cake/console/libs/tasks/plugin.php b/cake/console/libs/tasks/plugin.php index 1fc81374b..a23181322 100644 --- a/cake/console/libs/tasks/plugin.php +++ b/cake/console/libs/tasks/plugin.php @@ -136,7 +136,7 @@ class PluginTask extends Shell { $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y'); - if (low($looksGood) == 'y' || low($looksGood) == 'yes') { + if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') { $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n'); $Folder = new Folder($this->path . $pluginPath); @@ -146,7 +146,7 @@ class PluginTask extends Shell { $Folder->create($this->path . $pluginPath . DS . $directory); } - if (low($verbose) == 'y' || low($verbose) == 'yes') { + if (strtolower($verbose) == 'y' || strtolower($verbose) == 'yes') { foreach ($Folder->messages() as $message) { $this->out($message); } diff --git a/cake/console/libs/tasks/project.php b/cake/console/libs/tasks/project.php index 0502f8429..1d73e515f 100644 --- a/cake/console/libs/tasks/project.php +++ b/cake/console/libs/tasks/project.php @@ -144,7 +144,7 @@ class ProjectTask extends Shell { $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y'); - if (low($looksGood) == 'y' || low($looksGood) == 'yes') { + if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') { $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n'); $Folder = new Folder($skel); @@ -157,14 +157,14 @@ class ProjectTask extends Shell { return false; } - if (low($verbose) == 'y' || low($verbose) == 'yes') { + if (strtolower($verbose) == 'y' || strtolower($verbose) == 'yes') { foreach ($Folder->messages() as $message) { $this->out($message); } } return true; - } elseif (low($looksGood) == 'q' || low($looksGood) == 'quit') { + } elseif (strtolower($looksGood) == 'q' || strtolower($looksGood) == 'quit') { $this->out('Bake Aborted.'); } else { $this->execute(false); diff --git a/cake/console/libs/tasks/view.php b/cake/console/libs/tasks/view.php index 4dfdc5089..cd60df611 100644 --- a/cake/console/libs/tasks/view.php +++ b/cake/console/libs/tasks/view.php @@ -161,25 +161,25 @@ class ViewTask extends Shell { $this->controllerName = $this->Controller->getName(); - $this->controllerPath = low(Inflector::underscore($this->controllerName)); + $this->controllerPath = strtolower(Inflector::underscore($this->controllerName)); $interactive = $this->in("Would you like bake to build your views interactively?\nWarning: Choosing no will overwrite {$this->controllerName} views if it exist.", array('y','n'), 'y'); - if (low($interactive) == 'y' || low($interactive) == 'yes') { + if (strtolower($interactive) == 'y' || strtolower($interactive) == 'yes') { $this->interactive = true; $wannaDoScaffold = $this->in("Would you like to create some scaffolded views (index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller and model classes (including associated models).", array('y','n'), 'n'); } - if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') { + if (strtolower($wannaDoScaffold) == 'y' || strtolower($wannaDoScaffold) == 'yes') { $wannaDoAdmin = $this->in("Would you like to create the views for admin routing?", array('y','n'), 'y'); } $admin = false; - if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) { + if ((strtolower($wannaDoAdmin) == 'y' || strtolower($wannaDoAdmin) == 'yes')) { $admin = $this->getAdmin(); } - if (low($wannaDoScaffold) == 'y' || low($wannaDoScaffold) == 'yes') { + if (strtolower($wannaDoScaffold) == 'y' || strtolower($wannaDoScaffold) == 'yes') { $actions = $this->scaffoldActions; if ($admin) { foreach ($actions as $action) { @@ -213,7 +213,7 @@ class ViewTask extends Shell { $this->out("Path: ".$this->params['app'] . DS . $this->controllerPath . DS . Inflector::underscore($action) . ".ctp"); $this->hr(); $looksGood = $this->in('Look okay?', array('y','n'), 'y'); - if (low($looksGood) == 'y' || low($looksGood) == 'yes') { + if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') { $this->bake($action); $this->_stop(); } else { From 565cd96120dac6af170852f77c042f995f4660d7 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 24 Jul 2009 16:29:18 +0000 Subject: [PATCH 6/7] Fixing issue in CakeTestCase where fixture tables were incorrectly detected and dropped when running tests with no $test config or $test config and $default config on the same database. Updating TestAppsPostsController to fix compatibility issues with postgres. Defining $name in CakeTestFixture to remove notice errors, when constructing a nameless Fixture. Fixes #6518 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8254 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/tests/cases/libs/cake_test_case.test.php | 22 ++++++++++--------- cake/tests/lib/cake_test_case.php | 5 +++-- cake/tests/lib/cake_test_fixture.php | 9 ++++++-- .../tests_apps_posts_controller.php | 3 ++- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cake/tests/cases/libs/cake_test_case.test.php b/cake/tests/cases/libs/cake_test_case.test.php index 399106bc6..895eaaf40 100644 --- a/cake/tests/cases/libs/cake_test_case.test.php +++ b/cake/tests/cases/libs/cake_test_case.test.php @@ -219,6 +219,7 @@ class CakeTestCaseTest extends CakeTestCase { $this->Case->before('start'); $this->expectError(); $this->Case->loadFixtures('Wrong!'); + $this->Case->end(); } /** * testGetTests Method @@ -263,6 +264,10 @@ class CakeTestCaseTest extends CakeTestCase { $result = $this->Case->testAction('/tests_apps/set_action', array('return' => 'vars')); $this->assertEqual($result, array('var' => 'string')); + $db =& ConnectionManager::getDataSource('test_suite'); + $fixture =& new PostFixture(); + $fixture->create($db); + $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars')); $this->assertTrue(array_key_exists('posts', $result)); $this->assertEqual(count($result['posts']), 1); @@ -304,7 +309,7 @@ class CakeTestCaseTest extends CakeTestCase { ) )); $this->assertEqual(array_keys($result['data']), array('name', 'pork')); - + $fixture->drop($db); $db =& ConnectionManager::getDataSource('test_suite'); $_backPrefix = $db->config['prefix']; @@ -314,11 +319,11 @@ class CakeTestCaseTest extends CakeTestCase { $config['prefix'] = 'cake_testcase_test_'; ConnectionManager::create('cake_test_case', $config); - $db =& ConnectionManager::getDataSource('cake_test_case'); + $db2 =& ConnectionManager::getDataSource('cake_test_case'); - $fixture =& new PostFixture($db); - $fixture->create($db); - $fixture->insert($db); + $fixture =& new PostFixture($db2); + $fixture->create($db2); + $fixture->insert($db2); $result = $this->Case->testAction('/tests_apps_posts/fixtured', array( 'return' => 'vars', @@ -327,15 +332,12 @@ class CakeTestCaseTest extends CakeTestCase { )); $this->assertTrue(isset($result['posts'])); $this->assertEqual(count($result['posts']), 3); - $tables = $db->listSources(true); + $tables = $db2->listSources(); $this->assertFalse(in_array('cake_testaction_test_suite_posts', $tables)); - $fixture->drop($db); + $fixture->drop($db2); $db =& ConnectionManager::getDataSource('test_suite'); - $db->config['prefix'] = $_backPrefix; - $fixture->drop($db); - //test that drop tables behaves as exepected with testAction $db =& ConnectionManager::getDataSource('test_suite'); diff --git a/cake/tests/lib/cake_test_case.php b/cake/tests/lib/cake_test_case.php index f2a5fb442..f642e36a1 100644 --- a/cake/tests/lib/cake_test_case.php +++ b/cake/tests/lib/cake_test_case.php @@ -425,10 +425,11 @@ class CakeTestCase extends UnitTestCase { return; } foreach ($this->_fixtures as $fixture) { - if (in_array($fixture->table, $sources)) { + $table = $this->db->config['prefix'] . $fixture->table; + if (in_array($table, $sources)) { $fixture->drop($this->db); $fixture->create($this->db); - } elseif (!in_array($fixture->table, $sources)) { + } elseif (!in_array($table, $sources)) { $fixture->create($this->db); } } diff --git a/cake/tests/lib/cake_test_fixture.php b/cake/tests/lib/cake_test_fixture.php index acef3028c..cd12f9a32 100644 --- a/cake/tests/lib/cake_test_fixture.php +++ b/cake/tests/lib/cake_test_fixture.php @@ -31,6 +31,12 @@ * @subpackage cake.cake.tests.lib */ class CakeTestFixture extends Object { +/** + * Name of the object + * + * @var string + **/ + var $name = null; /** * Cake's DBO driver (e.g: DboMysql). * @@ -43,7 +49,6 @@ class CakeTestFixture extends Object { * @access public */ var $table = null; - /** * Instantiate the fixture. * @@ -185,4 +190,4 @@ class CakeTestFixture extends Object { return $return; } } -?> +?> \ No newline at end of file diff --git a/cake/tests/test_app/controllers/tests_apps_posts_controller.php b/cake/tests/test_app/controllers/tests_apps_posts_controller.php index e9b0d0464..c8ef7953f 100644 --- a/cake/tests/test_app/controllers/tests_apps_posts_controller.php +++ b/cake/tests/test_app/controllers/tests_apps_posts_controller.php @@ -33,7 +33,8 @@ class TestsAppsPostsController extends AppController { $data = array( 'Post' => array( 'title' => 'Test article', - 'body' => 'Body of article.' + 'body' => 'Body of article.', + 'author_id' => 1 ) ); $this->Post->save($data); From 9d7b5608178f2a0e690a74223e6b3f4ca0674305 Mon Sep 17 00:00:00 2001 From: jperras Date: Fri, 24 Jul 2009 22:17:14 +0000 Subject: [PATCH 7/7] Splitting model tests into separate files to allow more fine-grained test running. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8255 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/tests/cases/libs/model/model.test.php | 13427 +--------------- .../cases/libs/model/model_delete.test.php | 571 + .../libs/model/model_integration.test.php | 1837 +++ .../cases/libs/model/model_read.test.php | 7151 ++++++++ .../libs/model/model_validation.test.php | 126 + .../cases/libs/model/model_write.test.php | 3894 +++++ 6 files changed, 13581 insertions(+), 13425 deletions(-) create mode 100644 cake/tests/cases/libs/model/model_delete.test.php create mode 100644 cake/tests/cases/libs/model/model_integration.test.php create mode 100644 cake/tests/cases/libs/model/model_read.test.php create mode 100644 cake/tests/cases/libs/model/model_validation.test.php create mode 100644 cake/tests/cases/libs/model/model_write.test.php diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index cceabb035..ce87d8f62 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -1,7 +1,7 @@ loadFixtures('Article', 'Tag'); - $TestModel2 =& new ArticleB(); - $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id'); - } -/** - * Tests that $cacheSources can only be disabled in the db using model settings, not enabled - * - * @access public - * @return void - */ - function testCacheSourcesDisabling() { - $this->db->cacheSources = true; - $TestModel = new JoinA(); - $TestModel->cacheSources = false; - $TestModel->setSource('join_as'); - $this->assertFalse($this->db->cacheSources); - - $this->db->cacheSources = false; - $TestModel = new JoinA(); - $TestModel->cacheSources = true; - $TestModel->setSource('join_as'); - $this->assertFalse($this->db->cacheSources); - } -/** - * testPkInHabtmLinkModel method - * - * @access public - * @return void - */ - function testPkInHabtmLinkModel() { - //Test Nonconformant Models - $this->loadFixtures('Content', 'ContentAccount', 'Account'); - $TestModel =& new Content(); - $this->assertEqual($TestModel->ContentAccount->primaryKey, 'iContentAccountsId'); - - //test conformant models with no PK in the join table - $this->loadFixtures('Article', 'Tag'); - $TestModel2 =& new Article(); - $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id'); - - //test conformant models with PK in join table - $this->loadFixtures('Item', 'Portfolio', 'ItemsPortfolio'); - $TestModel3 =& new Portfolio(); - $this->assertEqual($TestModel3->ItemsPortfolio->primaryKey, 'id'); - - //test conformant models with PK in join table - join table contains extra field - $this->loadFixtures('JoinA', 'JoinB', 'JoinAB'); - $TestModel4 =& new JoinA(); - $this->assertEqual($TestModel4->JoinAsJoinB->primaryKey, 'id'); - - } -/** - * testDynamicBehaviorAttachment method - * - * @access public - * @return void - */ - function testDynamicBehaviorAttachment() { - $this->loadFixtures('Apple'); - $TestModel =& new Apple(); - $this->assertEqual($TestModel->Behaviors->attached(), array()); - - $TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field')); - $this->assertTrue(is_object($TestModel->Behaviors->Tree)); - $this->assertEqual($TestModel->Behaviors->attached(), array('Tree')); - - $expected = array( - 'parent' => 'parent_id', - 'left' => 'left_field', - 'right' => 'right_field', - 'scope' => '1 = 1', - 'type' => 'nested', - '__parentChange' => false, - 'recursive' => -1 - ); - - $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected); - - $expected['enabled'] = false; - $TestModel->Behaviors->attach('Tree', array('enabled' => false)); - $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected); - $this->assertEqual($TestModel->Behaviors->attached(), array('Tree')); - - $TestModel->Behaviors->detach('Tree'); - $this->assertEqual($TestModel->Behaviors->attached(), array()); - $this->assertFalse(isset($TestModel->Behaviors->Tree)); - } -/** - * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG - * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections, - * or one connection will step on the other. - */ - function testCrossDatabaseJoins() { - $config = new DATABASE_CONFIG(); - - $skip = $this->skipIf( - !isset($config->test) || !isset($config->test2), - '%s Primary and secondary test databases not configured, skipping cross-database ' - .'join tests.' - .' To run these tests, you must define $test and $test2 in your database configuration.' - ); - - if ($skip) { - return; - } - - $this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment'); - $TestModel =& new Article(); - - $expected = array( - array( - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - ), - array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - array( - 'id' => '4', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - )), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ))), - array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'Comment' => array( - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - )), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))), - array( - 'Article' => array( - 'id' => '3', - 'user_id' => '1', - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Comment' => array(), - 'Tag' => array() - )); - $this->assertEqual($TestModel->find('all'), $expected); - - $db2 =& ConnectionManager::getDataSource('test2'); - - foreach (array('User', 'Comment') as $class) { - $this->_fixtures[$this->_fixtureClassMap[$class]]->create($db2); - $this->_fixtures[$this->_fixtureClassMap[$class]]->insert($db2); - $this->db->truncate(Inflector::pluralize(Inflector::underscore($class))); - } - - $this->assertEqual($TestModel->User->find('all'), array()); - $this->assertEqual($TestModel->Comment->find('all'), array()); - $this->assertEqual($TestModel->find('count'), 3); - - $TestModel->User->setDataSource('test2'); - $TestModel->Comment->setDataSource('test2'); - - foreach ($expected as $key => $value) { - unset($value['Comment'], $value['Tag']); - $expected[$key] = $value; - } - - $TestModel->recursive = 0; - $result = $TestModel->find('all'); - $this->assertEqual($result, $expected); - - foreach ($expected as $key => $value) { - unset($value['Comment'], $value['Tag']); - $expected[$key] = $value; - } - - $TestModel->recursive = 0; - $result = $TestModel->find('all'); - $this->assertEqual($result, $expected); - - $result = Set::extract($TestModel->User->find('all'), '{n}.User.id'); - $this->assertEqual($result, array('1', '2', '3', '4')); - $this->assertEqual($TestModel->find('all'), $expected); - - $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment'))); - $expected = array( - array( - 'Comment' => array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - ), - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - array( - 'Comment' => array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - ), - 'User' => array( - 'id' => '4', - 'user' => 'garrett', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:22:23', - 'updated' => '2007-03-17 01:24:31' - ), - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - array( - 'Comment' => array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - array( - 'Comment' => array( - 'id' => '4', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - array( - 'Comment' => array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - )), - array( - 'Comment' => array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ), - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - ), - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ))); - $this->assertEqual($TestModel->Comment->find('all'), $expected); - - foreach (array('User', 'Comment') as $class) { - $this->_fixtures[$this->_fixtureClassMap[$class]]->drop($db2); - } - } -/** - * testDisplayField method - * - * @access public - * @return void - */ - function testDisplayField() { - $this->loadFixtures('Post', 'Comment', 'Person'); - $Post = new Post(); - $Comment = new Comment(); - $Person = new Person(); - - $this->assertEqual($Post->displayField, 'title'); - $this->assertEqual($Person->displayField, 'name'); - $this->assertEqual($Comment->displayField, 'id'); - } -/** - * testSchema method - * - * @access public - * @return void - */ - function testSchema() { - $Post = new Post(); - - $result = $Post->schema(); - $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated'); - $this->assertEqual(array_keys($result), $columns); - - $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime'); - $this->assertEqual(Set::extract(array_values($result), '{n}.type'), $types); - - $result = $Post->schema('body'); - $this->assertEqual($result['type'], 'text'); - $this->assertNull($Post->schema('foo')); - - $this->assertEqual($Post->getColumnTypes(), array_combine($columns, $types)); - } -/** - * test deconstruct() with time fields. - * - * @return void - **/ - function testDeconstructFieldsTime() { - $this->loadFixtures('Apple'); - $TestModel =& new Apple(); - - $data = array(); - $data['Apple']['mytime']['hour'] = ''; - $data['Apple']['mytime']['min'] = ''; - $data['Apple']['mytime']['sec'] = ''; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('mytime'=> '')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['mytime']['hour'] = ''; - $data['Apple']['mytime']['min'] = ''; - $data['Apple']['mytime']['meridan'] = ''; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('mytime'=> '')); - $this->assertEqual($TestModel->data, $expected, 'Empty values are not returning properly. %s'); - - $data = array(); - $data['Apple']['mytime']['hour'] = '12'; - $data['Apple']['mytime']['min'] = '0'; - $data['Apple']['mytime']['meridian'] = 'am'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('mytime'=> '00:00:00')); - $this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s'); - - $data = array(); - $data['Apple']['mytime']['hour'] = '00'; - $data['Apple']['mytime']['min'] = '00'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('mytime'=> '00:00:00')); - $this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s'); - - $data = array(); - $data['Apple']['mytime']['hour'] = '03'; - $data['Apple']['mytime']['min'] = '04'; - $data['Apple']['mytime']['sec'] = '04'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('mytime'=> '03:04:04')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['mytime']['hour'] = '3'; - $data['Apple']['mytime']['min'] = '4'; - $data['Apple']['mytime']['sec'] = '4'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple' => array('mytime'=> '03:04:04')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['mytime']['hour'] = '03'; - $data['Apple']['mytime']['min'] = '4'; - $data['Apple']['mytime']['sec'] = '4'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('mytime'=> '03:04:04')); - $this->assertEqual($TestModel->data, $expected); - - $db = ConnectionManager::getDataSource('test_suite'); - $data = array(); - $data['Apple']['mytime'] = $db->expression('NOW()'); - $TestModel->data = null; - $TestModel->set($data); - $this->assertEqual($TestModel->data, $data); - } -/** - * testDeconstructFields with datetime, timestamp, and date fields - * - * @access public - * @return void - */ - function testDeconstructFieldsDateTime() { - $this->loadFixtures('Apple'); - $TestModel =& new Apple(); - - //test null/empty values first - $data['Apple']['created']['year'] = ''; - $data['Apple']['created']['month'] = ''; - $data['Apple']['created']['day'] = ''; - $data['Apple']['created']['hour'] = ''; - $data['Apple']['created']['min'] = ''; - $data['Apple']['created']['sec'] = ''; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['date']['year'] = ''; - $data['Apple']['date']['month'] = ''; - $data['Apple']['date']['day'] = ''; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('date'=> '')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['year'] = '2007'; - $data['Apple']['created']['month'] = '08'; - $data['Apple']['created']['day'] = '20'; - $data['Apple']['created']['hour'] = ''; - $data['Apple']['created']['min'] = ''; - $data['Apple']['created']['sec'] = ''; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '2007-08-20 00:00:00')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['year'] = '2007'; - $data['Apple']['created']['month'] = '08'; - $data['Apple']['created']['day'] = '20'; - $data['Apple']['created']['hour'] = '10'; - $data['Apple']['created']['min'] = '12'; - $data['Apple']['created']['sec'] = ''; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '2007-08-20 10:12:00')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['year'] = '2007'; - $data['Apple']['created']['month'] = ''; - $data['Apple']['created']['day'] = '12'; - $data['Apple']['created']['hour'] = '20'; - $data['Apple']['created']['min'] = ''; - $data['Apple']['created']['sec'] = ''; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['hour'] = '20'; - $data['Apple']['created']['min'] = '33'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['hour'] = '20'; - $data['Apple']['created']['min'] = '33'; - $data['Apple']['created']['sec'] = '33'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['hour'] = '13'; - $data['Apple']['created']['min'] = '00'; - $data['Apple']['date']['year'] = '2006'; - $data['Apple']['date']['month'] = '12'; - $data['Apple']['date']['day'] = '25'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array( - 'Apple'=> array( - 'created'=> '', - 'date'=> '2006-12-25' - )); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['year'] = '2007'; - $data['Apple']['created']['month'] = '08'; - $data['Apple']['created']['day'] = '20'; - $data['Apple']['created']['hour'] = '10'; - $data['Apple']['created']['min'] = '12'; - $data['Apple']['created']['sec'] = '09'; - $data['Apple']['date']['year'] = '2006'; - $data['Apple']['date']['month'] = '12'; - $data['Apple']['date']['day'] = '25'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array( - 'Apple'=> array( - 'created'=> '2007-08-20 10:12:09', - 'date'=> '2006-12-25' - )); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['year'] = '--'; - $data['Apple']['created']['month'] = '--'; - $data['Apple']['created']['day'] = '--'; - $data['Apple']['created']['hour'] = '--'; - $data['Apple']['created']['min'] = '--'; - $data['Apple']['created']['sec'] = '--'; - $data['Apple']['date']['year'] = '--'; - $data['Apple']['date']['month'] = '--'; - $data['Apple']['date']['day'] = '--'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '', 'date'=> '')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['created']['year'] = '2007'; - $data['Apple']['created']['month'] = '--'; - $data['Apple']['created']['day'] = '20'; - $data['Apple']['created']['hour'] = '10'; - $data['Apple']['created']['min'] = '12'; - $data['Apple']['created']['sec'] = '09'; - $data['Apple']['date']['year'] = '2006'; - $data['Apple']['date']['month'] = '12'; - $data['Apple']['date']['day'] = '25'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('created'=> '', 'date'=> '2006-12-25')); - $this->assertEqual($TestModel->data, $expected); - - $data = array(); - $data['Apple']['date']['year'] = '2006'; - $data['Apple']['date']['month'] = '12'; - $data['Apple']['date']['day'] = '25'; - - $TestModel->data = null; - $TestModel->set($data); - $expected = array('Apple'=> array('date'=> '2006-12-25')); - $this->assertEqual($TestModel->data, $expected); - - $db = ConnectionManager::getDataSource('test_suite'); - $data = array(); - $data['Apple']['modified'] = $db->expression('NOW()'); - $TestModel->data = null; - $TestModel->set($data); - $this->assertEqual($TestModel->data, $data); - } -/** - * testTablePrefixSwitching method - * - * @access public - * @return void - */ - function testTablePrefixSwitching() { - ConnectionManager::create('database1', - array_merge($this->db->config, array('prefix' => 'aaa_') - )); - ConnectionManager::create('database2', - array_merge($this->db->config, array('prefix' => 'bbb_') - )); - - $db1 = ConnectionManager::getDataSource('database1'); - $db2 = ConnectionManager::getDataSource('database2'); - - $TestModel = new Apple(); - $TestModel->setDataSource('database1'); - $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples'); - $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples'); - $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples'); - - $TestModel->setDataSource('database2'); - $this->assertEqual($this->db->fullTableName($TestModel, false), 'bbb_apples'); - $this->assertEqual($db1->fullTableName($TestModel, false), 'bbb_apples'); - $this->assertEqual($db2->fullTableName($TestModel, false), 'bbb_apples'); - - $TestModel = new Apple(); - $TestModel->tablePrefix = 'custom_'; - $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples'); - $TestModel->setDataSource('database1'); - $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples'); - $this->assertEqual($db1->fullTableName($TestModel, false), 'custom_apples'); - - $TestModel = new Apple(); - $TestModel->setDataSource('database1'); - $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples'); - $TestModel->tablePrefix = ''; - $TestModel->setDataSource('database2'); - $this->assertEqual($db2->fullTableName($TestModel, false), 'apples'); - $this->assertEqual($db1->fullTableName($TestModel, false), 'apples'); - - $TestModel->tablePrefix = null; - $TestModel->setDataSource('database1'); - $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples'); - $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples'); - - $TestModel->tablePrefix = false; - $TestModel->setDataSource('database2'); - $this->assertEqual($db2->fullTableName($TestModel, false), 'apples'); - $this->assertEqual($db1->fullTableName($TestModel, false), 'apples'); - } -/** - * Tests validation parameter order in custom validation methods - * - * @access public - * @return void - */ - function testInvalidAssociation() { - $TestModel =& new ValidationTest1(); - $this->assertNull($TestModel->getAssociated('Foo')); - } -/** - * testLoadModelSecondIteration method - * - * @access public - * @return void - */ - function testLoadModelSecondIteration() { - $model = new ModelA(); - $this->assertIsA($model,'ModelA'); - - $this->assertIsA($model->ModelB, 'ModelB'); - $this->assertIsA($model->ModelB->ModelD, 'ModelD'); - - $this->assertIsA($model->ModelC, 'ModelC'); - $this->assertIsA($model->ModelC->ModelD, 'ModelD'); - } -/** - * ensure that __exists is reset on create - * - * @return void - **/ - function testResetOfExistsOnCreate() { - $this->loadFixtures('Article'); - $Article =& new Article(); - $Article->id = 1; - $Article->saveField('title', 'Reset me'); - $Article->delete(); - $Article->id = 1; - $this->assertFalse($Article->exists()); - - $Article->create(); - $this->assertFalse($Article->exists()); - $Article->id = 2; - $Article->saveField('title', 'Staying alive'); - $result = $Article->read(null, 2); - $this->assertEqual($result['Article']['title'], 'Staying alive'); - } -/** - * testPluginAssociations method - * - * @access public - * @return void - */ - function testPluginAssociations() { - $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment'); - $TestModel =& new TestPluginArticle(); - - $result = $TestModel->find('all'); - $expected = array( - array( - 'TestPluginArticle' => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Plugin Article', - 'body' => 'First Plugin Article Body', - 'published' => 'Y', - 'created' => '2008-09-24 10:39:23', - 'updated' => '2008-09-24 10:41:31' - ), - 'User' => array( - 'id' => 1, - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'TestPluginComment' => array( - array( - 'id' => 1, - 'article_id' => 1, - 'user_id' => 2, - 'comment' => 'First Comment for First Plugin Article', - 'published' => 'Y', - 'created' => '2008-09-24 10:45:23', - 'updated' => '2008-09-24 10:47:31' - ), - array( - 'id' => 2, - 'article_id' => 1, - 'user_id' => 4, - 'comment' => 'Second Comment for First Plugin Article', - 'published' => 'Y', - 'created' => '2008-09-24 10:47:23', - 'updated' => '2008-09-24 10:49:31' - ), - array( - 'id' => 3, - 'article_id' => 1, - 'user_id' => 1, - 'comment' => 'Third Comment for First Plugin Article', - 'published' => 'Y', - 'created' => '2008-09-24 10:49:23', - 'updated' => '2008-09-24 10:51:31' - ), - array( - 'id' => 4, - 'article_id' => 1, - 'user_id' => 1, - 'comment' => 'Fourth Comment for First Plugin Article', - 'published' => 'N', - 'created' => '2008-09-24 10:51:23', - 'updated' => '2008-09-24 10:53:31' - ))), - array( - 'TestPluginArticle' => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Plugin Article', - 'body' => 'Second Plugin Article Body', - 'published' => 'Y', - 'created' => '2008-09-24 10:41:23', - 'updated' => '2008-09-24 10:43:31' - ), - 'User' => array( - 'id' => 3, - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'TestPluginComment' => array( - array( - 'id' => 5, - 'article_id' => 2, - 'user_id' => 1, - 'comment' => 'First Comment for Second Plugin Article', - 'published' => 'Y', - 'created' => '2008-09-24 10:53:23', - 'updated' => '2008-09-24 10:55:31' - ), - array( - 'id' => 6, - 'article_id' => 2, - 'user_id' => 2, - 'comment' => 'Second Comment for Second Plugin Article', - 'published' => 'Y', - 'created' => '2008-09-24 10:55:23', - 'updated' => '2008-09-24 10:57:31' - ))), - array( - 'TestPluginArticle' => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Plugin Article', - 'body' => 'Third Plugin Article Body', - 'published' => 'Y', - 'created' => '2008-09-24 10:43:23', - 'updated' => '2008-09-24 10:45:31' - ), - 'User' => array( - 'id' => 1, - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'TestPluginComment' => array() - )); - - $this->assertEqual($result, $expected); - } -/** - * Tests getAssociated method - * - * @access public - * @return void - */ - function testGetAssociated() { - $this->loadFixtures('Article'); - $Article = ClassRegistry::init('Article'); - - $assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany'); - foreach ($assocTypes as $type) { - $this->assertEqual($Article->getAssociated($type), array_keys($Article->{$type})); - } - - $Article->bindModel(array('hasMany' => array('Category'))); - $this->assertEqual($Article->getAssociated('hasMany'), array('Comment', 'Category')); - - $results = $Article->getAssociated(); - $this->assertEqual(sort(array_keys($results)), array('Category', 'Comment', 'Tag')); - - $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag'))); - $this->assertEqual($Article->getAssociated('hasAndBelongsToMany'), array()); - - $result = $Article->getAssociated('Category'); - $expected = array( - 'className' => 'Category', - 'foreignKey' => 'article_id', - 'conditions' => '', - 'fields' => '', - 'order' => '', - 'limit' => '', - 'offset' => '', - 'dependent' => '', - 'exclusive' => '', - 'finderQuery' => '', - 'counterQuery' => '', - 'association' => 'hasMany', - ); - $this->assertEqual($result, $expected); - } - -/** - * testAutoConstructAssociations method - * - * @access public - * @return void - */ - function testAutoConstructAssociations() { - $this->loadFixtures('User', 'ArticleFeatured'); - $TestModel =& new AssociationTest1(); - - $result = $TestModel->hasAndBelongsToMany; - $expected = array('AssociationTest2' => array( - 'unique' => false, - 'joinTable' => 'join_as_join_bs', - 'foreignKey' => false, - 'className' => 'AssociationTest2', - 'with' => 'JoinAsJoinB', - 'associationForeignKey' => 'join_b_id', - 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', - 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '' - )); - $this->assertEqual($result, $expected); - - // Tests related to ticket https://trac.cakephp.org/ticket/5594 - $TestModel =& new ArticleFeatured(); - $TestFakeModel =& new ArticleFeatured(array('table' => false)); - - $expected = array( - 'User' => array( - 'className' => 'User', 'foreignKey' => 'user_id', - 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => '' - ), - 'Category' => array( - 'className' => 'Category', 'foreignKey' => 'category_id', - 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => '' - ) - ); - $this->assertIdentical($TestModel->belongsTo, $expected); - $this->assertIdentical($TestFakeModel->belongsTo, $expected); - - $this->assertEqual($TestModel->User->name, 'User'); - $this->assertEqual($TestFakeModel->User->name, 'User'); - $this->assertEqual($TestModel->Category->name, 'Category'); - $this->assertEqual($TestFakeModel->Category->name, 'Category'); - - $expected = array( - 'Featured' => array( - 'className' => 'Featured', - 'foreignKey' => 'article_featured_id', - 'conditions' => '', - 'fields' => '', - 'order' => '', - 'dependent' => '' - )); - - $this->assertIdentical($TestModel->hasOne, $expected); - $this->assertIdentical($TestFakeModel->hasOne, $expected); - - $this->assertEqual($TestModel->Featured->name, 'Featured'); - $this->assertEqual($TestFakeModel->Featured->name, 'Featured'); - - $expected = array( - 'Comment' => array( - 'className' => 'Comment', - 'dependent' => true, - 'foreignKey' => 'article_featured_id', - 'conditions' => '', - 'fields' => '', - 'order' => '', - 'limit' => '', - 'offset' => '', - 'exclusive' => '', - 'finderQuery' => '', - 'counterQuery' => '' - )); - - $this->assertIdentical($TestModel->hasMany, $expected); - $this->assertIdentical($TestFakeModel->hasMany, $expected); - - $this->assertEqual($TestModel->Comment->name, 'Comment'); - $this->assertEqual($TestFakeModel->Comment->name, 'Comment'); - - $expected = array( - 'Tag' => array( - 'className' => 'Tag', - 'joinTable' => 'article_featureds_tags', - 'with' => 'ArticleFeaturedsTag', - 'foreignKey' => 'article_featured_id', - 'associationForeignKey' => 'tag_id', - 'conditions' => '', - 'fields' => '', - 'order' => '', - 'limit' => '', - 'offset' => '', - 'unique' => true, - 'finderQuery' => '', - 'deleteQuery' => '', - 'insertQuery' => '' - )); - - $this->assertIdentical($TestModel->hasAndBelongsToMany, $expected); - $this->assertIdentical($TestFakeModel->hasAndBelongsToMany, $expected); - - $this->assertEqual($TestModel->Tag->name, 'Tag'); - $this->assertEqual($TestFakeModel->Tag->name, 'Tag'); - } -/** - * test Model::__construct - * - * ensure that $actsAS and $_findMethods are merged. - * - * @return void - **/ - function testConstruct() { - $this->loadFixtures('Post', 'Comment'); - - $TestModel =& ClassRegistry::init('MergeVarPluginPost'); - $this->assertEqual($TestModel->actsAs, array('Containable', 'Tree')); - $this->assertTrue(isset($TestModel->Behaviors->Containable)); - $this->assertTrue(isset($TestModel->Behaviors->Tree)); - - $TestModel =& ClassRegistry::init('MergeVarPluginComment'); - $expected = array('Containable', 'Containable' => array('some_settings')); - $this->assertEqual($TestModel->actsAs, $expected); - $this->assertTrue(isset($TestModel->Behaviors->Containable)); - } -/** - * test Model::__construct - * - * ensure that $actsAS and $_findMethods are merged. - * - * @return void - **/ - function testConstructWithAlternateDataSource() { - $TestModel =& ClassRegistry::init(array( - 'class' => 'DoesntMatter', 'ds' => 'test_suite', 'table' => false - )); - $this->assertEqual('test_suite', $TestModel->useDbConfig); - - //deprecated but test it anyway - $NewVoid =& new TheVoid(null, false, 'other'); - $this->assertEqual('other', $NewVoid->useDbConfig); - } -/** - * testColumnTypeFetching method - * - * @access public - * @return void - */ - function testColumnTypeFetching() { - $model =& new Test(); - $this->assertEqual($model->getColumnType('id'), 'integer'); - $this->assertEqual($model->getColumnType('notes'), 'text'); - $this->assertEqual($model->getColumnType('updated'), 'datetime'); - $this->assertEqual($model->getColumnType('unknown'), null); - - $model =& new Article(); - $this->assertEqual($model->getColumnType('User.created'), 'datetime'); - $this->assertEqual($model->getColumnType('Tag.id'), 'integer'); - $this->assertEqual($model->getColumnType('Article.id'), 'integer'); - } -/** - * testHabtmUniqueKey method - * - * @access public - * @return void - */ - function testHabtmUniqueKey() { - $model =& new Item(); - $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']); - } -/** - * testIdentity method - * - * @access public - * @return void - */ - function testIdentity() { - $TestModel =& new Test(); - $result = $TestModel->alias; - $expected = 'Test'; - $this->assertEqual($result, $expected); - - $TestModel =& new TestAlias(); - $result = $TestModel->alias; - $expected = 'TestAlias'; - $this->assertEqual($result, $expected); - - $TestModel =& new Test(array('alias' => 'AnotherTest')); - $result = $TestModel->alias; - $expected = 'AnotherTest'; - $this->assertEqual($result, $expected); - } -/** - * testWithAssociation method - * - * @access public - * @return void - */ - function testWithAssociation() { - $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); - $TestModel =& new Something(); - $result = $TestModel->SomethingElse->find('all'); - - $expected = array( - array( - 'SomethingElse' => array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'Something' => array( - array( - 'id' => '3', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31', - 'JoinThing' => array( - 'id' => '3', - 'something_id' => '3', - 'something_else_id' => '1', - 'doomed' => '1', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - )))), - array( - 'SomethingElse' => array( - 'id' => '2', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'Something' => array( - array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31', - 'JoinThing' => array( - 'id' => '1', - 'something_id' => '1', - 'something_else_id' => '2', - 'doomed' => '1', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )))), - array( - 'SomethingElse' => array( - 'id' => '3', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'Something' => array( - array( - 'id' => '2', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31', - 'JoinThing' => array( - 'id' => '2', - 'something_id' => '2', - 'something_else_id' => '3', - 'doomed' => '0', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ))))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all'); - $expected = array( - array( - 'Something' => array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'SomethingElse' => array( - array( - 'id' => '2', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31', - 'JoinThing' => array( - 'doomed' => '1', - 'something_id' => '1', - 'something_else_id' => '2' - )))), - array( - 'Something' => array( - 'id' => '2', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'SomethingElse' => array( - array( - 'id' => '3', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31', - 'JoinThing' => array( - 'doomed' => '0', - 'something_id' => '2', - 'something_else_id' => '3' - )))), - array( - 'Something' => array( - 'id' => '3', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'SomethingElse' => array( - array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31', - 'JoinThing' => array( - 'doomed' => '1', - 'something_id' => '3', - 'something_else_id' => '1' - ))))); - $this->assertEqual($result, $expected); - - $result = $TestModel->findById(1); - $expected = array( - 'Something' => array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'SomethingElse' => array( - array( - 'id' => '2', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31', - 'JoinThing' => array( - 'doomed' => '1', - 'something_id' => '1', - 'something_else_id' => '2' - )))); - $this->assertEqual($result, $expected); - - $expected = $TestModel->findById(1); - $TestModel->set($expected); - $TestModel->save(); - $result = $TestModel->findById(1); - $this->assertEqual($result, $expected); - - $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false; - $TestModel->create(array( - 'Something' => array('id' => 1), - 'SomethingElse' => array(3, array( - 'something_else_id' => 1, - 'doomed' => '1' - )))); - - $ts = date('Y-m-d H:i:s'); - $TestModel->save(); - - $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC'; - $result = $TestModel->findById(1); - $expected = array( - 'Something' => array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => $ts), - 'SomethingElse' => array( - array( - 'id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31', - 'JoinThing' => array( - 'doomed' => '1', - 'something_id' => '1', - 'something_else_id' => '1' - )), - array( - 'id' => '2', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31', - 'JoinThing' => array( - 'doomed' => '1', - 'something_id' => '1', - 'something_else_id' => '2' - )), - array( - 'id' => '3', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31', - 'JoinThing' => array( - 'doomed' => '0', - 'something_id' => '1', - 'something_else_id' => '3' - )))); - - $this->assertEqual($result, $expected); - } -/** - * testFindSelfAssociations method - * - * @access public - * @return void - */ - function testFindSelfAssociations() { - $this->loadFixtures('Person'); - - $TestModel =& new Person(); - $TestModel->recursive = 2; - $result = $TestModel->read(null, 1); - $expected = array( - 'Person' => array( - 'id' => 1, - 'name' => 'person', - 'mother_id' => 2, - 'father_id' => 3 - ), - 'Mother' => array( - 'id' => 2, - 'name' => 'mother', - 'mother_id' => 4, - 'father_id' => 5, - 'Mother' => array( - 'id' => 4, - 'name' => 'mother - grand mother', - 'mother_id' => 0, - 'father_id' => 0 - ), - 'Father' => array( - 'id' => 5, - 'name' => 'mother - grand father', - 'mother_id' => 0, - 'father_id' => 0 - )), - 'Father' => array( - 'id' => 3, - 'name' => 'father', - 'mother_id' => 6, - 'father_id' => 7, - 'Father' => array( - 'id' => 7, - 'name' => 'father - grand father', - 'mother_id' => 0, - 'father_id' => 0 - ), - 'Mother' => array( - 'id' => 6, - 'name' => 'father - grand mother', - 'mother_id' => 0, - 'father_id' => 0 - ))); - - $this->assertEqual($result, $expected); - - $TestModel->recursive = 3; - $result = $TestModel->read(null, 1); - $expected = array( - 'Person' => array( - 'id' => 1, - 'name' => 'person', - 'mother_id' => 2, - 'father_id' => 3 - ), - 'Mother' => array( - 'id' => 2, - 'name' => 'mother', - 'mother_id' => 4, - 'father_id' => 5, - 'Mother' => array( - 'id' => 4, - 'name' => 'mother - grand mother', - 'mother_id' => 0, - 'father_id' => 0, - 'Mother' => array(), - 'Father' => array()), - 'Father' => array( - 'id' => 5, - 'name' => 'mother - grand father', - 'mother_id' => 0, - 'father_id' => 0, - 'Father' => array(), - 'Mother' => array() - )), - 'Father' => array( - 'id' => 3, - 'name' => 'father', - 'mother_id' => 6, - 'father_id' => 7, - 'Father' => array( - 'id' => 7, - 'name' => 'father - grand father', - 'mother_id' => 0, - 'father_id' => 0, - 'Father' => array(), - 'Mother' => array() - ), - 'Mother' => array( - 'id' => 6, - 'name' => 'father - grand mother', - 'mother_id' => 0, - 'father_id' => 0, - 'Mother' => array(), - 'Father' => array() - ))); - - $this->assertEqual($result, $expected); - } -/** - * testDynamicAssociations method - * - * @access public - * @return void - */ - function testDynamicAssociations() { - $this->loadFixtures('Article', 'Comment'); - $TestModel =& new Article(); - - $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array(); - $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array( - 'foreignKey' => false, - 'conditions' => array('Comment.user_id =' => '2') - )); - $result = $TestModel->find('all'); - $expected = array( - array( - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ))), - array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ))), - array( - 'Article' => array( - 'id' => '3', - 'user_id' => '1', - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - )))); - - $this->assertEqual($result, $expected); - } -/** - * testCreation method - * - * @access public - * @return void - */ - function testCreation() { - $this->loadFixtures('Article'); - $TestModel =& new Test(); - $result = $TestModel->create(); - $expected = array('Test' => array('notes' => 'write some notes here')); - $this->assertEqual($result, $expected); - $TestModel =& new User(); - $result = $TestModel->schema(); - - if (isset($this->db->columns['primary_key']['length'])) { - $intLength = $this->db->columns['primary_key']['length']; - } elseif (isset($this->db->columns['integer']['length'])) { - $intLength = $this->db->columns['integer']['length']; - } else { - $intLength = 11; - } - foreach (array('collate', 'charset') as $type) { - unset($result['user'][$type]); - unset($result['password'][$type]); - } - - $expected = array( - 'id' => array( - 'type' => 'integer', - 'null' => false, - 'default' => null, - 'length' => $intLength, - 'key' => 'primary' - ), - 'user' => array( - 'type' => 'string', - 'null' => false, - 'default' => '', - 'length' => 255 - ), - 'password' => array( - 'type' => 'string', - 'null' => false, - 'default' => '', - 'length' => 255 - ), - 'created' => array( - 'type' => 'datetime', - 'null' => true, - 'default' => null, - 'length' => null - ), - 'updated'=> array( - 'type' => 'datetime', - 'null' => true, - 'default' => null, - 'length' => null - )); - - $this->assertEqual($result, $expected); - - $TestModel =& new Article(); - $result = $TestModel->create(); - $expected = array('Article' => array('published' => 'N')); - $this->assertEqual($result, $expected); - - $FeaturedModel =& new Featured(); - $data = array( - 'article_featured_id' => 1, - 'category_id' => 1, - 'published_date' => array( - 'year' => 2008, - 'month' => 06, - 'day' => 11 - ), - 'end_date' => array( - 'year' => 2008, - 'month' => 06, - 'day' => 20 - )); - - $expected = array( - 'Featured' => array( - 'article_featured_id' => 1, - 'category_id' => 1, - 'published_date' => '2008-6-11 00:00:00', - 'end_date' => '2008-6-20 00:00:00' - )); - - $this->assertEqual($FeaturedModel->create($data), $expected); - - $data = array( - 'published_date' => array( - 'year' => 2008, - 'month' => 06, - 'day' => 11 - ), - 'end_date' => array( - 'year' => 2008, - 'month' => 06, - 'day' => 20 - ), - 'article_featured_id' => 1, - 'category_id' => 1 - ); - - $expected = array( - 'Featured' => array( - 'published_date' => '2008-6-11 00:00:00', - 'end_date' => '2008-6-20 00:00:00', - 'article_featured_id' => 1, - 'category_id' => 1 - )); - - $this->assertEqual($FeaturedModel->create($data), $expected); - } -} -/** - * ModelFindTest - * - * @package cake - * @subpackage cake.tests.cases.libs.model - */ -class ModelReadTest extends BaseModelTest { -/** - * testFetchingNonUniqueFKJoinTableRecords() - * - * Tests if the results are properly returned in the case there are non-unique FK's - * in the join table but another fields value is different. For example: - * something_id | something_else_id | doomed = 1 - * something_id | something_else_id | doomed = 0 - * Should return both records and not just one. - * - * @access public - * @return void - */ - function testFetchingNonUniqueFKJoinTableRecords() { - $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); - $Something = new Something(); - - $joinThingData = array( - 'JoinThing' => array( - 'something_id' => 1, - 'something_else_id' => 2, - 'doomed' => '0', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ) - ); - $Something->JoinThing->create($joinThingData); - $Something->JoinThing->save(); - - $result = $Something->JoinThing->find('all', array('conditions' => array('something_else_id' => 2))); - $this->assertEqual($result[0]['JoinThing']['doomed'], 1); - $this->assertEqual($result[1]['JoinThing']['doomed'], 0); - - $result = $Something->find('first'); - $this->assertEqual(count($result['SomethingElse']), 2); - $this->assertEqual($result['SomethingElse'][0]['JoinThing']['doomed'], 1); - $this->assertEqual($result['SomethingElse'][1]['JoinThing']['doomed'], 0); - } -/** - * testGroupBy method - * - * These tests will never pass with Postgres or Oracle as all fields in a select must be - * part of an aggregate function or in the GROUP BY statement. - * - * @access public - * @return void - */ - function testGroupBy() { - $db = ConnectionManager::getDataSource('test_suite'); - $isStrictGroupBy = in_array($db->config['driver'], array('postgres', 'oracle')); - $message = '%s Postgresql and Oracle have strict GROUP BY and are incompatible with this test.'; - - if ($this->skipIf($isStrictGroupBy, $message )) { - return; - } - - $this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid'); - $Thread =& new Thread(); - $Product =& new Product(); - - $result = $Thread->find('all', array( - 'group' => 'Thread.project_id', - 'order' => 'Thread.id ASC' - )); - - $expected = array( - array( - 'Thread' => array( - 'id' => 1, - 'project_id' => 1, - 'name' => 'Project 1, Thread 1' - ), - 'Project' => array( - 'id' => 1, - 'name' => 'Project 1' - ), - 'Message' => array( - array( - 'id' => 1, - 'thread_id' => 1, - 'name' => 'Thread 1, Message 1' - ))), - array( - 'Thread' => array( - 'id' => 3, - 'project_id' => 2, - 'name' => 'Project 2, Thread 1' - ), - 'Project' => array( - 'id' => 2, - 'name' => 'Project 2' - ), - 'Message' => array( - array( - 'id' => 3, - 'thread_id' => 3, - 'name' => 'Thread 3, Message 1' - )))); - $this->assertEqual($result, $expected); - - $rows = $Thread->find('all', array( - 'group' => 'Thread.project_id', - 'fields' => array('Thread.project_id', 'COUNT(*) AS total') - )); - $result = array(); - foreach($rows as $row) { - $result[$row['Thread']['project_id']] = $row[0]['total']; - } - $expected = array( - 1 => 2, - 2 => 1 - ); - $this->assertEqual($result, $expected); - - $rows = $Thread->find('all', array( - 'group' => 'Thread.project_id', - 'fields' => array('Thread.project_id', 'COUNT(*) AS total'), - 'order'=> 'Thread.project_id' - )); - $result = array(); - foreach($rows as $row) { - $result[$row['Thread']['project_id']] = $row[0]['total']; - } - $expected = array( - 1 => 2, - 2 => 1 - ); - $this->assertEqual($result, $expected); - - $result = $Thread->find('all', array( - 'conditions' => array('Thread.project_id' => 1), - 'group' => 'Thread.project_id' - )); - $expected = array( - array( - 'Thread' => array( - 'id' => 1, - 'project_id' => 1, - 'name' => 'Project 1, Thread 1' - ), - 'Project' => array( - 'id' => 1, - 'name' => 'Project 1' - ), - 'Message' => array( - array( - 'id' => 1, - 'thread_id' => 1, - 'name' => 'Thread 1, Message 1' - )))); - $this->assertEqual($result, $expected); - - $result = $Thread->find('all', array( - 'conditions' => array('Thread.project_id' => 1), - 'group' => 'Thread.project_id, Project.id' - )); - $this->assertEqual($result, $expected); - - $result = $Thread->find('all', array( - 'conditions' => array('Thread.project_id' => 1), - 'group' => 'project_id' - )); - $this->assertEqual($result, $expected); - - - $result = $Thread->find('all', array( - 'conditions' => array('Thread.project_id' => 1), - 'group' => array('project_id') - )); - $this->assertEqual($result, $expected); - - - $result = $Thread->find('all', array( - 'conditions' => array('Thread.project_id' => 1), - 'group' => array('project_id', 'Project.id') - )); - $this->assertEqual($result, $expected); - - - $result = $Thread->find('all', array( - 'conditions' => array('Thread.project_id' => 1), - 'group' => array('Thread.project_id', 'Project.id') - )); - $this->assertEqual($result, $expected); - - - $expected = array( - array('Product' => array('type' => 'Clothing'), array('price' => 32)), - array('Product' => array('type' => 'Food'), array('price' => 9)), - array('Product' => array('type' => 'Music'), array( 'price' => 4)), - array('Product' => array('type' => 'Toy'), array('price' => 3)) - ); - $result = $Product->find('all',array( - 'fields'=>array('Product.type','MIN(Product.price) as price'), - 'group'=> 'Product.type', - 'order' => 'Product.type ASC' - )); - $this->assertEqual($result, $expected); - - $result = $Product->find('all', array( - 'fields'=>array('Product.type','MIN(Product.price) as price'), - 'group'=> array('Product.type'), - 'order' => 'Product.type ASC')); - $this->assertEqual($result, $expected); - } -/** - * testOldQuery method - * - * @access public - * @return void - */ - function testOldQuery() { - $this->loadFixtures('Article'); - $Article =& new Article(); - - $query = 'SELECT title FROM '; - $query .= $this->db->fullTableName('articles'); - $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id IN (1,2)'; - - $results = $Article->query($query); - $this->assertTrue(is_array($results)); - $this->assertEqual(count($results), 2); - - $query = 'SELECT title, body FROM '; - $query .= $this->db->fullTableName('articles'); - $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id = 1'; - - $results = $Article->query($query, false); - $this->assertTrue(!isset($this->db->_queryCache[$query])); - $this->assertTrue(is_array($results)); - - $query = 'SELECT title, id FROM '; - $query .= $this->db->fullTableName('articles'); - $query .= ' WHERE ' . $this->db->fullTableName('articles'); - $query .= '.published = ' . $this->db->value('Y'); - - $results = $Article->query($query, true); - $this->assertTrue(isset($this->db->_queryCache[$query])); - $this->assertTrue(is_array($results)); - } -/** - * testPreparedQuery method - * - * @access public - * @return void - */ - function testPreparedQuery() { - $this->loadFixtures('Article'); - $Article =& new Article(); - $this->db->_queryCache = array(); - - $finalQuery = 'SELECT title, published FROM '; - $finalQuery .= $this->db->fullTableName('articles'); - $finalQuery .= ' WHERE ' . $this->db->fullTableName('articles'); - $finalQuery .= '.id = ' . $this->db->value(1); - $finalQuery .= ' AND ' . $this->db->fullTableName('articles'); - $finalQuery .= '.published = ' . $this->db->value('Y'); - - $query = 'SELECT title, published FROM '; - $query .= $this->db->fullTableName('articles'); - $query .= ' WHERE ' . $this->db->fullTableName('articles'); - $query .= '.id = ? AND ' . $this->db->fullTableName('articles') . '.published = ?'; - - $params = array(1, 'Y'); - $result = $Article->query($query, $params); - $expected = array( - '0' => array( - $this->db->fullTableName('articles', false) => array( - 'title' => 'First Article', 'published' => 'Y') - )); - - if (isset($result[0][0])) { - $expected[0][0] = $expected[0][$this->db->fullTableName('articles', false)]; - unset($expected[0][$this->db->fullTableName('articles', false)]); - } - - $this->assertEqual($result, $expected); - $this->assertTrue(isset($this->db->_queryCache[$finalQuery])); - - $finalQuery = 'SELECT id, created FROM '; - $finalQuery .= $this->db->fullTableName('articles'); - $finalQuery .= ' WHERE ' . $this->db->fullTableName('articles'); - $finalQuery .= '.title = ' . $this->db->value('First Article'); - - $query = 'SELECT id, created FROM '; - $query .= $this->db->fullTableName('articles'); - $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title = ?'; - - $params = array('First Article'); - $result = $Article->query($query, $params, false); - $this->assertTrue(is_array($result)); - $this->assertTrue( - isset($result[0][$this->db->fullTableName('articles', false)]) - || isset($result[0][0]) - ); - $this->assertFalse(isset($this->db->_queryCache[$finalQuery])); - - $query = 'SELECT title FROM '; - $query .= $this->db->fullTableName('articles'); - $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title LIKE ?'; - - $params = array('%First%'); - $result = $Article->query($query, $params); - $this->assertTrue(is_array($result)); - $this->assertTrue( - isset($result[0][$this->db->fullTableName('articles', false)]['title']) - || isset($result[0][0]['title']) - ); - - //related to ticket #5035 - $query = 'SELECT title FROM '; - $query .= $this->db->fullTableName('articles') . ' WHERE title = ? AND published = ?'; - $params = array('First? Article', 'Y'); - $Article->query($query, $params); - - $expected = 'SELECT title FROM '; - $expected .= $this->db->fullTableName('articles'); - $expected .= " WHERE title = 'First? Article' AND published = 'Y'"; - $this->assertTrue(isset($this->db->_queryCache[$expected])); - - } -/** - * testParameterMismatch method - * - * @access public - * @return void - */ - function testParameterMismatch() { - $this->loadFixtures('Article'); - $Article =& new Article(); - - $query = 'SELECT * FROM ' . $this->db->fullTableName('articles'); - $query .= ' WHERE ' . $this->db->fullTableName('articles'); - $query .= '.published = ? AND ' . $this->db->fullTableName('articles') . '.user_id = ?'; - $params = array('Y'); - $this->expectError(); - - ob_start(); - $result = $Article->query($query, $params); - ob_end_clean(); - $this->assertEqual($result, null); - } -/** - * testVeryStrangeUseCase method - * - * @access public - * @return void - */ - function testVeryStrangeUseCase() { - $message = "%s skipping SELECT * FROM ? WHERE ? = ? AND ? = ?; prepared query."; - $message .= " MSSQL is incompatible with this test."; - - if ($this->skipIf($this->db->config['driver'] == 'mssql', $message)) { - return; - } - - $this->loadFixtures('Article'); - $Article =& new Article(); - - $query = 'SELECT * FROM ? WHERE ? = ? AND ? = ?'; - $param = array( - $this->db->fullTableName('articles'), - $this->db->fullTableName('articles') . '.user_id', '3', - $this->db->fullTableName('articles') . '.published', 'Y' - ); - $this->expectError(); - - ob_start(); - $result = $Article->query($query, $param); - ob_end_clean(); - } -/** - * testRecursiveUnbind method - * - * @access public - * @return void - */ - function testRecursiveUnbind() { - $this->loadFixtures('Apple', 'Sample'); - $TestModel =& new Apple(); - $TestModel->recursive = 2; - - $result = $TestModel->find('all'); - $expected = array( - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' =>'', - 'apple_id' => '', - 'name' => '' - ), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2', - 'Apple' => 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' - )), - 'Child' => 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 1, - 'apple_id' => 3, - 'name' => 'sample1' - )), - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3' - ), - 'Child' => array( - 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 1, - 'apple_id' => 3, - 'name' => 'sample1', - 'Apple' => 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' - )), - 'Child' => array() - ), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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'), - 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3', - 'Apple' => 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' - )), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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' - ))))), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4', - 'Apple' => 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' - )), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4' - ), - 'Child' => array( - 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - ), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array() - ))), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - ), - 'Child' => array())); - $this->assertEqual($result, $expected); - - $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample'))); - $this->assertTrue($result); - - $result = $TestModel->find('all'); - $expected = array( - array( - 'Apple' => 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'), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' =>'', - 'apple_id' => '', - 'name' => '' - ), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2', - 'Apple' => 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' - )), - 'Child' => 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 1, - 'apple_id' => 3, - 'name' => 'sample1' - )), - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3' - ), - 'Child' => array( - 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 1, - 'apple_id' => 3, - 'name' => 'sample1', - 'Apple' => 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' - )), - 'Child' => array() - ), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3', - 'Apple' => 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' - )), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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' - ))))), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4', - 'Apple' => 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' - )), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4' - ), - 'Child' => array( - 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - ), - 'Child' => array( - 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', - 'Parent' => 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' - ), - 'Sample' => array() - ))), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - ), - 'Child' => array() - )); - - $this->assertEqual($result, $expected); - - $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample'))); - $this->assertTrue($result); - - $result = $TestModel->unbindModel(array('hasMany' => array('Child'))); - $this->assertTrue($result); - - $result = $TestModel->find('all'); - $expected = array( - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' =>'', - 'apple_id' => '', - 'name' => '' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2', - 'Apple' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 1, - 'apple_id' => 3, - 'name' => 'sample1', - 'Apple' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3', - 'Apple' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4', - 'Apple' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - ))); - - $this->assertEqual($result, $expected); - - $result = $TestModel->unbindModel(array('hasMany' => array('Child'))); - $this->assertTrue($result); - - $result = $TestModel->Sample->unbindModel(array('belongsTo' => array('Apple'))); - $this->assertTrue($result); - - $result = $TestModel->find('all'); - $expected = array( - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' =>'', - 'apple_id' => '', - 'name' => '' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 1, - 'apple_id' => 3, - 'name' => 'sample1' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Parent' => 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' - ), - 'Sample' => array(), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->Parent->unbindModel(array('belongsTo' => array('Parent'))); - $this->assertTrue($result); - - $result = $TestModel->unbindModel(array('hasMany' => array('Child'))); - $this->assertTrue($result); - - $result = $TestModel->find('all'); - $expected = array( - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' =>'', - 'apple_id' => '', - 'name' => '' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Sample' => array(), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2', - 'Apple' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 1, - 'apple_id' => 3, - 'name' => 'sample1', - 'Apple' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Sample' => array( - 'id' => 2, - 'apple_id' => 2, - 'name' => 'sample2' - ), - 'Child' => 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' => 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' - ))), - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3', - 'Apple' => 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( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => 4, - 'apple_id' => 5, - 'name' => 'sample4', - 'Apple' => 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( - 'Apple' => 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'), - 'Parent' => 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', - 'Sample' => array( - 'id' => 3, - 'apple_id' => 4, - 'name' => 'sample3' - ), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - )), - array( - 'Apple' => 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' - ), - 'Parent' => 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', - 'Sample' => array(), - 'Child' => array( - 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' - ))), - 'Sample' => array( - 'id' => '', - 'apple_id' => '', - 'name' => '' - ))); - $this->assertEqual($result, $expected); - } -/** - * testSelfAssociationAfterFind method - * - * @access public - * @return void - */ - function testSelfAssociationAfterFind() { - $this->loadFixtures('Apple'); - $afterFindModel = new NodeAfterFind(); - $afterFindModel->recursive = 3; - $afterFindData = $afterFindModel->find('all'); - - $duplicateModel = new NodeAfterFind(); - $duplicateModel->recursive = 3; - $duplicateModelData = $duplicateModel->find('all'); - - $noAfterFindModel = new NodeNoAfterFind(); - $noAfterFindModel->recursive = 3; - $noAfterFindData = $noAfterFindModel->find('all'); - - $this->assertFalse($afterFindModel == $noAfterFindModel); - // Limitation of PHP 4 and PHP 5 > 5.1.6 when comparing recursive objects - if (PHP_VERSION === '5.1.6') { - $this->assertFalse($afterFindModel != $duplicateModel); - } - $this->assertEqual($afterFindData, $noAfterFindData); - } -/** - * testFindAllThreaded method - * - * @access public - * @return void - */ - function testFindAllThreaded() { - $this->loadFixtures('Category'); - $TestModel =& new Category(); - - $result = $TestModel->find('threaded'); - $expected = array( - array( - 'Category' => array( - 'id' => '1', - 'parent_id' => '0', - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '2', - 'parent_id' => '1', - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array('Category' => array( - 'id' => '7', - 'parent_id' => '2', - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array()), - array('Category' => array( - 'id' => '8', - 'parent_id' => '2', - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array())) - ), - array( - 'Category' => array( - 'id' => '3', - 'parent_id' => '1', - 'name' => 'Category 1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ) - ) - ), - array( - 'Category' => array( - 'id' => '4', - 'parent_id' => '0', - 'name' => 'Category 2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ), - array( - 'Category' => array( - 'id' => '5', - 'parent_id' => '0', - 'name' => 'Category 3', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '6', - 'parent_id' => '5', - 'name' => 'Category 3.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ) - ) - ) - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('threaded', array( - 'conditions' => array('Category.name LIKE' => 'Category 1%') - )); - - $expected = array( - array( - 'Category' => array( - 'id' => '1', - 'parent_id' => '0', - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '2', - 'parent_id' => '1', - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array('Category' => array( - 'id' => '7', - 'parent_id' => '2', - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array()), - array('Category' => array( - 'id' => '8', - 'parent_id' => '2', - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array())) - ), - array( - 'Category' => array( - 'id' => '3', - 'parent_id' => '1', - 'name' => 'Category 1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ) - ) - ) - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('threaded', array( - 'fields' => 'id, parent_id, name' - )); - - $expected = array( - array( - 'Category' => array( - 'id' => '1', - 'parent_id' => '0', - 'name' => 'Category 1' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '2', - 'parent_id' => '1', - 'name' => 'Category 1.1' - ), - 'children' => array( - array('Category' => array( - 'id' => '7', - 'parent_id' => '2', - 'name' => 'Category 1.1.1'), - 'children' => array()), - array('Category' => array( - 'id' => '8', - 'parent_id' => '2', - 'name' => 'Category 1.1.2'), - 'children' => array())) - ), - array( - 'Category' => array( - 'id' => '3', - 'parent_id' => '1', - 'name' => 'Category 1.2' - ), - 'children' => array() - ) - ) - ), - array( - 'Category' => array( - 'id' => '4', - 'parent_id' => '0', - 'name' => 'Category 2' - ), - 'children' => array() - ), - array( - 'Category' => array( - 'id' => '5', - 'parent_id' => '0', - 'name' => 'Category 3' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '6', - 'parent_id' => '5', - 'name' => 'Category 3.1' - ), - 'children' => array() - ) - ) - ) - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('threaded', array('order' => 'id DESC')); - - $expected = array( - array( - 'Category' => array( - 'id' => 5, - 'parent_id' => 0, - 'name' => 'Category 3', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => 6, - 'parent_id' => 5, - 'name' => 'Category 3.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ) - ) - ), - array( - 'Category' => array( - 'id' => 4, - 'parent_id' => 0, - 'name' => 'Category 2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ), - array( - 'Category' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => 3, - 'parent_id' => 1, - 'name' => 'Category 1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ), - array( - 'Category' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array('Category' => array( - 'id' => '8', - 'parent_id' => '2', - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array()), - array('Category' => array( - 'id' => '7', - 'parent_id' => '2', - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array())) - ) - ) - ) - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('threaded', array( - 'conditions' => array('Category.name LIKE' => 'Category 3%') - )); - $expected = array( - array( - 'Category' => array( - 'id' => '5', - 'parent_id' => '0', - 'name' => 'Category 3', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '6', - 'parent_id' => '5', - 'name' => 'Category 3.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'children' => array() - ) - ) - ) - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('threaded', array( - 'conditions' => array('Category.name LIKE' => 'Category 1.1%') - )); - $expected = array( - array('Category' => - array( - 'id' => '2', - 'parent_id' => '1', - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array( - array('Category' => array( - 'id' => '7', - 'parent_id' => '2', - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array()), - array('Category' => array( - 'id' => '8', - 'parent_id' => '2', - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31'), - 'children' => array())))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('threaded', array( - 'fields' => 'id, parent_id, name', - 'conditions' => array('Category.id !=' => 2) - )); - $expected = array( - array( - 'Category' => array( - 'id' => '1', - 'parent_id' => '0', - 'name' => 'Category 1' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '3', - 'parent_id' => '1', - 'name' => 'Category 1.2' - ), - 'children' => array() - ) - ) - ), - array( - 'Category' => array( - 'id' => '4', - 'parent_id' => '0', - 'name' => 'Category 2' - ), - 'children' => array() - ), - array( - 'Category' => array( - 'id' => '5', - 'parent_id' => '0', - 'name' => 'Category 3' - ), - 'children' => array( - array( - 'Category' => array( - 'id' => '6', - 'parent_id' => '5', - 'name' => 'Category 3.1' - ), - 'children' => array() - ) - ) - ) - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array( - 'fields' => 'id, name, parent_id', - 'conditions' => array('Category.id !=' => 1) - )); - $expected = array ( - array ('Category' => array( - 'id' => '2', - 'name' => 'Category 1.1', - 'parent_id' => '1' - )), - array ('Category' => array( - 'id' => '3', - 'name' => 'Category 1.2', - 'parent_id' => '1' - )), - array ('Category' => array( - 'id' => '4', - 'name' => 'Category 2', - 'parent_id' => '0' - )), - array ('Category' => array( - 'id' => '5', - 'name' => 'Category 3', - 'parent_id' => '0' - )), - array ('Category' => array( - 'id' => '6', - 'name' => 'Category 3.1', - 'parent_id' => '5' - )), - array ('Category' => array( - 'id' => '7', - 'name' => 'Category 1.1.1', - 'parent_id' => '2' - )), - array ('Category' => array( - 'id' => '8', - 'name' => 'Category 1.1.2', - 'parent_id' => '2' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('threaded', array( - 'fields' => 'id, parent_id, name', - 'conditions' => array('Category.id !=' => 1) - )); - $expected = array( - array( - 'Category' => array( - 'id' => '2', - 'parent_id' => '1', - 'name' => 'Category 1.1' - ), - 'children' => array( - array('Category' => array( - 'id' => '7', - 'parent_id' => '2', - 'name' => 'Category 1.1.1'), - 'children' => array()), - array('Category' => array( - 'id' => '8', - 'parent_id' => '2', - 'name' => 'Category 1.1.2'), - 'children' => array())) - ), - array( - 'Category' => array( - 'id' => '3', - 'parent_id' => '1', - 'name' => 'Category 1.2' - ), - 'children' => array() - ) - ); - $this->assertEqual($result, $expected); - } -/** - * test find('neighbors') - * - * @return void - * @access public - */ - function testFindNeighbors() { - $this->loadFixtures('User', 'Article'); - $TestModel =& new Article(); - - $TestModel->id = 1; - $result = $TestModel->find('neighbors', array('fields' => array('id'))); - $expected = array( - 'prev' => null, - 'next' => array( - 'Article' => array('id' => 2) - )); - $this->assertEqual($result, $expected); - - $TestModel->id = 2; - $result = $TestModel->find('neighbors', array( - 'fields' => array('id') - )); - - $expected = array( - 'prev' => array( - 'Article' => array( - 'id' => 1 - )), - 'next' => array( - 'Article' => array( - 'id' => 3 - ))); - $this->assertEqual($result, $expected); - - $TestModel->id = 3; - $result = $TestModel->find('neighbors', array('fields' => array('id'))); - $expected = array( - 'prev' => array( - 'Article' => array( - 'id' => 2 - )), - 'next' => null - ); - $this->assertEqual($result, $expected); - - $TestModel->id = 1; - $result = $TestModel->find('neighbors', array('recursive' => -1)); - $expected = array( - 'prev' => null, - 'next' => array( - 'Article' => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ) - ) - ); - $this->assertEqual($result, $expected); - - $TestModel->id = 2; - $result = $TestModel->find('neighbors', array('recursive' => -1)); - $expected = array( - 'prev' => array( - 'Article' => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ) - ), - 'next' => array( - 'Article' => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ) - ) - ); - $this->assertEqual($result, $expected); - - $TestModel->id = 3; - $result = $TestModel->find('neighbors', array('recursive' => -1)); - $expected = array( - 'prev' => array( - 'Article' => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ) - ), - 'next' => null - ); - $this->assertEqual($result, $expected); - - $TestModel->recursive = 0; - $TestModel->id = 1; - $one = $TestModel->read(); - $TestModel->id = 2; - $two = $TestModel->read(); - $TestModel->id = 3; - $three = $TestModel->read(); - - $TestModel->id = 1; - $result = $TestModel->find('neighbors'); - $expected = array('prev' => null, 'next' => $two); - $this->assertEqual($result, $expected); - - $TestModel->id = 2; - $result = $TestModel->find('neighbors'); - $expected = array('prev' => $one, 'next' => $three); - $this->assertEqual($result, $expected); - - $TestModel->id = 3; - $result = $TestModel->find('neighbors'); - $expected = array('prev' => $two, 'next' => null); - $this->assertEqual($result, $expected); - - $TestModel->recursive = 2; - $TestModel->id = 1; - $one = $TestModel->read(); - $TestModel->id = 2; - $two = $TestModel->read(); - $TestModel->id = 3; - $three = $TestModel->read(); - - $TestModel->id = 1; - $result = $TestModel->find('neighbors', array('recursive' => 2)); - $expected = array('prev' => null, 'next' => $two); - $this->assertEqual($result, $expected); - - $TestModel->id = 2; - $result = $TestModel->find('neighbors', array('recursive' => 2)); - $expected = array('prev' => $one, 'next' => $three); - $this->assertEqual($result, $expected); - - $TestModel->id = 3; - $result = $TestModel->find('neighbors', array('recursive' => 2)); - $expected = array('prev' => $two, 'next' => null); - $this->assertEqual($result, $expected); - } -/** - * test findNeighbours() method - * - * @return void - * @access public - */ - function testFindNeighboursLegacy() { - $this->loadFixtures('User', 'Article'); - $TestModel =& new Article(); - - $result = $TestModel->findNeighbours(null, 'Article.id', '2'); - $expected = array( - 'prev' => array( - 'Article' => array( - 'id' => 1 - )), - 'next' => array( - 'Article' => array( - 'id' => 3 - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->findNeighbours(null, 'Article.id', '3'); - $expected = array( - 'prev' => array( - 'Article' => array( - 'id' => 2 - )), - 'next' => array() - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->findNeighbours( - array('User.id' => 1), - array('Article.id', 'Article.title'), - 2 - ); - $expected = array( - 'prev' => array( - 'Article' => array( - 'id' => 1, - 'title' => 'First Article' - )), - 'next' => array( - 'Article' => array( - 'id' => 3, - 'title' => 'Third Article' - ))); - $this->assertEqual($result, $expected); - } -/** - * testFindCombinedRelations method - * - * @access public - * @return void - */ - function testFindCombinedRelations() { - $this->loadFixtures('Apple', 'Sample'); - $TestModel =& new Apple(); - - $result = $TestModel->find('all'); - - $expected = array( - array( - 'Apple' => 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' - ), - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => null, - 'apple_id' => null, - 'name' => null - ), - 'Child' => array( - 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( - 'Apple' => 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' - ), - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => '2', - 'apple_id' => '2', - 'name' => 'sample2' - ), - 'Child' => 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' => '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( - 'Apple' => 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' - ), - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => '1', - 'apple_id' => '3', - 'name' => 'sample1' - ), - 'Child' => array() - ), - array( - 'Apple' => 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' - ), - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => '3', - 'apple_id' => '4', - 'name' => 'sample3' - ), - 'Child' => array( - 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( - 'Apple' => 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' - ), - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => '4', - 'apple_id' => '5', - 'name' => 'sample4' - ), - 'Child' => array( - 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( - 'Apple' => 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' - ), - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => null, - 'apple_id' => null, - 'name' => null - ), - 'Child' => array( - 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' - ))), - array( - 'Apple' => 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' - ), - 'Parent' => 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' - ), - 'Sample' => array( - 'id' => null, - 'apple_id' => null, - 'name' => null - ), - 'Child' => array() - )); - $this->assertEqual($result, $expected); - } -/** - * testSaveEmpty method - * - * @access public - * @return void - */ - function testSaveEmpty() { - $this->loadFixtures('Thread'); - $TestModel =& new Thread(); - $data = array(); - $expected = $TestModel->save($data); - $this->assertFalse($expected); - } - // function testBasicValidation() { - // $TestModel =& new ValidationTest1(); - // $TestModel->testing = true; - // $TestModel->set(array('title' => '', 'published' => 1)); - // $this->assertEqual($TestModel->invalidFields(), array('title' => 'This field cannot be left blank')); - // - // $TestModel->create(); - // $TestModel->set(array('title' => 'Hello', 'published' => 0)); - // $this->assertEqual($TestModel->invalidFields(), array('published' => 'This field cannot be left blank')); - // - // $TestModel->create(); - // $TestModel->set(array('title' => 'Hello', 'published' => 1, 'body' => '')); - // $this->assertEqual($TestModel->invalidFields(), array('body' => 'This field cannot be left blank')); - // } - -/** - * testFindAllWithConditionInChildQuery - * - * @todo external conditions like this are going to need to be revisited at some point - * @access public - * @return void - */ - function testFindAllWithConditionInChildQuery() { - $this->loadFixtures('Basket', 'FilmFile'); - - $TestModel =& new Basket(); - $recursive = 3; - $result = $TestModel->find('all', compact('conditions', 'recursive')); - - $expected = array( - array( - 'Basket' => array( - 'id' => 1, - 'type' => 'nonfile', - 'name' => 'basket1', - 'object_id' => 1, - 'user_id' => 1, - ), - 'FilmFile' => array( - 'id' => '', - 'name' => '', - ) - ), - array( - 'Basket' => array( - 'id' => 2, - 'type' => 'file', - 'name' => 'basket2', - 'object_id' => 2, - 'user_id' => 1, - ), - 'FilmFile' => array( - 'id' => 2, - 'name' => 'two', - ) - ), - ); - $this->assertEqual($result, $expected); - } - -/** - * testFindAllWithConditionsHavingMixedDataTypes method - * - * @access public - * @return void - */ - function testFindAllWithConditionsHavingMixedDataTypes() { - $this->loadFixtures('Article'); - $TestModel =& new Article(); - $expected = array( - array( - 'Article' => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ) - ), - array( - 'Article' => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ) - ) - ); - $conditions = array('id' => array('1', 2)); - $recursive = -1; - $order = 'Article.id ASC'; - $result = $TestModel->find('all', compact('conditions', 'recursive', 'order')); - $this->assertEqual($result, $expected); - - - $conditions = array('id' => array('1', 2, '3.0')); - $order = 'Article.id ASC'; - $result = $TestModel->find('all', compact('recursive', 'conditions', 'order')); - $expected = array( - array( - 'Article' => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ) - ), - array( - 'Article' => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ) - ), - array( - 'Article' => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ) - ) - ); - $this->assertEqual($result, $expected); - } -/** - * testBindUnbind method - * - * @access public - * @return void - */ - function testBindUnbind() { - $this->loadFixtures('User', 'Comment', 'FeatureSet'); - $TestModel =& new User(); - - $result = $TestModel->hasMany; - $expected = array(); - $this->assertEqual($result, $expected); - - $result = $TestModel->bindModel(array('hasMany' => array('Comment'))); - $this->assertTrue($result); - - $result = $TestModel->find('all', array( - 'fields' => 'User.id, User.user' - )); - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Comment' => array( - array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - array( - 'id' => '4', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ), - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ))), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ))), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry' - ), - 'Comment' => array() - ), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett' - ), - 'Comment' => array( - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - )))); - - $this->assertEqual($result, $expected); - - $TestModel->resetAssociations(); - $result = $TestModel->hasMany; - $this->assertEqual($result, array()); - - $result = $TestModel->bindModel(array('hasMany' => array('Comment')), false); - $this->assertTrue($result); - - $result = $TestModel->find('all', array( - 'fields' => 'User.id, User.user' - )); - - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Comment' => array( - array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - array( - 'id' => '4', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ), - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ))), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ))), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry' - ), - 'Comment' => array() - ), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett' - ), - 'Comment' => array( - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - )))); - - $this->assertEqual($result, $expected); - - $result = $TestModel->hasMany; - $expected = array( - 'Comment' => array( - 'className' => 'Comment', - 'foreignKey' => 'user_id', - 'conditions' => null, - 'fields' => null, - 'order' => null, - 'limit' => null, - 'offset' => null, - 'dependent' => null, - 'exclusive' => null, - 'finderQuery' => null, - 'counterQuery' => null - )); - $this->assertEqual($result, $expected); - - $result = $TestModel->unbindModel(array('hasMany' => array('Comment'))); - $this->assertTrue($result); - - $result = $TestModel->hasMany; - $expected = array(); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array( - 'fields' => 'User.id, User.user' - )); - $expected = array( - array('User' => array('id' => '1', 'user' => 'mariano')), - array('User' => array('id' => '2', 'user' => 'nate')), - array('User' => array('id' => '3', 'user' => 'larry')), - array('User' => array('id' => '4', 'user' => 'garrett'))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array( - 'fields' => 'User.id, User.user' - )); - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Comment' => array( - array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - array( - 'id' => '4', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ), - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ))), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ))), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry' - ), - 'Comment' => array() - ), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett' - ), - 'Comment' => array( - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => - 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - )))); - $this->assertEqual($result, $expected); - - $result = $TestModel->unbindModel(array('hasMany' => array('Comment')), false); - $this->assertTrue($result); - - $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); - $expected = array( - array('User' => array('id' => '1', 'user' => 'mariano')), - array('User' => array('id' => '2', 'user' => 'nate')), - array('User' => array('id' => '3', 'user' => 'larry')), - array('User' => array('id' => '4', 'user' => 'garrett'))); - $this->assertEqual($result, $expected); - - $result = $TestModel->hasMany; - $expected = array(); - $this->assertEqual($result, $expected); - - $result = $TestModel->bindModel(array('hasMany' => array( - 'Comment' => array('className' => 'Comment', 'conditions' => 'Comment.published = \'Y\'') - ))); - $this->assertTrue($result); - - $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Comment' => array( - array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ))), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ))), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry' - ), - 'Comment' => array() - ), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett' - ), - 'Comment' => array( - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - )))); - - $this->assertEqual($result, $expected); - - $TestModel2 =& new DeviceType(); - - $expected = array( - 'className' => 'FeatureSet', - 'foreignKey' => 'feature_set_id', - 'conditions' => '', - 'fields' => '', - 'order' => '', - 'counterCache' => '' - ); - $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected); - - $TestModel2->bind('FeatureSet', array( - 'conditions' => array('active' => true) - )); - $expected['conditions'] = array('active' => true); - $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected); - - $TestModel2->bind('FeatureSet', array( - 'foreignKey' => false, - 'conditions' => array('Feature.name' => 'DeviceType.name') - )); - $expected['conditions'] = array('Feature.name' => 'DeviceType.name'); - $expected['foreignKey'] = false; - $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected); - - $TestModel2->bind('NewFeatureSet', array( - 'type' => 'hasMany', - 'className' => 'FeatureSet', - 'conditions' => array('active' => true) - )); - $expected = array( - 'className' => 'FeatureSet', - 'conditions' => array('active' => true), - 'foreignKey' => 'device_type_id', - 'fields' => '', - 'order' => '', - 'limit' => '', - 'offset' => '', - 'dependent' => '', - 'exclusive' => '', - 'finderQuery' => '', - 'counterQuery' => '' - ); - $this->assertEqual($TestModel2->hasMany['NewFeatureSet'], $expected); - $this->assertTrue(is_object($TestModel2->NewFeatureSet)); - } -/** - * testBindMultipleTimes method - * - * @access public - * @return void - */ - function testBindMultipleTimes() { - $this->loadFixtures('User', 'Comment', 'Article'); - $TestModel =& new User(); - - $result = $TestModel->hasMany; - $expected = array(); - $this->assertEqual($result, $expected); - - $result = $TestModel->bindModel(array( - 'hasMany' => array( - 'Items' => array('className' => 'Comment') - ))); - $this->assertTrue($result); - - $result = $TestModel->find('all', array( - 'fields' => 'User.id, User.user' - )); - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Items' => array( - array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - array( - 'id' => '4', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ), - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ))), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate' - ), - 'Items' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - ))), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry' - ), - 'Items' => array() - ), - array( - 'User' => array( - 'id' => '4', 'user' => 'garrett'), - 'Items' => array( - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - )))); - $this->assertEqual($result, $expected); - - $result = $TestModel->bindModel(array( - 'hasMany' => array( - 'Items' => array('className' => 'Article') - ))); - $this->assertTrue($result); - - $result = $TestModel->find('all', array( - 'fields' => 'User.id, User.user' - )); - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Items' => array( - array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ))), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate' - ), - 'Items' => array() - ), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry' - ), - 'Items' => array( - array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ))), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett' - ), - 'Items' => array() - )); - $this->assertEqual($result, $expected); - } - -/** - * test that bindModel behaves with Custom primary Key associations - * - * @return void - **/ - function bindWithCustomPrimaryKey() { - $this->loadFixtures('Story', 'StoriesTag', 'Tag'); - $Model =& ClassRegistry::init('StoriesTag'); - $Model->bindModel(array( - 'belongsTo' => array( - 'Tag' => array( - 'className' => 'Tag', - 'foreignKey' => 'story' - )))); - - $result = $Model->find('all'); - $this->assertFalse(empty($result)); - } - -/** - * testAssociationAfterFind method - * - * @access public - * @return void - */ - function testAssociationAfterFind() { - $this->loadFixtures('Post', 'Author', 'Comment'); - $TestModel =& new Post(); - $result = $TestModel->find('all'); - $expected = array( - array( - 'Post' => array( - 'id' => '1', - 'author_id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'Author' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31', - 'test' => 'working' - )), - array( - 'Post' => array( - 'id' => '2', - 'author_id' => '3', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'Author' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31', - 'test' => 'working' - )), - array( - 'Post' => array( - 'id' => '3', - 'author_id' => '1', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'Author' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31', - 'test' => 'working' - ))); - $this->assertEqual($result, $expected); - unset($TestModel); - - $Author =& new Author(); - $Author->Post->bindModel(array( - 'hasMany' => array( - 'Comment' => array( - 'className' => 'ModifiedComment', - 'foreignKey' => 'article_id', - ) - ))); - $result = $Author->find('all', array( - 'conditions' => array('Author.id' => 1), - 'recursive' => 2 - )); - $expected = array( - 'id' => 1, - 'article_id' => 1, - 'user_id' => 2, - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31', - 'callback' => 'Fire' - ); - $this->assertEqual($result[0]['Post'][0]['Comment'][0], $expected); - } -/** - * Tests that callbacks can be properly disabled - * - * @access public - * @return void - */ - function testCallbackDisabling() { - $this->loadFixtures('Author'); - $TestModel = new ModifiedAuthor(); - - $result = Set::extract($TestModel->find('all'), '/Author/user'); - $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)'); - $this->assertEqual($result, $expected); - - $result = Set::extract($TestModel->find('all', array('callbacks' => 'after')), '/Author/user'); - $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)'); - $this->assertEqual($result, $expected); - - $result = Set::extract($TestModel->find('all', array('callbacks' => 'before')), '/Author/user'); - $expected = array('mariano', 'nate', 'larry', 'garrett'); - $this->assertEqual($result, $expected); - - $result = Set::extract($TestModel->find('all', array('callbacks' => false)), '/Author/user'); - $expected = array('mariano', 'nate', 'larry', 'garrett'); - $this->assertEqual($result, $expected); - } -/** - * testMultipleBelongsToWithSameClass method - * - * @access public - * @return void - */ - function testMultipleBelongsToWithSameClass() { - $this->loadFixtures( - 'DeviceType', - 'DeviceTypeCategory', - 'FeatureSet', - 'ExteriorTypeCategory', - 'Document', - 'Device', - 'DocumentDirectory' - ); - - $DeviceType =& new DeviceType(); - - $DeviceType->recursive = 2; - $result = $DeviceType->read(null, 1); - - $expected = array( - 'DeviceType' => array( - 'id' => 1, - 'device_type_category_id' => 1, - 'feature_set_id' => 1, - 'exterior_type_category_id' => 1, - 'image_id' => 1, - 'extra1_id' => 1, - 'extra2_id' => 1, - 'name' => 'DeviceType 1', - 'order' => 0 - ), - 'Image' => array( - 'id' => 1, - 'document_directory_id' => 1, - 'name' => 'Document 1', - 'DocumentDirectory' => array( - 'id' => 1, - 'name' => 'DocumentDirectory 1' - )), - 'Extra1' => array( - 'id' => 1, - 'document_directory_id' => 1, - 'name' => 'Document 1', - 'DocumentDirectory' => array( - 'id' => 1, - 'name' => 'DocumentDirectory 1' - )), - 'Extra2' => array( - 'id' => 1, - 'document_directory_id' => 1, - 'name' => 'Document 1', - 'DocumentDirectory' => array( - 'id' => 1, - 'name' => 'DocumentDirectory 1' - )), - 'DeviceTypeCategory' => array( - 'id' => 1, - 'name' => 'DeviceTypeCategory 1' - ), - 'FeatureSet' => array( - 'id' => 1, - 'name' => 'FeatureSet 1' - ), - 'ExteriorTypeCategory' => array( - 'id' => 1, - 'image_id' => 1, - 'name' => 'ExteriorTypeCategory 1', - 'Image' => array( - 'id' => 1, - 'device_type_id' => 1, - 'name' => 'Device 1', - 'typ' => 1 - )), - 'Device' => array( - array( - 'id' => 1, - 'device_type_id' => 1, - 'name' => 'Device 1', - 'typ' => 1 - ), - array( - 'id' => 2, - 'device_type_id' => 1, - 'name' => 'Device 2', - 'typ' => 1 - ), - array( - 'id' => 3, - 'device_type_id' => 1, - 'name' => 'Device 3', - 'typ' => 2 - ))); - - $this->assertEqual($result, $expected); - } -/** - * testHabtmRecursiveBelongsTo method - * - * @access public - * @return void - */ - function testHabtmRecursiveBelongsTo() { - $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image'); - $Portfolio =& new Portfolio(); - - $result = $Portfolio->find(array('id' => 2), null, null, 3); - $expected = array( - 'Portfolio' => array( - 'id' => 2, - 'seller_id' => 1, - 'name' => 'Portfolio 2' - ), - 'Item' => array( - array( - 'id' => 2, - 'syfile_id' => 2, - 'published' => 0, - 'name' => 'Item 2', - 'ItemsPortfolio' => array( - 'id' => 2, - 'item_id' => 2, - 'portfolio_id' => 2 - ), - 'Syfile' => array( - 'id' => 2, - 'image_id' => 2, - 'name' => 'Syfile 2', - 'item_count' => null, - 'Image' => array( - 'id' => 2, - 'name' => 'Image 2' - ) - )), - array( - 'id' => 6, - 'syfile_id' => 6, - 'published' => 0, - 'name' => 'Item 6', - 'ItemsPortfolio' => array( - 'id' => 6, - 'item_id' => 6, - 'portfolio_id' => 2 - ), - 'Syfile' => array( - 'id' => 6, - 'image_id' => null, - 'name' => 'Syfile 6', - 'item_count' => null, - 'Image' => array() - )))); - - $this->assertEqual($result, $expected); - } -/** - * testHabtmFinderQuery method - * - * @access public - * @return void - */ - function testHabtmFinderQuery() { - $this->loadFixtures('Article', 'Tag', 'ArticlesTag'); - $Article =& new Article(); - - $sql = $this->db->buildStatement( - array( - 'fields' => $this->db->fields($Article->Tag, null, array( - 'Tag.id', 'Tag.tag', 'ArticlesTag.article_id', 'ArticlesTag.tag_id' - )), - 'table' => $this->db->fullTableName('tags'), - 'alias' => 'Tag', - 'limit' => null, - 'offset' => null, - 'group' => null, - 'joins' => array(array( - 'alias' => 'ArticlesTag', - 'table' => $this->db->fullTableName('articles_tags'), - 'conditions' => array( - array("ArticlesTag.article_id" => '{$__cakeID__$}'), - array("ArticlesTag.tag_id" => $this->db->identifier('Tag.id')) - ) - )), - 'conditions' => array(), - 'order' => null - ), - $Article - ); - - $Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql; - $result = $Article->find('first'); - $expected = array( - array( - 'id' => '1', - 'tag' => 'tag1' - ), - array( - 'id' => '2', - 'tag' => 'tag2' - )); - - $this->assertEqual($result['Tag'], $expected); - } -/** - * testHabtmLimitOptimization method - * - * @access public - * @return void - */ - function testHabtmLimitOptimization() { - $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag'); - $TestModel =& new Article(); - - $TestModel->hasAndBelongsToMany['Tag']['limit'] = 2; - $result = $TestModel->read(null, 2); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'Comment' => array( - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - )), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - - $this->assertEqual($result, $expected); - - $TestModel->hasAndBelongsToMany['Tag']['limit'] = 1; - $result = $TestModel->read(null, 2); - unset($expected['Tag'][1]); - - $this->assertEqual($result, $expected); - } -/** - * testHasManyLimitOptimization method - * - * @access public - * @return void - */ - function testHasManyLimitOptimization() { - $this->loadFixtures('Project', 'Thread', 'Message', 'Bid'); - $Project =& new Project(); - $Project->recursive = 3; - - $result = $Project->find('all'); - $expected = array( - array( - 'Project' => array( - 'id' => 1, - 'name' => 'Project 1' - ), - 'Thread' => array( - array( - 'id' => 1, - 'project_id' => 1, - 'name' => 'Project 1, Thread 1', - 'Project' => array( - 'id' => 1, - 'name' => 'Project 1', - 'Thread' => array( - array( - 'id' => 1, - 'project_id' => 1, - 'name' => 'Project 1, Thread 1' - ), - array( - 'id' => 2, - 'project_id' => 1, - 'name' => 'Project 1, Thread 2' - ))), - 'Message' => array( - array( - 'id' => 1, - 'thread_id' => 1, - 'name' => 'Thread 1, Message 1', - 'Bid' => array( - 'id' => 1, - 'message_id' => 1, - 'name' => 'Bid 1.1' - )))), - array( - 'id' => 2, - 'project_id' => 1, - 'name' => 'Project 1, Thread 2', - 'Project' => array( - 'id' => 1, - 'name' => 'Project 1', - 'Thread' => array( - array( - 'id' => 1, - 'project_id' => 1, - 'name' => 'Project 1, Thread 1' - ), - array( - 'id' => 2, - 'project_id' => 1, - 'name' => 'Project 1, Thread 2' - ))), - 'Message' => array( - array( - 'id' => 2, - 'thread_id' => 2, - 'name' => 'Thread 2, Message 1', - 'Bid' => array( - 'id' => 4, - 'message_id' => 2, - 'name' => 'Bid 2.1' - )))))), - array( - 'Project' => array( - 'id' => 2, - 'name' => 'Project 2' - ), - 'Thread' => array( - array( - 'id' => 3, - 'project_id' => 2, - 'name' => 'Project 2, Thread 1', - 'Project' => array( - 'id' => 2, - 'name' => 'Project 2', - 'Thread' => array( - array( - 'id' => 3, - 'project_id' => 2, - 'name' => 'Project 2, Thread 1' - ))), - 'Message' => array( - array( - 'id' => 3, - 'thread_id' => 3, - 'name' => 'Thread 3, Message 1', - 'Bid' => array( - 'id' => 3, - 'message_id' => 3, - 'name' => 'Bid 3.1' - )))))), - array( - 'Project' => array( - 'id' => 3, - 'name' => 'Project 3' - ), - 'Thread' => array() - )); - - $this->assertEqual($result, $expected); - } -/** - * testFindAllRecursiveSelfJoin method - * - * @access public - * @return void - */ - function testFindAllRecursiveSelfJoin() { - $this->loadFixtures('Home', 'AnotherArticle', 'Advertisement'); - $TestModel =& new Home(); - $TestModel->recursive = 2; - - $result = $TestModel->find('all'); - $expected = array( - array( - 'Home' => array( - 'id' => '1', - 'another_article_id' => '1', - 'advertisement_id' => '1', - 'title' => 'First Home', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'AnotherArticle' => array( - 'id' => '1', - 'title' => 'First Article', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31', - 'Home' => array( - array( - 'id' => '1', - 'another_article_id' => '1', - 'advertisement_id' => '1', - 'title' => 'First Home', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ))), - 'Advertisement' => array( - 'id' => '1', - 'title' => 'First Ad', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31', - 'Home' => array( - array( - 'id' => '1', - 'another_article_id' => '1', - 'advertisement_id' => '1', - 'title' => 'First Home', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - array( - 'id' => '2', - 'another_article_id' => '3', - 'advertisement_id' => '1', - 'title' => 'Second Home', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - )))), - array( - 'Home' => array( - 'id' => '2', - 'another_article_id' => '3', - 'advertisement_id' => '1', - 'title' => 'Second Home', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'AnotherArticle' => array( - 'id' => '3', - 'title' => 'Third Article', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31', - 'Home' => array( - array( - 'id' => '2', - 'another_article_id' => '3', - 'advertisement_id' => '1', - 'title' => 'Second Home', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ))), - 'Advertisement' => array( - 'id' => '1', - 'title' => 'First Ad', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31', - 'Home' => array( - array( - 'id' => '1', - 'another_article_id' => '1', - 'advertisement_id' => '1', - 'title' => 'First Home', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - array( - 'id' => '2', - 'another_article_id' => '3', - 'advertisement_id' => '1', - 'title' => 'Second Home', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ))))); - - $this->assertEqual($result, $expected); - - - - } -/** - * testFindAllRecursiveWithHabtm method - * - * @return void - * @access public - */ - function testFindAllRecursiveWithHabtm() { - $this->loadFixtures( - 'MyCategoriesMyUsers', - 'MyCategoriesMyProducts', - 'MyCategory', - 'MyUser', - 'MyProduct' - ); - - $MyUser =& new MyUser(); - $MyUser->recursive = 2; - - $result = $MyUser->find('all'); - $expected = array( - array( - 'MyUser' => array('id' => '1', 'firstname' => 'userA'), - 'MyCategory' => array( - array( - 'id' => '1', - 'name' => 'A', - 'MyProduct' => array( - array( - 'id' => '1', - 'name' => 'book' - ))), - array( - 'id' => '3', - 'name' => 'C', - 'MyProduct' => array( - array( - 'id' => '2', - 'name' => 'computer' - ))))), - array( - 'MyUser' => array( - 'id' => '2', - 'firstname' => 'userB' - ), - 'MyCategory' => array( - array( - 'id' => '1', - 'name' => 'A', - 'MyProduct' => array( - array( - 'id' => '1', - 'name' => 'book' - ))), - array( - 'id' => '2', - 'name' => 'B', - 'MyProduct' => array( - array( - 'id' => '1', - 'name' => 'book' - ), - array( - 'id' => '2', - 'name' => 'computer' - )))))); - - $this->assertIdentical($result, $expected); - } -/** - * testReadFakeThread method - * - * @access public - * @return void - */ - function testReadFakeThread() { - $this->loadFixtures('CategoryThread'); - $TestModel =& new CategoryThread(); - - $fullDebug = $this->db->fullDebug; - $this->db->fullDebug = true; - $TestModel->recursive = 6; - $TestModel->id = 7; - $result = $TestModel->read(); - $expected = array( - 'CategoryThread' => array( - 'id' => 7, - 'parent_id' => 6, - 'name' => 'Category 2.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 6, - 'parent_id' => 5, - 'name' => 'Category 2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 5, - 'parent_id' => 4, - 'name' => 'Category 1.1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 4, - 'parent_id' => 3, - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 3, - 'parent_id' => 2, - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ))))))); - - $this->db->fullDebug = $fullDebug; - $this->assertEqual($result, $expected); - } -/** - * testFindFakeThread method - * - * @access public - * @return void - */ - function testFindFakeThread() { - $this->loadFixtures('CategoryThread'); - $TestModel =& new CategoryThread(); - - $fullDebug = $this->db->fullDebug; - $this->db->fullDebug = true; - $TestModel->recursive = 6; - $result = $TestModel->find(array('CategoryThread.id' => 7)); - - $expected = array( - 'CategoryThread' => array( - 'id' => 7, - 'parent_id' => 6, - 'name' => 'Category 2.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 6, - 'parent_id' => 5, - 'name' => 'Category 2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 5, - 'parent_id' => 4, - 'name' => 'Category 1.1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 4, - 'parent_id' => 3, - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 3, - 'parent_id' => 2, - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ))))))); - - $this->db->fullDebug = $fullDebug; - $this->assertEqual($result, $expected); - } -/** - * testFindAllFakeThread method - * - * @access public - * @return void - */ - function testFindAllFakeThread() { - $this->loadFixtures('CategoryThread'); - $TestModel =& new CategoryThread(); - - $fullDebug = $this->db->fullDebug; - $this->db->fullDebug = true; - $TestModel->recursive = 6; - $result = $TestModel->find('all', null, null, 'CategoryThread.id ASC'); - $expected = array( - array( - 'CategoryThread' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => null, - 'parent_id' => null, - 'name' => null, - 'created' => null, - 'updated' => null, - 'ParentCategory' => array() - )), - array( - 'CategoryThread' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array() - )), - array( - 'CategoryThread' => array( - 'id' => 3, - 'parent_id' => 2, - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array() - ))), - array( - 'CategoryThread' => array( - 'id' => 4, - 'parent_id' => 3, - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 3, - 'parent_id' => 2, - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array() - )))), - array( - 'CategoryThread' => array( - 'id' => 5, - 'parent_id' => 4, - 'name' => 'Category 1.1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 4, - 'parent_id' => 3, - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 3, - 'parent_id' => 2, - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array() - ))))), - array( - 'CategoryThread' => array( - 'id' => 6, - 'parent_id' => 5, - 'name' => 'Category 2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 5, - 'parent_id' => 4, - 'name' => 'Category 1.1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 4, - 'parent_id' => 3, - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 3, - 'parent_id' => 2, - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array() - )))))), - array( - 'CategoryThread' => array( - 'id' => 7, - 'parent_id' => 6, - 'name' => 'Category 2.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ), - 'ParentCategory' => array( - 'id' => 6, - 'parent_id' => 5, - 'name' => 'Category 2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 5, - 'parent_id' => 4, - 'name' => 'Category 1.1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 4, - 'parent_id' => 3, - 'name' => 'Category 1.1.2', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 3, - 'parent_id' => 2, - 'name' => 'Category 1.1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 2, - 'parent_id' => 1, - 'name' => 'Category 1.1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31', - 'ParentCategory' => array( - 'id' => 1, - 'parent_id' => 0, - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - )))))))); - - $this->db->fullDebug = $fullDebug; - $this->assertEqual($result, $expected); - } -/** - * testConditionalNumerics method - * - * @access public - * @return void - */ - function testConditionalNumerics() { - $this->loadFixtures('NumericArticle'); - $NumericArticle =& new NumericArticle(); - $data = array('title' => '12345abcde'); - $result = $NumericArticle->find($data); - $this->assertTrue(!empty($result)); - - $data = array('title' => '12345'); - $result = $NumericArticle->find($data); - $this->assertTrue(empty($result)); - } - -/** - * test find('all') method - * - * @access public - * @return void - */ - function testFindAll() { - $this->loadFixtures('User'); - $TestModel =& new User(); - $TestModel->cacheQueries = false; - - $result = $TestModel->find('all'); - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - )), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - )), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - )), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:22:23', - 'updated' => '2007-03-17 01:24:31' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array('conditions' => 'User.id > 2')); - $expected = array( - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - )), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:22:23', - 'updated' => '2007-03-17 01:24:31' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array( - 'conditions' => array('User.id !=' => '0', 'User.user LIKE' => '%arr%') - )); - $expected = array( - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - )), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:22:23', - 'updated' => '2007-03-17 01:24:31' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array('conditions' => array('User.id' => '0'))); - $expected = array(); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array( - 'conditions' => array('or' => array('User.id' => '0', 'User.user LIKE' => '%a%') - ))); - - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - )), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - )), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - )), - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:22:23', - 'updated' => '2007-03-17 01:24:31' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); - $expected = array( - array('User' => array('id' => '1', 'user' => 'mariano')), - array('User' => array('id' => '2', 'user' => 'nate')), - array('User' => array('id' => '3', 'user' => 'larry')), - array('User' => array('id' => '4', 'user' => 'garrett'))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user ASC')); - $expected = array( - array('User' => array('user' => 'garrett')), - array('User' => array('user' => 'larry')), - array('User' => array('user' => 'mariano')), - array('User' => array('user' => 'nate'))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user DESC')); - $expected = array( - array('User' => array('user' => 'nate')), - array('User' => array('user' => 'mariano')), - array('User' => array('user' => 'larry')), - array('User' => array('user' => 'garrett'))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array('limit' => 3, 'page' => 1)); - - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - )), - array( - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - )), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ))); - $this->assertEqual($result, $expected); - - $ids = array(4 => 1, 5 => 3); - $result = $TestModel->find('all', array( - 'conditions' => array('User.id' => $ids), - 'order' => 'User.id' - )); - $expected = array( - array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - )), - array( - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ))); - $this->assertEqual($result, $expected); - - // These tests are expected to fail on SQL Server since the LIMIT/OFFSET - // hack can't handle small record counts. - if ($this->db->config['driver'] != 'mssql') { - $result = $TestModel->find('all', array('limit' => 3, 'page' => 2)); - $expected = array( - array( - 'User' => array( - 'id' => '4', - 'user' => 'garrett', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:22:23', - 'updated' => '2007-03-17 01:24:31' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array('limit' => 3, 'page' => 3)); - $expected = array(); - $this->assertEqual($result, $expected); - } - } -/** - * test find('list') method - * - * @access public - * @return void - */ - function testGenerateFindList() { - $this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User'); - - $TestModel =& new Article(); - $TestModel->displayField = 'title'; - - $result = $TestModel->find('list', array( - 'order' => 'Article.title ASC' - )); - - $expected = array( - 1 => 'First Article', - 2 => 'Second Article', - 3 => 'Third Article' - ); - $this->assertEqual($result, $expected); - - $db =& ConnectionManager::getDataSource('test_suite'); - if ($db->config['driver'] == 'mysql') { - $result = $TestModel->find('list', array( - 'order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC') - )); - $expected = array( - 1 => 'First Article', - 3 => 'Third Article', - 2 => 'Second Article' - ); - $this->assertEqual($result, $expected); - } - - $result = Set::combine( - $TestModel->find('all', array( - 'order' => 'Article.title ASC', - 'fields' => array('id', 'title') - )), - '{n}.Article.id', '{n}.Article.title' - ); - $expected = array( - 1 => 'First Article', - 2 => 'Second Article', - 3 => 'Third Article' - ); - $this->assertEqual($result, $expected); - - $result = Set::combine( - $TestModel->find('all', array( - 'order' => 'Article.title ASC' - )), - '{n}.Article.id', '{n}.Article' - ); - $expected = array( - 1 => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 2 => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 3 => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - )); - - $this->assertEqual($result, $expected); - - $result = Set::combine( - $TestModel->find('all', array( - 'order' => 'Article.title ASC' - )), - '{n}.Article.id', '{n}.Article', '{n}.Article.user_id' - ); - $expected = array( - 1 => array( - 1 => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 3 => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - )), - 3 => array( - 2 => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ))); - - $this->assertEqual($result, $expected); - - $result = Set::combine( - $TestModel->find('all', array( - 'order' => 'Article.title ASC', - 'fields' => array('id', 'title', 'user_id') - )), - '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id' - ); - - $expected = array( - 1 => array( - 1 => 'First Article', - 3 => 'Third Article' - ), - 3 => array( - 2 => 'Second Article' - )); - $this->assertEqual($result, $expected); - - $TestModel =& new Apple(); - $expected = array( - 1 => 'Red Apple 1', - 2 => 'Bright Red Apple', - 3 => 'green blue', - 4 => 'Test Name', - 5 => 'Blue Green', - 6 => 'My new apple', - 7 => 'Some odd color' - ); - - $this->assertEqual($TestModel->find('list'), $expected); - $this->assertEqual($TestModel->Parent->find('list'), $expected); - - $TestModel =& new Post(); - $result = $TestModel->find('list', array( - 'fields' => 'Post.title' - )); - $expected = array( - 1 => 'First Post', - 2 => 'Second Post', - 3 => 'Third Post' - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('list', array( - 'fields' => 'title' - )); - $expected = array( - 1 => 'First Post', - 2 => 'Second Post', - 3 => 'Third Post' - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('list', array( - 'fields' => array('title', 'id') - )); - $expected = array( - 'First Post' => '1', - 'Second Post' => '2', - 'Third Post' => '3' - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('list', array( - 'fields' => array('title', 'id', 'created') - )); - $expected = array( - '2007-03-18 10:39:23' => array( - 'First Post' => '1' - ), - '2007-03-18 10:41:23' => array( - 'Second Post' => '2' - ), - '2007-03-18 10:43:23' => array( - 'Third Post' => '3' - ), - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('list', array( - 'fields' => array('Post.body') - )); - $expected = array( - 1 => 'First Post Body', - 2 => 'Second Post Body', - 3 => 'Third Post Body' - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('list', array( - 'fields' => array('Post.title', 'Post.body') - )); - $expected = array( - 'First Post' => 'First Post Body', - 'Second Post' => 'Second Post Body', - 'Third Post' => 'Third Post Body' - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('list', array( - 'fields' => array('Post.id', 'Post.title', 'Author.user'), - 'recursive' => 1 - )); - $expected = array( - 'mariano' => array( - 1 => 'First Post', - 3 => 'Third Post' - ), - 'larry' => array( - 2 => 'Second Post' - )); - $this->assertEqual($result, $expected); - - $TestModel =& new User(); - $result = $TestModel->find('list', array( - 'fields' => array('User.user', 'User.password') - )); - $expected = array( - 'mariano' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'nate' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'larry' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99' - ); - $this->assertEqual($result, $expected); - - $TestModel =& new ModifiedAuthor(); - $result = $TestModel->find('list', array( - 'fields' => array('Author.id', 'Author.user') - )); - $expected = array( - 1 => 'mariano (CakePHP)', - 2 => 'nate (CakePHP)', - 3 => 'larry (CakePHP)', - 4 => 'garrett (CakePHP)' - ); - $this->assertEqual($result, $expected); - } -/** - * testFindField method - * - * @access public - * @return void - */ - function testFindField() { - $this->loadFixtures('User'); - $TestModel =& new User(); - - $TestModel->id = 1; - $result = $TestModel->field('user'); - $this->assertEqual($result, 'mariano'); - - $result = $TestModel->field('User.user'); - $this->assertEqual($result, 'mariano'); - - $TestModel->id = false; - $result = $TestModel->field('user', array( - 'user' => 'mariano' - )); - $this->assertEqual($result, 'mariano'); - - $result = $TestModel->field('COUNT(*) AS count', true); - $this->assertEqual($result, 4); - - $result = $TestModel->field('COUNT(*)', true); - $this->assertEqual($result, 4); - } -/** - * testFindUnique method - * - * @access public - * @return void - */ - function testFindUnique() { - $this->loadFixtures('User'); - $TestModel =& new User(); - - $this->assertFalse($TestModel->isUnique(array( - 'user' => 'nate' - ))); - $TestModel->id = 2; - $this->assertTrue($TestModel->isUnique(array( - 'user' => 'nate' - ))); - $this->assertFalse($TestModel->isUnique(array( - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' - ))); - } -/** - * test find('count') method - * - * @access public - * @return void - */ - function testFindCount() { - $this->loadFixtures('User', 'Project'); - - $TestModel =& new User(); - $result = $TestModel->find('count'); - $this->assertEqual($result, 4); - - $fullDebug = $this->db->fullDebug; - $this->db->fullDebug = true; - $TestModel->order = 'User.id'; - $this->db->_queriesLog = array(); - $result = $TestModel->find('count'); - $this->assertEqual($result, 4); - - $this->assertTrue(isset($this->db->_queriesLog[0]['query'])); - $this->assertNoPattern('/ORDER\s+BY/', $this->db->_queriesLog[0]['query']); - } - -/** - * test find with COUNT(DISTINCT field) - * - * @return void - **/ - function testFindCountDistinct() { - $skip = $this->skipIf( - $this->db->config['driver'] == 'sqlite', - 'SELECT COUNT(DISTINCT field) is not compatible with SQLite' - ); - if ($skip) { - return; - } - $this->loadFixtures('Project'); - $TestModel =& new Project(); - $TestModel->create(array('name' => 'project')) && $TestModel->save(); - $TestModel->create(array('name' => 'project')) && $TestModel->save(); - $TestModel->create(array('name' => 'project')) && $TestModel->save(); - - $result = $TestModel->find('count', array('fields' => 'DISTINCT name')); - $this->assertEqual($result, 4); - } -/** - * Test find(count) with Db::expression - * - * @access public - * @return void - */ - function testFindCountWithDbExpressions() { - if ($this->skipIf($this->db->config['driver'] == 'postgres', '%s testFindCountWithExpressions is not compatible with Postgres')) { - return; - } - $this->loadFixtures('Project'); - $db = ConnectionManager::getDataSource('test_suite'); - $TestModel =& new Project(); - - $result = $TestModel->find('count', array('conditions' => array( - $db->expression('Project.name = \'Project 3\'') - ))); - $this->assertEqual($result, 1); - - $result = $TestModel->find('count', array('conditions' => array( - 'Project.name' => $db->expression('\'Project 3\'') - ))); - $this->assertEqual($result, 1); - } -/** - * testFindMagic method - * - * @access public - * @return void - */ - function testFindMagic() { - $this->loadFixtures('User'); - $TestModel =& new User(); - - $result = $TestModel->findByUser('mariano'); - $expected = array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - )); - $this->assertEqual($result, $expected); - - $result = $TestModel->findByPassword('5f4dcc3b5aa765d61d8327deb882cf99'); - $expected = array('User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - )); - $this->assertEqual($result, $expected); - } -/** - * testRead method - * - * @access public - * @return void - */ - function testRead() { - $this->loadFixtures('User', 'Article'); - $TestModel =& new User(); - - $result = $TestModel->read(); - $this->assertFalse($result); - - $TestModel->id = 2; - $result = $TestModel->read(); - $expected = array( - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - )); - $this->assertEqual($result, $expected); - - $result = $TestModel->read(null, 2); - $expected = array( - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - )); - $this->assertEqual($result, $expected); - - $TestModel->id = 2; - $result = $TestModel->read(array('id', 'user')); - $expected = array('User' => array('id' => '2', 'user' => 'nate')); - $this->assertEqual($result, $expected); - - $result = $TestModel->read('id, user', 2); - $expected = array( - 'User' => array( - 'id' => '2', - 'user' => 'nate' - )); - $this->assertEqual($result, $expected); - - $result = $TestModel->bindModel(array('hasMany' => array('Article'))); - $this->assertTrue($result); - - $TestModel->id = 1; - $result = $TestModel->read('id, user'); - $expected = array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Article' => array( - array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - array( - 'id' => '3', - 'user_id' => '1', - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ))); - $this->assertEqual($result, $expected); - } -/** - * testRecursiveRead method - * - * @access public - * @return void - */ - function testRecursiveRead() { - $this->loadFixtures( - 'User', - 'Article', - 'Comment', - 'Tag', - 'ArticlesTag', - 'Featured', - 'ArticleFeatured' - ); - $TestModel =& new User(); - - $result = $TestModel->bindModel(array('hasMany' => array('Article')), false); - $this->assertTrue($result); - - $TestModel->recursive = 0; - $result = $TestModel->read('id, user', 1); - $expected = array( - 'User' => array('id' => '1', 'user' => 'mariano'), - ); - $this->assertEqual($result, $expected); - - $TestModel->recursive = 1; - $result = $TestModel->read('id, user', 1); - $expected = array( - 'User' => array( - 'id' => '1', - 'user' => 'mariano' - ), - 'Article' => array( - array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - array( - 'id' => '3', - 'user_id' => '1', - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ))); - $this->assertEqual($result, $expected); - - $TestModel->recursive = 2; - $result = $TestModel->read('id, user', 3); - $expected = array( - 'User' => array( - 'id' => '3', - 'user' => 'larry' - ), - 'Article' => array( - array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31', - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'Comment' => array( - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - )), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))))); - $this->assertEqual($result, $expected); - } - - function testRecursiveFindAll() { - $this->db->truncate(new Featured()); - - $this->loadFixtures( - 'User', - 'Article', - 'Comment', - 'Tag', - 'ArticlesTag', - 'Attachment', - 'ArticleFeatured', - 'Featured', - 'Category' - ); - $TestModel =& new Article(); - - $result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 1))); - $expected = array( - array( - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - ), - array( - 'id' => '3', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Third Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:49:23', - 'updated' => '2007-03-18 10:51:31' - ), - array( - 'id' => '4', - 'article_id' => '1', - 'user_id' => '1', - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ) - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ))), - array( - 'Article' => array( - 'id' => '3', - 'user_id' => '1', - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Comment' => array(), - 'Tag' => array() - ) - ); - $this->assertEqual($result, $expected); - - $result = $TestModel->find('all', array( - 'conditions' => array('Article.user_id' => 3), - 'limit' => 1, - 'recursive' => 2 - )); - - $expected = array( - array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'Comment' => array( - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31', - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Attachment' => array( - 'id' => '1', - 'comment_id' => 5, - 'attachment' => 'attachment.zip', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ) - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31', - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - ), - 'Attachment' => false - ) - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - )))); - - $this->assertEqual($result, $expected); - - $Featured = new Featured(); - - $Featured->recursive = 2; - $Featured->bindModel(array( - 'belongsTo' => array( - 'ArticleFeatured' => array( - 'conditions' => "ArticleFeatured.published = 'Y'", - 'fields' => 'id, title, user_id, published' - ) - ) - )); - - $Featured->ArticleFeatured->unbindModel(array( - 'hasMany' => array('Attachment', 'Comment'), - 'hasAndBelongsToMany' => array('Tag')) - ); - - $orderBy = 'ArticleFeatured.id ASC'; - $result = $Featured->find('all', array( - 'order' => $orderBy, 'limit' => 3 - )); - - $expected = array( - array( - 'Featured' => array( - 'id' => '1', - 'article_featured_id' => '1', - 'category_id' => '1', - 'published_date' => '2007-03-31 10:39:23', - 'end_date' => '2007-05-15 10:39:23', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'ArticleFeatured' => array( - 'id' => '1', - 'title' => 'First Article', - 'user_id' => '1', - 'published' => 'Y', - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Category' => array(), - 'Featured' => array( - 'id' => '1', - 'article_featured_id' => '1', - 'category_id' => '1', - 'published_date' => '2007-03-31 10:39:23', - 'end_date' => '2007-05-15 10:39:23', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - 'Category' => array( - 'id' => '1', - 'parent_id' => '0', - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - )), - array( - 'Featured' => array( - 'id' => '2', - 'article_featured_id' => '2', - 'category_id' => '1', - 'published_date' => '2007-03-31 10:39:23', - 'end_date' => '2007-05-15 10:39:23', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'ArticleFeatured' => array( - 'id' => '2', - 'title' => 'Second Article', - 'user_id' => '3', - 'published' => 'Y', - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'Category' => array(), - 'Featured' => array( - 'id' => '2', - 'article_featured_id' => '2', - 'category_id' => '1', - 'published_date' => '2007-03-31 10:39:23', - 'end_date' => '2007-05-15 10:39:23', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - 'Category' => array( - 'id' => '1', - 'parent_id' => '0', - 'name' => 'Category 1', - 'created' => '2007-03-18 15:30:23', - 'updated' => '2007-03-18 15:32:31' - ))); - $this->assertEqual($result, $expected); - } -/** - * testRecursiveFindAllWithLimit method - * - * @access public - * @return void - */ - function testRecursiveFindAllWithLimit() { - $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment'); - $TestModel =& new Article(); - - $TestModel->hasMany['Comment']['limit'] = 2; - - $result = $TestModel->find('all', array( - 'conditions' => array('Article.user_id' => 1) - )); - $expected = array( - array( - 'Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Comment' => array( - array( - 'id' => '1', - 'article_id' => '1', - 'user_id' => '2', - 'comment' => 'First Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:45:23', - 'updated' => '2007-03-18 10:47:31' - ), - array( - 'id' => '2', - 'article_id' => '1', - 'user_id' => '4', - 'comment' => 'Second Comment for First Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:47:23', - 'updated' => '2007-03-18 10:49:31' - ), - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ))), - array( - 'Article' => array( - 'id' => '3', - 'user_id' => '1', - 'title' => 'Third Article', - 'body' => 'Third Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Comment' => array(), - 'Tag' => array() - ) - ); - $this->assertEqual($result, $expected); - - $TestModel->hasMany['Comment']['limit'] = 1; - - $result = $TestModel->find('all', array( - 'conditions' => array('Article.user_id' => 3), - 'limit' => 1, - 'recursive' => 2 - )); - $expected = array( - array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'Comment' => array( - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31', - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Attachment' => array( - 'id' => '1', - 'comment_id' => 5, - 'attachment' => 'attachment.zip', - 'created' => '2007-03-18 10:51:23', - 'updated' => '2007-03-18 10:53:31' - ) - ) - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ) - ) - ) - ); - $this->assertEqual($result, $expected); - } -} - -/** - * ModelSaveAllTest - * - * @package cake - * @subpackage cake.tests.cases.libs.model - */ -class ModelWriteTest extends BaseModelTest { -/** - * testInsertAnotherHabtmRecordWithSameForeignKey method - * - * @access public - * @return void - */ - function testInsertAnotherHabtmRecordWithSameForeignKey() { - $this->loadFixtures('JoinA', 'JoinB', 'JoinAB'); - $TestModel = new JoinA(); - - $result = $TestModel->JoinAsJoinB->findById(1); - $expected = array( - 'JoinAsJoinB' => array( - 'id' => 1, - 'join_a_id' => 1, - 'join_b_id' => 2, - 'other' => 'Data for Join A 1 Join B 2', - 'created' => '2008-01-03 10:56:33', - 'updated' => '2008-01-03 10:56:33' - )); - $this->assertEqual($result, $expected); - - $TestModel->JoinAsJoinB->create(); - $result = $TestModel->JoinAsJoinB->save(array( - 'join_a_id' => 1, - 'join_b_id' => 1, - 'other' => 'Data for Join A 1 Join B 1', - 'created' => '2008-01-03 10:56:44', - 'updated' => '2008-01-03 10:56:44' - )); - $this->assertTrue($result); - $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID(); - $this->assertTrue($lastInsertId != null); - - $result = $TestModel->JoinAsJoinB->findById(1); - $expected = array( - 'JoinAsJoinB' => array( - 'id' => 1, - 'join_a_id' => 1, - 'join_b_id' => 2, - 'other' => 'Data for Join A 1 Join B 2', - 'created' => '2008-01-03 10:56:33', - 'updated' => '2008-01-03 10:56:33' - )); - $this->assertEqual($result, $expected); - - $updatedValue = 'UPDATED Data for Join A 1 Join B 2'; - $TestModel->JoinAsJoinB->id = 1; - $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false); - $this->assertTrue($result); - - $result = $TestModel->JoinAsJoinB->findById(1); - $this->assertEqual($result['JoinAsJoinB']['other'], $updatedValue); - } -/** - * testSaveDateAsFirstEntry method - * - * @access public - * @return void - */ - function testSaveDateAsFirstEntry() { - $this->loadFixtures('Article'); - - $Article =& new Article(); - - $data = array( - 'Article' => array( - 'created' => array( - 'day' => '1', - 'month' => '1', - 'year' => '2008' - ), - 'title' => 'Test Title', - 'user_id' => 1 - )); - $Article->create(); - $this->assertTrue($Article->save($data)); - - $testResult = $Article->find(array('Article.title' => 'Test Title')); - - $this->assertEqual($testResult['Article']['title'], $data['Article']['title']); - $this->assertEqual($testResult['Article']['created'], '2008-01-01 00:00:00'); - - } -/** - * testUnderscoreFieldSave method - * - * @access public - * @return void - */ - function testUnderscoreFieldSave() { - $this->loadFixtures('UnderscoreField'); - $UnderscoreField =& new UnderscoreField(); - - $currentCount = $UnderscoreField->find('count'); - $this->assertEqual($currentCount, 3); - $data = array('UnderscoreField' => array( - 'user_id' => '1', - 'my_model_has_a_field' => 'Content here', - 'body' => 'Body', - 'published' => 'Y', - 'another_field' => 4 - )); - $ret = $UnderscoreField->save($data); - $this->assertTrue($ret); - - $currentCount = $UnderscoreField->find('count'); - $this->assertEqual($currentCount, 4); - } -/** - * testAutoSaveUuid method - * - * @access public - * @return void - */ - function testAutoSaveUuid() { - // SQLite does not support non-integer primary keys, and SQL Server - // is still having problems with custom PK's - $this->skipIf( - $this->db->config['driver'] == 'sqlite' - || $this->db->config['driver'] == 'mssql' - ); - - $this->loadFixtures('Uuid'); - $TestModel =& new Uuid(); - - $TestModel->save(array('title' => 'Test record')); - $result = $TestModel->findByTitle('Test record'); - $this->assertEqual( - array_keys($result['Uuid']), - array('id', 'title', 'count', 'created', 'updated') - ); - $this->assertEqual(strlen($result['Uuid']['id']), 36); - } -/** - * testZeroDefaultFieldValue method - * - * @access public - * @return void - */ - function testZeroDefaultFieldValue() { - $this->skipIf( - $this->db->config['driver'] == 'sqlite', - '%s SQLite uses loose typing, this operation is unsupported' - ); - $this->loadFixtures('DataTest'); - $TestModel =& new DataTest(); - - $TestModel->create(array()); - $TestModel->save(); - $result = $TestModel->findById($TestModel->id); - $this->assertIdentical($result['DataTest']['count'], '0'); - $this->assertIdentical($result['DataTest']['float'], '0'); - } -/** - * testNonNumericHabtmJoinKey method - * - * @access public - * @return void - */ - function testNonNumericHabtmJoinKey() { - $this->loadFixtures('Post', 'Tag', 'PostsTag'); - $Post =& new Post(); - $Post->bind('Tag', array('type' => 'hasAndBelongsToMany')); - $Post->Tag->primaryKey = 'tag'; - - $result = $Post->find('all'); - $expected = array( - array( - 'Post' => array( - 'id' => '1', - 'author_id' => '1', - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - ), - 'Author' => array( - 'id' => null, - 'user' => null, - 'password' => null, - 'created' => null, - 'updated' => null, - 'test' => 'working' - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ))), - array( - 'Post' => array( - 'id' => '2', - 'author_id' => '3', - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'Author' => array( - 'id' => null, - 'user' => null, - 'password' => null, - 'created' => null, - 'updated' => null, - 'test' => 'working' - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))), - array( - 'Post' => array( - 'id' => '3', - 'author_id' => '1', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ), - 'Author' => array( - 'id' => null, - 'user' => null, - 'password' => null, - 'created' => null, - 'updated' => null, - 'test' => 'working' - ), - 'Tag' => array() - )); - $this->assertEqual($result, $expected); - } -/** - * Tests validation parameter order in custom validation methods - * - * @access public - * @return void - */ - function testAllowSimulatedFields() { - $TestModel =& new ValidationTest1(); - - $TestModel->create(array( - 'title' => 'foo', - 'bar' => 'baz' - )); - $expected = array( - 'ValidationTest1' => array( - 'title' => 'foo', - 'bar' => 'baz' - )); - $this->assertEqual($TestModel->data, $expected); - } -/** - * test that Caches are getting cleared on save(). - * ensure that both inflections of controller names are getting cleared - * as url for controller could be either overallFavorites/index or overall_favorites/index - * - * @return void - **/ - function testCacheClearOnSave() { - $_back = array( - 'check' => Configure::read('Cache.check'), - 'disable' => Configure::read('Cache.disable'), - ); - Configure::write('Cache.check', true); - Configure::write('Cache.disable', false); - - $this->loadFixtures('OverallFavorite'); - $OverallFavorite =& new OverallFavorite(); - - touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'); - touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'); - - $data = array( - 'OverallFavorite' => array( - 'model_type' => '8-track', - 'model_id' => '3', - 'priority' => '1' - ) - ); - $OverallFavorite->create($data); - $OverallFavorite->save(); - - $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php')); - $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php')); - - Configure::write('Cache.check', $_back['check']); - Configure::write('Cache.disable', $_back['disable']); - } -/** - * testSaveWithCounterCache method - * - * @access public - * @return void - */ - function testSaveWithCounterCache() { - $this->loadFixtures('Syfile', 'Item'); - $TestModel =& new Syfile(); - $TestModel2 =& new Item(); - - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], null); - - $TestModel2->save(array( - 'name' => 'Item 7', - 'syfile_id' => 1, - 'published' => false - )); - - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '2'); - - $TestModel2->delete(1); - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '1'); - - $TestModel2->id = 2; - $TestModel2->saveField('syfile_id', 1); - - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '2'); - - $result = $TestModel->findById(2); - $this->assertIdentical($result['Syfile']['item_count'], '0'); - } -/** - * Tests that counter caches are updated when records are added - * - * @access public - * @return void - */ - function testCounterCacheIncrease() { - $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); - $User = new CounterCacheUser(); - $Post = new CounterCachePost(); - $data = array('Post' => array( - 'title' => 'New Post', - 'user_id' => 66 - )); - - $Post->save($data); - $user = $User->find('first', array( - 'conditions' => array('id' => 66), - 'recursive' => -1 - )); - - $result = $user[$User->alias]['post_count']; - $expected = 3; - $this->assertEqual($result, $expected); - } -/** - * Tests that counter caches are updated when records are deleted - * - * @access public - * @return void - */ - function testCounterCacheDecrease() { - $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); - $User = new CounterCacheUser(); - $Post = new CounterCachePost(); - - $Post->del(2); - $user = $User->find('first', array( - 'conditions' => array('id' => 66), - 'recursive' => -1 - )); - - $result = $user[$User->alias]['post_count']; - $expected = 1; - $this->assertEqual($result, $expected); - } -/** - * Tests that counter caches are updated when foreign keys of counted records change - * - * @access public - * @return void - */ - function testCounterCacheUpdated() { - $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); - $User = new CounterCacheUser(); - $Post = new CounterCachePost(); - - $data = $Post->find('first', array( - 'conditions' => array('id' => 1), - 'recursive' => -1 - )); - $data[$Post->alias]['user_id'] = 301; - $Post->save($data); - - $users = $User->find('all',array('order' => 'User.id')); - $this->assertEqual($users[0]['User']['post_count'], 1); - $this->assertEqual($users[1]['User']['post_count'], 2); - } -/** - * Test counter cache with models that use a non-standard (i.e. not using 'id') - * as their primary key. - * - * @access public - * @return void - */ - function testCounterCacheWithNonstandardPrimaryKey() { - $this->loadFixtures( - 'CounterCacheUserNonstandardPrimaryKey', - 'CounterCachePostNonstandardPrimaryKey' - ); - - $User = new CounterCacheUserNonstandardPrimaryKey(); - $Post = new CounterCachePostNonstandardPrimaryKey(); - - $data = $Post->find('first', array( - 'conditions' => array('pid' => 1), - 'recursive' => -1 - )); - $data[$Post->alias]['uid'] = 301; - $Post->save($data); - - $users = $User->find('all',array('order' => 'User.uid')); - $this->assertEqual($users[0]['User']['post_count'], 1); - $this->assertEqual($users[1]['User']['post_count'], 2); - } - -/** - * test Counter Cache With Self Joining table - * - * @return void - * @access public - */ - function testCounterCacheWithSelfJoin() { - $skip = $this->skipIf( - ($this->db->config['driver'] == 'sqlite'), - 'SQLite 2.x does not support ALTER TABLE ADD COLUMN' - ); - if ($skip) { - return; - } - - $this->loadFixtures('CategoryThread'); - $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . " ADD COLUMN child_count INTEGER"); - $Category =& new CategoryThread(); - $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5)); - $this->assertTrue($result); - - $Category =& new CategoryThread(); - $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count'; - $Category->updateCounterCache(array('parent_id' => 5)); - $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count'); - $expected = array_fill(0, 1, 1); - $this->assertEqual($result, $expected); - } -/** - * testSaveWithCounterCacheScope method - * - * @access public - * @return void - */ - function testSaveWithCounterCacheScope() { - $this->loadFixtures('Syfile', 'Item'); - $TestModel =& new Syfile(); - $TestModel2 =& new Item(); - $TestModel2->belongsTo['Syfile']['counterCache'] = true; - $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true); - - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], null); - - $TestModel2->save(array( - 'name' => 'Item 7', - 'syfile_id' => 1, - 'published'=> true - )); - - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '1'); - - $TestModel2->id = 1; - $TestModel2->saveField('published', true); - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '2'); - - $TestModel2->save(array( - 'id' => 1, - 'syfile_id' => 1, - 'published'=> false - )); - - $result = $TestModel->findById(1); - $this->assertIdentical($result['Syfile']['item_count'], '1'); - } -/** - * testValidatesBackwards method - * - * @access public - * @return void - */ - function testValidatesBackwards() { - $TestModel =& new TestValidate(); - - $TestModel->validate = array( - 'user_id' => VALID_NUMBER, - 'title' => VALID_NOT_EMPTY, - 'body' => VALID_NOT_EMPTY - ); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => '', - 'body' => '' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 'title', - 'body' => '' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => '', - 'title' => 'title', - 'body' => 'body' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => 'not a number', - 'title' => 'title', - 'body' => 'body' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 'title', - 'body' => 'body' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - } -/** - * testValidates method - * - * @access public - * @return void - */ - function testValidates() { - $TestModel =& new TestValidate(); - - $TestModel->validate = array( - 'user_id' => 'numeric', - 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'), - 'body' => 'notEmpty' - ); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => '', - 'body' => 'body' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 'title', - 'body' => 'body' - )); - $result = $TestModel->create($data) && $TestModel->validates(); - $this->assertTrue($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => '0', - 'body' => 'body' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date'); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'modified' => '' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'modified' => '2007-05-01' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'modified' => 'invalid-date-here' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'modified' => 0 - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'modified' => '0' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date'); - - $data = array('TestValidate' => array('modified' => null)); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array('modified' => false)); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array('modified' => '')); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'modified' => '2007-05-01' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45)); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'slug' => '' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'slug' => 'slug-right-here' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $data = array('TestValidate' => array( - 'user_id' => '1', - 'title' => 0, - 'body' => 'body', - 'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $TestModel->validate = array( - 'number' => array( - 'rule' => 'validateNumber', - 'min' => 3, - 'max' => 5 - ), - 'title' => array( - 'allowEmpty' => false, - 'rule' => 'notEmpty' - )); - - $data = array('TestValidate' => array( - 'title' => 'title', - 'number' => '0' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'title' => 'title', - 'number' => 0 - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'title' => 'title', - 'number' => '3' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $data = array('TestValidate' => array( - 'title' => 'title', - 'number' => 3 - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $TestModel->validate = array( - 'number' => array( - 'rule' => 'validateNumber', - 'min' => 5, - 'max' => 10 - ), - 'title' => array( - 'allowEmpty' => false, - 'rule' => 'notEmpty' - )); - - $data = array('TestValidate' => array( - 'title' => 'title', - 'number' => '3' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'title' => 'title', - 'number' => 3 - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $TestModel->validate = array( - 'title' => array( - 'allowEmpty' => false, - 'rule' => 'validateTitle' - )); - - $data = array('TestValidate' => array('title' => '')); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array('title' => 'new title')); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array('title' => 'title-new')); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $TestModel->validate = array('title' => array( - 'allowEmpty' => true, - 'rule' => 'validateTitle' - )); - $data = array('TestValidate' => array('title' => '')); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $TestModel->validate = array( - 'title' => array( - 'length' => array( - 'allowEmpty' => true, - 'rule' => array('maxLength', 10) - ))); - $data = array('TestValidate' => array('title' => '')); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $TestModel->validate = array( - 'title' => array( - 'rule' => array('userDefined', 'Article', 'titleDuplicate') - )); - $data = array('TestValidate' => array('title' => 'My Article Title')); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertFalse($result); - - $data = array('TestValidate' => array( - 'title' => 'My Article With a Different Title' - )); - $result = $TestModel->create($data); - $this->assertTrue($result); - $result = $TestModel->validates(); - $this->assertTrue($result); - - $TestModel->validate = array( - 'title' => array( - 'tooShort' => array('rule' => array('minLength', 50)), - 'onlyLetters' => array('rule' => '/^[a-z]+$/i') - ), - ); - $data = array('TestValidate' => array( - 'title' => 'I am a short string' - )); - $TestModel->create($data); - $result = $TestModel->validates(); - $this->assertFalse($result); - $result = $TestModel->validationErrors; - $expected = array( - 'title' => 'onlyLetters' - ); - $this->assertEqual($result, $expected); - - $TestModel->validate = array( - 'title' => array( - 'tooShort' => array( - 'rule' => array('minLength', 50), - 'last' => true - ), - 'onlyLetters' => array('rule' => '/^[a-z]+$/i') - ), - ); - $data = array('TestValidate' => array( - 'title' => 'I am a short string' - )); - $TestModel->create($data); - $result = $TestModel->validates(); - $this->assertFalse($result); - $result = $TestModel->validationErrors; - $expected = array( - 'title' => 'tooShort' - ); - $this->assertEqual($result, $expected); - } -/** - * testSaveField method - * - * @access public - * @return void - */ - function testSaveField() { - $this->loadFixtures('Article'); - $TestModel =& new Article(); - - $TestModel->id = 1; - $result = $TestModel->saveField('title', 'New First Article'); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); - $expected = array('Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'New First Article', - 'body' => 'First Article Body' - )); - $this->assertEqual($result, $expected); - - $TestModel->id = 1; - $result = $TestModel->saveField('title', ''); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); - $expected = array('Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => '', - 'body' => 'First Article Body' - )); - $result['Article']['title'] = trim($result['Article']['title']); - $this->assertEqual($result, $expected); - - $TestModel->id = 1; - $TestModel->set('body', 'Messed up data'); - $this->assertTrue($TestModel->saveField('title', 'First Article')); - $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); - $expected = array('Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body' - )); - $this->assertEqual($result, $expected); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); - - $TestModel->id = 1; - $result = $TestModel->saveField('title', '', true); - $this->assertFalse($result); - - $this->loadFixtures('Node', 'Dependency'); - $Node =& new Node(); - $Node->set('id', 1); - $result = $Node->read(); - $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); - - $Node->saveField('state', 10); - $result = $Node->read(); - $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); - } -/** - * testSaveWithCreate method - * - * @access public - * @return void - */ - function testSaveWithCreate() { - $this->loadFixtures( - 'User', - 'Article', - 'User', - 'Comment', - 'Tag', - 'ArticlesTag', - 'Attachment' - ); - $TestModel =& new User(); - - $data = array('User' => array( - 'user' => 'user', - 'password' => '' - )); - $result = $TestModel->save($data); - $this->assertFalse($result); - $this->assertTrue(!empty($TestModel->validationErrors)); - - $TestModel =& new Article(); - - $data = array('Article' => array( - 'user_id' => '', - 'title' => '', - 'body' => '' - )); - $result = $TestModel->create($data) && $TestModel->save(); - $this->assertFalse($result); - $this->assertTrue(!empty($TestModel->validationErrors)); - - $data = array('Article' => array( - 'id' => 1, - 'user_id' => '1', - 'title' => 'New First Article', - 'body' => '' - )); - $result = $TestModel->create($data) && $TestModel->save(); - $this->assertFalse($result); - - $data = array('Article' => array( - 'id' => 1, - 'title' => 'New First Article' - )); - $result = $TestModel->create() && $TestModel->save($data, false); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1); - $expected = array('Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'New First Article', - 'body' => 'First Article Body', - 'published' => 'N' - )); - $this->assertEqual($result, $expected); - - $data = array('Article' => array( - 'id' => 1, - 'user_id' => '2', - 'title' => 'First Article', - 'body' => 'New First Article Body', - 'published' => 'Y' - )); - $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published')); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1); - $expected = array('Article' => array( - 'id' => '1', - 'user_id' => '1', - 'title' => 'First Article', - 'body' => 'First Article Body', - 'published' => 'Y' - )); - $this->assertEqual($result, $expected); - - $data = array( - 'Article' => array( - 'user_id' => '2', - 'title' => 'New Article', - 'body' => 'New Article Body', - 'created' => '2007-03-18 14:55:23', - 'updated' => '2007-03-18 14:57:31' - ), - 'Tag' => array('Tag' => array(1, 3)) - ); - $TestModel->create(); - $result = $TestModel->create() && $TestModel->save($data); - $this->assertTrue($result); - - $TestModel->recursive = 2; - $result = $TestModel->read(null, 4); - $expected = array( - 'Article' => array( - 'id' => '4', - 'user_id' => '2', - 'title' => 'New Article', - 'body' => 'New Article Body', - 'published' => 'N', - 'created' => '2007-03-18 14:55:23', - 'updated' => '2007-03-18 14:57:31' - ), - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - ), - 'Comment' => array(), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - $this->assertEqual($result, $expected); - - $data = array('Comment' => array( - 'article_id' => '4', - 'user_id' => '1', - 'comment' => 'Comment New Article', - 'published' => 'Y', - 'created' => '2007-03-18 14:57:23', - 'updated' => '2007-03-18 14:59:31' - )); - $result = $TestModel->Comment->create() && $TestModel->Comment->save($data); - $this->assertTrue($result); - - $data = array('Attachment' => array( - 'comment_id' => '7', - 'attachment' => 'newattachment.zip', - 'created' => '2007-03-18 15:02:23', - 'updated' => '2007-03-18 15:04:31' - )); - $result = $TestModel->Comment->Attachment->save($data); - $this->assertTrue($result); - - $TestModel->recursive = 2; - $result = $TestModel->read(null, 4); - $expected = array( - 'Article' => array( - 'id' => '4', - 'user_id' => '2', - 'title' => 'New Article', - 'body' => 'New Article Body', - 'published' => 'N', - 'created' => '2007-03-18 14:55:23', - 'updated' => '2007-03-18 14:57:31' - ), - 'User' => array( - 'id' => '2', - 'user' => 'nate', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:18:23', - 'updated' => '2007-03-17 01:20:31' - ), - 'Comment' => array( - array( - 'id' => '7', - 'article_id' => '4', - 'user_id' => '1', - 'comment' => 'Comment New Article', - 'published' => 'Y', - 'created' => '2007-03-18 14:57:23', - 'updated' => '2007-03-18 14:59:31', - 'Article' => array( - 'id' => '4', - 'user_id' => '2', - 'title' => 'New Article', - 'body' => 'New Article Body', - 'published' => 'N', - 'created' => '2007-03-18 14:55:23', - 'updated' => '2007-03-18 14:57:31' - ), - 'User' => array( - 'id' => '1', - 'user' => 'mariano', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:16:23', - 'updated' => '2007-03-17 01:18:31' - ), - 'Attachment' => array( - 'id' => '2', - 'comment_id' => '7', - 'attachment' => 'newattachment.zip', - 'created' => '2007-03-18 15:02:23', - 'updated' => '2007-03-18 15:04:31' - ))), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - - $this->assertEqual($result, $expected); - } -/** - * testSaveWithSet method - * - * @access public - * @return void - */ - function testSaveWithSet() { - $this->loadFixtures('Article'); - $TestModel =& new Article(); - - // Create record we will be updating later - - $data = array('Article' => array( - 'user_id' => '1', - 'title' => 'Fourth Article', - 'body' => 'Fourth Article Body', - 'published' => 'Y' - )); - $result = $TestModel->create() && $TestModel->save($data); - $this->assertTrue($result); - - // Check record we created - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); - $expected = array('Article' => array( - 'id' => '4', - 'user_id' => '1', - 'title' => 'Fourth Article', - 'body' => 'Fourth Article Body', - 'published' => 'Y' - )); - $this->assertEqual($result, $expected); - - // Create new record just to overlap Model->id on previously created record - - $data = array('Article' => array( - 'user_id' => '4', - 'title' => 'Fifth Article', - 'body' => 'Fifth Article Body', - 'published' => 'Y' - )); - $result = $TestModel->create() && $TestModel->save($data); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); - $expected = array('Article' => array( - 'id' => '5', - 'user_id' => '4', - 'title' => 'Fifth Article', - 'body' => 'Fifth Article Body', - 'published' => 'Y' - )); - $this->assertEqual($result, $expected); - - // Go back and edit the first article we created, starting by checking it's still there - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); - $expected = array('Article' => array( - 'id' => '4', - 'user_id' => '1', - 'title' => 'Fourth Article', - 'body' => 'Fourth Article Body', - 'published' => 'Y' - )); - $this->assertEqual($result, $expected); - - // And now do the update with set() - - $data = array('Article' => array( - 'id' => '4', - 'title' => 'Fourth Article - New Title', - 'published' => 'N' - )); - $result = $TestModel->set($data) && $TestModel->save(); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); - $expected = array('Article' => array( - 'id' => '4', - 'user_id' => '1', - 'title' => 'Fourth Article - New Title', - 'body' => 'Fourth Article Body', - 'published' => 'N' - )); - $this->assertEqual($result, $expected); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); - $expected = array('Article' => array( - 'id' => '5', - 'user_id' => '4', - 'title' => 'Fifth Article', - 'body' => 'Fifth Article Body', - 'published' => 'Y' - )); - $this->assertEqual($result, $expected); - - $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5')); - $result = ($TestModel->set($data) && $TestModel->save()); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); - $expected = array('Article' => array( - 'id' => '5', - 'user_id' => '4', - 'title' => 'Fifth Article - New Title 5', - 'body' => 'Fifth Article Body', - 'published' => 'Y' - )); - $this->assertEqual($result, $expected); - - $TestModel->recursive = -1; - $result = $TestModel->find('all', array('fields' => array('id', 'title'))); - $expected = array( - array('Article' => array('id' => 1, 'title' => 'First Article' )), - array('Article' => array('id' => 2, 'title' => 'Second Article' )), - array('Article' => array('id' => 3, 'title' => 'Third Article' )), - array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title' )), - array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5' )) - ); - $this->assertEqual($result, $expected); - } -/** - * testSaveWithNonExistentFields method - * - * @access public - * @return void - */ - function testSaveWithNonExistentFields() { - $this->loadFixtures('Article'); - $TestModel =& new Article(); - $TestModel->recursive = -1; - - $data = array( - 'non_existent' => 'This field does not exist', - 'user_id' => '1', - 'title' => 'Fourth Article - New Title', - 'body' => 'Fourth Article Body', - 'published' => 'N' - ); - $result = $TestModel->create() && $TestModel->save($data); - $this->assertTrue($result); - - $expected = array('Article' => array( - 'id' => '4', - 'user_id' => '1', - 'title' => 'Fourth Article - New Title', - 'body' => 'Fourth Article Body', - 'published' => 'N' - )); - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); - $this->assertEqual($result, $expected); - - $data = array( - 'user_id' => '1', - 'non_existent' => 'This field does not exist', - 'title' => 'Fiveth Article - New Title', - 'body' => 'Fiveth Article Body', - 'published' => 'N' - ); - $result = $TestModel->create() && $TestModel->save($data); - $this->assertTrue($result); - - $expected = array('Article' => array( - 'id' => '5', - 'user_id' => '1', - 'title' => 'Fiveth Article - New Title', - 'body' => 'Fiveth Article Body', - 'published' => 'N' - )); - $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); - $this->assertEqual($result, $expected); - } -/** - * testSaveFromXml method - * - * @access public - * @return void - */ - function testSaveFromXml() { - $this->loadFixtures('Article'); - App::import('Core', 'Xml'); - - $Article = new Article(); - $Article->save(new Xml('
')); - $this->assertTrue($Article->save(new Xml('
'))); - - $results = $Article->find(array('Article.title' => 'test xml')); - $this->assertTrue($results); - } -/** - * testSaveHabtm method - * - * @access public - * @return void - */ - function testSaveHabtm() { - $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag'); - $TestModel =& new Article(); - - $result = $TestModel->findById(2); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Second Article', - 'body' => 'Second Article Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - ), - 'User' => array( - 'id' => '3', - 'user' => 'larry', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', - 'created' => '2007-03-17 01:20:23', - 'updated' => '2007-03-17 01:22:31' - ), - 'Comment' => array( - array( - 'id' => '5', - 'article_id' => '2', - 'user_id' => '1', - 'comment' => 'First Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:53:23', - 'updated' => '2007-03-18 10:55:31' - ), - array( - 'id' => '6', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y', - 'created' => '2007-03-18 10:55:23', - 'updated' => '2007-03-18 10:57:31' - )), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ) - ) - ); - $this->assertEqual($result, $expected); - - $data = array( - 'Article' => array( - 'id' => '2', - 'title' => 'New Second Article' - ), - 'Tag' => array('Tag' => array(1, 2)) - ); - - $this->assertTrue($TestModel->set($data)); - $this->assertTrue($TestModel->save()); - - $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); - $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'New Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ))); - $this->assertEqual($result, $expected); - - $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3))); - $result = $TestModel->set($data); - $this->assertTrue($result); - - $result = $TestModel->save(); - $this->assertTrue($result); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'New Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - $this->assertEqual($result, $expected); - - $data = array('Tag' => array('Tag' => array(1, 2, 3))); - - $result = $TestModel->set($data); - $this->assertTrue($result); - - $result = $TestModel->save(); - $this->assertTrue($result); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'New Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - $this->assertEqual($result, $expected); - - $data = array('Tag' => array('Tag' => array())); - $result = $TestModel->set($data); - $this->assertTrue($result); - - $result = $TestModel->save(); - $this->assertTrue($result); - - $data = array('Tag' => array('Tag' => '')); - $result = $TestModel->set($data); - $this->assertTrue($result); - - $result = $TestModel->save(); - $this->assertTrue($result); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'New Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array() - ); - $this->assertEqual($result, $expected); - - $data = array('Tag' => array('Tag' => array(2, 3))); - $result = $TestModel->set($data); - $this->assertTrue($result); - - $result = $TestModel->save(); - $this->assertTrue($result); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'New Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - $this->assertEqual($result, $expected); - - $data = array( - 'Tag' => array( - 'Tag' => array(1, 2) - ), - 'Article' => array( - 'id' => '2', - 'title' => 'New Second Article' - )); - $this->assertTrue($TestModel->set($data)); - $this->assertTrue($TestModel->save()); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'New Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ))); - $this->assertEqual($result, $expected); - - $data = array( - 'Tag' => array( - 'Tag' => array(1, 2) - ), - 'Article' => array( - 'id' => '2', - 'title' => 'New Second Article Title' - )); - $result = $TestModel->set($data); - $this->assertTrue($result); - $this->assertTrue($TestModel->save()); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'New Second Article Title', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ) - ) - ); - $this->assertEqual($result, $expected); - - $data = array( - 'Tag' => array( - 'Tag' => array(2, 3) - ), - 'Article' => array( - 'id' => '2', - 'title' => 'Changed Second Article' - )); - $this->assertTrue($TestModel->set($data)); - $this->assertTrue($TestModel->save()); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Changed Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '2', - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ) - ) - ); - $this->assertEqual($result, $expected); - - $data = array( - 'Tag' => array( - 'Tag' => array(1, 3) - ), - 'Article' => array('id' => '2'), - ); - - $result = $TestModel->set($data); - $this->assertTrue($result); - - $result = $TestModel->save(); - $this->assertTrue($result); - - $TestModel->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); - $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); - $expected = array( - 'Article' => array( - 'id' => '2', - 'user_id' => '3', - 'title' => 'Changed Second Article', - 'body' => 'Second Article Body' - ), - 'Tag' => array( - array( - 'id' => '1', - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - array( - 'id' => '3', - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - $this->assertEqual($result, $expected); - - $data = array( - 'Article' => array( - 'id' => 10, - 'user_id' => '2', - 'title' => 'New Article With Tags and fieldList', - 'body' => 'New Article Body with Tags and fieldList', - 'created' => '2007-03-18 14:55:23', - 'updated' => '2007-03-18 14:57:31' - ), - 'Tag' => array( - 'Tag' => array(1, 2, 3) - )); - $result = $TestModel->create() - && $TestModel->save($data, true, array('user_id', 'title', 'published')); - $this->assertTrue($result); - - $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); - $result = $TestModel->read(); - $expected = array( - 'Article' => array( - 'id' => 4, - 'user_id' => 2, - 'title' => 'New Article With Tags and fieldList', - 'body' => '', - 'published' => 'N', - 'created' => '', - 'updated' => '' - ), - 'Tag' => array( - 0 => array( - 'id' => 1, - 'tag' => 'tag1', - 'created' => '2007-03-18 12:22:23', - 'updated' => '2007-03-18 12:24:31' - ), - 1 => array( - 'id' => 2, - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ), - 2 => array( - 'id' => 3, - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))); - $this->assertEqual($result, $expected); - - - $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB'); - $TestModel = new JoinA(); - $TestModel->hasBelongsToMany['JoinC']['unique'] = true; - $data = array( - 'JoinA' => array( - 'id' => 1, - 'name' => 'Join A 1', - 'body' => 'Join A 1 Body', - ), - 'JoinC' => array( - 'JoinC' => array( - array('join_c_id' => 2, 'other' => 'new record'), - array('join_c_id' => 3, 'other' => 'new record') - ) - ) - ); - $TestModel->save($data); - $result = $TestModel->read(null, 1); - $time = date('Y-M-D H:i:s'); - $expected = array(4, 5); - $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected); - $expected = array('new record', 'new record'); - $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected); - } -/** - * testSaveHabtmCustomKeys method - * - * @access public - * @return void - */ - function testSaveHabtmCustomKeys() { - $this->loadFixtures('Story', 'StoriesTag', 'Tag'); - $Story =& new Story(); - - $data = array( - 'Story' => array('story' => '1'), - 'Tag' => array( - 'Tag' => array(2, 3) - )); - $result = $Story->set($data); - $this->assertTrue($result); - - $result = $Story->save(); - $this->assertTrue($result); - - $result = $Story->find('all'); - $expected = array( - array( - 'Story' => array( - 'story' => 1, - 'title' => 'First Story' - ), - 'Tag' => array( - array( - 'id' => 2, - 'tag' => 'tag2', - 'created' => '2007-03-18 12:24:23', - 'updated' => '2007-03-18 12:26:31' - ), - array( - 'id' => 3, - 'tag' => 'tag3', - 'created' => '2007-03-18 12:26:23', - 'updated' => '2007-03-18 12:28:31' - ))), - array( - 'Story' => array( - 'story' => 2, - 'title' => 'Second Story' - ), - 'Tag' => array() - )); - $this->assertEqual($result, $expected); - } -/** - * testHabtmSaveKeyResolution method - * - * @access public - * @return void - */ - function testHabtmSaveKeyResolution() { - $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); - $ThePaper =& new ThePaper(); - - $ThePaper->id = 1; - $ThePaper->save(array('Monkey' => array(2, 3))); - - $result = $ThePaper->findById(1); - $expected = array( - array( - 'id' => '2', - 'device_type_id' => '1', - 'name' => 'Device 2', - 'typ' => '1' - ), - array( - 'id' => '3', - 'device_type_id' => '1', - 'name' => 'Device 3', - 'typ' => '2' - )); - $this->assertEqual($result['Monkey'], $expected); - - $ThePaper->id = 2; - $ThePaper->save(array('Monkey' => array(1, 2, 3))); - - $result = $ThePaper->findById(2); - $expected = array( - array( - 'id' => '1', - 'device_type_id' => '1', - 'name' => 'Device 1', - 'typ' => '1' - ), - array( - 'id' => '2', - 'device_type_id' => '1', - 'name' => 'Device 2', - 'typ' => '1' - ), - array( - 'id' => '3', - 'device_type_id' => '1', - 'name' => 'Device 3', - 'typ' => '2' - )); - $this->assertEqual($result['Monkey'], $expected); - - $ThePaper->id = 2; - $ThePaper->save(array('Monkey' => array(1, 3))); - - $result = $ThePaper->findById(2); - $expected = array( - array( - 'id' => '1', - 'device_type_id' => '1', - 'name' => 'Device 1', - 'typ' => '1' - ), - array( - 'id' => '3', - 'device_type_id' => '1', - 'name' => 'Device 3', - 'typ' => '2' - )); - $this->assertEqual($result['Monkey'], $expected); - - $result = $ThePaper->findById(1); - $expected = array( - array( - 'id' => '2', - 'device_type_id' => '1', - 'name' => 'Device 2', - 'typ' => '1' - ), - array( - 'id' => '3', - 'device_type_id' => '1', - 'name' => 'Device 3', - 'typ' => '2' - )); - $this->assertEqual($result['Monkey'], $expected); - } -/** - * testCreationOfEmptyRecord method - * - * @access public - * @return void - */ - function testCreationOfEmptyRecord() { - $this->loadFixtures('Author'); - $TestModel =& new Author(); - $this->assertEqual($TestModel->find('count'), 4); - - $TestModel->deleteAll(true, false, false); - $this->assertEqual($TestModel->find('count'), 0); - - $result = $TestModel->save(); - $this->assertTrue(isset($result['Author']['created'])); - $this->assertTrue(isset($result['Author']['updated'])); - $this->assertEqual($TestModel->find('count'), 1); - } -/** - * testCreateWithPKFiltering method - * - * @access public - * @return void - */ - function testCreateWithPKFiltering() { - $TestModel =& new Article(); - $data = array( - 'id' => 5, - 'user_id' => 2, - 'title' => 'My article', - 'body' => 'Some text' - ); - - $result = $TestModel->create($data); - $expected = array( - 'Article' => array( - 'published' => 'N', - 'id' => 5, - 'user_id' => 2, - 'title' => 'My article', - 'body' => 'Some text' - )); - - $this->assertEqual($result, $expected); - $this->assertEqual($TestModel->id, 5); - - $result = $TestModel->create($data, true); - $expected = array( - 'Article' => array( - 'published' => 'N', - 'id' => false, - 'user_id' => 2, - 'title' => 'My article', - 'body' => 'Some text' - )); - - $this->assertEqual($result, $expected); - $this->assertFalse($TestModel->id); - - $result = $TestModel->create(array('Article' => $data), true); - $expected = array( - 'Article' => array( - 'published' => 'N', - 'id' => false, - 'user_id' => 2, - 'title' => 'My article', - 'body' => 'Some text' - )); - - $this->assertEqual($result, $expected); - $this->assertFalse($TestModel->id); - - $data = array( - 'id' => 6, - 'user_id' => 2, - 'title' => 'My article', - 'body' => 'Some text', - 'created' => '1970-01-01 00:00:00', - 'updated' => '1970-01-01 12:00:00', - 'modified' => '1970-01-01 12:00:00' - ); - - $result = $TestModel->create($data); - $expected = array( - 'Article' => array( - 'published' => 'N', - 'id' => 6, - 'user_id' => 2, - 'title' => 'My article', - 'body' => 'Some text', - 'created' => '1970-01-01 00:00:00', - 'updated' => '1970-01-01 12:00:00', - 'modified' => '1970-01-01 12:00:00' - )); - $this->assertEqual($result, $expected); - $this->assertEqual($TestModel->id, 6); - - $result = $TestModel->create(array( - 'Article' => array_diff_key($data, array( - 'created' => true, - 'updated' => true, - 'modified' => true - ))), true); - $expected = array( - 'Article' => array( - 'published' => 'N', - 'id' => false, - 'user_id' => 2, - 'title' => 'My article', - 'body' => 'Some text' - )); - $this->assertEqual($result, $expected); - $this->assertFalse($TestModel->id); - } -/** - * testCreationWithMultipleData method - * - * @access public - * @return void - */ - function testCreationWithMultipleData() { - $this->loadFixtures('Article', 'Comment'); - $Article =& new Article(); - $Comment =& new Comment(); - - $articles = $Article->find('all', array( - 'fields' => array('id','title'), - 'recursive' => -1 - )); - - $comments = $Comment->find('all', array( - 'fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1)); - - $this->assertEqual($articles, array( - array('Article' => array( - 'id' => 1, - 'title' => 'First Article' - )), - array('Article' => array( - 'id' => 2, - 'title' => 'Second Article' - )), - array('Article' => array( - 'id' => 3, - 'title' => 'Third Article' - )))); - - $this->assertEqual($comments, array( - array('Comment' => array( - 'id' => 1, - 'article_id' => 1, - 'user_id' => 2, - 'comment' => 'First Comment for First Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 2, - 'article_id' => 1, - 'user_id' => 4, - 'comment' => 'Second Comment for First Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 3, - 'article_id' => 1, - 'user_id' => 1, - 'comment' => 'Third Comment for First Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 4, - 'article_id' => 1, - 'user_id' => 1, - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N' - )), - array('Comment' => array( - 'id' => 5, - 'article_id' => 2, - 'user_id' => 1, - 'comment' => 'First Comment for Second Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 6, - 'article_id' => 2, - 'user_id' => 2, - 'comment' => 'Second Comment for Second Article', - 'published' => 'Y' - )))); - - $data = array( - 'Comment' => array( - 'article_id' => 2, - 'user_id' => 4, - 'comment' => 'Brand New Comment', - 'published' => 'N' - ), - 'Article' => array( - 'id' => 2, - 'title' => 'Second Article Modified' - )); - - $result = $Comment->create($data); - - $this->assertTrue($result); - $result = $Comment->save(); - $this->assertTrue($result); - - $articles = $Article->find('all', array( - 'fields' => array('id','title'), - 'recursive' => -1 - )); - - $comments = $Comment->find('all', array( - 'fields' => array('id','article_id','user_id','comment','published'), - 'recursive' => -1 - )); - - $this->assertEqual($articles, array( - array('Article' => array( - 'id' => 1, - 'title' => 'First Article' - )), - array('Article' => array( - 'id' => 2, - 'title' => 'Second Article' - )), - array('Article' => array( - 'id' => 3, - 'title' => 'Third Article' - )))); - - $this->assertEqual($comments, array( - array('Comment' => array( - 'id' => 1, - 'article_id' => 1, - 'user_id' => 2, - 'comment' => 'First Comment for First Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 2, - 'article_id' => 1, - 'user_id' => 4, - 'comment' => 'Second Comment for First Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 3, - 'article_id' => 1, - 'user_id' => 1, - 'comment' => 'Third Comment for First Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 4, - 'article_id' => 1, - 'user_id' => 1, - 'comment' => 'Fourth Comment for First Article', - 'published' => 'N' - )), - array('Comment' => array( - 'id' => 5, - 'article_id' => 2, - 'user_id' => 1, - 'comment' => 'First Comment for Second Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 6, - 'article_id' => 2, - 'user_id' => 2, 'comment' => - 'Second Comment for Second Article', - 'published' => 'Y' - )), - array('Comment' => array( - 'id' => 7, - 'article_id' => 2, - 'user_id' => 4, - 'comment' => 'Brand New Comment', - 'published' => 'N' - )))); - - } -/** - * testCreationWithMultipleDataSameModel method - * - * @access public - * @return void - */ - function testCreationWithMultipleDataSameModel() { - $this->loadFixtures('Article'); - $Article =& new Article(); - $SecondaryArticle =& new Article(); - - $result = $Article->field('title', array('id' => 1)); - $this->assertEqual($result, 'First Article'); - - $data = array( - 'Article' => array( - 'user_id' => 2, - 'title' => 'Brand New Article', - 'body' => 'Brand New Article Body', - 'published' => 'Y' - ), - 'SecondaryArticle' => array( - 'id' => 1 - )); - - $Article->create(); - $result = $Article->save($data); - $this->assertTrue($result); - - $result = $Article->getInsertID(); - $this->assertTrue(!empty($result)); - - $result = $Article->field('title', array('id' => 1)); - $this->assertEqual($result, 'First Article'); - - $articles = $Article->find('all', array( - 'fields' => array('id','title'), - 'recursive' => -1 - )); - - $this->assertEqual($articles, array( - array('Article' => array( - 'id' => 1, - 'title' => 'First Article' - )), - array('Article' => array( - 'id' => 2, - 'title' => 'Second Article' - )), - array('Article' => array( - 'id' => 3, - 'title' => 'Third Article' - )), - array('Article' => array( - 'id' => 4, - 'title' => 'Brand New Article' - )))); - } -/** - * testCreationWithMultipleDataSameModelManualInstances method - * - * @access public - * @return void - */ - function testCreationWithMultipleDataSameModelManualInstances() { - $this->loadFixtures('PrimaryModel'); - $Primary =& new PrimaryModel(); - $Secondary =& new PrimaryModel(); - - $result = $Primary->field('primary_name', array('id' => 1)); - $this->assertEqual($result, 'Primary Name Existing'); - - $data = array( - 'PrimaryModel' => array( - 'primary_name' => 'Primary Name New' - ), - 'SecondaryModel' => array( - 'id' => array(1) - )); - - $Primary->create(); - $result = $Primary->save($data); - $this->assertTrue($result); - - $result = $Primary->field('primary_name', array('id' => 1)); - $this->assertEqual($result, 'Primary Name Existing'); - - $result = $Primary->getInsertID(); - $this->assertTrue(!empty($result)); - - $result = $Primary->field('primary_name', array('id' => $result)); - $this->assertEqual($result, 'Primary Name New'); - - $result = $Primary->find('count'); - $this->assertEqual($result, 2); - } -/** - * testRecordExists method - * - * @access public - * @return void - */ - function testRecordExists() { - $this->loadFixtures('User'); - $TestModel =& new User(); - - $this->assertFalse($TestModel->exists()); - $TestModel->read(null, 1); - $this->assertTrue($TestModel->exists()); - $TestModel->create(); - $this->assertFalse($TestModel->exists()); - $TestModel->id = 4; - $this->assertTrue($TestModel->exists()); - - $TestModel =& new TheVoid(); - $this->assertFalse($TestModel->exists()); - $TestModel->id = 5; - $this->assertFalse($TestModel->exists()); - } -/** - * testUpdateExisting method - * - * @access public - * @return void - */ - function testUpdateExisting() { - $this->loadFixtures('User', 'Article', 'Comment'); - $TestModel =& new User(); - $TestModel->create(); - - $TestModel->save(array( - 'User' => array( - 'user' => 'some user', - 'password' => 'some password' - ))); - $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5)); - $id = $TestModel->id; - - $TestModel->save(array( - 'User' => array( - 'user' => 'updated user' - ))); - $this->assertEqual($TestModel->id, $id); - - $result = $TestModel->findById($id); - $this->assertEqual($result['User']['user'], 'updated user'); - $this->assertEqual($result['User']['password'], 'some password'); - - $Article =& new Article(); - $Comment =& new Comment(); - $data = array( - 'Comment' => array( - 'id' => 1, - 'comment' => 'First Comment for First Article' - ), - 'Article' => array( - 'id' => 2, - 'title' => 'Second Article' - )); - - $result = $Article->save($data); - $this->assertTrue($result); - - $result = $Comment->save($data); - $this->assertTrue($result); - } -/** - * testUpdateMultiple method - * - * @access public - * @return void - */ - function testUpdateMultiple() { - $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread'); - $TestModel =& new Comment(); - $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id'); - $expected = array('2', '4', '1', '1', '1', '2'); - $this->assertEqual($result, $expected); - - $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2)); - $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id'); - $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5); - $this->assertEqual($result, $expected); - - $result = $TestModel->updateAll( - array('Comment.comment' => "'Updated today'"), - array('Comment.user_id' => 5) - ); - $this->assertTrue($result); - $result = Set::extract( - $TestModel->find('all', array( - 'conditions' => array( - 'Comment.user_id' => 5 - ))), - '{n}.Comment.comment' - ); - $expected = array_fill(0, 2, 'Updated today'); - $this->assertEqual($result, $expected); - } -/** - * testHabtmUuidWithUuidId method - * - * @access public - * @return void - */ - function testHabtmUuidWithUuidId() { - $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio'); - $TestModel =& new Uuidportfolio(); - - $data = array('Uuidportfolio' => array('name' => 'Portfolio 3')); - $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569'); - $TestModel->create($data); - $TestModel->save(); - $id = $TestModel->id; - $result = $TestModel->read(null, $id); - $this->assertEqual(1, count($result['Uuiditem'])); - $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36); - } -/** - * test HABTM saving when join table has no primary key and only 2 columns. - * - * @return void - **/ - function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() { - $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); - $Fruit =& new Fruit(); - $data = array( - 'Fruit' => array( - 'color' => 'Red', - 'shape' => 'Heart-shaped', - 'taste' => 'sweet', - 'name' => 'Strawberry', - ), - 'UuidTag' => array( - 'UuidTag' => array( - '481fc6d0-b920-43e0-e50f-6d1740cf8569' - ) - ) - ); - $this->assertTrue($Fruit->save($data)); - } -/** - * test HABTM saving when join table has no primary key and only 2 columns, no with model is used. - * - * @return void - **/ - function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() { - $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); - $Fruit =& new FruitNoWith(); - $data = array( - 'Fruit' => array( - 'color' => 'Red', - 'shape' => 'Heart-shaped', - 'taste' => 'sweet', - 'name' => 'Strawberry', - ), - 'UuidTag' => array( - 'UuidTag' => array( - '481fc6d0-b920-43e0-e50f-6d1740cf8569' - ) - ) - ); - $this->assertTrue($Fruit->save($data)); - } - -/** - * testHabtmUuidWithNumericId method - * - * @access public - * @return void - */ - function testHabtmUuidWithNumericId() { - $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid'); - $TestModel =& new Uuiditem(); - - $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0)); - $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569'); - $TestModel->create($data); - $TestModel->save(); - $id = $TestModel->id; - $result = $TestModel->read(null, $id); - $this->assertEqual(1, count($result['Uuidportfolio'])); - } -/** - * testSaveMultipleHabtm method - * - * @access public - * @return void - */ - function testSaveMultipleHabtm() { - $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC'); - $TestModel = new JoinA(); - $result = $TestModel->findById(1); - - $expected = array( - 'JoinA' => array( - 'id' => 1, - 'name' => 'Join A 1', - 'body' => 'Join A 1 Body', - 'created' => '2008-01-03 10:54:23', - 'updated' => '2008-01-03 10:54:23' - ), - 'JoinB' => array( - 0 => array( - 'id' => 2, - 'name' => 'Join B 2', - 'created' => '2008-01-03 10:55:02', - 'updated' => '2008-01-03 10:55:02', - 'JoinAsJoinB' => array( - 'id' => 1, - 'join_a_id' => 1, - 'join_b_id' => 2, - 'other' => 'Data for Join A 1 Join B 2', - 'created' => '2008-01-03 10:56:33', - 'updated' => '2008-01-03 10:56:33' - ))), - 'JoinC' => array( - 0 => array( - 'id' => 2, - 'name' => 'Join C 2', - 'created' => '2008-01-03 10:56:12', - 'updated' => '2008-01-03 10:56:12', - 'JoinAsJoinC' => array( - 'id' => 1, - 'join_a_id' => 1, - 'join_c_id' => 2, - 'other' => 'Data for Join A 1 Join C 2', - 'created' => '2008-01-03 10:57:22', - 'updated' => '2008-01-03 10:57:22' - )))); - - $this->assertEqual($result, $expected); - - $ts = date('Y-m-d H:i:s'); - $TestModel->id = 1; - $data = array( - 'JoinA' => array( - 'id' => '1', - 'name' => 'New name for Join A 1', - 'updated' => $ts - ), - 'JoinB' => array( - array( - 'id' => 1, - 'join_b_id' => 2, - 'other' => 'New data for Join A 1 Join B 2', - 'created' => $ts, - 'updated' => $ts - )), - 'JoinC' => array( - array( - 'id' => 1, - 'join_c_id' => 2, - 'other' => 'New data for Join A 1 Join C 2', - 'created' => $ts, - 'updated' => $ts - ))); - - $TestModel->set($data); - $TestModel->save(); - - $result = $TestModel->findById(1); - $expected = array( - 'JoinA' => array( - 'id' => 1, - 'name' => 'New name for Join A 1', - 'body' => 'Join A 1 Body', - 'created' => '2008-01-03 10:54:23', - 'updated' => $ts - ), - 'JoinB' => array( - 0 => array( - 'id' => 2, - 'name' => 'Join B 2', - 'created' => '2008-01-03 10:55:02', - 'updated' => '2008-01-03 10:55:02', - 'JoinAsJoinB' => array( - 'id' => 1, - 'join_a_id' => 1, - 'join_b_id' => 2, - 'other' => 'New data for Join A 1 Join B 2', - 'created' => $ts, - 'updated' => $ts - ))), - 'JoinC' => array( - 0 => array( - 'id' => 2, - 'name' => 'Join C 2', - 'created' => '2008-01-03 10:56:12', - 'updated' => '2008-01-03 10:56:12', - 'JoinAsJoinC' => array( - 'id' => 1, - 'join_a_id' => 1, - 'join_c_id' => 2, - 'other' => 'New data for Join A 1 Join C 2', - 'created' => $ts, - 'updated' => $ts - )))); - - $this->assertEqual($result, $expected); - } -/** - * testSaveAll method - * - * @access public - * @return void - */ - function testSaveAll() { - $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); - $TestModel =& new Post(); - - $result = $TestModel->find('all'); - $this->assertEqual(count($result), 3); - $this->assertFalse(isset($result[3])); - $ts = date('Y-m-d H:i:s'); - - $TestModel->saveAll(array( - 'Post' => array( - 'title' => 'Post with Author', - 'body' => 'This post will be saved with an author' - ), - 'Author' => array( - 'user' => 'bob', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf90' - ))); - - $result = $TestModel->find('all'); - $expected = array( - 'Post' => array( - 'id' => '4', - 'author_id' => '5', - 'title' => 'Post with Author', - 'body' => 'This post will be saved with an author', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - ), - 'Author' => array( - 'id' => '5', - 'user' => 'bob', - 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', - 'created' => $ts, - 'updated' => $ts, - 'test' => 'working' - )); - $this->assertEqual($result[3], $expected); - $this->assertEqual(count($result), 4); - - $TestModel->deleteAll(true); - $this->assertEqual($TestModel->find('all'), array()); - - // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass - $this->db->truncate($TestModel); - - $ts = date('Y-m-d H:i:s'); - $TestModel->saveAll(array( - array( - 'title' => 'Multi-record post 1', - 'body' => 'First multi-record post', - 'author_id' => 2 - ), - array( - 'title' => 'Multi-record post 2', - 'body' => 'Second multi-record post', - 'author_id' => 2 - ))); - - $result = $TestModel->find('all', array( - 'recursive' => -1, - 'order' => 'Post.id ASC' - )); - $expected = array( - array( - 'Post' => array( - 'id' => '1', - 'author_id' => '2', - 'title' => 'Multi-record post 1', - 'body' => 'First multi-record post', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - )), - array( - 'Post' => array( - 'id' => '2', - 'author_id' => '2', - 'title' => 'Multi-record post 2', - 'body' => 'Second multi-record post', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - ))); - $this->assertEqual($result, $expected); - - $TestModel =& new Comment(); - $ts = date('Y-m-d H:i:s'); - $result = $TestModel->saveAll(array( - 'Comment' => array( - 'article_id' => 2, - 'user_id' => 2, - 'comment' => 'New comment with attachment', - 'published' => 'Y' - ), - 'Attachment' => array( - 'attachment' => 'some_file.tgz' - ))); - $this->assertTrue($result); - - $result = $TestModel->find('all'); - $expected = array( - 'id' => '7', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'New comment with attachment', - 'published' => 'Y', - 'created' => $ts, - 'updated' => $ts - ); - $this->assertEqual($result[6]['Comment'], $expected); - - $expected = array( - 'id' => '7', - 'article_id' => '2', - 'user_id' => '2', - 'comment' => 'New comment with attachment', - 'published' => 'Y', - 'created' => $ts, - 'updated' => $ts - ); - $this->assertEqual($result[6]['Comment'], $expected); - - $expected = array( - 'id' => '2', - 'comment_id' => '7', - 'attachment' => 'some_file.tgz', - 'created' => $ts, - 'updated' => $ts - ); - $this->assertEqual($result[6]['Attachment'], $expected); - } -/** - * Test SaveAll with Habtm relations - * - * @access public - * @return void - */ - function testSaveAllHabtm() { - $this->loadFixtures('Article', 'Tag', 'Comment', 'User'); - $data = array( - 'Article' => array( - 'user_id' => 1, - 'title' => 'Article Has and belongs to Many Tags' - ), - 'Tag' => array( - 'Tag' => array(1, 2) - ), - 'Comment' => array( - array( - 'comment' => 'Article comment', - 'user_id' => 1 - ))); - $Article =& new Article(); - $result = $Article->saveAll($data); - $this->assertTrue($result); - - $result = $Article->read(); - $this->assertEqual(count($result['Tag']), 2); - $this->assertEqual($result['Tag'][0]['tag'], 'tag1'); - $this->assertEqual(count($result['Comment']), 1); - $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1); - } -/** - * Test SaveAll with Habtm relations and extra join table fields - * - * @access public - * @return void - */ - function testSaveAllHabtmWithExtraJoinTableFields() { - $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); - - $data = array( - 'Something' => array( - 'id' => 4, - 'title' => 'Extra Fields', - 'body' => 'Extra Fields Body', - 'published' => '1' - ), - 'SomethingElse' => array( - array('something_else_id' => 1, 'doomed' => '1'), - array('something_else_id' => 2, 'doomed' => '0'), - array('something_else_id' => 3, 'doomed' => '1') - ) - ); - - $Something =& new Something(); - $result = $Something->saveAll($data); - $this->assertTrue($result); - $result = $Something->read(); - - $this->assertEqual(count($result['SomethingElse']), 3); - $this->assertTrue(Set::matches('/Something[id=4]', $result)); - - $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result)); - $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result)); - $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result)); - - $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result)); - $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result)); - $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result)); - - $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result)); - $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result)); - $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result)); - } -/** - * testSaveAllHasOne method - * - * @access public - * @return void - */ - function testSaveAllHasOne() { - $model = new Comment(); - $model->deleteAll(true); - $this->assertEqual($model->find('all'), array()); - - $model->Attachment->deleteAll(true); - $this->assertEqual($model->Attachment->find('all'), array()); - - $this->assertTrue($model->saveAll(array( - 'Comment' => array( - 'comment' => 'Comment with attachment', - 'article_id' => 1, - 'user_id' => 1 - ), - 'Attachment' => array( - 'attachment' => 'some_file.zip' - )))); - $result = $model->find('all', array('fields' => array( - 'Comment.id', 'Comment.comment', 'Attachment.id', - 'Attachment.comment_id', 'Attachment.attachment' - ))); - $expected = array(array( - 'Comment' => array( - 'id' => '1', - 'comment' => 'Comment with attachment' - ), - 'Attachment' => array( - 'id' => '1', - 'comment_id' => '1', - 'attachment' => 'some_file.zip' - ))); - $this->assertEqual($result, $expected); - } -/** - * testSaveAllBelongsTo method - * - * @access public - * @return void - */ - function testSaveAllBelongsTo() { - $model = new Comment(); - $model->deleteAll(true); - $this->assertEqual($model->find('all'), array()); - - $model->Article->deleteAll(true); - $this->assertEqual($model->Article->find('all'), array()); - - $this->assertTrue($model->saveAll(array( - 'Comment' => array( - 'comment' => 'Article comment', - 'article_id' => 1, - 'user_id' => 1 - ), - 'Article' => array( - 'title' => 'Model Associations 101', - 'user_id' => 1 - )))); - $result = $model->find('all', array('fields' => array( - 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title' - ))); - $expected = array(array( - 'Comment' => array( - 'id' => '1', - 'article_id' => '1', - 'comment' => 'Article comment' - ), - 'Article' => array( - 'id' => '1', - 'title' => 'Model Associations 101' - ))); - $this->assertEqual($result, $expected); - } -/** - * testSaveAllHasOneValidation method - * - * @access public - * @return void - */ - function testSaveAllHasOneValidation() { - $model = new Comment(); - $model->deleteAll(true); - $this->assertEqual($model->find('all'), array()); - - $model->Attachment->deleteAll(true); - $this->assertEqual($model->Attachment->find('all'), array()); - - $model->validate = array('comment' => 'notEmpty'); - $model->Attachment->validate = array('attachment' => 'notEmpty'); - $model->Attachment->bind('Comment'); - - $this->assertFalse($model->saveAll( - array( - 'Comment' => array( - 'comment' => '', - 'article_id' => 1, - 'user_id' => 1 - ), - 'Attachment' => array('attachment' => '') - ), - array('validate' => 'first') - )); - $expected = array( - 'Comment' => array('comment' => 'This field cannot be left blank'), - 'Attachment' => array('attachment' => 'This field cannot be left blank') - ); - $this->assertEqual($model->validationErrors, $expected['Comment']); - $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']); - - $this->assertFalse($model->saveAll( - array( - 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1), - 'Attachment' => array('attachment' => '') - ), - array('validate' => 'only') - )); - $this->assertEqual($model->validationErrors, $expected['Comment']); - $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']); - } -/** - * testSaveAllAtomic method - * - * @access public - * @return void - */ - function testSaveAllAtomic() { - $this->loadFixtures('Article', 'User'); - $TestModel =& new Article(); - - $result = $TestModel->saveAll(array( - 'Article' => array( - 'title' => 'Post with Author', - 'body' => 'This post will be saved with an author', - 'user_id' => 2 - ), - 'Comment' => array( - array('comment' => 'First new comment', 'user_id' => 2)) - ), array('atomic' => false)); - - $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true))); - - $result = $TestModel->saveAll(array( - array( - 'id' => '1', - 'title' => 'Baleeted First Post', - 'body' => 'Baleeted!', - 'published' => 'N' - ), - array( - 'id' => '2', - 'title' => 'Just update the title' - ), - array( - 'title' => 'Creating a fourth post', - 'body' => 'Fourth post body', - 'user_id' => 2 - ) - ), array('atomic' => false)); - $this->assertIdentical($result, array(true, true, true)); - - $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); - $result = $TestModel->saveAll(array( - array( - 'id' => '1', - 'title' => 'Un-Baleeted First Post', - 'body' => 'Not Baleeted!', - 'published' => 'Y' - ), - array( - 'id' => '2', - 'title' => '', - 'body' => 'Trying to get away with an empty title' - ) - ), array('atomic' => false)); - $this->assertIdentical($result, array(true, false)); - - $result = $TestModel->saveAll(array( - 'Article' => array('id' => 2), - 'Comment' => array( - array( - 'comment' => 'First new comment', - 'published' => 'Y', - 'user_id' => 1 - ), - array( - 'comment' => 'Second new comment', - 'published' => 'Y', - 'user_id' => 2 - )) - ), array('atomic' => false)); - $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true))); - } -/** - * testSaveAllHasMany method - * - * @access public - * @return void - */ - function testSaveAllHasMany() { - $this->loadFixtures('Article', 'Comment'); - $TestModel =& new Article(); - $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); - - $result = $TestModel->saveAll(array( - 'Article' => array('id' => 2), - 'Comment' => array( - array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1), - array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2) - ) - )); - $this->assertTrue($result); - - $result = $TestModel->findById(2); - $expected = array( - 'First Comment for Second Article', - 'Second Comment for Second Article', - 'First new comment', - 'Second new comment' - ); - $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); - - $result = $TestModel->saveAll( - array( - 'Article' => array('id' => 2), - 'Comment' => array( - array( - 'comment' => 'Third new comment', - 'published' => 'Y', - 'user_id' => 1 - ))), - array('atomic' => false) - ); - $this->assertTrue($result); - - $result = $TestModel->findById(2); - $expected = array( - 'First Comment for Second Article', - 'Second Comment for Second Article', - 'First new comment', - 'Second new comment', - 'Third new comment' - ); - $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); - - $TestModel->beforeSaveReturn = false; - $result = $TestModel->saveAll( - array( - 'Article' => array('id' => 2), - 'Comment' => array( - array( - 'comment' => 'Fourth new comment', - 'published' => 'Y', - 'user_id' => 1 - ))), - array('atomic' => false) - ); - $this->assertEqual($result, array('Article' => false)); - - $result = $TestModel->findById(2); - $expected = array( - 'First Comment for Second Article', - 'Second Comment for Second Article', - 'First new comment', - 'Second new comment', - 'Third new comment' - ); - $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); - } -/** - * testSaveAllHasManyValidation method - * - * @access public - * @return void - */ - function testSaveAllHasManyValidation() { - $this->loadFixtures('Article', 'Comment'); - $TestModel =& new Article(); - $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); - $TestModel->Comment->validate = array('comment' => 'notEmpty'); - - $result = $TestModel->saveAll(array( - 'Article' => array('id' => 2), - 'Comment' => array( - array('comment' => '', 'published' => 'Y', 'user_id' => 1), - ) - )); - $expected = array('Comment' => array(false)); - $this->assertEqual($result, $expected); - - $expected = array('Comment' => array( - array('comment' => 'This field cannot be left blank') - )); - $this->assertEqual($TestModel->validationErrors, $expected); - $expected = array( - array('comment' => 'This field cannot be left blank') - ); - $this->assertEqual($TestModel->Comment->validationErrors, $expected); - - $result = $TestModel->saveAll(array( - 'Article' => array('id' => 2), - 'Comment' => array( - array( - 'comment' => '', - 'published' => 'Y', - 'user_id' => 1 - )) - ), array('validate' => 'only')); - } -/** - * testSaveAllTransaction method - * - * @access public - * @return void - */ - function testSaveAllTransaction() { - $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); - $TestModel =& new Post(); - - $TestModel->validate = array('title' => 'notEmpty'); - $data = array( - array('author_id' => 1, 'title' => 'New Fourth Post'), - array('author_id' => 1, 'title' => 'New Fifth Post'), - array('author_id' => 1, 'title' => '') - ); - $ts = date('Y-m-d H:i:s'); - $this->assertFalse($TestModel->saveAll($data)); - - $result = $TestModel->find('all', array('recursive' => -1)); - $expected = array( - array('Post' => array( - 'id' => '1', - 'author_id' => 1, - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - array('Post' => array( - 'id' => '2', - 'author_id' => 3, - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - )), - array('Post' => array( - 'id' => '3', - 'author_id' => 1, - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ))); - - if (count($result) != 3) { - // Database doesn't support transactions - $expected[] = array( - 'Post' => array( - 'id' => '4', - 'author_id' => 1, - 'title' => 'New Fourth Post', - 'body' => null, - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - )); - - $expected[] = array( - 'Post' => array( - 'id' => '5', - 'author_id' => 1, - 'title' => 'New Fifth Post', - 'body' => null, - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - )); - - $this->assertEqual($result, $expected); - // Skip the rest of the transactional tests - return; - } - - $this->assertEqual($result, $expected); - - $data = array( - array('author_id' => 1, 'title' => 'New Fourth Post'), - array('author_id' => 1, 'title' => ''), - array('author_id' => 1, 'title' => 'New Sixth Post') - ); - $ts = date('Y-m-d H:i:s'); - $this->assertFalse($TestModel->saveAll($data)); - - $result = $TestModel->find('all', array('recursive' => -1)); - $expected = array( - array('Post' => array( - 'id' => '1', - 'author_id' => 1, - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => '2007-03-18 10:41:31' - )), - array('Post' => array( - 'id' => '2', - 'author_id' => 3, - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => '2007-03-18 10:43:31' - )), - array('Post' => array( - 'id' => '3', - 'author_id' => 1, - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - ))); - - if (count($result) != 3) { - // Database doesn't support transactions - $expected[] = array( - 'Post' => array( - 'id' => '4', - 'author_id' => 1, - 'title' => 'New Fourth Post', - 'body' => 'Third Post Body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - )); - - $expected[] = array( - 'Post' => array( - 'id' => '5', - 'author_id' => 1, - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - )); - } - $this->assertEqual($result, $expected); - - $TestModel->validate = array('title' => 'notEmpty'); - $data = array( - array('author_id' => 1, 'title' => 'New Fourth Post'), - array('author_id' => 1, 'title' => 'New Fifth Post'), - array('author_id' => 1, 'title' => 'New Sixth Post') - ); - $this->assertTrue($TestModel->saveAll($data)); - - $result = $TestModel->find('all', array( - 'recursive' => -1, - 'fields' => array('author_id', 'title','body','published') - )); - - $expected = array( - array('Post' => array( - 'author_id' => 1, - 'title' => 'First Post', - 'body' => 'First Post Body', - 'published' => 'Y' - )), - array('Post' => array( - 'author_id' => 3, - 'title' => 'Second Post', - 'body' => 'Second Post Body', - 'published' => 'Y' - )), - array('Post' => array( - 'author_id' => 1, - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y' - )), - array('Post' => array( - 'author_id' => 1, - 'title' => 'New Fourth Post', - 'body' => '', - 'published' => 'N' - )), - array('Post' => array( - 'author_id' => 1, - 'title' => 'New Fifth Post', - 'body' => '', - 'published' => 'N' - )), - array('Post' => array( - 'author_id' => 1, - 'title' => 'New Sixth Post', - 'body' => '', - 'published' => 'N' - ))); - $this->assertEqual($result, $expected); - } - -/** - * testSaveAllValidation method - * - * @access public - * @return void - */ - function testSaveAllValidation() { - $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); - $TestModel =& new Post(); - - $data = array( - array( - 'id' => '1', - 'title' => 'Baleeted First Post', - 'body' => 'Baleeted!', - 'published' => 'N' - ), - array( - 'id' => '2', - 'title' => 'Just update the title' - ), - array( - 'title' => 'Creating a fourth post', - 'body' => 'Fourth post body', - 'author_id' => 2 - )); - - $this->assertTrue($TestModel->saveAll($data)); - - $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); - $ts = date('Y-m-d H:i:s'); - $expected = array( - array( - 'Post' => array( - 'id' => '1', - 'author_id' => '1', - 'title' => 'Baleeted First Post', - 'body' => 'Baleeted!', - 'published' => 'N', - 'created' => '2007-03-18 10:39:23', - 'updated' => $ts - )), - array( - 'Post' => array( - 'id' => '2', - 'author_id' => '3', - 'title' => 'Just update the title', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', 'updated' => $ts - )), - array( - 'Post' => array( - 'id' => '3', - 'author_id' => '1', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - )), - array( - 'Post' => array( - 'id' => '4', - 'author_id' => '2', - 'title' => 'Creating a fourth post', - 'body' => 'Fourth post body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - ))); - $this->assertEqual($result, $expected); - - $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); - $data = array( - array( - 'id' => '1', - 'title' => 'Un-Baleeted First Post', - 'body' => 'Not Baleeted!', - 'published' => 'Y' - ), - array( - 'id' => '2', - 'title' => '', - 'body' => 'Trying to get away with an empty title' - )); - $result = $TestModel->saveAll($data); - $this->assertEqual($result, false); - - $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); - $errors = array(1 => array('title' => 'This field cannot be left blank')); - $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result); - if (!$transactionWorked) { - $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result)); - $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result)); - } - - $this->assertEqual($TestModel->validationErrors, $errors); - - $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); - $data = array( - array( - 'id' => '1', - 'title' => 'Un-Baleeted First Post', - 'body' => 'Not Baleeted!', - 'published' => 'Y' - ), - array( - 'id' => '2', - 'title' => '', - 'body' => 'Trying to get away with an empty title' - )); - $result = $TestModel->saveAll($data, array('atomic' => false)); - $this->assertEqual($result, array(true, false)); - $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); - $errors = array(1 => array('title' => 'This field cannot be left blank')); - $newTs = date('Y-m-d H:i:s'); - $expected = array( - array( - 'Post' => array( - 'id' => '1', - 'author_id' => '1', - 'title' => 'Un-Baleeted First Post', - 'body' => 'Not Baleeted!', - 'published' => 'Y', - 'created' => '2007-03-18 10:39:23', - 'updated' => $newTs - )), - array( - 'Post' => array( - 'id' => '2', - 'author_id' => '3', - 'title' => 'Just update the title', - 'body' => 'Second Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:41:23', - 'updated' => $ts - )), - array( - 'Post' => array( - 'id' => '3', - 'author_id' => '1', - 'title' => 'Third Post', - 'body' => 'Third Post Body', - 'published' => 'Y', - 'created' => '2007-03-18 10:43:23', - 'updated' => '2007-03-18 10:45:31' - )), - array( - 'Post' => array( - 'id' => '4', - 'author_id' => '2', - 'title' => 'Creating a fourth post', - 'body' => 'Fourth post body', - 'published' => 'N', - 'created' => $ts, - 'updated' => $ts - ))); - $this->assertEqual($result, $expected); - $this->assertEqual($TestModel->validationErrors, $errors); - - $data = array( - array( - 'id' => '1', - 'title' => 'Re-Baleeted First Post', - 'body' => 'Baleeted!', - 'published' => 'N' - ), - array( - 'id' => '2', - 'title' => '', - 'body' => 'Trying to get away with an empty title' - )); - $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first'))); - - $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); - $this->assertEqual($result, $expected); - $this->assertEqual($TestModel->validationErrors, $errors); - - $data = array( - array( - 'title' => 'First new post', - 'body' => 'Woohoo!', - 'published' => 'Y' - ), - array( - 'title' => 'Empty body', - 'body' => '' - )); - - $TestModel->validate['body'] = 'notEmpty'; - } -/** - * testSaveAllValidationOnly method - * - * @access public - * @return void - */ - function testSaveAllValidationOnly() { - $TestModel =& new Comment(); - $TestModel->Attachment->validate = array('attachment' => 'notEmpty'); - - $data = array( - 'Comment' => array( - 'comment' => 'This is the comment' - ), - 'Attachment' => array( - 'attachment' => '' - ) - ); - - $result = $TestModel->saveAll($data, array('validate' => 'only')); - $this->assertFalse($result); - - $TestModel =& new Article(); - $TestModel->validate = array('title' => 'notEmpty'); - $result = $TestModel->saveAll( - array( - 0 => array('title' => ''), - 1 => array('title' => 'title 1'), - 2 => array('title' => 'title 2'), - ), - array('validate'=>'only') - ); - $this->assertFalse($result); - $expected = array( - 0 => array('title' => 'This field cannot be left blank'), - ); - $this->assertEqual($TestModel->validationErrors, $expected); - - $result = $TestModel->saveAll( - array( - 0 => array('title' => 'title 0'), - 1 => array('title' => ''), - 2 => array('title' => 'title 2'), - ), - array('validate'=>'only') - ); - $this->assertFalse($result); - $expected = array( - 1 => array('title' => 'This field cannot be left blank'), - ); - $this->assertEqual($TestModel->validationErrors, $expected); - } -/** - * testSaveAllValidateFirst method - * - * @access public - * @return void - */ - function testSaveAllValidateFirst() { - $model =& new Article(); - $model->deleteAll(true); - - $model->Comment->validate = array('comment' => 'notEmpty'); - $result = $model->saveAll(array( - 'Article' => array( - 'title' => 'Post with Author', - 'body' => 'This post will be saved author' - ), - 'Comment' => array( - array('comment' => 'First new comment'), - array('comment' => '') - ) - ), array('validate' => 'first')); - - $this->assertFalse($result); - - $result = $model->find('all'); - $this->assertEqual($result, array()); - $expected = array('Comment' => array( - 1 => array('comment' => 'This field cannot be left blank') - )); - - $this->assertEqual($model->Comment->validationErrors, $expected['Comment']); - - $this->assertIdentical($model->Comment->find('count'), 0); - - $result = $model->saveAll( - array( - 'Article' => array( - 'title' => 'Post with Author', - 'body' => 'This post will be saved with an author', - 'user_id' => 2 - ), - 'Comment' => array( - array( - 'comment' => 'Only new comment', - 'user_id' => 2 - ))), - array('validate' => 'first') - ); - - $this->assertIdentical($result, true); - - $result = $model->Comment->find('all'); - $this->assertIdentical(count($result), 1); - $result = Set::extract('/Comment/article_id', $result); - $this->assertTrue($result[0] === 1 || $result[0] === '1'); - - - $model->deleteAll(true); - $data = array( - 'Article' => array( - 'title' => 'Post with Author saveAlled from comment', - 'body' => 'This post will be saved with an author', - 'user_id' => 2 - ), - 'Comment' => array( - 'comment' => 'Only new comment', 'user_id' => 2 - )); - - $result = $model->Comment->saveAll($data, array('validate' => 'first')); - $this->assertTrue($result); - - $result = $model->find('all'); - $this->assertEqual( - $result[0]['Article']['title'], - 'Post with Author saveAlled from comment' - ); - $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment'); - } -/** - * testUpdateWithCalculation method - * - * @access public - * @return void - */ - function testUpdateWithCalculation() { - $this->loadFixtures('DataTest'); - $model =& new DataTest(); - $result = $model->saveAll(array( - array('count' => 5, 'float' => 1.1), - array('count' => 3, 'float' => 1.2), - array('count' => 4, 'float' => 1.3), - array('count' => 1, 'float' => 2.0), - )); - $this->assertTrue($result); - - $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); - $this->assertEqual($result, array(5, 3, 4, 1)); - - $this->assertTrue($model->updateAll(array('count' => 'count + 2'))); - $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); - $this->assertEqual($result, array(7, 5, 6, 3)); - - $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1'))); - $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); - $this->assertEqual($result, array(6, 4, 5, 2)); - } -/** - * testSaveAllHasManyValidationOnly method - * - * @access public - * @return void - */ - function testSaveAllHasManyValidationOnly() { - $this->loadFixtures('Article', 'Comment'); - $TestModel =& new Article(); - $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); - $TestModel->Comment->validate = array('comment' => 'notEmpty'); - - $result = $TestModel->saveAll( - array( - 'Article' => array('id' => 2), - 'Comment' => array( - array( - 'id' => 1, - 'comment' => '', - 'published' => 'Y', - 'user_id' => 1), - array( - 'id' => 2, - 'comment' => - 'comment', - 'published' => 'Y', - 'user_id' => 1 - ))), - array('validate' => 'only') - ); - $this->assertFalse($result); - - $result = $TestModel->saveAll( - array( - 'Article' => array('id' => 2), - 'Comment' => array( - array( - 'id' => 1, - 'comment' => '', - 'published' => 'Y', - 'user_id' => 1 - ), - array( - 'id' => 2, - 'comment' => 'comment', - 'published' => 'Y', - 'user_id' => 1 - ), - array( - 'id' => 3, - 'comment' => '', - 'published' => 'Y', - 'user_id' => 1 - ))), - array( - 'validate' => 'only', - 'atomic' => false - )); - $expected = array( - 'Article' => true, - 'Comment' => array(false, true, false) - ); - $this->assertIdentical($result, $expected); - - $expected = array('Comment' => array( - 0 => array('comment' => 'This field cannot be left blank'), - 2 => array('comment' => 'This field cannot be left blank') - )); - $this->assertEqual($TestModel->validationErrors, $expected); - - $expected = array( - 0 => array('comment' => 'This field cannot be left blank'), - 2 => array('comment' => 'This field cannot be left blank') - ); - $this->assertEqual($TestModel->Comment->validationErrors, $expected); - } - -} - -class ModelDeleteTest extends BaseModelTest { -/** - * testDeleteHabtmReferenceWithConditions method - * - * @access public - * @return void - */ - function testDeleteHabtmReferenceWithConditions() { - $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio'); - - $Portfolio =& new Portfolio(); - $Portfolio->hasAndBelongsToMany['Item']['conditions'] = array('ItemsPortfolio.item_id >' => 1); - - $result = $Portfolio->find('first', array( - 'conditions' => array('Portfolio.id' => 1) - )); - $expected = array( - array( - 'id' => 3, - 'syfile_id' => 3, - 'published' => 0, - 'name' => 'Item 3', - 'ItemsPortfolio' => array( - 'id' => 3, - 'item_id' => 3, - 'portfolio_id' => 1 - )), - array( - 'id' => 4, - 'syfile_id' => 4, - 'published' => 0, - 'name' => 'Item 4', - 'ItemsPortfolio' => array( - 'id' => 4, - 'item_id' => 4, - 'portfolio_id' => 1 - )), - array( - 'id' => 5, - 'syfile_id' => 5, - 'published' => 0, - 'name' => 'Item 5', - 'ItemsPortfolio' => array( - 'id' => 5, - 'item_id' => 5, - 'portfolio_id' => 1 - ))); - $this->assertEqual($result['Item'], $expected); - - $result = $Portfolio->ItemsPortfolio->find('all', array( - 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) - )); - $expected = array( - array( - 'ItemsPortfolio' => array( - 'id' => 1, - 'item_id' => 1, - 'portfolio_id' => 1 - )), - array( - 'ItemsPortfolio' => array( - 'id' => 3, - 'item_id' => 3, - 'portfolio_id' => 1 - )), - array( - 'ItemsPortfolio' => array( - 'id' => 4, - 'item_id' => 4, - 'portfolio_id' => 1 - )), - array( - 'ItemsPortfolio' => array( - 'id' => 5, - 'item_id' => 5, - 'portfolio_id' => 1 - ))); - $this->assertEqual($result, $expected); - - $Portfolio->delete(1); - - $result = $Portfolio->find('first', array( - 'conditions' => array('Portfolio.id' => 1) - )); - $this->assertFalse($result); - - $result = $Portfolio->ItemsPortfolio->find('all', array( - 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) - )); - $this->assertFalse($result); - } -/** - * testDeleteArticleBLinks method - * - * @access public - * @return void - */ - function testDeleteArticleBLinks() { - $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); - $TestModel =& new ArticleB(); - - $result = $TestModel->ArticlesTag->find('all'); - $expected = array( - array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')), - array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')), - array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), - array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) - ); - $this->assertEqual($result, $expected); - - $TestModel->delete(1); - $result = $TestModel->ArticlesTag->find('all'); - - $expected = array( - array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), - array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) - ); - $this->assertEqual($result, $expected); - } -/** - * testDeleteDependentWithConditions method - * - * @access public - * @return void - */ - function testDeleteDependentWithConditions() { - $this->loadFixtures('Cd','Book','OverallFavorite'); - - $Cd =& new Cd(); - $OverallFavorite =& new OverallFavorite(); - - $Cd->del(1); - - $result = $OverallFavorite->find('all', array( - 'fields' => array('model_type', 'model_id', 'priority') - )); - $expected = array( - array( - 'OverallFavorite' => array( - 'model_type' => 'Book', - 'model_id' => 1, - 'priority' => 2 - ))); - - $this->assertTrue(is_array($result)); - $this->assertEqual($result, $expected); - } -/** - * testDel method - * - * @access public - * @return void - */ - function testDel() { - $this->loadFixtures('Article'); - $TestModel =& new Article(); - - $result = $TestModel->del(2); - $this->assertTrue($result); - - $result = $TestModel->read(null, 2); - $this->assertFalse($result); - - $TestModel->recursive = -1; - $result = $TestModel->find('all', array( - 'fields' => array('id', 'title') - )); - $expected = array( - array('Article' => array( - 'id' => 1, - 'title' => 'First Article' - )), - array('Article' => array( - 'id' => 3, - 'title' => 'Third Article' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->del(3); - $this->assertTrue($result); - - $result = $TestModel->read(null, 3); - $this->assertFalse($result); - - $TestModel->recursive = -1; - $result = $TestModel->find('all', array( - 'fields' => array('id', 'title') - )); - $expected = array( - array('Article' => array( - 'id' => 1, - 'title' => 'First Article' - ))); - - $this->assertEqual($result, $expected); - - - // make sure deleting a non-existent record doesn't break save() - // ticket #6293 - $this->loadFixtures('Uuid'); - $Uuid =& new Uuid(); - $data = array( - 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3', - '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8', - '8208C7FE-E89C-47C5-B378-DED6C271F9B8'); - foreach ($data as $id) { - $Uuid->save(array('id' => $id)); - } - $Uuid->del('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); - $Uuid->del('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); - foreach ($data as $id) { - $Uuid->save(array('id' => $id)); - } - $result = $Uuid->find('all', array( - 'conditions' => array('id' => $data), - 'fields' => array('id'), - 'order' => 'id')); - $expected = array( - array('Uuid' => array( - 'id' => '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8')), - array('Uuid' => array( - 'id' => '8208C7FE-E89C-47C5-B378-DED6C271F9B8')), - array('Uuid' => array( - 'id' => 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3'))); - $this->assertEqual($result, $expected); - } -/** - * testDeleteAll method - * - * @access public - * @return void - */ - function testDeleteAll() { - $this->loadFixtures('Article'); - $TestModel =& new Article(); - - $data = array('Article' => array( - 'user_id' => 2, - 'id' => 4, - 'title' => 'Fourth Article', - 'published' => 'N' - )); - $result = $TestModel->set($data) && $TestModel->save(); - $this->assertTrue($result); - - $data = array('Article' => array( - 'user_id' => 2, - 'id' => 5, - 'title' => 'Fifth Article', - 'published' => 'Y' - )); - $result = $TestModel->set($data) && $TestModel->save(); - $this->assertTrue($result); - - $data = array('Article' => array( - 'user_id' => 1, - 'id' => 6, - 'title' => 'Sixth Article', - 'published' => 'N' - )); - $result = $TestModel->set($data) && $TestModel->save(); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->find('all', array( - 'fields' => array('id', 'user_id', 'title', 'published') - )); - - $expected = array( - array('Article' => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'published' => 'Y' - )), - array('Article' => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'published' => 'Y' - )), - array('Article' => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'published' => 'Y')), - array('Article' => array( - 'id' => 4, - 'user_id' => 2, - 'title' => 'Fourth Article', - 'published' => 'N' - )), - array('Article' => array( - 'id' => 5, - 'user_id' => 2, - 'title' => 'Fifth Article', - 'published' => 'Y' - )), - array('Article' => array( - 'id' => 6, - 'user_id' => 1, - 'title' => 'Sixth Article', - 'published' => 'N' - ))); - - $this->assertEqual($result, $expected); - - $result = $TestModel->deleteAll(array('Article.published' => 'N')); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->find('all', array( - 'fields' => array('id', 'user_id', 'title', 'published') - )); - $expected = array( - array('Article' => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'published' => 'Y' - )), - array('Article' => array( - 'id' => 2, - 'user_id' => 3, - 'title' => 'Second Article', - 'published' => 'Y' - )), - array('Article' => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'published' => 'Y' - )), - array('Article' => array( - 'id' => 5, - 'user_id' => 2, - 'title' => 'Fifth Article', - 'published' => 'Y' - ))); - $this->assertEqual($result, $expected); - - $data = array('Article.user_id' => array(2, 3)); - $result = $TestModel->deleteAll($data, true, true); - $this->assertTrue($result); - - $TestModel->recursive = -1; - $result = $TestModel->find('all', array( - 'fields' => array('id', 'user_id', 'title', 'published') - )); - $expected = array( - array('Article' => array( - 'id' => 1, - 'user_id' => 1, - 'title' => 'First Article', - 'published' => 'Y' - )), - array('Article' => array( - 'id' => 3, - 'user_id' => 1, - 'title' => 'Third Article', - 'published' => 'Y' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->deleteAll(array('Article.user_id' => 999)); - $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); - } -/** - * testRecursiveDel method - * - * @access public - * @return void - */ - function testRecursiveDel() { - $this->loadFixtures('Article', 'Comment', 'Attachment'); - $TestModel =& new Article(); - - $result = $TestModel->del(2); - $this->assertTrue($result); - - $TestModel->recursive = 2; - $result = $TestModel->read(null, 2); - $this->assertFalse($result); - - $result = $TestModel->Comment->read(null, 5); - $this->assertFalse($result); - - $result = $TestModel->Comment->read(null, 6); - $this->assertFalse($result); - - $result = $TestModel->Comment->Attachment->read(null, 1); - $this->assertFalse($result); - - $result = $TestModel->find('count'); - $this->assertEqual($result, 2); - - $result = $TestModel->Comment->find('count'); - $this->assertEqual($result, 4); - - $result = $TestModel->Comment->Attachment->find('count'); - $this->assertEqual($result, 0); - } -/** - * testDependentExclusiveDelete method - * - * @access public - * @return void - */ - function testDependentExclusiveDelete() { - $this->loadFixtures('Article', 'Comment'); - $TestModel =& new Article10(); - - $result = $TestModel->find('all'); - $this->assertEqual(count($result[0]['Comment']), 4); - $this->assertEqual(count($result[1]['Comment']), 2); - $this->assertEqual($TestModel->Comment->find('count'), 6); - - $TestModel->delete(1); - $this->assertEqual($TestModel->Comment->find('count'), 2); - } -/** - * testDeleteLinks method - * - * @access public - * @return void - */ - function testDeleteLinks() { - $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); - $TestModel =& new Article(); - - $result = $TestModel->ArticlesTag->find('all'); - $expected = array( - array('ArticlesTag' => array( - 'article_id' => '1', - 'tag_id' => '1' - )), - array('ArticlesTag' => array( - 'article_id' => '1', - 'tag_id' => '2' - )), - array('ArticlesTag' => array( - 'article_id' => '2', - 'tag_id' => '1' - )), - array('ArticlesTag' => array( - 'article_id' => '2', - 'tag_id' => '3' - ))); - $this->assertEqual($result, $expected); - - $TestModel->delete(1); - $result = $TestModel->ArticlesTag->find('all'); - - $expected = array( - array('ArticlesTag' => array( - 'article_id' => '2', - 'tag_id' => '1' - )), - array('ArticlesTag' => array( - 'article_id' => '2', - 'tag_id' => '3' - ))); - $this->assertEqual($result, $expected); - - $result = $TestModel->deleteAll(array('Article.user_id' => 999)); - $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); - } -/** - * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method - * - * @access public - * @return void - */ - function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() { - - $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); - $ThePaper =& new ThePaper(); - $ThePaper->id = 1; - $ThePaper->save(array('Monkey' => array(2, 3))); - - $result = $ThePaper->findById(1); - $expected = array( - array( - 'id' => '2', - 'device_type_id' => '1', - 'name' => 'Device 2', - 'typ' => '1' - ), - array( - 'id' => '3', - 'device_type_id' => '1', - 'name' => 'Device 3', - 'typ' => '2' - )); - $this->assertEqual($result['Monkey'], $expected); - - $ThePaper =& new ThePaper(); - $ThePaper->id = 2; - $ThePaper->save(array('Monkey' => array(2, 3))); - - $result = $ThePaper->findById(2); - $expected = array( - array( - 'id' => '2', - 'device_type_id' => '1', - 'name' => 'Device 2', - 'typ' => '1' - ), - array( - 'id' => '3', - 'device_type_id' => '1', - 'name' => 'Device 3', - 'typ' => '2' - )); - $this->assertEqual($result['Monkey'], $expected); - - $ThePaper->delete(1); - $result = $ThePaper->findById(2); - $expected = array( - array( - 'id' => '2', - 'device_type_id' => '1', - 'name' => 'Device 2', - 'typ' => '1' - ), - array( - 'id' => '3', - 'device_type_id' => '1', - 'name' => 'Device 3', - 'typ' => '2' - )); - $this->assertEqual($result['Monkey'], $expected); - } - -} - -class ModelValidationTest extends BaseModelTest { -/** - * Tests validation parameter order in custom validation methods - * - * @access public - * @return void - */ - function testValidationParams() { - $TestModel =& new ValidationTest1(); - $TestModel->validate['title'] = array( - 'rule' => 'customValidatorWithParams', - 'required' => true - ); - $TestModel->create(array('title' => 'foo')); - $TestModel->invalidFields(); - - $expected = array( - 'data' => array( - 'title' => 'foo' - ), - 'validator' => array( - 'rule' => 'customValidatorWithParams', - 'on' => null, - 'last' => false, - 'allowEmpty' => false, - 'required' => true - ), - 'or' => true, - 'ignore_on_same' => 'id' - ); - $this->assertEqual($TestModel->validatorParams, $expected); - - $TestModel->validate['title'] = array( - 'rule' => 'customValidatorWithMessage', - 'required' => true - ); - $expected = array( - 'title' => 'This field will *never* validate! Muhahaha!' - ); - - $this->assertEqual($TestModel->invalidFields(), $expected); - } -/** - * Tests validation parameter fieldList in invalidFields - * - * @access public - * @return void - */ - function testInvalidFieldsWithFieldListParams() { - $TestModel =& new ValidationTest1(); - $TestModel->validate = $validate = array( - 'title' => array( - 'rule' => 'customValidator', - 'required' => true - ), - 'name' => array( - 'rule' => 'allowEmpty', - 'required' => true - )); - $TestModel->invalidFields(array('fieldList' => array('title'))); - $expected = array( - 'title' => 'This field cannot be left blank' - ); - $this->assertEqual($TestModel->validationErrors, $expected); - $TestModel->validationErrors = array(); - - $TestModel->invalidFields(array('fieldList' => array('name'))); - $expected = array( - 'name' => 'This field cannot be left blank' - ); - $this->assertEqual($TestModel->validationErrors, $expected); - $TestModel->validationErrors = array(); - - $TestModel->invalidFields(array('fieldList' => array('name', 'title'))); - $expected = array( - 'name' => 'This field cannot be left blank', - 'title' => 'This field cannot be left blank' - ); - $this->assertEqual($TestModel->validationErrors, $expected); - $TestModel->validationErrors = array(); - - $TestModel->whitelist = array('name'); - $TestModel->invalidFields(); - $expected = array('name' => 'This field cannot be left blank'); - $this->assertEqual($TestModel->validationErrors, $expected); - $TestModel->validationErrors = array(); - - $this->assertEqual($TestModel->validate, $validate); - } - } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model_delete.test.php b/cake/tests/cases/libs/model/model_delete.test.php new file mode 100644 index 000000000..6d30d2bdf --- /dev/null +++ b/cake/tests/cases/libs/model/model_delete.test.php @@ -0,0 +1,571 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake + * @subpackage cake.tests.cases.libs.model + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision: 8225 $ + * @modifiedby $LastChangedBy: mark_story $ + * @lastmodified $Date: 2009-07-07 23:25:30 -0400 (Tue, 07 Jul 2009) $ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +require_once dirname(__FILE__) . DS . 'model.test.php'; +require_once dirname(__FILE__) . DS . 'model_delete.test.php'; +/** + * ModelDeleteTest + * + * @package cake + * @subpackage cake.tests.cases.libs.model.operations + */ +class ModelDeleteTest extends BaseModelTest { +/** + * testDeleteHabtmReferenceWithConditions method + * + * @access public + * @return void + */ + function testDeleteHabtmReferenceWithConditions() { + $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio'); + + $Portfolio =& new Portfolio(); + $Portfolio->hasAndBelongsToMany['Item']['conditions'] = array('ItemsPortfolio.item_id >' => 1); + + $result = $Portfolio->find('first', array( + 'conditions' => array('Portfolio.id' => 1) + )); + $expected = array( + array( + 'id' => 3, + 'syfile_id' => 3, + 'published' => 0, + 'name' => 'Item 3', + 'ItemsPortfolio' => array( + 'id' => 3, + 'item_id' => 3, + 'portfolio_id' => 1 + )), + array( + 'id' => 4, + 'syfile_id' => 4, + 'published' => 0, + 'name' => 'Item 4', + 'ItemsPortfolio' => array( + 'id' => 4, + 'item_id' => 4, + 'portfolio_id' => 1 + )), + array( + 'id' => 5, + 'syfile_id' => 5, + 'published' => 0, + 'name' => 'Item 5', + 'ItemsPortfolio' => array( + 'id' => 5, + 'item_id' => 5, + 'portfolio_id' => 1 + ))); + $this->assertEqual($result['Item'], $expected); + + $result = $Portfolio->ItemsPortfolio->find('all', array( + 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) + )); + $expected = array( + array( + 'ItemsPortfolio' => array( + 'id' => 1, + 'item_id' => 1, + 'portfolio_id' => 1 + )), + array( + 'ItemsPortfolio' => array( + 'id' => 3, + 'item_id' => 3, + 'portfolio_id' => 1 + )), + array( + 'ItemsPortfolio' => array( + 'id' => 4, + 'item_id' => 4, + 'portfolio_id' => 1 + )), + array( + 'ItemsPortfolio' => array( + 'id' => 5, + 'item_id' => 5, + 'portfolio_id' => 1 + ))); + $this->assertEqual($result, $expected); + + $Portfolio->delete(1); + + $result = $Portfolio->find('first', array( + 'conditions' => array('Portfolio.id' => 1) + )); + $this->assertFalse($result); + + $result = $Portfolio->ItemsPortfolio->find('all', array( + 'conditions' => array('ItemsPortfolio.portfolio_id' => 1) + )); + $this->assertFalse($result); + } +/** + * testDeleteArticleBLinks method + * + * @access public + * @return void + */ + function testDeleteArticleBLinks() { + $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); + $TestModel =& new ArticleB(); + + $result = $TestModel->ArticlesTag->find('all'); + $expected = array( + array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '1')), + array('ArticlesTag' => array('article_id' => '1', 'tag_id' => '2')), + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) + ); + $this->assertEqual($result, $expected); + + $TestModel->delete(1); + $result = $TestModel->ArticlesTag->find('all'); + + $expected = array( + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '1')), + array('ArticlesTag' => array('article_id' => '2', 'tag_id' => '3')) + ); + $this->assertEqual($result, $expected); + } +/** + * testDeleteDependentWithConditions method + * + * @access public + * @return void + */ + function testDeleteDependentWithConditions() { + $this->loadFixtures('Cd','Book','OverallFavorite'); + + $Cd =& new Cd(); + $OverallFavorite =& new OverallFavorite(); + + $Cd->del(1); + + $result = $OverallFavorite->find('all', array( + 'fields' => array('model_type', 'model_id', 'priority') + )); + $expected = array( + array( + 'OverallFavorite' => array( + 'model_type' => 'Book', + 'model_id' => 1, + 'priority' => 2 + ))); + + $this->assertTrue(is_array($result)); + $this->assertEqual($result, $expected); + } +/** + * testDel method + * + * @access public + * @return void + */ + function testDel() { + $this->loadFixtures('Article'); + $TestModel =& new Article(); + + $result = $TestModel->del(2); + $this->assertTrue($result); + + $result = $TestModel->read(null, 2); + $this->assertFalse($result); + + $TestModel->recursive = -1; + $result = $TestModel->find('all', array( + 'fields' => array('id', 'title') + )); + $expected = array( + array('Article' => array( + 'id' => 1, + 'title' => 'First Article' + )), + array('Article' => array( + 'id' => 3, + 'title' => 'Third Article' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->del(3); + $this->assertTrue($result); + + $result = $TestModel->read(null, 3); + $this->assertFalse($result); + + $TestModel->recursive = -1; + $result = $TestModel->find('all', array( + 'fields' => array('id', 'title') + )); + $expected = array( + array('Article' => array( + 'id' => 1, + 'title' => 'First Article' + ))); + + $this->assertEqual($result, $expected); + + + // make sure deleting a non-existent record doesn't break save() + // ticket #6293 + $this->loadFixtures('Uuid'); + $Uuid =& new Uuid(); + $data = array( + 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3', + '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8', + '8208C7FE-E89C-47C5-B378-DED6C271F9B8'); + foreach ($data as $id) { + $Uuid->save(array('id' => $id)); + } + $Uuid->del('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); + $Uuid->del('52C8865C-10EE-4302-AE6C-6E7D8E12E2C8'); + foreach ($data as $id) { + $Uuid->save(array('id' => $id)); + } + $result = $Uuid->find('all', array( + 'conditions' => array('id' => $data), + 'fields' => array('id'), + 'order' => 'id')); + $expected = array( + array('Uuid' => array( + 'id' => '52C8865C-10EE-4302-AE6C-6E7D8E12E2C8')), + array('Uuid' => array( + 'id' => '8208C7FE-E89C-47C5-B378-DED6C271F9B8')), + array('Uuid' => array( + 'id' => 'B607DAB9-88A2-46CF-B57C-842CA9E3B3B3'))); + $this->assertEqual($result, $expected); + } +/** + * testDeleteAll method + * + * @access public + * @return void + */ + function testDeleteAll() { + $this->loadFixtures('Article'); + $TestModel =& new Article(); + + $data = array('Article' => array( + 'user_id' => 2, + 'id' => 4, + 'title' => 'Fourth Article', + 'published' => 'N' + )); + $result = $TestModel->set($data) && $TestModel->save(); + $this->assertTrue($result); + + $data = array('Article' => array( + 'user_id' => 2, + 'id' => 5, + 'title' => 'Fifth Article', + 'published' => 'Y' + )); + $result = $TestModel->set($data) && $TestModel->save(); + $this->assertTrue($result); + + $data = array('Article' => array( + 'user_id' => 1, + 'id' => 6, + 'title' => 'Sixth Article', + 'published' => 'N' + )); + $result = $TestModel->set($data) && $TestModel->save(); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->find('all', array( + 'fields' => array('id', 'user_id', 'title', 'published') + )); + + $expected = array( + array('Article' => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'published' => 'Y' + )), + array('Article' => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'published' => 'Y' + )), + array('Article' => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'published' => 'Y')), + array('Article' => array( + 'id' => 4, + 'user_id' => 2, + 'title' => 'Fourth Article', + 'published' => 'N' + )), + array('Article' => array( + 'id' => 5, + 'user_id' => 2, + 'title' => 'Fifth Article', + 'published' => 'Y' + )), + array('Article' => array( + 'id' => 6, + 'user_id' => 1, + 'title' => 'Sixth Article', + 'published' => 'N' + ))); + + $this->assertEqual($result, $expected); + + $result = $TestModel->deleteAll(array('Article.published' => 'N')); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->find('all', array( + 'fields' => array('id', 'user_id', 'title', 'published') + )); + $expected = array( + array('Article' => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'published' => 'Y' + )), + array('Article' => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'published' => 'Y' + )), + array('Article' => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'published' => 'Y' + )), + array('Article' => array( + 'id' => 5, + 'user_id' => 2, + 'title' => 'Fifth Article', + 'published' => 'Y' + ))); + $this->assertEqual($result, $expected); + + $data = array('Article.user_id' => array(2, 3)); + $result = $TestModel->deleteAll($data, true, true); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->find('all', array( + 'fields' => array('id', 'user_id', 'title', 'published') + )); + $expected = array( + array('Article' => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'published' => 'Y' + )), + array('Article' => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'published' => 'Y' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->deleteAll(array('Article.user_id' => 999)); + $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); + } +/** + * testRecursiveDel method + * + * @access public + * @return void + */ + function testRecursiveDel() { + $this->loadFixtures('Article', 'Comment', 'Attachment'); + $TestModel =& new Article(); + + $result = $TestModel->del(2); + $this->assertTrue($result); + + $TestModel->recursive = 2; + $result = $TestModel->read(null, 2); + $this->assertFalse($result); + + $result = $TestModel->Comment->read(null, 5); + $this->assertFalse($result); + + $result = $TestModel->Comment->read(null, 6); + $this->assertFalse($result); + + $result = $TestModel->Comment->Attachment->read(null, 1); + $this->assertFalse($result); + + $result = $TestModel->find('count'); + $this->assertEqual($result, 2); + + $result = $TestModel->Comment->find('count'); + $this->assertEqual($result, 4); + + $result = $TestModel->Comment->Attachment->find('count'); + $this->assertEqual($result, 0); + } +/** + * testDependentExclusiveDelete method + * + * @access public + * @return void + */ + function testDependentExclusiveDelete() { + $this->loadFixtures('Article', 'Comment'); + $TestModel =& new Article10(); + + $result = $TestModel->find('all'); + $this->assertEqual(count($result[0]['Comment']), 4); + $this->assertEqual(count($result[1]['Comment']), 2); + $this->assertEqual($TestModel->Comment->find('count'), 6); + + $TestModel->delete(1); + $this->assertEqual($TestModel->Comment->find('count'), 2); + } +/** + * testDeleteLinks method + * + * @access public + * @return void + */ + function testDeleteLinks() { + $this->loadFixtures('Article', 'ArticlesTag', 'Tag'); + $TestModel =& new Article(); + + $result = $TestModel->ArticlesTag->find('all'); + $expected = array( + array('ArticlesTag' => array( + 'article_id' => '1', + 'tag_id' => '1' + )), + array('ArticlesTag' => array( + 'article_id' => '1', + 'tag_id' => '2' + )), + array('ArticlesTag' => array( + 'article_id' => '2', + 'tag_id' => '1' + )), + array('ArticlesTag' => array( + 'article_id' => '2', + 'tag_id' => '3' + ))); + $this->assertEqual($result, $expected); + + $TestModel->delete(1); + $result = $TestModel->ArticlesTag->find('all'); + + $expected = array( + array('ArticlesTag' => array( + 'article_id' => '2', + 'tag_id' => '1' + )), + array('ArticlesTag' => array( + 'article_id' => '2', + 'tag_id' => '3' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->deleteAll(array('Article.user_id' => 999)); + $this->assertTrue($result, 'deleteAll returned false when all no records matched conditions. %s'); + } +/** + * testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable method + * + * @access public + * @return void + */ + function testHabtmDeleteLinksWhenNoPrimaryKeyInJoinTable() { + + $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); + $ThePaper =& new ThePaper(); + $ThePaper->id = 1; + $ThePaper->save(array('Monkey' => array(2, 3))); + + $result = $ThePaper->findById(1); + $expected = array( + array( + 'id' => '2', + 'device_type_id' => '1', + 'name' => 'Device 2', + 'typ' => '1' + ), + array( + 'id' => '3', + 'device_type_id' => '1', + 'name' => 'Device 3', + 'typ' => '2' + )); + $this->assertEqual($result['Monkey'], $expected); + + $ThePaper =& new ThePaper(); + $ThePaper->id = 2; + $ThePaper->save(array('Monkey' => array(2, 3))); + + $result = $ThePaper->findById(2); + $expected = array( + array( + 'id' => '2', + 'device_type_id' => '1', + 'name' => 'Device 2', + 'typ' => '1' + ), + array( + 'id' => '3', + 'device_type_id' => '1', + 'name' => 'Device 3', + 'typ' => '2' + )); + $this->assertEqual($result['Monkey'], $expected); + + $ThePaper->delete(1); + $result = $ThePaper->findById(2); + $expected = array( + array( + 'id' => '2', + 'device_type_id' => '1', + 'name' => 'Device 2', + 'typ' => '1' + ), + array( + 'id' => '3', + 'device_type_id' => '1', + 'name' => 'Device 3', + 'typ' => '2' + )); + $this->assertEqual($result['Monkey'], $expected); + } + +} + +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model_integration.test.php b/cake/tests/cases/libs/model/model_integration.test.php new file mode 100644 index 000000000..e213187b6 --- /dev/null +++ b/cake/tests/cases/libs/model/model_integration.test.php @@ -0,0 +1,1837 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake + * @subpackage cake.tests.cases.libs.model + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision: 8225 $ + * @modifiedby $LastChangedBy: mark_story $ + * @lastmodified $Date: 2009-07-07 23:25:30 -0400 (Tue, 07 Jul 2009) $ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +require_once dirname(__FILE__) . DS . 'model.test.php'; +require_once dirname(__FILE__) . DS . 'model_integration.test.php'; + +/** + * ModelIntegrationTest + * + * @package cake + * @subpackage cake.tests.cases.libs.model.operations + */ +class ModelIntegrationTest extends BaseModelTest { +/** + * testPkInHAbtmLinkModelArticleB + * + * @access public + * @return void + */ + function testPkInHabtmLinkModelArticleB() { + $this->loadFixtures('Article', 'Tag'); + $TestModel2 =& new ArticleB(); + $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id'); + } +/** + * Tests that $cacheSources can only be disabled in the db using model settings, not enabled + * + * @access public + * @return void + */ + function testCacheSourcesDisabling() { + $this->db->cacheSources = true; + $TestModel = new JoinA(); + $TestModel->cacheSources = false; + $TestModel->setSource('join_as'); + $this->assertFalse($this->db->cacheSources); + + $this->db->cacheSources = false; + $TestModel = new JoinA(); + $TestModel->cacheSources = true; + $TestModel->setSource('join_as'); + $this->assertFalse($this->db->cacheSources); + } +/** + * testPkInHabtmLinkModel method + * + * @access public + * @return void + */ + function testPkInHabtmLinkModel() { + //Test Nonconformant Models + $this->loadFixtures('Content', 'ContentAccount', 'Account'); + $TestModel =& new Content(); + $this->assertEqual($TestModel->ContentAccount->primaryKey, 'iContentAccountsId'); + + //test conformant models with no PK in the join table + $this->loadFixtures('Article', 'Tag'); + $TestModel2 =& new Article(); + $this->assertEqual($TestModel2->ArticlesTag->primaryKey, 'article_id'); + + //test conformant models with PK in join table + $this->loadFixtures('Item', 'Portfolio', 'ItemsPortfolio'); + $TestModel3 =& new Portfolio(); + $this->assertEqual($TestModel3->ItemsPortfolio->primaryKey, 'id'); + + //test conformant models with PK in join table - join table contains extra field + $this->loadFixtures('JoinA', 'JoinB', 'JoinAB'); + $TestModel4 =& new JoinA(); + $this->assertEqual($TestModel4->JoinAsJoinB->primaryKey, 'id'); + + } +/** + * testDynamicBehaviorAttachment method + * + * @access public + * @return void + */ + function testDynamicBehaviorAttachment() { + $this->loadFixtures('Apple'); + $TestModel =& new Apple(); + $this->assertEqual($TestModel->Behaviors->attached(), array()); + + $TestModel->Behaviors->attach('Tree', array('left' => 'left_field', 'right' => 'right_field')); + $this->assertTrue(is_object($TestModel->Behaviors->Tree)); + $this->assertEqual($TestModel->Behaviors->attached(), array('Tree')); + + $expected = array( + 'parent' => 'parent_id', + 'left' => 'left_field', + 'right' => 'right_field', + 'scope' => '1 = 1', + 'type' => 'nested', + '__parentChange' => false, + 'recursive' => -1 + ); + + $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected); + + $expected['enabled'] = false; + $TestModel->Behaviors->attach('Tree', array('enabled' => false)); + $this->assertEqual($TestModel->Behaviors->Tree->settings['Apple'], $expected); + $this->assertEqual($TestModel->Behaviors->attached(), array('Tree')); + + $TestModel->Behaviors->detach('Tree'); + $this->assertEqual($TestModel->Behaviors->attached(), array()); + $this->assertFalse(isset($TestModel->Behaviors->Tree)); + } +/** + * Tests cross database joins. Requires $test and $test2 to both be set in DATABASE_CONFIG + * NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections, + * or one connection will step on the other. + */ + function testCrossDatabaseJoins() { + $config = new DATABASE_CONFIG(); + + $skip = $this->skipIf( + !isset($config->test) || !isset($config->test2), + '%s Primary and secondary test databases not configured, skipping cross-database ' + .'join tests.' + .' To run these tests, you must define $test and $test2 in your database configuration.' + ); + + if ($skip) { + return; + } + + $this->loadFixtures('Article', 'Tag', 'ArticlesTag', 'User', 'Comment'); + $TestModel =& new Article(); + + $expected = array( + array( + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + ), + array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + array( + 'id' => '4', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + )), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ))), + array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'Comment' => array( + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + )), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))), + array( + 'Article' => array( + 'id' => '3', + 'user_id' => '1', + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Comment' => array(), + 'Tag' => array() + )); + $this->assertEqual($TestModel->find('all'), $expected); + + $db2 =& ConnectionManager::getDataSource('test2'); + + foreach (array('User', 'Comment') as $class) { + $this->_fixtures[$this->_fixtureClassMap[$class]]->create($db2); + $this->_fixtures[$this->_fixtureClassMap[$class]]->insert($db2); + $this->db->truncate(Inflector::pluralize(Inflector::underscore($class))); + } + + $this->assertEqual($TestModel->User->find('all'), array()); + $this->assertEqual($TestModel->Comment->find('all'), array()); + $this->assertEqual($TestModel->find('count'), 3); + + $TestModel->User->setDataSource('test2'); + $TestModel->Comment->setDataSource('test2'); + + foreach ($expected as $key => $value) { + unset($value['Comment'], $value['Tag']); + $expected[$key] = $value; + } + + $TestModel->recursive = 0; + $result = $TestModel->find('all'); + $this->assertEqual($result, $expected); + + foreach ($expected as $key => $value) { + unset($value['Comment'], $value['Tag']); + $expected[$key] = $value; + } + + $TestModel->recursive = 0; + $result = $TestModel->find('all'); + $this->assertEqual($result, $expected); + + $result = Set::extract($TestModel->User->find('all'), '{n}.User.id'); + $this->assertEqual($result, array('1', '2', '3', '4')); + $this->assertEqual($TestModel->find('all'), $expected); + + $TestModel->Comment->unbindModel(array('hasOne' => array('Attachment'))); + $expected = array( + array( + 'Comment' => array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + ), + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + array( + 'Comment' => array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + ), + 'User' => array( + 'id' => '4', + 'user' => 'garrett', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:22:23', + 'updated' => '2007-03-17 01:24:31' + ), + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + array( + 'Comment' => array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + array( + 'Comment' => array( + 'id' => '4', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + array( + 'Comment' => array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + )), + array( + 'Comment' => array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ), + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + ), + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ))); + $this->assertEqual($TestModel->Comment->find('all'), $expected); + + foreach (array('User', 'Comment') as $class) { + $this->_fixtures[$this->_fixtureClassMap[$class]]->drop($db2); + } + } +/** + * testDisplayField method + * + * @access public + * @return void + */ + function testDisplayField() { + $this->loadFixtures('Post', 'Comment', 'Person'); + $Post = new Post(); + $Comment = new Comment(); + $Person = new Person(); + + $this->assertEqual($Post->displayField, 'title'); + $this->assertEqual($Person->displayField, 'name'); + $this->assertEqual($Comment->displayField, 'id'); + } +/** + * testSchema method + * + * @access public + * @return void + */ + function testSchema() { + $Post = new Post(); + + $result = $Post->schema(); + $columns = array('id', 'author_id', 'title', 'body', 'published', 'created', 'updated'); + $this->assertEqual(array_keys($result), $columns); + + $types = array('integer', 'integer', 'string', 'text', 'string', 'datetime', 'datetime'); + $this->assertEqual(Set::extract(array_values($result), '{n}.type'), $types); + + $result = $Post->schema('body'); + $this->assertEqual($result['type'], 'text'); + $this->assertNull($Post->schema('foo')); + + $this->assertEqual($Post->getColumnTypes(), array_combine($columns, $types)); + } +/** + * test deconstruct() with time fields. + * + * @return void + **/ + function testDeconstructFieldsTime() { + $this->loadFixtures('Apple'); + $TestModel =& new Apple(); + + $data = array(); + $data['Apple']['mytime']['hour'] = ''; + $data['Apple']['mytime']['min'] = ''; + $data['Apple']['mytime']['sec'] = ''; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('mytime'=> '')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['mytime']['hour'] = ''; + $data['Apple']['mytime']['min'] = ''; + $data['Apple']['mytime']['meridan'] = ''; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('mytime'=> '')); + $this->assertEqual($TestModel->data, $expected, 'Empty values are not returning properly. %s'); + + $data = array(); + $data['Apple']['mytime']['hour'] = '12'; + $data['Apple']['mytime']['min'] = '0'; + $data['Apple']['mytime']['meridian'] = 'am'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('mytime'=> '00:00:00')); + $this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s'); + + $data = array(); + $data['Apple']['mytime']['hour'] = '00'; + $data['Apple']['mytime']['min'] = '00'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('mytime'=> '00:00:00')); + $this->assertEqual($TestModel->data, $expected, 'Midnight is not returning proper values. %s'); + + $data = array(); + $data['Apple']['mytime']['hour'] = '03'; + $data['Apple']['mytime']['min'] = '04'; + $data['Apple']['mytime']['sec'] = '04'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('mytime'=> '03:04:04')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['mytime']['hour'] = '3'; + $data['Apple']['mytime']['min'] = '4'; + $data['Apple']['mytime']['sec'] = '4'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple' => array('mytime'=> '03:04:04')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['mytime']['hour'] = '03'; + $data['Apple']['mytime']['min'] = '4'; + $data['Apple']['mytime']['sec'] = '4'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('mytime'=> '03:04:04')); + $this->assertEqual($TestModel->data, $expected); + + $db = ConnectionManager::getDataSource('test_suite'); + $data = array(); + $data['Apple']['mytime'] = $db->expression('NOW()'); + $TestModel->data = null; + $TestModel->set($data); + $this->assertEqual($TestModel->data, $data); + } +/** + * testDeconstructFields with datetime, timestamp, and date fields + * + * @access public + * @return void + */ + function testDeconstructFieldsDateTime() { + $this->loadFixtures('Apple'); + $TestModel =& new Apple(); + + //test null/empty values first + $data['Apple']['created']['year'] = ''; + $data['Apple']['created']['month'] = ''; + $data['Apple']['created']['day'] = ''; + $data['Apple']['created']['hour'] = ''; + $data['Apple']['created']['min'] = ''; + $data['Apple']['created']['sec'] = ''; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['date']['year'] = ''; + $data['Apple']['date']['month'] = ''; + $data['Apple']['date']['day'] = ''; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('date'=> '')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['year'] = '2007'; + $data['Apple']['created']['month'] = '08'; + $data['Apple']['created']['day'] = '20'; + $data['Apple']['created']['hour'] = ''; + $data['Apple']['created']['min'] = ''; + $data['Apple']['created']['sec'] = ''; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '2007-08-20 00:00:00')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['year'] = '2007'; + $data['Apple']['created']['month'] = '08'; + $data['Apple']['created']['day'] = '20'; + $data['Apple']['created']['hour'] = '10'; + $data['Apple']['created']['min'] = '12'; + $data['Apple']['created']['sec'] = ''; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '2007-08-20 10:12:00')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['year'] = '2007'; + $data['Apple']['created']['month'] = ''; + $data['Apple']['created']['day'] = '12'; + $data['Apple']['created']['hour'] = '20'; + $data['Apple']['created']['min'] = ''; + $data['Apple']['created']['sec'] = ''; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['hour'] = '20'; + $data['Apple']['created']['min'] = '33'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['hour'] = '20'; + $data['Apple']['created']['min'] = '33'; + $data['Apple']['created']['sec'] = '33'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['hour'] = '13'; + $data['Apple']['created']['min'] = '00'; + $data['Apple']['date']['year'] = '2006'; + $data['Apple']['date']['month'] = '12'; + $data['Apple']['date']['day'] = '25'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array( + 'Apple'=> array( + 'created'=> '', + 'date'=> '2006-12-25' + )); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['year'] = '2007'; + $data['Apple']['created']['month'] = '08'; + $data['Apple']['created']['day'] = '20'; + $data['Apple']['created']['hour'] = '10'; + $data['Apple']['created']['min'] = '12'; + $data['Apple']['created']['sec'] = '09'; + $data['Apple']['date']['year'] = '2006'; + $data['Apple']['date']['month'] = '12'; + $data['Apple']['date']['day'] = '25'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array( + 'Apple'=> array( + 'created'=> '2007-08-20 10:12:09', + 'date'=> '2006-12-25' + )); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['year'] = '--'; + $data['Apple']['created']['month'] = '--'; + $data['Apple']['created']['day'] = '--'; + $data['Apple']['created']['hour'] = '--'; + $data['Apple']['created']['min'] = '--'; + $data['Apple']['created']['sec'] = '--'; + $data['Apple']['date']['year'] = '--'; + $data['Apple']['date']['month'] = '--'; + $data['Apple']['date']['day'] = '--'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '', 'date'=> '')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['created']['year'] = '2007'; + $data['Apple']['created']['month'] = '--'; + $data['Apple']['created']['day'] = '20'; + $data['Apple']['created']['hour'] = '10'; + $data['Apple']['created']['min'] = '12'; + $data['Apple']['created']['sec'] = '09'; + $data['Apple']['date']['year'] = '2006'; + $data['Apple']['date']['month'] = '12'; + $data['Apple']['date']['day'] = '25'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('created'=> '', 'date'=> '2006-12-25')); + $this->assertEqual($TestModel->data, $expected); + + $data = array(); + $data['Apple']['date']['year'] = '2006'; + $data['Apple']['date']['month'] = '12'; + $data['Apple']['date']['day'] = '25'; + + $TestModel->data = null; + $TestModel->set($data); + $expected = array('Apple'=> array('date'=> '2006-12-25')); + $this->assertEqual($TestModel->data, $expected); + + $db = ConnectionManager::getDataSource('test_suite'); + $data = array(); + $data['Apple']['modified'] = $db->expression('NOW()'); + $TestModel->data = null; + $TestModel->set($data); + $this->assertEqual($TestModel->data, $data); + } +/** + * testTablePrefixSwitching method + * + * @access public + * @return void + */ + function testTablePrefixSwitching() { + ConnectionManager::create('database1', + array_merge($this->db->config, array('prefix' => 'aaa_') + )); + ConnectionManager::create('database2', + array_merge($this->db->config, array('prefix' => 'bbb_') + )); + + $db1 = ConnectionManager::getDataSource('database1'); + $db2 = ConnectionManager::getDataSource('database2'); + + $TestModel = new Apple(); + $TestModel->setDataSource('database1'); + $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples'); + $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples'); + $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples'); + + $TestModel->setDataSource('database2'); + $this->assertEqual($this->db->fullTableName($TestModel, false), 'bbb_apples'); + $this->assertEqual($db1->fullTableName($TestModel, false), 'bbb_apples'); + $this->assertEqual($db2->fullTableName($TestModel, false), 'bbb_apples'); + + $TestModel = new Apple(); + $TestModel->tablePrefix = 'custom_'; + $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples'); + $TestModel->setDataSource('database1'); + $this->assertEqual($this->db->fullTableName($TestModel, false), 'custom_apples'); + $this->assertEqual($db1->fullTableName($TestModel, false), 'custom_apples'); + + $TestModel = new Apple(); + $TestModel->setDataSource('database1'); + $this->assertEqual($this->db->fullTableName($TestModel, false), 'aaa_apples'); + $TestModel->tablePrefix = ''; + $TestModel->setDataSource('database2'); + $this->assertEqual($db2->fullTableName($TestModel, false), 'apples'); + $this->assertEqual($db1->fullTableName($TestModel, false), 'apples'); + + $TestModel->tablePrefix = null; + $TestModel->setDataSource('database1'); + $this->assertEqual($db2->fullTableName($TestModel, false), 'aaa_apples'); + $this->assertEqual($db1->fullTableName($TestModel, false), 'aaa_apples'); + + $TestModel->tablePrefix = false; + $TestModel->setDataSource('database2'); + $this->assertEqual($db2->fullTableName($TestModel, false), 'apples'); + $this->assertEqual($db1->fullTableName($TestModel, false), 'apples'); + } +/** + * Tests validation parameter order in custom validation methods + * + * @access public + * @return void + */ + function testInvalidAssociation() { + $TestModel =& new ValidationTest1(); + $this->assertNull($TestModel->getAssociated('Foo')); + } +/** + * testLoadModelSecondIteration method + * + * @access public + * @return void + */ + function testLoadModelSecondIteration() { + $model = new ModelA(); + $this->assertIsA($model,'ModelA'); + + $this->assertIsA($model->ModelB, 'ModelB'); + $this->assertIsA($model->ModelB->ModelD, 'ModelD'); + + $this->assertIsA($model->ModelC, 'ModelC'); + $this->assertIsA($model->ModelC->ModelD, 'ModelD'); + } +/** + * ensure that __exists is reset on create + * + * @return void + **/ + function testResetOfExistsOnCreate() { + $this->loadFixtures('Article'); + $Article =& new Article(); + $Article->id = 1; + $Article->saveField('title', 'Reset me'); + $Article->delete(); + $Article->id = 1; + $this->assertFalse($Article->exists()); + + $Article->create(); + $this->assertFalse($Article->exists()); + $Article->id = 2; + $Article->saveField('title', 'Staying alive'); + $result = $Article->read(null, 2); + $this->assertEqual($result['Article']['title'], 'Staying alive'); + } +/** + * testPluginAssociations method + * + * @access public + * @return void + */ + function testPluginAssociations() { + $this->loadFixtures('TestPluginArticle', 'User', 'TestPluginComment'); + $TestModel =& new TestPluginArticle(); + + $result = $TestModel->find('all'); + $expected = array( + array( + 'TestPluginArticle' => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Plugin Article', + 'body' => 'First Plugin Article Body', + 'published' => 'Y', + 'created' => '2008-09-24 10:39:23', + 'updated' => '2008-09-24 10:41:31' + ), + 'User' => array( + 'id' => 1, + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'TestPluginComment' => array( + array( + 'id' => 1, + 'article_id' => 1, + 'user_id' => 2, + 'comment' => 'First Comment for First Plugin Article', + 'published' => 'Y', + 'created' => '2008-09-24 10:45:23', + 'updated' => '2008-09-24 10:47:31' + ), + array( + 'id' => 2, + 'article_id' => 1, + 'user_id' => 4, + 'comment' => 'Second Comment for First Plugin Article', + 'published' => 'Y', + 'created' => '2008-09-24 10:47:23', + 'updated' => '2008-09-24 10:49:31' + ), + array( + 'id' => 3, + 'article_id' => 1, + 'user_id' => 1, + 'comment' => 'Third Comment for First Plugin Article', + 'published' => 'Y', + 'created' => '2008-09-24 10:49:23', + 'updated' => '2008-09-24 10:51:31' + ), + array( + 'id' => 4, + 'article_id' => 1, + 'user_id' => 1, + 'comment' => 'Fourth Comment for First Plugin Article', + 'published' => 'N', + 'created' => '2008-09-24 10:51:23', + 'updated' => '2008-09-24 10:53:31' + ))), + array( + 'TestPluginArticle' => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Plugin Article', + 'body' => 'Second Plugin Article Body', + 'published' => 'Y', + 'created' => '2008-09-24 10:41:23', + 'updated' => '2008-09-24 10:43:31' + ), + 'User' => array( + 'id' => 3, + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'TestPluginComment' => array( + array( + 'id' => 5, + 'article_id' => 2, + 'user_id' => 1, + 'comment' => 'First Comment for Second Plugin Article', + 'published' => 'Y', + 'created' => '2008-09-24 10:53:23', + 'updated' => '2008-09-24 10:55:31' + ), + array( + 'id' => 6, + 'article_id' => 2, + 'user_id' => 2, + 'comment' => 'Second Comment for Second Plugin Article', + 'published' => 'Y', + 'created' => '2008-09-24 10:55:23', + 'updated' => '2008-09-24 10:57:31' + ))), + array( + 'TestPluginArticle' => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Plugin Article', + 'body' => 'Third Plugin Article Body', + 'published' => 'Y', + 'created' => '2008-09-24 10:43:23', + 'updated' => '2008-09-24 10:45:31' + ), + 'User' => array( + 'id' => 1, + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'TestPluginComment' => array() + )); + + $this->assertEqual($result, $expected); + } +/** + * Tests getAssociated method + * + * @access public + * @return void + */ + function testGetAssociated() { + $this->loadFixtures('Article'); + $Article = ClassRegistry::init('Article'); + + $assocTypes = array('hasMany', 'hasOne', 'belongsTo', 'hasAndBelongsToMany'); + foreach ($assocTypes as $type) { + $this->assertEqual($Article->getAssociated($type), array_keys($Article->{$type})); + } + + $Article->bindModel(array('hasMany' => array('Category'))); + $this->assertEqual($Article->getAssociated('hasMany'), array('Comment', 'Category')); + + $results = $Article->getAssociated(); + $this->assertEqual(sort(array_keys($results)), array('Category', 'Comment', 'Tag')); + + $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag'))); + $this->assertEqual($Article->getAssociated('hasAndBelongsToMany'), array()); + + $result = $Article->getAssociated('Category'); + $expected = array( + 'className' => 'Category', + 'foreignKey' => 'article_id', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'dependent' => '', + 'exclusive' => '', + 'finderQuery' => '', + 'counterQuery' => '', + 'association' => 'hasMany', + ); + $this->assertEqual($result, $expected); + } + +/** + * testAutoConstructAssociations method + * + * @access public + * @return void + */ + function testAutoConstructAssociations() { + $this->loadFixtures('User', 'ArticleFeatured'); + $TestModel =& new AssociationTest1(); + + $result = $TestModel->hasAndBelongsToMany; + $expected = array('AssociationTest2' => array( + 'unique' => false, + 'joinTable' => 'join_as_join_bs', + 'foreignKey' => false, + 'className' => 'AssociationTest2', + 'with' => 'JoinAsJoinB', + 'associationForeignKey' => 'join_b_id', + 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', + 'finderQuery' => '', 'deleteQuery' => '', 'insertQuery' => '' + )); + $this->assertEqual($result, $expected); + + // Tests related to ticket https://trac.cakephp.org/ticket/5594 + $TestModel =& new ArticleFeatured(); + $TestFakeModel =& new ArticleFeatured(array('table' => false)); + + $expected = array( + 'User' => array( + 'className' => 'User', 'foreignKey' => 'user_id', + 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => '' + ), + 'Category' => array( + 'className' => 'Category', 'foreignKey' => 'category_id', + 'conditions' => '', 'fields' => '', 'order' => '', 'counterCache' => '' + ) + ); + $this->assertIdentical($TestModel->belongsTo, $expected); + $this->assertIdentical($TestFakeModel->belongsTo, $expected); + + $this->assertEqual($TestModel->User->name, 'User'); + $this->assertEqual($TestFakeModel->User->name, 'User'); + $this->assertEqual($TestModel->Category->name, 'Category'); + $this->assertEqual($TestFakeModel->Category->name, 'Category'); + + $expected = array( + 'Featured' => array( + 'className' => 'Featured', + 'foreignKey' => 'article_featured_id', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'dependent' => '' + )); + + $this->assertIdentical($TestModel->hasOne, $expected); + $this->assertIdentical($TestFakeModel->hasOne, $expected); + + $this->assertEqual($TestModel->Featured->name, 'Featured'); + $this->assertEqual($TestFakeModel->Featured->name, 'Featured'); + + $expected = array( + 'Comment' => array( + 'className' => 'Comment', + 'dependent' => true, + 'foreignKey' => 'article_featured_id', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'exclusive' => '', + 'finderQuery' => '', + 'counterQuery' => '' + )); + + $this->assertIdentical($TestModel->hasMany, $expected); + $this->assertIdentical($TestFakeModel->hasMany, $expected); + + $this->assertEqual($TestModel->Comment->name, 'Comment'); + $this->assertEqual($TestFakeModel->Comment->name, 'Comment'); + + $expected = array( + 'Tag' => array( + 'className' => 'Tag', + 'joinTable' => 'article_featureds_tags', + 'with' => 'ArticleFeaturedsTag', + 'foreignKey' => 'article_featured_id', + 'associationForeignKey' => 'tag_id', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'unique' => true, + 'finderQuery' => '', + 'deleteQuery' => '', + 'insertQuery' => '' + )); + + $this->assertIdentical($TestModel->hasAndBelongsToMany, $expected); + $this->assertIdentical($TestFakeModel->hasAndBelongsToMany, $expected); + + $this->assertEqual($TestModel->Tag->name, 'Tag'); + $this->assertEqual($TestFakeModel->Tag->name, 'Tag'); + } +/** + * test Model::__construct + * + * ensure that $actsAS and $_findMethods are merged. + * + * @return void + **/ + function testConstruct() { + $this->loadFixtures('Post', 'Comment'); + + $TestModel =& ClassRegistry::init('MergeVarPluginPost'); + $this->assertEqual($TestModel->actsAs, array('Containable', 'Tree')); + $this->assertTrue(isset($TestModel->Behaviors->Containable)); + $this->assertTrue(isset($TestModel->Behaviors->Tree)); + + $TestModel =& ClassRegistry::init('MergeVarPluginComment'); + $expected = array('Containable', 'Containable' => array('some_settings')); + $this->assertEqual($TestModel->actsAs, $expected); + $this->assertTrue(isset($TestModel->Behaviors->Containable)); + } +/** + * test Model::__construct + * + * ensure that $actsAS and $_findMethods are merged. + * + * @return void + **/ + function testConstructWithAlternateDataSource() { + $TestModel =& ClassRegistry::init(array( + 'class' => 'DoesntMatter', 'ds' => 'test_suite', 'table' => false + )); + $this->assertEqual('test_suite', $TestModel->useDbConfig); + + //deprecated but test it anyway + $NewVoid =& new TheVoid(null, false, 'other'); + $this->assertEqual('other', $NewVoid->useDbConfig); + } +/** + * testColumnTypeFetching method + * + * @access public + * @return void + */ + function testColumnTypeFetching() { + $model =& new Test(); + $this->assertEqual($model->getColumnType('id'), 'integer'); + $this->assertEqual($model->getColumnType('notes'), 'text'); + $this->assertEqual($model->getColumnType('updated'), 'datetime'); + $this->assertEqual($model->getColumnType('unknown'), null); + + $model =& new Article(); + $this->assertEqual($model->getColumnType('User.created'), 'datetime'); + $this->assertEqual($model->getColumnType('Tag.id'), 'integer'); + $this->assertEqual($model->getColumnType('Article.id'), 'integer'); + } +/** + * testHabtmUniqueKey method + * + * @access public + * @return void + */ + function testHabtmUniqueKey() { + $model =& new Item(); + $this->assertFalse($model->hasAndBelongsToMany['Portfolio']['unique']); + } +/** + * testIdentity method + * + * @access public + * @return void + */ + function testIdentity() { + $TestModel =& new Test(); + $result = $TestModel->alias; + $expected = 'Test'; + $this->assertEqual($result, $expected); + + $TestModel =& new TestAlias(); + $result = $TestModel->alias; + $expected = 'TestAlias'; + $this->assertEqual($result, $expected); + + $TestModel =& new Test(array('alias' => 'AnotherTest')); + $result = $TestModel->alias; + $expected = 'AnotherTest'; + $this->assertEqual($result, $expected); + } +/** + * testWithAssociation method + * + * @access public + * @return void + */ + function testWithAssociation() { + $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); + $TestModel =& new Something(); + $result = $TestModel->SomethingElse->find('all'); + + $expected = array( + array( + 'SomethingElse' => array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'Something' => array( + array( + 'id' => '3', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31', + 'JoinThing' => array( + 'id' => '3', + 'something_id' => '3', + 'something_else_id' => '1', + 'doomed' => '1', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + )))), + array( + 'SomethingElse' => array( + 'id' => '2', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'Something' => array( + array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'JoinThing' => array( + 'id' => '1', + 'something_id' => '1', + 'something_else_id' => '2', + 'doomed' => '1', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )))), + array( + 'SomethingElse' => array( + 'id' => '3', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'Something' => array( + array( + 'id' => '2', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31', + 'JoinThing' => array( + 'id' => '2', + 'something_id' => '2', + 'something_else_id' => '3', + 'doomed' => '0', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ))))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all'); + $expected = array( + array( + 'Something' => array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'SomethingElse' => array( + array( + 'id' => '2', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31', + 'JoinThing' => array( + 'doomed' => '1', + 'something_id' => '1', + 'something_else_id' => '2' + )))), + array( + 'Something' => array( + 'id' => '2', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'SomethingElse' => array( + array( + 'id' => '3', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31', + 'JoinThing' => array( + 'doomed' => '0', + 'something_id' => '2', + 'something_else_id' => '3' + )))), + array( + 'Something' => array( + 'id' => '3', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'SomethingElse' => array( + array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'JoinThing' => array( + 'doomed' => '1', + 'something_id' => '3', + 'something_else_id' => '1' + ))))); + $this->assertEqual($result, $expected); + + $result = $TestModel->findById(1); + $expected = array( + 'Something' => array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'SomethingElse' => array( + array( + 'id' => '2', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31', + 'JoinThing' => array( + 'doomed' => '1', + 'something_id' => '1', + 'something_else_id' => '2' + )))); + $this->assertEqual($result, $expected); + + $expected = $TestModel->findById(1); + $TestModel->set($expected); + $TestModel->save(); + $result = $TestModel->findById(1); + $this->assertEqual($result, $expected); + + $TestModel->hasAndBelongsToMany['SomethingElse']['unique'] = false; + $TestModel->create(array( + 'Something' => array('id' => 1), + 'SomethingElse' => array(3, array( + 'something_else_id' => 1, + 'doomed' => '1' + )))); + + $ts = date('Y-m-d H:i:s'); + $TestModel->save(); + + $TestModel->hasAndBelongsToMany['SomethingElse']['order'] = 'SomethingElse.id ASC'; + $result = $TestModel->findById(1); + $expected = array( + 'Something' => array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => $ts), + 'SomethingElse' => array( + array( + 'id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'JoinThing' => array( + 'doomed' => '1', + 'something_id' => '1', + 'something_else_id' => '1' + )), + array( + 'id' => '2', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31', + 'JoinThing' => array( + 'doomed' => '1', + 'something_id' => '1', + 'something_else_id' => '2' + )), + array( + 'id' => '3', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31', + 'JoinThing' => array( + 'doomed' => '0', + 'something_id' => '1', + 'something_else_id' => '3' + )))); + + $this->assertEqual($result, $expected); + } +/** + * testFindSelfAssociations method + * + * @access public + * @return void + */ + function testFindSelfAssociations() { + $this->loadFixtures('Person'); + + $TestModel =& new Person(); + $TestModel->recursive = 2; + $result = $TestModel->read(null, 1); + $expected = array( + 'Person' => array( + 'id' => 1, + 'name' => 'person', + 'mother_id' => 2, + 'father_id' => 3 + ), + 'Mother' => array( + 'id' => 2, + 'name' => 'mother', + 'mother_id' => 4, + 'father_id' => 5, + 'Mother' => array( + 'id' => 4, + 'name' => 'mother - grand mother', + 'mother_id' => 0, + 'father_id' => 0 + ), + 'Father' => array( + 'id' => 5, + 'name' => 'mother - grand father', + 'mother_id' => 0, + 'father_id' => 0 + )), + 'Father' => array( + 'id' => 3, + 'name' => 'father', + 'mother_id' => 6, + 'father_id' => 7, + 'Father' => array( + 'id' => 7, + 'name' => 'father - grand father', + 'mother_id' => 0, + 'father_id' => 0 + ), + 'Mother' => array( + 'id' => 6, + 'name' => 'father - grand mother', + 'mother_id' => 0, + 'father_id' => 0 + ))); + + $this->assertEqual($result, $expected); + + $TestModel->recursive = 3; + $result = $TestModel->read(null, 1); + $expected = array( + 'Person' => array( + 'id' => 1, + 'name' => 'person', + 'mother_id' => 2, + 'father_id' => 3 + ), + 'Mother' => array( + 'id' => 2, + 'name' => 'mother', + 'mother_id' => 4, + 'father_id' => 5, + 'Mother' => array( + 'id' => 4, + 'name' => 'mother - grand mother', + 'mother_id' => 0, + 'father_id' => 0, + 'Mother' => array(), + 'Father' => array()), + 'Father' => array( + 'id' => 5, + 'name' => 'mother - grand father', + 'mother_id' => 0, + 'father_id' => 0, + 'Father' => array(), + 'Mother' => array() + )), + 'Father' => array( + 'id' => 3, + 'name' => 'father', + 'mother_id' => 6, + 'father_id' => 7, + 'Father' => array( + 'id' => 7, + 'name' => 'father - grand father', + 'mother_id' => 0, + 'father_id' => 0, + 'Father' => array(), + 'Mother' => array() + ), + 'Mother' => array( + 'id' => 6, + 'name' => 'father - grand mother', + 'mother_id' => 0, + 'father_id' => 0, + 'Mother' => array(), + 'Father' => array() + ))); + + $this->assertEqual($result, $expected); + } +/** + * testDynamicAssociations method + * + * @access public + * @return void + */ + function testDynamicAssociations() { + $this->loadFixtures('Article', 'Comment'); + $TestModel =& new Article(); + + $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = $TestModel->hasOne = array(); + $TestModel->hasMany['Comment'] = array_merge($TestModel->hasMany['Comment'], array( + 'foreignKey' => false, + 'conditions' => array('Comment.user_id =' => '2') + )); + $result = $TestModel->find('all'); + $expected = array( + array( + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ))), + array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ))), + array( + 'Article' => array( + 'id' => '3', + 'user_id' => '1', + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + )))); + + $this->assertEqual($result, $expected); + } +/** + * testCreation method + * + * @access public + * @return void + */ + function testCreation() { + $this->loadFixtures('Article'); + $TestModel =& new Test(); + $result = $TestModel->create(); + $expected = array('Test' => array('notes' => 'write some notes here')); + $this->assertEqual($result, $expected); + $TestModel =& new User(); + $result = $TestModel->schema(); + + if (isset($this->db->columns['primary_key']['length'])) { + $intLength = $this->db->columns['primary_key']['length']; + } elseif (isset($this->db->columns['integer']['length'])) { + $intLength = $this->db->columns['integer']['length']; + } else { + $intLength = 11; + } + foreach (array('collate', 'charset') as $type) { + unset($result['user'][$type]); + unset($result['password'][$type]); + } + + $expected = array( + 'id' => array( + 'type' => 'integer', + 'null' => false, + 'default' => null, + 'length' => $intLength, + 'key' => 'primary' + ), + 'user' => array( + 'type' => 'string', + 'null' => false, + 'default' => '', + 'length' => 255 + ), + 'password' => array( + 'type' => 'string', + 'null' => false, + 'default' => '', + 'length' => 255 + ), + 'created' => array( + 'type' => 'datetime', + 'null' => true, + 'default' => null, + 'length' => null + ), + 'updated'=> array( + 'type' => 'datetime', + 'null' => true, + 'default' => null, + 'length' => null + )); + + $this->assertEqual($result, $expected); + + $TestModel =& new Article(); + $result = $TestModel->create(); + $expected = array('Article' => array('published' => 'N')); + $this->assertEqual($result, $expected); + + $FeaturedModel =& new Featured(); + $data = array( + 'article_featured_id' => 1, + 'category_id' => 1, + 'published_date' => array( + 'year' => 2008, + 'month' => 06, + 'day' => 11 + ), + 'end_date' => array( + 'year' => 2008, + 'month' => 06, + 'day' => 20 + )); + + $expected = array( + 'Featured' => array( + 'article_featured_id' => 1, + 'category_id' => 1, + 'published_date' => '2008-6-11 00:00:00', + 'end_date' => '2008-6-20 00:00:00' + )); + + $this->assertEqual($FeaturedModel->create($data), $expected); + + $data = array( + 'published_date' => array( + 'year' => 2008, + 'month' => 06, + 'day' => 11 + ), + 'end_date' => array( + 'year' => 2008, + 'month' => 06, + 'day' => 20 + ), + 'article_featured_id' => 1, + 'category_id' => 1 + ); + + $expected = array( + 'Featured' => array( + 'published_date' => '2008-6-11 00:00:00', + 'end_date' => '2008-6-20 00:00:00', + 'article_featured_id' => 1, + 'category_id' => 1 + )); + + $this->assertEqual($FeaturedModel->create($data), $expected); + } + +} +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php new file mode 100644 index 000000000..a2fd76f82 --- /dev/null +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -0,0 +1,7151 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake + * @subpackage cake.tests.cases.libs.model + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision: 8225 $ + * @modifiedby $LastChangedBy: mark_story $ + * @lastmodified $Date: 2009-07-07 23:25:30 -0400 (Tue, 07 Jul 2009) $ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +require_once dirname(__FILE__) . DS . 'model.test.php'; +require_once dirname(__FILE__) . DS . 'model_read.test.php'; +/** + * ModelReadTest + * + * @package cake + * @subpackage cake.tests.cases.libs.model.operations + */ +class ModelReadTest extends BaseModelTest { +/** + * testFetchingNonUniqueFKJoinTableRecords() + * + * Tests if the results are properly returned in the case there are non-unique FK's + * in the join table but another fields value is different. For example: + * something_id | something_else_id | doomed = 1 + * something_id | something_else_id | doomed = 0 + * Should return both records and not just one. + * + * @access public + * @return void + */ + function testFetchingNonUniqueFKJoinTableRecords() { + $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); + $Something = new Something(); + + $joinThingData = array( + 'JoinThing' => array( + 'something_id' => 1, + 'something_else_id' => 2, + 'doomed' => '0', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ) + ); + $Something->JoinThing->create($joinThingData); + $Something->JoinThing->save(); + + $result = $Something->JoinThing->find('all', array('conditions' => array('something_else_id' => 2))); + $this->assertEqual($result[0]['JoinThing']['doomed'], 1); + $this->assertEqual($result[1]['JoinThing']['doomed'], 0); + + $result = $Something->find('first'); + $this->assertEqual(count($result['SomethingElse']), 2); + $this->assertEqual($result['SomethingElse'][0]['JoinThing']['doomed'], 1); + $this->assertEqual($result['SomethingElse'][1]['JoinThing']['doomed'], 0); + } +/** + * testGroupBy method + * + * These tests will never pass with Postgres or Oracle as all fields in a select must be + * part of an aggregate function or in the GROUP BY statement. + * + * @access public + * @return void + */ + function testGroupBy() { + $db = ConnectionManager::getDataSource('test_suite'); + $isStrictGroupBy = in_array($db->config['driver'], array('postgres', 'oracle')); + $message = '%s Postgresql and Oracle have strict GROUP BY and are incompatible with this test.'; + + if ($this->skipIf($isStrictGroupBy, $message )) { + return; + } + + $this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid'); + $Thread =& new Thread(); + $Product =& new Product(); + + $result = $Thread->find('all', array( + 'group' => 'Thread.project_id', + 'order' => 'Thread.id ASC' + )); + + $expected = array( + array( + 'Thread' => array( + 'id' => 1, + 'project_id' => 1, + 'name' => 'Project 1, Thread 1' + ), + 'Project' => array( + 'id' => 1, + 'name' => 'Project 1' + ), + 'Message' => array( + array( + 'id' => 1, + 'thread_id' => 1, + 'name' => 'Thread 1, Message 1' + ))), + array( + 'Thread' => array( + 'id' => 3, + 'project_id' => 2, + 'name' => 'Project 2, Thread 1' + ), + 'Project' => array( + 'id' => 2, + 'name' => 'Project 2' + ), + 'Message' => array( + array( + 'id' => 3, + 'thread_id' => 3, + 'name' => 'Thread 3, Message 1' + )))); + $this->assertEqual($result, $expected); + + $rows = $Thread->find('all', array( + 'group' => 'Thread.project_id', + 'fields' => array('Thread.project_id', 'COUNT(*) AS total') + )); + $result = array(); + foreach($rows as $row) { + $result[$row['Thread']['project_id']] = $row[0]['total']; + } + $expected = array( + 1 => 2, + 2 => 1 + ); + $this->assertEqual($result, $expected); + + $rows = $Thread->find('all', array( + 'group' => 'Thread.project_id', + 'fields' => array('Thread.project_id', 'COUNT(*) AS total'), + 'order'=> 'Thread.project_id' + )); + $result = array(); + foreach($rows as $row) { + $result[$row['Thread']['project_id']] = $row[0]['total']; + } + $expected = array( + 1 => 2, + 2 => 1 + ); + $this->assertEqual($result, $expected); + + $result = $Thread->find('all', array( + 'conditions' => array('Thread.project_id' => 1), + 'group' => 'Thread.project_id' + )); + $expected = array( + array( + 'Thread' => array( + 'id' => 1, + 'project_id' => 1, + 'name' => 'Project 1, Thread 1' + ), + 'Project' => array( + 'id' => 1, + 'name' => 'Project 1' + ), + 'Message' => array( + array( + 'id' => 1, + 'thread_id' => 1, + 'name' => 'Thread 1, Message 1' + )))); + $this->assertEqual($result, $expected); + + $result = $Thread->find('all', array( + 'conditions' => array('Thread.project_id' => 1), + 'group' => 'Thread.project_id, Project.id' + )); + $this->assertEqual($result, $expected); + + $result = $Thread->find('all', array( + 'conditions' => array('Thread.project_id' => 1), + 'group' => 'project_id' + )); + $this->assertEqual($result, $expected); + + + $result = $Thread->find('all', array( + 'conditions' => array('Thread.project_id' => 1), + 'group' => array('project_id') + )); + $this->assertEqual($result, $expected); + + + $result = $Thread->find('all', array( + 'conditions' => array('Thread.project_id' => 1), + 'group' => array('project_id', 'Project.id') + )); + $this->assertEqual($result, $expected); + + + $result = $Thread->find('all', array( + 'conditions' => array('Thread.project_id' => 1), + 'group' => array('Thread.project_id', 'Project.id') + )); + $this->assertEqual($result, $expected); + + + $expected = array( + array('Product' => array('type' => 'Clothing'), array('price' => 32)), + array('Product' => array('type' => 'Food'), array('price' => 9)), + array('Product' => array('type' => 'Music'), array( 'price' => 4)), + array('Product' => array('type' => 'Toy'), array('price' => 3)) + ); + $result = $Product->find('all',array( + 'fields'=>array('Product.type','MIN(Product.price) as price'), + 'group'=> 'Product.type', + 'order' => 'Product.type ASC' + )); + $this->assertEqual($result, $expected); + + $result = $Product->find('all', array( + 'fields'=>array('Product.type','MIN(Product.price) as price'), + 'group'=> array('Product.type'), + 'order' => 'Product.type ASC')); + $this->assertEqual($result, $expected); + } +/** + * testOldQuery method + * + * @access public + * @return void + */ + function testOldQuery() { + $this->loadFixtures('Article'); + $Article =& new Article(); + + $query = 'SELECT title FROM '; + $query .= $this->db->fullTableName('articles'); + $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id IN (1,2)'; + + $results = $Article->query($query); + $this->assertTrue(is_array($results)); + $this->assertEqual(count($results), 2); + + $query = 'SELECT title, body FROM '; + $query .= $this->db->fullTableName('articles'); + $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id = 1'; + + $results = $Article->query($query, false); + $this->assertTrue(!isset($this->db->_queryCache[$query])); + $this->assertTrue(is_array($results)); + + $query = 'SELECT title, id FROM '; + $query .= $this->db->fullTableName('articles'); + $query .= ' WHERE ' . $this->db->fullTableName('articles'); + $query .= '.published = ' . $this->db->value('Y'); + + $results = $Article->query($query, true); + $this->assertTrue(isset($this->db->_queryCache[$query])); + $this->assertTrue(is_array($results)); + } +/** + * testPreparedQuery method + * + * @access public + * @return void + */ + function testPreparedQuery() { + $this->loadFixtures('Article'); + $Article =& new Article(); + $this->db->_queryCache = array(); + + $finalQuery = 'SELECT title, published FROM '; + $finalQuery .= $this->db->fullTableName('articles'); + $finalQuery .= ' WHERE ' . $this->db->fullTableName('articles'); + $finalQuery .= '.id = ' . $this->db->value(1); + $finalQuery .= ' AND ' . $this->db->fullTableName('articles'); + $finalQuery .= '.published = ' . $this->db->value('Y'); + + $query = 'SELECT title, published FROM '; + $query .= $this->db->fullTableName('articles'); + $query .= ' WHERE ' . $this->db->fullTableName('articles'); + $query .= '.id = ? AND ' . $this->db->fullTableName('articles') . '.published = ?'; + + $params = array(1, 'Y'); + $result = $Article->query($query, $params); + $expected = array( + '0' => array( + $this->db->fullTableName('articles', false) => array( + 'title' => 'First Article', 'published' => 'Y') + )); + + if (isset($result[0][0])) { + $expected[0][0] = $expected[0][$this->db->fullTableName('articles', false)]; + unset($expected[0][$this->db->fullTableName('articles', false)]); + } + + $this->assertEqual($result, $expected); + $this->assertTrue(isset($this->db->_queryCache[$finalQuery])); + + $finalQuery = 'SELECT id, created FROM '; + $finalQuery .= $this->db->fullTableName('articles'); + $finalQuery .= ' WHERE ' . $this->db->fullTableName('articles'); + $finalQuery .= '.title = ' . $this->db->value('First Article'); + + $query = 'SELECT id, created FROM '; + $query .= $this->db->fullTableName('articles'); + $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title = ?'; + + $params = array('First Article'); + $result = $Article->query($query, $params, false); + $this->assertTrue(is_array($result)); + $this->assertTrue( + isset($result[0][$this->db->fullTableName('articles', false)]) + || isset($result[0][0]) + ); + $this->assertFalse(isset($this->db->_queryCache[$finalQuery])); + + $query = 'SELECT title FROM '; + $query .= $this->db->fullTableName('articles'); + $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title LIKE ?'; + + $params = array('%First%'); + $result = $Article->query($query, $params); + $this->assertTrue(is_array($result)); + $this->assertTrue( + isset($result[0][$this->db->fullTableName('articles', false)]['title']) + || isset($result[0][0]['title']) + ); + + //related to ticket #5035 + $query = 'SELECT title FROM '; + $query .= $this->db->fullTableName('articles') . ' WHERE title = ? AND published = ?'; + $params = array('First? Article', 'Y'); + $Article->query($query, $params); + + $expected = 'SELECT title FROM '; + $expected .= $this->db->fullTableName('articles'); + $expected .= " WHERE title = 'First? Article' AND published = 'Y'"; + $this->assertTrue(isset($this->db->_queryCache[$expected])); + + } +/** + * testParameterMismatch method + * + * @access public + * @return void + */ + function testParameterMismatch() { + $this->loadFixtures('Article'); + $Article =& new Article(); + + $query = 'SELECT * FROM ' . $this->db->fullTableName('articles'); + $query .= ' WHERE ' . $this->db->fullTableName('articles'); + $query .= '.published = ? AND ' . $this->db->fullTableName('articles') . '.user_id = ?'; + $params = array('Y'); + $this->expectError(); + + ob_start(); + $result = $Article->query($query, $params); + ob_end_clean(); + $this->assertEqual($result, null); + } +/** + * testVeryStrangeUseCase method + * + * @access public + * @return void + */ + function testVeryStrangeUseCase() { + $message = "%s skipping SELECT * FROM ? WHERE ? = ? AND ? = ?; prepared query."; + $message .= " MSSQL is incompatible with this test."; + + if ($this->skipIf($this->db->config['driver'] == 'mssql', $message)) { + return; + } + + $this->loadFixtures('Article'); + $Article =& new Article(); + + $query = 'SELECT * FROM ? WHERE ? = ? AND ? = ?'; + $param = array( + $this->db->fullTableName('articles'), + $this->db->fullTableName('articles') . '.user_id', '3', + $this->db->fullTableName('articles') . '.published', 'Y' + ); + $this->expectError(); + + ob_start(); + $result = $Article->query($query, $param); + ob_end_clean(); + } +/** + * testRecursiveUnbind method + * + * @access public + * @return void + */ + function testRecursiveUnbind() { + $this->loadFixtures('Apple', 'Sample'); + $TestModel =& new Apple(); + $TestModel->recursive = 2; + + $result = $TestModel->find('all'); + $expected = array( + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' =>'', + 'apple_id' => '', + 'name' => '' + ), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2', + 'Apple' => 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' + )), + 'Child' => 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 1, + 'apple_id' => 3, + 'name' => 'sample1' + )), + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3' + ), + 'Child' => array( + 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 1, + 'apple_id' => 3, + 'name' => 'sample1', + 'Apple' => 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' + )), + 'Child' => array() + ), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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'), + 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3', + 'Apple' => 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' + )), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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' + ))))), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4', + 'Apple' => 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' + )), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4' + ), + 'Child' => array( + 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + ), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array() + ))), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + ), + 'Child' => array())); + $this->assertEqual($result, $expected); + + $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample'))); + $this->assertTrue($result); + + $result = $TestModel->find('all'); + $expected = array( + array( + 'Apple' => 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'), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' =>'', + 'apple_id' => '', + 'name' => '' + ), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2', + 'Apple' => 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' + )), + 'Child' => 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 1, + 'apple_id' => 3, + 'name' => 'sample1' + )), + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3' + ), + 'Child' => array( + 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 1, + 'apple_id' => 3, + 'name' => 'sample1', + 'Apple' => 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' + )), + 'Child' => array() + ), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3', + 'Apple' => 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' + )), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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' + ))))), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4', + 'Apple' => 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' + )), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4' + ), + 'Child' => array( + 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + ), + 'Child' => array( + 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', + 'Parent' => 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' + ), + 'Sample' => array() + ))), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + ), + 'Child' => array() + )); + + $this->assertEqual($result, $expected); + + $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample'))); + $this->assertTrue($result); + + $result = $TestModel->unbindModel(array('hasMany' => array('Child'))); + $this->assertTrue($result); + + $result = $TestModel->find('all'); + $expected = array( + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' =>'', + 'apple_id' => '', + 'name' => '' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2', + 'Apple' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 1, + 'apple_id' => 3, + 'name' => 'sample1', + 'Apple' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3', + 'Apple' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4', + 'Apple' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + ))); + + $this->assertEqual($result, $expected); + + $result = $TestModel->unbindModel(array('hasMany' => array('Child'))); + $this->assertTrue($result); + + $result = $TestModel->Sample->unbindModel(array('belongsTo' => array('Apple'))); + $this->assertTrue($result); + + $result = $TestModel->find('all'); + $expected = array( + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' =>'', + 'apple_id' => '', + 'name' => '' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 1, + 'apple_id' => 3, + 'name' => 'sample1' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Parent' => 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' + ), + 'Sample' => array(), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->Parent->unbindModel(array('belongsTo' => array('Parent'))); + $this->assertTrue($result); + + $result = $TestModel->unbindModel(array('hasMany' => array('Child'))); + $this->assertTrue($result); + + $result = $TestModel->find('all'); + $expected = array( + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' =>'', + 'apple_id' => '', + 'name' => '' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Sample' => array(), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2', + 'Apple' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 1, + 'apple_id' => 3, + 'name' => 'sample1', + 'Apple' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Sample' => array( + 'id' => 2, + 'apple_id' => 2, + 'name' => 'sample2' + ), + 'Child' => 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' => 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' + ))), + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3', + 'Apple' => 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( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => 4, + 'apple_id' => 5, + 'name' => 'sample4', + 'Apple' => 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( + 'Apple' => 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'), + 'Parent' => 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', + 'Sample' => array( + 'id' => 3, + 'apple_id' => 4, + 'name' => 'sample3' + ), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + )), + array( + 'Apple' => 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' + ), + 'Parent' => 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', + 'Sample' => array(), + 'Child' => array( + 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' + ))), + 'Sample' => array( + 'id' => '', + 'apple_id' => '', + 'name' => '' + ))); + $this->assertEqual($result, $expected); + } +/** + * testSelfAssociationAfterFind method + * + * @access public + * @return void + */ + function testSelfAssociationAfterFind() { + $this->loadFixtures('Apple'); + $afterFindModel = new NodeAfterFind(); + $afterFindModel->recursive = 3; + $afterFindData = $afterFindModel->find('all'); + + $duplicateModel = new NodeAfterFind(); + $duplicateModel->recursive = 3; + $duplicateModelData = $duplicateModel->find('all'); + + $noAfterFindModel = new NodeNoAfterFind(); + $noAfterFindModel->recursive = 3; + $noAfterFindData = $noAfterFindModel->find('all'); + + $this->assertFalse($afterFindModel == $noAfterFindModel); + // Limitation of PHP 4 and PHP 5 > 5.1.6 when comparing recursive objects + if (PHP_VERSION === '5.1.6') { + $this->assertFalse($afterFindModel != $duplicateModel); + } + $this->assertEqual($afterFindData, $noAfterFindData); + } +/** + * testFindAllThreaded method + * + * @access public + * @return void + */ + function testFindAllThreaded() { + $this->loadFixtures('Category'); + $TestModel =& new Category(); + + $result = $TestModel->find('threaded'); + $expected = array( + array( + 'Category' => array( + 'id' => '1', + 'parent_id' => '0', + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '2', + 'parent_id' => '1', + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array('Category' => array( + 'id' => '7', + 'parent_id' => '2', + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array()), + array('Category' => array( + 'id' => '8', + 'parent_id' => '2', + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array())) + ), + array( + 'Category' => array( + 'id' => '3', + 'parent_id' => '1', + 'name' => 'Category 1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ) + ) + ), + array( + 'Category' => array( + 'id' => '4', + 'parent_id' => '0', + 'name' => 'Category 2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ), + array( + 'Category' => array( + 'id' => '5', + 'parent_id' => '0', + 'name' => 'Category 3', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '6', + 'parent_id' => '5', + 'name' => 'Category 3.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ) + ) + ) + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('threaded', array( + 'conditions' => array('Category.name LIKE' => 'Category 1%') + )); + + $expected = array( + array( + 'Category' => array( + 'id' => '1', + 'parent_id' => '0', + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '2', + 'parent_id' => '1', + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array('Category' => array( + 'id' => '7', + 'parent_id' => '2', + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array()), + array('Category' => array( + 'id' => '8', + 'parent_id' => '2', + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array())) + ), + array( + 'Category' => array( + 'id' => '3', + 'parent_id' => '1', + 'name' => 'Category 1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ) + ) + ) + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('threaded', array( + 'fields' => 'id, parent_id, name' + )); + + $expected = array( + array( + 'Category' => array( + 'id' => '1', + 'parent_id' => '0', + 'name' => 'Category 1' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '2', + 'parent_id' => '1', + 'name' => 'Category 1.1' + ), + 'children' => array( + array('Category' => array( + 'id' => '7', + 'parent_id' => '2', + 'name' => 'Category 1.1.1'), + 'children' => array()), + array('Category' => array( + 'id' => '8', + 'parent_id' => '2', + 'name' => 'Category 1.1.2'), + 'children' => array())) + ), + array( + 'Category' => array( + 'id' => '3', + 'parent_id' => '1', + 'name' => 'Category 1.2' + ), + 'children' => array() + ) + ) + ), + array( + 'Category' => array( + 'id' => '4', + 'parent_id' => '0', + 'name' => 'Category 2' + ), + 'children' => array() + ), + array( + 'Category' => array( + 'id' => '5', + 'parent_id' => '0', + 'name' => 'Category 3' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '6', + 'parent_id' => '5', + 'name' => 'Category 3.1' + ), + 'children' => array() + ) + ) + ) + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('threaded', array('order' => 'id DESC')); + + $expected = array( + array( + 'Category' => array( + 'id' => 5, + 'parent_id' => 0, + 'name' => 'Category 3', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => 6, + 'parent_id' => 5, + 'name' => 'Category 3.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ) + ) + ), + array( + 'Category' => array( + 'id' => 4, + 'parent_id' => 0, + 'name' => 'Category 2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ), + array( + 'Category' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => 3, + 'parent_id' => 1, + 'name' => 'Category 1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ), + array( + 'Category' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array('Category' => array( + 'id' => '8', + 'parent_id' => '2', + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array()), + array('Category' => array( + 'id' => '7', + 'parent_id' => '2', + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array())) + ) + ) + ) + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('threaded', array( + 'conditions' => array('Category.name LIKE' => 'Category 3%') + )); + $expected = array( + array( + 'Category' => array( + 'id' => '5', + 'parent_id' => '0', + 'name' => 'Category 3', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '6', + 'parent_id' => '5', + 'name' => 'Category 3.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'children' => array() + ) + ) + ) + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('threaded', array( + 'conditions' => array('Category.name LIKE' => 'Category 1.1%') + )); + $expected = array( + array('Category' => + array( + 'id' => '2', + 'parent_id' => '1', + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array( + array('Category' => array( + 'id' => '7', + 'parent_id' => '2', + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array()), + array('Category' => array( + 'id' => '8', + 'parent_id' => '2', + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31'), + 'children' => array())))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('threaded', array( + 'fields' => 'id, parent_id, name', + 'conditions' => array('Category.id !=' => 2) + )); + $expected = array( + array( + 'Category' => array( + 'id' => '1', + 'parent_id' => '0', + 'name' => 'Category 1' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '3', + 'parent_id' => '1', + 'name' => 'Category 1.2' + ), + 'children' => array() + ) + ) + ), + array( + 'Category' => array( + 'id' => '4', + 'parent_id' => '0', + 'name' => 'Category 2' + ), + 'children' => array() + ), + array( + 'Category' => array( + 'id' => '5', + 'parent_id' => '0', + 'name' => 'Category 3' + ), + 'children' => array( + array( + 'Category' => array( + 'id' => '6', + 'parent_id' => '5', + 'name' => 'Category 3.1' + ), + 'children' => array() + ) + ) + ) + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array( + 'fields' => 'id, name, parent_id', + 'conditions' => array('Category.id !=' => 1) + )); + $expected = array ( + array ('Category' => array( + 'id' => '2', + 'name' => 'Category 1.1', + 'parent_id' => '1' + )), + array ('Category' => array( + 'id' => '3', + 'name' => 'Category 1.2', + 'parent_id' => '1' + )), + array ('Category' => array( + 'id' => '4', + 'name' => 'Category 2', + 'parent_id' => '0' + )), + array ('Category' => array( + 'id' => '5', + 'name' => 'Category 3', + 'parent_id' => '0' + )), + array ('Category' => array( + 'id' => '6', + 'name' => 'Category 3.1', + 'parent_id' => '5' + )), + array ('Category' => array( + 'id' => '7', + 'name' => 'Category 1.1.1', + 'parent_id' => '2' + )), + array ('Category' => array( + 'id' => '8', + 'name' => 'Category 1.1.2', + 'parent_id' => '2' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('threaded', array( + 'fields' => 'id, parent_id, name', + 'conditions' => array('Category.id !=' => 1) + )); + $expected = array( + array( + 'Category' => array( + 'id' => '2', + 'parent_id' => '1', + 'name' => 'Category 1.1' + ), + 'children' => array( + array('Category' => array( + 'id' => '7', + 'parent_id' => '2', + 'name' => 'Category 1.1.1'), + 'children' => array()), + array('Category' => array( + 'id' => '8', + 'parent_id' => '2', + 'name' => 'Category 1.1.2'), + 'children' => array())) + ), + array( + 'Category' => array( + 'id' => '3', + 'parent_id' => '1', + 'name' => 'Category 1.2' + ), + 'children' => array() + ) + ); + $this->assertEqual($result, $expected); + } +/** + * test find('neighbors') + * + * @return void + * @access public + */ + function testFindNeighbors() { + $this->loadFixtures('User', 'Article'); + $TestModel =& new Article(); + + $TestModel->id = 1; + $result = $TestModel->find('neighbors', array('fields' => array('id'))); + $expected = array( + 'prev' => null, + 'next' => array( + 'Article' => array('id' => 2) + )); + $this->assertEqual($result, $expected); + + $TestModel->id = 2; + $result = $TestModel->find('neighbors', array( + 'fields' => array('id') + )); + + $expected = array( + 'prev' => array( + 'Article' => array( + 'id' => 1 + )), + 'next' => array( + 'Article' => array( + 'id' => 3 + ))); + $this->assertEqual($result, $expected); + + $TestModel->id = 3; + $result = $TestModel->find('neighbors', array('fields' => array('id'))); + $expected = array( + 'prev' => array( + 'Article' => array( + 'id' => 2 + )), + 'next' => null + ); + $this->assertEqual($result, $expected); + + $TestModel->id = 1; + $result = $TestModel->find('neighbors', array('recursive' => -1)); + $expected = array( + 'prev' => null, + 'next' => array( + 'Article' => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ) + ) + ); + $this->assertEqual($result, $expected); + + $TestModel->id = 2; + $result = $TestModel->find('neighbors', array('recursive' => -1)); + $expected = array( + 'prev' => array( + 'Article' => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ) + ), + 'next' => array( + 'Article' => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ) + ) + ); + $this->assertEqual($result, $expected); + + $TestModel->id = 3; + $result = $TestModel->find('neighbors', array('recursive' => -1)); + $expected = array( + 'prev' => array( + 'Article' => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ) + ), + 'next' => null + ); + $this->assertEqual($result, $expected); + + $TestModel->recursive = 0; + $TestModel->id = 1; + $one = $TestModel->read(); + $TestModel->id = 2; + $two = $TestModel->read(); + $TestModel->id = 3; + $three = $TestModel->read(); + + $TestModel->id = 1; + $result = $TestModel->find('neighbors'); + $expected = array('prev' => null, 'next' => $two); + $this->assertEqual($result, $expected); + + $TestModel->id = 2; + $result = $TestModel->find('neighbors'); + $expected = array('prev' => $one, 'next' => $three); + $this->assertEqual($result, $expected); + + $TestModel->id = 3; + $result = $TestModel->find('neighbors'); + $expected = array('prev' => $two, 'next' => null); + $this->assertEqual($result, $expected); + + $TestModel->recursive = 2; + $TestModel->id = 1; + $one = $TestModel->read(); + $TestModel->id = 2; + $two = $TestModel->read(); + $TestModel->id = 3; + $three = $TestModel->read(); + + $TestModel->id = 1; + $result = $TestModel->find('neighbors', array('recursive' => 2)); + $expected = array('prev' => null, 'next' => $two); + $this->assertEqual($result, $expected); + + $TestModel->id = 2; + $result = $TestModel->find('neighbors', array('recursive' => 2)); + $expected = array('prev' => $one, 'next' => $three); + $this->assertEqual($result, $expected); + + $TestModel->id = 3; + $result = $TestModel->find('neighbors', array('recursive' => 2)); + $expected = array('prev' => $two, 'next' => null); + $this->assertEqual($result, $expected); + } +/** + * test findNeighbours() method + * + * @return void + * @access public + */ + function testFindNeighboursLegacy() { + $this->loadFixtures('User', 'Article'); + $TestModel =& new Article(); + + $result = $TestModel->findNeighbours(null, 'Article.id', '2'); + $expected = array( + 'prev' => array( + 'Article' => array( + 'id' => 1 + )), + 'next' => array( + 'Article' => array( + 'id' => 3 + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->findNeighbours(null, 'Article.id', '3'); + $expected = array( + 'prev' => array( + 'Article' => array( + 'id' => 2 + )), + 'next' => array() + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->findNeighbours( + array('User.id' => 1), + array('Article.id', 'Article.title'), + 2 + ); + $expected = array( + 'prev' => array( + 'Article' => array( + 'id' => 1, + 'title' => 'First Article' + )), + 'next' => array( + 'Article' => array( + 'id' => 3, + 'title' => 'Third Article' + ))); + $this->assertEqual($result, $expected); + } +/** + * testFindCombinedRelations method + * + * @access public + * @return void + */ + function testFindCombinedRelations() { + $this->loadFixtures('Apple', 'Sample'); + $TestModel =& new Apple(); + + $result = $TestModel->find('all'); + + $expected = array( + array( + 'Apple' => 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' + ), + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => null, + 'apple_id' => null, + 'name' => null + ), + 'Child' => array( + 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( + 'Apple' => 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' + ), + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => '2', + 'apple_id' => '2', + 'name' => 'sample2' + ), + 'Child' => 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' => '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( + 'Apple' => 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' + ), + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => '1', + 'apple_id' => '3', + 'name' => 'sample1' + ), + 'Child' => array() + ), + array( + 'Apple' => 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' + ), + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => '3', + 'apple_id' => '4', + 'name' => 'sample3' + ), + 'Child' => array( + 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( + 'Apple' => 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' + ), + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => '4', + 'apple_id' => '5', + 'name' => 'sample4' + ), + 'Child' => array( + 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( + 'Apple' => 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' + ), + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => null, + 'apple_id' => null, + 'name' => null + ), + 'Child' => array( + 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' + ))), + array( + 'Apple' => 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' + ), + 'Parent' => 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' + ), + 'Sample' => array( + 'id' => null, + 'apple_id' => null, + 'name' => null + ), + 'Child' => array() + )); + $this->assertEqual($result, $expected); + } +/** + * testSaveEmpty method + * + * @access public + * @return void + */ + function testSaveEmpty() { + $this->loadFixtures('Thread'); + $TestModel =& new Thread(); + $data = array(); + $expected = $TestModel->save($data); + $this->assertFalse($expected); + } + // function testBasicValidation() { + // $TestModel =& new ValidationTest1(); + // $TestModel->testing = true; + // $TestModel->set(array('title' => '', 'published' => 1)); + // $this->assertEqual($TestModel->invalidFields(), array('title' => 'This field cannot be left blank')); + // + // $TestModel->create(); + // $TestModel->set(array('title' => 'Hello', 'published' => 0)); + // $this->assertEqual($TestModel->invalidFields(), array('published' => 'This field cannot be left blank')); + // + // $TestModel->create(); + // $TestModel->set(array('title' => 'Hello', 'published' => 1, 'body' => '')); + // $this->assertEqual($TestModel->invalidFields(), array('body' => 'This field cannot be left blank')); + // } + +/** + * testFindAllWithConditionInChildQuery + * + * @todo external conditions like this are going to need to be revisited at some point + * @access public + * @return void + */ + function testFindAllWithConditionInChildQuery() { + $this->loadFixtures('Basket', 'FilmFile'); + + $TestModel =& new Basket(); + $recursive = 3; + $result = $TestModel->find('all', compact('conditions', 'recursive')); + + $expected = array( + array( + 'Basket' => array( + 'id' => 1, + 'type' => 'nonfile', + 'name' => 'basket1', + 'object_id' => 1, + 'user_id' => 1, + ), + 'FilmFile' => array( + 'id' => '', + 'name' => '', + ) + ), + array( + 'Basket' => array( + 'id' => 2, + 'type' => 'file', + 'name' => 'basket2', + 'object_id' => 2, + 'user_id' => 1, + ), + 'FilmFile' => array( + 'id' => 2, + 'name' => 'two', + ) + ), + ); + $this->assertEqual($result, $expected); + } + +/** + * testFindAllWithConditionsHavingMixedDataTypes method + * + * @access public + * @return void + */ + function testFindAllWithConditionsHavingMixedDataTypes() { + $this->loadFixtures('Article'); + $TestModel =& new Article(); + $expected = array( + array( + 'Article' => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ) + ), + array( + 'Article' => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ) + ) + ); + $conditions = array('id' => array('1', 2)); + $recursive = -1; + $order = 'Article.id ASC'; + $result = $TestModel->find('all', compact('conditions', 'recursive', 'order')); + $this->assertEqual($result, $expected); + + + $conditions = array('id' => array('1', 2, '3.0')); + $order = 'Article.id ASC'; + $result = $TestModel->find('all', compact('recursive', 'conditions', 'order')); + $expected = array( + array( + 'Article' => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ) + ), + array( + 'Article' => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ) + ), + array( + 'Article' => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ) + ) + ); + $this->assertEqual($result, $expected); + } +/** + * testBindUnbind method + * + * @access public + * @return void + */ + function testBindUnbind() { + $this->loadFixtures('User', 'Comment', 'FeatureSet'); + $TestModel =& new User(); + + $result = $TestModel->hasMany; + $expected = array(); + $this->assertEqual($result, $expected); + + $result = $TestModel->bindModel(array('hasMany' => array('Comment'))); + $this->assertTrue($result); + + $result = $TestModel->find('all', array( + 'fields' => 'User.id, User.user' + )); + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Comment' => array( + array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + array( + 'id' => '4', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ), + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ))), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ))), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry' + ), + 'Comment' => array() + ), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett' + ), + 'Comment' => array( + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + )))); + + $this->assertEqual($result, $expected); + + $TestModel->resetAssociations(); + $result = $TestModel->hasMany; + $this->assertEqual($result, array()); + + $result = $TestModel->bindModel(array('hasMany' => array('Comment')), false); + $this->assertTrue($result); + + $result = $TestModel->find('all', array( + 'fields' => 'User.id, User.user' + )); + + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Comment' => array( + array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + array( + 'id' => '4', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ), + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ))), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ))), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry' + ), + 'Comment' => array() + ), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett' + ), + 'Comment' => array( + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + )))); + + $this->assertEqual($result, $expected); + + $result = $TestModel->hasMany; + $expected = array( + 'Comment' => array( + 'className' => 'Comment', + 'foreignKey' => 'user_id', + 'conditions' => null, + 'fields' => null, + 'order' => null, + 'limit' => null, + 'offset' => null, + 'dependent' => null, + 'exclusive' => null, + 'finderQuery' => null, + 'counterQuery' => null + )); + $this->assertEqual($result, $expected); + + $result = $TestModel->unbindModel(array('hasMany' => array('Comment'))); + $this->assertTrue($result); + + $result = $TestModel->hasMany; + $expected = array(); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array( + 'fields' => 'User.id, User.user' + )); + $expected = array( + array('User' => array('id' => '1', 'user' => 'mariano')), + array('User' => array('id' => '2', 'user' => 'nate')), + array('User' => array('id' => '3', 'user' => 'larry')), + array('User' => array('id' => '4', 'user' => 'garrett'))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array( + 'fields' => 'User.id, User.user' + )); + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Comment' => array( + array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + array( + 'id' => '4', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ), + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ))), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ))), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry' + ), + 'Comment' => array() + ), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett' + ), + 'Comment' => array( + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => + 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + )))); + $this->assertEqual($result, $expected); + + $result = $TestModel->unbindModel(array('hasMany' => array('Comment')), false); + $this->assertTrue($result); + + $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); + $expected = array( + array('User' => array('id' => '1', 'user' => 'mariano')), + array('User' => array('id' => '2', 'user' => 'nate')), + array('User' => array('id' => '3', 'user' => 'larry')), + array('User' => array('id' => '4', 'user' => 'garrett'))); + $this->assertEqual($result, $expected); + + $result = $TestModel->hasMany; + $expected = array(); + $this->assertEqual($result, $expected); + + $result = $TestModel->bindModel(array('hasMany' => array( + 'Comment' => array('className' => 'Comment', 'conditions' => 'Comment.published = \'Y\'') + ))); + $this->assertTrue($result); + + $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Comment' => array( + array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ))), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ))), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry' + ), + 'Comment' => array() + ), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett' + ), + 'Comment' => array( + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + )))); + + $this->assertEqual($result, $expected); + + $TestModel2 =& new DeviceType(); + + $expected = array( + 'className' => 'FeatureSet', + 'foreignKey' => 'feature_set_id', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'counterCache' => '' + ); + $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected); + + $TestModel2->bind('FeatureSet', array( + 'conditions' => array('active' => true) + )); + $expected['conditions'] = array('active' => true); + $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected); + + $TestModel2->bind('FeatureSet', array( + 'foreignKey' => false, + 'conditions' => array('Feature.name' => 'DeviceType.name') + )); + $expected['conditions'] = array('Feature.name' => 'DeviceType.name'); + $expected['foreignKey'] = false; + $this->assertEqual($TestModel2->belongsTo['FeatureSet'], $expected); + + $TestModel2->bind('NewFeatureSet', array( + 'type' => 'hasMany', + 'className' => 'FeatureSet', + 'conditions' => array('active' => true) + )); + $expected = array( + 'className' => 'FeatureSet', + 'conditions' => array('active' => true), + 'foreignKey' => 'device_type_id', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'dependent' => '', + 'exclusive' => '', + 'finderQuery' => '', + 'counterQuery' => '' + ); + $this->assertEqual($TestModel2->hasMany['NewFeatureSet'], $expected); + $this->assertTrue(is_object($TestModel2->NewFeatureSet)); + } +/** + * testBindMultipleTimes method + * + * @access public + * @return void + */ + function testBindMultipleTimes() { + $this->loadFixtures('User', 'Comment', 'Article'); + $TestModel =& new User(); + + $result = $TestModel->hasMany; + $expected = array(); + $this->assertEqual($result, $expected); + + $result = $TestModel->bindModel(array( + 'hasMany' => array( + 'Items' => array('className' => 'Comment') + ))); + $this->assertTrue($result); + + $result = $TestModel->find('all', array( + 'fields' => 'User.id, User.user' + )); + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Items' => array( + array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + array( + 'id' => '4', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ), + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ))), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate' + ), + 'Items' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + ))), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry' + ), + 'Items' => array() + ), + array( + 'User' => array( + 'id' => '4', 'user' => 'garrett'), + 'Items' => array( + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + )))); + $this->assertEqual($result, $expected); + + $result = $TestModel->bindModel(array( + 'hasMany' => array( + 'Items' => array('className' => 'Article') + ))); + $this->assertTrue($result); + + $result = $TestModel->find('all', array( + 'fields' => 'User.id, User.user' + )); + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Items' => array( + array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ))), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate' + ), + 'Items' => array() + ), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry' + ), + 'Items' => array( + array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ))), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett' + ), + 'Items' => array() + )); + $this->assertEqual($result, $expected); + } + +/** + * test that bindModel behaves with Custom primary Key associations + * + * @return void + **/ + function bindWithCustomPrimaryKey() { + $this->loadFixtures('Story', 'StoriesTag', 'Tag'); + $Model =& ClassRegistry::init('StoriesTag'); + $Model->bindModel(array( + 'belongsTo' => array( + 'Tag' => array( + 'className' => 'Tag', + 'foreignKey' => 'story' + )))); + + $result = $Model->find('all'); + $this->assertFalse(empty($result)); + } + +/** + * testAssociationAfterFind method + * + * @access public + * @return void + */ + function testAssociationAfterFind() { + $this->loadFixtures('Post', 'Author', 'Comment'); + $TestModel =& new Post(); + $result = $TestModel->find('all'); + $expected = array( + array( + 'Post' => array( + 'id' => '1', + 'author_id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'Author' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31', + 'test' => 'working' + )), + array( + 'Post' => array( + 'id' => '2', + 'author_id' => '3', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'Author' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31', + 'test' => 'working' + )), + array( + 'Post' => array( + 'id' => '3', + 'author_id' => '1', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'Author' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31', + 'test' => 'working' + ))); + $this->assertEqual($result, $expected); + unset($TestModel); + + $Author =& new Author(); + $Author->Post->bindModel(array( + 'hasMany' => array( + 'Comment' => array( + 'className' => 'ModifiedComment', + 'foreignKey' => 'article_id', + ) + ))); + $result = $Author->find('all', array( + 'conditions' => array('Author.id' => 1), + 'recursive' => 2 + )); + $expected = array( + 'id' => 1, + 'article_id' => 1, + 'user_id' => 2, + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31', + 'callback' => 'Fire' + ); + $this->assertEqual($result[0]['Post'][0]['Comment'][0], $expected); + } +/** + * Tests that callbacks can be properly disabled + * + * @access public + * @return void + */ + function testCallbackDisabling() { + $this->loadFixtures('Author'); + $TestModel = new ModifiedAuthor(); + + $result = Set::extract($TestModel->find('all'), '/Author/user'); + $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)'); + $this->assertEqual($result, $expected); + + $result = Set::extract($TestModel->find('all', array('callbacks' => 'after')), '/Author/user'); + $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)'); + $this->assertEqual($result, $expected); + + $result = Set::extract($TestModel->find('all', array('callbacks' => 'before')), '/Author/user'); + $expected = array('mariano', 'nate', 'larry', 'garrett'); + $this->assertEqual($result, $expected); + + $result = Set::extract($TestModel->find('all', array('callbacks' => false)), '/Author/user'); + $expected = array('mariano', 'nate', 'larry', 'garrett'); + $this->assertEqual($result, $expected); + } +/** + * testMultipleBelongsToWithSameClass method + * + * @access public + * @return void + */ + function testMultipleBelongsToWithSameClass() { + $this->loadFixtures( + 'DeviceType', + 'DeviceTypeCategory', + 'FeatureSet', + 'ExteriorTypeCategory', + 'Document', + 'Device', + 'DocumentDirectory' + ); + + $DeviceType =& new DeviceType(); + + $DeviceType->recursive = 2; + $result = $DeviceType->read(null, 1); + + $expected = array( + 'DeviceType' => array( + 'id' => 1, + 'device_type_category_id' => 1, + 'feature_set_id' => 1, + 'exterior_type_category_id' => 1, + 'image_id' => 1, + 'extra1_id' => 1, + 'extra2_id' => 1, + 'name' => 'DeviceType 1', + 'order' => 0 + ), + 'Image' => array( + 'id' => 1, + 'document_directory_id' => 1, + 'name' => 'Document 1', + 'DocumentDirectory' => array( + 'id' => 1, + 'name' => 'DocumentDirectory 1' + )), + 'Extra1' => array( + 'id' => 1, + 'document_directory_id' => 1, + 'name' => 'Document 1', + 'DocumentDirectory' => array( + 'id' => 1, + 'name' => 'DocumentDirectory 1' + )), + 'Extra2' => array( + 'id' => 1, + 'document_directory_id' => 1, + 'name' => 'Document 1', + 'DocumentDirectory' => array( + 'id' => 1, + 'name' => 'DocumentDirectory 1' + )), + 'DeviceTypeCategory' => array( + 'id' => 1, + 'name' => 'DeviceTypeCategory 1' + ), + 'FeatureSet' => array( + 'id' => 1, + 'name' => 'FeatureSet 1' + ), + 'ExteriorTypeCategory' => array( + 'id' => 1, + 'image_id' => 1, + 'name' => 'ExteriorTypeCategory 1', + 'Image' => array( + 'id' => 1, + 'device_type_id' => 1, + 'name' => 'Device 1', + 'typ' => 1 + )), + 'Device' => array( + array( + 'id' => 1, + 'device_type_id' => 1, + 'name' => 'Device 1', + 'typ' => 1 + ), + array( + 'id' => 2, + 'device_type_id' => 1, + 'name' => 'Device 2', + 'typ' => 1 + ), + array( + 'id' => 3, + 'device_type_id' => 1, + 'name' => 'Device 3', + 'typ' => 2 + ))); + + $this->assertEqual($result, $expected); + } +/** + * testHabtmRecursiveBelongsTo method + * + * @access public + * @return void + */ + function testHabtmRecursiveBelongsTo() { + $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image'); + $Portfolio =& new Portfolio(); + + $result = $Portfolio->find(array('id' => 2), null, null, 3); + $expected = array( + 'Portfolio' => array( + 'id' => 2, + 'seller_id' => 1, + 'name' => 'Portfolio 2' + ), + 'Item' => array( + array( + 'id' => 2, + 'syfile_id' => 2, + 'published' => 0, + 'name' => 'Item 2', + 'ItemsPortfolio' => array( + 'id' => 2, + 'item_id' => 2, + 'portfolio_id' => 2 + ), + 'Syfile' => array( + 'id' => 2, + 'image_id' => 2, + 'name' => 'Syfile 2', + 'item_count' => null, + 'Image' => array( + 'id' => 2, + 'name' => 'Image 2' + ) + )), + array( + 'id' => 6, + 'syfile_id' => 6, + 'published' => 0, + 'name' => 'Item 6', + 'ItemsPortfolio' => array( + 'id' => 6, + 'item_id' => 6, + 'portfolio_id' => 2 + ), + 'Syfile' => array( + 'id' => 6, + 'image_id' => null, + 'name' => 'Syfile 6', + 'item_count' => null, + 'Image' => array() + )))); + + $this->assertEqual($result, $expected); + } +/** + * testHabtmFinderQuery method + * + * @access public + * @return void + */ + function testHabtmFinderQuery() { + $this->loadFixtures('Article', 'Tag', 'ArticlesTag'); + $Article =& new Article(); + + $sql = $this->db->buildStatement( + array( + 'fields' => $this->db->fields($Article->Tag, null, array( + 'Tag.id', 'Tag.tag', 'ArticlesTag.article_id', 'ArticlesTag.tag_id' + )), + 'table' => $this->db->fullTableName('tags'), + 'alias' => 'Tag', + 'limit' => null, + 'offset' => null, + 'group' => null, + 'joins' => array(array( + 'alias' => 'ArticlesTag', + 'table' => $this->db->fullTableName('articles_tags'), + 'conditions' => array( + array("ArticlesTag.article_id" => '{$__cakeID__$}'), + array("ArticlesTag.tag_id" => $this->db->identifier('Tag.id')) + ) + )), + 'conditions' => array(), + 'order' => null + ), + $Article + ); + + $Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql; + $result = $Article->find('first'); + $expected = array( + array( + 'id' => '1', + 'tag' => 'tag1' + ), + array( + 'id' => '2', + 'tag' => 'tag2' + )); + + $this->assertEqual($result['Tag'], $expected); + } +/** + * testHabtmLimitOptimization method + * + * @access public + * @return void + */ + function testHabtmLimitOptimization() { + $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag'); + $TestModel =& new Article(); + + $TestModel->hasAndBelongsToMany['Tag']['limit'] = 2; + $result = $TestModel->read(null, 2); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'Comment' => array( + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + )), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + + $this->assertEqual($result, $expected); + + $TestModel->hasAndBelongsToMany['Tag']['limit'] = 1; + $result = $TestModel->read(null, 2); + unset($expected['Tag'][1]); + + $this->assertEqual($result, $expected); + } +/** + * testHasManyLimitOptimization method + * + * @access public + * @return void + */ + function testHasManyLimitOptimization() { + $this->loadFixtures('Project', 'Thread', 'Message', 'Bid'); + $Project =& new Project(); + $Project->recursive = 3; + + $result = $Project->find('all'); + $expected = array( + array( + 'Project' => array( + 'id' => 1, + 'name' => 'Project 1' + ), + 'Thread' => array( + array( + 'id' => 1, + 'project_id' => 1, + 'name' => 'Project 1, Thread 1', + 'Project' => array( + 'id' => 1, + 'name' => 'Project 1', + 'Thread' => array( + array( + 'id' => 1, + 'project_id' => 1, + 'name' => 'Project 1, Thread 1' + ), + array( + 'id' => 2, + 'project_id' => 1, + 'name' => 'Project 1, Thread 2' + ))), + 'Message' => array( + array( + 'id' => 1, + 'thread_id' => 1, + 'name' => 'Thread 1, Message 1', + 'Bid' => array( + 'id' => 1, + 'message_id' => 1, + 'name' => 'Bid 1.1' + )))), + array( + 'id' => 2, + 'project_id' => 1, + 'name' => 'Project 1, Thread 2', + 'Project' => array( + 'id' => 1, + 'name' => 'Project 1', + 'Thread' => array( + array( + 'id' => 1, + 'project_id' => 1, + 'name' => 'Project 1, Thread 1' + ), + array( + 'id' => 2, + 'project_id' => 1, + 'name' => 'Project 1, Thread 2' + ))), + 'Message' => array( + array( + 'id' => 2, + 'thread_id' => 2, + 'name' => 'Thread 2, Message 1', + 'Bid' => array( + 'id' => 4, + 'message_id' => 2, + 'name' => 'Bid 2.1' + )))))), + array( + 'Project' => array( + 'id' => 2, + 'name' => 'Project 2' + ), + 'Thread' => array( + array( + 'id' => 3, + 'project_id' => 2, + 'name' => 'Project 2, Thread 1', + 'Project' => array( + 'id' => 2, + 'name' => 'Project 2', + 'Thread' => array( + array( + 'id' => 3, + 'project_id' => 2, + 'name' => 'Project 2, Thread 1' + ))), + 'Message' => array( + array( + 'id' => 3, + 'thread_id' => 3, + 'name' => 'Thread 3, Message 1', + 'Bid' => array( + 'id' => 3, + 'message_id' => 3, + 'name' => 'Bid 3.1' + )))))), + array( + 'Project' => array( + 'id' => 3, + 'name' => 'Project 3' + ), + 'Thread' => array() + )); + + $this->assertEqual($result, $expected); + } +/** + * testFindAllRecursiveSelfJoin method + * + * @access public + * @return void + */ + function testFindAllRecursiveSelfJoin() { + $this->loadFixtures('Home', 'AnotherArticle', 'Advertisement'); + $TestModel =& new Home(); + $TestModel->recursive = 2; + + $result = $TestModel->find('all'); + $expected = array( + array( + 'Home' => array( + 'id' => '1', + 'another_article_id' => '1', + 'advertisement_id' => '1', + 'title' => 'First Home', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'AnotherArticle' => array( + 'id' => '1', + 'title' => 'First Article', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'Home' => array( + array( + 'id' => '1', + 'another_article_id' => '1', + 'advertisement_id' => '1', + 'title' => 'First Home', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ))), + 'Advertisement' => array( + 'id' => '1', + 'title' => 'First Ad', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'Home' => array( + array( + 'id' => '1', + 'another_article_id' => '1', + 'advertisement_id' => '1', + 'title' => 'First Home', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + array( + 'id' => '2', + 'another_article_id' => '3', + 'advertisement_id' => '1', + 'title' => 'Second Home', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + )))), + array( + 'Home' => array( + 'id' => '2', + 'another_article_id' => '3', + 'advertisement_id' => '1', + 'title' => 'Second Home', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'AnotherArticle' => array( + 'id' => '3', + 'title' => 'Third Article', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31', + 'Home' => array( + array( + 'id' => '2', + 'another_article_id' => '3', + 'advertisement_id' => '1', + 'title' => 'Second Home', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ))), + 'Advertisement' => array( + 'id' => '1', + 'title' => 'First Ad', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'Home' => array( + array( + 'id' => '1', + 'another_article_id' => '1', + 'advertisement_id' => '1', + 'title' => 'First Home', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + array( + 'id' => '2', + 'another_article_id' => '3', + 'advertisement_id' => '1', + 'title' => 'Second Home', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ))))); + + $this->assertEqual($result, $expected); + + + + } +/** + * testFindAllRecursiveWithHabtm method + * + * @return void + * @access public + */ + function testFindAllRecursiveWithHabtm() { + $this->loadFixtures( + 'MyCategoriesMyUsers', + 'MyCategoriesMyProducts', + 'MyCategory', + 'MyUser', + 'MyProduct' + ); + + $MyUser =& new MyUser(); + $MyUser->recursive = 2; + + $result = $MyUser->find('all'); + $expected = array( + array( + 'MyUser' => array('id' => '1', 'firstname' => 'userA'), + 'MyCategory' => array( + array( + 'id' => '1', + 'name' => 'A', + 'MyProduct' => array( + array( + 'id' => '1', + 'name' => 'book' + ))), + array( + 'id' => '3', + 'name' => 'C', + 'MyProduct' => array( + array( + 'id' => '2', + 'name' => 'computer' + ))))), + array( + 'MyUser' => array( + 'id' => '2', + 'firstname' => 'userB' + ), + 'MyCategory' => array( + array( + 'id' => '1', + 'name' => 'A', + 'MyProduct' => array( + array( + 'id' => '1', + 'name' => 'book' + ))), + array( + 'id' => '2', + 'name' => 'B', + 'MyProduct' => array( + array( + 'id' => '1', + 'name' => 'book' + ), + array( + 'id' => '2', + 'name' => 'computer' + )))))); + + $this->assertIdentical($result, $expected); + } +/** + * testReadFakeThread method + * + * @access public + * @return void + */ + function testReadFakeThread() { + $this->loadFixtures('CategoryThread'); + $TestModel =& new CategoryThread(); + + $fullDebug = $this->db->fullDebug; + $this->db->fullDebug = true; + $TestModel->recursive = 6; + $TestModel->id = 7; + $result = $TestModel->read(); + $expected = array( + 'CategoryThread' => array( + 'id' => 7, + 'parent_id' => 6, + 'name' => 'Category 2.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 6, + 'parent_id' => 5, + 'name' => 'Category 2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 5, + 'parent_id' => 4, + 'name' => 'Category 1.1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 4, + 'parent_id' => 3, + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 3, + 'parent_id' => 2, + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ))))))); + + $this->db->fullDebug = $fullDebug; + $this->assertEqual($result, $expected); + } +/** + * testFindFakeThread method + * + * @access public + * @return void + */ + function testFindFakeThread() { + $this->loadFixtures('CategoryThread'); + $TestModel =& new CategoryThread(); + + $fullDebug = $this->db->fullDebug; + $this->db->fullDebug = true; + $TestModel->recursive = 6; + $result = $TestModel->find(array('CategoryThread.id' => 7)); + + $expected = array( + 'CategoryThread' => array( + 'id' => 7, + 'parent_id' => 6, + 'name' => 'Category 2.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 6, + 'parent_id' => 5, + 'name' => 'Category 2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 5, + 'parent_id' => 4, + 'name' => 'Category 1.1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 4, + 'parent_id' => 3, + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 3, + 'parent_id' => 2, + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ))))))); + + $this->db->fullDebug = $fullDebug; + $this->assertEqual($result, $expected); + } +/** + * testFindAllFakeThread method + * + * @access public + * @return void + */ + function testFindAllFakeThread() { + $this->loadFixtures('CategoryThread'); + $TestModel =& new CategoryThread(); + + $fullDebug = $this->db->fullDebug; + $this->db->fullDebug = true; + $TestModel->recursive = 6; + $result = $TestModel->find('all', null, null, 'CategoryThread.id ASC'); + $expected = array( + array( + 'CategoryThread' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => null, + 'parent_id' => null, + 'name' => null, + 'created' => null, + 'updated' => null, + 'ParentCategory' => array() + )), + array( + 'CategoryThread' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array() + )), + array( + 'CategoryThread' => array( + 'id' => 3, + 'parent_id' => 2, + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array() + ))), + array( + 'CategoryThread' => array( + 'id' => 4, + 'parent_id' => 3, + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 3, + 'parent_id' => 2, + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array() + )))), + array( + 'CategoryThread' => array( + 'id' => 5, + 'parent_id' => 4, + 'name' => 'Category 1.1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 4, + 'parent_id' => 3, + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 3, + 'parent_id' => 2, + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array() + ))))), + array( + 'CategoryThread' => array( + 'id' => 6, + 'parent_id' => 5, + 'name' => 'Category 2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 5, + 'parent_id' => 4, + 'name' => 'Category 1.1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 4, + 'parent_id' => 3, + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 3, + 'parent_id' => 2, + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array() + )))))), + array( + 'CategoryThread' => array( + 'id' => 7, + 'parent_id' => 6, + 'name' => 'Category 2.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ), + 'ParentCategory' => array( + 'id' => 6, + 'parent_id' => 5, + 'name' => 'Category 2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 5, + 'parent_id' => 4, + 'name' => 'Category 1.1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 4, + 'parent_id' => 3, + 'name' => 'Category 1.1.2', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 3, + 'parent_id' => 2, + 'name' => 'Category 1.1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 2, + 'parent_id' => 1, + 'name' => 'Category 1.1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31', + 'ParentCategory' => array( + 'id' => 1, + 'parent_id' => 0, + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + )))))))); + + $this->db->fullDebug = $fullDebug; + $this->assertEqual($result, $expected); + } +/** + * testConditionalNumerics method + * + * @access public + * @return void + */ + function testConditionalNumerics() { + $this->loadFixtures('NumericArticle'); + $NumericArticle =& new NumericArticle(); + $data = array('title' => '12345abcde'); + $result = $NumericArticle->find($data); + $this->assertTrue(!empty($result)); + + $data = array('title' => '12345'); + $result = $NumericArticle->find($data); + $this->assertTrue(empty($result)); + } + +/** + * test find('all') method + * + * @access public + * @return void + */ + function testFindAll() { + $this->loadFixtures('User'); + $TestModel =& new User(); + $TestModel->cacheQueries = false; + + $result = $TestModel->find('all'); + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + )), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + )), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + )), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:22:23', + 'updated' => '2007-03-17 01:24:31' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array('conditions' => 'User.id > 2')); + $expected = array( + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + )), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:22:23', + 'updated' => '2007-03-17 01:24:31' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array( + 'conditions' => array('User.id !=' => '0', 'User.user LIKE' => '%arr%') + )); + $expected = array( + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + )), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:22:23', + 'updated' => '2007-03-17 01:24:31' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array('conditions' => array('User.id' => '0'))); + $expected = array(); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array( + 'conditions' => array('or' => array('User.id' => '0', 'User.user LIKE' => '%a%') + ))); + + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + )), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + )), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + )), + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:22:23', + 'updated' => '2007-03-17 01:24:31' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); + $expected = array( + array('User' => array('id' => '1', 'user' => 'mariano')), + array('User' => array('id' => '2', 'user' => 'nate')), + array('User' => array('id' => '3', 'user' => 'larry')), + array('User' => array('id' => '4', 'user' => 'garrett'))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user ASC')); + $expected = array( + array('User' => array('user' => 'garrett')), + array('User' => array('user' => 'larry')), + array('User' => array('user' => 'mariano')), + array('User' => array('user' => 'nate'))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user DESC')); + $expected = array( + array('User' => array('user' => 'nate')), + array('User' => array('user' => 'mariano')), + array('User' => array('user' => 'larry')), + array('User' => array('user' => 'garrett'))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array('limit' => 3, 'page' => 1)); + + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + )), + array( + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + )), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ))); + $this->assertEqual($result, $expected); + + $ids = array(4 => 1, 5 => 3); + $result = $TestModel->find('all', array( + 'conditions' => array('User.id' => $ids), + 'order' => 'User.id' + )); + $expected = array( + array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + )), + array( + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ))); + $this->assertEqual($result, $expected); + + // These tests are expected to fail on SQL Server since the LIMIT/OFFSET + // hack can't handle small record counts. + if ($this->db->config['driver'] != 'mssql') { + $result = $TestModel->find('all', array('limit' => 3, 'page' => 2)); + $expected = array( + array( + 'User' => array( + 'id' => '4', + 'user' => 'garrett', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:22:23', + 'updated' => '2007-03-17 01:24:31' + ))); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array('limit' => 3, 'page' => 3)); + $expected = array(); + $this->assertEqual($result, $expected); + } + } +/** + * test find('list') method + * + * @access public + * @return void + */ + function testGenerateFindList() { + $this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User'); + + $TestModel =& new Article(); + $TestModel->displayField = 'title'; + + $result = $TestModel->find('list', array( + 'order' => 'Article.title ASC' + )); + + $expected = array( + 1 => 'First Article', + 2 => 'Second Article', + 3 => 'Third Article' + ); + $this->assertEqual($result, $expected); + + $db =& ConnectionManager::getDataSource('test_suite'); + if ($db->config['driver'] == 'mysql') { + $result = $TestModel->find('list', array( + 'order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC') + )); + $expected = array( + 1 => 'First Article', + 3 => 'Third Article', + 2 => 'Second Article' + ); + $this->assertEqual($result, $expected); + } + + $result = Set::combine( + $TestModel->find('all', array( + 'order' => 'Article.title ASC', + 'fields' => array('id', 'title') + )), + '{n}.Article.id', '{n}.Article.title' + ); + $expected = array( + 1 => 'First Article', + 2 => 'Second Article', + 3 => 'Third Article' + ); + $this->assertEqual($result, $expected); + + $result = Set::combine( + $TestModel->find('all', array( + 'order' => 'Article.title ASC' + )), + '{n}.Article.id', '{n}.Article' + ); + $expected = array( + 1 => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 2 => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 3 => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + )); + + $this->assertEqual($result, $expected); + + $result = Set::combine( + $TestModel->find('all', array( + 'order' => 'Article.title ASC' + )), + '{n}.Article.id', '{n}.Article', '{n}.Article.user_id' + ); + $expected = array( + 1 => array( + 1 => array( + 'id' => 1, + 'user_id' => 1, + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 3 => array( + 'id' => 3, + 'user_id' => 1, + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + )), + 3 => array( + 2 => array( + 'id' => 2, + 'user_id' => 3, + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ))); + + $this->assertEqual($result, $expected); + + $result = Set::combine( + $TestModel->find('all', array( + 'order' => 'Article.title ASC', + 'fields' => array('id', 'title', 'user_id') + )), + '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id' + ); + + $expected = array( + 1 => array( + 1 => 'First Article', + 3 => 'Third Article' + ), + 3 => array( + 2 => 'Second Article' + )); + $this->assertEqual($result, $expected); + + $TestModel =& new Apple(); + $expected = array( + 1 => 'Red Apple 1', + 2 => 'Bright Red Apple', + 3 => 'green blue', + 4 => 'Test Name', + 5 => 'Blue Green', + 6 => 'My new apple', + 7 => 'Some odd color' + ); + + $this->assertEqual($TestModel->find('list'), $expected); + $this->assertEqual($TestModel->Parent->find('list'), $expected); + + $TestModel =& new Post(); + $result = $TestModel->find('list', array( + 'fields' => 'Post.title' + )); + $expected = array( + 1 => 'First Post', + 2 => 'Second Post', + 3 => 'Third Post' + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('list', array( + 'fields' => 'title' + )); + $expected = array( + 1 => 'First Post', + 2 => 'Second Post', + 3 => 'Third Post' + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('list', array( + 'fields' => array('title', 'id') + )); + $expected = array( + 'First Post' => '1', + 'Second Post' => '2', + 'Third Post' => '3' + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('list', array( + 'fields' => array('title', 'id', 'created') + )); + $expected = array( + '2007-03-18 10:39:23' => array( + 'First Post' => '1' + ), + '2007-03-18 10:41:23' => array( + 'Second Post' => '2' + ), + '2007-03-18 10:43:23' => array( + 'Third Post' => '3' + ), + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('list', array( + 'fields' => array('Post.body') + )); + $expected = array( + 1 => 'First Post Body', + 2 => 'Second Post Body', + 3 => 'Third Post Body' + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('list', array( + 'fields' => array('Post.title', 'Post.body') + )); + $expected = array( + 'First Post' => 'First Post Body', + 'Second Post' => 'Second Post Body', + 'Third Post' => 'Third Post Body' + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('list', array( + 'fields' => array('Post.id', 'Post.title', 'Author.user'), + 'recursive' => 1 + )); + $expected = array( + 'mariano' => array( + 1 => 'First Post', + 3 => 'Third Post' + ), + 'larry' => array( + 2 => 'Second Post' + )); + $this->assertEqual($result, $expected); + + $TestModel =& new User(); + $result = $TestModel->find('list', array( + 'fields' => array('User.user', 'User.password') + )); + $expected = array( + 'mariano' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'nate' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'larry' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99' + ); + $this->assertEqual($result, $expected); + + $TestModel =& new ModifiedAuthor(); + $result = $TestModel->find('list', array( + 'fields' => array('Author.id', 'Author.user') + )); + $expected = array( + 1 => 'mariano (CakePHP)', + 2 => 'nate (CakePHP)', + 3 => 'larry (CakePHP)', + 4 => 'garrett (CakePHP)' + ); + $this->assertEqual($result, $expected); + } +/** + * testFindField method + * + * @access public + * @return void + */ + function testFindField() { + $this->loadFixtures('User'); + $TestModel =& new User(); + + $TestModel->id = 1; + $result = $TestModel->field('user'); + $this->assertEqual($result, 'mariano'); + + $result = $TestModel->field('User.user'); + $this->assertEqual($result, 'mariano'); + + $TestModel->id = false; + $result = $TestModel->field('user', array( + 'user' => 'mariano' + )); + $this->assertEqual($result, 'mariano'); + + $result = $TestModel->field('COUNT(*) AS count', true); + $this->assertEqual($result, 4); + + $result = $TestModel->field('COUNT(*)', true); + $this->assertEqual($result, 4); + } +/** + * testFindUnique method + * + * @access public + * @return void + */ + function testFindUnique() { + $this->loadFixtures('User'); + $TestModel =& new User(); + + $this->assertFalse($TestModel->isUnique(array( + 'user' => 'nate' + ))); + $TestModel->id = 2; + $this->assertTrue($TestModel->isUnique(array( + 'user' => 'nate' + ))); + $this->assertFalse($TestModel->isUnique(array( + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99' + ))); + } +/** + * test find('count') method + * + * @access public + * @return void + */ + function testFindCount() { + $this->loadFixtures('User', 'Project'); + + $TestModel =& new User(); + $result = $TestModel->find('count'); + $this->assertEqual($result, 4); + + $fullDebug = $this->db->fullDebug; + $this->db->fullDebug = true; + $TestModel->order = 'User.id'; + $this->db->_queriesLog = array(); + $result = $TestModel->find('count'); + $this->assertEqual($result, 4); + + $this->assertTrue(isset($this->db->_queriesLog[0]['query'])); + $this->assertNoPattern('/ORDER\s+BY/', $this->db->_queriesLog[0]['query']); + } + +/** + * test find with COUNT(DISTINCT field) + * + * @return void + **/ + function testFindCountDistinct() { + $skip = $this->skipIf( + $this->db->config['driver'] == 'sqlite', + 'SELECT COUNT(DISTINCT field) is not compatible with SQLite' + ); + if ($skip) { + return; + } + $this->loadFixtures('Project'); + $TestModel =& new Project(); + $TestModel->create(array('name' => 'project')) && $TestModel->save(); + $TestModel->create(array('name' => 'project')) && $TestModel->save(); + $TestModel->create(array('name' => 'project')) && $TestModel->save(); + + $result = $TestModel->find('count', array('fields' => 'DISTINCT name')); + $this->assertEqual($result, 4); + } +/** + * Test find(count) with Db::expression + * + * @access public + * @return void + */ + function testFindCountWithDbExpressions() { + if ($this->skipIf($this->db->config['driver'] == 'postgres', '%s testFindCountWithExpressions is not compatible with Postgres')) { + return; + } + $this->loadFixtures('Project'); + $db = ConnectionManager::getDataSource('test_suite'); + $TestModel =& new Project(); + + $result = $TestModel->find('count', array('conditions' => array( + $db->expression('Project.name = \'Project 3\'') + ))); + $this->assertEqual($result, 1); + + $result = $TestModel->find('count', array('conditions' => array( + 'Project.name' => $db->expression('\'Project 3\'') + ))); + $this->assertEqual($result, 1); + } +/** + * testFindMagic method + * + * @access public + * @return void + */ + function testFindMagic() { + $this->loadFixtures('User'); + $TestModel =& new User(); + + $result = $TestModel->findByUser('mariano'); + $expected = array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + )); + $this->assertEqual($result, $expected); + + $result = $TestModel->findByPassword('5f4dcc3b5aa765d61d8327deb882cf99'); + $expected = array('User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + )); + $this->assertEqual($result, $expected); + } +/** + * testRead method + * + * @access public + * @return void + */ + function testRead() { + $this->loadFixtures('User', 'Article'); + $TestModel =& new User(); + + $result = $TestModel->read(); + $this->assertFalse($result); + + $TestModel->id = 2; + $result = $TestModel->read(); + $expected = array( + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + )); + $this->assertEqual($result, $expected); + + $result = $TestModel->read(null, 2); + $expected = array( + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + )); + $this->assertEqual($result, $expected); + + $TestModel->id = 2; + $result = $TestModel->read(array('id', 'user')); + $expected = array('User' => array('id' => '2', 'user' => 'nate')); + $this->assertEqual($result, $expected); + + $result = $TestModel->read('id, user', 2); + $expected = array( + 'User' => array( + 'id' => '2', + 'user' => 'nate' + )); + $this->assertEqual($result, $expected); + + $result = $TestModel->bindModel(array('hasMany' => array('Article'))); + $this->assertTrue($result); + + $TestModel->id = 1; + $result = $TestModel->read('id, user'); + $expected = array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Article' => array( + array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + array( + 'id' => '3', + 'user_id' => '1', + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ))); + $this->assertEqual($result, $expected); + } +/** + * testRecursiveRead method + * + * @access public + * @return void + */ + function testRecursiveRead() { + $this->loadFixtures( + 'User', + 'Article', + 'Comment', + 'Tag', + 'ArticlesTag', + 'Featured', + 'ArticleFeatured' + ); + $TestModel =& new User(); + + $result = $TestModel->bindModel(array('hasMany' => array('Article')), false); + $this->assertTrue($result); + + $TestModel->recursive = 0; + $result = $TestModel->read('id, user', 1); + $expected = array( + 'User' => array('id' => '1', 'user' => 'mariano'), + ); + $this->assertEqual($result, $expected); + + $TestModel->recursive = 1; + $result = $TestModel->read('id, user', 1); + $expected = array( + 'User' => array( + 'id' => '1', + 'user' => 'mariano' + ), + 'Article' => array( + array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + array( + 'id' => '3', + 'user_id' => '1', + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ))); + $this->assertEqual($result, $expected); + + $TestModel->recursive = 2; + $result = $TestModel->read('id, user', 3); + $expected = array( + 'User' => array( + 'id' => '3', + 'user' => 'larry' + ), + 'Article' => array( + array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31', + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'Comment' => array( + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + )), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))))); + $this->assertEqual($result, $expected); + } + + function testRecursiveFindAll() { + $this->db->truncate(new Featured()); + + $this->loadFixtures( + 'User', + 'Article', + 'Comment', + 'Tag', + 'ArticlesTag', + 'Attachment', + 'ArticleFeatured', + 'Featured', + 'Category' + ); + $TestModel =& new Article(); + + $result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 1))); + $expected = array( + array( + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + ), + array( + 'id' => '3', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Third Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:49:23', + 'updated' => '2007-03-18 10:51:31' + ), + array( + 'id' => '4', + 'article_id' => '1', + 'user_id' => '1', + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ) + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ))), + array( + 'Article' => array( + 'id' => '3', + 'user_id' => '1', + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Comment' => array(), + 'Tag' => array() + ) + ); + $this->assertEqual($result, $expected); + + $result = $TestModel->find('all', array( + 'conditions' => array('Article.user_id' => 3), + 'limit' => 1, + 'recursive' => 2 + )); + + $expected = array( + array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'Comment' => array( + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31', + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Attachment' => array( + 'id' => '1', + 'comment_id' => 5, + 'attachment' => 'attachment.zip', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ) + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31', + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + ), + 'Attachment' => false + ) + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + )))); + + $this->assertEqual($result, $expected); + + $Featured = new Featured(); + + $Featured->recursive = 2; + $Featured->bindModel(array( + 'belongsTo' => array( + 'ArticleFeatured' => array( + 'conditions' => "ArticleFeatured.published = 'Y'", + 'fields' => 'id, title, user_id, published' + ) + ) + )); + + $Featured->ArticleFeatured->unbindModel(array( + 'hasMany' => array('Attachment', 'Comment'), + 'hasAndBelongsToMany' => array('Tag')) + ); + + $orderBy = 'ArticleFeatured.id ASC'; + $result = $Featured->find('all', array( + 'order' => $orderBy, 'limit' => 3 + )); + + $expected = array( + array( + 'Featured' => array( + 'id' => '1', + 'article_featured_id' => '1', + 'category_id' => '1', + 'published_date' => '2007-03-31 10:39:23', + 'end_date' => '2007-05-15 10:39:23', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'ArticleFeatured' => array( + 'id' => '1', + 'title' => 'First Article', + 'user_id' => '1', + 'published' => 'Y', + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Category' => array(), + 'Featured' => array( + 'id' => '1', + 'article_featured_id' => '1', + 'category_id' => '1', + 'published_date' => '2007-03-31 10:39:23', + 'end_date' => '2007-05-15 10:39:23', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + 'Category' => array( + 'id' => '1', + 'parent_id' => '0', + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + )), + array( + 'Featured' => array( + 'id' => '2', + 'article_featured_id' => '2', + 'category_id' => '1', + 'published_date' => '2007-03-31 10:39:23', + 'end_date' => '2007-05-15 10:39:23', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'ArticleFeatured' => array( + 'id' => '2', + 'title' => 'Second Article', + 'user_id' => '3', + 'published' => 'Y', + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'Category' => array(), + 'Featured' => array( + 'id' => '2', + 'article_featured_id' => '2', + 'category_id' => '1', + 'published_date' => '2007-03-31 10:39:23', + 'end_date' => '2007-05-15 10:39:23', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + 'Category' => array( + 'id' => '1', + 'parent_id' => '0', + 'name' => 'Category 1', + 'created' => '2007-03-18 15:30:23', + 'updated' => '2007-03-18 15:32:31' + ))); + $this->assertEqual($result, $expected); + } +/** + * testRecursiveFindAllWithLimit method + * + * @access public + * @return void + */ + function testRecursiveFindAllWithLimit() { + $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment'); + $TestModel =& new Article(); + + $TestModel->hasMany['Comment']['limit'] = 2; + + $result = $TestModel->find('all', array( + 'conditions' => array('Article.user_id' => 1) + )); + $expected = array( + array( + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Comment' => array( + array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31' + ), + array( + 'id' => '2', + 'article_id' => '1', + 'user_id' => '4', + 'comment' => 'Second Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:47:23', + 'updated' => '2007-03-18 10:49:31' + ), + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ))), + array( + 'Article' => array( + 'id' => '3', + 'user_id' => '1', + 'title' => 'Third Article', + 'body' => 'Third Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Comment' => array(), + 'Tag' => array() + ) + ); + $this->assertEqual($result, $expected); + + $TestModel->hasMany['Comment']['limit'] = 1; + + $result = $TestModel->find('all', array( + 'conditions' => array('Article.user_id' => 3), + 'limit' => 1, + 'recursive' => 2 + )); + $expected = array( + array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'Comment' => array( + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31', + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Attachment' => array( + 'id' => '1', + 'comment_id' => 5, + 'attachment' => 'attachment.zip', + 'created' => '2007-03-18 10:51:23', + 'updated' => '2007-03-18 10:53:31' + ) + ) + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ) + ) + ) + ); + $this->assertEqual($result, $expected); + } +} +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model_validation.test.php b/cake/tests/cases/libs/model/model_validation.test.php new file mode 100644 index 000000000..b4dcf22fd --- /dev/null +++ b/cake/tests/cases/libs/model/model_validation.test.php @@ -0,0 +1,126 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake + * @subpackage cake.tests.cases.libs.model + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision: 8225 $ + * @modifiedby $LastChangedBy: mark_story $ + * @lastmodified $Date: 2009-07-07 23:25:30 -0400 (Tue, 07 Jul 2009) $ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +require_once dirname(__FILE__) . DS . 'model.test.php'; +require_once dirname(__FILE__) . DS . 'model_validation.test.php'; +/** + * ModelValidationTest + * + * @package cake + * @subpackage cake.tests.cases.libs.model.operations + */ +class ModelValidationTest extends BaseModelTest { +/** + * Tests validation parameter order in custom validation methods + * + * @access public + * @return void + */ + function testValidationParams() { + $TestModel =& new ValidationTest1(); + $TestModel->validate['title'] = array( + 'rule' => 'customValidatorWithParams', + 'required' => true + ); + $TestModel->create(array('title' => 'foo')); + $TestModel->invalidFields(); + + $expected = array( + 'data' => array( + 'title' => 'foo' + ), + 'validator' => array( + 'rule' => 'customValidatorWithParams', + 'on' => null, + 'last' => false, + 'allowEmpty' => false, + 'required' => true + ), + 'or' => true, + 'ignore_on_same' => 'id' + ); + $this->assertEqual($TestModel->validatorParams, $expected); + + $TestModel->validate['title'] = array( + 'rule' => 'customValidatorWithMessage', + 'required' => true + ); + $expected = array( + 'title' => 'This field will *never* validate! Muhahaha!' + ); + + $this->assertEqual($TestModel->invalidFields(), $expected); + } +/** + * Tests validation parameter fieldList in invalidFields + * + * @access public + * @return void + */ + function testInvalidFieldsWithFieldListParams() { + $TestModel =& new ValidationTest1(); + $TestModel->validate = $validate = array( + 'title' => array( + 'rule' => 'customValidator', + 'required' => true + ), + 'name' => array( + 'rule' => 'allowEmpty', + 'required' => true + )); + $TestModel->invalidFields(array('fieldList' => array('title'))); + $expected = array( + 'title' => 'This field cannot be left blank' + ); + $this->assertEqual($TestModel->validationErrors, $expected); + $TestModel->validationErrors = array(); + + $TestModel->invalidFields(array('fieldList' => array('name'))); + $expected = array( + 'name' => 'This field cannot be left blank' + ); + $this->assertEqual($TestModel->validationErrors, $expected); + $TestModel->validationErrors = array(); + + $TestModel->invalidFields(array('fieldList' => array('name', 'title'))); + $expected = array( + 'name' => 'This field cannot be left blank', + 'title' => 'This field cannot be left blank' + ); + $this->assertEqual($TestModel->validationErrors, $expected); + $TestModel->validationErrors = array(); + + $TestModel->whitelist = array('name'); + $TestModel->invalidFields(); + $expected = array('name' => 'This field cannot be left blank'); + $this->assertEqual($TestModel->validationErrors, $expected); + $TestModel->validationErrors = array(); + + $this->assertEqual($TestModel->validate, $validate); + } + +} +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model_write.test.php b/cake/tests/cases/libs/model/model_write.test.php new file mode 100644 index 000000000..c0f45af02 --- /dev/null +++ b/cake/tests/cases/libs/model/model_write.test.php @@ -0,0 +1,3894 @@ + + * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) + * + * 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. (http://www.cakefoundation.org) + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake + * @subpackage cake.tests.cases.libs.model + * @since CakePHP(tm) v 1.2.0.4206 + * @version $Revision: 8225 $ + * @modifiedby $LastChangedBy: mark_story $ + * @lastmodified $Date: 2009-07-07 23:25:30 -0400 (Tue, 07 Jul 2009) $ + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +require_once dirname(__FILE__) . DS . 'model.test.php'; +require_once dirname(__FILE__) . DS . 'model_write.test.php'; +/** + * ModelWriteTest + * + * @package cake + * @subpackage cake.tests.cases.libs.model.operations + */ +class ModelWriteTest extends BaseModelTest { +/** + * testInsertAnotherHabtmRecordWithSameForeignKey method + * + * @access public + * @return void + */ + function testInsertAnotherHabtmRecordWithSameForeignKey() { + $this->loadFixtures('JoinA', 'JoinB', 'JoinAB'); + $TestModel = new JoinA(); + + $result = $TestModel->JoinAsJoinB->findById(1); + $expected = array( + 'JoinAsJoinB' => array( + 'id' => 1, + 'join_a_id' => 1, + 'join_b_id' => 2, + 'other' => 'Data for Join A 1 Join B 2', + 'created' => '2008-01-03 10:56:33', + 'updated' => '2008-01-03 10:56:33' + )); + $this->assertEqual($result, $expected); + + $TestModel->JoinAsJoinB->create(); + $result = $TestModel->JoinAsJoinB->save(array( + 'join_a_id' => 1, + 'join_b_id' => 1, + 'other' => 'Data for Join A 1 Join B 1', + 'created' => '2008-01-03 10:56:44', + 'updated' => '2008-01-03 10:56:44' + )); + $this->assertTrue($result); + $lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID(); + $this->assertTrue($lastInsertId != null); + + $result = $TestModel->JoinAsJoinB->findById(1); + $expected = array( + 'JoinAsJoinB' => array( + 'id' => 1, + 'join_a_id' => 1, + 'join_b_id' => 2, + 'other' => 'Data for Join A 1 Join B 2', + 'created' => '2008-01-03 10:56:33', + 'updated' => '2008-01-03 10:56:33' + )); + $this->assertEqual($result, $expected); + + $updatedValue = 'UPDATED Data for Join A 1 Join B 2'; + $TestModel->JoinAsJoinB->id = 1; + $result = $TestModel->JoinAsJoinB->saveField('other', $updatedValue, false); + $this->assertTrue($result); + + $result = $TestModel->JoinAsJoinB->findById(1); + $this->assertEqual($result['JoinAsJoinB']['other'], $updatedValue); + } +/** + * testSaveDateAsFirstEntry method + * + * @access public + * @return void + */ + function testSaveDateAsFirstEntry() { + $this->loadFixtures('Article'); + + $Article =& new Article(); + + $data = array( + 'Article' => array( + 'created' => array( + 'day' => '1', + 'month' => '1', + 'year' => '2008' + ), + 'title' => 'Test Title', + 'user_id' => 1 + )); + $Article->create(); + $this->assertTrue($Article->save($data)); + + $testResult = $Article->find(array('Article.title' => 'Test Title')); + + $this->assertEqual($testResult['Article']['title'], $data['Article']['title']); + $this->assertEqual($testResult['Article']['created'], '2008-01-01 00:00:00'); + + } +/** + * testUnderscoreFieldSave method + * + * @access public + * @return void + */ + function testUnderscoreFieldSave() { + $this->loadFixtures('UnderscoreField'); + $UnderscoreField =& new UnderscoreField(); + + $currentCount = $UnderscoreField->find('count'); + $this->assertEqual($currentCount, 3); + $data = array('UnderscoreField' => array( + 'user_id' => '1', + 'my_model_has_a_field' => 'Content here', + 'body' => 'Body', + 'published' => 'Y', + 'another_field' => 4 + )); + $ret = $UnderscoreField->save($data); + $this->assertTrue($ret); + + $currentCount = $UnderscoreField->find('count'); + $this->assertEqual($currentCount, 4); + } +/** + * testAutoSaveUuid method + * + * @access public + * @return void + */ + function testAutoSaveUuid() { + // SQLite does not support non-integer primary keys, and SQL Server + // is still having problems with custom PK's + $this->skipIf( + $this->db->config['driver'] == 'sqlite' + || $this->db->config['driver'] == 'mssql' + ); + + $this->loadFixtures('Uuid'); + $TestModel =& new Uuid(); + + $TestModel->save(array('title' => 'Test record')); + $result = $TestModel->findByTitle('Test record'); + $this->assertEqual( + array_keys($result['Uuid']), + array('id', 'title', 'count', 'created', 'updated') + ); + $this->assertEqual(strlen($result['Uuid']['id']), 36); + } +/** + * testZeroDefaultFieldValue method + * + * @access public + * @return void + */ + function testZeroDefaultFieldValue() { + $this->skipIf( + $this->db->config['driver'] == 'sqlite', + '%s SQLite uses loose typing, this operation is unsupported' + ); + $this->loadFixtures('DataTest'); + $TestModel =& new DataTest(); + + $TestModel->create(array()); + $TestModel->save(); + $result = $TestModel->findById($TestModel->id); + $this->assertIdentical($result['DataTest']['count'], '0'); + $this->assertIdentical($result['DataTest']['float'], '0'); + } +/** + * testNonNumericHabtmJoinKey method + * + * @access public + * @return void + */ + function testNonNumericHabtmJoinKey() { + $this->loadFixtures('Post', 'Tag', 'PostsTag'); + $Post =& new Post(); + $Post->bind('Tag', array('type' => 'hasAndBelongsToMany')); + $Post->Tag->primaryKey = 'tag'; + + $result = $Post->find('all'); + $expected = array( + array( + 'Post' => array( + 'id' => '1', + 'author_id' => '1', + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + ), + 'Author' => array( + 'id' => null, + 'user' => null, + 'password' => null, + 'created' => null, + 'updated' => null, + 'test' => 'working' + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ))), + array( + 'Post' => array( + 'id' => '2', + 'author_id' => '3', + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'Author' => array( + 'id' => null, + 'user' => null, + 'password' => null, + 'created' => null, + 'updated' => null, + 'test' => 'working' + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))), + array( + 'Post' => array( + 'id' => '3', + 'author_id' => '1', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ), + 'Author' => array( + 'id' => null, + 'user' => null, + 'password' => null, + 'created' => null, + 'updated' => null, + 'test' => 'working' + ), + 'Tag' => array() + )); + $this->assertEqual($result, $expected); + } +/** + * Tests validation parameter order in custom validation methods + * + * @access public + * @return void + */ + function testAllowSimulatedFields() { + $TestModel =& new ValidationTest1(); + + $TestModel->create(array( + 'title' => 'foo', + 'bar' => 'baz' + )); + $expected = array( + 'ValidationTest1' => array( + 'title' => 'foo', + 'bar' => 'baz' + )); + $this->assertEqual($TestModel->data, $expected); + } +/** + * test that Caches are getting cleared on save(). + * ensure that both inflections of controller names are getting cleared + * as url for controller could be either overallFavorites/index or overall_favorites/index + * + * @return void + **/ + function testCacheClearOnSave() { + $_back = array( + 'check' => Configure::read('Cache.check'), + 'disable' => Configure::read('Cache.disable'), + ); + Configure::write('Cache.check', true); + Configure::write('Cache.disable', false); + + $this->loadFixtures('OverallFavorite'); + $OverallFavorite =& new OverallFavorite(); + + touch(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php'); + touch(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php'); + + $data = array( + 'OverallFavorite' => array( + 'model_type' => '8-track', + 'model_id' => '3', + 'priority' => '1' + ) + ); + $OverallFavorite->create($data); + $OverallFavorite->save(); + + $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overallfavorites_index.php')); + $this->assertFalse(file_exists(CACHE . 'views' . DS . 'some_dir_overall_favorites_index.php')); + + Configure::write('Cache.check', $_back['check']); + Configure::write('Cache.disable', $_back['disable']); + } +/** + * testSaveWithCounterCache method + * + * @access public + * @return void + */ + function testSaveWithCounterCache() { + $this->loadFixtures('Syfile', 'Item'); + $TestModel =& new Syfile(); + $TestModel2 =& new Item(); + + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], null); + + $TestModel2->save(array( + 'name' => 'Item 7', + 'syfile_id' => 1, + 'published' => false + )); + + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '2'); + + $TestModel2->delete(1); + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '1'); + + $TestModel2->id = 2; + $TestModel2->saveField('syfile_id', 1); + + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '2'); + + $result = $TestModel->findById(2); + $this->assertIdentical($result['Syfile']['item_count'], '0'); + } +/** + * Tests that counter caches are updated when records are added + * + * @access public + * @return void + */ + function testCounterCacheIncrease() { + $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); + $User = new CounterCacheUser(); + $Post = new CounterCachePost(); + $data = array('Post' => array( + 'title' => 'New Post', + 'user_id' => 66 + )); + + $Post->save($data); + $user = $User->find('first', array( + 'conditions' => array('id' => 66), + 'recursive' => -1 + )); + + $result = $user[$User->alias]['post_count']; + $expected = 3; + $this->assertEqual($result, $expected); + } +/** + * Tests that counter caches are updated when records are deleted + * + * @access public + * @return void + */ + function testCounterCacheDecrease() { + $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); + $User = new CounterCacheUser(); + $Post = new CounterCachePost(); + + $Post->del(2); + $user = $User->find('first', array( + 'conditions' => array('id' => 66), + 'recursive' => -1 + )); + + $result = $user[$User->alias]['post_count']; + $expected = 1; + $this->assertEqual($result, $expected); + } +/** + * Tests that counter caches are updated when foreign keys of counted records change + * + * @access public + * @return void + */ + function testCounterCacheUpdated() { + $this->loadFixtures('CounterCacheUser', 'CounterCachePost'); + $User = new CounterCacheUser(); + $Post = new CounterCachePost(); + + $data = $Post->find('first', array( + 'conditions' => array('id' => 1), + 'recursive' => -1 + )); + $data[$Post->alias]['user_id'] = 301; + $Post->save($data); + + $users = $User->find('all',array('order' => 'User.id')); + $this->assertEqual($users[0]['User']['post_count'], 1); + $this->assertEqual($users[1]['User']['post_count'], 2); + } +/** + * Test counter cache with models that use a non-standard (i.e. not using 'id') + * as their primary key. + * + * @access public + * @return void + */ + function testCounterCacheWithNonstandardPrimaryKey() { + $this->loadFixtures( + 'CounterCacheUserNonstandardPrimaryKey', + 'CounterCachePostNonstandardPrimaryKey' + ); + + $User = new CounterCacheUserNonstandardPrimaryKey(); + $Post = new CounterCachePostNonstandardPrimaryKey(); + + $data = $Post->find('first', array( + 'conditions' => array('pid' => 1), + 'recursive' => -1 + )); + $data[$Post->alias]['uid'] = 301; + $Post->save($data); + + $users = $User->find('all',array('order' => 'User.uid')); + $this->assertEqual($users[0]['User']['post_count'], 1); + $this->assertEqual($users[1]['User']['post_count'], 2); + } + +/** + * test Counter Cache With Self Joining table + * + * @return void + * @access public + */ + function testCounterCacheWithSelfJoin() { + $skip = $this->skipIf( + ($this->db->config['driver'] == 'sqlite'), + 'SQLite 2.x does not support ALTER TABLE ADD COLUMN' + ); + if ($skip) { + return; + } + + $this->loadFixtures('CategoryThread'); + $this->db->query('ALTER TABLE '. $this->db->fullTableName('category_threads') . " ADD COLUMN child_count INTEGER"); + $Category =& new CategoryThread(); + $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5)); + $this->assertTrue($result); + + $Category =& new CategoryThread(); + $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count'; + $Category->updateCounterCache(array('parent_id' => 5)); + $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count'); + $expected = array_fill(0, 1, 1); + $this->assertEqual($result, $expected); + } +/** + * testSaveWithCounterCacheScope method + * + * @access public + * @return void + */ + function testSaveWithCounterCacheScope() { + $this->loadFixtures('Syfile', 'Item'); + $TestModel =& new Syfile(); + $TestModel2 =& new Item(); + $TestModel2->belongsTo['Syfile']['counterCache'] = true; + $TestModel2->belongsTo['Syfile']['counterScope'] = array('published' => true); + + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], null); + + $TestModel2->save(array( + 'name' => 'Item 7', + 'syfile_id' => 1, + 'published'=> true + )); + + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '1'); + + $TestModel2->id = 1; + $TestModel2->saveField('published', true); + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '2'); + + $TestModel2->save(array( + 'id' => 1, + 'syfile_id' => 1, + 'published'=> false + )); + + $result = $TestModel->findById(1); + $this->assertIdentical($result['Syfile']['item_count'], '1'); + } +/** + * testValidatesBackwards method + * + * @access public + * @return void + */ + function testValidatesBackwards() { + $TestModel =& new TestValidate(); + + $TestModel->validate = array( + 'user_id' => VALID_NUMBER, + 'title' => VALID_NOT_EMPTY, + 'body' => VALID_NOT_EMPTY + ); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => '', + 'body' => '' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 'title', + 'body' => '' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => '', + 'title' => 'title', + 'body' => 'body' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => 'not a number', + 'title' => 'title', + 'body' => 'body' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 'title', + 'body' => 'body' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + } +/** + * testValidates method + * + * @access public + * @return void + */ + function testValidates() { + $TestModel =& new TestValidate(); + + $TestModel->validate = array( + 'user_id' => 'numeric', + 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'), + 'body' => 'notEmpty' + ); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => '', + 'body' => 'body' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 'title', + 'body' => 'body' + )); + $result = $TestModel->create($data) && $TestModel->validates(); + $this->assertTrue($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => '0', + 'body' => 'body' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $TestModel->validate['modified'] = array('allowEmpty' => true, 'rule' => 'date'); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'modified' => '' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'modified' => '2007-05-01' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'modified' => 'invalid-date-here' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'modified' => 0 + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'modified' => '0' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $TestModel->validate['modified'] = array('allowEmpty' => false, 'rule' => 'date'); + + $data = array('TestValidate' => array('modified' => null)); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array('modified' => false)); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array('modified' => '')); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'modified' => '2007-05-01' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $TestModel->validate['slug'] = array('allowEmpty' => false, 'rule' => array('maxLength', 45)); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'slug' => '' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'slug' => 'slug-right-here' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $data = array('TestValidate' => array( + 'user_id' => '1', + 'title' => 0, + 'body' => 'body', + 'slug' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $TestModel->validate = array( + 'number' => array( + 'rule' => 'validateNumber', + 'min' => 3, + 'max' => 5 + ), + 'title' => array( + 'allowEmpty' => false, + 'rule' => 'notEmpty' + )); + + $data = array('TestValidate' => array( + 'title' => 'title', + 'number' => '0' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'title' => 'title', + 'number' => 0 + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'title' => 'title', + 'number' => '3' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $data = array('TestValidate' => array( + 'title' => 'title', + 'number' => 3 + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $TestModel->validate = array( + 'number' => array( + 'rule' => 'validateNumber', + 'min' => 5, + 'max' => 10 + ), + 'title' => array( + 'allowEmpty' => false, + 'rule' => 'notEmpty' + )); + + $data = array('TestValidate' => array( + 'title' => 'title', + 'number' => '3' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'title' => 'title', + 'number' => 3 + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $TestModel->validate = array( + 'title' => array( + 'allowEmpty' => false, + 'rule' => 'validateTitle' + )); + + $data = array('TestValidate' => array('title' => '')); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array('title' => 'new title')); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array('title' => 'title-new')); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $TestModel->validate = array('title' => array( + 'allowEmpty' => true, + 'rule' => 'validateTitle' + )); + $data = array('TestValidate' => array('title' => '')); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $TestModel->validate = array( + 'title' => array( + 'length' => array( + 'allowEmpty' => true, + 'rule' => array('maxLength', 10) + ))); + $data = array('TestValidate' => array('title' => '')); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $TestModel->validate = array( + 'title' => array( + 'rule' => array('userDefined', 'Article', 'titleDuplicate') + )); + $data = array('TestValidate' => array('title' => 'My Article Title')); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertFalse($result); + + $data = array('TestValidate' => array( + 'title' => 'My Article With a Different Title' + )); + $result = $TestModel->create($data); + $this->assertTrue($result); + $result = $TestModel->validates(); + $this->assertTrue($result); + + $TestModel->validate = array( + 'title' => array( + 'tooShort' => array('rule' => array('minLength', 50)), + 'onlyLetters' => array('rule' => '/^[a-z]+$/i') + ), + ); + $data = array('TestValidate' => array( + 'title' => 'I am a short string' + )); + $TestModel->create($data); + $result = $TestModel->validates(); + $this->assertFalse($result); + $result = $TestModel->validationErrors; + $expected = array( + 'title' => 'onlyLetters' + ); + $this->assertEqual($result, $expected); + + $TestModel->validate = array( + 'title' => array( + 'tooShort' => array( + 'rule' => array('minLength', 50), + 'last' => true + ), + 'onlyLetters' => array('rule' => '/^[a-z]+$/i') + ), + ); + $data = array('TestValidate' => array( + 'title' => 'I am a short string' + )); + $TestModel->create($data); + $result = $TestModel->validates(); + $this->assertFalse($result); + $result = $TestModel->validationErrors; + $expected = array( + 'title' => 'tooShort' + ); + $this->assertEqual($result, $expected); + } +/** + * testSaveField method + * + * @access public + * @return void + */ + function testSaveField() { + $this->loadFixtures('Article'); + $TestModel =& new Article(); + + $TestModel->id = 1; + $result = $TestModel->saveField('title', 'New First Article'); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); + $expected = array('Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'New First Article', + 'body' => 'First Article Body' + )); + $this->assertEqual($result, $expected); + + $TestModel->id = 1; + $result = $TestModel->saveField('title', ''); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); + $expected = array('Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => '', + 'body' => 'First Article Body' + )); + $result['Article']['title'] = trim($result['Article']['title']); + $this->assertEqual($result, $expected); + + $TestModel->id = 1; + $TestModel->set('body', 'Messed up data'); + $this->assertTrue($TestModel->saveField('title', 'First Article')); + $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); + $expected = array('Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body' + )); + $this->assertEqual($result, $expected); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body'), 1); + + $TestModel->id = 1; + $result = $TestModel->saveField('title', '', true); + $this->assertFalse($result); + + $this->loadFixtures('Node', 'Dependency'); + $Node =& new Node(); + $Node->set('id', 1); + $result = $Node->read(); + $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); + + $Node->saveField('state', 10); + $result = $Node->read(); + $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); + } +/** + * testSaveWithCreate method + * + * @access public + * @return void + */ + function testSaveWithCreate() { + $this->loadFixtures( + 'User', + 'Article', + 'User', + 'Comment', + 'Tag', + 'ArticlesTag', + 'Attachment' + ); + $TestModel =& new User(); + + $data = array('User' => array( + 'user' => 'user', + 'password' => '' + )); + $result = $TestModel->save($data); + $this->assertFalse($result); + $this->assertTrue(!empty($TestModel->validationErrors)); + + $TestModel =& new Article(); + + $data = array('Article' => array( + 'user_id' => '', + 'title' => '', + 'body' => '' + )); + $result = $TestModel->create($data) && $TestModel->save(); + $this->assertFalse($result); + $this->assertTrue(!empty($TestModel->validationErrors)); + + $data = array('Article' => array( + 'id' => 1, + 'user_id' => '1', + 'title' => 'New First Article', + 'body' => '' + )); + $result = $TestModel->create($data) && $TestModel->save(); + $this->assertFalse($result); + + $data = array('Article' => array( + 'id' => 1, + 'title' => 'New First Article' + )); + $result = $TestModel->create() && $TestModel->save($data, false); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1); + $expected = array('Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'New First Article', + 'body' => 'First Article Body', + 'published' => 'N' + )); + $this->assertEqual($result, $expected); + + $data = array('Article' => array( + 'id' => 1, + 'user_id' => '2', + 'title' => 'First Article', + 'body' => 'New First Article Body', + 'published' => 'Y' + )); + $result = $TestModel->create() && $TestModel->save($data, true, array('id', 'title', 'published')); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 1); + $expected = array('Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y' + )); + $this->assertEqual($result, $expected); + + $data = array( + 'Article' => array( + 'user_id' => '2', + 'title' => 'New Article', + 'body' => 'New Article Body', + 'created' => '2007-03-18 14:55:23', + 'updated' => '2007-03-18 14:57:31' + ), + 'Tag' => array('Tag' => array(1, 3)) + ); + $TestModel->create(); + $result = $TestModel->create() && $TestModel->save($data); + $this->assertTrue($result); + + $TestModel->recursive = 2; + $result = $TestModel->read(null, 4); + $expected = array( + 'Article' => array( + 'id' => '4', + 'user_id' => '2', + 'title' => 'New Article', + 'body' => 'New Article Body', + 'published' => 'N', + 'created' => '2007-03-18 14:55:23', + 'updated' => '2007-03-18 14:57:31' + ), + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + ), + 'Comment' => array(), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + $this->assertEqual($result, $expected); + + $data = array('Comment' => array( + 'article_id' => '4', + 'user_id' => '1', + 'comment' => 'Comment New Article', + 'published' => 'Y', + 'created' => '2007-03-18 14:57:23', + 'updated' => '2007-03-18 14:59:31' + )); + $result = $TestModel->Comment->create() && $TestModel->Comment->save($data); + $this->assertTrue($result); + + $data = array('Attachment' => array( + 'comment_id' => '7', + 'attachment' => 'newattachment.zip', + 'created' => '2007-03-18 15:02:23', + 'updated' => '2007-03-18 15:04:31' + )); + $result = $TestModel->Comment->Attachment->save($data); + $this->assertTrue($result); + + $TestModel->recursive = 2; + $result = $TestModel->read(null, 4); + $expected = array( + 'Article' => array( + 'id' => '4', + 'user_id' => '2', + 'title' => 'New Article', + 'body' => 'New Article Body', + 'published' => 'N', + 'created' => '2007-03-18 14:55:23', + 'updated' => '2007-03-18 14:57:31' + ), + 'User' => array( + 'id' => '2', + 'user' => 'nate', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:18:23', + 'updated' => '2007-03-17 01:20:31' + ), + 'Comment' => array( + array( + 'id' => '7', + 'article_id' => '4', + 'user_id' => '1', + 'comment' => 'Comment New Article', + 'published' => 'Y', + 'created' => '2007-03-18 14:57:23', + 'updated' => '2007-03-18 14:59:31', + 'Article' => array( + 'id' => '4', + 'user_id' => '2', + 'title' => 'New Article', + 'body' => 'New Article Body', + 'published' => 'N', + 'created' => '2007-03-18 14:55:23', + 'updated' => '2007-03-18 14:57:31' + ), + 'User' => array( + 'id' => '1', + 'user' => 'mariano', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:16:23', + 'updated' => '2007-03-17 01:18:31' + ), + 'Attachment' => array( + 'id' => '2', + 'comment_id' => '7', + 'attachment' => 'newattachment.zip', + 'created' => '2007-03-18 15:02:23', + 'updated' => '2007-03-18 15:04:31' + ))), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + + $this->assertEqual($result, $expected); + } +/** + * testSaveWithSet method + * + * @access public + * @return void + */ + function testSaveWithSet() { + $this->loadFixtures('Article'); + $TestModel =& new Article(); + + // Create record we will be updating later + + $data = array('Article' => array( + 'user_id' => '1', + 'title' => 'Fourth Article', + 'body' => 'Fourth Article Body', + 'published' => 'Y' + )); + $result = $TestModel->create() && $TestModel->save($data); + $this->assertTrue($result); + + // Check record we created + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); + $expected = array('Article' => array( + 'id' => '4', + 'user_id' => '1', + 'title' => 'Fourth Article', + 'body' => 'Fourth Article Body', + 'published' => 'Y' + )); + $this->assertEqual($result, $expected); + + // Create new record just to overlap Model->id on previously created record + + $data = array('Article' => array( + 'user_id' => '4', + 'title' => 'Fifth Article', + 'body' => 'Fifth Article Body', + 'published' => 'Y' + )); + $result = $TestModel->create() && $TestModel->save($data); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); + $expected = array('Article' => array( + 'id' => '5', + 'user_id' => '4', + 'title' => 'Fifth Article', + 'body' => 'Fifth Article Body', + 'published' => 'Y' + )); + $this->assertEqual($result, $expected); + + // Go back and edit the first article we created, starting by checking it's still there + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); + $expected = array('Article' => array( + 'id' => '4', + 'user_id' => '1', + 'title' => 'Fourth Article', + 'body' => 'Fourth Article Body', + 'published' => 'Y' + )); + $this->assertEqual($result, $expected); + + // And now do the update with set() + + $data = array('Article' => array( + 'id' => '4', + 'title' => 'Fourth Article - New Title', + 'published' => 'N' + )); + $result = $TestModel->set($data) && $TestModel->save(); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); + $expected = array('Article' => array( + 'id' => '4', + 'user_id' => '1', + 'title' => 'Fourth Article - New Title', + 'body' => 'Fourth Article Body', + 'published' => 'N' + )); + $this->assertEqual($result, $expected); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); + $expected = array('Article' => array( + 'id' => '5', + 'user_id' => '4', + 'title' => 'Fifth Article', + 'body' => 'Fifth Article Body', + 'published' => 'Y' + )); + $this->assertEqual($result, $expected); + + $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5')); + $result = ($TestModel->set($data) && $TestModel->save()); + $this->assertTrue($result); + + $TestModel->recursive = -1; + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); + $expected = array('Article' => array( + 'id' => '5', + 'user_id' => '4', + 'title' => 'Fifth Article - New Title 5', + 'body' => 'Fifth Article Body', + 'published' => 'Y' + )); + $this->assertEqual($result, $expected); + + $TestModel->recursive = -1; + $result = $TestModel->find('all', array('fields' => array('id', 'title'))); + $expected = array( + array('Article' => array('id' => 1, 'title' => 'First Article' )), + array('Article' => array('id' => 2, 'title' => 'Second Article' )), + array('Article' => array('id' => 3, 'title' => 'Third Article' )), + array('Article' => array('id' => 4, 'title' => 'Fourth Article - New Title' )), + array('Article' => array('id' => 5, 'title' => 'Fifth Article - New Title 5' )) + ); + $this->assertEqual($result, $expected); + } +/** + * testSaveWithNonExistentFields method + * + * @access public + * @return void + */ + function testSaveWithNonExistentFields() { + $this->loadFixtures('Article'); + $TestModel =& new Article(); + $TestModel->recursive = -1; + + $data = array( + 'non_existent' => 'This field does not exist', + 'user_id' => '1', + 'title' => 'Fourth Article - New Title', + 'body' => 'Fourth Article Body', + 'published' => 'N' + ); + $result = $TestModel->create() && $TestModel->save($data); + $this->assertTrue($result); + + $expected = array('Article' => array( + 'id' => '4', + 'user_id' => '1', + 'title' => 'Fourth Article - New Title', + 'body' => 'Fourth Article Body', + 'published' => 'N' + )); + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 4); + $this->assertEqual($result, $expected); + + $data = array( + 'user_id' => '1', + 'non_existent' => 'This field does not exist', + 'title' => 'Fiveth Article - New Title', + 'body' => 'Fiveth Article Body', + 'published' => 'N' + ); + $result = $TestModel->create() && $TestModel->save($data); + $this->assertTrue($result); + + $expected = array('Article' => array( + 'id' => '5', + 'user_id' => '1', + 'title' => 'Fiveth Article - New Title', + 'body' => 'Fiveth Article Body', + 'published' => 'N' + )); + $result = $TestModel->read(array('id', 'user_id', 'title', 'body', 'published'), 5); + $this->assertEqual($result, $expected); + } +/** + * testSaveFromXml method + * + * @access public + * @return void + */ + function testSaveFromXml() { + $this->loadFixtures('Article'); + App::import('Core', 'Xml'); + + $Article = new Article(); + $Article->save(new Xml('
')); + $this->assertTrue($Article->save(new Xml('
'))); + + $results = $Article->find(array('Article.title' => 'test xml')); + $this->assertTrue($results); + } +/** + * testSaveHabtm method + * + * @access public + * @return void + */ + function testSaveHabtm() { + $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag'); + $TestModel =& new Article(); + + $result = $TestModel->findById(2); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Second Article', + 'body' => 'Second Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + ), + 'User' => array( + 'id' => '3', + 'user' => 'larry', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', + 'created' => '2007-03-17 01:20:23', + 'updated' => '2007-03-17 01:22:31' + ), + 'Comment' => array( + array( + 'id' => '5', + 'article_id' => '2', + 'user_id' => '1', + 'comment' => 'First Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:53:23', + 'updated' => '2007-03-18 10:55:31' + ), + array( + 'id' => '6', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:55:23', + 'updated' => '2007-03-18 10:57:31' + )), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ) + ) + ); + $this->assertEqual($result, $expected); + + $data = array( + 'Article' => array( + 'id' => '2', + 'title' => 'New Second Article' + ), + 'Tag' => array('Tag' => array(1, 2)) + ); + + $this->assertTrue($TestModel->set($data)); + $this->assertTrue($TestModel->save()); + + $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); + $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'New Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ))); + $this->assertEqual($result, $expected); + + $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3))); + $result = $TestModel->set($data); + $this->assertTrue($result); + + $result = $TestModel->save(); + $this->assertTrue($result); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'New Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + $this->assertEqual($result, $expected); + + $data = array('Tag' => array('Tag' => array(1, 2, 3))); + + $result = $TestModel->set($data); + $this->assertTrue($result); + + $result = $TestModel->save(); + $this->assertTrue($result); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id' => 2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'New Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + $this->assertEqual($result, $expected); + + $data = array('Tag' => array('Tag' => array())); + $result = $TestModel->set($data); + $this->assertTrue($result); + + $result = $TestModel->save(); + $this->assertTrue($result); + + $data = array('Tag' => array('Tag' => '')); + $result = $TestModel->set($data); + $this->assertTrue($result); + + $result = $TestModel->save(); + $this->assertTrue($result); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'New Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array() + ); + $this->assertEqual($result, $expected); + + $data = array('Tag' => array('Tag' => array(2, 3))); + $result = $TestModel->set($data); + $this->assertTrue($result); + + $result = $TestModel->save(); + $this->assertTrue($result); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'New Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + $this->assertEqual($result, $expected); + + $data = array( + 'Tag' => array( + 'Tag' => array(1, 2) + ), + 'Article' => array( + 'id' => '2', + 'title' => 'New Second Article' + )); + $this->assertTrue($TestModel->set($data)); + $this->assertTrue($TestModel->save()); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'New Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ))); + $this->assertEqual($result, $expected); + + $data = array( + 'Tag' => array( + 'Tag' => array(1, 2) + ), + 'Article' => array( + 'id' => '2', + 'title' => 'New Second Article Title' + )); + $result = $TestModel->set($data); + $this->assertTrue($result); + $this->assertTrue($TestModel->save()); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'New Second Article Title', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ) + ) + ); + $this->assertEqual($result, $expected); + + $data = array( + 'Tag' => array( + 'Tag' => array(2, 3) + ), + 'Article' => array( + 'id' => '2', + 'title' => 'Changed Second Article' + )); + $this->assertTrue($TestModel->set($data)); + $this->assertTrue($TestModel->save()); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Changed Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '2', + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ) + ) + ); + $this->assertEqual($result, $expected); + + $data = array( + 'Tag' => array( + 'Tag' => array(1, 3) + ), + 'Article' => array('id' => '2'), + ); + + $result = $TestModel->set($data); + $this->assertTrue($result); + + $result = $TestModel->save(); + $this->assertTrue($result); + + $TestModel->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment') + )); + $result = $TestModel->find(array('Article.id'=>2), array('id', 'user_id', 'title', 'body')); + $expected = array( + 'Article' => array( + 'id' => '2', + 'user_id' => '3', + 'title' => 'Changed Second Article', + 'body' => 'Second Article Body' + ), + 'Tag' => array( + array( + 'id' => '1', + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + array( + 'id' => '3', + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + $this->assertEqual($result, $expected); + + $data = array( + 'Article' => array( + 'id' => 10, + 'user_id' => '2', + 'title' => 'New Article With Tags and fieldList', + 'body' => 'New Article Body with Tags and fieldList', + 'created' => '2007-03-18 14:55:23', + 'updated' => '2007-03-18 14:57:31' + ), + 'Tag' => array( + 'Tag' => array(1, 2, 3) + )); + $result = $TestModel->create() + && $TestModel->save($data, true, array('user_id', 'title', 'published')); + $this->assertTrue($result); + + $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); + $result = $TestModel->read(); + $expected = array( + 'Article' => array( + 'id' => 4, + 'user_id' => 2, + 'title' => 'New Article With Tags and fieldList', + 'body' => '', + 'published' => 'N', + 'created' => '', + 'updated' => '' + ), + 'Tag' => array( + 0 => array( + 'id' => 1, + 'tag' => 'tag1', + 'created' => '2007-03-18 12:22:23', + 'updated' => '2007-03-18 12:24:31' + ), + 1 => array( + 'id' => 2, + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ), + 2 => array( + 'id' => 3, + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))); + $this->assertEqual($result, $expected); + + + $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB'); + $TestModel = new JoinA(); + $TestModel->hasBelongsToMany['JoinC']['unique'] = true; + $data = array( + 'JoinA' => array( + 'id' => 1, + 'name' => 'Join A 1', + 'body' => 'Join A 1 Body', + ), + 'JoinC' => array( + 'JoinC' => array( + array('join_c_id' => 2, 'other' => 'new record'), + array('join_c_id' => 3, 'other' => 'new record') + ) + ) + ); + $TestModel->save($data); + $result = $TestModel->read(null, 1); + $time = date('Y-M-D H:i:s'); + $expected = array(4, 5); + $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/id', $result), $expected); + $expected = array('new record', 'new record'); + $this->assertEqual(Set::extract('/JoinC/JoinAsJoinC/other', $result), $expected); + } +/** + * testSaveHabtmCustomKeys method + * + * @access public + * @return void + */ + function testSaveHabtmCustomKeys() { + $this->loadFixtures('Story', 'StoriesTag', 'Tag'); + $Story =& new Story(); + + $data = array( + 'Story' => array('story' => '1'), + 'Tag' => array( + 'Tag' => array(2, 3) + )); + $result = $Story->set($data); + $this->assertTrue($result); + + $result = $Story->save(); + $this->assertTrue($result); + + $result = $Story->find('all'); + $expected = array( + array( + 'Story' => array( + 'story' => 1, + 'title' => 'First Story' + ), + 'Tag' => array( + array( + 'id' => 2, + 'tag' => 'tag2', + 'created' => '2007-03-18 12:24:23', + 'updated' => '2007-03-18 12:26:31' + ), + array( + 'id' => 3, + 'tag' => 'tag3', + 'created' => '2007-03-18 12:26:23', + 'updated' => '2007-03-18 12:28:31' + ))), + array( + 'Story' => array( + 'story' => 2, + 'title' => 'Second Story' + ), + 'Tag' => array() + )); + $this->assertEqual($result, $expected); + } +/** + * testHabtmSaveKeyResolution method + * + * @access public + * @return void + */ + function testHabtmSaveKeyResolution() { + $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); + $ThePaper =& new ThePaper(); + + $ThePaper->id = 1; + $ThePaper->save(array('Monkey' => array(2, 3))); + + $result = $ThePaper->findById(1); + $expected = array( + array( + 'id' => '2', + 'device_type_id' => '1', + 'name' => 'Device 2', + 'typ' => '1' + ), + array( + 'id' => '3', + 'device_type_id' => '1', + 'name' => 'Device 3', + 'typ' => '2' + )); + $this->assertEqual($result['Monkey'], $expected); + + $ThePaper->id = 2; + $ThePaper->save(array('Monkey' => array(1, 2, 3))); + + $result = $ThePaper->findById(2); + $expected = array( + array( + 'id' => '1', + 'device_type_id' => '1', + 'name' => 'Device 1', + 'typ' => '1' + ), + array( + 'id' => '2', + 'device_type_id' => '1', + 'name' => 'Device 2', + 'typ' => '1' + ), + array( + 'id' => '3', + 'device_type_id' => '1', + 'name' => 'Device 3', + 'typ' => '2' + )); + $this->assertEqual($result['Monkey'], $expected); + + $ThePaper->id = 2; + $ThePaper->save(array('Monkey' => array(1, 3))); + + $result = $ThePaper->findById(2); + $expected = array( + array( + 'id' => '1', + 'device_type_id' => '1', + 'name' => 'Device 1', + 'typ' => '1' + ), + array( + 'id' => '3', + 'device_type_id' => '1', + 'name' => 'Device 3', + 'typ' => '2' + )); + $this->assertEqual($result['Monkey'], $expected); + + $result = $ThePaper->findById(1); + $expected = array( + array( + 'id' => '2', + 'device_type_id' => '1', + 'name' => 'Device 2', + 'typ' => '1' + ), + array( + 'id' => '3', + 'device_type_id' => '1', + 'name' => 'Device 3', + 'typ' => '2' + )); + $this->assertEqual($result['Monkey'], $expected); + } +/** + * testCreationOfEmptyRecord method + * + * @access public + * @return void + */ + function testCreationOfEmptyRecord() { + $this->loadFixtures('Author'); + $TestModel =& new Author(); + $this->assertEqual($TestModel->find('count'), 4); + + $TestModel->deleteAll(true, false, false); + $this->assertEqual($TestModel->find('count'), 0); + + $result = $TestModel->save(); + $this->assertTrue(isset($result['Author']['created'])); + $this->assertTrue(isset($result['Author']['updated'])); + $this->assertEqual($TestModel->find('count'), 1); + } +/** + * testCreateWithPKFiltering method + * + * @access public + * @return void + */ + function testCreateWithPKFiltering() { + $TestModel =& new Article(); + $data = array( + 'id' => 5, + 'user_id' => 2, + 'title' => 'My article', + 'body' => 'Some text' + ); + + $result = $TestModel->create($data); + $expected = array( + 'Article' => array( + 'published' => 'N', + 'id' => 5, + 'user_id' => 2, + 'title' => 'My article', + 'body' => 'Some text' + )); + + $this->assertEqual($result, $expected); + $this->assertEqual($TestModel->id, 5); + + $result = $TestModel->create($data, true); + $expected = array( + 'Article' => array( + 'published' => 'N', + 'id' => false, + 'user_id' => 2, + 'title' => 'My article', + 'body' => 'Some text' + )); + + $this->assertEqual($result, $expected); + $this->assertFalse($TestModel->id); + + $result = $TestModel->create(array('Article' => $data), true); + $expected = array( + 'Article' => array( + 'published' => 'N', + 'id' => false, + 'user_id' => 2, + 'title' => 'My article', + 'body' => 'Some text' + )); + + $this->assertEqual($result, $expected); + $this->assertFalse($TestModel->id); + + $data = array( + 'id' => 6, + 'user_id' => 2, + 'title' => 'My article', + 'body' => 'Some text', + 'created' => '1970-01-01 00:00:00', + 'updated' => '1970-01-01 12:00:00', + 'modified' => '1970-01-01 12:00:00' + ); + + $result = $TestModel->create($data); + $expected = array( + 'Article' => array( + 'published' => 'N', + 'id' => 6, + 'user_id' => 2, + 'title' => 'My article', + 'body' => 'Some text', + 'created' => '1970-01-01 00:00:00', + 'updated' => '1970-01-01 12:00:00', + 'modified' => '1970-01-01 12:00:00' + )); + $this->assertEqual($result, $expected); + $this->assertEqual($TestModel->id, 6); + + $result = $TestModel->create(array( + 'Article' => array_diff_key($data, array( + 'created' => true, + 'updated' => true, + 'modified' => true + ))), true); + $expected = array( + 'Article' => array( + 'published' => 'N', + 'id' => false, + 'user_id' => 2, + 'title' => 'My article', + 'body' => 'Some text' + )); + $this->assertEqual($result, $expected); + $this->assertFalse($TestModel->id); + } +/** + * testCreationWithMultipleData method + * + * @access public + * @return void + */ + function testCreationWithMultipleData() { + $this->loadFixtures('Article', 'Comment'); + $Article =& new Article(); + $Comment =& new Comment(); + + $articles = $Article->find('all', array( + 'fields' => array('id','title'), + 'recursive' => -1 + )); + + $comments = $Comment->find('all', array( + 'fields' => array('id','article_id','user_id','comment','published'), 'recursive' => -1)); + + $this->assertEqual($articles, array( + array('Article' => array( + 'id' => 1, + 'title' => 'First Article' + )), + array('Article' => array( + 'id' => 2, + 'title' => 'Second Article' + )), + array('Article' => array( + 'id' => 3, + 'title' => 'Third Article' + )))); + + $this->assertEqual($comments, array( + array('Comment' => array( + 'id' => 1, + 'article_id' => 1, + 'user_id' => 2, + 'comment' => 'First Comment for First Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 2, + 'article_id' => 1, + 'user_id' => 4, + 'comment' => 'Second Comment for First Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 3, + 'article_id' => 1, + 'user_id' => 1, + 'comment' => 'Third Comment for First Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 4, + 'article_id' => 1, + 'user_id' => 1, + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N' + )), + array('Comment' => array( + 'id' => 5, + 'article_id' => 2, + 'user_id' => 1, + 'comment' => 'First Comment for Second Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 6, + 'article_id' => 2, + 'user_id' => 2, + 'comment' => 'Second Comment for Second Article', + 'published' => 'Y' + )))); + + $data = array( + 'Comment' => array( + 'article_id' => 2, + 'user_id' => 4, + 'comment' => 'Brand New Comment', + 'published' => 'N' + ), + 'Article' => array( + 'id' => 2, + 'title' => 'Second Article Modified' + )); + + $result = $Comment->create($data); + + $this->assertTrue($result); + $result = $Comment->save(); + $this->assertTrue($result); + + $articles = $Article->find('all', array( + 'fields' => array('id','title'), + 'recursive' => -1 + )); + + $comments = $Comment->find('all', array( + 'fields' => array('id','article_id','user_id','comment','published'), + 'recursive' => -1 + )); + + $this->assertEqual($articles, array( + array('Article' => array( + 'id' => 1, + 'title' => 'First Article' + )), + array('Article' => array( + 'id' => 2, + 'title' => 'Second Article' + )), + array('Article' => array( + 'id' => 3, + 'title' => 'Third Article' + )))); + + $this->assertEqual($comments, array( + array('Comment' => array( + 'id' => 1, + 'article_id' => 1, + 'user_id' => 2, + 'comment' => 'First Comment for First Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 2, + 'article_id' => 1, + 'user_id' => 4, + 'comment' => 'Second Comment for First Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 3, + 'article_id' => 1, + 'user_id' => 1, + 'comment' => 'Third Comment for First Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 4, + 'article_id' => 1, + 'user_id' => 1, + 'comment' => 'Fourth Comment for First Article', + 'published' => 'N' + )), + array('Comment' => array( + 'id' => 5, + 'article_id' => 2, + 'user_id' => 1, + 'comment' => 'First Comment for Second Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 6, + 'article_id' => 2, + 'user_id' => 2, 'comment' => + 'Second Comment for Second Article', + 'published' => 'Y' + )), + array('Comment' => array( + 'id' => 7, + 'article_id' => 2, + 'user_id' => 4, + 'comment' => 'Brand New Comment', + 'published' => 'N' + )))); + + } +/** + * testCreationWithMultipleDataSameModel method + * + * @access public + * @return void + */ + function testCreationWithMultipleDataSameModel() { + $this->loadFixtures('Article'); + $Article =& new Article(); + $SecondaryArticle =& new Article(); + + $result = $Article->field('title', array('id' => 1)); + $this->assertEqual($result, 'First Article'); + + $data = array( + 'Article' => array( + 'user_id' => 2, + 'title' => 'Brand New Article', + 'body' => 'Brand New Article Body', + 'published' => 'Y' + ), + 'SecondaryArticle' => array( + 'id' => 1 + )); + + $Article->create(); + $result = $Article->save($data); + $this->assertTrue($result); + + $result = $Article->getInsertID(); + $this->assertTrue(!empty($result)); + + $result = $Article->field('title', array('id' => 1)); + $this->assertEqual($result, 'First Article'); + + $articles = $Article->find('all', array( + 'fields' => array('id','title'), + 'recursive' => -1 + )); + + $this->assertEqual($articles, array( + array('Article' => array( + 'id' => 1, + 'title' => 'First Article' + )), + array('Article' => array( + 'id' => 2, + 'title' => 'Second Article' + )), + array('Article' => array( + 'id' => 3, + 'title' => 'Third Article' + )), + array('Article' => array( + 'id' => 4, + 'title' => 'Brand New Article' + )))); + } +/** + * testCreationWithMultipleDataSameModelManualInstances method + * + * @access public + * @return void + */ + function testCreationWithMultipleDataSameModelManualInstances() { + $this->loadFixtures('PrimaryModel'); + $Primary =& new PrimaryModel(); + $Secondary =& new PrimaryModel(); + + $result = $Primary->field('primary_name', array('id' => 1)); + $this->assertEqual($result, 'Primary Name Existing'); + + $data = array( + 'PrimaryModel' => array( + 'primary_name' => 'Primary Name New' + ), + 'SecondaryModel' => array( + 'id' => array(1) + )); + + $Primary->create(); + $result = $Primary->save($data); + $this->assertTrue($result); + + $result = $Primary->field('primary_name', array('id' => 1)); + $this->assertEqual($result, 'Primary Name Existing'); + + $result = $Primary->getInsertID(); + $this->assertTrue(!empty($result)); + + $result = $Primary->field('primary_name', array('id' => $result)); + $this->assertEqual($result, 'Primary Name New'); + + $result = $Primary->find('count'); + $this->assertEqual($result, 2); + } +/** + * testRecordExists method + * + * @access public + * @return void + */ + function testRecordExists() { + $this->loadFixtures('User'); + $TestModel =& new User(); + + $this->assertFalse($TestModel->exists()); + $TestModel->read(null, 1); + $this->assertTrue($TestModel->exists()); + $TestModel->create(); + $this->assertFalse($TestModel->exists()); + $TestModel->id = 4; + $this->assertTrue($TestModel->exists()); + + $TestModel =& new TheVoid(); + $this->assertFalse($TestModel->exists()); + $TestModel->id = 5; + $this->assertFalse($TestModel->exists()); + } +/** + * testUpdateExisting method + * + * @access public + * @return void + */ + function testUpdateExisting() { + $this->loadFixtures('User', 'Article', 'Comment'); + $TestModel =& new User(); + $TestModel->create(); + + $TestModel->save(array( + 'User' => array( + 'user' => 'some user', + 'password' => 'some password' + ))); + $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5)); + $id = $TestModel->id; + + $TestModel->save(array( + 'User' => array( + 'user' => 'updated user' + ))); + $this->assertEqual($TestModel->id, $id); + + $result = $TestModel->findById($id); + $this->assertEqual($result['User']['user'], 'updated user'); + $this->assertEqual($result['User']['password'], 'some password'); + + $Article =& new Article(); + $Comment =& new Comment(); + $data = array( + 'Comment' => array( + 'id' => 1, + 'comment' => 'First Comment for First Article' + ), + 'Article' => array( + 'id' => 2, + 'title' => 'Second Article' + )); + + $result = $Article->save($data); + $this->assertTrue($result); + + $result = $Comment->save($data); + $this->assertTrue($result); + } +/** + * testUpdateMultiple method + * + * @access public + * @return void + */ + function testUpdateMultiple() { + $this->loadFixtures('Comment', 'Article', 'User', 'CategoryThread'); + $TestModel =& new Comment(); + $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id'); + $expected = array('2', '4', '1', '1', '1', '2'); + $this->assertEqual($result, $expected); + + $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2)); + $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id'); + $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5); + $this->assertEqual($result, $expected); + + $result = $TestModel->updateAll( + array('Comment.comment' => "'Updated today'"), + array('Comment.user_id' => 5) + ); + $this->assertTrue($result); + $result = Set::extract( + $TestModel->find('all', array( + 'conditions' => array( + 'Comment.user_id' => 5 + ))), + '{n}.Comment.comment' + ); + $expected = array_fill(0, 2, 'Updated today'); + $this->assertEqual($result, $expected); + } +/** + * testHabtmUuidWithUuidId method + * + * @access public + * @return void + */ + function testHabtmUuidWithUuidId() { + $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolio'); + $TestModel =& new Uuidportfolio(); + + $data = array('Uuidportfolio' => array('name' => 'Portfolio 3')); + $data['Uuiditem']['Uuiditem'] = array('483798c8-c7cc-430e-8cf9-4fcc40cf8569'); + $TestModel->create($data); + $TestModel->save(); + $id = $TestModel->id; + $result = $TestModel->read(null, $id); + $this->assertEqual(1, count($result['Uuiditem'])); + $this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36); + } +/** + * test HABTM saving when join table has no primary key and only 2 columns. + * + * @return void + **/ + function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() { + $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); + $Fruit =& new Fruit(); + $data = array( + 'Fruit' => array( + 'color' => 'Red', + 'shape' => 'Heart-shaped', + 'taste' => 'sweet', + 'name' => 'Strawberry', + ), + 'UuidTag' => array( + 'UuidTag' => array( + '481fc6d0-b920-43e0-e50f-6d1740cf8569' + ) + ) + ); + $this->assertTrue($Fruit->save($data)); + } +/** + * test HABTM saving when join table has no primary key and only 2 columns, no with model is used. + * + * @return void + **/ + function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() { + $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); + $Fruit =& new FruitNoWith(); + $data = array( + 'Fruit' => array( + 'color' => 'Red', + 'shape' => 'Heart-shaped', + 'taste' => 'sweet', + 'name' => 'Strawberry', + ), + 'UuidTag' => array( + 'UuidTag' => array( + '481fc6d0-b920-43e0-e50f-6d1740cf8569' + ) + ) + ); + $this->assertTrue($Fruit->save($data)); + } + +/** + * testHabtmUuidWithNumericId method + * + * @access public + * @return void + */ + function testHabtmUuidWithNumericId() { + $this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid'); + $TestModel =& new Uuiditem(); + + $data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0)); + $data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569'); + $TestModel->create($data); + $TestModel->save(); + $id = $TestModel->id; + $result = $TestModel->read(null, $id); + $this->assertEqual(1, count($result['Uuidportfolio'])); + } +/** + * testSaveMultipleHabtm method + * + * @access public + * @return void + */ + function testSaveMultipleHabtm() { + $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC'); + $TestModel = new JoinA(); + $result = $TestModel->findById(1); + + $expected = array( + 'JoinA' => array( + 'id' => 1, + 'name' => 'Join A 1', + 'body' => 'Join A 1 Body', + 'created' => '2008-01-03 10:54:23', + 'updated' => '2008-01-03 10:54:23' + ), + 'JoinB' => array( + 0 => array( + 'id' => 2, + 'name' => 'Join B 2', + 'created' => '2008-01-03 10:55:02', + 'updated' => '2008-01-03 10:55:02', + 'JoinAsJoinB' => array( + 'id' => 1, + 'join_a_id' => 1, + 'join_b_id' => 2, + 'other' => 'Data for Join A 1 Join B 2', + 'created' => '2008-01-03 10:56:33', + 'updated' => '2008-01-03 10:56:33' + ))), + 'JoinC' => array( + 0 => array( + 'id' => 2, + 'name' => 'Join C 2', + 'created' => '2008-01-03 10:56:12', + 'updated' => '2008-01-03 10:56:12', + 'JoinAsJoinC' => array( + 'id' => 1, + 'join_a_id' => 1, + 'join_c_id' => 2, + 'other' => 'Data for Join A 1 Join C 2', + 'created' => '2008-01-03 10:57:22', + 'updated' => '2008-01-03 10:57:22' + )))); + + $this->assertEqual($result, $expected); + + $ts = date('Y-m-d H:i:s'); + $TestModel->id = 1; + $data = array( + 'JoinA' => array( + 'id' => '1', + 'name' => 'New name for Join A 1', + 'updated' => $ts + ), + 'JoinB' => array( + array( + 'id' => 1, + 'join_b_id' => 2, + 'other' => 'New data for Join A 1 Join B 2', + 'created' => $ts, + 'updated' => $ts + )), + 'JoinC' => array( + array( + 'id' => 1, + 'join_c_id' => 2, + 'other' => 'New data for Join A 1 Join C 2', + 'created' => $ts, + 'updated' => $ts + ))); + + $TestModel->set($data); + $TestModel->save(); + + $result = $TestModel->findById(1); + $expected = array( + 'JoinA' => array( + 'id' => 1, + 'name' => 'New name for Join A 1', + 'body' => 'Join A 1 Body', + 'created' => '2008-01-03 10:54:23', + 'updated' => $ts + ), + 'JoinB' => array( + 0 => array( + 'id' => 2, + 'name' => 'Join B 2', + 'created' => '2008-01-03 10:55:02', + 'updated' => '2008-01-03 10:55:02', + 'JoinAsJoinB' => array( + 'id' => 1, + 'join_a_id' => 1, + 'join_b_id' => 2, + 'other' => 'New data for Join A 1 Join B 2', + 'created' => $ts, + 'updated' => $ts + ))), + 'JoinC' => array( + 0 => array( + 'id' => 2, + 'name' => 'Join C 2', + 'created' => '2008-01-03 10:56:12', + 'updated' => '2008-01-03 10:56:12', + 'JoinAsJoinC' => array( + 'id' => 1, + 'join_a_id' => 1, + 'join_c_id' => 2, + 'other' => 'New data for Join A 1 Join C 2', + 'created' => $ts, + 'updated' => $ts + )))); + + $this->assertEqual($result, $expected); + } +/** + * testSaveAll method + * + * @access public + * @return void + */ + function testSaveAll() { + $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); + $TestModel =& new Post(); + + $result = $TestModel->find('all'); + $this->assertEqual(count($result), 3); + $this->assertFalse(isset($result[3])); + $ts = date('Y-m-d H:i:s'); + + $TestModel->saveAll(array( + 'Post' => array( + 'title' => 'Post with Author', + 'body' => 'This post will be saved with an author' + ), + 'Author' => array( + 'user' => 'bob', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf90' + ))); + + $result = $TestModel->find('all'); + $expected = array( + 'Post' => array( + 'id' => '4', + 'author_id' => '5', + 'title' => 'Post with Author', + 'body' => 'This post will be saved with an author', + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + ), + 'Author' => array( + 'id' => '5', + 'user' => 'bob', + 'password' => '5f4dcc3b5aa765d61d8327deb882cf90', + 'created' => $ts, + 'updated' => $ts, + 'test' => 'working' + )); + $this->assertEqual($result[3], $expected); + $this->assertEqual(count($result), 4); + + $TestModel->deleteAll(true); + $this->assertEqual($TestModel->find('all'), array()); + + // SQLite seems to reset the PK counter when that happens, so we need this to make the tests pass + $this->db->truncate($TestModel); + + $ts = date('Y-m-d H:i:s'); + $TestModel->saveAll(array( + array( + 'title' => 'Multi-record post 1', + 'body' => 'First multi-record post', + 'author_id' => 2 + ), + array( + 'title' => 'Multi-record post 2', + 'body' => 'Second multi-record post', + 'author_id' => 2 + ))); + + $result = $TestModel->find('all', array( + 'recursive' => -1, + 'order' => 'Post.id ASC' + )); + $expected = array( + array( + 'Post' => array( + 'id' => '1', + 'author_id' => '2', + 'title' => 'Multi-record post 1', + 'body' => 'First multi-record post', + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + )), + array( + 'Post' => array( + 'id' => '2', + 'author_id' => '2', + 'title' => 'Multi-record post 2', + 'body' => 'Second multi-record post', + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + ))); + $this->assertEqual($result, $expected); + + $TestModel =& new Comment(); + $ts = date('Y-m-d H:i:s'); + $result = $TestModel->saveAll(array( + 'Comment' => array( + 'article_id' => 2, + 'user_id' => 2, + 'comment' => 'New comment with attachment', + 'published' => 'Y' + ), + 'Attachment' => array( + 'attachment' => 'some_file.tgz' + ))); + $this->assertTrue($result); + + $result = $TestModel->find('all'); + $expected = array( + 'id' => '7', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'New comment with attachment', + 'published' => 'Y', + 'created' => $ts, + 'updated' => $ts + ); + $this->assertEqual($result[6]['Comment'], $expected); + + $expected = array( + 'id' => '7', + 'article_id' => '2', + 'user_id' => '2', + 'comment' => 'New comment with attachment', + 'published' => 'Y', + 'created' => $ts, + 'updated' => $ts + ); + $this->assertEqual($result[6]['Comment'], $expected); + + $expected = array( + 'id' => '2', + 'comment_id' => '7', + 'attachment' => 'some_file.tgz', + 'created' => $ts, + 'updated' => $ts + ); + $this->assertEqual($result[6]['Attachment'], $expected); + } +/** + * Test SaveAll with Habtm relations + * + * @access public + * @return void + */ + function testSaveAllHabtm() { + $this->loadFixtures('Article', 'Tag', 'Comment', 'User'); + $data = array( + 'Article' => array( + 'user_id' => 1, + 'title' => 'Article Has and belongs to Many Tags' + ), + 'Tag' => array( + 'Tag' => array(1, 2) + ), + 'Comment' => array( + array( + 'comment' => 'Article comment', + 'user_id' => 1 + ))); + $Article =& new Article(); + $result = $Article->saveAll($data); + $this->assertTrue($result); + + $result = $Article->read(); + $this->assertEqual(count($result['Tag']), 2); + $this->assertEqual($result['Tag'][0]['tag'], 'tag1'); + $this->assertEqual(count($result['Comment']), 1); + $this->assertEqual(count($result['Comment'][0]['comment']['Article comment']), 1); + } +/** + * Test SaveAll with Habtm relations and extra join table fields + * + * @access public + * @return void + */ + function testSaveAllHabtmWithExtraJoinTableFields() { + $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); + + $data = array( + 'Something' => array( + 'id' => 4, + 'title' => 'Extra Fields', + 'body' => 'Extra Fields Body', + 'published' => '1' + ), + 'SomethingElse' => array( + array('something_else_id' => 1, 'doomed' => '1'), + array('something_else_id' => 2, 'doomed' => '0'), + array('something_else_id' => 3, 'doomed' => '1') + ) + ); + + $Something =& new Something(); + $result = $Something->saveAll($data); + $this->assertTrue($result); + $result = $Something->read(); + + $this->assertEqual(count($result['SomethingElse']), 3); + $this->assertTrue(Set::matches('/Something[id=4]', $result)); + + $this->assertTrue(Set::matches('/SomethingElse[id=1]', $result)); + $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[something_else_id=1]', $result)); + $this->assertTrue(Set::matches('/SomethingElse[id=1]/JoinThing[doomed=1]', $result)); + + $this->assertTrue(Set::matches('/SomethingElse[id=2]', $result)); + $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[something_else_id=2]', $result)); + $this->assertTrue(Set::matches('/SomethingElse[id=2]/JoinThing[doomed=0]', $result)); + + $this->assertTrue(Set::matches('/SomethingElse[id=3]', $result)); + $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[something_else_id=3]', $result)); + $this->assertTrue(Set::matches('/SomethingElse[id=3]/JoinThing[doomed=1]', $result)); + } +/** + * testSaveAllHasOne method + * + * @access public + * @return void + */ + function testSaveAllHasOne() { + $model = new Comment(); + $model->deleteAll(true); + $this->assertEqual($model->find('all'), array()); + + $model->Attachment->deleteAll(true); + $this->assertEqual($model->Attachment->find('all'), array()); + + $this->assertTrue($model->saveAll(array( + 'Comment' => array( + 'comment' => 'Comment with attachment', + 'article_id' => 1, + 'user_id' => 1 + ), + 'Attachment' => array( + 'attachment' => 'some_file.zip' + )))); + $result = $model->find('all', array('fields' => array( + 'Comment.id', 'Comment.comment', 'Attachment.id', + 'Attachment.comment_id', 'Attachment.attachment' + ))); + $expected = array(array( + 'Comment' => array( + 'id' => '1', + 'comment' => 'Comment with attachment' + ), + 'Attachment' => array( + 'id' => '1', + 'comment_id' => '1', + 'attachment' => 'some_file.zip' + ))); + $this->assertEqual($result, $expected); + } +/** + * testSaveAllBelongsTo method + * + * @access public + * @return void + */ + function testSaveAllBelongsTo() { + $model = new Comment(); + $model->deleteAll(true); + $this->assertEqual($model->find('all'), array()); + + $model->Article->deleteAll(true); + $this->assertEqual($model->Article->find('all'), array()); + + $this->assertTrue($model->saveAll(array( + 'Comment' => array( + 'comment' => 'Article comment', + 'article_id' => 1, + 'user_id' => 1 + ), + 'Article' => array( + 'title' => 'Model Associations 101', + 'user_id' => 1 + )))); + $result = $model->find('all', array('fields' => array( + 'Comment.id', 'Comment.comment', 'Comment.article_id', 'Article.id', 'Article.title' + ))); + $expected = array(array( + 'Comment' => array( + 'id' => '1', + 'article_id' => '1', + 'comment' => 'Article comment' + ), + 'Article' => array( + 'id' => '1', + 'title' => 'Model Associations 101' + ))); + $this->assertEqual($result, $expected); + } +/** + * testSaveAllHasOneValidation method + * + * @access public + * @return void + */ + function testSaveAllHasOneValidation() { + $model = new Comment(); + $model->deleteAll(true); + $this->assertEqual($model->find('all'), array()); + + $model->Attachment->deleteAll(true); + $this->assertEqual($model->Attachment->find('all'), array()); + + $model->validate = array('comment' => 'notEmpty'); + $model->Attachment->validate = array('attachment' => 'notEmpty'); + $model->Attachment->bind('Comment'); + + $this->assertFalse($model->saveAll( + array( + 'Comment' => array( + 'comment' => '', + 'article_id' => 1, + 'user_id' => 1 + ), + 'Attachment' => array('attachment' => '') + ), + array('validate' => 'first') + )); + $expected = array( + 'Comment' => array('comment' => 'This field cannot be left blank'), + 'Attachment' => array('attachment' => 'This field cannot be left blank') + ); + $this->assertEqual($model->validationErrors, $expected['Comment']); + $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']); + + $this->assertFalse($model->saveAll( + array( + 'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1), + 'Attachment' => array('attachment' => '') + ), + array('validate' => 'only') + )); + $this->assertEqual($model->validationErrors, $expected['Comment']); + $this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']); + } +/** + * testSaveAllAtomic method + * + * @access public + * @return void + */ + function testSaveAllAtomic() { + $this->loadFixtures('Article', 'User'); + $TestModel =& new Article(); + + $result = $TestModel->saveAll(array( + 'Article' => array( + 'title' => 'Post with Author', + 'body' => 'This post will be saved with an author', + 'user_id' => 2 + ), + 'Comment' => array( + array('comment' => 'First new comment', 'user_id' => 2)) + ), array('atomic' => false)); + + $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true))); + + $result = $TestModel->saveAll(array( + array( + 'id' => '1', + 'title' => 'Baleeted First Post', + 'body' => 'Baleeted!', + 'published' => 'N' + ), + array( + 'id' => '2', + 'title' => 'Just update the title' + ), + array( + 'title' => 'Creating a fourth post', + 'body' => 'Fourth post body', + 'user_id' => 2 + ) + ), array('atomic' => false)); + $this->assertIdentical($result, array(true, true, true)); + + $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); + $result = $TestModel->saveAll(array( + array( + 'id' => '1', + 'title' => 'Un-Baleeted First Post', + 'body' => 'Not Baleeted!', + 'published' => 'Y' + ), + array( + 'id' => '2', + 'title' => '', + 'body' => 'Trying to get away with an empty title' + ) + ), array('atomic' => false)); + $this->assertIdentical($result, array(true, false)); + + $result = $TestModel->saveAll(array( + 'Article' => array('id' => 2), + 'Comment' => array( + array( + 'comment' => 'First new comment', + 'published' => 'Y', + 'user_id' => 1 + ), + array( + 'comment' => 'Second new comment', + 'published' => 'Y', + 'user_id' => 2 + )) + ), array('atomic' => false)); + $this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true))); + } +/** + * testSaveAllHasMany method + * + * @access public + * @return void + */ + function testSaveAllHasMany() { + $this->loadFixtures('Article', 'Comment'); + $TestModel =& new Article(); + $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); + + $result = $TestModel->saveAll(array( + 'Article' => array('id' => 2), + 'Comment' => array( + array('comment' => 'First new comment', 'published' => 'Y', 'user_id' => 1), + array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2) + ) + )); + $this->assertTrue($result); + + $result = $TestModel->findById(2); + $expected = array( + 'First Comment for Second Article', + 'Second Comment for Second Article', + 'First new comment', + 'Second new comment' + ); + $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); + + $result = $TestModel->saveAll( + array( + 'Article' => array('id' => 2), + 'Comment' => array( + array( + 'comment' => 'Third new comment', + 'published' => 'Y', + 'user_id' => 1 + ))), + array('atomic' => false) + ); + $this->assertTrue($result); + + $result = $TestModel->findById(2); + $expected = array( + 'First Comment for Second Article', + 'Second Comment for Second Article', + 'First new comment', + 'Second new comment', + 'Third new comment' + ); + $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); + + $TestModel->beforeSaveReturn = false; + $result = $TestModel->saveAll( + array( + 'Article' => array('id' => 2), + 'Comment' => array( + array( + 'comment' => 'Fourth new comment', + 'published' => 'Y', + 'user_id' => 1 + ))), + array('atomic' => false) + ); + $this->assertEqual($result, array('Article' => false)); + + $result = $TestModel->findById(2); + $expected = array( + 'First Comment for Second Article', + 'Second Comment for Second Article', + 'First new comment', + 'Second new comment', + 'Third new comment' + ); + $this->assertEqual(Set::extract($result['Comment'], '{n}.comment'), $expected); + } +/** + * testSaveAllHasManyValidation method + * + * @access public + * @return void + */ + function testSaveAllHasManyValidation() { + $this->loadFixtures('Article', 'Comment'); + $TestModel =& new Article(); + $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); + $TestModel->Comment->validate = array('comment' => 'notEmpty'); + + $result = $TestModel->saveAll(array( + 'Article' => array('id' => 2), + 'Comment' => array( + array('comment' => '', 'published' => 'Y', 'user_id' => 1), + ) + )); + $expected = array('Comment' => array(false)); + $this->assertEqual($result, $expected); + + $expected = array('Comment' => array( + array('comment' => 'This field cannot be left blank') + )); + $this->assertEqual($TestModel->validationErrors, $expected); + $expected = array( + array('comment' => 'This field cannot be left blank') + ); + $this->assertEqual($TestModel->Comment->validationErrors, $expected); + + $result = $TestModel->saveAll(array( + 'Article' => array('id' => 2), + 'Comment' => array( + array( + 'comment' => '', + 'published' => 'Y', + 'user_id' => 1 + )) + ), array('validate' => 'only')); + } +/** + * testSaveAllTransaction method + * + * @access public + * @return void + */ + function testSaveAllTransaction() { + $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); + $TestModel =& new Post(); + + $TestModel->validate = array('title' => 'notEmpty'); + $data = array( + array('author_id' => 1, 'title' => 'New Fourth Post'), + array('author_id' => 1, 'title' => 'New Fifth Post'), + array('author_id' => 1, 'title' => '') + ); + $ts = date('Y-m-d H:i:s'); + $this->assertFalse($TestModel->saveAll($data)); + + $result = $TestModel->find('all', array('recursive' => -1)); + $expected = array( + array('Post' => array( + 'id' => '1', + 'author_id' => 1, + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + array('Post' => array( + 'id' => '2', + 'author_id' => 3, + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + )), + array('Post' => array( + 'id' => '3', + 'author_id' => 1, + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ))); + + if (count($result) != 3) { + // Database doesn't support transactions + $expected[] = array( + 'Post' => array( + 'id' => '4', + 'author_id' => 1, + 'title' => 'New Fourth Post', + 'body' => null, + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + )); + + $expected[] = array( + 'Post' => array( + 'id' => '5', + 'author_id' => 1, + 'title' => 'New Fifth Post', + 'body' => null, + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + )); + + $this->assertEqual($result, $expected); + // Skip the rest of the transactional tests + return; + } + + $this->assertEqual($result, $expected); + + $data = array( + array('author_id' => 1, 'title' => 'New Fourth Post'), + array('author_id' => 1, 'title' => ''), + array('author_id' => 1, 'title' => 'New Sixth Post') + ); + $ts = date('Y-m-d H:i:s'); + $this->assertFalse($TestModel->saveAll($data)); + + $result = $TestModel->find('all', array('recursive' => -1)); + $expected = array( + array('Post' => array( + 'id' => '1', + 'author_id' => 1, + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31' + )), + array('Post' => array( + 'id' => '2', + 'author_id' => 3, + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => '2007-03-18 10:43:31' + )), + array('Post' => array( + 'id' => '3', + 'author_id' => 1, + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + ))); + + if (count($result) != 3) { + // Database doesn't support transactions + $expected[] = array( + 'Post' => array( + 'id' => '4', + 'author_id' => 1, + 'title' => 'New Fourth Post', + 'body' => 'Third Post Body', + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + )); + + $expected[] = array( + 'Post' => array( + 'id' => '5', + 'author_id' => 1, + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + )); + } + $this->assertEqual($result, $expected); + + $TestModel->validate = array('title' => 'notEmpty'); + $data = array( + array('author_id' => 1, 'title' => 'New Fourth Post'), + array('author_id' => 1, 'title' => 'New Fifth Post'), + array('author_id' => 1, 'title' => 'New Sixth Post') + ); + $this->assertTrue($TestModel->saveAll($data)); + + $result = $TestModel->find('all', array( + 'recursive' => -1, + 'fields' => array('author_id', 'title','body','published') + )); + + $expected = array( + array('Post' => array( + 'author_id' => 1, + 'title' => 'First Post', + 'body' => 'First Post Body', + 'published' => 'Y' + )), + array('Post' => array( + 'author_id' => 3, + 'title' => 'Second Post', + 'body' => 'Second Post Body', + 'published' => 'Y' + )), + array('Post' => array( + 'author_id' => 1, + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y' + )), + array('Post' => array( + 'author_id' => 1, + 'title' => 'New Fourth Post', + 'body' => '', + 'published' => 'N' + )), + array('Post' => array( + 'author_id' => 1, + 'title' => 'New Fifth Post', + 'body' => '', + 'published' => 'N' + )), + array('Post' => array( + 'author_id' => 1, + 'title' => 'New Sixth Post', + 'body' => '', + 'published' => 'N' + ))); + $this->assertEqual($result, $expected); + } + +/** + * testSaveAllValidation method + * + * @access public + * @return void + */ + function testSaveAllValidation() { + $this->loadFixtures('Post', 'Author', 'Comment', 'Attachment'); + $TestModel =& new Post(); + + $data = array( + array( + 'id' => '1', + 'title' => 'Baleeted First Post', + 'body' => 'Baleeted!', + 'published' => 'N' + ), + array( + 'id' => '2', + 'title' => 'Just update the title' + ), + array( + 'title' => 'Creating a fourth post', + 'body' => 'Fourth post body', + 'author_id' => 2 + )); + + $this->assertTrue($TestModel->saveAll($data)); + + $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); + $ts = date('Y-m-d H:i:s'); + $expected = array( + array( + 'Post' => array( + 'id' => '1', + 'author_id' => '1', + 'title' => 'Baleeted First Post', + 'body' => 'Baleeted!', + 'published' => 'N', + 'created' => '2007-03-18 10:39:23', + 'updated' => $ts + )), + array( + 'Post' => array( + 'id' => '2', + 'author_id' => '3', + 'title' => 'Just update the title', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', 'updated' => $ts + )), + array( + 'Post' => array( + 'id' => '3', + 'author_id' => '1', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + )), + array( + 'Post' => array( + 'id' => '4', + 'author_id' => '2', + 'title' => 'Creating a fourth post', + 'body' => 'Fourth post body', + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + ))); + $this->assertEqual($result, $expected); + + $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); + $data = array( + array( + 'id' => '1', + 'title' => 'Un-Baleeted First Post', + 'body' => 'Not Baleeted!', + 'published' => 'Y' + ), + array( + 'id' => '2', + 'title' => '', + 'body' => 'Trying to get away with an empty title' + )); + $result = $TestModel->saveAll($data); + $this->assertEqual($result, false); + + $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); + $errors = array(1 => array('title' => 'This field cannot be left blank')); + $transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result); + if (!$transactionWorked) { + $this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result)); + $this->assertTrue(Set::matches('/Post[2][title=Just update the title]', $result)); + } + + $this->assertEqual($TestModel->validationErrors, $errors); + + $TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric'); + $data = array( + array( + 'id' => '1', + 'title' => 'Un-Baleeted First Post', + 'body' => 'Not Baleeted!', + 'published' => 'Y' + ), + array( + 'id' => '2', + 'title' => '', + 'body' => 'Trying to get away with an empty title' + )); + $result = $TestModel->saveAll($data, array('atomic' => false)); + $this->assertEqual($result, array(true, false)); + $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); + $errors = array(1 => array('title' => 'This field cannot be left blank')); + $newTs = date('Y-m-d H:i:s'); + $expected = array( + array( + 'Post' => array( + 'id' => '1', + 'author_id' => '1', + 'title' => 'Un-Baleeted First Post', + 'body' => 'Not Baleeted!', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => $newTs + )), + array( + 'Post' => array( + 'id' => '2', + 'author_id' => '3', + 'title' => 'Just update the title', + 'body' => 'Second Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:41:23', + 'updated' => $ts + )), + array( + 'Post' => array( + 'id' => '3', + 'author_id' => '1', + 'title' => 'Third Post', + 'body' => 'Third Post Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:43:23', + 'updated' => '2007-03-18 10:45:31' + )), + array( + 'Post' => array( + 'id' => '4', + 'author_id' => '2', + 'title' => 'Creating a fourth post', + 'body' => 'Fourth post body', + 'published' => 'N', + 'created' => $ts, + 'updated' => $ts + ))); + $this->assertEqual($result, $expected); + $this->assertEqual($TestModel->validationErrors, $errors); + + $data = array( + array( + 'id' => '1', + 'title' => 'Re-Baleeted First Post', + 'body' => 'Baleeted!', + 'published' => 'N' + ), + array( + 'id' => '2', + 'title' => '', + 'body' => 'Trying to get away with an empty title' + )); + $this->assertFalse($TestModel->saveAll($data, array('validate' => 'first'))); + + $result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC')); + $this->assertEqual($result, $expected); + $this->assertEqual($TestModel->validationErrors, $errors); + + $data = array( + array( + 'title' => 'First new post', + 'body' => 'Woohoo!', + 'published' => 'Y' + ), + array( + 'title' => 'Empty body', + 'body' => '' + )); + + $TestModel->validate['body'] = 'notEmpty'; + } +/** + * testSaveAllValidationOnly method + * + * @access public + * @return void + */ + function testSaveAllValidationOnly() { + $TestModel =& new Comment(); + $TestModel->Attachment->validate = array('attachment' => 'notEmpty'); + + $data = array( + 'Comment' => array( + 'comment' => 'This is the comment' + ), + 'Attachment' => array( + 'attachment' => '' + ) + ); + + $result = $TestModel->saveAll($data, array('validate' => 'only')); + $this->assertFalse($result); + + $TestModel =& new Article(); + $TestModel->validate = array('title' => 'notEmpty'); + $result = $TestModel->saveAll( + array( + 0 => array('title' => ''), + 1 => array('title' => 'title 1'), + 2 => array('title' => 'title 2'), + ), + array('validate'=>'only') + ); + $this->assertFalse($result); + $expected = array( + 0 => array('title' => 'This field cannot be left blank'), + ); + $this->assertEqual($TestModel->validationErrors, $expected); + + $result = $TestModel->saveAll( + array( + 0 => array('title' => 'title 0'), + 1 => array('title' => ''), + 2 => array('title' => 'title 2'), + ), + array('validate'=>'only') + ); + $this->assertFalse($result); + $expected = array( + 1 => array('title' => 'This field cannot be left blank'), + ); + $this->assertEqual($TestModel->validationErrors, $expected); + } +/** + * testSaveAllValidateFirst method + * + * @access public + * @return void + */ + function testSaveAllValidateFirst() { + $model =& new Article(); + $model->deleteAll(true); + + $model->Comment->validate = array('comment' => 'notEmpty'); + $result = $model->saveAll(array( + 'Article' => array( + 'title' => 'Post with Author', + 'body' => 'This post will be saved author' + ), + 'Comment' => array( + array('comment' => 'First new comment'), + array('comment' => '') + ) + ), array('validate' => 'first')); + + $this->assertFalse($result); + + $result = $model->find('all'); + $this->assertEqual($result, array()); + $expected = array('Comment' => array( + 1 => array('comment' => 'This field cannot be left blank') + )); + + $this->assertEqual($model->Comment->validationErrors, $expected['Comment']); + + $this->assertIdentical($model->Comment->find('count'), 0); + + $result = $model->saveAll( + array( + 'Article' => array( + 'title' => 'Post with Author', + 'body' => 'This post will be saved with an author', + 'user_id' => 2 + ), + 'Comment' => array( + array( + 'comment' => 'Only new comment', + 'user_id' => 2 + ))), + array('validate' => 'first') + ); + + $this->assertIdentical($result, true); + + $result = $model->Comment->find('all'); + $this->assertIdentical(count($result), 1); + $result = Set::extract('/Comment/article_id', $result); + $this->assertTrue($result[0] === 1 || $result[0] === '1'); + + + $model->deleteAll(true); + $data = array( + 'Article' => array( + 'title' => 'Post with Author saveAlled from comment', + 'body' => 'This post will be saved with an author', + 'user_id' => 2 + ), + 'Comment' => array( + 'comment' => 'Only new comment', 'user_id' => 2 + )); + + $result = $model->Comment->saveAll($data, array('validate' => 'first')); + $this->assertTrue($result); + + $result = $model->find('all'); + $this->assertEqual( + $result[0]['Article']['title'], + 'Post with Author saveAlled from comment' + ); + $this->assertEqual($result[0]['Comment'][0]['comment'], 'Only new comment'); + } +/** + * testUpdateWithCalculation method + * + * @access public + * @return void + */ + function testUpdateWithCalculation() { + $this->loadFixtures('DataTest'); + $model =& new DataTest(); + $result = $model->saveAll(array( + array('count' => 5, 'float' => 1.1), + array('count' => 3, 'float' => 1.2), + array('count' => 4, 'float' => 1.3), + array('count' => 1, 'float' => 2.0), + )); + $this->assertTrue($result); + + $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); + $this->assertEqual($result, array(5, 3, 4, 1)); + + $this->assertTrue($model->updateAll(array('count' => 'count + 2'))); + $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); + $this->assertEqual($result, array(7, 5, 6, 3)); + + $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1'))); + $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); + $this->assertEqual($result, array(6, 4, 5, 2)); + } +/** + * testSaveAllHasManyValidationOnly method + * + * @access public + * @return void + */ + function testSaveAllHasManyValidationOnly() { + $this->loadFixtures('Article', 'Comment'); + $TestModel =& new Article(); + $TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array(); + $TestModel->Comment->validate = array('comment' => 'notEmpty'); + + $result = $TestModel->saveAll( + array( + 'Article' => array('id' => 2), + 'Comment' => array( + array( + 'id' => 1, + 'comment' => '', + 'published' => 'Y', + 'user_id' => 1), + array( + 'id' => 2, + 'comment' => + 'comment', + 'published' => 'Y', + 'user_id' => 1 + ))), + array('validate' => 'only') + ); + $this->assertFalse($result); + + $result = $TestModel->saveAll( + array( + 'Article' => array('id' => 2), + 'Comment' => array( + array( + 'id' => 1, + 'comment' => '', + 'published' => 'Y', + 'user_id' => 1 + ), + array( + 'id' => 2, + 'comment' => 'comment', + 'published' => 'Y', + 'user_id' => 1 + ), + array( + 'id' => 3, + 'comment' => '', + 'published' => 'Y', + 'user_id' => 1 + ))), + array( + 'validate' => 'only', + 'atomic' => false + )); + $expected = array( + 'Article' => true, + 'Comment' => array(false, true, false) + ); + $this->assertIdentical($result, $expected); + + $expected = array('Comment' => array( + 0 => array('comment' => 'This field cannot be left blank'), + 2 => array('comment' => 'This field cannot be left blank') + )); + $this->assertEqual($TestModel->validationErrors, $expected); + + $expected = array( + 0 => array('comment' => 'This field cannot be left blank'), + 2 => array('comment' => 'This field cannot be left blank') + ); + $this->assertEqual($TestModel->Comment->validationErrors, $expected); + } + +} + +?> \ No newline at end of file