Changeset 1563
- Timestamp:
- 04/24/08 17:09:12 (3 weeks ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ipo/session/stubNoneDurableSession.php
r1549 r1563 7 7 * @subpackage ipo_session 8 8 */ 9 stubClassLoader::load('net::stubbles::ipo::session::stubAbstractSession'); 9 stubClassLoader::load('net::stubbles::ipo::request::validator::stubRegexValidator', 10 'net::stubbles::ipo::session::stubAbstractSession' 11 ); 10 12 /** 11 13 * Session class that is not durable for more than one request. … … 21 23 * @var int 22 24 */ 23 protected $id = 312;25 protected $id; 24 26 /** 25 27 * the data … … 27 29 * @var array 28 30 */ 29 protected $data = array(); 31 protected $data = array(); 32 /** 33 * the response instance 34 * 35 * @var stubResponse 36 */ 37 protected $response; 38 /** 39 * regular expression to validate the session id 40 */ 41 const REGEX_SESSION_ID = '/^([a-zA-Z0-9]{32})$/D'; 30 42 31 43 /** … … 39 51 protected function doConstruct(stubRequest $request, stubResponse $response, $sessionName) 40 52 { 53 $this->response = $response; 54 if ($request->hasValue($sessionName) === true) { 55 $this->id = $request->getValidatedValue(new stubRegexValidator(self::REGEX_SESSION_ID), $sessionName); 56 $this->data = array(stubSession::START_TIME => time(), 57 stubSession::FINGERPRINT => '', 58 stubSession::NEXT_TOKEN => '' 59 ); 60 } elseif ($request->hasValue($sessionName, stubRequest::SOURCE_COOKIE) === true) { 61 $this->id = $request->getValidatedValue(new stubRegexValidator(self::REGEX_SESSION_ID), $sessionName, stubRequest::SOURCE_COOKIE); 62 $this->data = array(stubSession::START_TIME => time(), 63 stubSession::FINGERPRINT => '', 64 stubSession::NEXT_TOKEN => '' 65 ); 66 } else { 67 $this->id = md5(uniqid(rand(), true)); 68 } 69 70 $this->response->setCookie(stubCookie::create($sessionName, $this->id)->forPath('/')); 41 71 return true; 42 72 } … … 57 87 * @return string the session id 58 88 */ 59 public function getI D()89 public function getId() 60 90 { 61 91 return $this->id; … … 65 95 * regenerates the session id but leaves session data 66 96 */ 67 public function regenerateI D()97 public function regenerateId() 68 98 { 69 $this->id++; 99 $this->id = md5(uniqid(rand(), true)); 100 $this->response->setCookie(stubCookie::create($this->sessionName, $this->id)->forPath('/')); 70 101 } 71 102 trunk/src/test/php/net/stubbles/ipo/session/stubNoneDurableSessionTestCase.php
r1531 r1563 22 22 */ 23 23 protected $session; 24 /** 25 * mocked request instance 26 * 27 * @var PHPUnit_Framework_MockObject_MockObject 28 */ 29 protected $mockRequest; 30 /** 31 * response instance 32 * 33 * @var stubResponse 34 */ 35 protected $response; 24 36 25 37 /** … … 28 40 public function setUp() 29 41 { 30 $this->session = new stubNoneDurableSession($this->getMock('stubRequest'), $this->getMock('stubResponse'), 'test'); 42 $this->mockRequest = $this->getMock('stubRequest'); 43 $this->response = new stubBaseResponse(); 44 $this->session = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 45 } 46 47 /** 48 * session id from request should be used 49 * 50 * @test 51 */ 52 public function useSessionIdFromRequest() 53 { 54 $this->mockRequest->expects($this->once()) 55 ->method('hasValue') 56 ->with($this->equalTo('test')) 57 ->will($this->returnValue(true)); 58 $this->mockRequest->expects($this->once()) 59 ->method('getValidatedValue') 60 ->will($this->returnValue('313')); 61 $session = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 62 $this->assertEquals('313', $session->getId()); 63 $cookies = $this->response->getCookies(); 64 $this->assertType('stubCookie', $cookies['test']); 65 $this->assertEquals('test', $cookies['test']->getName()); 66 $this->assertEquals('313', $cookies['test']->getValue()); 67 } 68 69 /** 70 * session id from cookie should be used 71 * 72 * @test 73 */ 74 public function useSessionIdFromCookie() 75 { 76 $this->mockRequest->expects($this->exactly(2)) 77 ->method('hasValue') 78 ->will($this->onConsecutiveCalls(false, true)); 79 $this->mockRequest->expects($this->once()) 80 ->method('getValidatedValue') 81 ->will($this->returnValue('313')); 82 $session = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 83 $this->assertEquals('313', $session->getId()); 84 $cookies = $this->response->getCookies(); 85 $this->assertType('stubCookie', $cookies['test']); 86 $this->assertEquals('test', $cookies['test']->getName()); 87 $this->assertEquals('313', $cookies['test']->getValue()); 88 } 89 90 /** 91 * new session id should be generated 92 * 93 * @test 94 */ 95 public function generateNewSessionId() 96 { 97 $this->mockRequest->expects($this->exactly(2)) 98 ->method('hasValue') 99 ->will($this->returnValue(false)); 100 $this->mockRequest->expects($this->never()) 101 ->method('getValidatedValue'); 102 $session = new stubNoneDurableSession($this->mockRequest, $this->response, 'test'); 103 $cookies = $this->response->getCookies(); 104 $this->assertType('stubCookie', $cookies['test']); 105 $this->assertEquals('test', $cookies['test']->getName()); 106 $this->assertEquals($session->getId(), $cookies['test']->getValue()); 31 107 } 32 108 … … 41 117 $this->session->regenerateId(); 42 118 $this->assertNotEquals($id, $this->session->getId()); 119 $cookies = $this->response->getCookies(); 120 $this->assertType('stubCookie', $cookies['test']); 121 $this->assertEquals('test', $cookies['test']->getName()); 122 $this->assertNotEquals($id, $cookies['test']->getValue()); 123 $this->assertEquals($this->session->getId(), $cookies['test']->getValue()); 43 124 } 44 125
