2011-02-06 04:42:19 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-02-06 04:42:19 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\HttpFoundation\Tests ;
2011-02-06 04:42:19 +00:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
2011-02-06 04:42:19 +00:00
use Symfony\Component\HttpFoundation\Cookie ;
/**
2014-12-21 17:00:50 +00:00
* CookieTest .
2011-02-06 04:42:19 +00:00
*
* @ author John Kary < john @ johnkary . net >
2011-03-03 13:49:34 +00:00
* @ author Hugo Hamon < hugo . hamon @ sensio . com >
2015-10-11 10:00:41 +01:00
*
* @ group time - sensitive
2011-02-06 04:42:19 +00:00
*/
2017-02-08 07:24:27 +00:00
class CookieTest extends TestCase
2011-02-06 04:42:19 +00:00
{
public function invalidNames ()
{
return array (
array ( '' ),
2015-03-21 10:51:07 +00:00
array ( ',MyName' ),
array ( ';MyName' ),
array ( ' MyName' ),
2011-02-06 04:42:19 +00:00
array ( " \t MyName " ),
array ( " \r MyName " ),
array ( " \n MyName " ),
array ( " \013 MyName " ),
array ( " \014 MyName " ),
);
}
/**
* @ dataProvider invalidNames
2013-11-25 08:44:14 +00:00
* @ expectedException \InvalidArgumentException
2011-02-06 04:42:19 +00:00
*/
public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters ( $name )
{
new Cookie ( $name );
}
2011-03-05 23:13:47 +00:00
/**
2013-11-25 08:44:14 +00:00
* @ expectedException \InvalidArgumentException
2011-03-05 23:13:47 +00:00
*/
public function testInvalidExpiration ()
{
2016-12-14 18:35:20 +00:00
new Cookie ( 'MyCookie' , 'foo' , 'bar' );
}
public function testNegativeExpirationIsNotPossible ()
{
$cookie = new Cookie ( 'foo' , 'bar' , - 100 );
$this -> assertSame ( 0 , $cookie -> getExpiresTime ());
2011-03-05 23:13:47 +00:00
}
2011-02-06 04:42:19 +00:00
public function testGetValue ()
{
$value = 'MyValue' ;
$cookie = new Cookie ( 'MyCookie' , $value );
$this -> assertSame ( $value , $cookie -> getValue (), '->getValue() returns the proper value' );
}
2011-03-03 13:49:34 +00:00
public function testGetPath ()
{
$cookie = new Cookie ( 'foo' , 'bar' );
$this -> assertSame ( '/' , $cookie -> getPath (), '->getPath() returns / as the default path' );
}
2011-04-13 19:09:04 +01:00
public function testGetExpiresTime ()
2011-03-03 13:49:34 +00:00
{
2016-11-26 11:57:47 +00:00
$cookie = new Cookie ( 'foo' , 'bar' );
$this -> assertEquals ( 0 , $cookie -> getExpiresTime (), '->getExpiresTime() returns the default expire date' );
$cookie = new Cookie ( 'foo' , 'bar' , $expire = time () + 3600 );
2011-03-03 13:49:34 +00:00
2016-11-26 11:57:47 +00:00
$this -> assertEquals ( $expire , $cookie -> getExpiresTime (), '->getExpiresTime() returns the expire date' );
2011-03-03 13:49:34 +00:00
}
2016-12-14 18:35:20 +00:00
public function testGetExpiresTimeIsCastToInt ()
{
$cookie = new Cookie ( 'foo' , 'bar' , 3600.9 );
$this -> assertSame ( 3600 , $cookie -> getExpiresTime (), '->getExpiresTime() returns the expire date as an integer' );
}
2012-09-04 17:27:42 +01:00
public function testConstructorWithDateTime ()
{
$expire = new \DateTime ();
$cookie = new Cookie ( 'foo' , 'bar' , $expire );
$this -> assertEquals ( $expire -> format ( 'U' ), $cookie -> getExpiresTime (), '->getExpiresTime() returns the expire date' );
}
2016-01-14 12:21:56 +00:00
public function testConstructorWithDateTimeImmutable ()
{
$expire = new \DateTimeImmutable ();
$cookie = new Cookie ( 'foo' , 'bar' , $expire );
$this -> assertEquals ( $expire -> format ( 'U' ), $cookie -> getExpiresTime (), '->getExpiresTime() returns the expire date' );
}
2012-09-04 17:27:42 +01:00
public function testGetExpiresTimeWithStringValue ()
{
2015-03-21 10:51:07 +00:00
$value = '+1 day' ;
2012-09-04 17:27:42 +01:00
$cookie = new Cookie ( 'foo' , 'bar' , $value );
$expire = strtotime ( $value );
2015-10-13 08:59:06 +01:00
$this -> assertEquals ( $expire , $cookie -> getExpiresTime (), '->getExpiresTime() returns the expire date' , 1 );
2012-09-04 17:27:42 +01:00
}
2011-03-03 13:49:34 +00:00
public function testGetDomain ()
{
2016-11-26 11:57:47 +00:00
$cookie = new Cookie ( 'foo' , 'bar' , 0 , '/' , '.myfoodomain.com' );
2011-03-03 13:49:34 +00:00
$this -> assertEquals ( '.myfoodomain.com' , $cookie -> getDomain (), '->getDomain() returns the domain name on which the cookie is valid' );
}
public function testIsSecure ()
{
2016-11-26 11:57:47 +00:00
$cookie = new Cookie ( 'foo' , 'bar' , 0 , '/' , '.myfoodomain.com' , true );
2011-03-03 13:49:34 +00:00
$this -> assertTrue ( $cookie -> isSecure (), '->isSecure() returns whether the cookie is transmitted over HTTPS' );
}
public function testIsHttpOnly ()
{
2016-11-26 11:57:47 +00:00
$cookie = new Cookie ( 'foo' , 'bar' , 0 , '/' , '.myfoodomain.com' , false , true );
2011-03-03 13:49:34 +00:00
$this -> assertTrue ( $cookie -> isHttpOnly (), '->isHttpOnly() returns whether the cookie is only transmitted over HTTP' );
}
public function testCookieIsNotCleared ()
{
2015-03-27 22:05:05 +00:00
$cookie = new Cookie ( 'foo' , 'bar' , time () + 3600 * 24 );
2011-03-03 13:49:34 +00:00
$this -> assertFalse ( $cookie -> isCleared (), '->isCleared() returns false if the cookie did not expire yet' );
}
public function testCookieIsCleared ()
{
2015-03-27 22:05:05 +00:00
$cookie = new Cookie ( 'foo' , 'bar' , time () - 20 );
2011-03-03 13:49:34 +00:00
$this -> assertTrue ( $cookie -> isCleared (), '->isCleared() returns true if the cookie has expired' );
2018-07-23 19:55:22 +01:00
$cookie = new Cookie ( 'foo' , 'bar' );
$this -> assertFalse ( $cookie -> isCleared ());
$cookie = new Cookie ( 'foo' , 'bar' , 0 );
$this -> assertFalse ( $cookie -> isCleared ());
$cookie = new Cookie ( 'foo' , 'bar' , - 1 );
$this -> assertFalse ( $cookie -> isCleared ());
2011-03-03 13:49:34 +00:00
}
2011-06-08 18:56:59 +01:00
2011-05-19 08:48:58 +01:00
public function testToString ()
{
2016-11-26 11:57:47 +00:00
$cookie = new Cookie ( 'foo' , 'bar' , $expire = strtotime ( 'Fri, 20-May-2011 15:25:52 GMT' ), '/' , '.myfoodomain.com' , true );
2018-04-05 13:19:22 +01:00
$this -> assertEquals ( 'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly' , ( string ) $cookie , '->__toString() returns string representation of the cookie' );
2011-06-08 18:56:59 +01:00
2017-07-09 19:05:54 +01:00
$cookie = new Cookie ( 'foo' , 'bar with white spaces' , strtotime ( 'Fri, 20-May-2011 15:25:52 GMT' ), '/' , '.myfoodomain.com' , true );
2018-04-05 13:19:22 +01:00
$this -> assertEquals ( 'foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly' , ( string ) $cookie , '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)' );
2017-07-09 19:05:54 +01:00
2011-09-28 09:49:50 +01:00
$cookie = new Cookie ( 'foo' , null , 1 , '/admin/' , '.myfoodomain.com' );
2018-04-05 13:19:22 +01:00
$this -> assertEquals ( 'foo=deleted; expires=' . gmdate ( 'D, d-M-Y H:i:s T' , $expire = time () - 31536001 ) . '; Max-Age=0; path=/admin/; domain=.myfoodomain.com; httponly' , ( string ) $cookie , '->__toString() returns string representation of a cleared cookie if value is NULL' );
2013-04-20 18:50:53 +01:00
$cookie = new Cookie ( 'foo' , 'bar' , 0 , '/' , '' );
2016-11-26 11:57:47 +00:00
$this -> assertEquals ( 'foo=bar; path=/; httponly' , ( string ) $cookie );
2011-05-19 08:48:58 +01:00
}
2016-03-11 09:58:00 +00:00
public function testRawCookie ()
{
2016-12-13 18:58:15 +00:00
$cookie = new Cookie ( 'foo' , 'b a r' , 0 , '/' , null , false , false );
2016-03-11 09:58:00 +00:00
$this -> assertFalse ( $cookie -> isRaw ());
2017-07-12 15:37:06 +01:00
$this -> assertEquals ( 'foo=b%20a%20r; path=/' , ( string ) $cookie );
2016-03-11 09:58:00 +00:00
2016-12-13 18:58:15 +00:00
$cookie = new Cookie ( 'foo' , 'b+a+r' , 0 , '/' , null , false , false , true );
2016-03-11 09:58:00 +00:00
$this -> assertTrue ( $cookie -> isRaw ());
2016-12-13 18:58:15 +00:00
$this -> assertEquals ( 'foo=b+a+r; path=/' , ( string ) $cookie );
2016-03-11 09:58:00 +00:00
}
2016-11-26 11:57:47 +00:00
public function testGetMaxAge ()
{
$cookie = new Cookie ( 'foo' , 'bar' );
$this -> assertEquals ( 0 , $cookie -> getMaxAge ());
$cookie = new Cookie ( 'foo' , 'bar' , $expire = time () + 100 );
$this -> assertEquals ( $expire - time (), $cookie -> getMaxAge ());
$cookie = new Cookie ( 'foo' , 'bar' , $expire = time () - 100 );
2018-04-05 13:19:22 +01:00
$this -> assertEquals ( 0 , $cookie -> getMaxAge ());
2016-11-26 11:57:47 +00:00
}
2016-11-19 14:10:17 +00:00
public function testFromString ()
{
$cookie = Cookie :: fromString ( 'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly' );
$this -> assertEquals ( new Cookie ( 'foo' , 'bar' , strtotime ( 'Fri, 20-May-2011 15:25:52 GMT' ), '/' , '.myfoodomain.com' , true , true , true ), $cookie );
$cookie = Cookie :: fromString ( 'foo=bar' , true );
2017-07-06 09:48:01 +01:00
$this -> assertEquals ( new Cookie ( 'foo' , 'bar' , 0 , '/' , null , false , false ), $cookie );
2018-04-20 15:29:33 +01:00
$cookie = Cookie :: fromString ( 'foo' , true );
$this -> assertEquals ( new Cookie ( 'foo' , null , 0 , '/' , null , false , false ), $cookie );
2017-07-06 09:48:01 +01:00
}
public function testFromStringWithHttpOnly ()
{
$cookie = Cookie :: fromString ( 'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure; httponly' );
$this -> assertTrue ( $cookie -> isHttpOnly ());
$cookie = Cookie :: fromString ( 'foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; path=/; domain=.myfoodomain.com; secure' );
$this -> assertFalse ( $cookie -> isHttpOnly ());
2016-11-19 14:10:17 +00:00
}
2017-07-21 12:04:46 +01:00
2017-07-19 12:17:30 +01:00
public function testSameSiteAttributeIsCaseInsensitive ()
{
$cookie = new Cookie ( 'foo' , 'bar' , 0 , '/' , null , false , true , false , 'Lax' );
$this -> assertEquals ( 'lax' , $cookie -> getSameSite ());
}
2011-02-06 04:42:19 +00:00
}