AutoLoginComponentTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. App::uses('AutoLoginComponent', 'Tools.Controller/Component');
  3. App::uses('Controller', 'Controller');
  4. /**
  5. * Short description for class.
  6. *
  7. */
  8. class AutoLoginComponentTest extends CakeTestCase {
  9. public $fixtures = array('core.cake_session', 'core.user');
  10. /**
  11. * setUp method
  12. *
  13. * @access public
  14. * @return void
  15. */
  16. public function setUp() {
  17. parent::setUp();
  18. Configure::write('AutoLogin.active', 1);
  19. Configure::write('AutoLogin.cookieName', 'autoLogin');
  20. $this->Controller = new AutoLoginTestController(new CakeRequest, new CakeResponse);
  21. $this->Controller->AutoLogin = new AutoLoginComponent(new ComponentCollection());
  22. }
  23. /**
  24. * Tear-down method. Resets environment state.
  25. *
  26. * @access public
  27. * @return void
  28. */
  29. public function tearDown() {
  30. parent::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. */
  83. class AutoLoginTestController extends Controller {
  84. /**
  85. * name property
  86. *
  87. * @var string 'SecurityTest'
  88. * @access public
  89. */
  90. /**
  91. * components property
  92. *
  93. * @var array
  94. * @access public
  95. */
  96. public $components = array('Tools.AutoLogin');
  97. /**
  98. * failed property
  99. *
  100. * @var bool false
  101. * @access public
  102. */
  103. public $failed = false;
  104. /**
  105. * Used for keeping track of headers in test
  106. *
  107. * @var array
  108. * @access public
  109. */
  110. public $testHeaders = array();
  111. /**
  112. * fail method
  113. *
  114. * @access public
  115. * @return void
  116. */
  117. public function fail() {
  118. $this->failed = true;
  119. }
  120. /**
  121. * redirect method
  122. *
  123. * @param mixed $option
  124. * @param mixed $code
  125. * @param mixed $exit
  126. * @access public
  127. * @return void
  128. */
  129. public function redirect($url, $status = null, $exit = true) {
  130. return $status;
  131. }
  132. /**
  133. * Conveinence method for header()
  134. *
  135. * @param string $status
  136. * @return void
  137. * @access public
  138. */
  139. public function header($status) {
  140. $this->testHeaders[] = $status;
  141. }
  142. }