From 67d7579fdaf630b95acf1e81ed3605837fd7789d Mon Sep 17 00:00:00 2001
From: nate <nate@cakephp.org>
Date: Sun, 3 Feb 2008 06:40:15 +0000
Subject: [PATCH] Adding tests for auto-constructing hasAndBelongsToMany
 associations, disproves #3859

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6431 3807eeeb-6ff5-0310-8944-8be069107fe0
---
 cake/tests/cases/libs/model/model.test.php | 86 +++++++++++++++-------
 1 file changed, 61 insertions(+), 25 deletions(-)

diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php
index b8d725a37..f5e8afb25 100644
--- a/cake/tests/cases/libs/model/model.test.php
+++ b/cake/tests/cases/libs/model/model.test.php
@@ -42,15 +42,14 @@ class Test extends Model {
 	var $useTable = false;
 	var $name = 'Test';
 
-	function schema() {
-		return array(
-			'id'=> array('type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8', 'key'=>'primary'),
-			'name'=> array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
-			'email'=> array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
-			'notes'=> array('type' => 'text', 'null' => '1', 'default' => 'write some notes here', 'length' => ''),
-			'created'=> array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
-			'updated'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null));
-	}
+	var $_schema = array(
+		'id'=> array('type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8', 'key'=>'primary'),
+		'name'=> array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
+		'email'=> array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
+		'notes'=> array('type' => 'text', 'null' => '1', 'default' => 'write some notes here', 'length' => ''),
+		'created'=> array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
+		'updated'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
+	);
 }
 
 /**
@@ -63,6 +62,15 @@ class TestValidate extends Model {
 	var $useTable = false;
 	var $name = 'TestValidate';
 
+	var $_schema = array(
+		'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
+		'title' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
+		'body' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => ''),
+		'number' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
+		'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
+		'modified' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
+	);
+
 	function validateNumber($value, $options) {
 		$options = am(array('min' => 0, 'max' => 100), $options);
 		$valid = ($value['number'] >= $options['min'] && $value['number'] <= $options['max']);
@@ -70,24 +78,9 @@ class TestValidate extends Model {
 	}
 
 	function validateTitle($value) {
-		if (!empty($value) && strpos(low($value['title']), 'title-') === 0) {
-			return true;
-		}
-		return false;
-	}
-
-	function schema() {
-		return array(
-			'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
-			'title' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
-			'body' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => ''),
-			'number' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
-			'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
-			'modified' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
-		);
+		return (!empty($value) && strpos(low($value['title']), 'title-') === 0);
 	}
 }
-
 /**
  * Short description for class.
  *
@@ -464,6 +457,34 @@ class JoinC extends CakeTestModel {
 	var $name = 'JoinC';
 	var $hasAndBelongsToMany = array('JoinA');
 }
+/**
+ * Short description for class.
+ *
+ * @package		cake.tests
+ * @subpackage	cake.tests.cases.libs.model
+ */
+class AssociationTest1 extends CakeTestModel {
+	var $useTable = 'join_as';
+	var $name = 'AssociationTest1';
+
+	var $hasAndBelongsToMany = array('AssociationTest2' => array(
+		'unique' => false, 'joinTable' => 'join_as_join_bs', 'foreignKey' => false
+	));
+}
+/**
+ * Short description for class.
+ *
+ * @package		cake.tests
+ * @subpackage	cake.tests.cases.libs.model
+ */
+class AssociationTest2 extends CakeTestModel {
+	var $useTable = 'join_bs';
+	var $name = 'AssociationTest2';
+
+	var $hasAndBelongsToMany = array('AssociationTest1' => array(
+		'unique' => false, 'joinTable' => 'join_as_join_bs'
+	));
+}
 /**
  * Short description for class.
  *
@@ -496,6 +517,21 @@ class ModelTest extends CakeTestCase {
 		Configure::write('debug', $this->debug);
 	}
 
+	function testAutoConstructAssociations() {
+		$this->loadFixtures('User');
+		$this->model =& new AssociationTest1();
+
+		$result = $this->model->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);
+	}
+
 	function testMultipleBelongsToWithSameClass() {
 		$this->loadFixtures('DeviceType', 'DeviceTypeCategory', 'FeatureSet', 'ExteriorTypeCategory', 'Document', 'Device', 'DocumentDirectory');
 		$this->DeviceType =& new DeviceType();