Skip to content
This repository
Browse code

check() for CookieComponent and Configure (similar to `CakeSession::c…

…heck()`)
  • Loading branch information...
commit 2170d87488017f3eb719289fcf11af07ee223f46 1 parent eb33929
Mark authored
14  lib/Cake/Controller/Component/CookieComponent.php
@@ -282,6 +282,20 @@ public function read($key = null) {
282 282
 	}
283 283
 
284 284
 /**
  285
+ * Returns true if given variable is set in cookie.
  286
+ *
  287
+ * @param string $var Variable name to check for
  288
+ * @return boolean True if variable is there
  289
+ */
  290
+	public function check($key = null) {
  291
+		if (empty($key)) {
  292
+			return false;
  293
+		}
  294
+		$result = $this->read($key);
  295
+		return isset($result);
  296
+	}
  297
+
  298
+/**
285 299
  * Delete a cookie value
286 300
  *
287 301
  * Optional [Name.], required key
14  lib/Cake/Core/Configure.php
@@ -171,6 +171,20 @@ public static function read($var = null) {
171 171
 	}
172 172
 
173 173
 /**
  174
+ * Returns true if given variable is set in Configure.
  175
+ *
  176
+ * @param string $var Variable name to check for
  177
+ * @return boolean True if variable is there
  178
+ */
  179
+	public static function check($var = null) {
  180
+		if (empty($var)) {
  181
+			return false;
  182
+		}
  183
+		$result = Hash::get(self::$_values, $var);
  184
+		return isset($result);
  185
+	}
  186
+
  187
+/**
174 188
  * Used to delete a variable from Configure.
175 189
  *
176 190
  * Usage:
54  lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php
@@ -537,6 +537,60 @@ public function testNoErrorOnNonArrayData() {
537 537
 	}
538 538
 
539 539
 /**
  540
+ * testCheck method
  541
+ *
  542
+ * @return void
  543
+ */
  544
+	public function testCheck() {
  545
+		$this->Cookie->write('CookieComponentTestCase', 'value');
  546
+		$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  547
+
  548
+		$this->assertFalse($this->Cookie->check('NotExistingCookieComponentTestCase'));
  549
+	}
  550
+
  551
+/**
  552
+ * testCheckingSavedEmpty method
  553
+ *
  554
+ * @return void
  555
+ */
  556
+	public function testCheckingSavedEmpty() {
  557
+		$this->Cookie->write('CookieComponentTestCase', 0);
  558
+		$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  559
+
  560
+		$this->Cookie->write('CookieComponentTestCase', '0');
  561
+		$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  562
+
  563
+		$this->Cookie->write('CookieComponentTestCase', false);
  564
+		$this->assertTrue($this->Cookie->check('CookieComponentTestCase'));
  565
+
  566
+		$this->Cookie->write('CookieComponentTestCase', null);
  567
+		$this->assertFalse($this->Cookie->check('CookieComponentTestCase'));
  568
+	}
  569
+
  570
+/**
  571
+ * testCheckKeyWithSpaces method
  572
+ *
  573
+ * @return void
  574
+ */
  575
+	public function testCheckKeyWithSpaces() {
  576
+		$this->Cookie->write('CookieComponent Test', "test");
  577
+		$this->assertTrue($this->Cookie->check('CookieComponent Test'));
  578
+		$this->Cookie->delete('CookieComponent Test');
  579
+
  580
+		$this->Cookie->write('CookieComponent Test.Test Case', "test");
  581
+		$this->assertTrue($this->Cookie->check('CookieComponent Test.Test Case'));
  582
+	}
  583
+
  584
+/**
  585
+ * testCheckEmpty
  586
+ *
  587
+ * @return void
  588
+ */
  589
+	public function testCheckEmpty() {
  590
+		$this->assertFalse($this->Cookie->check());
  591
+	}
  592
+
  593
+/**
540 594
  * test that deleting a top level keys kills the child elements too.
541 595
  *
542 596
  * @return void
54  lib/Cake/Test/Case/Core/ConfigureTest.php
@@ -178,6 +178,60 @@ public function testDelete() {
178 178
 	}
179 179
 
180 180
 /**
  181
+ * testCheck method
  182
+ *
  183
+ * @return void
  184
+ */
  185
+	public function testCheck() {
  186
+		Configure::write('ConfigureTestCase', 'value');
  187
+		$this->assertTrue(Configure::check('ConfigureTestCase'));
  188
+
  189
+		$this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
  190
+	}
  191
+
  192
+/**
  193
+ * testCheckingSavedEmpty method
  194
+ *
  195
+ * @return void
  196
+ */
  197
+	public function testCheckingSavedEmpty() {
  198
+		$this->assertTrue(Configure::write('ConfigureTestCase', 0));
  199
+		$this->assertTrue(Configure::check('ConfigureTestCase'));
  200
+
  201
+		$this->assertTrue(Configure::write('ConfigureTestCase', '0'));
  202
+		$this->assertTrue(Configure::check('ConfigureTestCase'));
  203
+
  204
+		$this->assertTrue(Configure::write('ConfigureTestCase', false));
  205
+		$this->assertTrue(Configure::check('ConfigureTestCase'));
  206
+
  207
+		$this->assertTrue(Configure::write('ConfigureTestCase', null));
  208
+		$this->assertFalse(Configure::check('ConfigureTestCase'));
  209
+	}
  210
+
  211
+/**
  212
+ * testCheckKeyWithSpaces method
  213
+ *
  214
+ * @return void
  215
+ */
  216
+	public function testCheckKeyWithSpaces() {
  217
+		$this->assertTrue(Configure::write('Configure Test', "test"));
  218
+		$this->assertTrue(Configure::check('Configure Test'));
  219
+		Configure::delete('Configure Test');
  220
+
  221
+		$this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
  222
+		$this->assertTrue(Configure::check('Configure Test.Test Case'));
  223
+	}
  224
+
  225
+/**
  226
+ * testCheckEmpty
  227
+ *
  228
+ * @return void
  229
+ */
  230
+	public function testCheckEmpty() {
  231
+		$this->assertFalse(Configure::check());
  232
+	}
  233
+
  234
+/**
181 235
  * testLoad method
182 236
  *
183 237
  * @expectedException RuntimeException
2  lib/Cake/Test/Case/Model/Datasource/CakeSessionTest.php
@@ -231,7 +231,7 @@ public function testCheck() {
231 231
 		TestCakeSession::write('SessionTestCase', 'value');
232 232
 		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
233 233
 
234  
-		$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'), false);
  234
+		$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'));
235 235
 	}
236 236
 
237 237
 /**

0 notes on commit 2170d87

Please sign in to comment.
Something went wrong with that request. Please try again.