CommonComponentTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Tools\Test\TestCase\Controller\Component;
  3. use Cake\Controller\ComponentRegistry;
  4. use Tools\Controller\Component\Component;
  5. use Cake\Controller\Component\CommonComponent;
  6. use Cake\Controller\Controller;
  7. use Cake\Core\Configure;
  8. use Cake\Network\Request;
  9. use Cake\Network\Session;
  10. use Cake\Routing\DispatcherFactory;
  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();
  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('Tools.Session', ['foo' => 'bar']);
  40. $this->Controller->components()->unload('Session');
  41. $this->Controller->Common->loadComponent('Tools.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', array('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', array('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. /**
  110. * Use Controller instead of AppController to avoid conflicts
  111. */
  112. class CommonComponentTestController extends Controller {
  113. public $components = array('Tools.Common');
  114. public $failed = false;
  115. public $testHeaders = array();
  116. public function fail() {
  117. $this->failed = true;
  118. }
  119. public function redirect($url, $status = null, $exit = true) {
  120. return $status;
  121. }
  122. public function header($status) {
  123. $this->testHeaders[] = $status;
  124. }
  125. }