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
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, 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
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing 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';
|
|
|
|
}
|
|
|
|
}
|
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();
|
2010-07-04 21:15:10 +00:00
|
|
|
$Collection = new ComponentCollection();
|
2010-09-19 23:42:06 +00:00
|
|
|
$this->Cookie = $this->getMock('CookieComponent', array('_setcookie'), array($Collection));
|
2008-06-10 22:38:05 +00:00
|
|
|
$this->Controller = new CookieComponentTestController();
|
2010-07-04 21:15:10 +00:00
|
|
|
$this->Cookie->initialize($this->Controller);
|
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() {
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($this->Cookie->name, 'CakeTestCookie');
|
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!');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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!');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
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->expects($this->once())->method('_setcookie');
|
|
|
|
|
|
|
|
$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->expects($this->once())->method('_setcookie')
|
|
|
|
->with('CakeTestCookie[Testing]', 'value', time() + 10, '/', '', false, true);
|
|
|
|
|
|
|
|
$this->Cookie->write('Testing', 'value', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->expects($this->once())->method('_setcookie')
|
|
|
|
->with('CakeTestCookie[Testing]', '', time() - 42000, '/', '', false, true);
|
|
|
|
|
|
|
|
$this->Cookie->delete('Testing', false);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($this->Cookie->read('name'), 'CakePHP');
|
|
|
|
$this->assertEquals($this->Cookie->read('version'), '1.2.0.x');
|
|
|
|
$this->assertEquals($this->Cookie->read('tag'), 'CakePHP Rocks!');
|
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->expects($this->once())->method('_setcookie')
|
|
|
|
->with('CakeTestCookie[Testing]', '[1,2,3]', time() + 10, '/', '', false, false);
|
|
|
|
|
|
|
|
$this->Cookie->write('Testing', array(1, 2, 3), false);
|
|
|
|
}
|
|
|
|
|
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!'));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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!');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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!');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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!';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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!';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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!';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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!';
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, $expected);
|
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(
|
2011-12-01 07:21:31 +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(
|
2008-10-09 02:35:33 +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
|
|
|
|
2010-12-18 05:03:03 +00:00
|
|
|
$this->Cookie->startup(null);
|
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(
|
2011-12-01 07:21:31 +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(
|
2008-10-09 02:35:33 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-06-23 19:48:06 +00:00
|
|
|
/**
|
|
|
|
* Test deleting recursively with keys that don't exist.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @param mixed $value
|
|
|
|
* @return string
|
2008-10-09 02:35:33 +00:00
|
|
|
*/
|
2009-03-19 21:10:13 +00:00
|
|
|
function __encrypt($value) {
|
|
|
|
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
|
|
|
}
|