CommonComponentTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Tools\Test\TestCase\Controller\Component;
  3. use Cake\Core\Configure;
  4. use Cake\Event\Event;
  5. use Cake\Http\ServerRequest;
  6. use Shim\TestSuite\TestCase;
  7. use TestApp\Controller\CommonComponentTestController;
  8. use Tools\Controller\Component\CommonComponent;
  9. class CommonComponentTest extends TestCase {
  10. /**
  11. * @var \TestApp\Controller\CommonComponentTestController
  12. */
  13. protected $Controller;
  14. /**
  15. * @var \Cake\Http\ServerRequest
  16. */
  17. protected $request;
  18. /**
  19. * @return void
  20. */
  21. public function setUp(): void {
  22. parent::setUp();
  23. Configure::write('App.fullBaseUrl', 'http://localhost');
  24. $this->loadRoutes();
  25. $this->request = new ServerRequest(['url' => '/my-controller/foo']);
  26. $this->request = $this->request->withParam('controller', 'MyController')
  27. ->withParam('action', 'foo');
  28. $this->Controller = new CommonComponentTestController($this->request);
  29. $this->Controller->startupProcess();
  30. }
  31. /**
  32. * @return void
  33. */
  34. public function tearDown(): void {
  35. parent::tearDown();
  36. unset($this->Controller);
  37. }
  38. /**
  39. * @return void
  40. */
  41. public function testGetParams() {
  42. $is = $this->Controller->Common->getPassedParam('x');
  43. $this->assertNull($is);
  44. $is = $this->Controller->Common->getPassedParam('x', 'y');
  45. $this->assertSame('y', $is);
  46. }
  47. /**
  48. * @return void
  49. */
  50. public function testCurrentUrl() {
  51. $is = $this->Controller->Common->currentUrl();
  52. $this->assertTrue(is_array($is) && !empty($is));
  53. $is = $this->Controller->Common->currentUrl(true);
  54. $this->assertTrue(!is_array($is) && !empty($is));
  55. }
  56. /**
  57. * @return void
  58. */
  59. public function testIsForeignReferer() {
  60. $ref = 'http://www.spiegel.de';
  61. $is = $this->Controller->Common->isForeignReferer($ref);
  62. $this->assertTrue($is);
  63. $ref = Configure::read('App.fullBaseUrl') . '/some/controller/action';
  64. $is = $this->Controller->Common->isForeignReferer($ref);
  65. $this->assertFalse($is);
  66. $ref = '';
  67. $is = $this->Controller->Common->isForeignReferer($ref);
  68. $this->assertFalse($is);
  69. $is = $this->Controller->Common->isForeignReferer();
  70. $this->assertFalse($is);
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function testPostRedirect() {
  76. $this->Controller->Common->postRedirect(['controller' => 'MyController', 'action' => 'foo']);
  77. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  78. $this->assertSame('http://localhost/my-controller/foo', $is);
  79. $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
  80. }
  81. /**
  82. * @return void
  83. */
  84. public function testAutoRedirect() {
  85. $this->Controller->Common->autoRedirect(['controller' => 'MyController', 'action' => 'foo']);
  86. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  87. $this->assertSame('http://localhost/my-controller/foo', $is);
  88. $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function testAutoRedirectReferer() {
  94. $url = 'http://localhost/my-controller/some-referer-action';
  95. $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', $url));
  96. $this->Controller->Common->autoRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  97. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  98. $this->assertSame($url, $is);
  99. $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
  100. }
  101. /**
  102. * @return void
  103. */
  104. public function testAutoPostRedirect() {
  105. $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  106. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  107. $this->assertSame('http://localhost/my-controller/foo', $is);
  108. $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
  109. }
  110. /**
  111. * @return void
  112. */
  113. public function testAutoPostRedirectReferer() {
  114. $url = 'http://localhost/my-controller/allowed';
  115. $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', $url));
  116. $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  117. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  118. $this->assertSame($url, $is);
  119. $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
  120. }
  121. /**
  122. * @return void
  123. */
  124. public function testListActions() {
  125. $actions = $this->Controller->Common->listActions();
  126. $this->assertSame([], $actions);
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testAutoPostRedirectRefererNotWhitelisted() {
  132. $this->Controller->setRequest($this->Controller->getRequest()->withEnv('HTTP_REFERER', 'http://localhost/my-controller/wrong'));
  133. $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  134. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  135. $this->assertSame('http://localhost/my-controller/foo', $is);
  136. $this->assertSame(302, $this->Controller->getResponse()->getStatusCode());
  137. }
  138. /**
  139. * @return void
  140. */
  141. public function testGetSafeRedirectUrl() {
  142. $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
  143. $this->assertSame(['action' => 'default'], $result);
  144. $this->request = $this->request->withQueryParams(['redirect' => '/foo/bar']);
  145. $this->Controller->setRequest($this->request);
  146. $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
  147. $this->assertSame('/foo/bar', $result);
  148. $this->request = $this->request->withQueryParams(['redirect' => 'https://dangerous.url/foo/bar']);
  149. $this->Controller->setRequest($this->request);
  150. $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
  151. $this->assertSame(['action' => 'default'], $result);
  152. }
  153. /**
  154. * @return void
  155. */
  156. public function testIsPosted() {
  157. $this->Controller->setRequest($this->Controller->getRequest()->withMethod('POST'));
  158. $this->assertTrue($this->Controller->Common->isPosted());
  159. $this->Controller->setRequest($this->Controller->getRequest()->withMethod('PUT'));
  160. $this->assertTrue($this->Controller->Common->isPosted());
  161. $this->Controller->setRequest($this->Controller->getRequest()->withMethod('PATCH'));
  162. $this->assertTrue($this->Controller->Common->isPosted());
  163. }
  164. /**
  165. * @return void
  166. */
  167. public function testDefaultUrlParams() {
  168. $result = CommonComponent::defaultUrlParams();
  169. $expected = [
  170. 'plugin' => false,
  171. 'prefix' => false,
  172. ];
  173. $this->assertEquals($expected, $result);
  174. }
  175. /**
  176. * @return void
  177. */
  178. public function testForceCache() {
  179. $this->Controller->Common->forceCache();
  180. $cache_control = $this->Controller->getResponse()->getHeaderLine('Cache-Control');
  181. $this->assertEquals('public, max-age=' . HOUR, $cache_control);
  182. }
  183. /**
  184. * @return void
  185. */
  186. public function testTrimQuery() {
  187. Configure::write('DataPreparation.notrim', false);
  188. $request = $this->Controller->getRequest();
  189. $request = $request->withQueryParams([
  190. 'a' => [
  191. 'b' => [
  192. ' c ',
  193. ],
  194. ],
  195. ' d ',
  196. ' e',
  197. 'f ',
  198. ]);
  199. $this->Controller->setRequest($request);
  200. $this->Controller->Common->startup(new Event('Test'));
  201. $query = $this->Controller->getRequest()->getQuery();
  202. $expected = [
  203. 'a' => [
  204. 'b' => [
  205. 'c',
  206. ],
  207. ],
  208. 'd',
  209. 'e',
  210. 'f',
  211. ];
  212. $this->assertSame($expected, $query);
  213. }
  214. /**
  215. * @return void
  216. */
  217. public function testTrimPass() {
  218. Configure::write('DataPreparation.notrim', false);
  219. $request = $this->Controller->getRequest();
  220. $request = $request->withParam('pass', [
  221. 'a' => [
  222. 'b' => [
  223. ' c ',
  224. ],
  225. ],
  226. ' d ',
  227. ' e',
  228. 'f ',
  229. ]);
  230. $this->Controller->setRequest($request);
  231. $this->Controller->Common->startup(new Event('Test'));
  232. $pass = $this->Controller->getRequest()->getParam('pass');
  233. $expected = [
  234. 'a' => [
  235. 'b' => [
  236. 'c',
  237. ],
  238. ],
  239. 'd',
  240. 'e',
  241. 'f',
  242. ];
  243. $this->assertSame($expected, $pass);
  244. }
  245. /**
  246. * @return void
  247. */
  248. public function testTrimData() {
  249. Configure::write('DataPreparation.notrim', false);
  250. $request = $this->Controller->getRequest();
  251. $request = $request->withData('data', [
  252. 'a' => [
  253. 'b' => [
  254. ' c ',
  255. ],
  256. ],
  257. ' d ',
  258. ' e',
  259. 'f ',
  260. ]);
  261. $this->Controller->setRequest($request);
  262. $this->Controller->Common->startup(new Event('Test'));
  263. $pass = $this->Controller->getRequest()->getData('data');
  264. $expected = [
  265. 'a' => [
  266. 'b' => [
  267. 'c',
  268. ],
  269. ],
  270. 'd',
  271. 'e',
  272. 'f',
  273. ];
  274. $this->assertSame($expected, $pass);
  275. }
  276. }