123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- <?php
- namespace Illuminate\Tests\Session;
- use Illuminate\Cookie\CookieJar;
- use Illuminate\Session\CookieSessionHandler;
- use Illuminate\Session\Store;
- use Illuminate\Support\Str;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use ReflectionClass;
- use SessionHandlerInterface;
- use Symfony\Component\HttpFoundation\Request;
- class SessionStoreTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testSessionIsLoadedFromHandler()
- {
- $session = $this->getSession();
- $session->getHandler()->shouldReceive('read')->once()->with($this->getSessionId())->andReturn(serialize(['foo' => 'bar', 'bagged' => ['name' => 'taylor']]));
- $session->start();
- $this->assertSame('bar', $session->get('foo'));
- $this->assertSame('baz', $session->get('bar', 'baz'));
- $this->assertTrue($session->has('foo'));
- $this->assertFalse($session->has('bar'));
- $this->assertTrue($session->isStarted());
- $session->put('baz', 'boom');
- $this->assertTrue($session->has('baz'));
- }
- public function testSessionMigration()
- {
- $session = $this->getSession();
- $oldId = $session->getId();
- $session->getHandler()->shouldReceive('destroy')->never();
- $this->assertTrue($session->migrate());
- $this->assertNotEquals($oldId, $session->getId());
- $session = $this->getSession();
- $oldId = $session->getId();
- $session->getHandler()->shouldReceive('destroy')->once()->with($oldId);
- $this->assertTrue($session->migrate(true));
- $this->assertNotEquals($oldId, $session->getId());
- }
- public function testSessionRegeneration()
- {
- $session = $this->getSession();
- $oldId = $session->getId();
- $session->getHandler()->shouldReceive('destroy')->never();
- $this->assertTrue($session->regenerate());
- $this->assertNotEquals($oldId, $session->getId());
- }
- public function testCantSetInvalidId()
- {
- $session = $this->getSession();
- $this->assertTrue($session->isValidId($session->getId()));
- $session->setId(null);
- $this->assertNotNull($session->getId());
- $this->assertTrue($session->isValidId($session->getId()));
- $session->setId(['a']);
- $this->assertNotSame(['a'], $session->getId());
- $session->setId('wrong');
- $this->assertNotSame('wrong', $session->getId());
- }
- public function testSessionInvalidate()
- {
- $session = $this->getSession();
- $oldId = $session->getId();
- $session->put('foo', 'bar');
- $this->assertGreaterThan(0, count($session->all()));
- $session->flash('name', 'Taylor');
- $this->assertTrue($session->has('name'));
- $session->getHandler()->shouldReceive('destroy')->once()->with($oldId);
- $this->assertTrue($session->invalidate());
- $this->assertFalse($session->has('name'));
- $this->assertNotEquals($oldId, $session->getId());
- $this->assertCount(0, $session->all());
- }
- public function testBrandNewSessionIsProperlySaved()
- {
- $session = $this->getSession();
- $session->getHandler()->shouldReceive('read')->once()->andReturn(serialize([]));
- $session->start();
- $session->put('foo', 'bar');
- $session->flash('baz', 'boom');
- $session->now('qux', 'norf');
- $session->getHandler()->shouldReceive('write')->once()->with(
- $this->getSessionId(),
- serialize([
- '_token' => $session->token(),
- 'foo' => 'bar',
- 'baz' => 'boom',
- '_flash' => [
- 'new' => [],
- 'old' => ['baz'],
- ],
- ])
- );
- $session->save();
- $this->assertFalse($session->isStarted());
- }
- public function testSessionIsProperlyUpdated()
- {
- $session = $this->getSession();
- $session->getHandler()->shouldReceive('read')->once()->andReturn(serialize([
- '_token' => Str::random(40),
- 'foo' => 'bar',
- 'baz' => 'boom',
- '_flash' => [
- 'new' => [],
- 'old' => ['baz'],
- ],
- ]));
- $session->start();
- $session->getHandler()->shouldReceive('write')->once()->with(
- $this->getSessionId(),
- serialize([
- '_token' => $session->token(),
- 'foo' => 'bar',
- '_flash' => [
- 'new' => [],
- 'old' => [],
- ],
- ])
- );
- $session->save();
- $this->assertFalse($session->isStarted());
- }
- public function testSessionIsReSavedWhenNothingHasChanged()
- {
- $session = $this->getSession();
- $session->getHandler()->shouldReceive('read')->once()->andReturn(serialize([
- '_token' => Str::random(40),
- 'foo' => 'bar',
- 'baz' => 'boom',
- '_flash' => [
- 'new' => [],
- 'old' => [],
- ],
- ]));
- $session->start();
- $session->getHandler()->shouldReceive('write')->once()->with(
- $this->getSessionId(),
- serialize([
- '_token' => $session->token(),
- 'foo' => 'bar',
- 'baz' => 'boom',
- '_flash' => [
- 'new' => [],
- 'old' => [],
- ],
- ])
- );
- $session->save();
- $this->assertFalse($session->isStarted());
- }
- public function testSessionIsReSavedWhenNothingHasChangedExceptSessionId()
- {
- $session = $this->getSession();
- $oldId = $session->getId();
- $token = Str::random(40);
- $session->getHandler()->shouldReceive('read')->once()->with($oldId)->andReturn(serialize([
- '_token' => $token,
- 'foo' => 'bar',
- 'baz' => 'boom',
- '_flash' => [
- 'new' => [],
- 'old' => [],
- ],
- ]));
- $session->start();
- $oldId = $session->getId();
- $session->migrate();
- $newId = $session->getId();
- $this->assertNotEquals($newId, $oldId);
- $session->getHandler()->shouldReceive('write')->once()->with(
- $newId,
- serialize([
- '_token' => $token,
- 'foo' => 'bar',
- 'baz' => 'boom',
- '_flash' => [
- 'new' => [],
- 'old' => [],
- ],
- ])
- );
- $session->save();
- $this->assertFalse($session->isStarted());
- }
- public function testOldInputFlashing()
- {
- $session = $this->getSession();
- $session->put('boom', 'baz');
- $session->flashInput(['foo' => 'bar', 'bar' => 0]);
- $this->assertTrue($session->hasOldInput('foo'));
- $this->assertSame('bar', $session->getOldInput('foo'));
- $this->assertEquals(0, $session->getOldInput('bar'));
- $this->assertFalse($session->hasOldInput('boom'));
- $session->ageFlashData();
- $this->assertTrue($session->hasOldInput('foo'));
- $this->assertSame('bar', $session->getOldInput('foo'));
- $this->assertEquals(0, $session->getOldInput('bar'));
- $this->assertFalse($session->hasOldInput('boom'));
- }
- public function testDataFlashing()
- {
- $session = $this->getSession();
- $session->flash('foo', 'bar');
- $session->flash('bar', 0);
- $session->flash('baz');
- $this->assertTrue($session->has('foo'));
- $this->assertSame('bar', $session->get('foo'));
- $this->assertEquals(0, $session->get('bar'));
- $this->assertTrue($session->get('baz'));
- $session->ageFlashData();
- $this->assertTrue($session->has('foo'));
- $this->assertSame('bar', $session->get('foo'));
- $this->assertEquals(0, $session->get('bar'));
- $session->ageFlashData();
- $this->assertFalse($session->has('foo'));
- $this->assertNull($session->get('foo'));
- }
- public function testDataFlashingNow()
- {
- $session = $this->getSession();
- $session->now('foo', 'bar');
- $session->now('bar', 0);
- $this->assertTrue($session->has('foo'));
- $this->assertSame('bar', $session->get('foo'));
- $this->assertEquals(0, $session->get('bar'));
- $session->ageFlashData();
- $this->assertFalse($session->has('foo'));
- $this->assertNull($session->get('foo'));
- }
- public function testDataMergeNewFlashes()
- {
- $session = $this->getSession();
- $session->flash('foo', 'bar');
- $session->put('fu', 'baz');
- $session->put('_flash.old', ['qu']);
- $this->assertNotFalse(array_search('foo', $session->get('_flash.new')));
- $this->assertFalse(array_search('fu', $session->get('_flash.new')));
- $session->keep(['fu', 'qu']);
- $this->assertNotFalse(array_search('foo', $session->get('_flash.new')));
- $this->assertNotFalse(array_search('fu', $session->get('_flash.new')));
- $this->assertNotFalse(array_search('qu', $session->get('_flash.new')));
- $this->assertFalse(array_search('qu', $session->get('_flash.old')));
- }
- public function testReflash()
- {
- $session = $this->getSession();
- $session->flash('foo', 'bar');
- $session->put('_flash.old', ['foo']);
- $session->reflash();
- $this->assertNotFalse(array_search('foo', $session->get('_flash.new')));
- $this->assertFalse(array_search('foo', $session->get('_flash.old')));
- }
- public function testReflashWithNow()
- {
- $session = $this->getSession();
- $session->now('foo', 'bar');
- $session->reflash();
- $this->assertNotFalse(array_search('foo', $session->get('_flash.new')));
- $this->assertFalse(array_search('foo', $session->get('_flash.old')));
- }
- public function testOnly()
- {
- $session = $this->getSession();
- $session->put('foo', 'bar');
- $session->put('qu', 'ux');
- $this->assertEquals(['foo' => 'bar', 'qu' => 'ux'], $session->all());
- $this->assertEquals(['qu' => 'ux'], $session->only(['qu']));
- }
- public function testReplace()
- {
- $session = $this->getSession();
- $session->put('foo', 'bar');
- $session->put('qu', 'ux');
- $session->replace(['foo' => 'baz']);
- $this->assertSame('baz', $session->get('foo'));
- $this->assertSame('ux', $session->get('qu'));
- }
- public function testRemove()
- {
- $session = $this->getSession();
- $session->put('foo', 'bar');
- $pulled = $session->remove('foo');
- $this->assertFalse($session->has('foo'));
- $this->assertSame('bar', $pulled);
- }
- public function testClear()
- {
- $session = $this->getSession();
- $session->put('foo', 'bar');
- $session->flush();
- $this->assertFalse($session->has('foo'));
- $session->put('foo', 'bar');
- $session->flush();
- $this->assertFalse($session->has('foo'));
- }
- public function testIncrement()
- {
- $session = $this->getSession();
- $session->put('foo', 5);
- $foo = $session->increment('foo');
- $this->assertEquals(6, $foo);
- $this->assertEquals(6, $session->get('foo'));
- $foo = $session->increment('foo', 4);
- $this->assertEquals(10, $foo);
- $this->assertEquals(10, $session->get('foo'));
- $session->increment('bar');
- $this->assertEquals(1, $session->get('bar'));
- }
- public function testDecrement()
- {
- $session = $this->getSession();
- $session->put('foo', 5);
- $foo = $session->decrement('foo');
- $this->assertEquals(4, $foo);
- $this->assertEquals(4, $session->get('foo'));
- $foo = $session->decrement('foo', 4);
- $this->assertEquals(0, $foo);
- $this->assertEquals(0, $session->get('foo'));
- $session->decrement('bar');
- $this->assertEquals(-1, $session->get('bar'));
- }
- public function testHasOldInputWithoutKey()
- {
- $session = $this->getSession();
- $session->flash('boom', 'baz');
- $this->assertFalse($session->hasOldInput());
- $session->flashInput(['foo' => 'bar']);
- $this->assertTrue($session->hasOldInput());
- }
- public function testHandlerNeedsRequest()
- {
- $session = $this->getSession();
- $this->assertFalse($session->handlerNeedsRequest());
- $session->getHandler()->shouldReceive('setRequest')->never();
- $session = new Store('test', m::mock(new CookieSessionHandler(new CookieJar, 60)));
- $this->assertTrue($session->handlerNeedsRequest());
- $session->getHandler()->shouldReceive('setRequest')->once();
- $request = new Request;
- $session->setRequestOnHandler($request);
- }
- public function testToken()
- {
- $session = $this->getSession();
- $this->assertEquals($session->token(), $session->token());
- }
- public function testRegenerateToken()
- {
- $session = $this->getSession();
- $token = $session->token();
- $session->regenerateToken();
- $this->assertNotEquals($token, $session->token());
- }
- public function testName()
- {
- $session = $this->getSession();
- $this->assertEquals($session->getName(), $this->getSessionName());
- $session->setName('foo');
- $this->assertSame('foo', $session->getName());
- }
- public function testKeyExists()
- {
- $session = $this->getSession();
- $session->put('foo', 'bar');
- $this->assertTrue($session->exists('foo'));
- $session->put('baz', null);
- $session->put('hulk', ['one' => true]);
- $this->assertFalse($session->has('baz'));
- $this->assertTrue($session->exists('baz'));
- $this->assertFalse($session->exists('bogus'));
- $this->assertTrue($session->exists(['foo', 'baz']));
- $this->assertFalse($session->exists(['foo', 'baz', 'bogus']));
- $this->assertTrue($session->exists(['hulk.one']));
- $this->assertFalse($session->exists(['hulk.two']));
- }
- public function testKeyMissing()
- {
- $session = $this->getSession();
- $session->put('foo', 'bar');
- $this->assertFalse($session->missing('foo'));
- $session->put('baz', null);
- $session->put('hulk', ['one' => true]);
- $this->assertFalse($session->has('baz'));
- $this->assertFalse($session->missing('baz'));
- $this->assertTrue($session->missing('bogus'));
- $this->assertFalse($session->missing(['foo', 'baz']));
- $this->assertTrue($session->missing(['foo', 'baz', 'bogus']));
- $this->assertFalse($session->missing(['hulk.one']));
- $this->assertTrue($session->missing(['hulk.two']));
- }
- public function testRememberMethodCallsPutAndReturnsDefault()
- {
- $session = $this->getSession();
- $session->getHandler()->shouldReceive('get')->andReturn(null);
- $result = $session->remember('foo', function () {
- return 'bar';
- });
- $this->assertSame('bar', $session->get('foo'));
- $this->assertSame('bar', $result);
- }
- public function getSession()
- {
- $reflection = new ReflectionClass(Store::class);
- return $reflection->newInstanceArgs($this->getMocks());
- }
- public function getMocks()
- {
- return [
- $this->getSessionName(),
- m::mock(SessionHandlerInterface::class),
- $this->getSessionId(),
- ];
- }
- public function getSessionId()
- {
- return 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
- }
- public function getSessionName()
- {
- return 'name';
- }
- }
|