Skip to content

Commit

Permalink
Merge pull request #1128 from andreif23/develop
Browse files Browse the repository at this point in the history
Run session tests in separate processes - fix for #1106
  • Loading branch information
lonnieezell authored Aug 3, 2018
2 parents 6c3ad4c + 6b6703d commit 6efd2b0
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ function set_radio(string $field, string $value = '', bool $default = false): st

// Unchecked checkbox and radio inputs are not even submitted by browsers ...
$result = '';
if ($request->getPost())
if (!empty($input = $request->getPost($field)) || !empty($input = old($field)))
{
$result = ($input === $value) ? ' checked="checked"' : '';
}
Expand Down
51 changes: 51 additions & 0 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use CodeIgniter\Session\Handlers\FileHandler;
use Config\App;
use Config\Autoload;
use CodeIgniter\Config\Services;
Expand All @@ -8,8 +9,11 @@
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use Config\Logger;
use Tests\Support\Autoloader\MockFileLocator;
use Tests\Support\HTTP\MockIncomingRequest;
use Tests\Support\Log\TestLogger;
use Tests\Support\Session\MockSession;

/**
* @backupGlobals enabled
Expand Down Expand Up @@ -156,19 +160,38 @@ public function testEscapeBadContext()

// ------------------------------------------------------------------------

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSessionInstance()
{
$this->injectSessionMock();

$this->assertInstanceOf(CodeIgniter\Session\Session::class, session());
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSessionVariable()
{
$this->injectSessionMock();

$_SESSION['notbogus'] = 'Hi there';

$this->assertEquals('Hi there', session('notbogus'));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSessionVariableNotThere()
{
$this->injectSessionMock();

$_SESSION['bogus'] = 'Hi there';
$this->assertEquals(null, session('notbogus'));
}
Expand Down Expand Up @@ -231,8 +254,13 @@ public function testCSRFField()

// ------------------------------------------------------------------------

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testOldInput()
{
$this->injectSessionMock();
// setup from RedirectResponseTest...
$_SERVER['REQUEST_METHOD'] = 'GET';

Expand Down Expand Up @@ -275,4 +303,27 @@ public function testSlashItem()
$this->assertEquals('en/', slash_item('defaultLocale')); // slash appended
}

protected function injectSessionMock()
{
$defaults = [
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler',
'sessionCookieName' => 'ci_session',
'sessionExpiration' => 7200,
'sessionSavePath' => null,
'sessionMatchIP' => false,
'sessionTimeToUpdate' => 300,
'sessionRegenerateDestroy' => false,
'cookieDomain' => '',
'cookiePrefix' => '',
'cookiePath' => '/',
'cookieSecure' => false,
];

$config = (object)$defaults;

$session = new MockSession(new FileHandler($config), $config);
$session->setLogger(new TestLogger(new Logger()));
\CodeIgniter\Config\BaseService::injectMock('session', $session);
}

}
12 changes: 12 additions & 0 deletions tests/system/Config/ServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,30 @@ public function testNewViewcell()
$this->assertInstanceOf(\CodeIgniter\View\Cell::class, $actual);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testNewSession()
{
$actual = Services::session($this->config);
$this->assertInstanceOf(\CodeIgniter\Session\Session::class, $actual);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testNewSessionWithNullConfig()
{
$actual = Services::session(null, false);
$this->assertInstanceOf(\CodeIgniter\Session\Session::class, $actual);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testCallStatic()
{
// __callStatic should kick in for this but fail
Expand Down
12 changes: 12 additions & 0 deletions tests/system/HTTP/RedirectResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function testRedirectRelativeConvertsToFullURI()
$this->assertEquals('http://example.com/foo', $response->getHeaderLine('Location'));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testWithInput()
{
$_SESSION = [];
Expand All @@ -68,6 +72,10 @@ public function testWithInput()
$this->assertEquals('baz', $_SESSION['_ci_old_input']['post']['bar']);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testWithValidationErrors()
{
$_SESSION = [];
Expand All @@ -85,6 +93,10 @@ public function testWithValidationErrors()
$this->assertArrayHasKey('_ci_validation_errors', $_SESSION);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testWith()
{
$_SESSION = [];
Expand Down
8 changes: 8 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,10 @@ public function testSetCheckbox()
}

// ------------------------------------------------------------------------
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetRadio()
{
$_SESSION = [
Expand All @@ -771,6 +775,10 @@ public function testSetRadio()
unset($_SESSION['_ci_old_input']);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSetRadioFromPost()
{
$_POST['bar'] = 'baz';
Expand Down
9 changes: 6 additions & 3 deletions tests/system/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Tests\Support\Session\MockSession;
use CodeIgniter\Session\Handlers\FileHandler;

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class SessionTest extends \CIUnitTestCase
{
public function setUp()
Expand Down Expand Up @@ -110,7 +114,7 @@ public function testGetReturnsNullWhenNotFound()
$this->assertNull($session->get('foo'));
}

public function testGetReturnsAllWithNoKeys()
public function testGetReturnsAllWithNoKeys()
{
$_SESSION = [
'foo' => 'bar',
Expand All @@ -126,8 +130,7 @@ public function testGetReturnsAllWithNoKeys()
$this->assertTrue(array_key_exists('bar', $result));
}


public function testGetAsProperty()
public function testGetAsProperty()
{
$session = $this->getInstance();
$session->start();
Expand Down

0 comments on commit 6efd2b0

Please sign in to comment.