Browse Source

Update delete() to work with new implementation of cookies.

Update delete method, and fix typos in test data.
mark_story 11 years ago
parent
commit
eee450f179

+ 16 - 21
src/Controller/Component/CookieComponent.php

@@ -245,33 +245,25 @@ class CookieComponent extends Component {
  * This method will delete both the top level and 2nd level cookies set.
  * This method will delete both the top level and 2nd level cookies set.
  * For example assuming that $name = App, deleting `User` will delete
  * For example assuming that $name = App, deleting `User` will delete
  * both `App[User]` and any other cookie values like `App[User][email]`
  * both `App[User]` and any other cookie values like `App[User][email]`
- * This is done to clean up cookie storage from before 2.4.3, where cookies
- * were stored inconsistently.
  *
  *
  * @param string $key Key of the value to be deleted
  * @param string $key Key of the value to be deleted
  * @return void
  * @return void
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete
  */
  */
 	public function delete($key) {
 	public function delete($key) {
-		$cookieName = $this->config('name');
-		if (empty($this->_values[$cookieName])) {
-			$this->read();
-		}
-		if (strpos($key, '.') === false) {
-			if (isset($this->_values[$cookieName][$key]) && is_array($this->_values[$cookieName][$key])) {
-				foreach ($this->_values[$cookieName][$key] as $idx => $val) {
-					$this->_delete("[$key][$idx]");
-				}
-			}
-			$this->_delete("[$key]");
-			unset($this->_values[$cookieName][$key]);
-			return;
+		if (empty($this->_values)) {
+			$this->_load();
 		}
 		}
-		$names = explode('.', $key, 2);
-		if (isset($this->_values[$cookieName][$names[0]])) {
-			$this->_values[$cookieName][$names[0]] = Hash::remove($this->_values[$cookieName][$names[0]], $names[1]);
+
+		$this->_values = Hash::remove($this->_values, $key);
+		$parts = explode('.', $key);
+		$top = $parts[0];
+
+		if (isset($this->_values[$top])) {
+			$this->_write($top, $this->_values[$top]);
+		} else {
+			$this->_delete($top);
 		}
 		}
-		$this->_delete('[' . implode('][', $names) . ']');
 	}
 	}
 
 
 /**
 /**
@@ -347,14 +339,17 @@ class CookieComponent extends Component {
 	}
 	}
 
 
 /**
 /**
- * Sets a cookie expire time to remove cookie value
+ * Sets a cookie expire time to remove cookie value.
+ *
+ * This is only done once all values in a cookie key have been
+ * removed with delete.
  *
  *
  * @param string $name Name of cookie
  * @param string $name Name of cookie
  * @return void
  * @return void
  */
  */
 	protected function _delete($name) {
 	protected function _delete($name) {
 		$config = $this->configKey($name);
 		$config = $this->configKey($name);
-		$expires = new \DateTime($config['expires']);
+		$expires = new \DateTime('now');
 
 
 		$this->_response->cookie(array(
 		$this->_response->cookie(array(
 			'name' => $name,
 			'name' => $name,

+ 28 - 49
tests/TestCase/Controller/Component/CookieComponentTest.php

@@ -106,9 +106,9 @@ class CookieComponentTest extends TestCase {
  */
  */
 	protected function _setCookieData() {
 	protected function _setCookieData() {
 		$this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')));
 		$this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')));
-		$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!'));
+		$this->Cookie->write(array('Encrypted_multi_cookies.name' => 'CakePHP'));
+		$this->Cookie->write(array('Encrypted_multi_cookies.version' => '1.2.0.x'));
+		$this->Cookie->write(array('Encrypted_multi_cookies.tag' => 'CakePHP Rocks!'));
 
 
 		$this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false);
 		$this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false);
 		$this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false);
 		$this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false);
@@ -142,7 +142,7 @@ class CookieComponentTest extends TestCase {
 		$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
 		$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
 		$this->assertEquals($expected, $data);
 		$this->assertEquals($expected, $data);
 
 
-		$data = $this->Cookie->read('Encrytped_multi_cookies');
+		$data = $this->Cookie->read('Encrypted_multi_cookies');
 		$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
 		$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
 		$this->assertEquals($expected, $data);
 		$this->assertEquals($expected, $data);
 	}
 	}
@@ -311,59 +311,39 @@ class CookieComponentTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testDeleteHttpOnly() {
 	public function testDeleteHttpOnly() {
-		$this->markTestIncomplete();
 		$this->Cookie->config([
 		$this->Cookie->config([
 			'httpOnly' => true,
 			'httpOnly' => true,
 			'secure' => false
 			'secure' => false
 		]);
 		]);
-		$this->Cookie->delete('Testing', false);
+		$this->Cookie->delete('Testing');
 		$expected = array(
 		$expected = array(
-			'name' => $this->Cookie->config('name') . '[Testing]',
+			'name' => 'Testing',
 			'value' => '',
 			'value' => '',
 			'expire' => time() - 42000,
 			'expire' => time() - 42000,
 			'path' => '/',
 			'path' => '/',
 			'domain' => '',
 			'domain' => '',
 			'secure' => false,
 			'secure' => false,
 			'httpOnly' => true);
 			'httpOnly' => true);
-		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]');
+		$result = $this->Controller->response->cookie('Testing');
 		$this->assertEquals($expected, $result);
 		$this->assertEquals($expected, $result);
 	}
 	}
 
 
 /**
 /**
- * testWritePlainCookieArray
- *
- * @return void
- */
-	public function testWritePlainCookieArray() {
-		$this->markTestIncomplete();
-		$this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false);
-
-		$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'));
-
-		$this->Cookie->delete('name');
-		$this->Cookie->delete('version');
-		$this->Cookie->delete('tag');
-	}
-
-/**
  * test writing values that are not scalars
  * test writing values that are not scalars
  *
  *
  * @return void
  * @return void
  */
  */
 	public function testWriteArrayValues() {
 	public function testWriteArrayValues() {
-		$this->markTestIncomplete();
-		$this->Cookie->config('secure', false);
-		$this->Cookie->write('Testing', array(1, 2, 3), false);
+		$this->Cookie->write('Testing', array(1, 2, 3));
 		$expected = array(
 		$expected = array(
-			'name' => $this->Cookie->config('name') . '[Testing]',
+			'name' => 'Testing',
 			'value' => '[1,2,3]',
 			'value' => '[1,2,3]',
 			'path' => '/',
 			'path' => '/',
 			'domain' => '',
 			'domain' => '',
 			'secure' => false,
 			'secure' => false,
-			'httpOnly' => false);
-		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]');
+			'httpOnly' => false
+		);
+		$result = $this->Controller->response->cookie('Testing');
 
 
 		$this->assertWithinMargin($result['expire'], time() + 10, 1);
 		$this->assertWithinMargin($result['expire'], time() + 10, 1);
 		unset($result['expire']);
 		unset($result['expire']);
@@ -376,19 +356,17 @@ class CookieComponentTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testWriteMixedArray() {
 	public function testWriteMixedArray() {
-		$this->markTestIncomplete();
-		$this->Cookie->config('encrypt', false);
 		$this->Cookie->write('User', array('name' => 'mark'), false);
 		$this->Cookie->write('User', array('name' => 'mark'), false);
 		$this->Cookie->write('User.email', 'mark@example.com', false);
 		$this->Cookie->write('User.email', 'mark@example.com', false);
 		$expected = array(
 		$expected = array(
-			'name' => $this->Cookie->config('name') . '[User]',
+			'name' => 'User',
 			'value' => '{"name":"mark","email":"mark@example.com"}',
 			'value' => '{"name":"mark","email":"mark@example.com"}',
 			'path' => '/',
 			'path' => '/',
 			'domain' => '',
 			'domain' => '',
 			'secure' => false,
 			'secure' => false,
 			'httpOnly' => false
 			'httpOnly' => false
 		);
 		);
-		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[User]');
+		$result = $this->Controller->response->cookie('User');
 		unset($result['expire']);
 		unset($result['expire']);
 
 
 		$this->assertEquals($expected, $result);
 		$this->assertEquals($expected, $result);
@@ -396,14 +374,14 @@ class CookieComponentTest extends TestCase {
 		$this->Cookie->write('User.email', 'mark@example.com', false);
 		$this->Cookie->write('User.email', 'mark@example.com', false);
 		$this->Cookie->write('User', array('name' => 'mark'), false);
 		$this->Cookie->write('User', array('name' => 'mark'), false);
 		$expected = array(
 		$expected = array(
-			'name' => $this->Cookie->config('name') . '[User]',
+			'name' => 'User',
 			'value' => '{"name":"mark"}',
 			'value' => '{"name":"mark"}',
 			'path' => '/',
 			'path' => '/',
 			'domain' => '',
 			'domain' => '',
 			'secure' => false,
 			'secure' => false,
 			'httpOnly' => false
 			'httpOnly' => false
 		);
 		);
-		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[User]');
+		$result = $this->Controller->response->cookie('User');
 		unset($result['expire']);
 		unset($result['expire']);
 
 
 		$this->assertEquals($expected, $result);
 		$this->assertEquals($expected, $result);
@@ -422,7 +400,7 @@ class CookieComponentTest extends TestCase {
 				'name' => 'CakePHP',
 				'name' => 'CakePHP',
 				'version' => '1.2.0.x',
 				'version' => '1.2.0.x',
 				'tag' => 'CakePHP Rocks!'),
 				'tag' => 'CakePHP Rocks!'),
-			'Encrytped_multi_cookies' => array(
+			'Encrypted_multi_cookies' => array(
 				'name' => 'CakePHP',
 				'name' => 'CakePHP',
 				'version' => '1.2.0.x',
 				'version' => '1.2.0.x',
 				'tag' => 'CakePHP Rocks!'),
 				'tag' => 'CakePHP Rocks!'),
@@ -443,10 +421,9 @@ class CookieComponentTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testDeleteCookieValue() {
 	public function testDeleteCookieValue() {
-		$this->markTestIncomplete();
 		$this->_setCookieData();
 		$this->_setCookieData();
-		$this->Cookie->delete('Encrytped_multi_cookies.name');
-		$data = $this->Cookie->read('Encrytped_multi_cookies');
+		$this->Cookie->delete('Encrypted_multi_cookies.name');
+		$data = $this->Cookie->read('Encrypted_multi_cookies');
 		$expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
 		$expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
 		$this->assertEquals($expected, $data);
 		$this->assertEquals($expected, $data);
 
 
@@ -484,15 +461,15 @@ class CookieComponentTest extends TestCase {
 		$expected = 'CakePHP Rocks!';
 		$expected = 'CakePHP Rocks!';
 		$this->assertEquals($expected, $data);
 		$this->assertEquals($expected, $data);
 
 
-		$data = $this->Cookie->read('Encrytped_multi_cookies.name');
+		$data = $this->Cookie->read('Encrypted_multi_cookies.name');
 		$expected = 'CakePHP';
 		$expected = 'CakePHP';
 		$this->assertEquals($expected, $data);
 		$this->assertEquals($expected, $data);
 
 
-		$data = $this->Cookie->read('Encrytped_multi_cookies.version');
+		$data = $this->Cookie->read('Encrypted_multi_cookies.version');
 		$expected = '1.2.0.x';
 		$expected = '1.2.0.x';
 		$this->assertEquals($expected, $data);
 		$this->assertEquals($expected, $data);
 
 
-		$data = $this->Cookie->read('Encrytped_multi_cookies.tag');
+		$data = $this->Cookie->read('Encrypted_multi_cookies.tag');
 		$expected = 'CakePHP Rocks!';
 		$expected = 'CakePHP Rocks!';
 		$this->assertEquals($expected, $data);
 		$this->assertEquals($expected, $data);
 
 
@@ -661,8 +638,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testDeleteRemovesChildren() {
 	public function testDeleteRemovesChildren() {
-		$this->markTestIncomplete();
-		$this->request->cookies['CakeTestCookie'] = array(
+		$this->request->cookies = array(
 			'User' => array('email' => 'example@example.com', 'name' => 'mark'),
 			'User' => array('email' => 'example@example.com', 'name' => 'mark'),
 			'other' => 'value'
 			'other' => 'value'
 		);
 		);
@@ -670,7 +646,11 @@ class CookieComponentTest extends TestCase {
 
 
 		$this->Cookie->delete('User');
 		$this->Cookie->delete('User');
 		$this->assertNull($this->Cookie->read('User.email'));
 		$this->assertNull($this->Cookie->read('User.email'));
-		$this->Cookie->destroy();
+		$this->assertNull($this->Cookie->read('User.name'));
+
+		$result = $this->Controller->response->cookie('User');
+		$this->assertEquals('', $result['value']);
+		$this->assertLessThan(time(), $result['expire']);
 	}
 	}
 
 
 /**
 /**
@@ -679,7 +659,6 @@ class CookieComponentTest extends TestCase {
  * @return void
  * @return void
  */
  */
 	public function testDeleteChildrenNotExist() {
 	public function testDeleteChildrenNotExist() {
-		$this->markTestIncomplete();
 		$this->assertNull($this->Cookie->delete('NotFound'));
 		$this->assertNull($this->Cookie->delete('NotFound'));
 		$this->assertNull($this->Cookie->delete('Not.Found'));
 		$this->assertNull($this->Cookie->delete('Not.Found'));
 	}
 	}