CommonComponentTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Tools\Test\TestCase\Controller\Component;
  3. use App\Controller\CommonComponentTestController;
  4. use Cake\Core\Configure;
  5. use Cake\Http\ServerRequest;
  6. use Tools\TestSuite\TestCase;
  7. /**
  8. */
  9. class CommonComponentTest extends TestCase {
  10. /**
  11. * @var \App\Controller\CommonComponentTestController
  12. */
  13. public $Controller;
  14. /**
  15. * @var \Cake\Http\ServerRequest
  16. */
  17. public $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->params['controller'] = 'MyController';
  26. $this->request->params['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->Common);
  36. unset($this->Controller);
  37. }
  38. /**
  39. * @return void
  40. */
  41. public function testLoadComponent() {
  42. $this->assertTrue(!isset($this->Controller->Apple));
  43. $this->Controller->Common->loadComponent('Apple');
  44. $this->assertTrue(isset($this->Controller->Apple));
  45. // with plugin
  46. $this->Controller->Session = null;
  47. $this->assertTrue(!isset($this->Controller->Session));
  48. $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'bar']);
  49. $this->Controller->components()->unload('Session');
  50. $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'baz']);
  51. $this->assertTrue(isset($this->Controller->Session));
  52. // with options
  53. $this->Controller->Test = null;
  54. $this->assertTrue(!isset($this->Controller->Test));
  55. $this->Controller->Common->loadComponent('Test', ['x' => 'z'], false);
  56. $this->assertTrue(isset($this->Controller->Test));
  57. $this->assertFalse($this->Controller->Test->isInit);
  58. $this->assertFalse($this->Controller->Test->isStartup);
  59. // with options
  60. $this->Controller->components()->unload('Test');
  61. $this->Controller->Test = null;
  62. $this->assertTrue(!isset($this->Controller->Test));
  63. $this->Controller->Common->loadComponent('Test', ['x' => 'y']);
  64. $this->assertTrue(isset($this->Controller->Test));
  65. $this->assertTrue($this->Controller->Test->isInit);
  66. $this->assertTrue($this->Controller->Test->isStartup);
  67. $config = $this->Controller->Test->getConfig();
  68. $this->assertEquals(['x' => 'y'], $config);
  69. }
  70. /**
  71. * @return void
  72. */
  73. public function testGetParams() {
  74. $is = $this->Controller->Common->getPassedParam('x');
  75. $this->assertNull($is);
  76. $is = $this->Controller->Common->getPassedParam('x', 'y');
  77. $this->assertSame('y', $is);
  78. }
  79. /**
  80. * @return void
  81. */
  82. public function testGetDefaultUrlParams() {
  83. $is = $this->Controller->Common->defaultUrlParams();
  84. $this->assertNotEmpty($is);
  85. }
  86. /**
  87. * CommonComponentTest::testcurrentUrl()
  88. *
  89. * @return void
  90. */
  91. public function testCurrentUrl() {
  92. $is = $this->Controller->Common->currentUrl();
  93. $this->assertTrue(is_array($is) && !empty($is));
  94. $is = $this->Controller->Common->currentUrl(true);
  95. $this->assertTrue(!is_array($is) && !empty($is));
  96. }
  97. /**
  98. * @return void
  99. */
  100. public function testIsForeignReferer() {
  101. $ref = 'http://www.spiegel.de';
  102. $is = $this->Controller->Common->isForeignReferer($ref);
  103. $this->assertTrue($is);
  104. $ref = Configure::read('App.fullBaseUrl') . '/some/controller/action';
  105. $is = $this->Controller->Common->isForeignReferer($ref);
  106. $this->assertFalse($is);
  107. $ref = '';
  108. $is = $this->Controller->Common->isForeignReferer($ref);
  109. $this->assertFalse($is);
  110. }
  111. /**
  112. * @return void
  113. */
  114. public function testPostRedirect() {
  115. $is = $this->Controller->Common->postRedirect(['action' => 'foo']);
  116. $is = $this->Controller->response->header();
  117. $this->assertSame('http://localhost/foo', $is['Location']);
  118. $this->assertSame(302, $this->Controller->response->getStatusCode());
  119. }
  120. /**
  121. * @return void
  122. */
  123. public function testAutoRedirect() {
  124. $is = $this->Controller->Common->autoRedirect(['action' => 'foo']);
  125. $is = $this->Controller->response->header();
  126. $this->assertSame('http://localhost/foo', $is['Location']);
  127. $this->assertSame(302, $this->Controller->response->getStatusCode());
  128. }
  129. /**
  130. * @return void
  131. */
  132. public function testAutoRedirectReferer() {
  133. $url = 'http://localhost/my_controller/some-referer-action';
  134. $this->Controller->setRequest($this->request->withEnv('HTTP_REFERER', $url));
  135. $this->Controller->Common->autoRedirect(['action' => 'foo'], true);
  136. $headers = $this->Controller->response->getHeaders();
  137. $this->assertSame([$url], $headers['Location']);
  138. $this->assertSame(302, $this->Controller->response->getStatusCode());
  139. }
  140. /**
  141. * @return void
  142. */
  143. public function testAutoPostRedirect() {
  144. $this->Controller->Common->autoPostRedirect(['action' => 'foo'], true);
  145. $is = $this->Controller->response->header();
  146. $this->assertSame('http://localhost/foo', $is['Location']);
  147. $this->assertSame(302, $this->Controller->response->getStatusCode());
  148. }
  149. /**
  150. * @return void
  151. */
  152. public function testAutoPostRedirectReferer() {
  153. $url = 'http://localhost/my_controller/allowed';
  154. $this->Controller->setRequest($this->request->withEnv('HTTP_REFERER', $url));
  155. $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  156. $headers = $this->Controller->response->getHeaders();
  157. $this->assertSame([$url], $headers['Location']);
  158. $this->assertSame(302, $this->Controller->response->getStatusCode());
  159. }
  160. /**
  161. * @return void
  162. */
  163. public function testListActions() {
  164. $actions = $this->Controller->Common->listActions();
  165. $this->assertSame([], $actions);
  166. }
  167. /**
  168. * @return void
  169. */
  170. public function testAutoPostRedirectRefererNotWhitelisted() {
  171. $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/wrong');
  172. $is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  173. $is = $this->Controller->response->header();
  174. $this->assertSame('http://localhost/my_controller/foo', $is['Location']);
  175. $this->assertSame(302, $this->Controller->response->getStatusCode());
  176. }
  177. /**
  178. * @return void
  179. */
  180. public function testGetSafeRedirectUrl() {
  181. $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
  182. $this->assertSame(['action' => 'default'], $result);
  183. $this->request = $this->request->withQueryParams(['redirect' => '/foo/bar']);
  184. $this->Controller->setRequest($this->request);
  185. $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
  186. $this->assertSame('/foo/bar', $result);
  187. $this->request = $this->request->withQueryParams(['redirect' => 'https://dangerous.url/foo/bar']);
  188. $this->Controller->setRequest($this->request);
  189. $result = $this->Controller->Common->getSafeRedirectUrl(['action' => 'default']);
  190. $this->assertSame(['action' => 'default'], $result);
  191. }
  192. }