Browse Source

Merge pull request #10997 from cakephp/session-tests-php72

Attempt to get the session tests passing in PHP7.2
Mark Story 8 years ago
parent
commit
086dc611ef

+ 5 - 3
src/Network/Session.php

@@ -108,7 +108,9 @@ class Session
             $sessionConfig['ini']['session.name'] = $sessionConfig['cookie'];
         }
 
-        if (!empty($sessionConfig['handler'])) {
+        // In PHP7.1.0+ session.save_handler can't be set to user by the user.
+        // https://github.com/php/php-src/blob/master/ext/session/session.c#L559
+        if (!empty($sessionConfig['handler']) && version_compare(PHP_VERSION, '7.1.0', '<=')) {
             $sessionConfig['ini']['session.save_handler'] = 'user';
         }
 
@@ -283,7 +285,7 @@ class Session
      */
     public function options(array $options)
     {
-        if (session_status() === \PHP_SESSION_ACTIVE) {
+        if (session_status() === \PHP_SESSION_ACTIVE || headers_sent()) {
             return;
         }
 
@@ -453,7 +455,7 @@ class Session
      */
     public function id($id = null)
     {
-        if ($id !== null) {
+        if ($id !== null && !headers_sent()) {
             session_id($id);
         }
 

+ 15 - 0
src/Network/Session/DatabaseSession.php

@@ -63,6 +63,21 @@ class DatabaseSession implements SessionHandlerInterface
     }
 
     /**
+     * Set the timeout value for sessions.
+     *
+     * Primarily used in testing.
+     *
+     * @param int $timeout The timeout duration.
+     * @return $this
+     */
+    public function setTimeout($timeout)
+    {
+        $this->_timeout = $timeout;
+
+        return $this;
+    }
+
+    /**
      * Method called on open of a database session.
      *
      * @param string $savePath The path where to store/retrieve the session.

+ 2 - 0
tests/TestCase/Http/RequestTransformerTest.php

@@ -266,6 +266,8 @@ class RequestTransformerTest extends TestCase
      * Test that the transformed request sets the session path
      * as expected.
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testToCakeBaseSessionPath()

+ 2 - 0
tests/TestCase/Http/ServerRequestFactoryTest.php

@@ -122,6 +122,8 @@ class ServerRequestFactoryTest extends TestCase
     /**
      * Test fromGlobals includes the session
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testFromGlobalsUrlSession()

+ 1 - 1
tests/TestCase/Network/Session/DatabaseSessionTest.php

@@ -153,8 +153,8 @@ class DatabaseSessionTest extends TestCase
     {
         TableRegistry::clear();
 
-        ini_set('session.gc_maxlifetime', '0');
         $storage = new DatabaseSession();
+        $storage->setTimeout(0);
         $storage->write('foo', 'Some value');
 
         sleep(1);

+ 25 - 35
tests/TestCase/Network/SessionTest.php

@@ -78,39 +78,6 @@ class SessionTest extends TestCase
     public $fixtures = ['core.cake_sessions', 'core.sessions'];
 
     /**
-     * setup before class.
-     *
-     * @return void
-     */
-    public static function setupBeforeClass()
-    {
-        // Make sure garbage collector will be called
-        static::$_gcDivisor = ini_get('session.gc_divisor');
-        ini_set('session.gc_divisor', '1');
-    }
-
-    /**
-     * teardown after class
-     *
-     * @return void
-     */
-    public static function teardownAfterClass()
-    {
-        // Revert to the default setting
-        ini_set('session.gc_divisor', static::$_gcDivisor);
-    }
-
-    /**
-     * setUp method
-     *
-     * @return void
-     */
-    public function setUp()
-    {
-        parent::setUp();
-    }
-
-    /**
      * tearDown method
      *
      * @return void
@@ -124,6 +91,8 @@ class SessionTest extends TestCase
     /**
      * test setting ini properties with Session configuration.
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testSessionConfigIniSetting()
@@ -149,6 +118,8 @@ class SessionTest extends TestCase
     /**
      * test session cookie path setting
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testCookiePath()
@@ -298,15 +269,17 @@ class SessionTest extends TestCase
     /**
      * testId method
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testId()
     {
         $session = new Session();
+        $session->start();
         $result = $session->id();
-        $expected = session_id();
         $this->assertNotEmpty($result);
-        $this->assertSame($expected, $result);
+        $this->assertSame(session_id(), $result);
 
         $session->id('MySessionId');
         $this->assertSame('MySessionId', $session->id());
@@ -489,6 +462,8 @@ class SessionTest extends TestCase
     /**
      * test using a handler from app/Model/Datasource/Session.
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testUsingAppLibsHandler()
@@ -512,6 +487,8 @@ class SessionTest extends TestCase
     /**
      * test using a handler from a plugin.
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testUsingPluginHandler()
@@ -534,6 +511,8 @@ class SessionTest extends TestCase
     /**
      * Tests that it is possible to pass an already made instance as the session engine
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testEngineWithPreMadeInstance()
@@ -564,6 +543,8 @@ class SessionTest extends TestCase
     /**
      * Test that cookieTimeout matches timeout when unspecified.
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testCookieTimeoutFallback()
@@ -581,6 +562,8 @@ class SessionTest extends TestCase
     /**
      * Tests that the cookie name can be changed with configuration
      *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      * @return void
      */
     public function testSessionName()
@@ -591,6 +574,9 @@ class SessionTest extends TestCase
 
     /**
      * Test that a call of check() starts the session when cookies are disabled in php.ini
+     *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
      */
     public function testCheckStartsSessionWithCookiesDisabled()
     {
@@ -631,6 +617,10 @@ class SessionTest extends TestCase
 
     /**
      * Test that a call of check() starts the session when the session ID is passed via URL and session.use_trans_sid is enabled
+     *
+     * @preserveGlobalState disabled
+     * @runInSeparateProcess
+     * @return void
      */
     public function testCheckStartsSessionWithSIDinURL()
     {

+ 1 - 0
tests/bootstrap.php

@@ -131,6 +131,7 @@ Date::setTestNow(Date::now());
 MutableDate::setTestNow(MutableDate::now());
 
 ini_set('intl.default_locale', 'en_US');
+ini_set('session.gc_divisor', '1');
 
 if (class_exists('PHPUnit_Runner_Version')) {
     class_alias('PHPUnit_Framework_TestResult', 'PHPUnit\Framework\TestResult');