Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #843 from dereuromark/2.3-ticket-3172

check() for CookieComponent and Configure
  • Loading branch information...
commit 89c98233b594e1af2383b5ee86d635f5d5a12605 2 parents a5481f1 + 6d3e0a2
Mark Story markstory authored
13 lib/Cake/Controller/Component/CookieComponent.php
View
@@ -282,6 +282,19 @@ public function read($key = null) {
}
/**
+ * Returns true if given variable is set in cookie.
+ *
+ * @param string $var Variable name to check for
+ * @return boolean True if variable is there
+ */
+ public function check($key = null) {
+ if (empty($key)) {
+ return false;
+ }
+ return $this->read($key) !== null;
+ }
+
+/**
* Delete a cookie value
*
* Optional [Name.], required key
13 lib/Cake/Core/Configure.php
View
@@ -171,6 +171,19 @@ public static function read($var = null) {
}
/**
+ * Returns true if given variable is set in Configure.
+ *
+ * @param string $var Variable name to check for
+ * @return boolean True if variable is there
+ */
+ public static function check($var = null) {
+ if (empty($var)) {
+ return false;
+ }
+ return Hash::get(self::$_values, $var) !== null;
+ }
+
+/**
* Used to delete a variable from Configure.
*
* Usage:
6 lib/Cake/Model/Datasource/CakeSession.php
View
@@ -218,8 +218,7 @@ public static function check($name = null) {
if (empty($name)) {
return false;
}
- $result = Hash::get($_SESSION, $name);
- return isset($result);
+ return Hash::get($_SESSION, $name) !== null;
}
/**
@@ -283,9 +282,8 @@ protected static function _overwrite(&$old, $new) {
protected static function _error($errorNumber) {
if (!is_array(self::$error) || !array_key_exists($errorNumber, self::$error)) {
return false;
- } else {
- return self::$error[$errorNumber];
}
+ return self::$error[$errorNumber];
}
/**
54 lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php
View
@@ -537,6 +537,60 @@ public function testNoErrorOnNonArrayData() {
}
/**
+ * testCheck method
+ *
+ * @return void
+ */
+ public function testCheck() {
+ $this->Cookie->write('CookieComponentTestCase', 'value');
+ $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
+
+ $this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
+ }
+
+/**
+ * testCheckingSavedEmpty method
+ *
+ * @return void
+ */
+ public function testCheckingSavedEmpty() {
+ $this->Cookie->write('CookieComponentTestCase', 0);
+ $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
+
+ $this->Cookie->write('CookieComponentTestCase', '0');
+ $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
+
+ $this->Cookie->write('CookieComponentTestCase', false);
+ $this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
+
+ $this->Cookie->write('CookieComponentTestCase', null);
+ $this->assertFalse($this->Cookie->check('CookieComponentTestCase'));
+ }
+
+/**
+ * testCheckKeyWithSpaces method
+ *
+ * @return void
+ */
+ public function testCheckKeyWithSpaces() {
+ $this->Cookie->write('CookieComponent Test', "test");
+ $this->assertTrue($this->Cookie->check('CookieComponent Test'));
+ $this->Cookie->delete('CookieComponent Test');
+
+ $this->Cookie->write('CookieComponent Test.Test Case', "test");
+ $this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
+ }
+
+/**
+ * testCheckEmpty
+ *
+ * @return void
+ */
+ public function testCheckEmpty() {
+ $this->assertFalse($this->Cookie->check());
+ }
+
+/**
* test that deleting a top level keys kills the child elements too.
*
* @return void
54 lib/Cake/Test/Case/Core/ConfigureTest.php
View
@@ -178,6 +178,60 @@ public function testDelete() {
}
/**
+ * testCheck method
+ *
+ * @return void
+ */
+ public function testCheck() {
+ Configure::write('ConfigureTestCase', 'value');
+ $this->assertTrue(Configure::check('ConfigureTestCase'));
+
+ $this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
+ }
+
+/**
+ * testCheckingSavedEmpty method
+ *
+ * @return void
+ */
+ public function testCheckingSavedEmpty() {
+ $this->assertTrue(Configure::write('ConfigureTestCase', 0));
+ $this->assertTrue(Configure::check('ConfigureTestCase'));
+
+ $this->assertTrue(Configure::write('ConfigureTestCase', '0'));
+ $this->assertTrue(Configure::check('ConfigureTestCase'));
+
+ $this->assertTrue(Configure::write('ConfigureTestCase', false));
+ $this->assertTrue(Configure::check('ConfigureTestCase'));
+
+ $this->assertTrue(Configure::write('ConfigureTestCase', null));
+ $this->assertFalse(Configure::check('ConfigureTestCase'));
+ }
+
+/**
+ * testCheckKeyWithSpaces method
+ *
+ * @return void
+ */
+ public function testCheckKeyWithSpaces() {
+ $this->assertTrue(Configure::write('Configure Test', "test"));
+ $this->assertTrue(Configure::check('Configure Test'));
+ Configure::delete('Configure Test');
+
+ $this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
+ $this->assertTrue(Configure::check('Configure Test.Test Case'));
+ }
+
+/**
+ * testCheckEmpty
+ *
+ * @return void
+ */
+ public function testCheckEmpty() {
+ $this->assertFalse(Configure::check());
+ }
+
+/**
* testLoad method
*
* @expectedException RuntimeException
2  lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php
View
@@ -231,7 +231,7 @@ public function testCheck() {
TestCakeSession::write('SessionTestCase', 'value');
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
- $this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'), false);
+ $this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'));
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.