@@ -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 | } |
@@ -9,163 +9,163 @@ |
||
9 | 9 | class HybridSession extends BaseStore |
10 | 10 | { |
11 | 11 | |
12 | - /** |
|
13 | - * List of session handlers |
|
14 | - * |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $handlers = []; |
|
18 | - |
|
19 | - /** |
|
20 | - * True if this session store has been initialised |
|
21 | - * |
|
22 | - * @var bool |
|
23 | - */ |
|
24 | - protected static $enabled = false; |
|
25 | - |
|
26 | - /** |
|
27 | - * @param SessionHandlerInterface[] |
|
28 | - * |
|
29 | - * @return $this |
|
30 | - */ |
|
31 | - public function setHandlers($handlers) |
|
32 | - { |
|
33 | - $this->handlers = $handlers; |
|
34 | - $this->setKey($this->getKey()); |
|
35 | - |
|
36 | - return $this; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @param string |
|
41 | - * |
|
42 | - * @return $this |
|
43 | - */ |
|
44 | - public function setKey($key) |
|
45 | - { |
|
46 | - parent::setKey($key); |
|
47 | - |
|
48 | - foreach ($this->handlers as $handler) { |
|
49 | - $handler->setKey($key); |
|
50 | - } |
|
51 | - |
|
52 | - return $this; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @return SessionHandlerInterface[] |
|
57 | - */ |
|
58 | - public function getHandlers() |
|
59 | - { |
|
60 | - return $this->handlers; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @param string $save_path |
|
65 | - * @param string $name |
|
66 | - * |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function open($save_path, $name) |
|
70 | - { |
|
71 | - if ($this->handlers) { |
|
72 | - foreach ($this->handlers as $handler) { |
|
73 | - $handler->open($save_path, $name); |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - return true; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @return bool |
|
82 | - */ |
|
83 | - public function close() |
|
84 | - { |
|
85 | - if ($this->handlers) { |
|
86 | - foreach ($this->handlers as $handler) { |
|
87 | - $handler->close(); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - return true; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $session_id |
|
96 | - * |
|
97 | - * @return string |
|
98 | - */ |
|
99 | - public function read($session_id) |
|
100 | - { |
|
101 | - if ($this->handlers) { |
|
102 | - foreach ($this->handlers as $handler) { |
|
103 | - if ($data = $handler->read($session_id)) { |
|
104 | - return $data; |
|
105 | - } |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - return ''; |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - public function write($session_id, $session_data) |
|
114 | - { |
|
115 | - if ($this->handlers) { |
|
116 | - foreach ($this->handlers as $handler) { |
|
117 | - if ($handler->write($session_id, $session_data)) { |
|
118 | - return true; |
|
119 | - } |
|
120 | - } |
|
121 | - } |
|
122 | - return false; |
|
123 | - } |
|
124 | - |
|
125 | - public function destroy($session_id) |
|
126 | - { |
|
127 | - if ($this->handlers) { |
|
128 | - foreach ($this->handlers as $handler) { |
|
129 | - $handler->destroy($session_id); |
|
130 | - } |
|
131 | - } |
|
132 | - return true; |
|
133 | - } |
|
134 | - |
|
135 | - public function gc($maxlifetime) |
|
136 | - { |
|
137 | - if ($this->handlers) { |
|
138 | - foreach ($this->handlers as $handler) { |
|
139 | - $handler->gc($maxlifetime); |
|
140 | - } |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Register the session handler as the default |
|
146 | - * |
|
147 | - * @param string $key Desired session key |
|
148 | - */ |
|
149 | - public static function init($key = null) |
|
150 | - { |
|
151 | - $instance = Injector::inst()->get(__CLASS__); |
|
152 | - |
|
153 | - if (empty($key)) { |
|
154 | - user_error( |
|
155 | - 'HybridSession::init() was not given a $key. Disabling cookie-based storage', |
|
156 | - E_USER_WARNING |
|
157 | - ); |
|
158 | - } else { |
|
159 | - $instance->setKey($key); |
|
160 | - } |
|
161 | - |
|
162 | - session_set_save_handler($instance, true); |
|
163 | - |
|
164 | - self::$enabled = true; |
|
165 | - } |
|
166 | - |
|
167 | - public static function is_enabled() |
|
168 | - { |
|
169 | - return self::$enabled; |
|
170 | - } |
|
12 | + /** |
|
13 | + * List of session handlers |
|
14 | + * |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $handlers = []; |
|
18 | + |
|
19 | + /** |
|
20 | + * True if this session store has been initialised |
|
21 | + * |
|
22 | + * @var bool |
|
23 | + */ |
|
24 | + protected static $enabled = false; |
|
25 | + |
|
26 | + /** |
|
27 | + * @param SessionHandlerInterface[] |
|
28 | + * |
|
29 | + * @return $this |
|
30 | + */ |
|
31 | + public function setHandlers($handlers) |
|
32 | + { |
|
33 | + $this->handlers = $handlers; |
|
34 | + $this->setKey($this->getKey()); |
|
35 | + |
|
36 | + return $this; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @param string |
|
41 | + * |
|
42 | + * @return $this |
|
43 | + */ |
|
44 | + public function setKey($key) |
|
45 | + { |
|
46 | + parent::setKey($key); |
|
47 | + |
|
48 | + foreach ($this->handlers as $handler) { |
|
49 | + $handler->setKey($key); |
|
50 | + } |
|
51 | + |
|
52 | + return $this; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @return SessionHandlerInterface[] |
|
57 | + */ |
|
58 | + public function getHandlers() |
|
59 | + { |
|
60 | + return $this->handlers; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @param string $save_path |
|
65 | + * @param string $name |
|
66 | + * |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function open($save_path, $name) |
|
70 | + { |
|
71 | + if ($this->handlers) { |
|
72 | + foreach ($this->handlers as $handler) { |
|
73 | + $handler->open($save_path, $name); |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + return true; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @return bool |
|
82 | + */ |
|
83 | + public function close() |
|
84 | + { |
|
85 | + if ($this->handlers) { |
|
86 | + foreach ($this->handlers as $handler) { |
|
87 | + $handler->close(); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + return true; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $session_id |
|
96 | + * |
|
97 | + * @return string |
|
98 | + */ |
|
99 | + public function read($session_id) |
|
100 | + { |
|
101 | + if ($this->handlers) { |
|
102 | + foreach ($this->handlers as $handler) { |
|
103 | + if ($data = $handler->read($session_id)) { |
|
104 | + return $data; |
|
105 | + } |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + return ''; |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + public function write($session_id, $session_data) |
|
114 | + { |
|
115 | + if ($this->handlers) { |
|
116 | + foreach ($this->handlers as $handler) { |
|
117 | + if ($handler->write($session_id, $session_data)) { |
|
118 | + return true; |
|
119 | + } |
|
120 | + } |
|
121 | + } |
|
122 | + return false; |
|
123 | + } |
|
124 | + |
|
125 | + public function destroy($session_id) |
|
126 | + { |
|
127 | + if ($this->handlers) { |
|
128 | + foreach ($this->handlers as $handler) { |
|
129 | + $handler->destroy($session_id); |
|
130 | + } |
|
131 | + } |
|
132 | + return true; |
|
133 | + } |
|
134 | + |
|
135 | + public function gc($maxlifetime) |
|
136 | + { |
|
137 | + if ($this->handlers) { |
|
138 | + foreach ($this->handlers as $handler) { |
|
139 | + $handler->gc($maxlifetime); |
|
140 | + } |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Register the session handler as the default |
|
146 | + * |
|
147 | + * @param string $key Desired session key |
|
148 | + */ |
|
149 | + public static function init($key = null) |
|
150 | + { |
|
151 | + $instance = Injector::inst()->get(__CLASS__); |
|
152 | + |
|
153 | + if (empty($key)) { |
|
154 | + user_error( |
|
155 | + 'HybridSession::init() was not given a $key. Disabling cookie-based storage', |
|
156 | + E_USER_WARNING |
|
157 | + ); |
|
158 | + } else { |
|
159 | + $instance->setKey($key); |
|
160 | + } |
|
161 | + |
|
162 | + session_set_save_handler($instance, true); |
|
163 | + |
|
164 | + self::$enabled = true; |
|
165 | + } |
|
166 | + |
|
167 | + public static function is_enabled() |
|
168 | + { |
|
169 | + return self::$enabled; |
|
170 | + } |
|
171 | 171 | } |
@@ -12,29 +12,29 @@ |
||
12 | 12 | class HybridSessionTest extends AbstractTest |
13 | 13 | { |
14 | 14 | |
15 | - protected function setUp() |
|
16 | - { |
|
17 | - parent::setUp(); |
|
15 | + protected function setUp() |
|
16 | + { |
|
17 | + parent::setUp(); |
|
18 | 18 | |
19 | - if (!DB::get_conn() instanceof MySQLDatabase) { |
|
20 | - // we can't always use the DB driver, so remove it if we aren't running a MySQL DB |
|
21 | - Config::modify()->set(HybridSession::class, 'dependencies', [ |
|
22 | - 'handlers' => [ |
|
23 | - '%$\SilverStripe\HybridSessions\Store\CookieStore', |
|
24 | - ], |
|
25 | - ]); |
|
26 | - } |
|
27 | - } |
|
19 | + if (!DB::get_conn() instanceof MySQLDatabase) { |
|
20 | + // we can't always use the DB driver, so remove it if we aren't running a MySQL DB |
|
21 | + Config::modify()->set(HybridSession::class, 'dependencies', [ |
|
22 | + 'handlers' => [ |
|
23 | + '%$\SilverStripe\HybridSessions\Store\CookieStore', |
|
24 | + ], |
|
25 | + ]); |
|
26 | + } |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @return HybridSessionStore_Cookie |
|
31 | - */ |
|
32 | - protected function getStore() |
|
33 | - { |
|
34 | - $store = Injector::inst()->create(HybridSession::class); |
|
35 | - $store->setKey(uniqid()); |
|
36 | - $store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE'); |
|
29 | + /** |
|
30 | + * @return HybridSessionStore_Cookie |
|
31 | + */ |
|
32 | + protected function getStore() |
|
33 | + { |
|
34 | + $store = Injector::inst()->create(HybridSession::class); |
|
35 | + $store->setKey(uniqid()); |
|
36 | + $store->open(TempFolder::getTempFolder(BASE_PATH).'/'.__CLASS__, 'SESSIONCOOKIE'); |
|
37 | 37 | |
38 | - return $store; |
|
39 | - } |
|
38 | + return $store; |
|
39 | + } |
|
40 | 40 | } |
@@ -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,20 +9,20 @@ |
||
9 | 9 | |
10 | 10 | class DatabaseStoreTest extends AbstractTest |
11 | 11 | { |
12 | - protected function setUp() |
|
13 | - { |
|
14 | - parent::setUp(); |
|
12 | + protected function setUp() |
|
13 | + { |
|
14 | + parent::setUp(); |
|
15 | 15 | |
16 | - if (!DB::get_conn() instanceof MySQLDatabase) { |
|
17 | - $this->markTestSkipped('Only MySQL databases are supported'); |
|
18 | - } |
|
19 | - } |
|
16 | + if (!DB::get_conn() instanceof MySQLDatabase) { |
|
17 | + $this->markTestSkipped('Only MySQL databases are supported'); |
|
18 | + } |
|
19 | + } |
|
20 | 20 | |
21 | - protected function getStore() |
|
22 | - { |
|
23 | - $store = Injector::inst()->get(DatabaseStore::class); |
|
24 | - $store->setKey(uniqid()); |
|
21 | + protected function getStore() |
|
22 | + { |
|
23 | + $store = Injector::inst()->get(DatabaseStore::class); |
|
24 | + $store->setKey(uniqid()); |
|
25 | 25 | |
26 | - return $store; |
|
27 | - } |
|
26 | + return $store; |
|
27 | + } |
|
28 | 28 | } |