Browse Source

Added tests for check() starting the session if needed

Clemens Weiß 8 years ago
parent
commit
ea001bc9ed
2 changed files with 103 additions and 4 deletions
  1. 1 1
      src/Network/Session.php
  2. 102 3
      tests/TestCase/Network/SessionTest.php

+ 1 - 1
src/Network/Session.php

@@ -371,7 +371,7 @@ class Session
      * Returns given session variable, or all of them, if no parameters given.
      *
      * @param string|null $name The name of the session variable (or a path as sent to Hash.extract)
-     * @return string|null The value of the session variable, null if session not available,
+     * @return string|array|null The value of the session variable, null if session not available,
      *   session not started, or provided name not found in the session.
      */
     public function read($name = null)

+ 102 - 3
tests/TestCase/Network/SessionTest.php

@@ -44,6 +44,25 @@ class TestDatabaseSession extends DatabaseSession
 }
 
 /**
+ * Overwrite Session to simulate a web session even if the test runs on CLI.
+ */
+class TestWebSession extends Session
+{
+
+    protected function _hasSession()
+    {
+        $isCLI = $this->_isCLI;
+        $this->_isCLI = false;
+
+        $result = parent::_hasSession();
+
+        $this->_isCLI = $isCLI;
+
+        return $result;
+    }
+}
+
+/**
  * SessionTest class
  */
 class SessionTest extends TestCase
@@ -121,7 +140,7 @@ class SessionTest extends TestCase
             ]
         ];
 
-        $session = Session::create($config);
+        Session::create($config);
         $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
         $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
         $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
@@ -136,10 +155,10 @@ class SessionTest extends TestCase
     {
         ini_set('session.cookie_path', '/foo');
 
-        $session = new Session();
+        new Session();
         $this->assertEquals('/', ini_get('session.cookie_path'));
 
-        $session = new Session(['cookiePath' => '/base']);
+        new Session(['cookiePath' => '/base']);
         $this->assertEquals('/base', ini_get('session.cookie_path'));
     }
 
@@ -569,4 +588,84 @@ class SessionTest extends TestCase
         new Session(['cookie' => 'made_up_name']);
         $this->assertEquals('made_up_name', session_name());
     }
+
+    /**
+     * Test that a call of check() starts the session when cookies are disabled in php.ini
+     */
+    public function testCheckStartsSessionWithCookiesDisabled()
+    {
+        $_COOKIE = [];
+        $_GET = [];
+
+        $session = new TestWebSession([
+            'ini' => [
+                'session.use_cookies' => 0,
+                'session.use_trans_sid' => 0,
+            ]
+        ]);
+
+        $this->assertFalse($session->started());
+        $session->check('something');
+        $this->assertTrue($session->started());
+    }
+
+    /**
+     * Test that a call of check() starts the session when a cookie is already set
+     */
+    public function testCheckStartsSessionWithCookie()
+    {
+        $_COOKIE[session_name()] = '123abc';
+        $_GET = [];
+
+        $session = new TestWebSession([
+            'ini' => [
+                'session.use_cookies' => 1,
+                'session.use_trans_sid' => 0,
+            ]
+        ]);
+
+        $this->assertFalse($session->started());
+        $session->check('something');
+        $this->assertTrue($session->started());
+    }
+
+    /**
+     * Test that a call of check() starts the session when the session ID is passed via URL and session.use_trans_sid is enabled
+     */
+    public function testCheckStartsSessionWithSIDinURL()
+    {
+        $_COOKIE = [];
+        $_GET[session_name()] = '123abc';
+
+        $session = new TestWebSession([
+            'ini' => [
+                'session.use_cookies' => 1,
+                'session.use_trans_sid' => 1,
+            ]
+        ]);
+
+        $this->assertFalse($session->started());
+        $session->check('something');
+        $this->assertTrue($session->started());
+    }
+
+    /**
+     * Test that a call of check() does not start the session when the session ID is passed via URL and session.use_trans_sid is disabled
+     */
+    public function testCheckDoesntStartSessionWithoutTransSID()
+    {
+        $_COOKIE = [];
+        $_GET[session_name()] = '123abc';
+
+        $session = new TestWebSession([
+            'ini' => [
+                'session.use_cookies' => 1,
+                'session.use_trans_sid' => 0,
+            ]
+        ]);
+
+        $this->assertFalse($session->started());
+        $session->check('something');
+        $this->assertFalse($session->started());
+    }
 }