PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/TestCase/Http/RequestTransformerTest.php

http://github.com/cakephp/cakephp
PHP | 313 lines | 226 code | 19 blank | 68 comment | 0 complexity | 9677da39bc3f84171de4809112c21f94 MD5 | raw file
Possible License(s): JSON
  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.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Core\Configure;
  17. use Cake\Http\RequestTransformer;
  18. use Cake\Http\ServerRequestFactory;
  19. use Cake\Network\Request;
  20. use Cake\Network\Session;
  21. use Cake\TestSuite\TestCase;
  22. use Zend\Diactoros\Stream;
  23. /**
  24. * Test for RequestTransformer
  25. */
  26. class RequestTransformerTest extends TestCase
  27. {
  28. /**
  29. * Test transforming GET params.
  30. *
  31. * @return void
  32. */
  33. public function testToCakeGetParams()
  34. {
  35. $psr = ServerRequestFactory::fromGlobals(null, ['a' => 'b', 'c' => ['d' => 'e']]);
  36. $cake = RequestTransformer::toCake($psr);
  37. $this->assertEquals('b', $cake->query('a'));
  38. $this->assertEquals(['d' => 'e'], $cake->query('c'));
  39. $this->assertEmpty($cake->data);
  40. $this->assertEmpty($cake->cookies);
  41. }
  42. /**
  43. * Test transforming POST params.
  44. *
  45. * @return void
  46. */
  47. public function testToCakePostParams()
  48. {
  49. $psr = ServerRequestFactory::fromGlobals(null, null, ['title' => 'first post', 'some' => 'data']);
  50. $cake = RequestTransformer::toCake($psr);
  51. $this->assertEquals('first post', $cake->data('title'));
  52. $this->assertEquals('data', $cake->data('some'));
  53. $this->assertEmpty($cake->query);
  54. $this->assertEmpty($cake->cookies);
  55. }
  56. /**
  57. * Test transforming COOKIE params.
  58. *
  59. * @return void
  60. */
  61. public function testToCakeCookies()
  62. {
  63. $psr = ServerRequestFactory::fromGlobals(null, null, null, ['gtm' => 'watchingyou']);
  64. $cake = RequestTransformer::toCake($psr);
  65. $this->assertEmpty($cake->query);
  66. $this->assertEmpty($cake->data);
  67. $this->assertEquals('watchingyou', $cake->cookie('gtm'));
  68. }
  69. /**
  70. * Test transforming header and server params.
  71. *
  72. * @return void
  73. */
  74. public function testToCakeHeadersAndEnvironment()
  75. {
  76. $server = [
  77. 'HTTPS' => 'on',
  78. 'HTTP_HOST' => 'example.com',
  79. 'REQUEST_METHOD' => 'PATCH',
  80. 'HTTP_ACCEPT' => 'application/json',
  81. 'SERVER_PROTOCOL' => '1.1',
  82. 'SERVER_PORT' => 443,
  83. ];
  84. $psr = ServerRequestFactory::fromGlobals($server);
  85. $cake = RequestTransformer::toCake($psr);
  86. $this->assertEmpty($cake->query);
  87. $this->assertEmpty($cake->data);
  88. $this->assertEmpty($cake->cookie);
  89. $this->assertSame('application/json', $cake->header('accept'));
  90. $this->assertSame('PATCH', $cake->method());
  91. $this->assertSame('https', $cake->scheme());
  92. $this->assertSame(443, $cake->port());
  93. $this->assertSame('example.com', $cake->host());
  94. }
  95. /**
  96. * Test transforming with no routing parameters
  97. * still has the required keys.
  98. *
  99. * @return void
  100. */
  101. public function testToCakeParamsEmpty()
  102. {
  103. $psr = ServerRequestFactory::fromGlobals();
  104. $cake = RequestTransformer::toCake($psr);
  105. $this->assertArrayHasKey('controller', $cake->params);
  106. $this->assertArrayHasKey('action', $cake->params);
  107. $this->assertArrayHasKey('plugin', $cake->params);
  108. $this->assertArrayHasKey('_ext', $cake->params);
  109. $this->assertArrayHasKey('pass', $cake->params);
  110. }
  111. /**
  112. * Test transforming with non-empty params.
  113. *
  114. * @return void
  115. */
  116. public function testToCakeParamsPopulated()
  117. {
  118. $psr = ServerRequestFactory::fromGlobals();
  119. $psr = $psr->withAttribute('params', ['controller' => 'Articles', 'action' => 'index']);
  120. $cake = RequestTransformer::toCake($psr);
  121. $this->assertEmpty($cake->query);
  122. $this->assertEmpty($cake->data);
  123. $this->assertEmpty($cake->cookie);
  124. $this->assertSame('Articles', $cake->param('controller'));
  125. $this->assertSame('index', $cake->param('action'));
  126. $this->assertArrayHasKey('plugin', $cake->params);
  127. $this->assertArrayHasKey('_ext', $cake->params);
  128. $this->assertArrayHasKey('pass', $cake->params);
  129. }
  130. /**
  131. * Test transforming uploaded files
  132. *
  133. * @return void
  134. */
  135. public function testToCakeUploadedFiles()
  136. {
  137. $files = [
  138. 'no_file' => [
  139. 'name' => ['file' => ''],
  140. 'type' => ['file' => ''],
  141. 'tmp_name' => ['file' => ''],
  142. 'error' => ['file' => UPLOAD_ERR_NO_FILE],
  143. 'size' => ['file' => 0]
  144. ],
  145. 'image_main' => [
  146. 'name' => ['file' => 'born on.txt'],
  147. 'type' => ['file' => 'text/plain'],
  148. 'tmp_name' => ['file' => __FILE__],
  149. 'error' => ['file' => 0],
  150. 'size' => ['file' => 17178]
  151. ],
  152. 0 => [
  153. 'name' => ['image' => 'scratch.text'],
  154. 'type' => ['image' => 'text/plain'],
  155. 'tmp_name' => ['image' => __FILE__],
  156. 'error' => ['image' => 0],
  157. 'size' => ['image' => 1490]
  158. ],
  159. 'pictures' => [
  160. 'name' => [
  161. 0 => ['file' => 'a-file.png'],
  162. 1 => ['file' => 'a-moose.png']
  163. ],
  164. 'type' => [
  165. 0 => ['file' => 'image/png'],
  166. 1 => ['file' => 'image/jpg']
  167. ],
  168. 'tmp_name' => [
  169. 0 => ['file' => __FILE__],
  170. 1 => ['file' => __FILE__]
  171. ],
  172. 'error' => [
  173. 0 => ['file' => 0],
  174. 1 => ['file' => 0]
  175. ],
  176. 'size' => [
  177. 0 => ['file' => 17188],
  178. 1 => ['file' => 2010]
  179. ],
  180. ]
  181. ];
  182. $post = [
  183. 'pictures' => [
  184. 0 => ['name' => 'A cat'],
  185. 1 => ['name' => 'A moose']
  186. ],
  187. 0 => [
  188. 'name' => 'A dog'
  189. ]
  190. ];
  191. $psr = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  192. $request = RequestTransformer::toCake($psr);
  193. $expected = [
  194. 'image_main' => [
  195. 'file' => [
  196. 'name' => 'born on.txt',
  197. 'type' => 'text/plain',
  198. 'tmp_name' => __FILE__,
  199. 'error' => 0,
  200. 'size' => 17178,
  201. ]
  202. ],
  203. 'no_file' => [
  204. 'file' => [
  205. 'name' => '',
  206. 'type' => '',
  207. 'tmp_name' => '',
  208. 'error' => UPLOAD_ERR_NO_FILE,
  209. 'size' => 0,
  210. ]
  211. ],
  212. 'pictures' => [
  213. 0 => [
  214. 'name' => 'A cat',
  215. 'file' => [
  216. 'name' => 'a-file.png',
  217. 'type' => 'image/png',
  218. 'tmp_name' => __FILE__,
  219. 'error' => 0,
  220. 'size' => 17188,
  221. ]
  222. ],
  223. 1 => [
  224. 'name' => 'A moose',
  225. 'file' => [
  226. 'name' => 'a-moose.png',
  227. 'type' => 'image/jpg',
  228. 'tmp_name' => __FILE__,
  229. 'error' => 0,
  230. 'size' => 2010,
  231. ]
  232. ]
  233. ],
  234. 0 => [
  235. 'name' => 'A dog',
  236. 'image' => [
  237. 'name' => 'scratch.text',
  238. 'type' => 'text/plain',
  239. 'tmp_name' => __FILE__,
  240. 'error' => 0,
  241. 'size' => 1490
  242. ]
  243. ]
  244. ];
  245. $this->assertEquals($expected, $request->data);
  246. }
  247. /**
  248. * Test that the transformed request sets the session path
  249. * as expected.
  250. *
  251. * @return void
  252. */
  253. public function testToCakeBaseSessionPath()
  254. {
  255. Configure::write('App.baseUrl', false);
  256. $server = [
  257. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  258. 'PHP_SELF' => '/thisapp/webroot/index.php',
  259. 'REQUEST_URI' => '/posts/view/1',
  260. ];
  261. $psr = ServerRequestFactory::fromGlobals($server);
  262. $cake = RequestTransformer::toCake($psr);
  263. $this->assertEquals('/thisapp/', ini_get('session.cookie_path'));
  264. }
  265. /**
  266. * Test transforming session objects
  267. *
  268. * @return void
  269. */
  270. public function testToCakeSession()
  271. {
  272. $psr = ServerRequestFactory::fromGlobals();
  273. $session = new Session(['defaults' => 'php']);
  274. $session->write('test', 'value');
  275. $psr = $psr->withAttribute('session', $session);
  276. $cake = RequestTransformer::toCake($psr);
  277. $this->assertSame($session, $cake->session());
  278. }
  279. /**
  280. * Test transforming request bodies
  281. *
  282. * @return void
  283. */
  284. public function testToCakeRequestBody()
  285. {
  286. $psr = ServerRequestFactory::fromGlobals();
  287. $stream = new Stream('php://memory', 'rw');
  288. $stream->write('{"hello":"world"}');
  289. $stream->rewind();
  290. $psr = $psr->withBody($stream);
  291. $cake = RequestTransformer::toCake($psr);
  292. $this->assertSame(['hello' => 'world'], $cake->input('json_decode', true));
  293. }
  294. }