-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathNoCacheSessionTest.php
259 lines (204 loc) · 7.85 KB
/
NoCacheSessionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
namespace Tests\StaticCaching;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\StaticCaching\NoCache\Session;
use Statamic\StaticCaching\NoCache\StringRegion;
use Tests\FakesContent;
use Tests\FakesViews;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class NoCacheSessionTest extends TestCase
{
use FakesContent;
use FakesViews;
use PreventSavingStacheItemsToDisk;
#[Test]
public function when_pushing_a_region_it_will_filter_out_cascade()
{
$session = new Session('/');
$session->setCascade([
'csrf_token' => 'abc',
'now' => 'carbon',
'title' => 'base title',
]);
$region = $session->pushRegion('', [
'csrf_token' => 'abc',
'now' => 'carbon',
'title' => 'different title',
'foo' => 'bar',
'baz' => 'qux',
], '');
$this->assertEquals([
'title' => 'different title',
'foo' => 'bar',
'baz' => 'qux',
], $region->context());
}
#[Test]
public function it_gets_the_fragment_data()
{
// fragment data should be the context,
// with the cascade merged in.
$session = new Session('/');
$region = $session->pushRegion('', [
'foo' => 'bar',
'baz' => 'qux',
'title' => 'local title',
], '');
$session->setCascade([
'csrf_token' => 'abc',
'now' => 'carbon',
'title' => 'root title',
]);
$this->assertEquals([
'csrf_token' => 'abc',
'now' => 'carbon',
'foo' => 'bar',
'baz' => 'qux',
'title' => 'local title',
], $region->fragmentData());
}
#[Test]
public function it_writes()
{
Carbon::setTestNow('2014-02-15');
// Testing that the cache key used is unique to the url.
// The contents aren't really important.
Cache::shouldReceive('store')
->andReturnSelf();
Cache::shouldReceive('forever')
->with('nocache::session.'.md5('/'), Mockery::any())
->once();
Cache::shouldReceive('forever')
->with('nocache::session.'.md5('/foo'), Mockery::any())
->once();
// ...and that the urls are tracked in the cache.
Cache::shouldReceive('get')
->with('nocache::urls', [])
->times(2)
->andReturn([], ['/']);
Cache::shouldReceive('forever')
->with('nocache::urls', ['/'])
->once();
Cache::shouldReceive('forever')
->with('nocache::urls', ['/', '/foo'])
->once();
// When pushing regions, they will get written too...
Cache::shouldReceive('forever')
->withArgs(fn ($arg) => str_starts_with($arg, 'nocache::region.'))
->twice();
tap(new Session('/'), function ($session) {
$session->pushRegion('test', [], '.html');
})->write();
tap(new Session('/foo'), function ($session) {
$session->pushRegion('test', [], '.html');
})->write();
}
#[Test]
public function it_restores_from_cache()
{
Cache::forever('nocache::region.abc', $regionOne = Mockery::mock(StringRegion::class));
Cache::forever('nocache::region.def', $regionTwo = Mockery::mock(StringRegion::class));
Cache::forever('nocache::session.'.md5('http://localhost/test'), [
'regions' => ['abc', 'def'],
]);
$this->createPage('/test', [
'with' => ['title' => 'Test page'],
]);
$session = new Session('http://localhost/test');
$this->assertEquals([], $session->regions()->all());
$this->assertEquals([], $session->cascade());
$session->restore();
$this->assertEquals(['abc' => $regionOne, 'def' => $regionTwo], $session->regions()->all());
$this->assertNotEquals([], $cascade = $session->cascade());
$this->assertEquals('/test', $cascade['url']);
$this->assertEquals('Test page', $cascade['title']);
$this->assertEquals('http://localhost/cp', $cascade['cp_url']);
}
#[Test]
public function a_singleton_is_bound_in_the_container()
{
$this->get('/test?foo=bar&bar=baz');
$session = $this->app->make(Session::class);
$this->assertInstanceOf(Session::class, $session);
$this->assertEquals('http://localhost/test?bar=baz&foo=bar', $session->url());
}
#[Test]
public function it_ignores_the_query_string()
{
config(['statamic.static_caching.ignore_query_strings' => true]);
$this->get('/test?foo=bar');
$session = $this->app->make(Session::class);
$this->assertInstanceOf(Session::class, $session);
$this->assertEquals('http://localhost/test', $session->url());
}
#[Test]
public function it_normalizes_query_string()
{
$this->get('/test?utm_source=linkedin.com&utm_medium=referral&utm_campaign=foo');
$session = $this->app->make(Session::class);
$this->assertInstanceOf(Session::class, $session);
$this->assertEquals('http://localhost/test?utm_campaign=foo&utm_medium=referral&utm_source=linkedin.com', $session->url());
}
#[Test]
public function it_writes_session_if_a_nocache_tag_is_used()
{
$this->withStandardFakeViews();
$this->viewShouldReturnRaw('default', '{{ title }} {{ nocache }}{{ title }}{{ /nocache }}');
$this->createPage('test', ['with' => ['title' => 'Test']]);
$this->assertNull(Cache::get('nocache::session.'.md5('http://localhost/test')));
$this
->get('/test')
->assertOk()
->assertSee('Test Test');
$this->assertNotNull(Cache::get('nocache::session.'.md5('http://localhost/test')));
}
#[Test]
public function it_doesnt_write_session_if_a_nocache_tag_is_not_used()
{
$this->withStandardFakeViews();
$this->viewShouldReturnRaw('default', '{{ title }}');
$this->createPage('test', ['with' => ['title' => 'Test']]);
$this->assertNull(Cache::get('nocache::session.'.md5('http://localhost/test')));
$this
->get('/test')
->assertOk()
->assertSee('Test');
$this->assertNull(Cache::get('nocache::session.'.md5('http://localhost/test')));
}
#[Test]
public function it_restores_session_if_theres_a_nocache_placeholder_in_the_response()
{
$this->withStandardFakeViews();
$this->viewShouldReturnRendered('default', 'Hello <span class="nocache" data-nocache="abc">NOCACHE_PLACEHOLDER</span>');
$this->createPage('test');
Cache::forever('nocache::region.abc', $region = Mockery::mock(StringRegion::class));
Cache::put('nocache::session.'.md5('http://localhost/test'), [
'regions' => ['abc'],
]);
$region->shouldReceive('render')->andReturn('world');
$this
->get('/test')
->assertOk()
->assertSee('Hello world');
$this->assertEquals(['abc' => $region], app(Session::class)->regions()->all());
}
#[Test]
public function it_doesnt_restore_session_if_there_is_no_nocache_placeholder_in_the_response()
{
$this->withStandardFakeViews();
$this->viewShouldReturnRendered('default', 'Hello');
$this->createPage('test');
Cache::put('nocache::session.'.md5('http://localhost/test'), [
'regions' => ['abc' => ['type' => 'string', 'contents' => 'world', 'extension' => 'html', 'context' => ['foo' => 'bar']]],
]);
$this
->get('/test')
->assertOk()
->assertSee('Hello');
$this->assertEquals([], app(Session::class)->regions()->all());
}
}