How do I destroy a CakePHP session from inside of a test? - session

I found a test that fails when run from a browser if you are logged in. I need to make sure the session is cleared out before running this test. Is there an easy way to clear the CakePHP session from within a test?
I tried calling Session::Destroy(); but the test cannot seem to get visibility of Session.
Maybe I am trying to import Session incorrectly?
I am using: App::import('Core', 'Session');

You should mock the Session component. Mark Story himself wrote an article showing how to test controllers.

I believe gustavotkg is correct. We should be using mock objects. Unfortunately, it will not be completely straight forward to do this the way the code was set up. I will have to go back and rework some things. For a temporary fix this is what I did:
$appController = ClassRegistry::init('AppController');
$appController->constructClasses();
$appController->Session->destroy();

if(App::import('Core','Session')) {
$session = new CakeSession();
$session->start();
}
hope this can help you..

Related

How to add a flashdata after session_write_close()

I upgraded my application from CI2 to CI3 (CI v3.1.9 and PHP7). Now I have performance issue with the new concurrency system in the session (see doc).
Some of the actions in the application are very long (because of calling an external APIs that can takes several minutes to respond for example) and I don't want those actions to lock the session. As recommended, I would use session_write_close() function in the controller before doing the very long action.
The problem is that I want to display a message to user after redirecting at the end of this action. Right now, I am using session->set_flashdata() before the redirection, but because I closed the session earlier, it is not working.
Does anyone have recommendations on how to achieve that?
If I am starting the session again with session_start() it is working, but I have no idea if this is best practice to use PHP session like that with Codeigniter.
There is no problem with starting the session again using session_start(). The CodeIgniter "Session" class is still loaded and the instance is still valid. So all the "special" stuff that CI does to make sessions work is good to go.
I tested and then used this scheme in a project some time back and didn't experience any problems. Haven't had any blow-back from the client of a still operating site either. YMMV.
BTW, in the __construct() function of the CI_Session class a call to session_start() is made in order to start up PHP's session extension. So making that call is clearly not a "bad" practice. :)

How to generate log in service now when a browser is closed?

I need to generate logs in service now whenever the browser is closed or the session is expired. I tried using global business rule but couldnt achieve it. It will be greatfull if i can if i get an idea to achieve this.
Thanks in advance.
I assume you already know how to generate a log and such in ServiceNow, and the problem you're having, is how to run code when the user closed their browser.
The best solution would probably be to create an onload client script which sets onbeforeunload to a function that triggers a log.
More on that here: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload
For the second part of your question, user sessions are tracked in a "user sessions" table. If you want to also log when theirs expires, you can do so with a business rule on that table.
You can use
GlideAjax to call from your client script to a server side script include.
Look at GSLog
Try this one
var gl = new GSLog("com.snc.sla.tasksla.log", "TaskSLA");

Joomla - Unset multiple session variables not working

I thought this would be simple but I guess there's a catch somewhere...
I'm developing a custom part of code for a Joomla installation and I need to unset some session variables before executing my code. So, naturally, I have
$session->clear('var1');
$session->clear('var2');
$session->clear('var3');
$session->clear('var4');
but the page appears totally blank and nothing happens. Any suggestions?
Assuming that you got the $session variable like this:
$session = JFactory::getSession();
If you are getting a blank page, you probably some error in your code. Do you have access to some kind of error log? If not, you can try to force displaying errors from your code if it's not a production environment (although it's not the best way to do it) or enable debug mode from the joomla administrator.
You can also try to run the php file in your browser, and if everything is ok and there are no parse errors in the file, you should see a message like 'Restricted access' or similar.
Besides, if the script is not crashing, you can check what value is returning each call to $session->clear( 'xxx' ) (It should return the value you just cleared.
The last thing that comes to my mind is that the vars you have stored in session are in a different "context". When you get/set data to session, you can pass a "namespace" as an additional parameter, so these vars are stored in that "namespace" (in fact, it's stored inside another index inside the session. So if possible, you should check if these variables are stored in session using a different "namespace":
$session->set( 'var1', $value, 'another_namespace' );
If so, you should clear it like this:
$session->clear( 'var1', 'another_namespace' );
P.S.: I said "namespace" because it's the parameter name that Joomla uses in these session methods, but don't get confused with PHP namespaces.
I hope it helped!

How to trace CakePHP's inner workflow

Short description
I'm getting used to CakePHP right now and am wondering about how to get more debug-information about what is happening inside the framework.
Let me please explain my situation a little more detailed
As you know CakePHP does a lot for you without putting you into the need to write additional code. One example is the handling of models.
I just created a model User and added validation-rules (no other methods). As described in the API methods like save will just work.
After that I created the needed controller and view to add a new user. When I try to add a user from the view I just receive the flash-message The user could not be created. Please, try again. No validation-violations are flashed.
I also did set the debug-level to 2: Configure::write('debug', 2); but do not receive any errors. The error.log inside \tmp\logs is also empty.
I really do want to learn how to solve those issues in the future.
So what else can I do to debug / display inner processes of cake?
Thank you very much for your help!
DebugKit is an official plugin that gives you lots of information on the request, queries and variables produced by Cake:
https://github.com/cakephp/debug_kit
You can also use trace() and other methods in the Debugger to show what is being executed in the background:
http://book.cakephp.org/2.0/en/development/debugging.html
Use a PHP IDE with an integrated debugger. That will allow you to follow execution line by line as it is executed and even inspect variable values as you go. Netbeans is a free one.

How do I access a asp.net session variable from APP_CODE?

I have seen lots of posts here and elsewhere stating that one can access session variables from app_code. I want to access an already created session.
this code errors out because of a null exception.
string myFile = HttpContext.Current.Session["UploadedFile"];
this creates a null session variable.
System.Web.SessionState.HttpSessionState Session = HttpContext.Current.Session;
It looks like I can create a new session variable but not access an already created one. Anyone have any idea what might be giving me problems?
I was having ths same problem, but I was able to fix it because I controlled where in the event lifecycle I was attempting to leverage HttpContect.Current.Session... it makes a difference because Session isn't available, for example, on construction.
Maybe that will help you.

Resources