CommonComponentTest.php 8.6 KB

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