CommonComponentTest.php 11 KB

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