01.09 php saved
robby
Tags add more
auth, requiresSecurity, testAction and unit test  
Note
This was my solution for the problems I was having with testAction() redirecting because of application settings in the Auth and Security components, and I wanted a solution that was as flexible as possible.
  1. In test.php file, before including boostrap.php:
  2. ----------------------------------------------------------
  3. define('CAKE_UNIT_TEST', true);
  4.  
  5.  
  6. In test.php, before creating the dispatcher. (Optional, done mostly
  7. as a convenience so I remember what the settings are later):
  8. ----------------------------------------------------------
  9. Configure::write('UnitTest.bypassSSL',    true); // bypass SSL settings
  10. Configure::write('UnitTest.bypassAuth',  true); // bypass Auth settings (auto allow())
  11. Configure::write('UnitTest.spoofAuthUser'false); // data array to set as the Auth User
  12.  
  13.  
  14. In bootstrap.php, anywhere:
  15. ----------------------------------------------------------
  16. if ( !defined('CAKE_UNIT_TEST') ) {
  17.     define('CAKE_UNIT_TEST', false);
  18. }
  19.  
  20.  
  21. In app_controller.php:
  22. ----------------------------------------------------------
  23. if ( !CAKE_UNIT_TEST or is_empty(Configure::read('UnitTest.bypassSSL')) ) {
  24.     $this->Security->requireSecure();
  25.     $this->Security->blackHoleCallback = '_blackHole';
  26. }
  27.  
  28.  
  29.  
  30. In app_controller, after defining Auth settings.
  31. Important! must define Auth->sessionKey or this will fail
  32. ----------------------------------------------------------
  33. if ( CAKE_UNIT_TEST ) {
  34.     // Allow the user
  35.     if ( !is_empty(Configure::read('UnitTest.bypassAuth')) ) {
  36.         $this->Auth->allow();
  37.     }
  38.    
  39.     // Spoof the user
  40.     if ( !is_empty(Configure::read('UnitTest.spoofAuthUser')) ) {
  41.         $this->Auth->Session->write($this->Auth->sessionKey, Configure::read('UnitTest.spoofAuthUser'));
  42.     }
  43. }
  44.  
Parsed in 0.073 seconds, using GeSHi 1.0.7.14

Modify this Paste