Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Generate a session id for new sessions with data #21

Merged
merged 7 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2017, Zend Technologies USA, Inc.
Copyright (c) 2017-2018, Zend Technologies USA, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
10 changes: 6 additions & 4 deletions src/PhpSessionPersistence.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-session-ext for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-session-ext/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Expressive\Session\Ext;

use Dflydev\FigCookies\FigCookies\Cookie;
use Dflydev\FigCookies\FigRequestCookies;
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;
Expand All @@ -16,7 +15,6 @@
use Zend\Expressive\Session\Session;
use Zend\Expressive\Session\SessionInterface;
use Zend\Expressive\Session\SessionPersistenceInterface;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change, we should have an empty line between different type of imports.

use function array_merge;
use function bin2hex;
use function filemtime;
Expand Down Expand Up @@ -100,7 +98,11 @@ public function initializeSessionFromRequest(ServerRequestInterface $request) :

public function persistSession(SessionInterface $session, ResponseInterface $response) : ResponseInterface
{
if ($session->isRegenerated()) {
// Regenerate if the session is marked as regenerated
// Regenerate if there is no cookie id set but the session has changed (new session with data)
if ($session->isRegenerated()
|| (! $this->cookie && $session->hasChanged())
) {
$this->regenerateSession();
}

Expand Down
30 changes: 28 additions & 2 deletions test/PhpSessionPersistenceTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive-session-ext for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-session-ext/blob/master/LICENSE.md New BSD License
*/

Expand Down Expand Up @@ -198,7 +198,7 @@ public function testPersistSessionIfSessionHasContents()
{
$this->startSession();
$session = new Session(['foo' => 'bar']);
$this->persistence->persistSession($session, new Response);
$this->persistence->persistSession($session, new Response());
$this->assertSame($session->toArray(), $_SESSION);
}

Expand Down Expand Up @@ -422,4 +422,30 @@ public function testPersistSessionReturnsExpectedResponseWithoutAddedCacheHeader

$this->restoreOriginalSessionIniSettings($ini);
}

public function testCookiesNotSetWithoutRegenerate()
{
$persistence = new PhpSessionPersistence();
$request = new ServerRequest();
$session = $persistence->initializeSessionFromRequest($request);

$response = new Response();
$response = $persistence->persistSession($session, $response);

$this->assertFalse($response->hasHeader('Set-Cookie'));
}

public function testCookiesSetWithoutRegenerate()
{
$persistence = new PhpSessionPersistence();
$request = new ServerRequest();
$session = $persistence->initializeSessionFromRequest($request);

$session->set('foo', 'bar');

$response = new Response();
$response = $persistence->persistSession($session, $response);

$this->assertNotEmpty($response->getHeaderLine('Set-Cookie'));
}
}