Updating core test cases and groups

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4625 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-03-16 23:35:25 +00:00
parent 7ff6ac5bcb
commit 116b330503
3 changed files with 758 additions and 20 deletions

View file

@ -90,9 +90,146 @@ class TestModel2 extends Model {
*/
class TestModel3 extends Model {
var $name = 'TestModel2';
var $name = 'TestModel3';
var $useTable = false;
}
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model.datasources
*/
class TestModel4 extends Model {
var $name = 'TestModel4';
var $table = 'test_model4';
var $useTable = false;
var $belongsTo = array(
'TestModel4Parent' => array(
'className' => 'TestModel4',
'foreignKey' => 'parent_id'
)
);
var $hasOne = array(
'TestModel5' => array(
'className' => 'TestModel5',
'foreignKey' => 'test_model4_id'
)
);
var $hasAndBelongsToMany = array('TestModel7' => array(
'className' => 'TestModel7',
'joinTable' => 'test_model4_test_model7',
'foreignKey' => 'test_model4_id',
'associationForeignKey' => 'test_model7_id'
));
function loadInfo() {
if (!isset($this->_tableInfo)) {
$this->_tableInfo = new Set(array(
array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
));
}
return $this->_tableInfo;
}
}
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model.datasources
*/
class TestModel5 extends Model {
var $name = 'TestModel5';
var $table = 'test_model5';
var $useTable = false;
var $belongsTo = array('TestModel4' => array(
'className' => 'TestModel4',
'foreignKey' => 'test_model4_id'
));
var $hasMany = array('TestModel6' => array(
'className' => 'TestModel6',
'foreignKey' => 'test_model5_id'
));
function loadInfo() {
if (!isset($this->_tableInfo)) {
$this->_tableInfo = new Set(array(
array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
array('name' => 'test_model4_id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
));
}
return $this->_tableInfo;
}
}
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model.datasources
*/
class TestModel6 extends Model {
var $name = 'TestModel6';
var $table = 'test_model6';
var $useTable = false;
var $belongsTo = array('TestModel5' => array(
'className' => 'TestModel5',
'foreignKey' => 'test_model5_id'
));
function loadInfo() {
if (!isset($this->_tableInfo)) {
$this->_tableInfo = new Set(array(
array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
array('name' => 'test_model5_id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
));
}
return $this->_tableInfo;
}
}
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model.datasources
*/
class TestModel7 extends Model {
var $name = 'TestModel7';
var $table = 'test_model7';
var $useTable = false;
function loadInfo() {
if (!isset($this->_tableInfo)) {
$this->_tableInfo = new Set(array(
array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
));
}
return $this->_tableInfo;
}
}
/**
* Short description for class.
*
@ -126,6 +263,440 @@ class DboSourceTest extends UnitTestCase {
$this->db =& new DboTest($config->default);
$this->model = new TestModel();
}
function tearDown() {
unset($this->model);
unset($this->db);
}
function testGenerateAssociationQuerySelfJoin() {
$this->model = new TestModel4();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'belongsTo', 'model'=>'TestModel4Parent');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateSelfAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertTrue($result);
$expected = 'SELECT ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`, `TestModel4Parent`.`id`, `TestModel4Parent`.`name`, `TestModel4Parent`.`created`, `TestModel4Parent`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' LEFT JOIN ';
$expected .= '`test_model4` AS `TestModel4Parent`';
$expected .= ' ON ';
$expected .= '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`';
$this->assertEqual($queryData['selfJoin'][0], $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`, `TestModel4Parent`.`id`, `TestModel4Parent`.`name`, `TestModel4Parent`.`created`, `TestModel4Parent`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' LEFT JOIN ';
$expected .= '`test_model4` AS `TestModel4Parent`';
$expected .= ' ON ';
$expected .= '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`';
$expected .= ' WHERE ';
$expected .= '1 = 1';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQuerySelfJoinWithConditions() {
$this->model = new TestModel4();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'belongsTo', 'model'=>'TestModel4Parent');
$queryData = array('conditions' => array('TestModel4Parent.name' => '!= mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateSelfAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertTrue($result);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`, `TestModel4Parent`.`id`, `TestModel4Parent`.`name`, `TestModel4Parent`.`created`, `TestModel4Parent`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' LEFT JOIN ';
$expected .= '`test_model4` AS `TestModel4Parent`';
$expected .= ' ON ';
$expected .= '`TestModel4`.`parent_id` = `TestModel4Parent`.`id`';
$expected .= ' WHERE ';
$expected .= '(`TestModel4Parent`.`name` != \'mariano\')';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryHasOne() {
$this->model = new TestModel4();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'hasOne', 'model'=>'TestModel5');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertTrue($result);
$expected = ' LEFT JOIN `test_model5` AS `TestModel5` ON `TestModel5`.`test_model4_id` = `TestModel4`.`id`';
$this->assertEqual($queryData['joins'][0], $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`';
$expected .= ', ';
$expected .= '`TestModel5`.`id`, `TestModel5`.`test_model4_id`, `TestModel5`.`name`, `TestModel5`.`created`, `TestModel5`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' LEFT JOIN ';
$expected .= '`test_model5` AS `TestModel5`';
$expected .= ' ON ';
$expected .= '`TestModel5`.`test_model4_id` = `TestModel4`.`id`';
$expected .= ' WHERE ';
$expected .= '( 1 = 1 ) AND ( 1 = 1 )';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryHasOneWithConditions() {
$this->model = new TestModel4();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'hasOne', 'model'=>'TestModel5');
$queryData = array('conditions' => array('TestModel5.name' => '!= mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertTrue($result);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`';
$expected .= ', ';
$expected .= '`TestModel5`.`id`, `TestModel5`.`test_model4_id`, `TestModel5`.`name`, `TestModel5`.`created`, `TestModel5`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' LEFT JOIN ';
$expected .= '`test_model5` AS `TestModel5`';
$expected .= ' ON ';
$expected .= '`TestModel5`.`test_model4_id` = `TestModel4`.`id`';
$expected .= ' WHERE ';
$expected .= '( 1 = 1 )';
$expected .= ' AND ';
$expected .= '(`TestModel5`.`name` != \'mariano\')';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryBelongsTo() {
$this->model = new TestModel5();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'belongsTo', 'model'=>'TestModel4');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertTrue($result);
$expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON `TestModel5`.`test_model4_id` = `TestModel4`.`id`';
$this->assertEqual($queryData['joins'][0], $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel5`.`id`, `TestModel5`.`test_model4_id`, `TestModel5`.`name`, `TestModel5`.`created`, `TestModel5`.`updated`';
$expected .= ', ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model5` AS `TestModel5`';
$expected .= ' LEFT JOIN ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' ON ';
$expected .= '`TestModel5`.`test_model4_id` = `TestModel4`.`id`';
$expected .= ' WHERE ';
$expected .= '( 1 = 1 ) AND ( 1 = 1 )';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryBelongsToWithConditions() {
$this->model = new TestModel5();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'belongsTo', 'model'=>'TestModel4');
$queryData = array('conditions' => array('TestModel5.name' => '!= mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertTrue($result);
$expected = ' LEFT JOIN `test_model4` AS `TestModel4` ON `TestModel5`.`test_model4_id` = `TestModel4`.`id`';
$this->assertEqual($queryData['joins'][0], $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel5`.`id`, `TestModel5`.`test_model4_id`, `TestModel5`.`name`, `TestModel5`.`created`, `TestModel5`.`updated`';
$expected .= ', ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model5` AS `TestModel5`';
$expected .= ' LEFT JOIN ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' ON ';
$expected .= '`TestModel5`.`test_model4_id` = `TestModel4`.`id`';
$expected .= ' WHERE ';
$expected .= '( 1 = 1 ) AND (`TestModel5`.`name` != \'mariano\')';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryHasMany() {
$this->model = new TestModel5();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'hasMany', 'model'=>'TestModel6');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$expected = 'SELECT ';
$expected .= '`TestModel6`.`id`, `TestModel6`.`test_model5_id`, `TestModel6`.`name`, `TestModel6`.`created`, `TestModel6`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model6` AS `TestModel6`';
$expected .= ' WHERE ';
$expected .= '1 = 1';
$expected .= ' AND ';
$expected .= '`TestModel6`.`test_model5_id` = {$__cakeID__$}';
$this->assertEqual($result, $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel5`.`id`, `TestModel5`.`test_model4_id`, `TestModel5`.`name`, `TestModel5`.`created`, `TestModel5`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model5` AS `TestModel5`';
$expected .= ' WHERE ';
$expected .= '1 = 1';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryHasManyWithConditions() {
$this->model = new TestModel5();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'hasMany', 'model'=>'TestModel6');
$queryData = array('conditions' => array('TestModel5.name' => '!= mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$expected = 'SELECT ';
$expected .= '`TestModel6`.`id`, `TestModel6`.`test_model5_id`, `TestModel6`.`name`, `TestModel6`.`created`, `TestModel6`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model6` AS `TestModel6`';
$expected .= ' WHERE ';
$expected .= '1 = 1';
$expected .= ' AND ';
$expected .= '`TestModel6`.`test_model5_id` = {$__cakeID__$}';
$this->assertEqual($result, $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel5`.`id`, `TestModel5`.`test_model4_id`, `TestModel5`.`name`, `TestModel5`.`created`, `TestModel5`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model5` AS `TestModel5`';
$expected .= ' WHERE ';
$expected .= '(`TestModel5`.`name` != \'mariano\')';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryHasAndBelongsToMany() {
$this->model = new TestModel4();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
$queryData = array();
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$expected = 'SELECT ';
$expected .= '`TestModel7`.`id`, `TestModel7`.`name`, `TestModel7`.`created`, `TestModel7`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model7` AS `TestModel7`';
$expected .= ' JOIN ';
$expected .= '`test_model4_test_model7`';
$expected .= ' ON ';
$expected .= '`test_model4_test_model7`.`test_model4_id` = {$__cakeID__$}';
$expected .= ' AND ';
$expected .= '`test_model4_test_model7`.`test_model7_id` = `TestModel7`.`id`';
$expected .= ' WHERE ';
$expected .= '1 = 1 ';
$this->assertEqual($result, $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' WHERE ';
$expected .= '1 = 1';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function testGenerateAssociationQueryHasAndBelongsToManyWithConditions() {
$this->model = new TestModel4();
$this->model->loadInfo();
$this->_buildRelatedModels($this->model);
$binding = array('type'=>'hasAndBelongsToMany', 'model'=>'TestModel7');
$queryData = array('conditions' => array('TestModel4.name' => '!= mariano'));
$resultSet = null;
$null = null;
$params = &$this->_prepareAssociationQuery($this->model, $queryData, $binding);
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$expected = 'SELECT ';
$expected .= '`TestModel7`.`id`, `TestModel7`.`name`, `TestModel7`.`created`, `TestModel7`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model7` AS `TestModel7`';
$expected .= ' JOIN ';
$expected .= '`test_model4_test_model7`';
$expected .= ' ON ';
$expected .= '`test_model4_test_model7`.`test_model4_id` = {$__cakeID__$}';
$expected .= ' AND ';
$expected .= '`test_model4_test_model7`.`test_model7_id` = `TestModel7`.`id`';
$expected .= ' WHERE ';
$expected .= '1 = 1 ';
$this->assertEqual($result, $expected);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$expected = 'SELECT ';
$expected .= '`TestModel4`.`id`, `TestModel4`.`name`, `TestModel4`.`created`, `TestModel4`.`updated`';
$expected .= ' FROM ';
$expected .= '`test_model4` AS `TestModel4`';
$expected .= ' WHERE ';
$expected .= '(`TestModel4`.`name` != \'mariano\')';
$expected .= ' ';
$this->assertEqual($result, $expected);
}
function _buildRelatedModels(&$model) {
foreach($model->__associations as $type) {
foreach($model->{$type} as $assoc => $assocData) {
if (is_string($assocData)) {
$className = $assocData;
} else if (isset($assocData['className'])) {
$className = $assocData['className'];
}
$model->$className = new $className();
$model->$className->loadInfo();
}
}
}
function &_prepareAssociationQuery(&$model, &$queryData, $binding) {
$type = $binding['type'];
$assoc = $binding['model'];
$assocData = $model->{$type}[$assoc];
$className = $assocData['className'];
$linkModel =& $model->{$className};
$external = isset($assocData['external']);
$this->db->__scrubQueryData($queryData);
$result = array(
'linkModel'=> &$linkModel,
'type'=> $type,
'assoc'=> $assoc,
'assocData'=> $assocData,
'external'=> $external
);
return $result;
}
function testStringConditionsParsing() {
$result = $this->db->conditions("Candy.name LIKE 'a' AND HardCandy.name LIKE 'c'");

View file

@ -26,6 +26,10 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
}
require_once LIBS.'../app_helper.php';
require_once LIBS.'class_registry.php';
require_once LIBS.DS.'view'.DS.'view.php';
@ -33,11 +37,28 @@
require_once LIBS.DS.'view'.DS.'helpers'.DS.'html.php';
require_once LIBS.DS.'view'.DS.'helpers'.DS.'form.php';
require_once LIBS.DS.'controller'.DS.'controller.php';
require_once LIBS.DS.'model'.DS.'model.php';
class TheTestController extends Controller {
var $name = 'TheTest';
var $uses = null;
}
}
class Contact extends Model {
var $primaryKey = 'id';
var $useTable = false;
function loadInfo() {
return new Set(array(
array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
));
}
}
/**
* Short description for class.
*
@ -48,38 +69,186 @@ class TheTestController extends Controller {
class FormHelperTest extends UnitTestCase {
function setUp() {
$this->form = new FormHelper();
$this->form->Html = new HtmlHelper();
$this->Form = new FormHelper();
$this->Form->Html = new HtmlHelper();
$view = new View(new TheTestController());
ClassRegistry::addObject('view', $view);
ClassRegistry::addObject('Contact', new Contact());
}
function testFormInput() {
$result = $this->form->input('Model/field', array('type' => 'text'));
$result = $this->Form->input('Model/field', array('type' => 'text'));
$expected = '<div class="input"><label for="ModelField">Field</label><input name="data[Model][field]" value="" id="ModelField" type="text" /></div>';
//$this->assertEqual($result, $expected);
$result = $this->form->input('Model/password');
$result = $this->Form->input('Model/password');
$expected = '<div class="input"><label for="ModelPassword">Password</label><input type="password" name="data[Model][password]" value="" id="ModelPassword" /></div>';
$this->assertEqual($result, $expected);
}
function testLabel() {
$this->Form->text('Person/name');
$result = $this->Form->label();
$this->assertEqual($result, '<label for="PersonName">Name</label>');
$result = $this->Form->label('first_name');
$this->assertEqual($result, '<label for="first_name">First Name</label>');
$result = $this->Form->label('first_name', 'Your first name');
$this->assertEqual($result, '<label for="first_name">Your first name</label>');
$result = $this->Form->label('first_name', 'Your first name', array('class' => 'my-class'));
$this->assertEqual($result, '<label for="first_name" class="my-class">Your first name</label>');
$result = $this->Form->label('first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
$this->assertEqual($result, '<label for="first_name" class="my-class" id="LabelID">Your first name</label>');
}
function testTextbox() {
$result = $this->form->text('Model/field');
$expected = '<input name="data[Model][field]" type="text" value="" id="ModelField" />';
$this->assertEqual($result, $expected);
$result = $this->Form->text('Model/field');
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+type="text"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+value=""[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+id="ModelField"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input[^<>]+name="[^<>]+name="[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|name|id|value]=[^<>]*>/', $result);
$result = $this->form->text('Model/field', array('type' => 'password'));
$expected = '<input name="data[Model][field]" type="password" value="" id="ModelField" />';
$this->assertEqual($result, $expected);
$result = $this->Form->text('Model/field', array('type' => 'password'));
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+type="password"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+value=""[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+id="ModelField"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input[^<>]+name="[^<>]+name="[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|name|id|value]=[^<>]*>/', $result);
$result = $this->form->text('Model/field', array('id' => 'theID'));
$result = $this->Form->text('Model/field', array('id' => 'theID'));
$expected = '<input name="data[Model][field]" type="text" id="theID" value="" />';
$this->assertEqual($result, $expected);
$this->Form->validationErrors['Model']['text'] = 1;
$this->Form->data['Model']['text'] = 'test';
$result = $this->Form->text('Model/text', array('id' => 'theID'));
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[text\]"[^<>]+id="theID"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+value="test"[^<>]+class="form-error"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input[^<>]+name="[^<>]+name="[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|name|id|value|class]=[^<>]*>/', $result);
}
function testPassword() {
$result = $this->Form->password('Model/field');
$expected = '<input name="data[Model][field]" type="password" value="" id="ModelField" />';
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+type="password"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+value=""[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+id="ModelField"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input[^<>]+name="[^<>]+name="[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|name|id|value]=[^<>]*>/', $result);
$this->Form->validationErrors['Model']['passwd'] = 1;
$this->Form->data['Model']['passwd'] = 'test';
$result = $this->Form->password('Model/passwd', array('id' => 'theID'));
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[passwd\]"[^<>]+id="theID"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+value="test"[^<>]+class="form-error"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input[^<>]+name="[^<>]+name="[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|name|id|value|class]=[^<>]*>/', $result);
}
function testSelect() {
$result = $this->Form->select('Model/field', array());
$this->assertPattern('/^<select [^<>]+>\n<option [^<>]+>/', $result);
$this->assertPattern('/<option value="" ><\/option>/', $result);
$this->assertPattern('/<\/select>$/', $result);
$this->assertPattern('/<select[^<>]+name="data\[Model\]\[field\]"[^<>]*>/', $result);
$this->assertPattern('/<select[^<>]+id="ModelField"[^<>]*>/', $result);
$this->assertNoPattern('/^<select[^<>]+name="[^<>]+name="[^<>]+>$/', $result);
$this->Form->data = array('Model' => array('field' => 'value'));
$result = $this->Form->select('Model/field', array('value' => 'good', 'other' => 'bad'));
$this->assertPattern('/option value=""/', $result);
$this->assertPattern('/option value="value"\s+selected="selected"/', $result);
$this->assertPattern('/option value="other"/', $result);
$this->assertPattern('/<\/option>\s+<option/', $result);
$this->assertPattern('/<\/option>\s+<\/select>/', $result);
$this->assertNoPattern('/option value="other"\s+selected="selected"/', $result);
$this->assertNoPattern('/<select[^<>]+[^name|id]=[^<>]*>/', $result);
$this->assertNoPattern('/<option[^<>]+[^value|selected]=[^<>]*>/', $result);
}
function testTextArea() {
$this->Form->data = array('Model' => array('field' => 'some test data'));
$result = $this->Form->textarea('Model/field');
$this->assertPattern('/^<textarea[^<>]+name="data\[Model\]\[field\]"[^<>]+id="ModelField"/', $result);
$this->assertPattern('/^<textarea[^<>]+>some test data<\/textarea>$/', $result);
$this->assertNoPattern('/^<textarea[^<>]+value="[^<>]+>/', $result);
$this->assertNoPattern('/^<textarea[^<>]+name="[^<>]+name="[^<>]+>$/', $result);
$this->assertNoPattern('/<textarea[^<>]+[^name|id]=[^<>]*>/', $result);
$result = $this->Form->textarea('Model/tmp');
$this->assertPattern('/^<textarea[^<>]+name="data\[Model\]\[tmp\]"[^<>]+><\/textarea>/', $result);
}
function testHiddenField() {
$this->Form->validationErrors['Model']['field'] = 1;
$this->Form->data['Model']['field'] = 'test';
$result = $this->Form->hidden('Model/field', array('id' => 'theID'));
$this->assertPattern('/^<input[^<>]+type="hidden"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[field\]"[^<>]+id="theID"[^<>]+value="test"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input[^<>]+name="[^<>]+name="[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|name|id|value]=[^<>]*>/', $result);
}
function testFileUploadField() {
$result = $this->Form->file('Model/upload');
$this->assertPattern('/^<input type="file"[^<>]+name="data\[Model\]\[upload\]"[^<>]+\/>$/', $result);
$this->assertPattern('/^<input type="file"[^<>]+value=""[^<>]+\/>$/', $result);
$this->assertPattern('/^<input type="file"[^<>]+id="ModelUpload"[^<>]+\/>$/', $result);
$this->assertNoPattern('/^<input[^<>]+name="[^<>]+name="[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|name|value|id]=[^<>]*>/', $result);
}
function testSubmitButton() {
$result = $this->Form->submit('Test Submit');
$this->assertPattern('/^<div\s+class="submit"><input type="submit"[^<>]+value="Test Submit"[^<>]+\/><\/div>$/', $result);
$result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
$this->assertPattern('/^<input type="submit"[^<>]+value="Test Submit"[^<>]+\/>$/', $result);
$this->assertPattern('/^<[^<>]+class="save"[^<>]+\/>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|class|value]=[^<>]*>/', $result);
$result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
$this->assertPattern('/^<div[^<>]+id="SaveButton"[^<>]*><input type="submit"[^<>]+value="Test Submit"[^<>]+\/><\/div>$/', $result);
$this->assertNoPattern('/<input[^<>]+[^type|value]=[^<>]*>/', $result);
}
function testFormCreate() {
$result = $this->Form->create('Contact');
$this->assertPattern('/^<form [^<>]+>/', $result);
$this->assertPattern('/\s+id="ContactAddForm"/', $result);
$this->assertPattern('/\s+method="post"/', $result);
$this->assertPattern('/\s+action="\/contacts\/add\/"/', $result);
$result = $this->Form->create('Contact', array('type' => 'GET'));
$this->assertPattern('/^<form [^<>]+method="get"[^<>]+>$/', $result);
$result = $this->Form->create('Contact', array('type' => 'get'));
$this->assertPattern('/^<form [^<>]+method="get"[^<>]+>$/', $result);
$result = $this->Form->create('Contact', array('type' => 'put'));
$this->assertPattern('/^<form [^<>]+method="post"[^<>]+>/', $result);
$this->Form->data['Contact']['id'] = 1;
$result = $this->Form->create('Contact');
$this->assertPattern('/^<form[^<>]+method="post"[^<>]+>/', $result);
$this->assertPattern('/^<form[^<>]+id="ContactEditForm"[^<>]+>/', $result);
$this->assertPattern('/^<form[^<>]+action="\/contacts\/edit\/1"[^<>]*>/', $result);
$this->assertNoPattern('/^<form[^<>]+[^id|method|action]=[^<>]*>/', $result);
}
function testFormEnd() {
$this->assertEqual($this->Form->end(), '</form>');
}
function tearDown() {
unset($this->form);
unset($this->Form);
}
}

View file

@ -34,19 +34,17 @@
* @subpackage cake.tests.groups
*/
class AllCoreWithOutDatabaseGroupTest extends GroupTest {
var $label = 'All Test without a database connection';
var $label = 'All tests without a database connection';
function AllCoreWithOutDatabaseGroupTest() {
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'router');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'dispatcher');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'router');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'inflector');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'validation');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'session');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'view' . DS . 'helpers' . DS . 'paginator');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'socket');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'view' . DS . 'helpers' . DS . 'ajax');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'view' . DS . 'helpers' . DS . 'html');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'view' . DS . 'helpers' . DS . 'javascript');
TestManager::addTestFile($this, CORE_TEST_CASES . DS . 'libs' . DS . 'model' . DS . 'model');
TestManager::addTestCasesFromDirectory($this, CORE_TEST_CASES . DS . 'libs' . DS . 'view');
}
}
?>