| @@ -4,7 +4,6 @@ | ||
| 4 | 4 | |
| 5 | 5 | use SilverStripe\Control\Cookie; | 
| 6 | 6 | use SilverStripe\HybridSessions\Crypto\CryptoHandler; | 
| 7 | -use SilverStripe\Core\Config\Config; | |
| 8 | 7 | use SilverStripe\Core\Injector\Injector; | 
| 9 | 8 | |
| 10 | 9 | /** | 
| @@ -22,184 +22,184 @@ | ||
| 22 | 22 | class CookieStore extends BaseStore | 
| 23 | 23 |  { | 
| 24 | 24 | |
| 25 | - /** | |
| 26 | - * Maximum length of a cookie value in characters | |
| 27 | - * | |
| 28 | - * @var int | |
| 29 | - * @config | |
| 30 | - */ | |
| 31 | - private static $max_length = 1024; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * Encryption service | |
| 35 | - * | |
| 36 | - * @var HybridSessionStore_Crypto | |
| 37 | - */ | |
| 38 | - protected $crypto; | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Name of cookie | |
| 42 | - * | |
| 43 | - * @var string | |
| 44 | - */ | |
| 45 | - protected $cookie; | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Known unmodified value of this cookie. If the cookie backend has been read into the application, | |
| 49 | - * then the backend is unable to verify the modification state of this value internally within the | |
| 50 | - * system, so this will be left null unless written back. | |
| 51 | - * | |
| 52 | - * If the content exceeds max_length then the backend can also not maintain this cookie, also | |
| 53 | - * setting this variable to null. | |
| 54 | - * | |
| 55 | - * @var string | |
| 56 | - */ | |
| 57 | - protected $currentCookieData; | |
| 58 | - | |
| 59 | - public function open($save_path, $name) | |
| 60 | -    { | |
| 61 | - $this->cookie = $name . '_2'; | |
| 62 | - | |
| 63 | - // Read the incoming value, then clear the cookie - we might not be able | |
| 64 | - // to do so later if write() is called after headers are sent | |
| 65 | - // This is intended to force a failover to the database store if the | |
| 66 | - // modified session cannot be emitted. | |
| 67 | - $this->currentCookieData = Cookie::get($this->cookie); | |
| 68 | - | |
| 69 | -        if ($this->currentCookieData) { | |
| 70 | - Cookie::set($this->cookie, ''); | |
| 71 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - public function close() | |
| 75 | -    { | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * Get the cryptography store for the specified session | |
| 80 | - * | |
| 81 | - * @param string $session_id | |
| 82 | - * @return HybridSessionStore_Crypto | |
| 83 | - */ | |
| 84 | - protected function getCrypto($session_id) | |
| 85 | -    { | |
| 86 | - $key = $this->getKey(); | |
| 87 | - | |
| 88 | -        if (!$key) { | |
| 89 | - return null; | |
| 90 | - } | |
| 91 | - | |
| 92 | -        if (!$this->crypto || $this->crypto->getSalt() != $session_id) { | |
| 93 | - $this->crypto = Injector::inst()->create(CryptoHandler::class, $key, $session_id); | |
| 94 | - } | |
| 95 | - | |
| 96 | - return $this->crypto; | |
| 97 | - } | |
| 98 | - | |
| 99 | - public function read($session_id) | |
| 100 | -    { | |
| 101 | - // Check ability to safely decrypt content | |
| 102 | - if (!$this->currentCookieData | |
| 103 | - || !($crypto = $this->getCrypto($session_id)) | |
| 104 | -        ) { | |
| 105 | - return; | |
| 106 | - } | |
| 107 | - | |
| 108 | - // Decrypt and invalidate old data | |
| 109 | - $cookieData = $crypto->decrypt($this->currentCookieData); | |
| 110 | - $this->currentCookieData = null; | |
| 111 | - | |
| 112 | - // Verify expiration | |
| 113 | -        if ($cookieData) { | |
| 114 | - $expiry = (int)substr($cookieData, 0, 10); | |
| 115 | - $data = substr($cookieData, 10); | |
| 116 | - | |
| 117 | -            if ($expiry > $this->getNow()) { | |
| 118 | - return $data; | |
| 119 | - } | |
| 120 | - } | |
| 121 | - } | |
| 122 | - | |
| 123 | - /** | |
| 124 | - * Determine if the session could be verifably written to cookie storage | |
| 125 | - * | |
| 126 | - * @return bool | |
| 127 | - */ | |
| 128 | - protected function canWrite() | |
| 129 | -    { | |
| 130 | - return !headers_sent(); | |
| 131 | - } | |
| 132 | - | |
| 133 | - public function write($session_id, $session_data) | |
| 134 | -    { | |
| 135 | - $canWrite = $this->canWrite(); | |
| 136 | -        $isExceedingCookieLimit = (strlen($session_data) > static::config()->get('max_length')); | |
| 137 | - $crypto = $this->getCrypto($session_id); | |
| 138 | - | |
| 139 | - // Check ability to safely encrypt and write content | |
| 140 | -        if (!$canWrite || $isExceedingCookieLimit || !$crypto) { | |
| 141 | -            if ($canWrite && $isExceedingCookieLimit) { | |
| 142 | - $params = session_get_cookie_params(); | |
| 143 | - // Clear stored cookie value and cookie when length exceeds the set limit | |
| 144 | - $this->currentCookieData = null; | |
| 145 | - Cookie::set( | |
| 146 | - $this->cookie, | |
| 147 | - '', | |
| 148 | - 0, | |
| 149 | - $params['path'], | |
| 150 | - $params['domain'], | |
| 151 | - $params['secure'], | |
| 152 | - $params['httponly'] | |
| 153 | - ); | |
| 154 | - } | |
| 155 | - | |
| 156 | - return false; | |
| 157 | - } | |
| 158 | - | |
| 159 | - // Prepare content for write | |
| 160 | - $params = session_get_cookie_params(); | |
| 161 | - // Total max lifetime, stored internally | |
| 162 | - $lifetime = $this->getLifetime(); | |
| 163 | - $expiry = $this->getNow() + $lifetime; | |
| 164 | - | |
| 165 | - // Restore the known good cookie value | |
| 166 | - $this->currentCookieData = $this->crypto->encrypt( | |
| 167 | -            sprintf('%010u', $expiry) . $session_data | |
| 168 | - ); | |
| 169 | - | |
| 170 | - // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) | |
| 171 | - $cookieLifetime = min((int)$params['lifetime'], $lifetime); | |
| 172 | - | |
| 173 | - Cookie::set( | |
| 174 | - $this->cookie, | |
| 175 | - $this->currentCookieData, | |
| 176 | - $cookieLifetime / 86400, | |
| 177 | - $params['path'], | |
| 178 | - $params['domain'], | |
| 179 | - $params['secure'], | |
| 180 | - $params['httponly'] | |
| 181 | - ); | |
| 182 | - | |
| 183 | - return true; | |
| 184 | - } | |
| 185 | - | |
| 186 | - public function destroy($session_id) | |
| 187 | -    { | |
| 188 | - $this->currentCookieData = null; | |
| 189 | - | |
| 190 | - $params = session_get_cookie_params(); | |
| 191 | - | |
| 192 | - Cookie::force_expiry( | |
| 193 | - $this->cookie, | |
| 194 | - $params['path'], | |
| 195 | - $params['domain'], | |
| 196 | - $params['secure'], | |
| 197 | - $params['httponly'] | |
| 198 | - ); | |
| 199 | - } | |
| 200 | - | |
| 201 | - public function gc($maxlifetime) | |
| 202 | -    { | |
| 203 | - // NOP | |
| 204 | - } | |
| 25 | + /** | |
| 26 | + * Maximum length of a cookie value in characters | |
| 27 | + * | |
| 28 | + * @var int | |
| 29 | + * @config | |
| 30 | + */ | |
| 31 | + private static $max_length = 1024; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Encryption service | |
| 35 | + * | |
| 36 | + * @var HybridSessionStore_Crypto | |
| 37 | + */ | |
| 38 | + protected $crypto; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Name of cookie | |
| 42 | + * | |
| 43 | + * @var string | |
| 44 | + */ | |
| 45 | + protected $cookie; | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Known unmodified value of this cookie. If the cookie backend has been read into the application, | |
| 49 | + * then the backend is unable to verify the modification state of this value internally within the | |
| 50 | + * system, so this will be left null unless written back. | |
| 51 | + * | |
| 52 | + * If the content exceeds max_length then the backend can also not maintain this cookie, also | |
| 53 | + * setting this variable to null. | |
| 54 | + * | |
| 55 | + * @var string | |
| 56 | + */ | |
| 57 | + protected $currentCookieData; | |
| 58 | + | |
| 59 | + public function open($save_path, $name) | |
| 60 | +	{ | |
| 61 | + $this->cookie = $name . '_2'; | |
| 62 | + | |
| 63 | + // Read the incoming value, then clear the cookie - we might not be able | |
| 64 | + // to do so later if write() is called after headers are sent | |
| 65 | + // This is intended to force a failover to the database store if the | |
| 66 | + // modified session cannot be emitted. | |
| 67 | + $this->currentCookieData = Cookie::get($this->cookie); | |
| 68 | + | |
| 69 | +		if ($this->currentCookieData) { | |
| 70 | + Cookie::set($this->cookie, ''); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + public function close() | |
| 75 | +	{ | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Get the cryptography store for the specified session | |
| 80 | + * | |
| 81 | + * @param string $session_id | |
| 82 | + * @return HybridSessionStore_Crypto | |
| 83 | + */ | |
| 84 | + protected function getCrypto($session_id) | |
| 85 | +	{ | |
| 86 | + $key = $this->getKey(); | |
| 87 | + | |
| 88 | +		if (!$key) { | |
| 89 | + return null; | |
| 90 | + } | |
| 91 | + | |
| 92 | +		if (!$this->crypto || $this->crypto->getSalt() != $session_id) { | |
| 93 | + $this->crypto = Injector::inst()->create(CryptoHandler::class, $key, $session_id); | |
| 94 | + } | |
| 95 | + | |
| 96 | + return $this->crypto; | |
| 97 | + } | |
| 98 | + | |
| 99 | + public function read($session_id) | |
| 100 | +	{ | |
| 101 | + // Check ability to safely decrypt content | |
| 102 | + if (!$this->currentCookieData | |
| 103 | + || !($crypto = $this->getCrypto($session_id)) | |
| 104 | +		) { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Decrypt and invalidate old data | |
| 109 | + $cookieData = $crypto->decrypt($this->currentCookieData); | |
| 110 | + $this->currentCookieData = null; | |
| 111 | + | |
| 112 | + // Verify expiration | |
| 113 | +		if ($cookieData) { | |
| 114 | + $expiry = (int)substr($cookieData, 0, 10); | |
| 115 | + $data = substr($cookieData, 10); | |
| 116 | + | |
| 117 | +			if ($expiry > $this->getNow()) { | |
| 118 | + return $data; | |
| 119 | + } | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Determine if the session could be verifably written to cookie storage | |
| 125 | + * | |
| 126 | + * @return bool | |
| 127 | + */ | |
| 128 | + protected function canWrite() | |
| 129 | +	{ | |
| 130 | + return !headers_sent(); | |
| 131 | + } | |
| 132 | + | |
| 133 | + public function write($session_id, $session_data) | |
| 134 | +	{ | |
| 135 | + $canWrite = $this->canWrite(); | |
| 136 | +		$isExceedingCookieLimit = (strlen($session_data) > static::config()->get('max_length')); | |
| 137 | + $crypto = $this->getCrypto($session_id); | |
| 138 | + | |
| 139 | + // Check ability to safely encrypt and write content | |
| 140 | +		if (!$canWrite || $isExceedingCookieLimit || !$crypto) { | |
| 141 | +			if ($canWrite && $isExceedingCookieLimit) { | |
| 142 | + $params = session_get_cookie_params(); | |
| 143 | + // Clear stored cookie value and cookie when length exceeds the set limit | |
| 144 | + $this->currentCookieData = null; | |
| 145 | + Cookie::set( | |
| 146 | + $this->cookie, | |
| 147 | + '', | |
| 148 | + 0, | |
| 149 | + $params['path'], | |
| 150 | + $params['domain'], | |
| 151 | + $params['secure'], | |
| 152 | + $params['httponly'] | |
| 153 | + ); | |
| 154 | + } | |
| 155 | + | |
| 156 | + return false; | |
| 157 | + } | |
| 158 | + | |
| 159 | + // Prepare content for write | |
| 160 | + $params = session_get_cookie_params(); | |
| 161 | + // Total max lifetime, stored internally | |
| 162 | + $lifetime = $this->getLifetime(); | |
| 163 | + $expiry = $this->getNow() + $lifetime; | |
| 164 | + | |
| 165 | + // Restore the known good cookie value | |
| 166 | + $this->currentCookieData = $this->crypto->encrypt( | |
| 167 | +			sprintf('%010u', $expiry) . $session_data | |
| 168 | + ); | |
| 169 | + | |
| 170 | + // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) | |
| 171 | + $cookieLifetime = min((int)$params['lifetime'], $lifetime); | |
| 172 | + | |
| 173 | + Cookie::set( | |
| 174 | + $this->cookie, | |
| 175 | + $this->currentCookieData, | |
| 176 | + $cookieLifetime / 86400, | |
| 177 | + $params['path'], | |
| 178 | + $params['domain'], | |
| 179 | + $params['secure'], | |
| 180 | + $params['httponly'] | |
| 181 | + ); | |
| 182 | + | |
| 183 | + return true; | |
| 184 | + } | |
| 185 | + | |
| 186 | + public function destroy($session_id) | |
| 187 | +	{ | |
| 188 | + $this->currentCookieData = null; | |
| 189 | + | |
| 190 | + $params = session_get_cookie_params(); | |
| 191 | + | |
| 192 | + Cookie::force_expiry( | |
| 193 | + $this->cookie, | |
| 194 | + $params['path'], | |
| 195 | + $params['domain'], | |
| 196 | + $params['secure'], | |
| 197 | + $params['httponly'] | |
| 198 | + ); | |
| 199 | + } | |
| 200 | + | |
| 201 | + public function gc($maxlifetime) | |
| 202 | +	{ | |
| 203 | + // NOP | |
| 204 | + } | |
| 205 | 205 | } | 
| @@ -8,56 +8,56 @@ | ||
| 8 | 8 | |
| 9 | 9 | abstract class BaseStore implements SessionHandlerInterface | 
| 10 | 10 |  { | 
| 11 | - use Configurable; | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * Session secret key | |
| 15 | - * | |
| 16 | - * @var string | |
| 17 | - */ | |
| 18 | - protected $key = null; | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Assign a new session secret key | |
| 22 | - * | |
| 23 | - * @param string $key | |
| 24 | - */ | |
| 25 | - public function setKey($key) | |
| 26 | -    { | |
| 27 | - $this->key = $key; | |
| 28 | - } | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * Get the session secret key | |
| 32 | - * | |
| 33 | - * @return string | |
| 34 | - */ | |
| 35 | - protected function getKey() | |
| 36 | -    { | |
| 37 | - return $this->key; | |
| 38 | - } | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Get lifetime in number of seconds | |
| 42 | - * | |
| 43 | - * @return int | |
| 44 | - */ | |
| 45 | - protected function getLifetime() | |
| 46 | -    { | |
| 47 | - $params = session_get_cookie_params(); | |
| 48 | - $cookieLifetime = (int)$params['lifetime']; | |
| 49 | -        $gcLifetime = (int)ini_get('session.gc_maxlifetime'); | |
| 50 | - | |
| 51 | - return $cookieLifetime ? min($cookieLifetime, $gcLifetime) : $gcLifetime; | |
| 52 | - } | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Gets the current unix timestamp | |
| 56 | - * | |
| 57 | - * @return int | |
| 58 | - */ | |
| 59 | - protected function getNow() | |
| 60 | -    { | |
| 61 | - return (int) DBDatetime::now()->getTimestamp(); | |
| 62 | - } | |
| 11 | + use Configurable; | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * Session secret key | |
| 15 | + * | |
| 16 | + * @var string | |
| 17 | + */ | |
| 18 | + protected $key = null; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Assign a new session secret key | |
| 22 | + * | |
| 23 | + * @param string $key | |
| 24 | + */ | |
| 25 | + public function setKey($key) | |
| 26 | +	{ | |
| 27 | + $this->key = $key; | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Get the session secret key | |
| 32 | + * | |
| 33 | + * @return string | |
| 34 | + */ | |
| 35 | + protected function getKey() | |
| 36 | +	{ | |
| 37 | + return $this->key; | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Get lifetime in number of seconds | |
| 42 | + * | |
| 43 | + * @return int | |
| 44 | + */ | |
| 45 | + protected function getLifetime() | |
| 46 | +	{ | |
| 47 | + $params = session_get_cookie_params(); | |
| 48 | + $cookieLifetime = (int)$params['lifetime']; | |
| 49 | +		$gcLifetime = (int)ini_get('session.gc_maxlifetime'); | |
| 50 | + | |
| 51 | + return $cookieLifetime ? min($cookieLifetime, $gcLifetime) : $gcLifetime; | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Gets the current unix timestamp | |
| 56 | + * | |
| 57 | + * @return int | |
| 58 | + */ | |
| 59 | + protected function getNow() | |
| 60 | +	{ | |
| 61 | + return (int) DBDatetime::now()->getTimestamp(); | |
| 62 | + } | |
| 63 | 63 | } | 
| @@ -6,18 +6,18 @@ | ||
| 6 | 6 | |
| 7 | 7 | class HybridSessionDataObject extends DataObject | 
| 8 | 8 |  { | 
| 9 | - private static $db = [ | |
| 10 | - 'SessionID' => 'Varchar(64)', | |
| 11 | - 'Expiry' => 'Int', | |
| 12 | - 'Data' => 'Text' | |
| 13 | - ]; | |
| 9 | + private static $db = [ | |
| 10 | + 'SessionID' => 'Varchar(64)', | |
| 11 | + 'Expiry' => 'Int', | |
| 12 | + 'Data' => 'Text' | |
| 13 | + ]; | |
| 14 | 14 | |
| 15 | - private static $indexes = [ | |
| 16 | - 'SessionID' => [ | |
| 17 | - 'type' => 'unique' | |
| 18 | - ], | |
| 19 | - 'Expiry' => true | |
| 20 | - ]; | |
| 15 | + private static $indexes = [ | |
| 16 | + 'SessionID' => [ | |
| 17 | + 'type' => 'unique' | |
| 18 | + ], | |
| 19 | + 'Expiry' => true | |
| 20 | + ]; | |
| 21 | 21 | |
| 22 | - private static $table_name = 'HybridSessionDataObject'; | |
| 22 | + private static $table_name = 'HybridSessionDataObject'; | |
| 23 | 23 | } | 
| @@ -5,27 +5,27 @@ | ||
| 5 | 5 | interface CryptoHandler | 
| 6 | 6 |  { | 
| 7 | 7 | |
| 8 | - /** | |
| 9 | - * @param string $data | |
| 10 | - * | |
| 11 | - * @return string | |
| 12 | - */ | |
| 13 | - public function encrypt($data); | |
| 8 | + /** | |
| 9 | + * @param string $data | |
| 10 | + * | |
| 11 | + * @return string | |
| 12 | + */ | |
| 13 | + public function encrypt($data); | |
| 14 | 14 | |
| 15 | - /** | |
| 16 | - * @param string $data | |
| 17 | - * | |
| 18 | - * @return string | |
| 19 | - */ | |
| 20 | - public function decrypt($data); | |
| 15 | + /** | |
| 16 | + * @param string $data | |
| 17 | + * | |
| 18 | + * @return string | |
| 19 | + */ | |
| 20 | + public function decrypt($data); | |
| 21 | 21 | |
| 22 | - /** | |
| 23 | - * @return string | |
| 24 | - */ | |
| 25 | - public function getKey(); | |
| 22 | + /** | |
| 23 | + * @return string | |
| 24 | + */ | |
| 25 | + public function getKey(); | |
| 26 | 26 | |
| 27 | - /** | |
| 28 | - * @return string | |
| 29 | - */ | |
| 30 | - public function getSalt(); | |
| 27 | + /** | |
| 28 | + * @return string | |
| 29 | + */ | |
| 30 | + public function getSalt(); | |
| 31 | 31 | } | 
| @@ -8,25 +8,25 @@ | ||
| 8 | 8 | |
| 9 | 9 | class HybridSessionMiddleware implements HTTPMiddleware | 
| 10 | 10 |  { | 
| 11 | - public function process(HTTPRequest $request, callable $delegate) | |
| 12 | -    { | |
| 13 | -        try { | |
| 14 | - // Start session and execute | |
| 15 | - $request->getSession()->init($request); | |
| 11 | + public function process(HTTPRequest $request, callable $delegate) | |
| 12 | +	{ | |
| 13 | +		try { | |
| 14 | + // Start session and execute | |
| 15 | + $request->getSession()->init($request); | |
| 16 | 16 | |
| 17 | - // Generate output | |
| 18 | - $response = $delegate($request); | |
| 19 | -        } finally { | |
| 20 | - // Save session data, even if there was an exception | |
| 21 | - // Note that save() will start/resume the session if required. | |
| 22 | - $request->getSession()->save($request); | |
| 17 | + // Generate output | |
| 18 | + $response = $delegate($request); | |
| 19 | +		} finally { | |
| 20 | + // Save session data, even if there was an exception | |
| 21 | + // Note that save() will start/resume the session if required. | |
| 22 | + $request->getSession()->save($request); | |
| 23 | 23 | |
| 24 | -            if (HybridSession::is_enabled()) { | |
| 25 | - // Close the session | |
| 26 | - session_write_close(); | |
| 27 | - } | |
| 28 | - } | |
| 24 | +			if (HybridSession::is_enabled()) { | |
| 25 | + // Close the session | |
| 26 | + session_write_close(); | |
| 27 | + } | |
| 28 | + } | |
| 29 | 29 | |
| 30 | - return $response; | |
| 31 | - } | |
| 30 | + return $response; | |
| 31 | + } | |
| 32 | 32 | } | 
| @@ -9,12 +9,12 @@ | ||
| 9 | 9 | |
| 10 | 10 | class ConfigurationTest extends SapphireTest | 
| 11 | 11 |  { | 
| 12 | - public function testHybridSessionsSessionMiddlewareReplacesCore() | |
| 13 | -    { | |
| 14 | - $this->assertInstanceOf( | |
| 15 | - HybridSessionMiddleware::class, | |
| 16 | - Injector::inst()->get(SessionMiddleware::class), | |
| 17 | - 'HybridSession\'s middleware should replace the default SessionMiddleware' | |
| 18 | - ); | |
| 19 | - } | |
| 12 | + public function testHybridSessionsSessionMiddlewareReplacesCore() | |
| 13 | +	{ | |
| 14 | + $this->assertInstanceOf( | |
| 15 | + HybridSessionMiddleware::class, | |
| 16 | + Injector::inst()->get(SessionMiddleware::class), | |
| 17 | + 'HybridSession\'s middleware should replace the default SessionMiddleware' | |
| 18 | + ); | |
| 19 | + } | |
| 20 | 20 | } | 
| @@ -10,104 +10,104 @@ | ||
| 10 | 10 | |
| 11 | 11 | abstract class AbstractTest extends SapphireTest | 
| 12 | 12 |  { | 
| 13 | - protected $usesDatabase = true; | |
| 14 | - | |
| 15 | - protected function setUp() | |
| 16 | -    { | |
| 17 | - parent::setUp(); | |
| 18 | - | |
| 19 | - TestCookieStore::$override_headers_sent = false; | |
| 20 | - | |
| 21 | - Injector::inst()->registerService( | |
| 22 | - new TestCookieStore(), | |
| 23 | - CookieStore::class | |
| 24 | - ); | |
| 25 | - | |
| 26 | -        DBDatetime::set_mock_now('2010-03-15 12:00:00'); | |
| 27 | - } | |
| 28 | - | |
| 29 | - protected function tearDown() | |
| 30 | -    { | |
| 31 | - DBDatetime::clear_mock_now(); | |
| 32 | - | |
| 33 | - parent::tearDown(); | |
| 34 | - } | |
| 35 | - | |
| 36 | - abstract protected function getStore(); | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * Test how this store handles large volumes of data (>1000 characters) | |
| 40 | - */ | |
| 41 | - public function testStoreLargeData() | |
| 42 | -    { | |
| 43 | - $session = uniqid(); | |
| 44 | - $store = $this->getStore(); | |
| 45 | - | |
| 46 | - // Test new session is blank | |
| 47 | - $result = $store->read($session); | |
| 48 | - $this->assertEmpty($result); | |
| 49 | - | |
| 50 | - // Save data against session | |
| 51 | - $data1 = array( | |
| 52 | -            'Large' => str_repeat('A', 600), | |
| 53 | -            'Content' => str_repeat('B', 600) | |
| 54 | - ); | |
| 55 | - $store->write($session, serialize($data1)); | |
| 56 | - $result = $store->read($session); | |
| 57 | - $this->assertEquals($data1, unserialize($result)); | |
| 58 | - } | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * Test storage of data | |
| 62 | - */ | |
| 63 | - public function testStoreData() | |
| 64 | -    { | |
| 65 | - $session = uniqid(); | |
| 66 | - $store = $this->getStore(); | |
| 67 | - | |
| 68 | - // Test new session is blank | |
| 69 | - $result = $store->read($session); | |
| 70 | - $this->assertEmpty($result); | |
| 71 | - | |
| 72 | - // Save data against session | |
| 73 | - $data1 = array( | |
| 74 | - 'Color' => 'red', | |
| 75 | - 'Animal' => 'elephant' | |
| 76 | - ); | |
| 77 | - $store->write($session, serialize($data1)); | |
| 78 | - $result = $store->read($session); | |
| 79 | - $this->assertEquals($data1, unserialize($result)); | |
| 80 | - | |
| 81 | - // Save larger data | |
| 82 | - $data2 = array( | |
| 83 | - 'Color' => 'blue', | |
| 84 | -            'Animal' => str_repeat('bat', 100) | |
| 85 | - ); | |
| 86 | - $store->write($session, serialize($data2)); | |
| 87 | - $result = $store->read($session); | |
| 88 | - $this->assertEquals($data2, unserialize($result)); | |
| 89 | - } | |
| 90 | - | |
| 91 | - /** | |
| 92 | - * Test expiry of data | |
| 93 | - */ | |
| 94 | - public function testExpiry() | |
| 95 | -    { | |
| 96 | - $session1 = uniqid(); | |
| 97 | - $store = $this->getStore(); | |
| 98 | - | |
| 99 | - // Store data now | |
| 100 | - $data1 = array( | |
| 101 | - 'Food' => 'Pizza' | |
| 102 | - ); | |
| 103 | - $store->write($session1, serialize($data1)); | |
| 104 | - $result1 = $store->read($session1); | |
| 105 | - $this->assertEquals($data1, unserialize($result1)); | |
| 106 | - | |
| 107 | - // Go to the future and test that the expiry is accurate | |
| 108 | -        DBDatetime::set_mock_now('2040-03-16 12:00:00'); | |
| 109 | - $result2 = $store->read($session1); | |
| 110 | - | |
| 111 | - $this->assertEmpty($result2); | |
| 112 | - } | |
| 13 | + protected $usesDatabase = true; | |
| 14 | + | |
| 15 | + protected function setUp() | |
| 16 | +	{ | |
| 17 | + parent::setUp(); | |
| 18 | + | |
| 19 | + TestCookieStore::$override_headers_sent = false; | |
| 20 | + | |
| 21 | + Injector::inst()->registerService( | |
| 22 | + new TestCookieStore(), | |
| 23 | + CookieStore::class | |
| 24 | + ); | |
| 25 | + | |
| 26 | +		DBDatetime::set_mock_now('2010-03-15 12:00:00'); | |
| 27 | + } | |
| 28 | + | |
| 29 | + protected function tearDown() | |
| 30 | +	{ | |
| 31 | + DBDatetime::clear_mock_now(); | |
| 32 | + | |
| 33 | + parent::tearDown(); | |
| 34 | + } | |
| 35 | + | |
| 36 | + abstract protected function getStore(); | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Test how this store handles large volumes of data (>1000 characters) | |
| 40 | + */ | |
| 41 | + public function testStoreLargeData() | |
| 42 | +	{ | |
| 43 | + $session = uniqid(); | |
| 44 | + $store = $this->getStore(); | |
| 45 | + | |
| 46 | + // Test new session is blank | |
| 47 | + $result = $store->read($session); | |
| 48 | + $this->assertEmpty($result); | |
| 49 | + | |
| 50 | + // Save data against session | |
| 51 | + $data1 = array( | |
| 52 | +			'Large' => str_repeat('A', 600), | |
| 53 | +			'Content' => str_repeat('B', 600) | |
| 54 | + ); | |
| 55 | + $store->write($session, serialize($data1)); | |
| 56 | + $result = $store->read($session); | |
| 57 | + $this->assertEquals($data1, unserialize($result)); | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Test storage of data | |
| 62 | + */ | |
| 63 | + public function testStoreData() | |
| 64 | +	{ | |
| 65 | + $session = uniqid(); | |
| 66 | + $store = $this->getStore(); | |
| 67 | + | |
| 68 | + // Test new session is blank | |
| 69 | + $result = $store->read($session); | |
| 70 | + $this->assertEmpty($result); | |
| 71 | + | |
| 72 | + // Save data against session | |
| 73 | + $data1 = array( | |
| 74 | + 'Color' => 'red', | |
| 75 | + 'Animal' => 'elephant' | |
| 76 | + ); | |
| 77 | + $store->write($session, serialize($data1)); | |
| 78 | + $result = $store->read($session); | |
| 79 | + $this->assertEquals($data1, unserialize($result)); | |
| 80 | + | |
| 81 | + // Save larger data | |
| 82 | + $data2 = array( | |
| 83 | + 'Color' => 'blue', | |
| 84 | +			'Animal' => str_repeat('bat', 100) | |
| 85 | + ); | |
| 86 | + $store->write($session, serialize($data2)); | |
| 87 | + $result = $store->read($session); | |
| 88 | + $this->assertEquals($data2, unserialize($result)); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Test expiry of data | |
| 93 | + */ | |
| 94 | + public function testExpiry() | |
| 95 | +	{ | |
| 96 | + $session1 = uniqid(); | |
| 97 | + $store = $this->getStore(); | |
| 98 | + | |
| 99 | + // Store data now | |
| 100 | + $data1 = array( | |
| 101 | + 'Food' => 'Pizza' | |
| 102 | + ); | |
| 103 | + $store->write($session1, serialize($data1)); | |
| 104 | + $result1 = $store->read($session1); | |
| 105 | + $this->assertEquals($data1, unserialize($result1)); | |
| 106 | + | |
| 107 | + // Go to the future and test that the expiry is accurate | |
| 108 | +		DBDatetime::set_mock_now('2040-03-16 12:00:00'); | |
| 109 | + $result2 = $store->read($session1); | |
| 110 | + | |
| 111 | + $this->assertEmpty($result2); | |
| 112 | + } | |
| 113 | 113 | } | 
| @@ -9,83 +9,83 @@ | ||
| 9 | 9 | |
| 10 | 10 | class HybridSessionTest extends SapphireTest | 
| 11 | 11 |  { | 
| 12 | - /** | |
| 13 | - * @var BaseStore | |
| 14 | - */ | |
| 15 | - protected $handler; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * @var HybridSession | |
| 19 | - */ | |
| 20 | - protected $instance; | |
| 21 | - | |
| 22 | - protected function setUp() | |
| 23 | -    { | |
| 24 | - parent::setUp(); | |
| 25 | - | |
| 26 | - $this->handler = $this->createMock(TestCookieStore::class); | |
| 27 | - | |
| 28 | - $this->instance = new HybridSession(); | |
| 29 | - } | |
| 30 | - | |
| 31 | - public function testSetHandlersAlsoSetsKeyToEachHandler() | |
| 32 | -    { | |
| 33 | -        $this->instance->setKey('foobar'); | |
| 34 | -        $this->handler->expects($this->once())->method('setKey')->with('foobar'); | |
| 35 | - $this->instance->setHandlers([$this->handler]); | |
| 36 | - } | |
| 37 | - | |
| 38 | - public function testOpenDelegatesToAllHandlers() | |
| 39 | -    { | |
| 40 | -        $this->handler->expects($this->once())->method('open')->with('foo', 'bar'); | |
| 41 | - $this->instance->setHandlers([$this->handler]); | |
| 42 | -        $this->assertTrue($this->instance->open('foo', 'bar'), 'Method returns true after delegation'); | |
| 43 | - } | |
| 44 | - | |
| 45 | - public function testCloseDelegatesToAllHandlers() | |
| 46 | -    { | |
| 47 | -        $this->handler->expects($this->once())->method('close'); | |
| 48 | - $this->instance->setHandlers([$this->handler]); | |
| 49 | - $this->assertTrue($this->instance->close(), 'Method returns true after delegation'); | |
| 50 | - } | |
| 51 | - | |
| 52 | - public function testReadReturnsEmptyStringWithNoHandlers() | |
| 53 | -    { | |
| 54 | -        $this->handler->expects($this->once())->method('read')->with('foosession')->willReturn(false); | |
| 55 | - $this->instance->setHandlers([$this->handler]); | |
| 56 | -        $this->assertSame('', $this->instance->read('foosession')); | |
| 57 | - } | |
| 58 | - | |
| 59 | - public function testReadReturnsHandlerDelegateResult() | |
| 60 | -    { | |
| 61 | -        $this->handler->expects($this->once())->method('read')->with('foo.session')->willReturn('success!'); | |
| 62 | - $this->instance->setHandlers([$this->handler]); | |
| 63 | -        $this->assertSame('success!', $this->instance->read('foo.session')); | |
| 64 | - } | |
| 65 | - | |
| 66 | - public function testWriteDelegatesToHandlerAndReturnsTrue() | |
| 67 | -    { | |
| 68 | -        $this->handler->expects($this->once())->method('write')->with('foo', 'bar')->willReturn(true); | |
| 69 | - $this->instance->setHandlers([$this->handler]); | |
| 70 | -        $this->assertTrue($this->instance->write('foo', 'bar')); | |
| 71 | - } | |
| 72 | - | |
| 73 | - public function testWriteReturnsFalseWithNoHandlers() | |
| 74 | -    { | |
| 75 | -        $this->assertFalse($this->instance->write('no', 'handlers')); | |
| 76 | - } | |
| 77 | - | |
| 78 | - public function testDestroyDelegatesToHandler() | |
| 79 | -    { | |
| 80 | -        $this->handler->expects($this->once())->method('destroy')->with('sessid1234'); | |
| 81 | - $this->instance->setHandlers([$this->handler]); | |
| 82 | -        $this->assertTrue($this->instance->destroy('sessid1234'), 'Method returns true after delegation'); | |
| 83 | - } | |
| 84 | - | |
| 85 | - public function testGcDelegatesToHandlers() | |
| 86 | -    { | |
| 87 | -        $this->handler->expects($this->once())->method('gc')->with(12345); | |
| 88 | - $this->instance->setHandlers([$this->handler]); | |
| 89 | - $this->instance->gc(12345); | |
| 90 | - } | |
| 12 | + /** | |
| 13 | + * @var BaseStore | |
| 14 | + */ | |
| 15 | + protected $handler; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * @var HybridSession | |
| 19 | + */ | |
| 20 | + protected $instance; | |
| 21 | + | |
| 22 | + protected function setUp() | |
| 23 | +	{ | |
| 24 | + parent::setUp(); | |
| 25 | + | |
| 26 | + $this->handler = $this->createMock(TestCookieStore::class); | |
| 27 | + | |
| 28 | + $this->instance = new HybridSession(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function testSetHandlersAlsoSetsKeyToEachHandler() | |
| 32 | +	{ | |
| 33 | +		$this->instance->setKey('foobar'); | |
| 34 | +		$this->handler->expects($this->once())->method('setKey')->with('foobar'); | |
| 35 | + $this->instance->setHandlers([$this->handler]); | |
| 36 | + } | |
| 37 | + | |
| 38 | + public function testOpenDelegatesToAllHandlers() | |
| 39 | +	{ | |
| 40 | +		$this->handler->expects($this->once())->method('open')->with('foo', 'bar'); | |
| 41 | + $this->instance->setHandlers([$this->handler]); | |
| 42 | +		$this->assertTrue($this->instance->open('foo', 'bar'), 'Method returns true after delegation'); | |
| 43 | + } | |
| 44 | + | |
| 45 | + public function testCloseDelegatesToAllHandlers() | |
| 46 | +	{ | |
| 47 | +		$this->handler->expects($this->once())->method('close'); | |
| 48 | + $this->instance->setHandlers([$this->handler]); | |
| 49 | + $this->assertTrue($this->instance->close(), 'Method returns true after delegation'); | |
| 50 | + } | |
| 51 | + | |
| 52 | + public function testReadReturnsEmptyStringWithNoHandlers() | |
| 53 | +	{ | |
| 54 | +		$this->handler->expects($this->once())->method('read')->with('foosession')->willReturn(false); | |
| 55 | + $this->instance->setHandlers([$this->handler]); | |
| 56 | +		$this->assertSame('', $this->instance->read('foosession')); | |
| 57 | + } | |
| 58 | + | |
| 59 | + public function testReadReturnsHandlerDelegateResult() | |
| 60 | +	{ | |
| 61 | +		$this->handler->expects($this->once())->method('read')->with('foo.session')->willReturn('success!'); | |
| 62 | + $this->instance->setHandlers([$this->handler]); | |
| 63 | +		$this->assertSame('success!', $this->instance->read('foo.session')); | |
| 64 | + } | |
| 65 | + | |
| 66 | + public function testWriteDelegatesToHandlerAndReturnsTrue() | |
| 67 | +	{ | |
| 68 | +		$this->handler->expects($this->once())->method('write')->with('foo', 'bar')->willReturn(true); | |
| 69 | + $this->instance->setHandlers([$this->handler]); | |
| 70 | +		$this->assertTrue($this->instance->write('foo', 'bar')); | |
| 71 | + } | |
| 72 | + | |
| 73 | + public function testWriteReturnsFalseWithNoHandlers() | |
| 74 | +	{ | |
| 75 | +		$this->assertFalse($this->instance->write('no', 'handlers')); | |
| 76 | + } | |
| 77 | + | |
| 78 | + public function testDestroyDelegatesToHandler() | |
| 79 | +	{ | |
| 80 | +		$this->handler->expects($this->once())->method('destroy')->with('sessid1234'); | |
| 81 | + $this->instance->setHandlers([$this->handler]); | |
| 82 | +		$this->assertTrue($this->instance->destroy('sessid1234'), 'Method returns true after delegation'); | |
| 83 | + } | |
| 84 | + | |
| 85 | + public function testGcDelegatesToHandlers() | |
| 86 | +	{ | |
| 87 | +		$this->handler->expects($this->once())->method('gc')->with(12345); | |
| 88 | + $this->instance->setHandlers([$this->handler]); | |
| 89 | + $this->instance->gc(12345); | |
| 90 | + } | |
| 91 | 91 | } | 
| @@ -7,21 +7,21 @@ | ||
| 7 | 7 | |
| 8 | 8 | class TestCookieStore extends CookieStore implements TestOnly | 
| 9 | 9 |  { | 
| 10 | - /** | |
| 11 | - * Override value of 'headers_sent' but only if running tests. | |
| 12 | - * | |
| 13 | - * Set to true or false, or null to not override | |
| 14 | - * | |
| 15 | - * @var string | |
| 16 | - */ | |
| 17 | - public static $override_headers_sent = null; | |
| 10 | + /** | |
| 11 | + * Override value of 'headers_sent' but only if running tests. | |
| 12 | + * | |
| 13 | + * Set to true or false, or null to not override | |
| 14 | + * | |
| 15 | + * @var string | |
| 16 | + */ | |
| 17 | + public static $override_headers_sent = null; | |
| 18 | 18 | |
| 19 | - protected function canWrite() | |
| 20 | -    { | |
| 21 | -        if (self::$override_headers_sent !== null) { | |
| 22 | - return !self::$override_headers_sent; | |
| 23 | - } | |
| 19 | + protected function canWrite() | |
| 20 | +	{ | |
| 21 | +		if (self::$override_headers_sent !== null) { | |
| 22 | + return !self::$override_headers_sent; | |
| 23 | + } | |
| 24 | 24 | |
| 25 | - parent::canWrite(); | |
| 26 | - } | |
| 25 | + parent::canWrite(); | |
| 26 | + } | |
| 27 | 27 | } |