From ae4ebec603e0242dd4322ee35c9881e0dd62b250 Mon Sep 17 00:00:00 2001
From: gwoo <gwoo@cakephp.org>
Date: Wed, 29 Aug 2007 08:32:18 +0000
Subject: [PATCH] updating schema and tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5605 3807eeeb-6ff5-0310-8944-8be069107fe0
---
 cake/libs/model/schema.php                  |  52 ++++++----
 cake/tests/cases/libs/model/schema.test.php | 105 ++++++++++----------
 cake/tests/lib/cake_test_case.php           |  12 +--
 3 files changed, 92 insertions(+), 77 deletions(-)

diff --git a/cake/libs/model/schema.php b/cake/libs/model/schema.php
index e16bacbaa..c233b61e7 100644
--- a/cake/libs/model/schema.php
+++ b/cake/libs/model/schema.php
@@ -160,6 +160,10 @@ class CakeSchema extends Object {
 		));
 		$db =& ConnectionManager::getDataSource($connection);
 		$currentTables = array_flip($db->sources());
+		$prefix = null;
+		if(isset($db->config['prefix'])) {
+			$prefix = $db->config['prefix'];
+		}
 		if (empty($models)) {
 			$models = Configure::listObjects('model');
 		}
@@ -179,23 +183,30 @@ class CakeSchema extends Object {
 			if(class_exists(low($model))) {
 				$Object =& new $model();
 				$Object->setDataSource($connection);
-				if (is_object($Object) && isset($currentTables[$Object->table])) {
-					if(empty($tables[$Object->table])) {
-						$tables[$Object->table] = $this->__columns($Object);
-						$tables[$Object->table]['indexes'] = $db->index($Object);
-						unset($currentTables[$Object->table]);
-					}
-					if(!empty($Object->hasAndBelongsToMany)) {
-						foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
-							if (isset($assocData['with'])) {
-								$class = $assocData['with'];
-							} elseif ($assocData['_with']) {
-								$class = $assocData['_with'];
-							}
-							if (is_object($Object->$class) && isset($currentTables[$Object->$class->table])) {
-								$tables[$Object->$class->table] = $this->__columns($Object->$class);
-								$tables[$Object->$class->table]['indexes'] = $db->index($Object->$class);
-								unset($currentTables[$Object->$class->table]);
+				$table = $db->fullTableName($Object, false);
+				if (is_object($Object)) {
+					$table = $db->fullTableName($Object, false);
+					if(isset($currentTables[$table])) {
+						if(empty($tables[$Object->table])) {
+							$tables[$Object->table] = $this->__columns($Object);
+							$tables[$Object->table]['indexes'] = $db->index($Object);
+							unset($currentTables[$table]);
+						}
+						if(!empty($Object->hasAndBelongsToMany)) {
+							foreach($Object->hasAndBelongsToMany as $Assoc => $assocData) {
+								if (isset($assocData['with'])) {
+									$class = $assocData['with'];
+								} elseif ($assocData['_with']) {
+									$class = $assocData['_with'];
+								}
+								if (is_object($Object->$class)) { 
+									$table = $db->fullTableName($Object->$class, false);
+									if(isset($currentTables[$table])) {
+										$tables[$Object->$class->table] = $this->__columns($Object->$class);
+										$tables[$Object->$class->table]['indexes'] = $db->index($Object->$class);
+										unset($currentTables[$table]);
+									}
+								}
 							}
 						}
 					}
@@ -205,6 +216,9 @@ class CakeSchema extends Object {
 
 		if(!empty($currentTables)) {
 			foreach(array_flip($currentTables) as $table) {
+				if($prefix) {
+					$table = str_replace($prefix, '', $table);
+				}
 				$Object = new AppModel(array('name'=> Inflector::classify($table), 'table'=> $table, 'ds'=> $connection));
 				$tables['missing'][$table] = $this->__columns($Object);
 				$tables['missing'][$table]['indexes'] = $db->index($Object);
@@ -341,7 +355,7 @@ class CakeSchema extends Object {
 			}
 			foreach ($fields as $field => $value) {
 				if (isset($old[$table][$field])) {
-					$diff = array_diff($value, $old[$table][$field]);
+					$diff = array_diff_assoc($value, $old[$table][$field]);
 					if (!empty($diff)) {
 						$tables[$table]['change'][$field] = am($old[$table][$field], $diff);
 					}
@@ -419,7 +433,7 @@ class CakeSchema extends Object {
 				unset($value['limit']);
 			}
 
-			if (empty($value['default'])) {
+			if (isset($value['default']) && $value['default'] != 0) {
 				unset($value['default']);
 			}
 			if (empty($value['length'])) {
diff --git a/cake/tests/cases/libs/model/schema.test.php b/cake/tests/cases/libs/model/schema.test.php
index 50c7b03e9..2ce4bdb32 100644
--- a/cake/tests/cases/libs/model/schema.test.php
+++ b/cake/tests/cases/libs/model/schema.test.php
@@ -37,30 +37,29 @@ class MyAppSchema extends CakeSchema {
 	var $name = 'MyApp';
 
 	var $connection = 'test_suite';
-	
+
 	var $comments = array(
-			'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
-			'post_id' => array('type'=>'integer', 'null' => false),
-			'user_id' => array('type'=>'integer', 'null' => false),
-			'title' => array('type'=>'string', 'null' => false, 'length' => 100),
-			'comment' => array('type'=>'text', 'null' => false),
+			'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
+			'post_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
+			'user_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
+			'title' => array('type'=>'string', 'null' => false, 'length'=> 100),
+			'comment' => array('type'=>'text', 'null' => false, 'default'=> null),
 			'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
-			'created' => array('type'=>'datetime', 'null' => true),
-			'updated' => array('type'=>'datetime', 'null' => true),
+			'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
+			'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
 			'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
 		);
-	
+
 	var $posts = array(
-			'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
-			'author_id' => array('type'=>'integer', 'null' => false, 'default' => ''),
-			'title' => array('type'=>'string', 'null' => false, 'default' => 'Title'),
-			'summary' => array('type'=>'text', 'null' => true),
-			'body' => array('type'=>'text', 'null' => true),
+			'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
+			'author_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
+			'title' => array('type'=>'string', 'null' => false, 'default'=> 'Title'),
+			'body' => array('type'=>'text', 'null' => true, 'default'=> null),
+			'summary'=>array('type'=> 'text', 'null'=> true),
 			'published' => array('type'=>'string', 'null' => true, 'default' => 'Y', 'length' => 1),
-			'created' => array('type'=>'datetime', 'null' => true),
-			'updated' => array('type'=>'datetime', 'null' => true),
+			'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
+			'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
 			'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
-			
 		);
 
 	function setup($version) {
@@ -72,42 +71,42 @@ class MyAppSchema extends CakeSchema {
 class TestAppSchema extends CakeSchema {
 
 	var $name = 'MyApp';
-	
+
 	var $comments = array(
-			'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
-			'article_id' => array('type'=>'integer', 'null' => false),
-			'user_id' => array('type'=>'integer', 'null' => false),
-			'comment' => array('type'=>'text', 'null' => true),
+			'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
+			'article_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
+			'user_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
+			'comment' => array('type'=>'text', 'null' => true, 'default'=> null),
 			'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
-			'created' => array('type'=>'datetime', 'null' => true),
-			'updated' => array('type'=>'datetime', 'null' => true),
+			'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
+			'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
 			'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
 		);
-		
+
 	var $posts = array(
-			'id' => array('type'=>'integer', 'null' => false, 'key' => 'primary', 'extra'=> 'auto_increment'),
-			'author_id' => array('type'=>'integer', 'null' => false),
-			'title' => array('type'=>'string', 'null' => false),
-			'body' => array('type'=>'text', 'null' => true),
+			'id' => array('type'=>'integer', 'null' => false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
+			'author_id' => array('type'=>'integer', 'null' => false, 'default'=> 0),
+			'title' => array('type'=>'string', 'null' => false, 'default'=> null),
+			'body' => array('type'=>'text', 'null' => true, 'default'=> null),
 			'published' => array('type'=>'string', 'null' => true, 'default' => 'N', 'length' => 1),
-			'created' => array('type'=>'datetime', 'null' => true),
-			'updated' => array('type'=>'datetime', 'null' => true),
+			'created' => array('type'=>'datetime', 'null' => true, 'default'=> null),
+			'updated' => array('type'=>'datetime', 'null' => true, 'default'=> null),
 			'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
 		);
-		
+
 	var $posts_tags = array(
-		'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
-		'tag_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
+		'post_id' => array('type' => 'integer', 'null' => false, 'default'=> 0, 'key' => 'primary'),
+		'tag_id' => array('type' => 'integer', 'null' => false, 'default'=> 0, 'key' => 'primary'),
 		'indexes' => array('UNIQUE_TAG' => array('column'=> array('post_id', 'tag_id'), 'unique'=>1))
 	);
-		
+
 	var $tags = array(
-		'id' => array('type' => 'integer', 'null'=> false, 'key' => 'primary', 'extra'=> 'auto_increment'),
-		'tag' => array('type' => 'string', 'null' => false),
-		'created' => array('type' => 'datetime', 'null' => true),
-		'updated' => array('type' => 'datetime', 'null' => true),
+		'id' => array('type' => 'integer', 'null'=> false, 'default'=> 0, 'key' => 'primary', 'extra'=> 'auto_increment'),
+		'tag' => array('type' => 'string', 'null' => false, 'default'=> null),
+		'created' => array('type' => 'datetime', 'null' => true, 'default'=> null),
+		'updated' => array('type' => 'datetime', 'null' => true, 'default'=> null),
 		'indexes' => array('PRIMARY'=>array('column'=>'id', 'unique' => true)),
-	);	
+	);
 
 
 	function setup($version) {
@@ -127,7 +126,7 @@ class SchemaPost extends CakeTestModel {
 	var $useTable = 'posts';
 	var $hasMany = array('SchemaComment');
 	var $hasAndBelongsToMany = array('SchemaTag');
-	
+
 }
 /**
  * Short description for class.
@@ -151,6 +150,10 @@ class SchemaTag extends CakeTestModel {
 	var $useTable = 'tags';
 	var $hasAndBelongsToMany = array('SchemaPost');
 }
+class PostsTag extends CakeTestModel {
+	var $name = 'PostsTag';
+	var $useTable = 'posts_tags';
+}
 /**
  * Short description for class.
  *
@@ -159,7 +162,7 @@ class SchemaTag extends CakeTestModel {
  */
 class CakeSchemaTest extends CakeTestCase {
 
-	var $fixtures = array('core.post', 'core.comment', 'core.author', 'core.tag', 'core.posts_tag');
+	var $fixtures = array('core.post', 'core.tag', 'core.posts_tag', 'core.comment');
 
 	function setUp() {
 		$this->Schema = new TestAppSchema();
@@ -171,9 +174,9 @@ class CakeSchemaTest extends CakeTestCase {
 		unset($read['tables']['missing']);
 		$this->assertEqual($read['tables'], $this->Schema->tables);
 	}
-	
+
 	function testSchemaWrite() {
-		
+
 		$write = $this->Schema->write(array('name'=>'MyOtherApp', 'tables'=> $this->Schema->tables, 'path'=> TMP . 'tests'));
 		$file = file_get_contents(TMP . 'tests' . DS .'schema.php');
 		$this->assertEqual($write, $file);
@@ -181,22 +184,22 @@ class CakeSchemaTest extends CakeTestCase {
 		require_once( TMP . 'tests' . DS .'schema.php');
 		$OtherSchema = new MyOtherAppSchema();
 		$this->assertEqual($this->Schema->tables, $OtherSchema->tables);
-		
+
 	}
 
 	function testSchemaComparison() {
 		$New = new MyAppSchema();
 		$compare = $New->compare($this->Schema);
 		$expected = array(
+					'comments'=> array(
+						'add'=>array('post_id'=>array('type'=> 'integer', 'null'=> false, 'default'=> 0), 'title'=>array('type'=> 'string', 'null'=> false, 'length'=> 100)),
+						'drop'=>array('article_id'=>array('type'=> 'integer', 'null'=> false, 'default'=> 0)),
+						'change'=>array('comment'=>array('type'=>'text', 'null'=> false, 'default'=> null))
+
+						),
 					'posts'=> array(
 						'add'=> array('summary'=>array('type'=> 'text', 'null'=> 1)),
 						'change'=> array('title'=>array('type'=>'string', 'null'=> false, 'default'=> 'Title'), 'published'=>array('type'=>'string', 'null'=> true, 'default'=>'Y', 'length'=> '1')),
-						),
-					'comments'=> array(
-						'add'=>array('post_id'=>array('type'=> 'integer', 'null'=> false), 'title'=>array('type'=> 'string', 'null'=> false, 'length'=> 100)),
-						'drop'=>array('article_id'=>array('type'=> 'integer', 'null'=> false)),
-						'change'=>array('comment'=>array('type'=>'text', 'null'=> false))
-
 						),
 					);
 
diff --git a/cake/tests/lib/cake_test_case.php b/cake/tests/lib/cake_test_case.php
index 6f7d8dd68..90bdb17e6 100644
--- a/cake/tests/lib/cake_test_case.php
+++ b/cake/tests/lib/cake_test_case.php
@@ -418,13 +418,9 @@ class CakeTestCase extends UnitTestCase {
 			$db =& ConnectionManager::getDataSource('default');
 		}
 
-		// Add test prefix
-		$config = $db->config;
-		$config['prefix'] .= 'test_suite_';
-
-		// Set up db connection
-		ConnectionManager::create('test_suite', $config);
+		$db->config['prefix'] = 'test_suite_';
 
+		ConnectionManager::create('test_suite', $db->config);
 		// Get db connection
 		$this->db =& ConnectionManager::getDataSource('test_suite');
 		$this->db->cacheSources  = false;
@@ -457,11 +453,13 @@ class CakeTestCase extends UnitTestCase {
 			} elseif (strpos($fixture, 'app.') === 0) {
 				$fixture = substr($fixture, strlen('app.'));
 				$fixturePaths = array(
-					APP . 'tests' . DS . 'fixtures'
+					TESTS . DS . 'fixtures',
+					VENDORS . 'tests' . DS . 'fixtures'
 				);
 			} else {
 				$fixturePaths = array(
 					TESTS . 'fixtures',
+					VENDORS . 'tests' . DS . 'fixtures',
 					CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
 				);
 			}