CommonComponentTest.php 5.9 KB

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