CommonComponentTest.php 9.8 KB

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