CommonComponentTest.php 6.0 KB

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