2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* CookieComponentTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5435
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 05:55:24 +00:00
|
|
|
|
|
|
|
App::uses('Component', 'Controller');
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('CookieComponent', 'Controller/Component');
|
|
|
|
|
2009-07-26 09:59:51 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
|
|
|
* CookieComponentTestController class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2009-03-19 21:10:13 +00:00
|
|
|
*/
|
2008-06-10 22:38:05 +00:00
|
|
|
class CookieComponentTestController extends Controller {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
|
|
|
* components property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $components = array('Cookie');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
|
|
|
* beforeFilter method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function beforeFilter() {
|
2008-06-10 22:38:05 +00:00
|
|
|
$this->Cookie->name = 'CakeTestCookie';
|
|
|
|
$this->Cookie->time = 10;
|
|
|
|
$this->Cookie->path = '/';
|
|
|
|
$this->Cookie->domain = '';
|
|
|
|
$this->Cookie->secure = false;
|
|
|
|
$this->Cookie->key = 'somerandomhaskey';
|
|
|
|
}
|
2012-03-12 02:20:25 +00:00
|
|
|
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* CookieComponentTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class CookieComponentTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
|
|
|
* Controller property
|
|
|
|
*
|
|
|
|
* @var CookieComponentTestController
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $Controller;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* start
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-09-19 23:30:41 +00:00
|
|
|
$_COOKIE = array();
|
2012-02-09 13:24:10 +00:00
|
|
|
$this->Controller = new CookieComponentTestController(new CakeRequest(), new CakeResponse());
|
2012-02-15 19:06:13 +00:00
|
|
|
$this->Controller->constructClasses();
|
|
|
|
$this->Cookie = $this->Controller->Cookie;
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->name = 'CakeTestCookie';
|
|
|
|
$this->Cookie->time = 10;
|
|
|
|
$this->Cookie->path = '/';
|
|
|
|
$this->Cookie->domain = '';
|
|
|
|
$this->Cookie->secure = false;
|
|
|
|
$this->Cookie->key = 'somerandomhaskey';
|
2010-09-19 23:30:41 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->startup($this->Controller);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->destroy();
|
2009-03-19 21:10:13 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-19 23:42:06 +00:00
|
|
|
/**
|
|
|
|
* sets up some default cookie data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-06-10 04:47:02 +00:00
|
|
|
protected function _setCookieData() {
|
2011-12-01 07:21:31 +00:00
|
|
|
$this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')));
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->write(array('Encrytped_multi_cookies.name' => 'CakePHP'));
|
|
|
|
$this->Cookie->write(array('Encrytped_multi_cookies.version' => '1.2.0.x'));
|
|
|
|
$this->Cookie->write(array('Encrytped_multi_cookies.tag' => 'CakePHP Rocks!'));
|
|
|
|
|
2011-12-01 07:21:31 +00:00
|
|
|
$this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false);
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false);
|
|
|
|
$this->Cookie->write(array('Plain_multi_cookies.version' => '1.2.0.x'), null, false);
|
|
|
|
$this->Cookie->write(array('Plain_multi_cookies.tag' => 'CakePHP Rocks!'), null, false);
|
2010-06-10 04:47:02 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 03:57:04 +00:00
|
|
|
/**
|
|
|
|
* test that initialize sets settings from components array
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSettings() {
|
2009-12-17 03:57:04 +00:00
|
|
|
$settings = array(
|
|
|
|
'time' => '5 days',
|
|
|
|
'path' => '/'
|
|
|
|
);
|
2010-07-04 21:15:10 +00:00
|
|
|
$Cookie = new CookieComponent(new ComponentCollection(), $settings);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($Cookie->time, $settings['time']);
|
|
|
|
$this->assertEquals($Cookie->path, $settings['path']);
|
2009-12-17 03:57:04 +00:00
|
|
|
}
|
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testCookieName
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCookieName() {
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('CakeTestCookie', $this->Cookie->name);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testReadEncryptedCookieData
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadEncryptedCookieData() {
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->_setCookieData();
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testReadPlainCookieData
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadPlainCookieData() {
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->_setCookieData();
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2012-03-16 01:45:48 +00:00
|
|
|
/**
|
|
|
|
* test read() after switching the cookie name.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testReadWithNameSwitch() {
|
|
|
|
$_COOKIE = array(
|
|
|
|
'CakeTestCookie' => array(
|
|
|
|
'key' => 'value'
|
|
|
|
),
|
|
|
|
'OtherTestCookie' => array(
|
|
|
|
'key' => 'other value'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals('value', $this->Cookie->read('key'));
|
|
|
|
|
|
|
|
$this->Cookie->name = 'OtherTestCookie';
|
|
|
|
$this->assertEquals('other value', $this->Cookie->read('key'));
|
|
|
|
}
|
|
|
|
|
2010-09-19 23:42:06 +00:00
|
|
|
/**
|
|
|
|
* test a simple write()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteSimple() {
|
2010-09-19 23:42:06 +00:00
|
|
|
$this->Cookie->write('Testing', 'value');
|
|
|
|
$result = $this->Cookie->read('Testing');
|
|
|
|
|
|
|
|
$this->assertEquals('value', $result);
|
|
|
|
}
|
|
|
|
|
2010-09-26 03:24:37 +00:00
|
|
|
/**
|
|
|
|
* test write with httpOnly cookies
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteHttpOnly() {
|
2010-09-26 03:24:37 +00:00
|
|
|
$this->Cookie->httpOnly = true;
|
|
|
|
$this->Cookie->secure = false;
|
|
|
|
$this->Cookie->write('Testing', 'value', false);
|
2012-02-09 13:24:10 +00:00
|
|
|
$expected = array(
|
2012-03-12 02:20:25 +00:00
|
|
|
'name' => $this->Cookie->name . '[Testing]',
|
2012-02-09 13:24:10 +00:00
|
|
|
'value' => 'value',
|
|
|
|
'expire' => time() + 10,
|
|
|
|
'path' => '/',
|
|
|
|
'domain' => '',
|
|
|
|
'secure' => false,
|
|
|
|
'httpOnly' => true);
|
2012-02-15 19:06:13 +00:00
|
|
|
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-09-26 03:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test delete with httpOnly
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDeleteHttpOnly() {
|
2010-09-26 03:24:37 +00:00
|
|
|
$this->Cookie->httpOnly = true;
|
|
|
|
$this->Cookie->secure = false;
|
|
|
|
$this->Cookie->delete('Testing', false);
|
2012-02-09 13:24:10 +00:00
|
|
|
$expected = array(
|
2012-03-12 02:20:25 +00:00
|
|
|
'name' => $this->Cookie->name . '[Testing]',
|
2012-02-09 13:24:10 +00:00
|
|
|
'value' => '',
|
|
|
|
'expire' => time() - 42000,
|
|
|
|
'path' => '/',
|
|
|
|
'domain' => '',
|
|
|
|
'secure' => false,
|
|
|
|
'httpOnly' => true);
|
2012-02-15 19:06:13 +00:00
|
|
|
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-09-26 03:24:37 +00:00
|
|
|
}
|
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testWritePlainCookieArray
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWritePlainCookieArray() {
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false);
|
2008-06-27 06:52:36 +00:00
|
|
|
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('CakePHP', $this->Cookie->read('name'));
|
|
|
|
$this->assertEquals('1.2.0.x', $this->Cookie->read('version'));
|
|
|
|
$this->assertEquals('CakePHP Rocks!', $this->Cookie->read('tag'));
|
2008-06-27 06:52:36 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->delete('name');
|
|
|
|
$this->Cookie->delete('version');
|
|
|
|
$this->Cookie->delete('tag');
|
2008-06-27 06:52:36 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-03-19 18:32:43 +00:00
|
|
|
/**
|
|
|
|
* test writing values that are not scalars
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteArrayValues() {
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->Cookie->secure = false;
|
|
|
|
$this->Cookie->write('Testing', array(1, 2, 3), false);
|
2012-02-09 13:24:10 +00:00
|
|
|
$expected = array(
|
2012-03-12 02:20:25 +00:00
|
|
|
'name' => $this->Cookie->name . '[Testing]',
|
2012-02-09 13:24:10 +00:00
|
|
|
'value' => '[1,2,3]',
|
|
|
|
'path' => '/',
|
|
|
|
'domain' => '',
|
|
|
|
'secure' => false,
|
|
|
|
'httpOnly' => false);
|
2012-03-12 02:20:25 +00:00
|
|
|
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
|
2012-09-21 01:07:14 +00:00
|
|
|
|
|
|
|
$this->assertWithinMargin($result['expire'], time() + 10, 1);
|
|
|
|
unset($result['expire']);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-03-19 18:32:43 +00:00
|
|
|
}
|
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testReadingCookieValue
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadingCookieValue() {
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->_setCookieData();
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read();
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = array(
|
|
|
|
'Encrytped_array' => array(
|
|
|
|
'name' => 'CakePHP',
|
|
|
|
'version' => '1.2.0.x',
|
|
|
|
'tag' => 'CakePHP Rocks!'),
|
|
|
|
'Encrytped_multi_cookies' => array(
|
|
|
|
'name' => 'CakePHP',
|
|
|
|
'version' => '1.2.0.x',
|
|
|
|
'tag' => 'CakePHP Rocks!'),
|
|
|
|
'Plain_array' => array(
|
|
|
|
'name' => 'CakePHP',
|
|
|
|
'version' => '1.2.0.x',
|
|
|
|
'tag' => 'CakePHP Rocks!'),
|
|
|
|
'Plain_multi_cookies' => array(
|
|
|
|
'name' => 'CakePHP',
|
|
|
|
'version' => '1.2.0.x',
|
|
|
|
'tag' => 'CakePHP Rocks!'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testDeleteCookieValue
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDeleteCookieValue() {
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->_setCookieData();
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->delete('Encrytped_multi_cookies.name');
|
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->delete('Encrytped_array');
|
|
|
|
$data = $this->Cookie->read('Encrytped_array');
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->assertNull($data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->delete('Plain_multi_cookies.name');
|
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->delete('Plain_array');
|
|
|
|
$data = $this->Cookie->read('Plain_array');
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->assertNull($data);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testReadingCookieArray
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadingCookieArray() {
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->_setCookieData();
|
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array.name');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array.version');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = '1.2.0.x';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array.tag');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP Rocks!';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies.name');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies.version');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = '1.2.0.x';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies.tag');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP Rocks!';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array.name');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array.version');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = '1.2.0.x';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array.tag');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP Rocks!';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies.name');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies.version');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = '1.2.0.x';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies.tag');
|
2008-06-10 22:38:05 +00:00
|
|
|
$expected = 'CakePHP Rocks!';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testReadingCookieDataOnStartup
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadingCookieDataOnStartup() {
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array');
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->assertNull($data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->assertNull($data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array');
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->assertNull($data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies');
|
2010-06-10 04:47:02 +00:00
|
|
|
$this->assertNull($data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
|
|
|
$_COOKIE['CakeTestCookie'] = array(
|
2012-11-04 11:23:36 +00:00
|
|
|
'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
|
2008-06-10 22:38:05 +00:00
|
|
|
'Encrytped_multi_cookies' => array(
|
2012-11-04 11:23:36 +00:00
|
|
|
'name' => $this->_encrypt('CakePHP'),
|
|
|
|
'version' => $this->_encrypt('1.2.0.x'),
|
|
|
|
'tag' => $this->_encrypt('CakePHP Rocks!')),
|
2011-03-19 18:32:43 +00:00
|
|
|
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
|
2008-06-10 22:38:05 +00:00
|
|
|
'Plain_multi_cookies' => array(
|
|
|
|
'name' => 'CakePHP',
|
|
|
|
'version' => '1.2.0.x',
|
|
|
|
'tag' => 'CakePHP Rocks!'));
|
2011-03-19 18:32:43 +00:00
|
|
|
|
2012-02-23 14:23:59 +00:00
|
|
|
$this->Cookie->startup(new CookieComponentTestController());
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->destroy();
|
2008-06-10 22:38:05 +00:00
|
|
|
unset($_COOKIE['CakeTestCookie']);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
|
|
|
* testReadingCookieDataWithoutStartup
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadingCookieDataWithoutStartup() {
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array');
|
2010-06-10 04:47:02 +00:00
|
|
|
$expected = null;
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
2010-06-10 04:47:02 +00:00
|
|
|
$expected = null;
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array');
|
2010-06-10 04:47:02 +00:00
|
|
|
$expected = null;
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies');
|
2010-06-10 04:47:02 +00:00
|
|
|
$expected = null;
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
|
|
|
$_COOKIE['CakeTestCookie'] = array(
|
2012-11-04 11:23:36 +00:00
|
|
|
'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
|
2008-06-10 22:38:05 +00:00
|
|
|
'Encrytped_multi_cookies' => array(
|
2012-11-04 11:23:36 +00:00
|
|
|
'name' => $this->_encrypt('CakePHP'),
|
|
|
|
'version' => $this->_encrypt('1.2.0.x'),
|
|
|
|
'tag' => $this->_encrypt('CakePHP Rocks!')),
|
2011-03-19 18:32:43 +00:00
|
|
|
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
|
2008-06-10 22:38:05 +00:00
|
|
|
'Plain_multi_cookies' => array(
|
|
|
|
'name' => 'CakePHP',
|
|
|
|
'version' => '1.2.0.x',
|
|
|
|
'tag' => 'CakePHP Rocks!'));
|
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_array');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Encrytped_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_array');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-04 21:15:10 +00:00
|
|
|
$data = $this->Cookie->read('Plain_multi_cookies');
|
2011-12-01 07:21:31 +00:00
|
|
|
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
|
2011-03-19 18:32:43 +00:00
|
|
|
$this->assertEquals($expected, $data);
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->destroy();
|
2008-06-10 22:38:05 +00:00
|
|
|
unset($_COOKIE['CakeTestCookie']);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-04-19 02:01:08 +00:00
|
|
|
/**
|
|
|
|
* Test Reading legacy cookie values.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadLegacyCookieValue() {
|
2011-04-19 02:01:08 +00:00
|
|
|
$_COOKIE['CakeTestCookie'] = array(
|
|
|
|
'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
|
|
|
|
);
|
|
|
|
$result = $this->Cookie->read('Legacy.value');
|
|
|
|
$expected = array(1, 2, 3);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2010-11-25 03:09:08 +00:00
|
|
|
|
2012-02-11 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* Test reading empty values.
|
|
|
|
*/
|
|
|
|
public function testReadEmpty() {
|
|
|
|
$_COOKIE['CakeTestCookie'] = array(
|
2012-05-12 01:38:21 +00:00
|
|
|
'JSON' => '{"name":"value"}',
|
|
|
|
'Empty' => '',
|
|
|
|
'String' => '{"somewhat:"broken"}'
|
2012-02-11 16:32:44 +00:00
|
|
|
);
|
2012-04-21 13:02:35 +00:00
|
|
|
$this->assertEquals(array('name' => 'value'), $this->Cookie->read('JSON'));
|
|
|
|
$this->assertEquals('value', $this->Cookie->read('JSON.name'));
|
|
|
|
$this->assertEquals('', $this->Cookie->read('Empty'));
|
|
|
|
$this->assertEquals('{"somewhat:"broken"}', $this->Cookie->read('String'));
|
2012-02-11 16:32:44 +00:00
|
|
|
}
|
|
|
|
|
2010-11-25 03:09:08 +00:00
|
|
|
/**
|
|
|
|
* test that no error is issued for non array data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNoErrorOnNonArrayData() {
|
2010-12-11 03:51:42 +00:00
|
|
|
$this->Cookie->destroy();
|
2010-11-25 03:09:08 +00:00
|
|
|
$_COOKIE['CakeTestCookie'] = 'kaboom';
|
|
|
|
|
2010-12-11 03:51:42 +00:00
|
|
|
$this->assertNull($this->Cookie->read('value'));
|
2010-11-25 03:09:08 +00:00
|
|
|
}
|
2011-05-04 05:40:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that deleting a top level keys kills the child elements too.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDeleteRemovesChildren() {
|
2011-05-04 05:40:23 +00:00
|
|
|
$_COOKIE['CakeTestCookie'] = array(
|
|
|
|
'User' => array('email' => 'example@example.com', 'name' => 'mark'),
|
|
|
|
'other' => 'value'
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('mark', $this->Cookie->read('User.name'));
|
2011-05-04 05:40:23 +00:00
|
|
|
|
|
|
|
$this->Cookie->delete('User');
|
|
|
|
$this->assertNull($this->Cookie->read('User.email'));
|
|
|
|
$this->Cookie->destroy();
|
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2011-06-23 19:48:06 +00:00
|
|
|
/**
|
|
|
|
* Test deleting recursively with keys that don't exist.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-17 07:13:12 +00:00
|
|
|
public function testDeleteChildrenNotExist() {
|
2011-06-23 20:22:08 +00:00
|
|
|
$this->assertNull($this->Cookie->delete('NotFound'));
|
|
|
|
$this->assertNull($this->Cookie->delete('Not.Found'));
|
2011-06-23 19:48:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 18:32:43 +00:00
|
|
|
/**
|
2011-04-19 02:01:08 +00:00
|
|
|
* Helper method for generating old style encoded cookie values.
|
2011-03-19 18:32:43 +00:00
|
|
|
*
|
2011-04-19 02:01:08 +00:00
|
|
|
* @return string.
|
2011-03-19 18:32:43 +00:00
|
|
|
*/
|
2011-04-19 02:01:08 +00:00
|
|
|
protected function _oldImplode(array $array) {
|
2011-03-19 18:32:43 +00:00
|
|
|
$string = '';
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$string .= ',' . $key . '|' . $value;
|
|
|
|
}
|
|
|
|
return substr($string, 1);
|
|
|
|
}
|
2010-11-25 03:09:08 +00:00
|
|
|
|
2011-04-19 02:01:08 +00:00
|
|
|
/**
|
|
|
|
* Implode method to keep keys are multidimensional arrays
|
|
|
|
*
|
|
|
|
* @param array $array Map of key and values
|
|
|
|
* @return string String in the form key1|value1,key2|value2
|
|
|
|
*/
|
|
|
|
protected function _implode(array $array) {
|
|
|
|
return json_encode($array);
|
|
|
|
}
|
|
|
|
|
2008-10-09 02:35:33 +00:00
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* encrypt method
|
2008-10-09 02:35:33 +00:00
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param array|string $value
|
2009-03-19 21:10:13 +00:00
|
|
|
* @return string
|
2008-10-09 02:35:33 +00:00
|
|
|
*/
|
2012-11-04 11:23:36 +00:00
|
|
|
protected function _encrypt($value) {
|
2009-03-19 21:10:13 +00:00
|
|
|
if (is_array($value)) {
|
2011-03-19 18:32:43 +00:00
|
|
|
$value = $this->_implode($value);
|
2009-03-19 21:10:13 +00:00
|
|
|
}
|
2010-07-04 21:15:10 +00:00
|
|
|
return "Q2FrZQ==." . base64_encode(Security::cipher($value, $this->Cookie->key));
|
2009-03-19 21:10:13 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|