TextTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Input;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\View\Input\Text;
  18. use Cake\View\StringTemplate;
  19. /**
  20. * Text input test.
  21. */
  22. class TextTest extends TestCase {
  23. public function setUp() {
  24. parent::setUp();
  25. $templates = [
  26. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
  27. ];
  28. $this->templates = new StringTemplate($templates);
  29. }
  30. /**
  31. * Test render in a simple case.
  32. *
  33. * @return void
  34. */
  35. public function testRenderSimple() {
  36. $text = new Text($this->templates);
  37. $result = $text->render(['name' => 'my_input']);
  38. $expected = [
  39. 'input' => ['type' => 'text', 'name' => 'my_input']
  40. ];
  41. $this->assertTags($result, $expected);
  42. }
  43. /**
  44. * Test render with custom type
  45. *
  46. * @return void
  47. */
  48. public function testRenderType() {
  49. $text = new Text($this->templates);
  50. $data = [
  51. 'name' => 'my_input',
  52. 'type' => 'email',
  53. ];
  54. $result = $text->render($data);
  55. $expected = [
  56. 'input' => ['type' => 'email', 'name' => 'my_input']
  57. ];
  58. $this->assertTags($result, $expected);
  59. }
  60. /**
  61. * Test render with a value
  62. *
  63. * @return void
  64. */
  65. public function testRenderWithValue() {
  66. $text = new Text($this->templates);
  67. $data = [
  68. 'name' => 'my_input',
  69. 'type' => 'email',
  70. 'val' => 'Some <value>'
  71. ];
  72. $result = $text->render($data);
  73. $expected = [
  74. 'input' => [
  75. 'type' => 'email',
  76. 'name' => 'my_input',
  77. 'value' => 'Some &lt;value&gt;'
  78. ]
  79. ];
  80. $this->assertTags($result, $expected);
  81. }
  82. /**
  83. * Test render with additional attributes.
  84. *
  85. * @return void
  86. */
  87. public function testRenderAttributes() {
  88. $text = new Text($this->templates);
  89. $data = [
  90. 'name' => 'my_input',
  91. 'type' => 'email',
  92. 'class' => 'form-control',
  93. 'required' => true
  94. ];
  95. $result = $text->render($data);
  96. $expected = [
  97. 'input' => [
  98. 'type' => 'email',
  99. 'name' => 'my_input',
  100. 'class' => 'form-control',
  101. 'required' => 'required',
  102. ]
  103. ];
  104. $this->assertTags($result, $expected);
  105. }
  106. }