CommonComponentTest.php 9.0 KB

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