* Copyright (c) 2006, Larry E. Masters Shorewood, IL. 60431 * Author(s): Larry E. Masters aka PhpNut * * Licensed under The Open Group Test Suite License * Redistributions of files must retain the above copyright notice. * * @filesource * @author Larry E. Masters aka PhpNut * @copyright Copyright (c) 2006, Larry E. Masters Shorewood, IL. 60431 * @link http://www.phpnut.com/projects/ * @package test_suite * @subpackage test_suite.cases.app * @since CakePHP Test Suite v 1.0.0.0 * @version $Revision$ * @modifiedby $LastChangedBy$ * @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'; require_once LIBS.DS.'view'.DS.'helper.php'; 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'; if (!class_exists('TheTestController')) { class TheTestController extends Controller { var $name = 'TheTest'; var $uses = null; } } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class Contact extends Model { var $primaryKey = 'id'; var $useTable = false; var $name = 'Contact'; 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' => 'published', 'type' => 'date', 'null' => true, 'default' => null, 'length' => null), array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) )); } } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class UserForm extends Model { var $useTable = false; var $primaryKey = 'id'; var $name = 'UserForm'; var $hasMany = array('OpenidUrl' => array( 'className' => 'OpenidUrl', 'foreignKey' => 'user_form_id' )); 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. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class OpenidUrl extends Model { var $useTable = false; var $primaryKey = 'id'; var $name = 'OpenidUrl'; var $belongsTo = array('UserForm' => array( 'className' => 'UserForm', 'foreignKey' => 'user_form_id' )); function loadInfo() { return new Set(array( array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), array('name' => 'user_form_id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), array('name' => 'url', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), )); } function beforeValidate() { $this->invalidate('openid_not_registered'); return true; } } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class ValidateUser extends Model { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateUser'; var $hasOne = array('ValidateProfile' => array( 'className' => 'ValidateProfile', 'foreignKey' => 'user_id' )); 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' => 'email', '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) )); } function beforeValidate() { $this->invalidate('email'); return false; } } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class ValidateProfile extends Model { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateProfile'; var $hasOne = array('ValidateItem' => array( 'className' => 'ValidateItem', 'foreignKey' => 'profile_id' )); var $belongsTo = array('ValidateUser' => array( 'className' => 'ValidateUser', 'foreignKey' => 'user_id' )); function loadInfo() { return new Set(array( array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), array('name' => 'user_id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), array('name' => 'full_name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), array('name' => 'city', '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) )); } function beforeValidate() { $this->invalidate('full_name'); $this->invalidate('city'); return false; } } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class ValidateItem extends Model { var $primaryKey = 'id'; var $useTable = false; var $name = 'ValidateItem'; var $belongsTo = array('ValidateProfile' => array( 'className' => 'ValidateProfile', 'foreignKey' => 'profile_id' )); function loadInfo() { return new Set(array( array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), array('name' => 'profile_id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), array('name' => 'description', '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) )); } function beforeValidate() { $this->invalidate('description'); return false; } } /** * Short description for class. * * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ class FormHelperTest extends CakeTestCase { function startTest($method) { $this->Form =& new FormHelper(); $this->Form->Html =& new HtmlHelper(); $this->Controller =& new TheTestController(); $this->View =& new View($this->Controller); ClassRegistry::addObject('view', $view); ClassRegistry::addObject('Contact', new Contact()); } function endTest($method) { if (isset($this->Form)) { unset($this->Form->Html); unset($this->Form); } unset($this->Controller); unset($this->View); } function testFormValidationAssociated() { $this->UserForm =& new UserForm(); $this->UserForm->OpenidUrl =& new OpenidUrl(); $data = array( 'UserForm' => array( 'name' => 'user' ), 'OpenidUrl' => array( 'url' => 'http://www.cricava.com' ) ); $result = $this->UserForm->OpenidUrl->create($data); $this->assertTrue($result); $result = $this->UserForm->OpenidUrl->validates(); $this->assertFalse($result); $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login')); $this->assertPattern('/^]*>$/', $result); $expected = array( 'OpenidUrl' => array( 'openid_not_registered' => 1 ) ); $this->assertEqual($this->Form->validationErrors, $expected); $result = $this->Form->error('OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)); $this->assertEqual($result, 'Error, not registered'); unset($this->UserForm->OpenidUrl); unset($this->UserForm); } function testFormValidationAssociatedFirstLevel() { $this->ValidateUser =& new ValidateUser(); $this->ValidateUser->ValidateProfile =& new ValidateProfile(); $data = array( 'ValidateUser' => array( 'name' => 'mariano' ), 'ValidateProfile' => array( 'full_name' => 'Mariano Iglesias' ) ); $result = $this->ValidateUser->create($data); $this->assertTrue($result); $result = $this->ValidateUser->validates(); $this->assertFalse($result); $result = $this->ValidateUser->ValidateProfile->validates(); $this->assertFalse($result); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); $this->assertPattern('/^]*>$/', $result); $expected = array( 'OpenidUrl' => array( 'openid_not_registered' => 1 ), 'ValidateUser' => array( 'email' => 1 ), 'ValidateProfile' => array( 'full_name' => 1, 'city' => 1 ) ); $this->assertEqual($this->Form->validationErrors, $expected); unset($this->ValidateUser->ValidateProfile); unset($this->ValidateUser); } function testFormValidationAssociatedSecondLevel() { $this->ValidateUser =& new ValidateUser(); $this->ValidateUser->ValidateProfile =& new ValidateProfile(); $this->ValidateUser->ValidateProfile->ValidateItem =& new ValidateItem(); $data = array( 'ValidateUser' => array( 'name' => 'mariano' ), 'ValidateProfile' => array( 'full_name' => 'Mariano Iglesias' ), 'ValidateItem' => array( 'name' => 'Item' ) ); $result = $this->ValidateUser->create($data); $this->assertTrue($result); $result = $this->ValidateUser->validates(); $this->assertFalse($result); $result = $this->ValidateUser->ValidateProfile->validates(); $this->assertFalse($result); $result = $this->ValidateUser->ValidateProfile->ValidateItem->validates(); $this->assertFalse($result); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); $this->assertPattern('/^]*>$/', $result); $expected = array( 'OpenidUrl' => array( 'openid_not_registered' => 1 ), 'ValidateUser' => array( 'email' => 1 ), 'ValidateProfile' => array( 'full_name' => 1, 'city' => 1 ), 'ValidateItem' => array( 'description' => 1 ) ); $this->assertEqual($this->Form->validationErrors, $expected); unset($this->ValidateUser->ValidateProfile->ValidateItem); unset($this->ValidateUser->ValidateProfile); unset($this->ValidateUser); } function testFormInput() { $result = $this->Form->input('Model.field', array('type' => 'text')); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->input('Model/password'); $expected = '
'; $this->assertEqual($result, $expected); $result = $this->Form->input('test', array('options' => array('First', 'Second'), 'empty' => true)); $this->assertPattern('/'; $this->assertEqual($result, $expected); unset($this->Form->data); } function testLabel() { $this->Form->text('Person/name'); $result = $this->Form->label(); $this->assertEqual($result, ''); $this->Form->text('Person.name'); $result = $this->Form->label(); $this->assertEqual($result, ''); $this->Form->text('Person.Name'); $result = $this->Form->label(); $this->assertEqual($result, ''); $result = $this->Form->label('first_name'); $this->assertEqual($result, ''); $result = $this->Form->label('first_name', 'Your first name'); $this->assertEqual($result, ''); $result = $this->Form->label('first_name', 'Your first name', array('class' => 'my-class')); $this->assertEqual($result, ''); $result = $this->Form->label('first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID')); $this->assertEqual($result, ''); $result = $this->Form->label('first_name', ''); $this->assertEqual($result, ''); } function testTextbox() { $result = $this->Form->text('Model.field'); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="text"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value]=[^<>]*>/', $result); $result = $this->Form->text('Model.field', array('type' => 'password')); $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="password"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value]=[^<>]*>/', $result); $result = $this->Form->text('Model.field', array('id' => 'theID')); $expected = ''; $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('/^]+name="data\[Model\]\[text\]"[^<>]+id="theID"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value="test"[^<>]+class="form-error"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value|class]=[^<>]*>/', $result); } function testDefaultValue() { $this->Form->data['Model']['field'] = 'test'; $result = $this->Form->text('Model.field', array('default' => 'default value')); $this->assertPattern('/^]+value="test"[^<>]+\/>$/', $result); unset($this->Form->data['Model']['field']); $result = $this->Form->text('Model.field', array('default' => 'default value')); $this->assertPattern('/^]+value="default value"[^<>]+\/>$/', $result); } function testFieldError() { $this->Form->validationErrors['Model']['field'] = 1; $result = $this->Form->error('Model.field'); $this->assertEqual($result, '
Error in field Field
'); $result = $this->Form->error('Model.field', null, array('wrap' => false)); $this->assertEqual($result, 'Error in field Field'); $this->Form->validationErrors['Model']['field'] = "This field contains invalid input"; $result = $this->Form->error('Model.field', null, array('wrap' => false)); $this->assertEqual($result, 'This field contains invalid input'); $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false)); $this->assertEqual($result, '<strong>Badness!</strong>'); $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false, 'escape' => true)); $this->assertEqual($result, '<strong>Badness!</strong>'); $result = $this->Form->error('Model.field', "Badness!", array('wrap' => false, 'escape' => false)); $this->assertEqual($result, 'Badness!'); } function testPassword() { $result = $this->Form->password('Model.field'); $expected = ''; $this->assertPattern('/^]+name="data\[Model\]\[field\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+type="password"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelField"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^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('/^]+name="data\[Model\]\[passwd\]"[^<>]+id="theID"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value="test"[^<>]+class="form-error"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|id|value|class]=[^<>]*>/', $result); } function testSelect() { $result = $this->Form->select('Model.field', array()); $this->assertPattern('/^]+name="data\[Model\]\[upload\]"[^<>]+\/>$/', $result); $this->assertPattern('/^]+value=""[^<>]+\/>$/', $result); $this->assertPattern('/^]+id="ModelUpload"[^<>]+\/>$/', $result); $this->assertNoPattern('/^]+name="[^<>]+name="[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|name|value|id]=[^<>]*>$/', $result); } function testSubmitButton() { $result = $this->Form->submit('Test Submit'); $this->assertPattern('/^]+value="Test Submit"[^<>]+\/><\/div>$/', $result); $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false)); $this->assertPattern('/^]+value="Test Submit"[^<>]+\/>$/', $result); $this->assertPattern('/^<[^<>]+class="save"[^<>]+\/>$/', $result); $this->assertNoPattern('/]+[^type|class|value]=[^<>]*>/', $result); $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton'))); $this->assertPattern('/^]+id="SaveButton"[^<>]*>]+value="Test Submit"[^<>]+\/><\/div>$/', $result); $this->assertNoPattern('/]+[^type|value]=[^<>]*>/', $result); $result = $this->Form->submit('Next >'); $this->assertPattern('/^]+value="Next >"[^<>]+\/><\/div>$/', $result); $result = $this->Form->submit('Next >', array('escape'=>false)); $this->assertPattern('/^]+value="Next >"[^<>]+\/><\/div>$/', $result); } function testFormCreate() { $result = $this->Form->create('Contact'); $this->assertPattern('/^
]+>/', $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('/^]+method="get"[^<>]+>$/', $result); $result = $this->Form->create('Contact', array('type' => 'get')); $this->assertPattern('/^]+method="get"[^<>]+>$/', $result); $result = $this->Form->create('Contact', array('type' => 'put')); $this->assertPattern('/^]+method="post"[^<>]+>/', $result); $this->Form->data['Contact']['id'] = 1; $result = $this->Form->create('Contact'); $this->assertPattern('/^]+method="post"[^<>]+>/', $result); $this->assertPattern('/^]+id="ContactEditForm"[^<>]+>/', $result); $this->assertPattern('/^]+action="\/contacts\/edit\/1"[^<>]*>/', $result); $this->assertNoPattern('/^]+[^id|method|action]=[^<>]*>/', $result); $result = $this->Form->create('Contact', array('id' => 'TestId')); $this->assertPattern('/id="TestId"/', $result); } function testFormMagicInput() { $result = $this->Form->create('Contact'); $this->assertPattern('/^$/', $result); $result = $this->Form->input('Contact.name'); $this->assertPattern('/^
' . '