TextareaWidgetTest.php 3.3 KB

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