FileWidgetTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\FileWidget;
  19. /**
  20. * File input test.
  21. */
  22. class FileWidgetTest extends TestCase
  23. {
  24. /**
  25. * setup
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $templates = [
  33. 'file' => '<input type="file" name="{{name}}"{{attrs}}>',
  34. ];
  35. $this->templates = new StringTemplate($templates);
  36. $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  37. }
  38. /**
  39. * Test render in a simple case.
  40. *
  41. * @return void
  42. */
  43. public function testRenderSimple()
  44. {
  45. $input = new FileWidget($this->templates);
  46. $result = $input->render(['name' => 'image'], $this->context);
  47. $expected = [
  48. 'input' => ['type' => 'file', 'name' => 'image'],
  49. ];
  50. $this->assertHtml($expected, $result);
  51. }
  52. /**
  53. * Test render with a value
  54. *
  55. * @return void
  56. */
  57. public function testRenderAttributes()
  58. {
  59. $input = new FileWidget($this->templates);
  60. $data = ['name' => 'image', 'required' => true, 'val' => 'nope'];
  61. $result = $input->render($data, $this->context);
  62. $expected = [
  63. 'input' => ['type' => 'file', 'required' => 'required', 'name' => 'image'],
  64. ];
  65. $this->assertHtml($expected, $result);
  66. }
  67. /**
  68. * Ensure templateVars option is hooked up.
  69. *
  70. * @return void
  71. */
  72. public function testRenderTemplateVars()
  73. {
  74. $this->templates->add([
  75. 'file' => '<input custom="{{custom}}" type="file" name="{{name}}"{{attrs}}>',
  76. ]);
  77. $input = new FileWidget($this->templates);
  78. $data = [
  79. 'templateVars' => ['custom' => 'value'],
  80. 'name' => 'files',
  81. ];
  82. $result = $input->render($data, $this->context);
  83. $expected = [
  84. 'input' => [
  85. 'type' => 'file',
  86. 'name' => 'files',
  87. 'custom' => 'value',
  88. ],
  89. ];
  90. $this->assertHtml($expected, $result);
  91. }
  92. }