CommonComponentTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. class CommonComponentTest extends TestCase {
  11. /**
  12. * @var \App\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 testGetDefaultUrlParams() {
  54. $is = $this->Controller->Common->defaultUrlParams();
  55. $this->assertNotEmpty($is);
  56. }
  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(['controller' => 'MyController', 'action' => 'foo']);
  87. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  88. $this->assertSame('http://localhost/my-controller/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(['controller' => 'MyController', 'action' => 'foo']);
  96. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  97. $this->assertSame('http://localhost/my-controller/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(['controller' => 'MyController', '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(['controller' => 'MyController', 'action' => 'foo'], true);
  116. $is = $this->Controller->getResponse()->getHeaderLine('Location');
  117. $this->assertSame('http://localhost/my-controller/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. $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 testAddHelpers() {
  178. $this->Controller->Common->addHelpers(['Tester']);
  179. $helpers = $this->Controller->viewBuilder()->getHelpers();
  180. $this->assertEquals(['Tester'], $helpers);
  181. $this->Controller->Common->addHelpers(['Tester123']);
  182. $helpers = $this->Controller->viewBuilder()->getHelpers();
  183. $this->assertEquals(['Tester', 'Tester123'], $helpers);
  184. }
  185. /**
  186. * @return void
  187. */
  188. public function testDefaultUrlParams() {
  189. Configure::write('Routing.prefixes', ['admin', 'tests']);
  190. $result = CommonComponent::defaultUrlParams();
  191. $expected = [
  192. 'plugin' => false,
  193. 'admin' => false,
  194. 'tests' => false,
  195. ];
  196. $this->assertEquals($expected, $result);
  197. Configure::write('Routing.prefixes', 'admin');
  198. $result = CommonComponent::defaultUrlParams();
  199. $expected = [
  200. 'plugin' => false,
  201. 'admin' => false,
  202. ];
  203. $this->assertEquals($expected, $result);
  204. }
  205. /**
  206. * @return void
  207. */
  208. public function testForceCache() {
  209. $this->Controller->Common->forceCache();
  210. $cache_control = $this->Controller->getResponse()->getHeaderLine('Cache-Control');
  211. $this->assertEquals('public, max-age=' . HOUR, $cache_control);
  212. }
  213. /**
  214. * @return void
  215. */
  216. public function testTrimQuery() {
  217. Configure::write('DataPreparation.notrim', false);
  218. $request = $this->Controller->getRequest();
  219. $request = $request->withQueryParams([
  220. 'a' => [
  221. 'b' => [
  222. ' c ',
  223. ],
  224. ],
  225. ' d ',
  226. ' e',
  227. 'f ',
  228. ]);
  229. $this->Controller->setRequest($request);
  230. $this->Controller->Common->startup(new Event('Test'));
  231. $query = $this->Controller->getRequest()->getQuery();
  232. $expected = [
  233. 'a' => [
  234. 'b' => [
  235. 'c',
  236. ],
  237. ],
  238. 'd',
  239. 'e',
  240. 'f',
  241. ];
  242. $this->assertSame($expected, $query);
  243. }
  244. /**
  245. * @return void
  246. */
  247. public function testTrimPass() {
  248. Configure::write('DataPreparation.notrim', false);
  249. $request = $this->Controller->getRequest();
  250. $request = $request->withParam('pass', [
  251. 'a' => [
  252. 'b' => [
  253. ' c ',
  254. ],
  255. ],
  256. ' d ',
  257. ' e',
  258. 'f ',
  259. ]);
  260. $this->Controller->setRequest($request);
  261. $this->Controller->Common->startup(new Event('Test'));
  262. $pass = $this->Controller->getRequest()->getParam('pass');
  263. $expected = [
  264. 'a' => [
  265. 'b' => [
  266. 'c',
  267. ],
  268. ],
  269. 'd',
  270. 'e',
  271. 'f',
  272. ];
  273. $this->assertSame($expected, $pass);
  274. }
  275. /**
  276. * @return void
  277. */
  278. public function testTrimData() {
  279. Configure::write('DataPreparation.notrim', false);
  280. $request = $this->Controller->getRequest();
  281. $request = $request->withData('data', [
  282. 'a' => [
  283. 'b' => [
  284. ' c ',
  285. ],
  286. ],
  287. ' d ',
  288. ' e',
  289. 'f ',
  290. ]);
  291. $this->Controller->setRequest($request);
  292. $this->Controller->Common->startup(new Event('Test'));
  293. $pass = $this->Controller->getRequest()->getData('data');
  294. $expected = [
  295. 'a' => [
  296. 'b' => [
  297. 'c',
  298. ],
  299. ],
  300. 'd',
  301. 'e',
  302. 'f',
  303. ];
  304. $this->assertSame($expected, $pass);
  305. }
  306. }