CommonComponentTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * @return void
  18. */
  19. public function setUp() {
  20. parent::setUp();
  21. Configure::write('App.namespace', 'TestApp');
  22. Configure::write('App.fullBaseUrl', 'http://localhost');
  23. $this->request = new Request('/my_controller/foo');
  24. $this->request->params['controller'] = 'MyController';
  25. $this->request->params['action'] = 'foo';
  26. $this->Controller = new CommonComponentTestController($this->request);
  27. $this->Controller->startupProcess();
  28. }
  29. /**
  30. * @return void
  31. */
  32. public function tearDown() {
  33. parent::tearDown();
  34. unset($this->Controller->Common);
  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->config();
  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. }
  110. /**
  111. * @return void
  112. */
  113. public function testPostRedirect() {
  114. $is = $this->Controller->Common->postRedirect(['action' => 'foo']);
  115. $is = $this->Controller->response->header();
  116. $this->assertSame('http://localhost/foo', $is['Location']);
  117. $this->assertSame(302, $this->Controller->response->statusCode());
  118. }
  119. /**
  120. * @return void
  121. */
  122. public function testAutoRedirect() {
  123. $is = $this->Controller->Common->autoRedirect(['action' => 'foo']);
  124. $is = $this->Controller->response->header();
  125. $this->assertSame('http://localhost/foo', $is['Location']);
  126. $this->assertSame(302, $this->Controller->response->statusCode());
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testAutoRedirectReferer() {
  132. $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/some-referer-action');
  133. $is = $this->Controller->Common->autoRedirect(['action' => 'foo'], true);
  134. $is = $this->Controller->response->header();
  135. $this->assertSame('http://localhost/my_controller/some-referer-action', $is['Location']);
  136. $this->assertSame(302, $this->Controller->response->statusCode());
  137. }
  138. /**
  139. * @return void
  140. */
  141. public function testAutoPostRedirect() {
  142. $is = $this->Controller->Common->autoPostRedirect(['action' => 'foo'], true);
  143. $is = $this->Controller->response->header();
  144. $this->assertSame('http://localhost/foo', $is['Location']);
  145. $this->assertSame(302, $this->Controller->response->statusCode());
  146. }
  147. /**
  148. * @return void
  149. */
  150. public function testAutoPostRedirectReferer() {
  151. $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/allowed');
  152. $is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  153. $is = $this->Controller->response->header();
  154. $this->assertSame('http://localhost/my_controller/allowed', $is['Location']);
  155. $this->assertSame(302, $this->Controller->response->statusCode());
  156. }
  157. /**
  158. * @return void
  159. */
  160. public function testAutoPostRedirectRefererNotWhitelisted() {
  161. $this->request->env('HTTP_REFERER', 'http://localhost/my_controller/wrong');
  162. $is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
  163. $is = $this->Controller->response->header();
  164. $this->assertSame('http://localhost/my_controller/foo', $is['Location']);
  165. $this->assertSame(302, $this->Controller->response->statusCode());
  166. }
  167. }