BasicWidgetTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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\BasicWidget;
  19. /**
  20. * Basic input test.
  21. */
  22. class BasicWidgetTest extends TestCase
  23. {
  24. public function setUp()
  25. {
  26. parent::setUp();
  27. $templates = [
  28. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
  29. ];
  30. $this->templates = new StringTemplate($templates);
  31. $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  32. }
  33. /**
  34. * Test render in a simple case.
  35. *
  36. * @return void
  37. */
  38. public function testRenderSimple()
  39. {
  40. $text = new BasicWidget($this->templates);
  41. $result = $text->render(['name' => 'my_input'], $this->context);
  42. $expected = [
  43. 'input' => ['type' => 'text', 'name' => 'my_input']
  44. ];
  45. $this->assertHtml($expected, $result);
  46. }
  47. /**
  48. * Test render with custom type
  49. *
  50. * @return void
  51. */
  52. public function testRenderType()
  53. {
  54. $text = new BasicWidget($this->templates);
  55. $data = [
  56. 'name' => 'my_input',
  57. 'type' => 'email',
  58. ];
  59. $result = $text->render($data, $this->context);
  60. $expected = [
  61. 'input' => ['type' => 'email', 'name' => 'my_input']
  62. ];
  63. $this->assertHtml($expected, $result);
  64. }
  65. /**
  66. * Test render with a value
  67. *
  68. * @return void
  69. */
  70. public function testRenderWithValue()
  71. {
  72. $text = new BasicWidget($this->templates);
  73. $data = [
  74. 'name' => 'my_input',
  75. 'type' => 'email',
  76. 'val' => 'Some <value>'
  77. ];
  78. $result = $text->render($data, $this->context);
  79. $expected = [
  80. 'input' => [
  81. 'type' => 'email',
  82. 'name' => 'my_input',
  83. 'value' => 'Some &lt;value&gt;'
  84. ]
  85. ];
  86. $this->assertHtml($expected, $result);
  87. }
  88. /**
  89. * Test render with additional attributes.
  90. *
  91. * @return void
  92. */
  93. public function testRenderAttributes()
  94. {
  95. $text = new BasicWidget($this->templates);
  96. $data = [
  97. 'name' => 'my_input',
  98. 'type' => 'email',
  99. 'class' => 'form-control',
  100. 'required' => true
  101. ];
  102. $result = $text->render($data, $this->context);
  103. $expected = [
  104. 'input' => [
  105. 'type' => 'email',
  106. 'name' => 'my_input',
  107. 'class' => 'form-control',
  108. 'required' => 'required',
  109. ]
  110. ];
  111. $this->assertHtml($expected, $result);
  112. }
  113. /**
  114. * Test render with template params.
  115. *
  116. * @return void
  117. */
  118. public function testRenderTemplateParams()
  119. {
  120. $text = new BasicWidget(new StringTemplate([
  121. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}><span>{{help}}</span>',
  122. ]));
  123. $data = [
  124. 'name' => 'my_input',
  125. 'type' => 'email',
  126. 'class' => 'form-control',
  127. 'required' => true,
  128. 'templateVars' => ['help' => 'SOS']
  129. ];
  130. $result = $text->render($data, $this->context);
  131. $expected = [
  132. 'input' => [
  133. 'type' => 'email',
  134. 'name' => 'my_input',
  135. 'class' => 'form-control',
  136. 'required' => 'required',
  137. ],
  138. '<span', 'SOS', '/span'
  139. ];
  140. $this->assertHtml($expected, $result);
  141. }
  142. }