AutoLoginComponentTest.php 3.5 KB

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