ButtonWidgetTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\ButtonWidget;
  21. /**
  22. * Basic input test.
  23. */
  24. class ButtonWidgetTest 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. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $templates = [
  38. 'button' => '<button{{attrs}}>{{text}}</button>',
  39. ];
  40. $this->templates = new StringTemplate($templates);
  41. $this->context = new NullContext([]);
  42. }
  43. /**
  44. * Test render in a simple case.
  45. */
  46. public function testRenderSimple(): void
  47. {
  48. $button = new ButtonWidget($this->templates);
  49. $result = $button->render(['name' => 'my_input'], $this->context);
  50. $expected = [
  51. 'button' => ['type' => 'submit', 'name' => 'my_input'],
  52. '/button',
  53. ];
  54. $this->assertHtml($expected, $result);
  55. }
  56. /**
  57. * Test render with custom type
  58. */
  59. public function testRenderType(): void
  60. {
  61. $button = new ButtonWidget($this->templates);
  62. $data = [
  63. 'name' => 'my_input',
  64. 'type' => 'button',
  65. 'text' => 'Some button',
  66. ];
  67. $result = $button->render($data, $this->context);
  68. $expected = [
  69. 'button' => ['type' => 'button', 'name' => 'my_input'],
  70. 'Some button',
  71. '/button',
  72. ];
  73. $this->assertHtml($expected, $result);
  74. }
  75. /**
  76. * Test render with a text
  77. */
  78. public function testRenderWithText(): void
  79. {
  80. $button = new ButtonWidget($this->templates);
  81. $data = [
  82. 'text' => 'Some <value>',
  83. 'onclick' => '<escape me>',
  84. ];
  85. $result = $button->render($data, $this->context);
  86. $expected = [
  87. 'button' => ['type' => 'submit', 'onclick' => '&lt;escape me&gt;'],
  88. 'Some &lt;value&gt;',
  89. '/button',
  90. ];
  91. $this->assertHtml($expected, $result);
  92. $data['escapeTitle'] = false;
  93. $result = $button->render($data, $this->context);
  94. $expected = [
  95. 'button' => ['type' => 'submit', 'onclick' => '&lt;escape me&gt;'],
  96. 'Some <value>',
  97. '/button',
  98. ];
  99. $this->assertHtml($expected, $result);
  100. }
  101. /**
  102. * Test render with additional attributes.
  103. */
  104. public function testRenderAttributes(): void
  105. {
  106. $button = new ButtonWidget($this->templates);
  107. $data = [
  108. 'name' => 'my_input',
  109. 'text' => 'Go',
  110. 'class' => 'btn',
  111. 'required' => true,
  112. ];
  113. $result = $button->render($data, $this->context);
  114. $expected = [
  115. 'button' => [
  116. 'type' => 'submit',
  117. 'name' => 'my_input',
  118. 'class' => 'btn',
  119. 'required' => 'required',
  120. ],
  121. 'Go',
  122. '/button',
  123. ];
  124. $this->assertHtml($expected, $result);
  125. }
  126. /**
  127. * Ensure templateVars option is hooked up.
  128. */
  129. public function testRenderTemplateVars(): void
  130. {
  131. $this->templates->add([
  132. 'button' => '<button {{attrs}} custom="{{custom}}">{{text}}</button>',
  133. ]);
  134. $button = new ButtonWidget($this->templates);
  135. $data = [
  136. 'templateVars' => ['custom' => 'value'],
  137. 'text' => 'Go',
  138. ];
  139. $result = $button->render($data, $this->context);
  140. $expected = [
  141. 'button' => [
  142. 'type' => 'submit',
  143. 'custom' => 'value',
  144. ],
  145. 'Go',
  146. '/button',
  147. ];
  148. $this->assertHtml($expected, $result);
  149. }
  150. }