Skip to content

Commit 292ada0

Browse files
committed
Add has_old helper
1 parent 25d1bbb commit 292ada0

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

boot/helpers.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ function old(?string $key, bool $escape = true) : mixed
164164
return $data;
165165
}
166166

167+
/**
168+
* Tells if session has old data.
169+
*
170+
* @param string|null $key null to check all data or a specific key in the
171+
* array simple format
172+
*
173+
* @see old()
174+
*
175+
* @return bool
176+
*/
177+
function has_old(string $key = null) : bool
178+
{
179+
App::session()->activate();
180+
return App::request()->getRedirectData($key) !== null;
181+
}
182+
167183
/**
168184
* Renders the AntiCSRF input.
169185
*

tests/boot/HelpersTest.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,7 @@ public function testSession() : void
9292

9393
public function testOld() : void
9494
{
95-
App::session()->activate();
96-
App::response()->redirect('/foo', [
97-
'user' => [
98-
'name' => 'John Doe',
99-
],
100-
'xss' => '<script>alert("xss")</script>',
101-
]);
102-
App::session()->stop();
95+
$this->setOldData();
10396
self::assertSame('', old('user'));
10497
self::assertSame('John Doe', old('user[name]'));
10598
self::assertSame(
@@ -112,6 +105,38 @@ public function testOld() : void
112105
);
113106
}
114107

108+
protected function setOldData() : void
109+
{
110+
App::session()->activate();
111+
App::response()->redirect('/foo', [
112+
'user' => [
113+
'name' => 'John Doe',
114+
],
115+
'xss' => '<script>alert("xss")</script>',
116+
]);
117+
App::session()->stop();
118+
}
119+
120+
public function testHasOld() : void
121+
{
122+
$this->setOldData();
123+
self::assertTrue(has_old());
124+
self::assertTrue(has_old('user'));
125+
self::assertTrue(has_old('user[name]'));
126+
self::assertTrue(has_old('xss'));
127+
self::assertFalse(has_old('foo'));
128+
self::assertFalse(has_old('bar'));
129+
self::assertFalse(has_old('bar[bazzz]'));
130+
}
131+
132+
public function testHasOldWithoutRedirectData() : void
133+
{
134+
self::assertFalse(has_old());
135+
self::assertFalse(has_old('user'));
136+
self::assertFalse(has_old('user[name]'));
137+
self::assertFalse(has_old('bar[bazzz]'));
138+
}
139+
115140
public function testRedirect() : void
116141
{
117142
self::assertNull(App::response()->getHeader('Location'));

0 commit comments

Comments
 (0)