From c8974be88733a00a83f0635f006a81c20436728f Mon Sep 17 00:00:00 2001 From: nate Date: Fri, 11 Jan 2008 06:45:00 +0000 Subject: [PATCH] Refactoring fixture handling and Model tests, fixing UPDATE and DELETE queries for other databases, fixes #3794. Only forcing 'test_suite' prefix on default database connection, fixes #3873 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6361 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo/dbo_mssql.php | 24 +- .../model/datasources/dbo/dbo_postgres.php | 57 +-- .../libs/model/datasources/dbo/dbo_sqlite.php | 31 +- cake/libs/model/datasources/dbo_source.php | 78 +++- cake/tests/cases/libs/model/model.test.php | 378 ++++++------------ cake/tests/lib/cake_test_case.php | 66 +-- cake/tests/lib/cake_test_fixture.php | 71 +--- 7 files changed, 302 insertions(+), 403 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_mssql.php b/cake/libs/model/datasources/dbo/dbo_mssql.php index 1fae64532..8f9802c54 100644 --- a/cake/libs/model/datasources/dbo/dbo_mssql.php +++ b/cake/libs/model/datasources/dbo/dbo_mssql.php @@ -339,14 +339,16 @@ class DboMssql extends DboSource { return false; } /** - * Removes Identity (primary key) column from update data before returning to parent + * Generates and executes an SQL UPDATE statement for given model, fields, and values. + * Removes Identity (primary key) column from update data before returning to parent. * * @param Model $model * @param array $fields * @param array $values + * @param mixed $conditions * @return array */ - function update(&$model, $fields = array(), $values = array()) { + function update(&$model, $fields = array(), $values = null, $conditions = null) { foreach ($fields as $i => $field) { if ($field == $model->primaryKey) { unset ($fields[$i]); @@ -354,7 +356,23 @@ class DboMssql extends DboSource { break; } } - return parent::update($model, $fields, $values); + if (empty($conditions)) { + return parent::update($model, $fields, $values, null); + } + return parent::_update($model, $fields, $values, $conditions); + } +/** + * Generates and executes an SQL DELETE statement + * + * @param Model $model + * @param mixed $conditions + * @return boolean Success + */ + function delete(&$model, $conditions = null) { + if (empty($conditions)) { + return parent::delete($model, null); + } + return parent::_delete($model, $conditions); } /** * Returns a formatted error message from previous database operation. diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index e5d2c84c0..549de7a88 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -403,33 +403,8 @@ class DboPostgres extends DboSource { function update(&$model, $fields = array(), $values = null, $conditions = null) { if (empty($conditions)) { return parent::update($model, $fields, $values, null); - } elseif ($conditions === true) { - $conditions = $this->conditions(true); - } else { - $idList = $model->find('all', array('fields' => $model->escapeField(), 'conditions' => $conditions)); - - if (empty($idList)) { - return false; - } - $conditions = $this->conditions(array( - $model->primaryKey => Set::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}") - )); } - if ($values == null) { - $combined = $fields; - } else { - $combined = array_combine($fields, $values); - } - $fields = join(', ', $this->_prepareUpdateFields($model, $combined, false, false)); - - $alias = $joins = null; - $table = $this->fullTableName($model); - - if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) { - $model->onError(); - return false; - } - return true; + return parent::_update($model, $fields, $values, $conditions); } /** * Generates and executes an SQL DELETE statement @@ -441,26 +416,8 @@ class DboPostgres extends DboSource { function delete(&$model, $conditions = null) { if (empty($conditions)) { return parent::delete($model, null); - } elseif ($conditions === true) { - $conditions = $this->conditions(true); - } else { - $idList = $model->find('all', array('fields' => $model->escapeField(), 'conditions' => $conditions)); - - if (empty($idList)) { - return false; - } - $conditions = $this->conditions(array( - $model->primaryKey => Set::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}") - )); } - $alias = $joins = null; - $table = $this->fullTableName($model); - - if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) { - $model->onError(); - return false; - } - return true; + return parent::_delete($model, $conditions); } /** * Prepares field names to be quoted by parent @@ -684,16 +641,6 @@ class DboPostgres extends DboSource { function getEncoding() { return pg_client_encoding($this->connection); } -/** - * Inserts multiple values into a join table - * - * @param string $table - * @param string $fields - * @param array $values - */ - function insertMulti($table, $fields, $values) { - parent::__insertMulti($table, $fields, $values); - } /** * Generate a Postgres-native column schema string * diff --git a/cake/libs/model/datasources/dbo/dbo_sqlite.php b/cake/libs/model/datasources/dbo/dbo_sqlite.php index 573405c10..c07092782 100644 --- a/cake/libs/model/datasources/dbo/dbo_sqlite.php +++ b/cake/libs/model/datasources/dbo/dbo_sqlite.php @@ -192,15 +192,12 @@ class DboSqlite extends DboSource { if ($parent != null) { return $parent; } - if ($data === null) { return 'NULL'; } - if ($data === '') { return "''"; } - switch ($column) { case 'boolean': $data = $this->boolean((bool)$data); @@ -211,6 +208,34 @@ class DboSqlite extends DboSource { } return "'" . $data . "'"; } +/** + * Generates and executes an SQL UPDATE statement for given model, fields, and values. + * + * @param Model $model + * @param array $fields + * @param array $values + * @param mixed $conditions + * @return array + */ + function update(&$model, $fields = array(), $values = null, $conditions = null) { + if (empty($conditions)) { + return parent::update($model, $fields, $values, null); + } + return parent::_update($model, $fields, $values, $conditions); + } +/** + * Generates and executes an SQL DELETE statement + * + * @param Model $model + * @param mixed $conditions + * @return boolean Success + */ + function delete(&$model, $conditions = null) { + if (empty($conditions)) { + return parent::delete($model, null); + } + return parent::_delete($model, $conditions); + } /** * Begin a transaction * diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 0d1913a35..11321173e 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -1318,6 +1318,45 @@ class DboSource extends DataSource { return true; } /** + * Generates and executes an SQL UPDATE statement for given model, fields, and values. + * For databases that do not support aliases in UPDATE queries. + * + * @param Model $model + * @param array $fields + * @param array $values + * @param mixed $conditions + * @return array + */ + function _update(&$model, $fields = array(), $values = null, $conditions = null) { + if ($conditions === true) { + $conditions = $this->conditions(true); + } else { + $idList = $model->find('all', array('fields' => $model->escapeField(), 'conditions' => $conditions)); + + if (empty($idList)) { + return false; + } + $conditions = $this->conditions(array( + $model->primaryKey => Set::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}") + )); + } + if ($values == null) { + $combined = $fields; + } else { + $combined = array_combine($fields, $values); + } + $fields = join(', ', $this->_prepareUpdateFields($model, $combined, false, false)); + + $alias = $joins = null; + $table = $this->fullTableName($model); + + if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) { + $model->onError(); + return false; + } + return true; + } +/** * Quotes and prepares fields and values for an SQL UPDATE statement * * @param Model $model @@ -1377,6 +1416,36 @@ class DboSource extends DataSource { return true; } /** + * Generates and executes an SQL DELETE statement. + * For databases that do not support aliases in UPDATE queries. + * + * @param Model $model + * @param mixed $conditions + * @return boolean Success + */ + function _delete(&$model, $conditions = null) { + if ($conditions === true) { + $conditions = $this->conditions(true); + } else { + $idList = $model->find('all', array('fields' => $model->escapeField(), 'conditions' => $conditions)); + + if (empty($idList)) { + return false; + } + $conditions = $this->conditions(array( + $model->primaryKey => Set::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}") + )); + } + $alias = $joins = null; + $table = $this->fullTableName($model); + + if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) { + $model->onError(); + return false; + } + return true; + } +/** * Returns an array of SQL JOIN fragments from a model's associations * * @param object $model @@ -1935,8 +2004,9 @@ class DboSource extends DataSource { * @param array $values */ function insertMulti($table, $fields, $values) { - if (is_object($table)) { - $table = $this->fullTableName($table); + $table = $this->fullTableName($table); + if (is_array($fields)) { + $fields = join(', ', array_map(array(&$this, 'name'), $fields)); } $values = implode(', ', $values); $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}"); @@ -1953,6 +2023,9 @@ class DboSource extends DataSource { if (is_object($table)) { $table = $this->fullTableName($table); } + if (is_array($fields)) { + $fields = join(', ', array_map(array(&$this, 'name'), $fields)); + } $count = count($values); for ($x = 0; $x < $count; $x++) { $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}"); @@ -2094,7 +2167,6 @@ class DboSource extends DataSource { } elseif (isset($column['null']) && $column['null'] == false) { $out .= ' NOT NULL'; } - return $out; } /** diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index f7ba6c21d..6c04f1d91 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -497,6 +497,7 @@ class ModelTest extends CakeTestCase { } function testMultipleBelongsToWithSameClass() { + $this->loadFixtures('DeviceType', 'DeviceTypeCategory', 'FeatureSet', 'ExteriorTypeCategory', 'Document', 'Device', 'DocumentDirectory'); $this->DeviceType =& new DeviceType(); $this->DeviceType->recursive = 2; @@ -533,11 +534,12 @@ class ModelTest extends CakeTestCase { } function testHabtmRecursiveBelongsTo() { + $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image'); $this->Portfolio =& new Portfolio(); $result = $this->Portfolio->find(array('id' => 2), null, null, 3); $expected = array('Portfolio' => array( - 'id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2' ), + 'id' => 2, 'seller_id' => 1, 'name' => 'Portfolio 2'), 'Item' => array( array('id' => 2, 'syfile_id' => 2, 'name' => 'Item 2', 'ItemsPortfolio' => array('id' => 2, 'item_id' => 2, 'portfolio_id' => 2), @@ -553,6 +555,7 @@ class ModelTest extends CakeTestCase { } function testHasManyOptimization() { + $this->loadFixtures('Project', 'Thread', 'Message', 'Bid'); $this->Project =& new Project(); $this->Project->recursive = 3; @@ -575,6 +578,7 @@ class ModelTest extends CakeTestCase { } function testWithAssociation() { + $this->loadFixtures('Something', 'SomethingElse', 'JoinThing'); $this->model =& new Something(); $result = $this->model->SomethingElse->find('all'); @@ -637,8 +641,10 @@ class ModelTest extends CakeTestCase { } function testSaveMultipleHabtm() { + $this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC'); $this->model = new JoinA(); $result = $this->model->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( @@ -672,6 +678,7 @@ class ModelTest extends CakeTestCase { } function testFindAllRecursiveSelfJoin() { + $this->loadFixtures('Home', 'AnotherArticle', 'Advertisement'); $this->model =& new Home(); $this->model->recursive = 2; @@ -701,6 +708,7 @@ class ModelTest extends CakeTestCase { } function testCreation() { + $this->loadFixtures('Article'); $this->model =& new Test(); $result = $this->model->create(); $expected = array('Test' => array('notes' => 'write some notes here')); @@ -733,6 +741,7 @@ class ModelTest extends CakeTestCase { } function testCreationOfEmptyRecord() { + $this->loadFixtures('Author'); $this->model =& new Author(); $this->assertEqual($this->model->find('count'), 4); @@ -746,6 +755,7 @@ class ModelTest extends CakeTestCase { } function testCreationWithMultipleData() { + $this->loadFixtures('Article', 'Comment'); $this->Article =& new Article(); $this->Comment =& new Comment(); @@ -787,6 +797,7 @@ class ModelTest extends CakeTestCase { } function testCreationWithMultipleDataSameModel() { + $this->loadFixtures('Article'); $this->Article =& new Article(); $this->SecondaryArticle =& new Article(); @@ -814,6 +825,7 @@ class ModelTest extends CakeTestCase { } function testCreationWithMultipleDataSameModelManualInstances() { + $this->loadFixtures('PrimaryModel'); $Primary =& new PrimaryModel(); $Secondary =& new PrimaryModel(); @@ -840,6 +852,7 @@ class ModelTest extends CakeTestCase { } function testReadFakeThread() { + $this->loadFixtures('CategoryThread'); $this->model =& new CategoryThread(); $this->db->fullDebug = true; @@ -858,6 +871,7 @@ class ModelTest extends CakeTestCase { } function testFindFakeThread() { + $this->loadFixtures('CategoryThread'); $this->model =& new CategoryThread(); $this->db->fullDebug = true; @@ -876,6 +890,7 @@ class ModelTest extends CakeTestCase { } function testFindAllFakeThread() { + $this->loadFixtures('CategoryThread'); $this->model =& new CategoryThread(); $this->db->fullDebug = true; @@ -916,6 +931,7 @@ class ModelTest extends CakeTestCase { } function testFindAll() { + $this->loadFixtures('User'); $this->model =& new User(); $result = $this->model->findAll(); @@ -1000,6 +1016,7 @@ class ModelTest extends CakeTestCase { } function testGenerateList() { + $this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User'); $this->model =& new Article(); $this->model->displayField = 'title'; @@ -1059,6 +1076,7 @@ class ModelTest extends CakeTestCase { } function testFindField() { + $this->loadFixtures('User'); $this->model =& new User(); $this->model->id = 1; @@ -1079,7 +1097,18 @@ class ModelTest extends CakeTestCase { $this->assertEqual($result, 4); } + function testFindUnique() { + $this->loadFixtures('User'); + $this->model =& new User(); + + $this->assertFalse($this->model->isUnique(array('user' => 'nate'))); + $this->model->id = 2; + $this->assertTrue($this->model->isUnique(array('user' => 'nate'))); + $this->assertFalse($this->model->isUnique(array('user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'))); + } + function testUpdateExisting() { + $this->loadFixtures('User', 'Article', 'Comment'); $this->model =& new User(); $this->model->id = $id = 1000; $this->model->delete(); @@ -1103,6 +1132,7 @@ class ModelTest extends CakeTestCase { } function testBindUnbind() { + $this->loadFixtures('User', 'Comment', 'FeatureSet'); $this->model =& new User(); $result = $this->model->hasMany; @@ -1232,6 +1262,7 @@ class ModelTest extends CakeTestCase { } function testFindCount() { + $this->loadFixtures('User'); $this->model =& new User(); $result = $this->model->findCount(); $this->assertEqual($result, 4); @@ -1250,6 +1281,7 @@ class ModelTest extends CakeTestCase { } function testFindMagic() { + $this->loadFixtures('User'); $this->model =& new User(); $result = $this->model->findByUser('mariano'); @@ -1262,6 +1294,7 @@ class ModelTest extends CakeTestCase { } function testRead() { + $this->loadFixtures('User', 'Article'); $this->model =& new User(); $result = $this->model->read(); @@ -1298,6 +1331,7 @@ class ModelTest extends CakeTestCase { } function testRecursiveRead() { + $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Featured', 'ArticleFeatured'); $this->model =& new User(); $result = $this->model->bindModel(array('hasMany' => array('Article')), false); @@ -1332,17 +1366,14 @@ class ModelTest extends CakeTestCase { } function testRecursiveFindAll() { + $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Attachment', 'ArticleFeatured', 'Featured', 'Category'); $this->model =& new Article(); $result = $this->model->findAll(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' - ), + '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'), @@ -1355,12 +1386,8 @@ class ModelTest extends CakeTestCase { ) ), 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' - ), + '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() ) @@ -1373,30 +1400,18 @@ class ModelTest extends CakeTestCase { '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' - ), + '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' - ) + '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' - ), + '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 ) ), @@ -1422,101 +1437,32 @@ class ModelTest extends CakeTestCase { $this->Featured->ArticleFeatured->unbindModel(array( 'hasMany' => array('Attachment', 'Comment'), - 'hasAndBelongsToMany'=>array('Tag')) + 'hasAndBelongsToMany' => array('Tag')) ); $orderBy = 'ArticleFeatured.id ASC'; $result = $this->Featured->findAll(null, null, $orderBy, 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' - ), + 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' - ), + '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' - ) + '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); } function testRecursiveFindAllWithLimit() { + $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment'); $this->model =& new Article(); $this->model->hasMany['Comment']['limit'] = 2; @@ -1524,12 +1470,8 @@ class ModelTest extends CakeTestCase { $result = $this->model->findAll(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' - ), + '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'), @@ -1540,12 +1482,8 @@ class ModelTest extends CakeTestCase { ) ), 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' - ), + '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() ) @@ -1557,24 +1495,14 @@ class ModelTest extends CakeTestCase { $result = $this->model->findAll(array('Article.user_id' => 3), null, null, null, 1, 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' - ), + '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' - ) + '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( @@ -1587,6 +1515,7 @@ class ModelTest extends CakeTestCase { } function testAssociationAfterFind() { + $this->loadFixtures('Post', 'Author'); $this->model =& new Post(); $result = $this->model->findAll(); $expected = array( @@ -1754,11 +1683,7 @@ class ModelTest extends CakeTestCase { $this->assertFalse($result); $this->model->validate = array( - 'number' => array( - 'rule' => 'validateNumber', - 'min' => 3, - 'max' => 5 - ), + 'number' => array('rule' => 'validateNumber', 'min' => 3, 'max' => 5), 'title' => array('allowEmpty' => false, 'rule' => VALID_NOT_EMPTY) ); @@ -1787,11 +1712,7 @@ class ModelTest extends CakeTestCase { $this->assertTrue($result); $this->model->validate = array( - 'number' => array( - 'rule' => 'validateNumber', - 'min' => 5, - 'max' => 10 - ), + 'number' => array('rule' => 'validateNumber', 'min' => 5, 'max' => 10), 'title' => array('allowEmpty' => false, 'rule' => VALID_NOT_EMPTY) ); @@ -1829,9 +1750,7 @@ class ModelTest extends CakeTestCase { $result = $this->model->validates(); $this->assertTrue($result); - $this->model->validate = array( - 'title' => array('allowEmpty' => true, 'rule' => 'validateTitle') - ); + $this->model->validate = array('title' => array('allowEmpty' => true, 'rule' => 'validateTitle')); $data = array('TestValidate' => array('title' => '')); $result = $this->model->create($data); @@ -1855,6 +1774,7 @@ class ModelTest extends CakeTestCase { } function testSaveField() { + $this->loadFixtures('Article'); $this->model =& new Article(); $this->model->id = 1; @@ -1897,6 +1817,7 @@ class ModelTest extends CakeTestCase { } function testSaveWithCreate() { + $this->loadFixtures('User', 'Article', 'User', 'Comment', 'Tag', 'ArticlesTag', 'Attachment'); $this->model =& new User(); $data = array('User' => array('user' => 'user', 'password' => '')); @@ -1938,12 +1859,8 @@ class ModelTest extends CakeTestCase { $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) - ) + '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)) ); $this->model->create(); $result = $this->model->create() && $this->model->save($data); @@ -1952,12 +1869,8 @@ class ModelTest extends CakeTestCase { $this->model->recursive = 2; $result = $this->model->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' - ), + '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'), @@ -1966,9 +1879,7 @@ class ModelTest extends CakeTestCase { ); $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' - )); + $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 = $this->model->Comment->create() && $this->model->Comment->save($data); $this->assertTrue($result); @@ -1979,24 +1890,14 @@ class ModelTest extends CakeTestCase { $this->model->recursive = 2; $result = $this->model->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' - ), + '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' - ) + '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( @@ -2008,6 +1909,7 @@ class ModelTest extends CakeTestCase { } function testSaveWithSet() { + $this->loadFixtures('Article'); $this->model =& new Article(); // Create record we will be updating later @@ -2020,9 +1922,7 @@ class ModelTest extends CakeTestCase { $this->model->recursive = -1; $result = $this->model->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' - )); + $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 @@ -2042,36 +1942,20 @@ class ModelTest extends CakeTestCase { $this->model->recursive = -1; $result = $this->model->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' - )); + $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 = $this->model->set($data) && $this->model->save(); - - // THIS WORKS, but it just looks awful and should not be needed - // $result = $this->model->set($data) && $this->model->save($data); - - // THIS WORKS, but should not be used for editing since create() uses default DB values for fields I am not editing: - // $result = $this->model->create() && $this->model->save($data); - $this->assertTrue($result); - // And see if it got edited - $this->model->recursive = -1; $result = $this->model->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' - )); + $expected = array('Article' => array('id' => '4', 'user_id' => '1', 'title' => 'Fourth Article - New Title', 'body' => 'Fourth Article Body', 'published' => 'N')); $this->assertEqual($result, $expected); - // Make sure article we created to overlap is still intact - $this->model->recursive = -1; $result = $this->model->read(array('id', 'user_id', 'title', 'body', 'published'), 5); $expected = array('Article' => array( @@ -2079,24 +1963,15 @@ class ModelTest extends CakeTestCase { )); $this->assertEqual($result, $expected); - // Edit new this overlapped article - $data = array('Article' => array('id' => '5', 'title' => 'Fifth Article - New Title 5')); - - $result = $this->model->set($data) && $this->model->save(); + $result = ($this->model->set($data) && $this->model->save()); $this->assertTrue($result); - // Check it's now updated - $this->model->recursive = -1; $result = $this->model->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' - )); + $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); - // And now do a final check on all article titles - $this->model->recursive = -1; $result = $this->model->findAll(null, array('id', 'title')); $expected = array( @@ -2110,6 +1985,7 @@ class ModelTest extends CakeTestCase { } function testSaveFromXml() { + $this->loadFixtures('Article'); if (!class_exists('Xml')) { uses('xml'); } @@ -2122,16 +1998,13 @@ class ModelTest extends CakeTestCase { } function testSaveHabtm() { + $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag'); $this->model =& new Article(); $result = $this->model->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' - ), + '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') @@ -2156,15 +2029,10 @@ class ModelTest extends CakeTestCase { $result = $this->model->save(); $this->assertTrue($result); - $this->model->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); + $this->model->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); $result = $this->model->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' - ), + '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') @@ -2172,30 +2040,17 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - // Setting just parent ID - - $data = array( - 'Article' => array('id' => '2' ), - 'Tag' => array( - 'Tag' => array( 2, 3 ) - ) - ); - + $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3))); $result = $this->model->set($data); $this->assertTrue($result); $result = $this->model->save(); $this->assertTrue($result); - $this->model->unbindModel(array( - 'belongsTo' => array('User'), - 'hasMany' => array('Comment') - )); + $this->model->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment'))); $result = $this->model->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' - ), + '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') @@ -2203,13 +2058,7 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - // Setting no parent data - - $data = array( - 'Tag' => array( - 'Tag' => array( 1, 2, 3 ) - ) - ); + $data = array('Tag' => array('Tag' => array(1, 2, 3))); $result = $this->model->set($data); $this->assertTrue($result); @@ -2223,9 +2072,7 @@ class ModelTest extends CakeTestCase { )); $result = $this->model->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' - ), + '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'), @@ -2234,11 +2081,7 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - $data = array( - 'Tag' => array( - 'Tag' => array( ) - ) - ); + $data = array('Tag' => array('Tag' => array())); $result = $this->model->set($data); $this->assertTrue($result); @@ -2425,6 +2268,7 @@ class ModelTest extends CakeTestCase { } function testSaveAll() { + $this->loadFixtures('Post', 'Author'); $this->model =& new Post(); $result = $this->model->find('all'); @@ -2463,6 +2307,7 @@ class ModelTest extends CakeTestCase { } function testSaveWithCounterCache() { + $this->loadFixtures('Syfile', 'Item'); $this->model =& new Syfile(); $this->model2 =& new Item(); @@ -2479,6 +2324,7 @@ class ModelTest extends CakeTestCase { } function testDel() { + $this->loadFixtures('Article'); $this->model =& new Article(); $result = $this->model->del(2); @@ -2510,10 +2356,9 @@ class ModelTest extends CakeTestCase { } function testDeleteAll() { + $this->loadFixtures('Article'); $this->model =& new Article(); - // Add some more articles - $data = array('Article' => array('user_id' => 2, 'id' => 4, 'title' => 'Fourth Article', 'published' => 'N')); $result = $this->model->set($data) && $this->model->save(); $this->assertTrue($result); @@ -2538,8 +2383,6 @@ class ModelTest extends CakeTestCase { ); $this->assertEqual($result, $expected); - // Delete with conditions - $result = $this->model->deleteAll(array('Article.published' => 'N')); $this->assertTrue($result); @@ -2566,6 +2409,7 @@ class ModelTest extends CakeTestCase { } function testRecursiveDel() { + $this->loadFixtures('Article', 'Comment', 'Attachment'); $this->model =& new Article(); $result = $this->model->del(2); @@ -2595,6 +2439,7 @@ class ModelTest extends CakeTestCase { } function testDependentExclusiveDelete() { + $this->loadFixtures('Article', 'Comment'); $this->model =& new Article10(); $result = $this->model->find('all'); @@ -2607,6 +2452,7 @@ class ModelTest extends CakeTestCase { } function testFindAllThreaded() { + $this->loadFixtures('Category'); $this->model =& new Category(); $result = $this->model->findAllThreaded(); @@ -2722,6 +2568,7 @@ class ModelTest extends CakeTestCase { } function testFindNeighbours() { + $this->loadFixtures('User', 'Article'); $this->model =& new Article(); $result = $this->model->findNeighbours(null, 'Article.id', '2'); @@ -2741,6 +2588,7 @@ class ModelTest extends CakeTestCase { } function testFindCombinedRelations() { + $this->loadFixtures('Apple', 'Sample'); $this->model =& new Apple(); $result = $this->model->findAll(); @@ -2787,6 +2635,7 @@ class ModelTest extends CakeTestCase { } function testSaveEmpty() { + $this->loadFixtures('Thread'); $this->model =& new Thread(); $data = array(); $expected = $this->model->save($data); @@ -2810,8 +2659,6 @@ class ModelTest extends CakeTestCase { function testMultipleValidation() { $this->model =& new ValidationTest(); - - } function testLoadModelSecondIteration() { @@ -2826,6 +2673,7 @@ class ModelTest extends CakeTestCase { } function testRecursiveUnbind() { + $this->loadFixtures('Apple', 'Sample'); $this->model =& new Apple(); $this->model->recursive = 2; @@ -3257,6 +3105,7 @@ class ModelTest extends CakeTestCase { } function testSelfAssociationAfterFind() { + $this->loadFixtures('Apple'); $afterFindModel = new NodeAfterFind(); $afterFindModel->recursive = 3; $afterFindData = $afterFindModel->findAll(); @@ -3283,6 +3132,7 @@ class ModelTest extends CakeTestCase { } function testDeconstructFields() { + $this->loadFixtures('Apple'); $this->model =& new Apple(); $data['Apple']['created']['year'] = ''; diff --git a/cake/tests/lib/cake_test_case.php b/cake/tests/lib/cake_test_case.php index 87ca10d13..1f3cf9f0a 100644 --- a/cake/tests/lib/cake_test_case.php +++ b/cake/tests/lib/cake_test_case.php @@ -74,6 +74,21 @@ class CakeTestCase extends UnitTestCase { */ var $methods = array('start', 'end', 'startcase', 'endcase', 'starttest', 'endtest'); var $__truncated = true; +/** + * By default, all fixtures attached to this class will be truncated and reloaded after each test. + * Set this to false to handle manually + * + * @var array + * @access public + */ + var $autoFixtures = true; +/** + * Maps fixture class names to fixture identifiers as included in CakeTestCase::$fixtures + * + * @var array + * @access protected + */ + var $_fixtureClassMap = array(); /** * Called when a test case (group of methods) is about to start (to be overriden when needed.) * @@ -153,7 +168,7 @@ class CakeTestCase extends UnitTestCase { $fixture->init(); - $createFixture = $fixture->create(); + $createFixture = $fixture->create($this->db); $insertsFixture = $fixture->insert(); $dropFixture = $fixture->drop(); @@ -309,17 +324,9 @@ class CakeTestCase extends UnitTestCase { } // Create records - if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end')) && $this->__truncated) { + if (isset($this->_fixtures) && isset($this->db) && !in_array(low($method), array('start', 'end')) && $this->__truncated && $this->autoFixtures == true) { foreach ($this->_fixtures as $fixture) { - $inserts = $fixture->insert(); - - if (isset($inserts) && !empty($inserts)) { - foreach ($inserts as $query) { - if (isset($query) && $query !== false) { - $this->db->execute($query); - } - } - } + $inserts = $fixture->insert($this->db); } } @@ -335,10 +342,7 @@ class CakeTestCase extends UnitTestCase { function start() { if (isset($this->_fixtures) && isset($this->db)) { foreach ($this->_fixtures as $fixture) { - $query = $fixture->create(); - if (isset($query) && $query !== false) { - $this->db->execute($query); - } + $fixture->create($this->db); } } } @@ -350,10 +354,7 @@ class CakeTestCase extends UnitTestCase { function end() { if (isset($this->_fixtures) && isset($this->db)) { foreach (array_reverse($this->_fixtures) as $fixture) { - $query = $fixture->drop(); - if (isset($query) && $query !== false) { - $this->db->execute($query); - } + $fixture->drop($this->db); } } } @@ -393,6 +394,25 @@ class CakeTestCase extends UnitTestCase { $methods = am(am(array('start', 'startCase'), $methods), array('endCase', 'end')); return $methods; } +/** + * Chooses which fixtures to load for a given test + * + * @param string $fixture Each parameter is a model name that corresponds to a fixture, i.e. 'Post', 'Author', etc. + * @access public + * @see CakeTestCase::$autoFixtures + */ + function loadFixtures() { + $args = func_get_args(); + foreach ($args as $class) { + if (isset($this->_fixtureClassMap[$class])) { + $fixture = $this->_fixtures[$this->_fixtureClassMap[$class]]; + $this->db->truncate($fixture->table); + $fixture->insert($this->db); + } else { + trigger_error("Non-existent fixture class {$class} referenced in test", E_USER_WARNING); + } + } + } /** * Initialize DB connection. * @@ -417,10 +437,9 @@ class CakeTestCase extends UnitTestCase { // Try for default DB if (!$testDbAvailable) { $db =& ConnectionManager::getDataSource('default'); + $db->config['prefix'] = 'test_suite_'; } - $db->config['prefix'] = 'test_suite_'; - ConnectionManager::create('test_suite', $db->config); // Get db connection $this->db =& ConnectionManager::getDataSource('test_suite'); @@ -438,7 +457,7 @@ class CakeTestCase extends UnitTestCase { } if (!is_array($this->fixtures)) { - $this->fixtures = array( $this->fixtures ); + $this->fixtures = array_map('trim', explode(',', $this->fixtures)); } $this->_fixtures = array(); @@ -474,10 +493,9 @@ class CakeTestCase extends UnitTestCase { if (isset($fixtureFile)) { require_once($fixtureFile); - $fixtureClass = Inflector::camelize($fixture) . 'Fixture'; - $this->_fixtures[$this->fixtures[$index]] =& new $fixtureClass($this->db); + $this->_fixtureClassMap[Inflector::camelize($fixture)] = $this->fixtures[$index]; } } diff --git a/cake/tests/lib/cake_test_fixture.php b/cake/tests/lib/cake_test_fixture.php index 78983705b..f373f5806 100644 --- a/cake/tests/lib/cake_test_fixture.php +++ b/cake/tests/lib/cake_test_fixture.php @@ -42,7 +42,6 @@ class CakeTestFixture extends Object { * @access public */ function __construct(&$db) { - $this->db =& $db; $this->init(); if(!class_exists('cakeschema')) { uses('model' . DS .'schema'); @@ -59,13 +58,12 @@ class CakeTestFixture extends Object { if (is_string($this->import) || is_array($this->import) && isset($this->import['model'])) { $import = am(array('records' => false), ife(is_array($this->import), $this->import, array())); - $import['model'] = ife(is_array($this->import), $this->import['model'], $this->import); } elseif (isset($this->import['table'])) { $import = am(array('connection' => 'default', 'records' => false), $this->import); } - if (isset($import['model']) && (class_exists($import['model']) || loadModel($import['model']))) { + if (isset($import['model']) && (class_exists($import['model']) || App::import('Model', $import['model']))) { $model =& new $import['model']; $db =& ConnectionManager::getDataSource($model->useDbConfig); $db->cacheSources = false; @@ -96,7 +94,6 @@ class CakeTestFixture extends Object { foreach ($query['fields'] as $index => $field) { $query['fields'][$index] = $db->name($query['alias']) . '.' . $db->name($field); } - $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias); if ($records !== false && !empty($records)) { @@ -108,11 +105,9 @@ class CakeTestFixture extends Object { if (!isset($this->table)) { $this->table = Inflector::underscore(Inflector::pluralize($this->name)); } - if (!isset($this->primaryKey) && isset($this->fields['id'])) { $this->primaryKey = 'id'; } - if (isset($this->fields)) { foreach ($this->fields as $index => $field) { if (empty($field['default'])) { @@ -124,74 +119,48 @@ class CakeTestFixture extends Object { /** * Run before all tests execute, should return SQL statement to create table for this fixture. * - * @return string SQL CREATE TABLE statement, false if not applicable. - * + * @param object $db An instance of the database object used to create the fixture table + * @return boolean True on success, false on failure * @access public */ - function create() { - if (!isset($this->_create)) { - if (!isset($this->fields) || empty($this->fields)) { - return null; - } - $this->Schema->_build(array($this->table => $this->fields)); - $this->_create = $this->db->createSchema($this->Schema); + function create(&$db) { + if (!isset($this->fields) || empty($this->fields)) { + return false; } - return $this->_create; + $this->Schema->_build(array($this->table => $this->fields)); + return ($db->execute($db->createSchema($this->Schema)) !== false); } /** * Run after all tests executed, should return SQL statement to drop table for this fixture. * - * @return string SQL DROP TABLE statement, false if not applicable. - * + * @param object $db An instance of the database object used to create the fixture table + * @return boolean True on success, false on failure * @access public */ - function drop() { - if (!isset($this->_drop)) { - $this->Schema->_build(array($this->table => $this->fields)); - $this->_drop = $this->db->dropSchema($this->Schema); - } - return $this->_drop; + function drop(&$db) { + $this->Schema->_build(array($this->table => $this->fields)); + return ($db->execute($db->dropSchema($this->Schema)) !== false); } /** * Run before each tests is executed, should return a set of SQL statements to insert records for the table of this fixture. * - * @return array SQL INSERT statements, empty array if not applicable. - * + * @param object $db An instance of the database into which the records will be inserted + * @return boolean on success or if there are no records to insert, or false on failure * @access public */ - function insert() { + function insert(&$db) { if (!isset($this->_insert)) { - $inserts = array(); + $values = array(); if (isset($this->records) && !empty($this->records)) { foreach ($this->records as $record) { $fields = array_keys($record); - $values = array_values($record); - - $insert = 'INSERT INTO ' . $this->db->name($this->db->config['prefix'] . $this->table) . '('; - - foreach ($fields as $field) { - $insert .= $this->db->name($field) . ','; - } - $insert = substr($insert, 0, -1); - - $insert .= ') VALUES ('; - - foreach ($values as $values) { - $insert .= $this->db->value($values) . ','; - } - $insert = substr($insert, 0, -1); - - $insert .= ')'; - - $inserts[] = $insert; + $values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')'; } + return $db->insertMulti($this->table, $fields, $values); } - - $this->_insert = $inserts; + return true; } - - return $this->_insert; } } ?> \ No newline at end of file