CommonComponentTest.php 8.7 KB

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