Browse Source

Start hacking on read() function and trying to get it working.

mark_story 12 years ago
parent
commit
82c1443ee1

+ 31 - 126
src/Controller/Component/CookieComponent.php

@@ -38,9 +38,7 @@ class CookieComponent extends Component {
 /**
  * Default config
  *
- * - `name` - The name of the cookie.
- * - `time` - The time a cookie will remain valid. Can be either integer
- *   unix timestamp or a date string.
+ * - `expires` - How long the cookies should last for by default.
  * - `path` - The path on the server in which the cookie will be available on.
  *   If path is set to '/foo/', the cookie will only be available within the
  *   /foo/ directory and all sub-directories such as /foo/bar/ of domain.
@@ -58,14 +56,13 @@ class CookieComponent extends Component {
  * @var array
  */
 	protected $_defaultConfig = [
-		'name' => 'CakeCookie',
-		'time' => null,
 		'path' => '/',
 		'domain' => '',
 		'secure' => false,
 		'key' => null,
 		'httpOnly' => false,
-		'encryption' => 'aes'
+		'encryption' => 'aes',
+		'expires' => null,
 	];
 
 /**
@@ -115,10 +112,6 @@ class CookieComponent extends Component {
 			$this->config('key', Configure::read('Security.salt'));
 		}
 
-		if ($this->_config['time']) {
-			$this->_expire($this->_config['time']);
-		}
-
 		$controller = $collection->getController();
 		if ($controller && isset($controller->request)) {
 			$this->_request = $controller->request;
@@ -155,34 +148,17 @@ class CookieComponent extends Component {
 	}
 
 /**
- * Start CookieComponent for use in the controller
- *
- * @param Event $event An Event instance
- * @return void
- */
-	public function startup(Event $event) {
-		$this->_expire($this->_config['time']);
-
-		$this->_values[$this->_config['name']] = array();
-	}
-
-/**
  * Events supported by this component.
  *
  * @return array
  */
 	public function implementedEvents() {
-		return [
-			'Controller.startup' => 'startup',
-		];
+		return [];
 	}
 
 /**
  * Write a value to the $_COOKIE[$key];
  *
- * Optional [Name.], required key, optional $value, optional $encrypt, optional $expires
- * $this->Cookie->write('[Name.]key, $value);
- *
  * By default all values are encrypted.
  * You must pass $encrypt false to store values in clear test
  *
@@ -191,89 +167,53 @@ class CookieComponent extends Component {
  *
  * @param string|array $key Key for the value
  * @param mixed $value Value
- * @param bool $encrypt Set to true to encrypt value, false otherwise
- * @param int|string $expires Can be either the number of seconds until a cookie
- *   expires, or a strtotime compatible time offset.
  * @return void
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::write
  */
-	public function write($key, $value = null, $encrypt = true, $expires = null) {
-		$cookieName = $this->config('name');
-		if (empty($this->_values[$cookieName])) {
-			$this->read();
-		}
-
-		if ($encrypt === null) {
-			$encrypt = true;
+	public function write($key, $value = null) {
+		if (empty($this->_values)) {
+			$this->_load();
 		}
-		$this->_encrypted = $encrypt;
-		$this->_expire($expires);
 
 		if (!is_array($key)) {
 			$key = array($key => $value);
 		}
 
 		foreach ($key as $name => $value) {
-			$names = array($name);
-			if (strpos($name, '.') !== false) {
-				$names = explode('.', $name, 2);
-			}
-			$firstName = $names[0];
-			$isMultiValue = (is_array($value) || count($names) > 1);
-
-			if (!isset($this->_values[$cookieName][$firstName]) && $isMultiValue) {
-				$this->_values[$cookieName][$firstName] = array();
-			}
-
-			if (count($names) > 1) {
-				$this->_values[$cookieName][$firstName] = Hash::insert(
-					$this->_values[$cookieName][$firstName],
-					$names[1],
-					$value
-				);
-			} else {
-				$this->_values[$cookieName][$firstName] = $value;
-			}
-			$this->_write('[' . $firstName . ']', $this->_values[$cookieName][$firstName]);
+			$this->_values = Hash::insert($this->_values, $name, $value);
 		}
-		$this->_encrypted = true;
+		// TODO write cookie data out.
 	}
 
 /**
- * Read the value of the $_COOKIE[$key];
- *
- * Optional [Name.], required key
- * $this->Cookie->read(Name.key);
+ * Read the value of key path from request cookies.
  *
  * @param string $key Key of the value to be obtained. If none specified, obtain map key => values
  * @return string or null, value for specified key
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::read
  */
 	public function read($key = null) {
-		$cookieName = $this->config('name');
-		$values = $this->_request->cookie($cookieName);
-		if (empty($this->_values[$cookieName]) && $values) {
-			$this->_values[$cookieName] = $this->_decrypt($values);
-		}
-		if (empty($this->_values[$cookieName])) {
-			$this->_values[$cookieName] = array();
+		if (empty($this->_values)) {
+			$this->_load();
 		}
 		if ($key === null) {
-			return $this->_values[$cookieName];
-		}
-
-		if (strpos($key, '.') !== false) {
-			$names = explode('.', $key, 2);
-			$key = $names[0];
-		}
-		if (!isset($this->_values[$cookieName][$key])) {
-			return null;
+			return $this->_values;
 		}
+		return Hash::get($this->_values, $key);
+	}
 
-		if (!empty($names[1])) {
-			return Hash::get($this->_values[$cookieName][$key], $names[1]);
+/**
+ * Load the cookie data from the request and response objects.
+ *
+ * Based on the configuration data, cookies will be decrypted. When cookies
+ * contain array data, that data will be expanded.
+ *
+ * @return void
+ */
+	protected function _load() {
+		foreach ($this->_request->cookies as $name => $value) {
+			$this->_values[$name] = $this->_decrypt($value);
 		}
-		return $this->_values[$cookieName][$key];
 	}
 
 /**
@@ -292,9 +232,6 @@ class CookieComponent extends Component {
 /**
  * Delete a cookie value
  *
- * Optional [Name.], required key
- * $this->Cookie->delete('Name.key);
- *
  * You must use this method before any output is sent to the browser.
  * Failure to do so will result in header already sent errors.
  *
@@ -381,36 +318,6 @@ class CookieComponent extends Component {
 	}
 
 /**
- * Set the expire time for a session variable.
- *
- * Creates a new expire time for a session variable.
- * $expire can be either integer Unix timestamp or a date string.
- *
- * Used by write()
- * CookieComponent::write(string, string, boolean, 8400);
- * CookieComponent::write(string, string, boolean, '5 Days');
- *
- * @param int|string $expires Can be either Unix timestamp, or date string
- * @return int Unix timestamp
- */
-	protected function _expire($expires = null) {
-		if ($expires === null) {
-			return $this->_expires;
-		}
-		$this->_reset = $this->_expires;
-		if (!$expires) {
-			return $this->_expires = 0;
-		}
-		$now = new \DateTime();
-
-		if (is_int($expires) || is_numeric($expires)) {
-			return $this->_expires = $now->format('U') + intval($expires);
-		}
-		$now->modify($expires);
-		return $this->_expires = $now->format('U');
-	}
-
-/**
  * Set cookie
  *
  * @param string $name Name for cookie
@@ -419,20 +326,18 @@ class CookieComponent extends Component {
  */
 	protected function _write($name, $value) {
 		$config = $this->config();
+
+		$expires = new \DateTime($config['expires']);
+
 		$this->_response->cookie(array(
-			'name' => $config['name'] . $name,
+			'name' => $name,
 			'value' => $this->_encrypt($value),
-			'expire' => $this->_expires,
+			'expire' => $expires->format('U'),
 			'path' => $config['path'],
 			'domain' => $config['domain'],
 			'secure' => $config['secure'],
 			'httpOnly' => $config['httpOnly']
 		));
-
-		if (!empty($this->_reset)) {
-			$this->_expires = $this->_reset;
-			$this->_reset = null;
-		}
 	}
 
 /**

+ 33 - 40
tests/TestCase/Controller/Component/CookieComponentTest.php

@@ -47,26 +47,12 @@ class CookieComponentTest extends TestCase {
 		$this->request = $controller->request;
 
 		$this->Cookie->config([
-			'name' => 'CakeTestCookie',
-			'time' => 10,
+			'expires' => '+10 seconds',
 			'path' => '/',
 			'domain' => '',
 			'secure' => false,
 			'key' => 'somerandomhaskeysomerandomhaskey'
 		]);
-
-		$event = new Event('Controller.startup', $this->Controller);
-		$this->Cookie->startup($event);
-	}
-
-/**
- * end
- *
- * @return void
- */
-	public function tearDown() {
-		parent::tearDown();
-		$this->Cookie->destroy();
 	}
 
 /**
@@ -85,8 +71,6 @@ class CookieComponentTest extends TestCase {
 			'secure' => false,
 			'httpOnly' => false,
 			'encryption' => 'aes',
-			'name' => 'CakeTestCookie',
-			'time' => 10,
 		];
 		$this->assertEquals($expected, $result);
 	}
@@ -110,8 +94,6 @@ class CookieComponentTest extends TestCase {
 			'secure' => false,
 			'httpOnly' => false,
 			'encryption' => 'aes',
-			'name' => 'CakeTestCookie',
-			'time' => 10,
 		];
 		$this->assertEquals($expected, $result);
 	}
@@ -149,20 +131,12 @@ class CookieComponentTest extends TestCase {
 	}
 
 /**
- * testCookieName
- *
- * @return void
- */
-	public function testCookieName() {
-		$this->assertEquals('CakeTestCookie', $this->Cookie->config('name'));
-	}
-
-/**
  * testReadEncryptedCookieData
  *
  * @return void
  */
 	public function testReadEncryptedCookieData() {
+		$this->markTestIncomplete();
 		$this->_setCookieData();
 		$data = $this->Cookie->read('Encrytped_array');
 		$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
@@ -179,6 +153,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testReadPlainCookieData() {
+		$this->markTestIncomplete();
 		$this->_setCookieData();
 		$data = $this->Cookie->read('Plain_array');
 		$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
@@ -194,19 +169,18 @@ class CookieComponentTest extends TestCase {
  *
  * @return void
  */
-	public function testReadWithNameSwitch() {
+	public function testReadMultipleNames() {
 		$this->request->cookies = array(
-			'CakeTestCookie' => array(
+			'CakeCookie' => array(
 				'key' => 'value'
 			),
-			'OtherTestCookie' => array(
+			'OtherCookie' => array(
 				'key' => 'other value'
 			)
 		);
-		$this->assertEquals('value', $this->Cookie->read('key'));
-
-		$this->Cookie->config('name', 'OtherTestCookie');
-		$this->assertEquals('other value', $this->Cookie->read('key'));
+		$this->assertEquals('value', $this->Cookie->read('CakeCookie.key'));
+		$this->assertEquals(['key' => 'value'], $this->Cookie->read('CakeCookie'));
+		$this->assertEquals('other value', $this->Cookie->read('OtherCookie.key'));
 	}
 
 /**
@@ -215,6 +189,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteSimple() {
+		$this->markTestIncomplete();
 		$this->Cookie->write('Testing', 'value');
 		$result = $this->Cookie->read('Testing');
 
@@ -227,6 +202,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteWithFalseyValue() {
+		$this->markTestIncomplete();
 		$this->Cookie->encryption('aes');
 		$this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
 
@@ -261,6 +237,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteMultipleShareExpiry() {
+		$this->markTestIncomplete();
 		$this->Cookie->write('key1', 'value1', false);
 		$this->Cookie->write('key2', 'value2', false);
 
@@ -279,6 +256,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteFarFuture() {
+		$this->markTestIncomplete();
 		$this->Cookie->write('Testing', 'value', false, '+90 years');
 		$future = new \DateTime('now');
 		$future->modify('+90 years');
@@ -304,6 +282,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteHttpOnly() {
+		$this->markTestIncomplete();
 		$this->Cookie->config([
 			'httpOnly' => true,
 			'secure' => false
@@ -327,6 +306,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testDeleteHttpOnly() {
+		$this->markTestIncomplete();
 		$this->Cookie->config([
 			'httpOnly' => true,
 			'secure' => false
@@ -350,6 +330,7 @@ class CookieComponentTest extends TestCase {
  * @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'));
@@ -367,6 +348,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteArrayValues() {
+		$this->markTestIncomplete();
 		$this->Cookie->config('secure', false);
 		$this->Cookie->write('Testing', array(1, 2, 3), false);
 		$expected = array(
@@ -389,6 +371,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteMixedArray() {
+		$this->markTestIncomplete();
 		$this->Cookie->config('encrypt', false);
 		$this->Cookie->write('User', array('name' => 'mark'), false);
 		$this->Cookie->write('User.email', 'mark@example.com', false);
@@ -422,11 +405,11 @@ class CookieComponentTest extends TestCase {
 	}
 
 /**
- * testReadingCookieValue
+ * test reading all values at once.
  *
  * @return void
  */
-	public function testReadingCookieValue() {
+	public function testReadingAllValues() {
 		$this->_setCookieData();
 		$data = $this->Cookie->read();
 		$expected = array(
@@ -455,6 +438,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testDeleteCookieValue() {
+		$this->markTestIncomplete();
 		$this->_setCookieData();
 		$this->Cookie->delete('Encrytped_multi_cookies.name');
 		$data = $this->Cookie->read('Encrytped_multi_cookies');
@@ -537,7 +521,7 @@ class CookieComponentTest extends TestCase {
  *
  * @return void
  */
-	public function testReadingCookieDataOnStartup() {
+	public function testReadingDataFromRequest() {
 		$data = $this->Cookie->read('Encrytped_array');
 		$this->assertNull($data);
 
@@ -550,7 +534,7 @@ class CookieComponentTest extends TestCase {
 		$data = $this->Cookie->read('Plain_multi_cookies');
 		$this->assertNull($data);
 
-		$this->request->cookies['CakeTestCookie'] = array(
+		$this->request->cookies = array(
 			'Encrytped_array' => $this->_encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
 			'Encrytped_multi_cookies' => array(
 				'name' => $this->_encrypt('CakePHP'),
@@ -580,7 +564,6 @@ class CookieComponentTest extends TestCase {
 		$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();
 	}
 
 /**
@@ -589,6 +572,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testReadingCookieDataWithoutStartup() {
+		$this->markTestIncomplete();
 		$data = $this->Cookie->read('Encrytped_array');
 		$expected = null;
 		$this->assertEquals($expected, $data);
@@ -644,6 +628,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testReadLegacyCookieValue() {
+		$this->markTestIncomplete();
 		$this->request->cookies['CakeTestCookie'] = array(
 			'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
 		);
@@ -658,6 +643,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testReadEmpty() {
+		$this->markTestIncomplete();
 		$this->request->cookies['CakeTestCookie'] = array(
 			'JSON' => '{"name":"value"}',
 			'Empty' => '',
@@ -677,6 +663,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testNoErrorOnNonArrayData() {
+		$this->markTestIncomplete();
 		$this->request->cookies['CakeTestCookie'] = 'kaboom';
 
 		$this->assertNull($this->Cookie->read('value'));
@@ -688,6 +675,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testCheck() {
+		$this->markTestIncomplete();
 		$this->Cookie->write('CookieComponentTestCase', 'value');
 		$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
 
@@ -700,6 +688,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testCheckingSavedEmpty() {
+		$this->markTestIncomplete();
 		$this->Cookie->write('CookieComponentTestCase', 0);
 		$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
 
@@ -713,6 +702,7 @@ class CookieComponentTest extends TestCase {
  * @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');
@@ -727,6 +717,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testCheckEmpty() {
+		$this->markTestIncomplete();
 		$this->assertFalse($this->Cookie->check());
 	}
 
@@ -736,6 +727,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testDeleteRemovesChildren() {
+		$this->markTestIncomplete();
 		$this->request->cookies['CakeTestCookie'] = array(
 			'User' => array('email' => 'example@example.com', 'name' => 'mark'),
 			'other' => 'value'
@@ -753,6 +745,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testDeleteChildrenNotExist() {
+		$this->markTestIncomplete();
 		$this->assertNull($this->Cookie->delete('NotFound'));
 		$this->assertNull($this->Cookie->delete('Not.Found'));
 	}