TextareaWidgetTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Widget;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\View\StringTemplate;
  18. use Cake\View\Widget\TextareaWidget;
  19. /**
  20. * Textarea input test.
  21. */
  22. class TextareaWidgetTest extends TestCase
  23. {
  24. /**
  25. * setup
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $templates = [
  33. 'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
  34. ];
  35. $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  36. $this->templates = new StringTemplate($templates);
  37. }
  38. /**
  39. * Test render in a simple case.
  40. *
  41. * @return void
  42. */
  43. public function testRenderSimple()
  44. {
  45. $input = new TextareaWidget($this->templates);
  46. $result = $input->render(['name' => 'comment'], $this->context);
  47. $expected = [
  48. 'textarea' => ['name' => 'comment', 'rows' => 5],
  49. '/textarea',
  50. ];
  51. $this->assertHtml($expected, $result);
  52. }
  53. /**
  54. * Test render with a value
  55. *
  56. * @return void
  57. */
  58. public function testRenderWithValue()
  59. {
  60. $input = new TextareaWidget($this->templates);
  61. $data = ['name' => 'comment', 'data-foo' => '<val>', 'val' => 'some <html>'];
  62. $result = $input->render($data, $this->context);
  63. $expected = [
  64. 'textarea' => ['name' => 'comment', 'rows' => 5, 'data-foo' => '&lt;val&gt;'],
  65. 'some &lt;html&gt;',
  66. '/textarea',
  67. ];
  68. $this->assertHtml($expected, $result);
  69. $data['escape'] = false;
  70. $result = $input->render($data, $this->context);
  71. $expected = [
  72. 'textarea' => ['name' => 'comment', 'rows' => 5, 'data-foo' => '<val>'],
  73. 'some <html>',
  74. '/textarea',
  75. ];
  76. $this->assertHtml($expected, $result);
  77. }
  78. /**
  79. * Ensure templateVars option is hooked up.
  80. *
  81. * @return void
  82. */
  83. public function testRenderTemplateVars()
  84. {
  85. $this->templates->add([
  86. 'textarea' => '<textarea custom="{{custom}}" name="{{name}}"{{attrs}}>{{value}}</textarea>',
  87. ]);
  88. $input = new TextareaWidget($this->templates);
  89. $data = [
  90. 'templateVars' => ['custom' => 'value'],
  91. 'name' => 'comment',
  92. 'val' => 'body'
  93. ];
  94. $result = $input->render($data, $this->context);
  95. $expected = [
  96. 'textarea' => ['name' => 'comment', 'rows' => 5, 'custom' => 'value'],
  97. 'body',
  98. '/textarea',
  99. ];
  100. $this->assertHtml($expected, $result);
  101. }
  102. }