form.test.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 1,949 行 · 第 1/5 页

PHP
1,949
字号
		$fields = $this->__sortFields($fields);		$fieldsKey = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt')));		$fields['__Token']['fields'] = $fieldsKey;		$this->Form->params['_Token']['key'] = 'testKey';		$result = $this->Form->create('Contact', array('url' => '/contacts/add'));		$expected = array(			'form' => array('method' => 'post', 'action' => '/contacts/add'),			'fieldset' => array('style' => 'display:none;'),			array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),			array('input' => array('type' => 'hidden', 'name' => 'data[__Token][key]', 'value' => 'testKey', 'id' => 'preg:/Token\d+/')),			'/fieldset'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('UserForm.published', array('type' => 'text'));		$expected = array(			'div' => array('class' => 'input text'),			'label' => array('for' => 'UserFormPublished'),			'Published',			'/label',			array('input' => array('type' => 'text', 'name' => 'data[UserForm][published]', 'value' => '', 'id' => 'UserFormPublished')),			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('UserForm.other', array('type' => 'text'));		$expected = array(			'div' => array('class' => 'input text'),			'label' => array('for' => 'UserFormOther'),			'Other',			'/label',			array('input' => array('type' => 'text', 'name' => 'data[UserForm][other]', 'value' => '', 'id' => 'UserFormOther')),			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->hidden('UserForm.stuff');		$expected = array(			'input' => array('type' => 'hidden', 'name' => 'data[_UserForm][stuff]', 'value' => '', 'id' => 'UserFormStuff')		);		$this->assertTags($result, $expected);		$result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));		$expected = array(			'div' => array('class' => 'input checkbox'),			array('input' => array('type' => 'hidden', 'name' => 'data[_UserForm][something]', 'value' => '0', 'id' => 'UserFormSomething_')),			array('input' => array('type' => 'checkbox', 'name' => 'data[UserForm][something]', 'value' => '1', 'id' => 'UserFormSomething')),			'label' => array('for' => 'UserFormSomething'),			'Something',			'/label',			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->secure($this->Form->fields);		$expected = array(			'fieldset' => array('style' => 'display:none;'),			array('input' => array('type' => 'hidden', 'name' => 'data[__Token][fields]', 'value' => $fieldsKey, 'id' => 'preg:/TokenFields\d+/')),			'/fieldset'		);		$this->assertTags($result, $expected);		$result = $this->Form->fields;		$result = $this->__sortFields($result);		$this->assertEqual($result, $fields);	}/** * testPasswordValidation method * * @access public * @return void */	function testPasswordValidation() {		$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';		$result = $this->Form->input('Contact.password');		$expected = array(			'div' => array('class' => 'input password error'),			'label' => array('for' => 'ContactPassword'),			'Password',			'/label',			'input' => array('type' => 'password', 'name' => 'data[Contact][password]', 'value' => '', 'id' => 'ContactPassword', 'class' => 'form-error'),			array('div' => array('class' => 'error-message')),			'Please provide a password',			'/div',			'/div'		);		$this->assertTags($result, $expected);	}/** * testFormValidationAssociated method * * @access public * @return void */	function testFormValidationAssociated() {		$this->UserForm =& ClassRegistry::getObject('UserForm');		$this->UserForm->OpenidUrl =& ClassRegistry::getObject('OpenidUrl');		$data = array('UserForm' => array('name' => 'user'), 'OpenidUrl' => array('url' => 'http://www.cakephp.org'));		$this->assertTrue($this->UserForm->OpenidUrl->create($data));		$this->assertFalse($this->UserForm->OpenidUrl->validates());		$result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));		$expected = array(			'form' => array('method' => 'post', 'action' => '/user_forms/login/', 'id' => 'UserFormLoginForm'),			'fieldset' => array('style' => 'display:none;'),			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),			'/fieldset'		);		$this->assertTags($result, $expected);		$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);	}/** * testFormValidationAssociatedFirstLevel method * * @access public * @return void */	function testFormValidationAssociatedFirstLevel() {		$this->ValidateUser =& ClassRegistry::getObject('ValidateUser');		$this->ValidateUser->ValidateProfile =& ClassRegistry::getObject('ValidateProfile');		$data = array('ValidateUser' => array('name' => 'mariano'), 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'));		$this->assertTrue($this->ValidateUser->create($data));		$this->assertFalse($this->ValidateUser->validates());		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));		$expected = array(			'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id'),			'fieldset' => array('style' => 'display:none;'),			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),			'/fieldset'		);		$this->assertTags($result, $expected);		$expected = array(			'ValidateUser' => array('email' => 1),			'ValidateProfile' => array('full_name' => 1, 'city' => 1)		);		$this->assertEqual($this->Form->validationErrors, $expected);		unset($this->ValidateUser->ValidateProfile);		unset($this->ValidateUser);	}/** * testFormValidationAssociatedSecondLevel method * * @access public * @return void */	function testFormValidationAssociatedSecondLevel() {		$this->ValidateUser =& ClassRegistry::getObject('ValidateUser');		$this->ValidateUser->ValidateProfile =& ClassRegistry::getObject('ValidateProfile');		$this->ValidateUser->ValidateProfile->ValidateItem =& ClassRegistry::getObject('ValidateItem');		$data = array(			'ValidateUser' => array('name' => 'mariano'),			'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),			'ValidateItem' => array('name' => 'Item')		);		$this->assertTrue($this->ValidateUser->create($data));		$this->assertFalse($this->ValidateUser->validates());		$this->assertFalse($this->ValidateUser->ValidateProfile->validates());		$this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());		$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));		$expected = array(			'form' => array('method' => 'post', 'action' => '/validate_users/add/', 'id'),			'fieldset' => array('style' => 'display:none;'),			'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),			'/fieldset'		);		$this->assertTags($result, $expected);		$expected = array(			'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);	}/** * testFormValidationMultiRecord method * * @access public * @return void */	function testFormValidationMultiRecord() {		$this->Form->validationErrors['Contact'] = array(2 => array('name' => 'This field cannot be left blank'));		$result = $this->Form->input('Contact.2.name');		$expected = array(			'div' => array('class'),			'label' => array('for'),			'preg:/[^<]+/',			'/label',			'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error', 'maxlength' => 255),			array('div' => array('class' => 'error-message')),			'This field cannot be left blank',			'/div',			'/div'		);		$this->assertTags($result, $expected);		$this->Form->validationErrors['UserForm'] = array('OpenidUrl' => array('url' => 'You must provide a URL'));		$this->Form->create('UserForm');		$result = $this->Form->input('OpenidUrl.url');		$expected = array(			'div' => array('class'),			'label' => array('for'),			'preg:/[^<]+/',			'/label',			'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error'),			array('div' => array('class' => 'error-message')),			'You must provide a URL',			'/div',			'/div'		);	}/** * testMultipleInputValidation method * * @access public * @return void */	function testMultipleInputValidation() {		$this->Form->create();		$this->Form->validationErrors['Address'][0]['title'] = 'This field cannot be empty';		$this->Form->validationErrors['Address'][0]['first_name'] = 'This field cannot be empty';		$this->Form->validationErrors['Address'][1]['last_name'] = 'You must have a last name';		$result = $this->Form->input('Address.0.title');		$expected = array(			'div' => array('class'),			'label' => array('for'),			'preg:/[^<]+/',			'/label',			'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error'),			array('div' => array('class' => 'error-message')),			'This field cannot be empty',			'/div',			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Address.0.first_name');		$expected = array(			'div' => array('class'),			'label' => array('for'),			'preg:/[^<]+/',			'/label',			'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error'),			array('div' => array('class' => 'error-message')),			'This field cannot be empty',			'/div',			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Address.0.last_name');		$expected = array(			'div' => array('class'),			'label' => array('for'),			'preg:/[^<]+/',			'/label',			'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error'),			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Address.1.last_name');		$expected = array(			'div' => array('class'),			'label' => array('for'),			'preg:/[^<]+/',			'/label',			'input' => array('type' => 'text', 'name' => 'preg:/[^<]+/', 'value' => '', 'id' => 'preg:/[^<]+/', 'class' => 'form-error'),			array('div' => array('class' => 'error-message')),			'You must have a last name',			'/div',			'/div'		);		$this->assertTags($result, $expected);	}/** * testFormInput method * * @access public * @return void */	function testFormInput() {		$result = $this->Form->input('ValidateUser.balance');		$expected = array(			'div' => array('class'),			'label' => array('for'),			'Balance',			'/label',			'input' => array('name', 'type' => 'text', 'maxlength' => 8, 'value' => '', 'id'),			'/div',		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Contact.email', array('id' => 'custom'));		$expected = array(			'div' => array('class' => 'input text'),			'label' => array('for' => 'custom'),			'Email',			'/label',			array('input' => array('type' => 'text', 'name' => 'data[Contact][email]', 'value' => '', 'id' => 'custom', 'maxlength' => 255)),			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->hidden('Contact/idontexist');		$expected = array(			'input' => array('type' => 'hidden', 'name' => 'data[Contact][idontexist]', 'value' => '', 'id' => 'ContactIdontexist'),		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Contact.email', array('type' => 'text'));		$expected = array(			'div' => array('class' => 'input text'),			'label' => array('for' => 'ContactEmail'),			'Email',			'/label',			array('input' => array('type' => 'text', 'name' => 'data[Contact][email]', 'value' => '', 'id' => 'ContactEmail')),			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Contact.5.email', array('type' => 'text'));		$expected = array(			'div' => array('class' => 'input text'),			'label' => array('for' => 'Contact5Email'),			'Email',			'/label',			array('input' => array('type' => 'text', 'name' => 'data[Contact][5][email]', 'value' => '', 'id' => 'Contact5Email')),			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Contact/password');		$expected = array(			'div' => array('class' => 'input password'),			'label' => array('for' => 'ContactPassword'),			'Password',			'/label',			array('input' => array('type' => 'password', 'name' => 'data[Contact][password]', 'value' => '', 'id' => 'ContactPassword')),			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('email', array('options' => array('First', 'Second'), 'empty' => true));		$expected = array(			'div' => array('class' => 'input select'),			'label' => array('for' => 'email'),			'Email',			'/label',			array('select' => array('name' => 'data[email]', 'id' => 'email')),			array('option' => array('value' => '')),			'/option',			array('option' => array('value' => '0')),			'First',			'/option',			array('option' => array('value' => '1')),			'Second',			'/option',			'/select',			'/div'		);		$this->assertTags($result, $expected);		$result = $this->Form->input('Contact.email', array('type' => 'file', 'class' => 'textbox'));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?