2007-11-04 18:07:59 +00:00
|
|
|
<?php
|
2007-11-20 04:51:06 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2007-11-04 18:07:59 +00:00
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-11-04 18:07:59 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-11-04 18:07:59 +00:00
|
|
|
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2007-11-20 04:51:06 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
2007-11-04 18:07:59 +00:00
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
|
|
|
*/
|
|
|
|
uses('view' . DS . 'view', 'view' . DS . 'helper');
|
|
|
|
|
|
|
|
class HelperTestPost extends Model {
|
|
|
|
|
|
|
|
var $useTable = false;
|
|
|
|
|
|
|
|
function schema() {
|
|
|
|
$this->_schema = array(
|
|
|
|
'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
|
|
|
|
'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
|
|
|
|
'body' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => ''),
|
|
|
|
'number' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
|
2007-12-12 04:08:19 +00:00
|
|
|
'date' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
|
2007-11-04 18:07:59 +00:00
|
|
|
'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
|
|
|
|
'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
|
|
|
|
);
|
|
|
|
return $this->_schema;
|
|
|
|
}
|
2007-11-09 00:17:44 +00:00
|
|
|
|
|
|
|
var $hasAndBelongsToMany = array('HelperTestTag'=> array());
|
2007-11-04 18:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class HelperTestComment extends Model {
|
|
|
|
|
|
|
|
var $useTable = false;
|
|
|
|
|
|
|
|
function schema() {
|
|
|
|
$this->_schema = array(
|
|
|
|
'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
|
|
|
|
'author_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
|
|
|
|
'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
|
|
|
|
'body' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => ''),
|
|
|
|
'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
|
|
|
|
'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
|
|
|
|
);
|
|
|
|
return $this->_schema;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-09 00:17:44 +00:00
|
|
|
class HelperTestTag extends Model {
|
|
|
|
|
|
|
|
var $useTable = false;
|
|
|
|
function schema() {
|
|
|
|
$this->_schema = array(
|
|
|
|
'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
|
|
|
|
'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
|
|
|
|
'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
|
|
|
|
'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
|
|
|
|
);
|
|
|
|
return $this->_schema;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class HelperTestPostsTag extends Model {
|
|
|
|
|
|
|
|
var $useTable = false;
|
|
|
|
|
|
|
|
function schema() {
|
|
|
|
$this->_schema = array(
|
|
|
|
'helper_test_post_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
|
|
|
|
'helper_test_tag_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
|
|
|
|
);
|
|
|
|
return $this->_schema;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-04 18:07:59 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
*/
|
|
|
|
class HelperTest extends UnitTestCase {
|
|
|
|
|
|
|
|
function setUp() {
|
2007-12-23 01:07:10 +00:00
|
|
|
ClassRegistry::flush();
|
2007-11-04 18:07:59 +00:00
|
|
|
Router::reload();
|
|
|
|
$null = null;
|
|
|
|
$this->View = new View($null);
|
|
|
|
$this->Helper = new Helper();
|
|
|
|
ClassRegistry::addObject('HelperTestPost', new HelperTestPost());
|
|
|
|
ClassRegistry::addObject('HelperTestComment', new HelperTestComment());
|
2007-11-09 00:17:44 +00:00
|
|
|
ClassRegistry::addObject('HelperTestTag', new HelperTestTag());
|
2007-11-04 18:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testFormFieldNameParsing() {
|
2008-03-09 18:28:38 +00:00
|
|
|
// PHP4 reference hack
|
|
|
|
ClassRegistry::removeObject('view');
|
|
|
|
ClassRegistry::addObject('view', $this->View);
|
|
|
|
|
2007-11-04 18:07:59 +00:00
|
|
|
$this->Helper->setEntity('HelperTestPost.id');
|
|
|
|
$this->assertFalse($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('HelperTestComment.body');
|
|
|
|
$this->assertFalse($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestComment');
|
|
|
|
$this->assertEqual($this->View->field, 'body');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('HelperTestPost', true);
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, null);
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->Helper->setEntity('_Token.fields');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'fields');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, '_Token');
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
|
2007-11-04 18:07:59 +00:00
|
|
|
$this->Helper->setEntity('id');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('HelperTestComment.body');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'body');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, 'HelperTestComment');
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('body');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'body');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->Helper->setEntity('Something.else');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'else');
|
|
|
|
$this->assertEqual($this->View->modelId, false);
|
|
|
|
$this->assertEqual($this->View->association, 'Something');
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, '');
|
|
|
|
|
2007-11-08 00:37:55 +00:00
|
|
|
$this->Helper->setEntity('5.id');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, '5');
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
2007-11-19 14:08:37 +00:00
|
|
|
$this->assertEqual($this->View->entity(), array('HelperTestPost', 5, 'id'));
|
|
|
|
|
|
|
|
$this->Helper->setEntity('0.id');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, '0');
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->assertEqual($this->View->entity(), array('HelperTestPost', 0, 'id'));
|
|
|
|
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->Helper->setEntity('5.created.month');
|
2007-11-04 18:07:59 +00:00
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->assertEqual($this->View->field, 'created');
|
2007-11-04 18:07:59 +00:00
|
|
|
$this->assertEqual($this->View->modelId, '5');
|
|
|
|
$this->assertEqual($this->View->association, null);
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->assertEqual($this->View->fieldSuffix, 'month');
|
2007-11-04 18:07:59 +00:00
|
|
|
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->Helper->setEntity('HelperTestComment.5.id');
|
2007-11-04 18:07:59 +00:00
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, '5');
|
|
|
|
$this->assertEqual($this->View->association, 'HelperTestComment');
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('HelperTestComment.id.time');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, 'HelperTestComment');
|
2007-11-04 18:07:59 +00:00
|
|
|
$this->assertEqual($this->View->fieldSuffix, 'time');
|
|
|
|
|
2007-11-09 00:17:44 +00:00
|
|
|
$this->Helper->setEntity('HelperTestTag');
|
|
|
|
$this->assertTrue($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'HelperTestTag');
|
|
|
|
$this->assertEqual($this->View->modelId, '');
|
|
|
|
$this->assertEqual($this->View->association, 'HelperTestTag');
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, '');
|
|
|
|
|
2007-11-04 18:07:59 +00:00
|
|
|
$this->Helper->setEntity(null);
|
|
|
|
$this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist');
|
|
|
|
$this->assertFalse($this->View->modelScope);
|
|
|
|
$this->assertEqual($this->View->model, 'ModelThatDoesntExist');
|
|
|
|
$this->assertEqual($this->View->field, 'field_that_doesnt_exist');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
}
|
|
|
|
|
2007-12-07 10:55:42 +00:00
|
|
|
function testFieldsWithSameName() {
|
2008-03-09 18:28:38 +00:00
|
|
|
// PHP4 reference hack
|
|
|
|
ClassRegistry::removeObject('view');
|
|
|
|
ClassRegistry::addObject('view', $this->View);
|
|
|
|
|
2007-12-07 10:55:42 +00:00
|
|
|
$this->Helper->setEntity('HelperTestTag', true);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('HelperTestTag.id');
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestTag');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('My.id');
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestTag');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, 'My');
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('MyOther.id');
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestTag');
|
|
|
|
$this->assertEqual($this->View->field, 'id');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, 'MyOther');
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testFieldSameAsModel() {
|
2008-03-09 18:28:38 +00:00
|
|
|
// PHP4 reference hack
|
|
|
|
ClassRegistry::removeObject('view');
|
|
|
|
ClassRegistry::addObject('view', $this->View);
|
2007-12-07 10:55:42 +00:00
|
|
|
|
|
|
|
$this->Helper->setEntity('HelperTestTag', true);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('helper_test_post');
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestTag');
|
|
|
|
$this->assertEqual($this->View->field, 'helper_test_post');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-12-12 04:08:19 +00:00
|
|
|
function testFieldSuffixForDate() {
|
2008-03-09 18:28:38 +00:00
|
|
|
// PHP4 reference hack
|
|
|
|
ClassRegistry::removeObject('view');
|
|
|
|
ClassRegistry::addObject('view', $this->View);
|
|
|
|
|
2007-12-12 04:08:19 +00:00
|
|
|
$this->Helper->setEntity('HelperTestPost', true);
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, null);
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, null);
|
|
|
|
|
|
|
|
$this->Helper->setEntity('date.month');
|
|
|
|
$this->assertEqual($this->View->model, 'HelperTestPost');
|
|
|
|
$this->assertEqual($this->View->field, 'date');
|
|
|
|
$this->assertEqual($this->View->modelId, null);
|
|
|
|
$this->assertEqual($this->View->association, null);
|
|
|
|
$this->assertEqual($this->View->fieldSuffix, 'month');
|
|
|
|
}
|
2007-12-06 01:16:14 +00:00
|
|
|
|
|
|
|
function testMulitDimensionValue() {
|
|
|
|
$this->Helper->data = array();
|
|
|
|
for($i = 0; $i < 2; $i++) {
|
|
|
|
$this->Helper->data['Model'][$i] = 'what';
|
|
|
|
$result[] = $this->Helper->value("Model.{$i}");
|
|
|
|
$this->Helper->data['Model'][$i] = array();
|
|
|
|
for($j = 0; $j < 2; $j++) {
|
|
|
|
$this->Helper->data['Model'][$i][$j] = 'how';
|
|
|
|
$result[] = $this->Helper->value("Model.{$i}.{$j}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$expected = array('what', 'how', 'how', 'what', 'how', 'how');
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$this->Helper->data['HelperTestComment']['5']['id'] = 'ok';
|
|
|
|
$result = $this->Helper->value('HelperTestComment.5.id');
|
|
|
|
$this->assertEqual($result, 'ok');
|
|
|
|
|
|
|
|
$this->Helper->setEntity('HelperTestPost', true);
|
|
|
|
$this->Helper->data['HelperTestPost']['5']['created']['month'] = '10';
|
|
|
|
$result = $this->Helper->value('5.created.month');
|
|
|
|
$this->assertEqual($result, 10);
|
|
|
|
|
|
|
|
$this->Helper->data['HelperTestPost']['0']['id'] = 100;
|
|
|
|
$result = $this->Helper->value('0.id');
|
|
|
|
$this->assertEqual($result, 100);
|
|
|
|
}
|
|
|
|
|
2008-05-05 14:41:15 +00:00
|
|
|
function testClean() {
|
|
|
|
$result = $this->Helper->clean(array());
|
|
|
|
$this->assertEqual($result, null);
|
|
|
|
|
|
|
|
$result = $this->Helper->clean(array('<script>with something</script>', '<applet>something else</applet>'));
|
|
|
|
$this->assertEqual($result, array('with something', 'something else'));
|
|
|
|
|
|
|
|
$result = $this->Helper->clean('<script>with something</script>');
|
|
|
|
$this->assertEqual($result, 'with something');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-11-04 18:07:59 +00:00
|
|
|
function tearDown() {
|
|
|
|
unset($this->Helper, $this->View);
|
|
|
|
ClassRegistry::flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|