ButtonWidgetTest.php 3.5 KB

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