Skip to content

Commit

Permalink
LDEV-5241 add SessionExists()
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Jan 8, 2025
1 parent b5b542e commit eff411b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package lucee.runtime.functions.system;

import lucee.runtime.PageContext;
import lucee.runtime.PageContextImpl;
import lucee.runtime.ext.function.Function;

public class SessionExists implements Function {
private static final long serialVersionUID = -5243745566257724777L;

public static boolean call(PageContext pc) {
return ((PageContextImpl) pc).hasCFSession();
}
}
12 changes: 12 additions & 0 deletions core/src/main/java/resource/fld/core-base.fld
Original file line number Diff line number Diff line change
Expand Up @@ -12678,6 +12678,18 @@ You can find a list of all available timezones in the Lucee administrator (Setti
<type>void</type>
</return>
</function>

<!-- SessionExists -->
<function>
<name>SessionExists</name>
<class>lucee.runtime.functions.system.SessionExists</class>
<description>
Returns a boolean indicating if a session has been already created / exists.
</description>
<return>
<type>boolean</type>
</return>
</function>

<!-- setEncoding -->
<function>
Expand Down
31 changes: 30 additions & 1 deletion test/tickets/LDEV5155.cfc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
component extends = "org.lucee.cfml.test.LuceeTestCase" skip=true {
component extends = "org.lucee.cfml.test.LuceeTestCase" {

function beforeAll(){
variables.uri = createURI("LDEV5155");
Expand All @@ -7,6 +7,7 @@ component extends = "org.lucee.cfml.test.LuceeTestCase" skip=true {
function run( testResults, testBox ){
describe( "Test case for LDEV5155", function(){
it(title = "StructKeyExists should not create session when no session exists.",
skip = true,
body = function( currentSpec ){
local.result = _InternalRequest(
template : "#uri#/ldev5155_structKeyExists.cfm",
Expand All @@ -18,6 +19,7 @@ component extends = "org.lucee.cfml.test.LuceeTestCase" skip=true {
});

it(title = "elvis operator should not create session when no session exists.",
skip = true,
body = function( currentSpec ){
local.result = _InternalRequest(
template : "#uri#/ldev5155_elvis.cfm",
Expand All @@ -29,6 +31,33 @@ component extends = "org.lucee.cfml.test.LuceeTestCase" skip=true {
});

});

describe( "Test case for SessionExists - LDEV-5241", function(){
it(title = "Test sessionExists after creating session",
body = function( currentSpec ){
local.result = _InternalRequest(
template : "#uri#/ldev5155_sessionExists.cfm",
url: {
type: "sessionExists_with_session",
createSession: true
}
);
expect( trim ( local.result.filecontent ) ).toBe( "true:true", "session was created and exists" );
});

it(title = "Test sessionExists without a session",
body = function( currentSpec ){
local.result = _InternalRequest(
template : "#uri#/ldev5155_sessionExists.cfm",
url: {
type: "sessionExists_without_session"
}
);
expect( trim ( local.result.filecontent ) ).toBe( "false:false", "session wasn't created" );
});

});

}

private string function createURI(string calledName){
Expand Down
6 changes: 6 additions & 0 deletions test/tickets/LDEV5155/ldev5155_sessionExists.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<cfscript>
param name="url.createSession" default="false";
if ( url.createSession )
session.ldev5155 = true;
echo( structKeyExists(getPageContext().getCFMLFactory().getScopeContext().getAllCFSessionScopes(), "LDEV-5155-#url.type#" ) & ":" & sessionExists() );
</cfscript>

0 comments on commit eff411b

Please sign in to comment.