CommonComponentTest.php 4.6 KB

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