ButtonTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 3.0.0
  13. * @license http://www.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\Button;
  19. /**
  20. * Basic input test.
  21. */
  22. class ButtonTest extends TestCase {
  23. public function setUp() {
  24. parent::setUp();
  25. $templates = [
  26. 'button' => '<button{{attrs}}>{{text}}</button>',
  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. $button = new Button($this->templates);
  37. $result = $button->render(['name' => 'my_input']);
  38. $expected = [
  39. 'button' => ['type' => 'submit', 'name' => 'my_input'],
  40. '/button'
  41. ];
  42. $this->assertTags($result, $expected);
  43. }
  44. /**
  45. * Test render with custom type
  46. *
  47. * @return void
  48. */
  49. public function testRenderType() {
  50. $button = new Button($this->templates);
  51. $data = [
  52. 'name' => 'my_input',
  53. 'type' => 'button',
  54. 'text' => 'Some button'
  55. ];
  56. $result = $button->render($data);
  57. $expected = [
  58. 'button' => ['type' => 'button', 'name' => 'my_input'],
  59. 'Some button',
  60. '/button'
  61. ];
  62. $this->assertTags($result, $expected);
  63. }
  64. /**
  65. * Test render with a text
  66. *
  67. * @return void
  68. */
  69. public function testRenderWithText() {
  70. $button = new Button($this->templates);
  71. $data = [
  72. 'text' => 'Some <value>'
  73. ];
  74. $result = $button->render($data);
  75. $expected = [
  76. 'button' => ['type' => 'submit'],
  77. 'Some <value>',
  78. '/button'
  79. ];
  80. $this->assertTags($result, $expected);
  81. $data['escape'] = true;
  82. $result = $button->render($data);
  83. $expected = [
  84. 'button' => ['type' => 'submit'],
  85. 'Some &lt;value&gt;',
  86. '/button'
  87. ];
  88. $this->assertTags($result, $expected);
  89. }
  90. /**
  91. * Test render with additional attributes.
  92. *
  93. * @return void
  94. */
  95. public function testRenderAttributes() {
  96. $button = new Button($this->templates);
  97. $data = [
  98. 'name' => 'my_input',
  99. 'text' => 'Go',
  100. 'class' => 'btn',
  101. 'required' => true
  102. ];
  103. $result = $button->render($data);
  104. $expected = [
  105. 'button' => [
  106. 'type' => 'submit',
  107. 'name' => 'my_input',
  108. 'class' => 'btn',
  109. 'required' => 'required'
  110. ],
  111. 'Go',
  112. '/button'
  113. ];
  114. $this->assertTags($result, $expected);
  115. }
  116. }