ButtonTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. $this->context = $this->getMock('Cake\View\Form\ContextInterface');
  30. }
  31. /**
  32. * Test render in a simple case.
  33. *
  34. * @return void
  35. */
  36. public function testRenderSimple() {
  37. $button = new Button($this->templates);
  38. $result = $button->render(['name' => 'my_input'], $this->context);
  39. $expected = [
  40. 'button' => ['type' => 'submit', 'name' => 'my_input'],
  41. '/button'
  42. ];
  43. $this->assertTags($result, $expected);
  44. }
  45. /**
  46. * Test render with custom type
  47. *
  48. * @return void
  49. */
  50. public function testRenderType() {
  51. $button = new Button($this->templates);
  52. $data = [
  53. 'name' => 'my_input',
  54. 'type' => 'button',
  55. 'text' => 'Some button'
  56. ];
  57. $result = $button->render($data, $this->context);
  58. $expected = [
  59. 'button' => ['type' => 'button', 'name' => 'my_input'],
  60. 'Some button',
  61. '/button'
  62. ];
  63. $this->assertTags($result, $expected);
  64. }
  65. /**
  66. * Test render with a text
  67. *
  68. * @return void
  69. */
  70. public function testRenderWithText() {
  71. $button = new Button($this->templates);
  72. $data = [
  73. 'text' => 'Some <value>'
  74. ];
  75. $result = $button->render($data, $this->context);
  76. $expected = [
  77. 'button' => ['type' => 'submit'],
  78. 'Some <value>',
  79. '/button'
  80. ];
  81. $this->assertTags($result, $expected);
  82. $data['escape'] = true;
  83. $result = $button->render($data, $this->context);
  84. $expected = [
  85. 'button' => ['type' => 'submit'],
  86. 'Some &lt;value&gt;',
  87. '/button'
  88. ];
  89. $this->assertTags($result, $expected);
  90. }
  91. /**
  92. * Test render with additional attributes.
  93. *
  94. * @return void
  95. */
  96. public function testRenderAttributes() {
  97. $button = new Button($this->templates);
  98. $data = [
  99. 'name' => 'my_input',
  100. 'text' => 'Go',
  101. 'class' => 'btn',
  102. 'required' => true
  103. ];
  104. $result = $button->render($data, $this->context);
  105. $expected = [
  106. 'button' => [
  107. 'type' => 'submit',
  108. 'name' => 'my_input',
  109. 'class' => 'btn',
  110. 'required' => 'required'
  111. ],
  112. 'Go',
  113. '/button'
  114. ];
  115. $this->assertTags($result, $expected);
  116. }
  117. }