cookie.test.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php namespace Laravel;
  2. /**
  3. * Stub the global setcookie method into the Laravel namespace.
  4. */
  5. function setcookie($name, $value, $time, $path, $domain, $secure)
  6. {
  7. $_SERVER['cookie.stub'][$name] = compact('name', 'value', 'time', 'path', 'domain', 'secure');
  8. }
  9. function headers_sent()
  10. {
  11. return $_SERVER['function.headers_sent'];
  12. }
  13. class CookieTest extends \PHPUnit_Framework_TestCase {
  14. /**
  15. * Setup the test environment.
  16. */
  17. public function setUp()
  18. {
  19. Cookie::$jar = array();
  20. }
  21. /**
  22. * Tear down the test environment.
  23. */
  24. public function tearDown()
  25. {
  26. Cookie::$jar = array();
  27. }
  28. /**
  29. * Test Cookie::has method.
  30. *
  31. * @group laravel
  32. */
  33. public function testHasMethodIndicatesIfCookieInSet()
  34. {
  35. Cookie::$jar['foo'] = array('value' => 'bar');
  36. $this->assertTrue(Cookie::has('foo'));
  37. $this->assertFalse(Cookie::has('bar'));
  38. Cookie::put('baz', 'foo');
  39. $this->assertTrue(Cookie::has('baz'));
  40. }
  41. /**
  42. * Test the Cookie::get method.
  43. *
  44. * @group laravel
  45. */
  46. public function testGetMethodCanReturnValueOfCookies()
  47. {
  48. Cookie::$jar['foo'] = array('value' => 'bar');
  49. $this->assertEquals('bar', Cookie::get('foo'));
  50. Cookie::put('bar', 'baz');
  51. $this->assertEquals('baz', Cookie::get('bar'));
  52. }
  53. /**
  54. * Test the Cookie::get method respects signatures.
  55. *
  56. * @group laravel
  57. */
  58. public function testTamperedCookiesAreReturnedAsNull()
  59. {
  60. $_COOKIE['foo'] = Cookie::sign('foo', 'bar');
  61. $this->assertEquals('bar', Cookie::get('foo'));
  62. $_COOKIE['foo'] .= '-baz';
  63. $this->assertNull(Cookie::get('foo'));
  64. $_COOKIE['foo'] = Cookie::sign('foo', 'bar');
  65. $_COOKIE['foo'] = 'aslk'.$_COOKIE['foo'];
  66. $this->assertNull(Cookie::get('foo'));
  67. }
  68. /**
  69. * Test Cookie::forever method.
  70. *
  71. * @group laravel
  72. */
  73. public function testForeverShouldUseATonOfMinutes()
  74. {
  75. Cookie::forever('foo', 'bar');
  76. $this->assertEquals('bar', Cookie::$jar['foo']['value']);
  77. $this->assertEquals(525600, Cookie::$jar['foo']['minutes']);
  78. Cookie::forever('bar', 'baz', 'path', 'domain', true);
  79. $this->assertEquals('path', Cookie::$jar['bar']['path']);
  80. $this->assertEquals('domain', Cookie::$jar['bar']['domain']);
  81. $this->assertTrue(Cookie::$jar['bar']['secure']);
  82. }
  83. /**
  84. * Test the Cookie::forget method.
  85. *
  86. * @group laravel
  87. */
  88. public function testForgetSetsCookieWithExpiration()
  89. {
  90. Cookie::forget('bar', 'path', 'domain', true);
  91. $this->assertEquals(-2000, Cookie::$jar['bar']['minutes']);
  92. $this->assertEquals('path', Cookie::$jar['bar']['path']);
  93. $this->assertEquals('domain', Cookie::$jar['bar']['domain']);
  94. $this->assertTrue(Cookie::$jar['bar']['secure']);
  95. }
  96. /**
  97. * Test the Cookie::send method.
  98. *
  99. * @group laravel
  100. */
  101. public function testSendMethodSetsProperValuesOnCookie()
  102. {
  103. $_SERVER['cookie.stub'] = array();
  104. $_SERVER['function.headers_sent'] = false;
  105. Cookie::send();
  106. $this->assertTrue(count($_SERVER['cookie.stub']) == 0);
  107. Cookie::put('foo', 'bar', 20, 'path', 'domain', true);
  108. Cookie::send();
  109. $this->assertTrue(count($_SERVER['cookie.stub']) == 1);
  110. $this->assertEquals('foo', $_SERVER['cookie.stub']['foo']['name']);
  111. $this->assertEquals(Cookie::sign('foo', 'bar'), $_SERVER['cookie.stub']['foo']['value']);
  112. $this->assertEquals('path', $_SERVER['cookie.stub']['foo']['path']);
  113. $this->assertEquals('domain', $_SERVER['cookie.stub']['foo']['domain']);
  114. $this->assertEquals((time() + (20 * 60)), $_SERVER['cookie.stub']['foo']['time']);
  115. $this->assertTrue($_SERVER['cookie.stub']['foo']['secure']);
  116. Cookie::put('bar', 'baz', 0);
  117. Cookie::send();
  118. $this->assertEquals(0, $_SERVER['cookie.stub']['bar']['time']);
  119. }
  120. }