AutoLoginComponentTest.php 3.8 KB

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