FileWidgetTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Widget;
  17. use Cake\Core\Configure;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\View\Form\NullContext;
  20. use Cake\View\StringTemplate;
  21. use Cake\View\Widget\FileWidget;
  22. /**
  23. * File input test.
  24. */
  25. class FileWidgetTest extends TestCase
  26. {
  27. /**
  28. * @var \Cake\View\Form\NullContext
  29. */
  30. protected $context;
  31. /**
  32. * @var \Cake\View\StringTemplate
  33. */
  34. protected $templates;
  35. /**
  36. * setup
  37. */
  38. public function setUp(): void
  39. {
  40. parent::setUp();
  41. $templates = [
  42. 'file' => '<input type="file" name="{{name}}"{{attrs}}>',
  43. ];
  44. $this->templates = new StringTemplate($templates);
  45. $this->context = new NullContext([]);
  46. }
  47. /**
  48. * Test render in a simple case.
  49. */
  50. public function testRenderSimple(): void
  51. {
  52. $input = new FileWidget($this->templates);
  53. $result = $input->render(['name' => 'image'], $this->context);
  54. $expected = [
  55. 'input' => ['type' => 'file', 'name' => 'image'],
  56. ];
  57. $this->assertHtml($expected, $result);
  58. }
  59. /**
  60. * Test render with a value
  61. */
  62. public function testRenderAttributes(): void
  63. {
  64. $input = new FileWidget($this->templates);
  65. $data = ['name' => 'image', 'required' => true, 'val' => 'nope'];
  66. $result = $input->render($data, $this->context);
  67. $expected = [
  68. 'input' => ['type' => 'file', 'required' => 'required', 'name' => 'image'],
  69. ];
  70. $this->assertHtml($expected, $result);
  71. }
  72. /**
  73. * Ensure templateVars option is hooked up.
  74. */
  75. public function testRenderTemplateVars(): void
  76. {
  77. $this->templates->add([
  78. 'file' => '<input custom="{{custom}}" type="file" name="{{name}}"{{attrs}}>',
  79. ]);
  80. $input = new FileWidget($this->templates);
  81. $data = [
  82. 'templateVars' => ['custom' => 'value'],
  83. 'name' => 'files',
  84. ];
  85. $result = $input->render($data, $this->context);
  86. $expected = [
  87. 'input' => [
  88. 'type' => 'file',
  89. 'name' => 'files',
  90. 'custom' => 'value',
  91. ],
  92. ];
  93. $this->assertHtml($expected, $result);
  94. }
  95. /**
  96. * Test secureFields
  97. */
  98. public function testSecureFields(): void
  99. {
  100. $input = new FileWidget($this->templates);
  101. $data = ['name' => 'image', 'required' => true, 'val' => 'nope'];
  102. $this->assertEquals(['image'], $input->secureFields($data));
  103. Configure::write('App.uploadedFilesAsObjects', false);
  104. $this->assertEquals(
  105. ['image[name]', 'image[type]', 'image[tmp_name]', 'image[error]', 'image[size]'],
  106. $input->secureFields($data)
  107. );
  108. }
  109. }