/usr/share/php/tests/Horde_View/Horde/View/Helper/FormTest.php is in php-horde-view 2.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | <?php
/**
* Copyright 2007-2008 Maintainable Software, LLC
* Copyright 2008-2013 Horde LLC (http://www.horde.org/)
*
* @author Mike Naberezny <mike@maintainable.com>
* @author Derek DeVries <derek@maintainable.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package View
* @subpackage UnitTests
*/
/**
* @group view
* @author Mike Naberezny <mike@maintainable.com>
* @author Derek DeVries <derek@maintainable.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @license http://www.horde.org/licenses/bsd
* @category Horde
* @package View
* @subpackage UnitTests
*/
class Horde_View_Helper_FormTest extends Horde_Test_Case
{
public function setUp()
{
$this->view = new Horde_View();
$this->view->addHelper('Form');
$this->view->addHelper('FormTag');
$this->view->addHelper('Tag');
$this->view->addHelper(new Horde_View_Helper_FormTest_MockUrlHelper($this->view));
$this->post = (object)array('title', 'authorName', 'body',
'secret', 'writtenOn', 'cost');
$this->post->title = 'Hello World';
$this->post->authorName = '';
$this->post->body = 'Back to the hill and over it again!';
$this->post->secret = 1;
$this->post->writtenOn = mktime(2004, 6, 15);
$this->post->id = 123;
$this->post->id_before_type_cast = 123;
$this->view->post = $this->post;
}
public function testTextField()
{
$this->assertEquals(
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />',
$this->view->textField('post', 'title'));
$this->assertEquals(
'<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />',
$this->view->passwordField('post', 'title'));
$this->assertEquals(
'<input id="person_name" name="person[name]" size="30" type="password" />',
$this->view->passwordField("person", "name"));
}
public function testTextFieldWithEscapes()
{
$this->post->title = '<b>Hello World</b>';
$this->assertEquals(
'<input id="post_title" name="post[title]" size="30" type="text" value="<b>Hello World</b>" />',
$this->view->textField('post', 'title'));
}
public function testTextFieldWithOptions()
{
$expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />';
$this->assertEquals($expected, $this->view->textField('post', 'title', array('size' => 35)));
}
public function testTextFieldAssumingSize()
{
$expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />';
$this->assertEquals($expected, $this->view->textField('post', 'title', array('maxlength' => 35)));
}
public function testTextFieldDoesntChangeParamValues()
{
$objectName = 'post[]';
$expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />';
$this->assertEquals($expected, $this->view->textField($objectName, 'title'));
$this->assertEquals($objectName, 'post[]');
}
public function testCheckBox()
{
$this->assertEquals(
'<input name="post[secret]" type="hidden" value="0" /><input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />',
$this->view->checkBox('post', 'secret'));
$this->post->secret = 0;
$this->assertEquals(
'<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
$this->view->checkBox('post', 'secret'));
$this->assertEquals(
'<input name="post[secret]" type="hidden" value="0" /><input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />',
$this->view->checkBox('post', 'secret', array('checked' => true)));
$this->post->secret = true;
$this->assertEquals(
'<input name="post[secret]" type="hidden" value="0" /><input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />',
$this->view->checkBox('post', 'secret'));
}
public function testCheckBoxWithExplicitCheckedAndUncheckedValues()
{
$this->post->secret = 'on';
$this->assertEquals(
'<input name="post[secret]" type="hidden" value="off" /><input checked id="post_secret" name="post[secret]" type="checkbox" value="on" />',
$this->view->checkBox('post', 'secret', array(), 'on', 'off'));
}
public function testRadioButton()
{
$this->assertEquals(
'<input checked id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
$this->view->radioButton('post', 'title', 'Hello World'));
$this->assertEquals(
'<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
$this->view->radioButton('post', 'title', 'Goodbye World'));
}
public function testRadioButtonIsCheckedWithIntegers()
{
$this->assertEquals(
'<input checked id="post_secret_1" name="post[secret]" type="radio" value="1" />',
$this->view->radioButton('post', 'secret', '1'));
}
public function testRadioButtonRespectsPassedInId()
{
$this->assertEquals(
'<input checked id="foo" name="post[secret]" type="radio" value="1" />',
$this->view->radioButton('post', 'secret', '1', array('id' => 'foo')));
}
public function testTextArea()
{
$this->assertEquals(
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
$this->view->textArea('post', 'body'));
}
public function testTextAreaWithEscapes()
{
$this->post->body = "Back to <i>the</i> hill and over it again!";
$this->assertEquals(
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to <i>the</i> hill and over it again!</textarea>',
$this->view->textArea('post', 'body'));
}
public function testTextAreaWithAlternateValue()
{
$this->assertEquals(
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
$this->view->textArea('post', 'body', array('value' => 'Testing alternate values.')));
}
public function testTextAreaWithSizeOption()
{
$this->assertEquals(
'<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
$this->view->textArea('post', 'body', array('size' => '183x820')));
}
public function testExplicitName()
{
$this->assertEquals(
'<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />',
$this->view->textField("post", "title", array("name" => "dont guess")));
$this->assertEquals(
'<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
$this->view->textArea("post", "body", array("name" => "really!")));
$this->assertEquals(
'<input name="i mean it" type="hidden" value="0" /><input checked id="post_secret" name="i mean it" type="checkbox" value="1" />',
$this->view->checkBox("post", "secret", array("name" => "i mean it")));
}
public function testExplicitId()
{
$this->assertEquals(
'<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />',
$this->view->textField("post", "title", array("id" => "dont guess")));
$this->assertEquals(
'<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
$this->view->textArea("post", "body", array("id" => "really!")));
$this->assertEquals(
'<input name="post[secret]" type="hidden" value="0" /><input checked id="i mean it" name="post[secret]" type="checkbox" value="1" />',
$this->view->checkBox("post", "secret", array("id" => "i mean it")));
}
public function testAutoIndex()
{
$pid = $this->post->id;
$this->assertEquals(
"<input id=\"post_{$pid}_title\" name=\"post[{$pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />",
$this->view->textField("post[]", "title"));
$this->assertEquals(
"<textarea cols=\"40\" id=\"post_{$pid}_body\" name=\"post[{$pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
$this->view->textArea("post[]", "body"));
$this->assertEquals(
"<input name=\"post[{$pid}][secret]\" type=\"hidden\" value=\"0\" /><input checked id=\"post_{$pid}_secret\" name=\"post[{$pid}][secret]\" type=\"checkbox\" value=\"1\" />",
$this->view->checkBox('post[]', 'secret'));
$this->assertEquals(
"<input checked id=\"post_{$pid}_title_hello_world\" name=\"post[{$pid}][title]\" type=\"radio\" value=\"Hello World\" />",
$this->view->radioButton('post[]', 'title', 'Hello World'));
$this->assertEquals(
"<input id=\"post_{$pid}_title_goodbye_world\" name=\"post[{$pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
$this->view->radioButton('post[]', 'title', 'Goodbye World'));
}
public function testFormFor()
{
ob_start();
$form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post')));
echo $form->textField('title');
echo $form->textArea('body');
echo $form->checkBox('secret');
echo $form->submit('Create post');
$form->end();
$expected =
'<form action="http://www.example.com" id="create-post" method="post">' .
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
'<input name="post[secret]" type="hidden" value="0" />' .
'<input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
'<input id="post_submit" name="commit" type="submit" value="Create post" />' .
"</form>";
$this->assertEquals($expected, ob_get_clean());
}
public function testFormForWithMethod()
{
ob_start();
$form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post',
'method' => 'put')));
echo $form->textField('title');
echo $form->textArea('body');
echo $form->checkBox('secret');
$form->end();
$expected =
'<form action="http://www.example.com" id="create-post" method="post">' .
'<div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>' .
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
'<input name="post[secret]" type="hidden" value="0" />' .
'<input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
"</form>";
$this->assertEquals($expected, ob_get_clean());
}
public function testFormForWithoutObject()
{
ob_start();
$form = $this->view->formFor('post', array('html' => array('id' => 'create-post')));
echo $form->textField('title');
echo $form->textArea('body');
echo $form->checkBox('secret');
$form->end();
$expected =
'<form action="http://www.example.com" id="create-post" method="post">' .
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
'<input name="post[secret]" type="hidden" value="0" />' .
'<input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />' .
"</form>";
$this->assertEquals($expected, ob_get_clean());
}
public function testFormForWithIndex()
{
ob_start();
$form = $this->view->formFor('post[]', $this->post);
echo $form->textField('title');
echo $form->textArea('body');
echo $form->checkBox('secret');
$form->end();
$expected =
'<form action="http://www.example.com" method="post">' .
'<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />' .
'<textarea cols="40" id="post_123_body" name="post[123][body]" rows="20">Back to the hill and over it again!</textarea>' .
'<input name="post[123][secret]" type="hidden" value="0" />' .
'<input checked id="post_123_secret" name="post[123][secret]" type="checkbox" value="1" />' .
'</form>';
$this->assertEquals($expected, ob_get_clean());
}
public function testFieldsFor()
{
ob_start();
$fields = $this->view->fieldsFor('post', $this->post);
echo $fields->textField('title');
echo $fields->textArea('body');
echo $fields->checkBox('secret');
$fields->end();
$expected =
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
'<input name="post[secret]" type="hidden" value="0" />' .
'<input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />';
$this->assertEquals($expected, ob_get_clean());
}
public function testNestedFieldsFor()
{
ob_start();
$form = $this->view->formFor('post', $this->post);
$fields = $form->fieldsFor('comment', $this->post);
echo $fields->textField('title');
$fields->end();
$form->end();
$expected =
'<form action="http://www.example.com" method="post">' .
'<input id="post_comment_title" name="post[comment][title]" size="30" type="text" value="Hello World" />' .
'</form>';
$this->assertEquals($expected, ob_get_clean());
}
public function testFieldsForWithoutObject()
{
ob_start();
$fields = $this->view->fieldsFor('post');
echo $fields->textField('title');
echo $fields->textArea('body');
echo $fields->checkBox('secret');
$fields->end();
$expected =
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
'<input name="post[secret]" type="hidden" value="0" />' .
'<input checked id="post_secret" name="post[secret]" type="checkbox" value="1" />';
$this->assertEquals($expected, ob_get_clean());
}
public function testFieldsForObjectWithBracketedName()
{
ob_start();
$fields = $this->view->fieldsFor('author[post]', $this->post);
echo $fields->textField('title');
$fields->end();
$this->assertEquals(
'<input id="author_post_title" name="author[post][title]" size="30" type="text" value="Hello World" />',
ob_get_clean());
}
public function testFormBuilderDoesNotHaveFormForMethod()
{
$methods = get_class_methods('Horde_View_Helper_Form_Builder');
$this->assertTrue(empty($methods['formFor']));
}
public function testFormForAndFieldsFor()
{
ob_start();
$postForm = $this->view->formFor('post', $this->post, array('html' => array('id' => 'create-post')));
echo $postForm->textField('title');
echo $postForm->textArea('body');
$parentFields = $this->view->fieldsFor('parent_post', $this->post);
echo $parentFields->checkBox('secret');
$parentFields->end();
$postForm->end();
$expected =
'<form action="http://www.example.com" id="create-post" method="post">' .
'<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />' .
'<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>' .
'<input name="parent_post[secret]" type="hidden" value="0" />' .
'<input checked id="parent_post_secret" name="parent_post[secret]" type="checkbox" value="1" />' .
'</form>';
$this->assertEquals($expected, ob_get_clean());
}
public function testFormForWithCustomBuilder()
{
ob_start();
$form = $this->view->formFor('post', $this->post, array('builder' => 'Horde_View_Helper_FormTest_BuilderMock'));
echo $form->textField('bar');
echo $form->foo();
$form->end();
$expected =
'<form action="http://www.example.com" method="post">' .
'<input id="post_bar" name="post[bar]" size="30" type="text" />' .
'<foo /></form>';
$this->assertEquals($expected, ob_get_clean());
}
public function testDefaultFormBuilder()
{
$oldDefaultFormBuilder = Horde_View_Base::$defaultFormBuilder;
Horde_View_Base::$defaultFormBuilder = 'Horde_View_Helper_FormTest_BuilderMock';
try {
ob_start();
$form = $this->view->formFor('post', $this->post);
echo $form->textField('bar');
echo $form->foo();
$form->end();
$expected =
'<form action="http://www.example.com" method="post">' .
'<input id="post_bar" name="post[bar]" size="30" type="text" />' .
'<foo /></form>';
$this->assertEquals($expected, ob_get_clean());
} catch (Exception $e) {}
Horde_View_Base::$defaultFormBuilder = $oldDefaultFormBuilder;
}
// @todo test_default_form_builder_with_active_record_helpers
// @todo test_remote_form_for_with_labelled_builder
public function testFieldsForWithCustomBuilder()
{
ob_start();
$fields = $this->view->fieldsFor('post', $this->post, array('builder' => 'Horde_View_Helper_FormTest_BuilderMock'));
echo $fields->textField('bar');
echo $fields->foo();
$fields->end();
$this->assertEquals(
'<input id="post_bar" name="post[bar]" size="30" type="text" /><foo />',
ob_get_clean());
}
public function testFormForWithHtmlOptionsAddsOptionsToFormTag()
{
ob_start();
$form = $this->view->formFor('post', $this->post, array('html' => array('id' => 'some_form',
'class' => 'some_class')));
$form->end();
$this->assertEquals(
'<form action="http://www.example.com" class="some_class" id="some_form" method="post"></form>',
ob_get_clean());
}
public function testFormForWithHiddenField()
{
ob_start();
$form = $this->view->formFor('post', $this->post);
echo $form->hiddenField('title');
$form->end();
$expected =
'<form action="http://www.example.com" method="post">' .
'<input id="post_title" name="post[title]" type="hidden" value="Hello World" />' .
'</form>';
$this->assertEquals($expected, ob_get_clean());
}
public function testFormForWithFileField()
{
ob_start();
$form = $this->view->formFor('post', $this->post);
echo $form->fileField('title');
$form->end();
$expected =
'<form action="http://www.example.com" method="post">' .
'<input id="post_title" name="post[title]" size="30" type="file" />' .
'</form>';
$this->assertEquals($expected, ob_get_clean());
}
// @todo test_form_for_with_string_url_option
// @todo test_form_for_with_hash_url_option
// @todo test_remote_form_for_with_html_options_adds_options_to_form_tag
}
class Horde_View_Helper_FormTest_MockUrlHelper extends Horde_View_Helper_Base
{
public function urlFor($options)
{
return 'http://www.example.com';
}
}
class Horde_View_Helper_FormTest_BuilderMock extends Horde_View_Helper_Form_Builder
{
public function foo()
{
return '<foo />';
}
}
|