AutoLoginComponentTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. App::uses('AutoLoginComponent', 'Tools.Controller/Component');
  3. App::uses('Controller', 'Controller');
  4. /**
  5. * Short description for class.
  6. *
  7. * @package cake.tests
  8. * @subpackage cake.tests.cases.libs.controller.components
  9. */
  10. class AutoLoginComponentTest extends CakeTestCase {
  11. public $fixtures = array('core.cake_session', 'core.user');
  12. /**
  13. * setUp method
  14. *
  15. * @access public
  16. * @return void
  17. */
  18. public function setUp() {
  19. parent::setUp();
  20. Configure::write('AutoLogin.active', 1);
  21. Configure::write('AutoLogin.cookieName', 'autoLogin');
  22. $this->Controller = new AutoLoginTestController(new CakeRequest, new CakeResponse);
  23. $this->Controller->AutoLogin = new AutoLoginComponent(new ComponentCollection());
  24. }
  25. /**
  26. * Tear-down method. Resets environment state.
  27. *
  28. * @access public
  29. * @return void
  30. */
  31. public function tearDown() {
  32. parent::tearDown();
  33. unset($this->Controller->AutoLogin);
  34. unset($this->Controller);
  35. }
  36. /**
  37. * test if suhosin isn't messing up srand() and mt_srand()
  38. * run this on every the environment you want AutoLogin to work!
  39. * It this test fails add `suhosin.srand.ignore = Off`
  40. * in your `/etc/php5/apache2/php.ini`
  41. * And don't forget to restart apache or at least `/etc/init.d/apache2 force-reload`
  42. */
  43. public function testIfRandWillWork() {
  44. srand('1234567890');
  45. $rand1 = rand(0, 255);
  46. srand('1234567890');
  47. $rand2 = rand(0, 255);
  48. $this->assertSame($rand1, $rand2, 'You have the Suhosin BUG! Add `suhosin.srand.ignore = Off` to your php.ini!');
  49. }
  50. /**
  51. * test merge of configs
  52. */
  53. public function testConfigs() {
  54. $this->Controller->AutoLogin->initialize($this->Controller);
  55. $settings = $this->Controller->AutoLogin->settings;
  56. $this->assertTextStartsWith('autoLogin', $settings['cookieName']);
  57. }
  58. /**
  59. * test cookie name
  60. */
  61. public function testConfigsWithCustomCookieName() {
  62. Configure::write('AutoLogin.cookieName', 'myAutoLogin');
  63. $this->Controller->AutoLogin = new AutoLoginComponent(new ComponentCollection());
  64. $this->Controller->AutoLogin->initialize($this->Controller);
  65. $settings = $this->Controller->AutoLogin->settings;
  66. $this->assertTextStartsWith('myAutoLogin', $settings['cookieName']);
  67. Configure::write('AutoLogin.cookieName', 'myOtherAutoLogin');
  68. $this->Controller->AutoLogin = new AutoLoginComponent(new ComponentCollection());
  69. $this->Controller->AutoLogin->initialize($this->Controller);
  70. $settings = $this->Controller->AutoLogin->settings;
  71. $this->assertTextStartsWith('myOtherAutoLogin', $settings['cookieName']);
  72. }
  73. public function testLogin() {
  74. $this->Controller->AutoLogin = new AutoLoginComponent(new ComponentCollection());
  75. $this->Controller->AutoLogin->initialize($this->Controller);
  76. $settings = $this->Controller->AutoLogin->settings;
  77. //die(returns($settings));
  78. //TODO
  79. }
  80. }
  81. /**
  82. * Short description for class.
  83. *
  84. * @package cake.tests
  85. * @subpackage cake.tests.cases.libs.controller.components
  86. */
  87. class AutoLoginTestController extends Controller {
  88. /**
  89. * name property
  90. *
  91. * @var string 'SecurityTest'
  92. * @access public
  93. */
  94. /**
  95. * components property
  96. *
  97. * @var array
  98. * @access public
  99. */
  100. public $components = array('Tools.AutoLogin');
  101. /**
  102. * failed property
  103. *
  104. * @var bool false
  105. * @access public
  106. */
  107. public $failed = false;
  108. /**
  109. * Used for keeping track of headers in test
  110. *
  111. * @var array
  112. * @access public
  113. */
  114. public $testHeaders = array();
  115. /**
  116. * fail method
  117. *
  118. * @access public
  119. * @return void
  120. */
  121. public function fail() {
  122. $this->failed = true;
  123. }
  124. /**
  125. * redirect method
  126. *
  127. * @param mixed $option
  128. * @param mixed $code
  129. * @param mixed $exit
  130. * @access public
  131. * @return void
  132. */
  133. public function redirect($url, $status = null, $exit = true) {
  134. return $status;
  135. }
  136. /**
  137. * Conveinence method for header()
  138. *
  139. * @param string $status
  140. * @return void
  141. * @access public
  142. */
  143. public function header($status) {
  144. $this->testHeaders[] = $status;
  145. }
  146. }