Skip to content

Commit

Permalink
Fix read() and check().
Browse files Browse the repository at this point in the history
Both appear to be working now.
  • Loading branch information
markstory committed May 29, 2014
1 parent 82c1443 commit 875dd30
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 81 deletions.
14 changes: 9 additions & 5 deletions src/Controller/Component/CookieComponent.php
Expand Up @@ -326,7 +326,6 @@ public function encryption($type = null) {
*/
protected function _write($name, $value) {
$config = $this->config();

$expires = new \DateTime($config['expires']);

$this->_response->cookie(array(
Expand All @@ -348,10 +347,12 @@ protected function _write($name, $value) {
*/
protected function _delete($name) {
$config = $this->config();
$expires = new \DateTime($config['expires']);

$this->_response->cookie(array(
'name' => $config['name'] . $name,
'name' => $name,
'value' => '',
'expire' => time() - 42000,
'expire' => $expires->format('U') - 42000,
'path' => $config['path'],
'domain' => $config['domain'],
'secure' => $config['secure'],
Expand Down Expand Up @@ -389,9 +390,12 @@ protected function _encrypt($value) {
* @return string decrypted string
*/
protected function _decrypt($values) {
$decrypted = array();
if (is_string($values)) {
return $this->_decode($values);
}

foreach ((array)$values as $name => $value) {
$decrypted = array();
foreach ($values as $name => $value) {
if (is_array($value)) {
foreach ($value as $key => $val) {
$decrypted[$name][$key] = $this->_decode($val);
Expand Down
78 changes: 2 additions & 76 deletions tests/TestCase/Controller/Component/CookieComponentTest.php
Expand Up @@ -566,70 +566,13 @@ public function testReadingDataFromRequest() {
$this->assertEquals($expected, $data);
}

/**
* testReadingCookieDataWithoutStartup
*
* @return void
*/
public function testReadingCookieDataWithoutStartup() {
$this->markTestIncomplete();
$data = $this->Cookie->read('Encrytped_array');
$expected = null;
$this->assertEquals($expected, $data);

$data = $this->Cookie->read('Encrytped_multi_cookies');
$expected = null;
$this->assertEquals($expected, $data);

$data = $this->Cookie->read('Plain_array');
$expected = null;
$this->assertEquals($expected, $data);

$data = $this->Cookie->read('Plain_multi_cookies');
$expected = null;
$this->assertEquals($expected, $data);

$this->request->cookies['CakeTestCookie'] = array(
'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
'Encrytped_multi_cookies' => array(
'name' => $this->_encrypt('CakePHP'),
'version' => $this->_encrypt('1.2.0.x'),
'tag' => $this->_encrypt('CakePHP Rocks!')
),
'Plain_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!'
)
);

$data = $this->Cookie->read('Encrytped_array');
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
$this->assertEquals($expected, $data);

$data = $this->Cookie->read('Encrytped_multi_cookies');
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
$this->assertEquals($expected, $data);

$data = $this->Cookie->read('Plain_array');
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
$this->assertEquals($expected, $data);

$data = $this->Cookie->read('Plain_multi_cookies');
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
$this->assertEquals($expected, $data);
$this->Cookie->destroy();
}

/**
* Test Reading legacy cookie values.
*
* @return void
*/
public function testReadLegacyCookieValue() {
$this->markTestIncomplete();
$this->request->cookies['CakeTestCookie'] = array(
$this->request->cookies = array(
'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
);
$result = $this->Cookie->read('Legacy.value');
Expand All @@ -643,8 +586,7 @@ public function testReadLegacyCookieValue() {
* @return void
*/
public function testReadEmpty() {
$this->markTestIncomplete();
$this->request->cookies['CakeTestCookie'] = array(
$this->request->cookies = array(
'JSON' => '{"name":"value"}',
'Empty' => '',
'String' => '{"somewhat:"broken"}',
Expand All @@ -657,25 +599,12 @@ public function testReadEmpty() {
$this->assertEquals(array(), $this->Cookie->read('Array'));
}

/**
* test that no error is issued for non array data.
*
* @return void
*/
public function testNoErrorOnNonArrayData() {
$this->markTestIncomplete();
$this->request->cookies['CakeTestCookie'] = 'kaboom';

$this->assertNull($this->Cookie->read('value'));
}

/**
* testCheck method
*
* @return void
*/
public function testCheck() {
$this->markTestIncomplete();
$this->Cookie->write('CookieComponentTestCase', 'value');
$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));

Expand All @@ -688,7 +617,6 @@ public function testCheck() {
* @return void
*/
public function testCheckingSavedEmpty() {
$this->markTestIncomplete();
$this->Cookie->write('CookieComponentTestCase', 0);
$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));

Expand All @@ -702,7 +630,6 @@ public function testCheckingSavedEmpty() {
* @return void
*/
public function testCheckKeyWithSpaces() {
$this->markTestIncomplete();
$this->Cookie->write('CookieComponent Test', "test");
$this->assertTrue($this->Cookie->check('CookieComponent Test'));
$this->Cookie->delete('CookieComponent Test');
Expand All @@ -717,7 +644,6 @@ public function testCheckKeyWithSpaces() {
* @return void
*/
public function testCheckEmpty() {
$this->markTestIncomplete();
$this->assertFalse($this->Cookie->check());
}

Expand Down

0 comments on commit 875dd30

Please sign in to comment.