CommonComponentTest.php 8.5 KB

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