CommonComponentTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Tools\Test\TestCase\Controller\Component;
  3. use App\Controller\CommonComponentTestController;
  4. use Cake\Controller\Controller;
  5. use Cake\Core\Configure;
  6. use Cake\Network\Request;
  7. use Cake\Network\Session;
  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\Network\Request
  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 Request('/my_controller/foo');
  27. $this->request->params['controller'] = 'MyController';
  28. $this->request->params['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->Common);
  38. unset($this->Controller);
  39. }
  40. /**
  41. * @return void
  42. */
  43. public function testLoadComponent() {
  44. $this->assertTrue(!isset($this->Controller->Apple));
  45. $this->Controller->Common->loadComponent('Apple');
  46. $this->assertTrue(isset($this->Controller->Apple));
  47. // with plugin
  48. $this->Controller->Session = null;
  49. $this->assertTrue(!isset($this->Controller->Session));
  50. $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'bar']);
  51. $this->Controller->components()->unload('Session');
  52. $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'baz']);
  53. $this->assertTrue(isset($this->Controller->Session));
  54. // with options
  55. $this->Controller->Test = null;
  56. $this->assertTrue(!isset($this->Controller->Test));
  57. $this->Controller->Common->loadComponent('Test', ['x' => 'z'], false);
  58. $this->assertTrue(isset($this->Controller->Test));
  59. $this->assertFalse($this->Controller->Test->isInit);
  60. $this->assertFalse($this->Controller->Test->isStartup);
  61. // with options
  62. $this->Controller->components()->unload('Test');
  63. $this->Controller->Test = null;
  64. $this->assertTrue(!isset($this->Controller->Test));
  65. $this->Controller->Common->loadComponent('Test', ['x' => 'y']);
  66. $this->assertTrue(isset($this->Controller->Test));
  67. $this->assertTrue($this->Controller->Test->isInit);
  68. $this->assertTrue($this->Controller->Test->isStartup);
  69. $config = $this->Controller->Test->config();
  70. $this->assertEquals(['x' => 'y'], $config);
  71. }
  72. /**
  73. * @return void
  74. */
  75. public function testGetParams() {
  76. $is = $this->Controller->Common->getPassedParam('x');
  77. $this->assertNull($is);
  78. $is = $this->Controller->Common->getPassedParam('x', 'y');
  79. $this->assertSame('y', $is);
  80. }
  81. /**
  82. * @return void
  83. */
  84. public function testGetDefaultUrlParams() {
  85. $is = $this->Controller->Common->defaultUrlParams();
  86. $this->assertNotEmpty($is);
  87. }
  88. /**
  89. * CommonComponentTest::testcurrentUrl()
  90. *
  91. * @return void
  92. */
  93. public function testCurrentUrl() {
  94. $is = $this->Controller->Common->currentUrl();
  95. $this->assertTrue(is_array($is) && !empty($is));
  96. $is = $this->Controller->Common->currentUrl(true);
  97. $this->assertTrue(!is_array($is) && !empty($is));
  98. }
  99. /**
  100. * @return void
  101. */
  102. public function testIsForeignReferer() {
  103. $ref = 'http://www.spiegel.de';
  104. $is = $this->Controller->Common->isForeignReferer($ref);
  105. $this->assertTrue($is);
  106. $ref = Configure::read('App.fullBaseUrl') . '/some/controller/action';
  107. $is = $this->Controller->Common->isForeignReferer($ref);
  108. $this->assertFalse($is);
  109. $ref = '';
  110. $is = $this->Controller->Common->isForeignReferer($ref);
  111. $this->assertFalse($is);
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function testPostRedirect() {
  117. $is = $this->Controller->Common->postRedirect(['action' => 'foo']);
  118. $is = $this->Controller->response->header();
  119. $this->assertSame('http://localhost/foo', $is['Location']);
  120. $this->assertSame(302, $this->Controller->response->statusCode());
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function testAutoRedirect() {
  126. $is = $this->Controller->Common->autoRedirect(['action' => 'foo']);
  127. $is = $this->Controller->response->header();
  128. $this->assertSame('http://localhost/foo', $is['Location']);
  129. $this->assertSame(302, $this->Controller->response->statusCode());
  130. }
  131. /**
  132. * @return void
  133. */
  134. public function testAutoRedirectReferer() {
  135. $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/some-referer-action');
  136. $is = $this->Controller->Common->autoRedirect(['action' => 'foo'], true);
  137. $is = $this->Controller->response->header();
  138. $this->assertSame('http://localhost/my_controller/some-referer-action', $is['Location']);
  139. $this->assertSame(302, $this->Controller->response->statusCode());
  140. }
  141. /**
  142. * @return void
  143. */
  144. public function testAutoPostRedirect() {
  145. $is = $this->Controller->Common->autoPostRedirect(['action' => 'foo'], true);
  146. $is = $this->Controller->response->header();
  147. $this->assertSame('http://localhost/foo', $is['Location']);
  148. $this->assertSame(302, $this->Controller->response->statusCode());
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function testAutoPostRedirectReferer() {
  154. $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/allowed');
  155. $is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  156. $is = $this->Controller->response->header();
  157. $this->assertSame('http://localhost/my_controller/allowed', $is['Location']);
  158. $this->assertSame(302, $this->Controller->response->statusCode());
  159. }
  160. /**
  161. * @return void
  162. */
  163. public function testAutoPostRedirectRefererNotWhitelisted() {
  164. $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/wrong');
  165. $is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  166. $is = $this->Controller->response->header();
  167. $this->assertSame('http://localhost/my_controller/foo', $is['Location']);
  168. $this->assertSame(302, $this->Controller->response->statusCode());
  169. }
  170. }