BasicTest.php 2.6 KB

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