Skip to content
This repository was 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 3 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
8 changes: 5 additions & 3 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 Down Expand Up @@ -100,7 +99,10 @@ 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()
|| ($session->hasChanged() && ! $this->cookie)) {
Copy link
Member

Choose a reason for hiding this comment

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

) { from if statement should be in new line

$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(): void
Copy link
Member

Choose a reason for hiding this comment

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

Usually we are not adding : void in tests, but if we keep it here, there should be one space before : (consistency).

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

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

$this->assertEmpty($response->getHeaderLine('Set-Cookie'));
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can just check $this->assertFalse($response->hasHeader()) ?

}

public function testCookiesSetWithoutRegenerate(): void
{
$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'));
}
}