| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- App::uses('FormExtHelper', 'Tools.View/Helper');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- App::uses('View', 'View');
- class FormExtHelperTest extends MyCakeTestCase {
- public function setUp() {
- $this->Form = new FormExtHelper(new View(null));
- parent::setUp();
- }
- public function testObject() {
- $this->assertInstanceOf('FormExtHelper', $this->Form);
- }
- /**
- * FormExtHelperTest::testPostLink()
- *
- * @return void
- */
- public function testPostLink() {
- $result = $this->Form->postLink('Delete', '/posts/delete/1');
- $this->assertTags($result, [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['href' => '#', 'class' => 'post-link postLink', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ]);
- }
- /**
- * FormExtHelperTest::testPostLinkShim()
- *
- * @return void
- */
- public function testPostLinkShim() {
- $result = $this->Form->postLink('foo', '/bar', ['confirm' => 'Confirm me']);
- $this->assertTextContains('onclick="if (confirm("Confirm me")) {', $result);
- }
- /**
- * FormExtHelperTest::testDeleteLink()
- *
- * @return void
- */
- public function testDeleteLink() {
- $result = $this->Form->deleteLink('Delete', '/posts/delete/1');
- $this->assertTags($result, [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
- '/form',
- 'a' => ['href' => '#', 'class' => 'delete-link deleteLink', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a'
- ]);
- }
- //Not needed right now as autoRequire has been disabled
- public function _testAutoRequire() {
- $this->Form->request->data['ContactExt']['id'] = 1;
- $this->Form->create('ContactExt');
- Configure::write('Validation.autoRequire', false);
- $result = $this->Form->input('ContactExt.imrequiredonupdate');
- $this->assertTags($result, [
- 'div' => [
- 'class' => 'input text'
- ],
- 'label' => ['for' => 'ContactExtImrequiredonupdate'],
- 'Imrequiredonupdate',
- '/label',
- 'input' => ['name' => 'data[ContactExt][imrequiredonupdate]', 'type' => 'text', 'id' => 'ContactExtImrequiredonupdate'],
- '/div'
- ]);
- Configure::write('Validation.autoRequire', true);
- $result = $this->Form->input('ContactExt.imrequiredonupdate');
- $this->assertTags($result, [
- 'div' => [
- 'class' => 'input text required'
- ],
- 'label' => ['for' => 'ContactExtImrequiredonupdate'],
- 'Imrequiredonupdate',
- '/label',
- 'input' => ['name' => 'data[ContactExt][imrequiredonupdate]', 'type' => 'text', 'id' => 'ContactExtImrequiredonupdate'],
- '/div'
- ]);
- }
- /**
- * Test that browserAutoRequire disables html5 frontend form validation
- *
- * @return void
- */
- public function testBrowserAutoRequire() {
- $this->Form->request->data['ContactExt']['id'] = 1;
- Configure::write('Validation.browserAutoRequire', false);
- $result = $this->Form->create('ContactExt');
- $this->assertTags($result, [
- 'form' => [
- 'action' => '/',
- 'novalidate' => 'novalidate',
- 'id' => 'ContactExtForm',
- 'method' => 'post',
- 'accept-charset' => 'utf-8',
- ],
- 'div' => [
- 'style' => 'display:none;'
- ],
- 'input' => [
- 'type' => 'hidden',
- 'name' => '_method',
- 'value' => 'PUT'
- ],
- '/div',
- ]);
- Configure::write('Validation.browserAutoRequire', true);
- $result = $this->Form->create('ContactExt');
- $this->assertTags($result, [
- 'form' => [
- 'action' => '/',
- 'id' => 'ContactExtForm',
- 'method' => 'post',
- 'accept-charset' => 'utf-8',
- ],
- 'div' => [
- 'style' => 'display:none;'
- ],
- 'input' => [
- 'type' => 'hidden',
- 'name' => '_method',
- 'value' => 'PUT'
- ],
- '/div',
- ]);
- }
- /**
- * TestNormalize method
- *
- * test that whitespaces are normalized for all inputs except textareas (which also understand new line characters)
- *
- * @return void
- */
- public function testNormalize() {
- $this->Form->request->data['Model']['field'] = "My\nvalue";
- $result = $this->Form->text('Model.field');
- $this->assertTags($result, ['input' => ['type' => 'text', 'name' => 'data[Model][field]', 'value' => 'My value', 'id' => 'ModelField']]);
- $this->Form->request->data['Model']['field'] = "My\nvalue";
- $result = $this->Form->textarea('Model.field');
- $this->assertTags($result, [
- 'textarea' => ['name' => 'data[Model][field]', 'id' => 'ModelField'],
- "My\nvalue",
- '/textarea'
- ]);
- $this->Form->request->data['Model']['field'] = "My\nvalue";
- $result = $this->Form->input('Model.field', ['type' => 'text']);
- $this->assertTags($result, [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'ModelField'],
- 'Field',
- '/label',
- 'input' => ['name' => 'data[Model][field]', 'type' => 'text', 'value' => 'My value', 'id' => 'ModelField'],
- '/div'
- ]);
- $this->Form->request->data['Model']['field'] = "My\nvalue";
- $result = $this->Form->input('Model.field', ['type' => 'textarea']);
- //debug($result);
- $this->assertTags($result, [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => 'ModelField'],
- 'Field',
- '/label',
- 'textarea' => ['name' => 'data[Model][field]', 'cols' => '30', 'rows' => 6, 'id' => 'ModelField'],
- "My\nvalue",
- '/textarea',
- '/div'
- ]);
- }
- }
- /**
- * Contact class
- *
- */
- class ContactExt extends CakeTestModel {
- /**
- * UseTable property
- *
- * @var bool
- */
- public $useTable = false;
- /**
- * Default schema
- *
- * @var array
- */
- protected $_schema = [
- 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
- 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
- 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
- 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
- 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
- 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null]
- ];
- /**
- * Validate property
- *
- * @var array
- */
- public $validate = [
- 'non_existing' => [],
- 'idontexist' => [],
- 'imrequired' => ['rule' => ['between', 5, 30], 'allowEmpty' => false],
- 'imrequiredonupdate' => ['notBlank' => ['rule' => 'alphaNumeric', 'on' => 'update']],
- 'imrequiredoncreate' => ['required' => ['rule' => 'alphaNumeric', 'on' => 'create']],
- 'imrequiredonboth' => [
- 'required' => ['rule' => 'alphaNumeric'],
- ],
- 'string_required' => 'notBlank',
- 'imalsorequired' => ['rule' => 'alphaNumeric', 'allowEmpty' => false],
- 'imrequiredtoo' => ['rule' => 'notBlank'],
- 'required_one' => ['required' => ['rule' => ['notBlank']]],
- 'imnotrequired' => ['required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true],
- 'imalsonotrequired' => [
- 'alpha' => ['rule' => 'alphaNumeric', 'allowEmpty' => true],
- 'between' => ['rule' => ['between', 5, 30]],
- ],
- 'imalsonotrequired2' => [
- 'alpha' => ['rule' => 'alphaNumeric', 'allowEmpty' => true],
- 'between' => ['rule' => ['between', 5, 30], 'allowEmpty' => true],
- ],
- 'imnotrequiredeither' => ['required' => true, 'rule' => ['between', 5, 30], 'allowEmpty' => true],
- 'iamrequiredalways' => [
- 'email' => ['rule' => 'email'],
- 'rule_on_create' => ['rule' => ['maxLength', 50], 'on' => 'create'],
- 'rule_on_update' => ['rule' => ['between', 1, 50], 'on' => 'update'],
- ],
- ];
- /**
- * Schema method
- *
- * @return void
- */
- public function setSchema($schema) {
- $this->_schema = $schema;
- }
- }
|