[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Http / ServerRequestFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Http;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Http\ServerRequestFactory;
18 use TYPO3\CMS\Core\Http\UploadedFile;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Testcase for \TYPO3\CMS\Core\Http\ServerRequestFactory
23 */
24 class ServerRequestFactoryTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
25 {
26 /**
27 * Set up
28 */
29 protected function setUp()
30 {
31 GeneralUtility::flushInternalRuntimeCaches();
32 }
33
34 /**
35 * @test
36 */
37 public function uploadedFilesAreNormalizedFromFilesSuperGlobal()
38 {
39 $_SERVER['HTTP_HOST'] = 'localhost';
40 $_SERVER['REQUEST_URI'] = '/index.php';
41 $_FILES = [
42 'tx_uploadexample_piexample' => [
43 'name' => [
44 'newExample' => [
45 'image' => 'o51pb.jpg',
46 'imageCollection' => [
47 0 => 'composer.json',
48 ],
49 ],
50 ],
51 'type' => [
52 'newExample' => [
53 'image' => 'image/jpeg',
54 'imageCollection' => [
55 0 => 'application/json'
56 ]
57 ]
58 ],
59 'tmp_name' => [
60 'newExample' => [
61 'image' => '/Applications/MAMP/tmp/php/phphXdbcd',
62 'imageCollection' => [
63 0 => '/Applications/MAMP/tmp/php/phpgrZ4bb'
64 ]
65 ]
66 ],
67 'error' => [
68 'newExample' => [
69 'image' => 0,
70 'imageCollection' => [
71 0 => 0
72 ]
73 ]
74 ],
75 'size' => [
76 'newExample' => [
77 'image' => 59065,
78 'imageCollection' => [
79 0 => 683
80 ]
81 ]
82 ]
83 ]
84 ];
85
86 $uploadedFiles = ServerRequestFactory::fromGlobals()->getUploadedFiles();
87
88 $this->assertNotEmpty($uploadedFiles['tx_uploadexample_piexample']['newExample']['image']);
89 $this->assertTrue($uploadedFiles['tx_uploadexample_piexample']['newExample']['image'] instanceof UploadedFile);
90 $this->assertNotEmpty($uploadedFiles['tx_uploadexample_piexample']['newExample']['imageCollection'][0]);
91 $this->assertTrue($uploadedFiles['tx_uploadexample_piexample']['newExample']['imageCollection'][0] instanceof UploadedFile);
92 }
93
94 /**
95 * @test
96 */
97 public function uploadedFilesAreNotCreatedForEmptyFilesArray()
98 {
99 $_SERVER['HTTP_HOST'] = 'localhost';
100 $_SERVER['REQUEST_URI'] = '/index.php';
101 $_FILES = [];
102
103 $uploadedFiles = ServerRequestFactory::fromGlobals()->getUploadedFiles();
104
105 $this->assertEmpty($uploadedFiles);
106 }
107
108 /**
109 * @test
110 */
111 public function uploadedFilesAreNotCreatedIfTmpNameIsEmpty()
112 {
113 $_SERVER['HTTP_HOST'] = 'localhost';
114 $_SERVER['REQUEST_URI'] = '/index.php';
115 $_FILES = [
116 'tx_uploadexample_piexample' => [
117 'name' => '',
118 'tmp_name' => '',
119 'error' => 4,
120 'size' => 0,
121 ],
122 ];
123
124 $uploadedFiles = ServerRequestFactory::fromGlobals()->getUploadedFiles();
125
126 $this->assertEmpty($uploadedFiles);
127 }
128 }