CommonComponentTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace Tools\Test\TestCase\Controller\Component;
  3. use Cake\Controller\ComponentRegistry;
  4. use Cake\Controller\Component\CommonComponent;
  5. use Cake\Controller\Controller;
  6. use Cake\Core\Configure;
  7. use Cake\Network\Request;
  8. use Cake\Network\Session;
  9. use Cake\Routing\DispatcherFactory;
  10. use Shim\Controller\Component\Component;
  11. use Tools\TestSuite\TestCase;
  12. /**
  13. */
  14. class CommonComponentTest extends TestCase {
  15. //public $fixtures = array('core.sessions', 'plugin.tools.tools_users', 'plugin.tools.roles');
  16. public function setUp() {
  17. parent::setUp();
  18. Configure::write('App.namespace', 'TestApp');
  19. $this->Controller = new CommonComponentTestController(new Request('/test'));
  20. $this->Controller->startupProcess();
  21. }
  22. public function tearDown() {
  23. parent::tearDown();
  24. unset($this->Controller->Common);
  25. unset($this->Controller);
  26. }
  27. /**
  28. * CommonComponentTest::testLoadComponent()
  29. *
  30. * @return void
  31. */
  32. public function testLoadComponent() {
  33. $this->assertTrue(!isset($this->Controller->Apple));
  34. $this->Controller->Common->loadComponent('Apple');
  35. $this->assertTrue(isset($this->Controller->Apple));
  36. // with plugin
  37. $this->Controller->Session = null;
  38. $this->assertTrue(!isset($this->Controller->Session));
  39. $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'bar']);
  40. $this->Controller->components()->unload('Session');
  41. $this->Controller->Common->loadComponent('Shim.Session', ['foo' => 'baz']);
  42. $this->assertTrue(isset($this->Controller->Session));
  43. // with options
  44. $this->Controller->Test = null;
  45. $this->assertTrue(!isset($this->Controller->Test));
  46. $this->Controller->Common->loadComponent('Test', ['x' => 'z'], false);
  47. $this->assertTrue(isset($this->Controller->Test));
  48. $this->assertFalse($this->Controller->Test->isInit);
  49. $this->assertFalse($this->Controller->Test->isStartup);
  50. // with options
  51. $this->Controller->components()->unload('Test');
  52. $this->Controller->Test = null;
  53. $this->assertTrue(!isset($this->Controller->Test));
  54. $this->Controller->Common->loadComponent('Test', ['x' => 'y']);
  55. $this->assertTrue(isset($this->Controller->Test));
  56. $this->assertTrue($this->Controller->Test->isInit);
  57. $this->assertTrue($this->Controller->Test->isStartup);
  58. $config = $this->Controller->Test->config();
  59. $this->assertEquals(['x' => 'y'], $config);
  60. }
  61. /**
  62. * CommonComponentTest::testGetParams()
  63. *
  64. * @return void
  65. */
  66. public function testGetParams() {
  67. $is = $this->Controller->Common->getPassedParam('x');
  68. $this->assertNull($is);
  69. $is = $this->Controller->Common->getPassedParam('x', 'y');
  70. $this->assertSame('y', $is);
  71. }
  72. /**
  73. * CommonComponentTest::testGetDefaultUrlParams()
  74. *
  75. * @return void
  76. */
  77. public function testGetDefaultUrlParams() {
  78. $is = $this->Controller->Common->defaultUrlParams();
  79. $this->assertNotEmpty($is);
  80. }
  81. /**
  82. * CommonComponentTest::testcurrentUrl()
  83. *
  84. * @return void
  85. */
  86. public function testCurrentUrl() {
  87. $is = $this->Controller->Common->currentUrl();
  88. $this->assertTrue(is_array($is) && !empty($is));
  89. $is = $this->Controller->Common->currentUrl(true);
  90. $this->assertTrue(!is_array($is) && !empty($is));
  91. }
  92. /**
  93. * CommonComponentTest::testIsForeignReferer()
  94. *
  95. * @return void
  96. */
  97. public function testIsForeignReferer() {
  98. $ref = 'http://www.spiegel.de';
  99. $is = $this->Controller->Common->isForeignReferer($ref);
  100. $this->assertTrue($is);
  101. $ref = Configure::read('App.fullBaseUrl') . '/some/controller/action';
  102. $is = $this->Controller->Common->isForeignReferer($ref);
  103. $this->assertFalse($is);
  104. $ref = '';
  105. $is = $this->Controller->Common->isForeignReferer($ref);
  106. $this->assertFalse($is);
  107. }
  108. /**
  109. * CommonComponentTest::testAutoRedirect()
  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('/foo', $is['Location']);
  117. $this->assertSame(302, $this->Controller->response->statusCode());
  118. }
  119. /**
  120. * CommonComponentTest::testAutoRedirect()
  121. *
  122. * @return void
  123. */
  124. public function testAutoRedirect() {
  125. $is = $this->Controller->Common->autoRedirect(['action' => 'foo']);
  126. $is = $this->Controller->response->header();
  127. $this->assertSame('/foo', $is['Location']);
  128. $this->assertSame(200, $this->Controller->response->statusCode());
  129. }
  130. /**
  131. * CommonComponentTest::testAutoRedirect()
  132. *
  133. * @return void
  134. */
  135. public function testAutoRedirectReferer() {
  136. $is = $this->Controller->Common->autoRedirect(['action' => 'foo'], true);
  137. $is = $this->Controller->response->header();
  138. $this->assertSame('/foo', $is['Location']);
  139. $this->assertSame(200, $this->Controller->response->statusCode());
  140. }
  141. }
  142. /**
  143. * Use Controller instead of AppController to avoid conflicts
  144. */
  145. class CommonComponentTestController extends Controller {
  146. /**
  147. * @var array
  148. */
  149. public $components = ['Tools.Common'];
  150. }