1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\HybridSessions\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
6
|
|
|
use SilverStripe\Core\TempFolder; |
7
|
|
|
use SilverStripe\HybridSessions\Store\CookieStore; |
8
|
|
|
use SilverStripe\HybridSessions\Tests\Store\TestCookieStore; |
9
|
|
|
|
10
|
|
|
class CookieStoreTest extends AbstractTest |
11
|
|
|
{ |
12
|
|
|
protected function getStore() |
13
|
|
|
{ |
14
|
|
|
$store = Injector::inst()->get(CookieStore::class); |
15
|
|
|
$store->setKey(uniqid()); |
16
|
|
|
$store->open(TempFolder::getTempFolder(BASE_PATH) . '/' . __CLASS__, 'SESSIONCOOKIE'); |
17
|
|
|
|
18
|
|
|
return $store; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
View Code Duplication |
public function testStoreLargeData() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$session = uniqid(); |
24
|
|
|
$store = $this->getStore(); |
25
|
|
|
|
26
|
|
|
// Test new session is blank |
27
|
|
|
$result = $store->read($session); |
28
|
|
|
$this->assertEmpty($result); |
29
|
|
|
|
30
|
|
|
// Save data against session |
31
|
|
|
$data1 = array( |
32
|
|
|
'Large' => str_repeat('A', 600), |
33
|
|
|
'Content' => str_repeat('B', 600) |
34
|
|
|
); |
35
|
|
|
$store->write($session, serialize($data1)); |
36
|
|
|
$result = $store->read($session); |
37
|
|
|
|
38
|
|
|
// Cookies should not try to store data that large |
39
|
|
|
$this->assertEmpty($result); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Ensure that subsequent reads without the necessary write do not report data |
44
|
|
|
*/ |
45
|
|
|
public function testReadInvalidatesData() |
46
|
|
|
{ |
47
|
|
|
$session = uniqid(); |
48
|
|
|
$store = $this->getStore(); |
49
|
|
|
|
50
|
|
|
// Test new session is blank |
51
|
|
|
$result = $store->read($session); |
52
|
|
|
$this->assertEmpty($result); |
53
|
|
|
|
54
|
|
|
// Save data against session |
55
|
|
|
$data1 = array( |
56
|
|
|
'Color' => 'red', |
57
|
|
|
'Animal' => 'elephant' |
58
|
|
|
); |
59
|
|
|
$store->write($session, serialize($data1)); |
60
|
|
|
$result = $store->read($session); |
61
|
|
|
$this->assertEquals($data1, unserialize($result)); |
62
|
|
|
|
63
|
|
|
// Since we have read the data into the result, the application could modify this content |
64
|
|
|
// and be unable to write it back due to headers being sent. We should thus assume |
65
|
|
|
// that subsequent reads without a successful write do not purport to have valid data |
66
|
|
|
$data1['Color'] = 'blue'; |
67
|
|
|
$result = $store->read($session); |
68
|
|
|
$this->assertEmpty($result); |
69
|
|
|
|
70
|
|
|
// Check that writing to cookie fails after headers are sent and these results remain |
71
|
|
|
// invalidated |
72
|
|
|
TestCookieStore::$override_headers_sent = true; |
|
|
|
|
73
|
|
|
$store->write($session, serialize($data1)); |
74
|
|
|
$result = $store->read($session); |
75
|
|
|
$this->assertEmpty($result); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.