Browse Source

Fixing some Session tests

Jose Lorenzo Rodriguez 12 years ago
parent
commit
0a802949e0
3 changed files with 60 additions and 166 deletions
  1. 11 2
      src/Network/Session.php
  2. 47 162
      tests/TestCase/Network/SessionTest.php
  3. 2 2
      tests/bootstrap.php

+ 11 - 2
src/Network/Session.php

@@ -217,6 +217,11 @@ class Session {
 			return true;
 			return true;
 		}
 		}
 
 
+		if (php_sapi_name() === 'cli') {
+			$_SESSION = [];
+			return $this->_started = true;
+		}
+
 		if (session_status() === \PHP_SESSION_ACTIVE) {
 		if (session_status() === \PHP_SESSION_ACTIVE) {
 			throw new \RuntimeException('Session was already started');
 			throw new \RuntimeException('Session was already started');
 		}
 		}
@@ -277,8 +282,12 @@ class Session {
 			$this->start();
 			$this->start();
 		}
 		}
 
 
+		if (!isset($_SESSION)) {
+			return null;
+		}
+
 		if ($name === null) {
 		if ($name === null) {
-			return $_SESSION ?: [];
+			return isset($_SESSION) ? $_SESSION : [];
 		}
 		}
 
 
 		return Hash::get($_SESSION, $name);
 		return Hash::get($_SESSION, $name);
@@ -379,7 +388,7 @@ class Session {
 
 
 		session_destroy();
 		session_destroy();
 
 
-		$_SESSION = null;
+		$_SESSION = [];
 		$this->_started = false;
 		$this->_started = false;
 	}
 	}
 
 

+ 47 - 162
tests/TestCase/Network/SessionTest.php

@@ -18,7 +18,6 @@ namespace Cake\Test\TestCase\Network;
 
 
 use Cake\Cache\Cache;
 use Cake\Cache\Cache;
 use Cake\Core\App;
 use Cake\Core\App;
-use Cake\Core\Configure;
 use Cake\Core\Plugin;
 use Cake\Core\Plugin;
 use Cake\Network\Session;
 use Cake\Network\Session;
 use Cake\Network\Session\CacheSession;
 use Cake\Network\Session\CacheSession;
@@ -26,22 +25,6 @@ use Cake\Network\Session\DatabaseSession;
 use Cake\TestSuite\TestCase;
 use Cake\TestSuite\TestCase;
 
 
 /**
 /**
- * Class TestCakeSession
- *
- */
-class TestCakeSession extends Session {
-
-	public static function setUserAgent($value) {
-		static::$_userAgent = $value;
-	}
-
-	public static function setHost($host) {
-		static::_setHost($host);
-	}
-
-}
-
-/**
  * Class TestCacheSession
  * Class TestCacheSession
  *
  *
  */
  */
@@ -108,13 +91,6 @@ class SessionTest extends TestCase {
  */
  */
 	public function setUp() {
 	public function setUp() {
 		parent::setUp();
 		parent::setUp();
-		Configure::write('Session', array(
-			'defaults' => 'php',
-			'cookie' => 'cakephp',
-			'timeout' => 120,
-			'cookieTimeout' => 120,
-			'ini' => array(),
-		));
 	}
 	}
 
 
 /**
 /**
@@ -123,9 +99,6 @@ class SessionTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function tearDown() {
 	public function tearDown() {
-		if (TestCakeSession::started()) {
-			session_write_close();
-		}
 		unset($_SESSION);
 		unset($_SESSION);
 		parent::tearDown();
 		parent::tearDown();
 	}
 	}
@@ -138,7 +111,7 @@ class SessionTest extends TestCase {
 	public function testSessionConfigIniSetting() {
 	public function testSessionConfigIniSetting() {
 		$_SESSION = null;
 		$_SESSION = null;
 
 
-		Configure::write('Session', array(
+		$config = array(
 			'cookie' => 'test',
 			'cookie' => 'test',
 			'checkAgent' => false,
 			'checkAgent' => false,
 			'timeout' => 86400,
 			'timeout' => 86400,
@@ -146,106 +119,28 @@ class SessionTest extends TestCase {
 				'session.referer_check' => 'example.com',
 				'session.referer_check' => 'example.com',
 				'session.use_trans_sid' => false
 				'session.use_trans_sid' => false
 			)
 			)
-		));
-		TestCakeSession::start();
+		);
+
+		$session = Session::create($config);
 		$this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
 		$this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
 		$this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
 		$this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
 		$this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
 		$this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
 	}
 	}
 
 
 /**
 /**
- * testSessionPath
- *
- * @return void
- */
-	public function testSessionPath() {
-		TestCakeSession::init('/index.php');
-		$this->assertEquals('/', TestCakeSession::$path);
-
-		TestCakeSession::init('/sub_dir/index.php');
-		$this->assertEquals('/sub_dir/', TestCakeSession::$path);
-	}
-
-/**
- * testCakeSessionPathEmpty
- *
- * @return void
- */
-	public function testCakeSessionPathEmpty() {
-		TestCakeSession::init('');
-		$this->assertEquals('/', TestCakeSession::$path, 'Session path is empty, with "" as $base needs to be /');
-	}
-
-/**
- * testCakeSessionPathContainsParams
- *
- * @return void
- */
-	public function testCakeSessionPathContainsQuestion() {
-		TestCakeSession::init('/index.php?');
-		$this->assertEquals('/', TestCakeSession::$path);
-	}
-
-/**
- * testSetHost
- *
- * @return void
- */
-	public function testSetHost() {
-		TestCakeSession::init();
-		TestCakeSession::setHost('cakephp.org');
-		$this->assertEquals('cakephp.org', TestCakeSession::$host);
-	}
-
-/**
- * testSetHostWithPort
- *
- * @return void
- */
-	public function testSetHostWithPort() {
-		TestCakeSession::init();
-		TestCakeSession::setHost('cakephp.org:443');
-		$this->assertEquals('cakephp.org', TestCakeSession::$host);
-	}
-
-/**
- * test valid with bogus user agent.
- *
- * @return void
- */
-	public function testValidBogusUserAgent() {
-		Configure::write('Session.checkAgent', true);
-		TestCakeSession::start();
-		$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
-
-		TestCakeSession::userAgent('bogus!');
-		$this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.');
-	}
-
-/**
- * test valid with bogus user agent.
- *
- * @return void
- */
-	public function testValidTimeExpiry() {
-		Configure::write('Session.checkAgent', true);
-		TestCakeSession::start();
-		$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
-
-		TestCakeSession::$time = strtotime('next year');
-		$this->assertFalse(TestCakeSession::valid(), 'time should cause failure.');
-	}
-
-/**
  * testCheck method
  * testCheck method
  *
  *
  * @return void
  * @return void
  */
  */
 	public function testCheck() {
 	public function testCheck() {
-		TestCakeSession::write('SessionTestCase', 'value');
-		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
-
-		$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'));
+		$session = new Session();
+		$session->write('SessionTestCase', 'value');
+		$this->assertTrue($session->check('SessionTestCase'));
+		$this->assertFalse($session->check('NotExistingSessionTestCase'));
+		$this->assertFalse($session->check('Crazy.foo'));
+		$session->write('Crazy.foo', ['bar' => 'baz']);
+		$this->assertTrue($session->check('Crazy.foo'));
+		$this->assertTrue($session->check('Crazy.foo.bar'));
 	}
 	}
 
 
 /**
 /**
@@ -254,34 +149,34 @@ class SessionTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testSimpleRead() {
 	public function testSimpleRead() {
-		TestCakeSession::write('testing', '1,2,3');
-		$result = TestCakeSession::read('testing');
+		$session = new Session();
+		$session->write('testing', '1,2,3');
+		$result = $session->read('testing');
 		$this->assertEquals('1,2,3', $result);
 		$this->assertEquals('1,2,3', $result);
 
 
-		TestCakeSession::write('testing', array('1' => 'one', '2' => 'two', '3' => 'three'));
-		$result = TestCakeSession::read('testing.1');
+		$session->write('testing', ['1' => 'one', '2' => 'two', '3' => 'three']);
+		$result = $session->read('testing.1');
 		$this->assertEquals('one', $result);
 		$this->assertEquals('one', $result);
 
 
-		$result = TestCakeSession::read('testing');
-		$this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result);
+		$result = $session->read('testing');
+		$this->assertEquals(['1' => 'one', '2' => 'two', '3' => 'three'], $result);
 
 
-		$result = TestCakeSession::read();
+		$result = $session->read();
 		$this->assertTrue(isset($result['testing']));
 		$this->assertTrue(isset($result['testing']));
-		$this->assertTrue(isset($result['Config']));
-		$this->assertTrue(isset($result['Config']['userAgent']));
 
 
-		TestCakeSession::write('This.is.a.deep.array.my.friend', 'value');
-		$result = TestCakeSession::read('This.is.a.deep.array.my.friend');
-		$this->assertEquals('value', $result);
+		$session->write('This.is.a.deep.array.my.friend', 'value');
+		$result = $session->read('This.is.a.deep.array');
+		$this->assertEquals(['my' => ['friend' => 'value']], $result);
 	}
 	}
 
 
 /**
 /**
- * testReadyEmpty
+ * testReadEmpty
  *
  *
  * @return void
  * @return void
  */
  */
-	public function testReadyEmpty() {
-		$this->assertFalse(TestCakeSession::read(''));
+	public function testReadEmpty() {
+		$session = new Session();
+		$this->assertNull($session->read(''));
 	}
 	}
 
 
 /**
 /**
@@ -290,27 +185,16 @@ class SessionTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testWriteArray() {
 	public function testWriteArray() {
-		$result = TestCakeSession::write(array(
+		$session = new Session();
+		$session->write([
 			'one' => 1,
 			'one' => 1,
 			'two' => 2,
 			'two' => 2,
-			'three' => array('something'),
+			'three' => ['something'],
 			'null' => null
 			'null' => null
-		));
-		$this->assertTrue($result);
-		$this->assertEquals(1, TestCakeSession::read('one'));
-		$this->assertEquals(array('something'), TestCakeSession::read('three'));
-		$this->assertEquals(null, TestCakeSession::read('null'));
-	}
-
-/**
- * testWriteEmptyKey
- *
- * @return void
- */
-	public function testWriteEmptyKey() {
-		$this->assertFalse(TestCakeSession::write('', 'graham'));
-		$this->assertFalse(TestCakeSession::write('', ''));
-		$this->assertFalse(TestCakeSession::write(''));
+		]);
+		$this->assertEquals(1, $session->read('one'));
+		$this->assertEquals(['something'], $session->read('three'));
+		$this->assertEquals(null, $session->read('null'));
 	}
 	}
 
 
 /**
 /**
@@ -319,14 +203,12 @@ class SessionTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testWriteOverwriteStringValue() {
 	public function testWriteOverwriteStringValue() {
-		TestCakeSession::write('Some.string', 'value');
-		$this->assertEquals('value', TestCakeSession::read('Some.string'));
+		$session = new Session();
+		$session->write('Some.string', 'value');
+		$this->assertEquals('value', $session->read('Some.string'));
 
 
-		TestCakeSession::write('Some.string.array', array('values'));
-		$this->assertEquals(
-			array('values'),
-			TestCakeSession::read('Some.string.array')
-		);
+		$session->write('Some.string.array', ['values']);
+		$this->assertEquals(['values'], $session->read('Some.string.array'));
 	}
 	}
 
 
 /**
 /**
@@ -335,15 +217,18 @@ class SessionTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testId() {
 	public function testId() {
-		TestCakeSession::destroy();
-
-		$result = TestCakeSession::id();
+		$session = new Session();
+		$result = $session->id();
 		$expected = session_id();
 		$expected = session_id();
 		$this->assertEquals($expected, $result);
 		$this->assertEquals($expected, $result);
 
 
-		TestCakeSession::id('MySessionId');
-		$result = TestCakeSession::id();
+		$session->id('MySessionId');
+		$result = $session->id();
 		$this->assertEquals('MySessionId', $result);
 		$this->assertEquals('MySessionId', $result);
+		$this->assertEquals('MySessionId', session_id());
+
+		$session->id('');
+		$this->assertEquals('', session_id());
 	}
 	}
 
 
 /**
 /**

+ 2 - 2
tests/bootstrap.php

@@ -17,7 +17,7 @@ use Cake\Datasource\ConnectionManager;
 use Cake\I18n\I18n;
 use Cake\I18n\I18n;
 use Cake\Log\Log;
 use Cake\Log\Log;
 
 
-require 'vendor/autoload.php';
+require_once 'vendor/autoload.php';
 
 
 define('DS', DIRECTORY_SEPARATOR);
 define('DS', DIRECTORY_SEPARATOR);
 define('ROOT', dirname(__DIR__));
 define('ROOT', dirname(__DIR__));
@@ -60,7 +60,7 @@ $loader->addNamespace('TestPlugin', TEST_APP . 'Plugin/TestPlugin');
 $loader->addNamespace('TestPluginTwo', TEST_APP . 'Plugin/TestPluginTwo');
 $loader->addNamespace('TestPluginTwo', TEST_APP . 'Plugin/TestPluginTwo');
 $loader->addNamespace('PluginJs', TEST_APP . 'Plugin/PluginJs');
 $loader->addNamespace('PluginJs', TEST_APP . 'Plugin/PluginJs');
 
 
-require CAKE . 'bootstrap.php';
+require_once CAKE . 'bootstrap.php';
 
 
 date_default_timezone_set('UTC');
 date_default_timezone_set('UTC');
 mb_internal_encoding('UTF-8');
 mb_internal_encoding('UTF-8');