Browse Source

Merge branch 'session_close' into 3.next

Closes #11907
Mark Story 7 years ago
parent
commit
40ff5d6767
3 changed files with 40 additions and 1 deletions
  1. 0 1
      .travis.yml
  2. 20 0
      src/Http/Session.php
  3. 20 0
      tests/TestCase/Http/SessionTest.php

+ 0 - 1
.travis.yml

@@ -41,7 +41,6 @@ matrix:
       env: PHPSTAN=1 DEFAULT=0
 
 before_install:
-  - echo cakephp version && tail -1 VERSION.txt
   - phpenv config-rm xdebug.ini
 
   - if [ $DB = 'mysql' ]; then mysql -u root -e 'CREATE DATABASE cakephp_test;'; fi

+ 20 - 0
src/Http/Session.php

@@ -369,6 +369,26 @@ class Session
     }
 
     /**
+     * Write data and close the session
+     *
+     * @return bool True if session was started
+     */
+    public function close()
+    {
+        if (!$this->_started) {
+            return true;
+        }
+
+        if (!session_write_close()) {
+            throw new RuntimeException('Could not close the session');
+        }
+
+        $this->_started = false;
+
+        return true;
+    }
+
+    /**
      * Determine if Session has already been started.
      *
      * @return bool True if session has been started.

+ 20 - 0
tests/TestCase/Http/SessionTest.php

@@ -19,6 +19,7 @@ use Cake\Http\Session;
 use Cake\Http\Session\CacheSession;
 use Cake\Http\Session\DatabaseSession;
 use Cake\TestSuite\TestCase;
+use RuntimeException;
 
 /**
  * TestCacheSession
@@ -305,6 +306,25 @@ class SessionTest extends TestCase
     }
 
     /**
+     * test close method
+     *
+     * @return void
+     */
+    public function testCloseFailure()
+    {
+        $session = new Session();
+        $session->started();
+        $this->assertTrue($session->start());
+        try {
+            $session->close();
+        } catch (RuntimeException $e) {
+            // closing the session in CLI should raise an error
+            // and won't close the session.
+            $this->assertTrue($session->started());
+        }
+    }
+
+    /**
      * testClear method
      *
      * @return void