CommonComponentTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. $result = CommonComponent::defaultUrlParams();
  183. $expected = [
  184. 'plugin' => false,
  185. 'prefix' => false,
  186. ];
  187. $this->assertEquals($expected, $result);
  188. }
  189. /**
  190. * @return void
  191. */
  192. public function testForceCache() {
  193. $this->Controller->Common->forceCache();
  194. $cache_control = $this->Controller->getResponse()->getHeaderLine('Cache-Control');
  195. $this->assertEquals('public, max-age=' . HOUR, $cache_control);
  196. }
  197. /**
  198. * @return void
  199. */
  200. public function testTrimQuery() {
  201. Configure::write('DataPreparation.notrim', false);
  202. $request = $this->Controller->getRequest();
  203. $request = $request->withQueryParams([
  204. 'a' => [
  205. 'b' => [
  206. ' c ',
  207. ],
  208. ],
  209. ' d ',
  210. ' e',
  211. 'f ',
  212. ]);
  213. $this->Controller->setRequest($request);
  214. $this->Controller->Common->startup(new Event('Test'));
  215. $query = $this->Controller->getRequest()->getQuery();
  216. $expected = [
  217. 'a' => [
  218. 'b' => [
  219. 'c',
  220. ],
  221. ],
  222. 'd',
  223. 'e',
  224. 'f',
  225. ];
  226. $this->assertSame($expected, $query);
  227. }
  228. /**
  229. * @return void
  230. */
  231. public function testTrimPass() {
  232. Configure::write('DataPreparation.notrim', false);
  233. $request = $this->Controller->getRequest();
  234. $request = $request->withParam('pass', [
  235. 'a' => [
  236. 'b' => [
  237. ' c ',
  238. ],
  239. ],
  240. ' d ',
  241. ' e',
  242. 'f ',
  243. ]);
  244. $this->Controller->setRequest($request);
  245. $this->Controller->Common->startup(new Event('Test'));
  246. $pass = $this->Controller->getRequest()->getParam('pass');
  247. $expected = [
  248. 'a' => [
  249. 'b' => [
  250. 'c',
  251. ],
  252. ],
  253. 'd',
  254. 'e',
  255. 'f',
  256. ];
  257. $this->assertSame($expected, $pass);
  258. }
  259. /**
  260. * @return void
  261. */
  262. public function testTrimData() {
  263. Configure::write('DataPreparation.notrim', false);
  264. $request = $this->Controller->getRequest();
  265. $request = $request->withData('data', [
  266. 'a' => [
  267. 'b' => [
  268. ' c ',
  269. ],
  270. ],
  271. ' d ',
  272. ' e',
  273. 'f ',
  274. ]);
  275. $this->Controller->setRequest($request);
  276. $this->Controller->Common->startup(new Event('Test'));
  277. $pass = $this->Controller->getRequest()->getData('data');
  278. $expected = [
  279. 'a' => [
  280. 'b' => [
  281. 'c',
  282. ],
  283. ],
  284. 'd',
  285. 'e',
  286. 'f',
  287. ];
  288. $this->assertSame($expected, $pass);
  289. }
  290. }