CookieTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Illuminate\Tests\Cookie;
  3. use Illuminate\Cookie\CookieJar;
  4. use PHPUnit\Framework\TestCase;
  5. use ReflectionObject;
  6. use Symfony\Component\HttpFoundation\Cookie;
  7. class CookieTest extends TestCase
  8. {
  9. public function testCookiesAreCreatedWithProperOptions()
  10. {
  11. $cookie = $this->getCreator();
  12. $cookie->setDefaultPathAndDomain('foo', 'bar');
  13. $c = $cookie->make('color', 'blue', 10, '/path', '/domain', true, false, false, 'lax');
  14. $this->assertSame('blue', $c->getValue());
  15. $this->assertFalse($c->isHttpOnly());
  16. $this->assertTrue($c->isSecure());
  17. $this->assertSame('/domain', $c->getDomain());
  18. $this->assertSame('/path', $c->getPath());
  19. $this->assertSame('lax', $c->getSameSite());
  20. $c2 = $cookie->forever('color', 'blue', '/path', '/domain', true, false, false, 'strict');
  21. $this->assertSame('blue', $c2->getValue());
  22. $this->assertFalse($c2->isHttpOnly());
  23. $this->assertTrue($c2->isSecure());
  24. $this->assertSame('/domain', $c2->getDomain());
  25. $this->assertSame('/path', $c2->getPath());
  26. $this->assertSame('strict', $c2->getSameSite());
  27. $c3 = $cookie->forget('color');
  28. $this->assertNull($c3->getValue());
  29. $this->assertTrue($c3->getExpiresTime() < time());
  30. }
  31. public function testCookiesAreCreatedWithProperOptionsUsingDefaultPathAndDomain()
  32. {
  33. $cookie = $this->getCreator();
  34. $cookie->setDefaultPathAndDomain('/path', '/domain', true, 'lax');
  35. $c = $cookie->make('color', 'blue');
  36. $this->assertSame('blue', $c->getValue());
  37. $this->assertTrue($c->isSecure());
  38. $this->assertSame('/domain', $c->getDomain());
  39. $this->assertSame('/path', $c->getPath());
  40. $this->assertSame('lax', $c->getSameSite());
  41. }
  42. public function testCookiesCanSetSecureOptionUsingDefaultPathAndDomain()
  43. {
  44. $cookie = $this->getCreator();
  45. $cookie->setDefaultPathAndDomain('/path', '/domain', true, 'lax');
  46. $c = $cookie->make('color', 'blue', 10, null, null, false);
  47. $this->assertSame('blue', $c->getValue());
  48. $this->assertFalse($c->isSecure());
  49. $this->assertSame('/domain', $c->getDomain());
  50. $this->assertSame('/path', $c->getPath());
  51. $this->assertSame('lax', $c->getSameSite());
  52. }
  53. public function testQueuedCookies()
  54. {
  55. $cookie = $this->getCreator();
  56. $this->assertEmpty($cookie->getQueuedCookies());
  57. $this->assertFalse($cookie->hasQueued('foo'));
  58. $cookie->queue($cookie->make('foo', 'bar'));
  59. $this->assertTrue($cookie->hasQueued('foo'));
  60. $this->assertInstanceOf(Cookie::class, $cookie->queued('foo'));
  61. $cookie->queue('qu', 'ux');
  62. $this->assertTrue($cookie->hasQueued('qu'));
  63. $this->assertInstanceOf(Cookie::class, $cookie->queued('qu'));
  64. }
  65. public function testQueuedWithPath(): void
  66. {
  67. $cookieJar = $this->getCreator();
  68. $cookieOne = $cookieJar->make('foo', 'bar', 0, '/path');
  69. $cookieTwo = $cookieJar->make('foo', 'rab', 0, '/');
  70. $cookieJar->queue($cookieOne);
  71. $cookieJar->queue($cookieTwo);
  72. $this->assertEquals($cookieOne, $cookieJar->queued('foo', null, '/path'));
  73. $this->assertEquals($cookieTwo, $cookieJar->queued('foo', null, '/'));
  74. }
  75. public function testQueuedWithoutPath(): void
  76. {
  77. $cookieJar = $this->getCreator();
  78. $cookieOne = $cookieJar->make('foo', 'bar', 0, '/path');
  79. $cookieTwo = $cookieJar->make('foo', 'rab', 0, '/');
  80. $cookieJar->queue($cookieOne);
  81. $cookieJar->queue($cookieTwo);
  82. $this->assertEquals($cookieTwo, $cookieJar->queued('foo'));
  83. }
  84. public function testHasQueued(): void
  85. {
  86. $cookieJar = $this->getCreator();
  87. $cookie = $cookieJar->make('foo', 'bar');
  88. $cookieJar->queue($cookie);
  89. $this->assertTrue($cookieJar->hasQueued('foo'));
  90. }
  91. public function testHasQueuedWithPath(): void
  92. {
  93. $cookieJar = $this->getCreator();
  94. $cookieOne = $cookieJar->make('foo', 'bar', 0, '/path');
  95. $cookieTwo = $cookieJar->make('foo', 'rab', 0, '/');
  96. $cookieJar->queue($cookieOne);
  97. $cookieJar->queue($cookieTwo);
  98. $this->assertTrue($cookieJar->hasQueued('foo', '/path'));
  99. $this->assertTrue($cookieJar->hasQueued('foo', '/'));
  100. $this->assertFalse($cookieJar->hasQueued('foo', '/wrongPath'));
  101. }
  102. public function testExpire()
  103. {
  104. $cookieJar = $this->getCreator();
  105. $this->assertCount(0, $cookieJar->getQueuedCookies());
  106. $cookieJar->expire('foobar', '/path', '/domain');
  107. $cookie = $cookieJar->queued('foobar');
  108. $this->assertEquals('foobar', $cookie->getName());
  109. $this->assertEquals(null, $cookie->getValue());
  110. $this->assertEquals('/path', $cookie->getPath());
  111. $this->assertEquals('/domain', $cookie->getDomain());
  112. $this->assertTrue($cookie->getExpiresTime() < time());
  113. $this->assertCount(1, $cookieJar->getQueuedCookies());
  114. }
  115. public function testUnqueue()
  116. {
  117. $cookie = $this->getCreator();
  118. $cookie->queue($cookie->make('foo', 'bar'));
  119. $cookie->unqueue('foo');
  120. $this->assertEmpty($cookie->getQueuedCookies());
  121. }
  122. public function testUnqueueWithPath(): void
  123. {
  124. $cookieJar = $this->getCreator();
  125. $cookieOne = $cookieJar->make('foo', 'bar', 0, '/path');
  126. $cookieTwo = $cookieJar->make('foo', 'rab', 0, '/');
  127. $cookieJar->queue($cookieOne);
  128. $cookieJar->queue($cookieTwo);
  129. $cookieJar->unqueue('foo', '/path');
  130. $this->assertEquals(['foo' => ['/' => $cookieTwo]], $this->getQueuedPropertyValue($cookieJar));
  131. }
  132. public function testUnqueueOnlyCookieForName(): void
  133. {
  134. $cookieJar = $this->getCreator();
  135. $cookie = $cookieJar->make('foo', 'bar', 0, '/path');
  136. $cookieJar->queue($cookie);
  137. $cookieJar->unqueue('foo', '/path');
  138. $this->assertEmpty($this->getQueuedPropertyValue($cookieJar));
  139. }
  140. public function testCookieJarIsMacroable()
  141. {
  142. $cookie = $this->getCreator();
  143. $cookie->macro('foo', function () {
  144. return 'bar';
  145. });
  146. $this->assertSame('bar', $cookie->foo());
  147. }
  148. public function testQueueCookie(): void
  149. {
  150. $cookieJar = $this->getCreator();
  151. $cookie = $cookieJar->make('foo', 'bar', 0, '/path');
  152. $cookieJar->queue($cookie);
  153. $this->assertEquals(['foo' => ['/path' => $cookie]], $this->getQueuedPropertyValue($cookieJar));
  154. }
  155. public function testQueueWithCreatingNewCookie(): void
  156. {
  157. $cookieJar = $this->getCreator();
  158. $cookieJar->queue('foo', 'bar', 0, '/path');
  159. $this->assertEquals(
  160. ['foo' => ['/path' => new Cookie('foo', 'bar', 0, '/path')]],
  161. $this->getQueuedPropertyValue($cookieJar)
  162. );
  163. }
  164. public function testGetQueuedCookies(): void
  165. {
  166. $cookieJar = $this->getCreator();
  167. $cookieOne = $cookieJar->make('foo', 'bar', 0, '/path');
  168. $cookieTwo = $cookieJar->make('foo', 'rab', 0, '/');
  169. $cookieThree = $cookieJar->make('oof', 'bar', 0, '/path');
  170. $cookieJar->queue($cookieOne);
  171. $cookieJar->queue($cookieTwo);
  172. $cookieJar->queue($cookieThree);
  173. $this->assertEquals(
  174. [$cookieOne, $cookieTwo, $cookieThree],
  175. $cookieJar->getQueuedCookies()
  176. );
  177. }
  178. public function testFlushQueuedCookies(): void
  179. {
  180. $cookieJar = $this->getCreator();
  181. $cookieJar->queue($cookieJar->make('foo', 'bar', 0, '/path'));
  182. $cookieJar->queue($cookieJar->make('foo', 'rab', 0, '/'));
  183. $this->assertCount(2, $cookieJar->getQueuedCookies());
  184. $cookieJar->flushQueuedCookies();
  185. $this->assertEmpty($cookieJar->getQueuedCookies());
  186. }
  187. public function getCreator()
  188. {
  189. return new CookieJar;
  190. }
  191. private function getQueuedPropertyValue(CookieJar $cookieJar)
  192. {
  193. $property = (new ReflectionObject($cookieJar))->getProperty('queued');
  194. $property->setAccessible(true);
  195. return $property->getValue($cookieJar);
  196. }
  197. }