Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement $request->is('requested');

Add tests and new detector type for request parameters
Fixes #1995
  • Loading branch information...
commit eb17653965b8119284b5e9629ae50834923265f9 1 parent 53bc963
@markstory markstory authored
View
14 lib/Cake/Network/CakeRequest.php
@@ -104,7 +104,8 @@ class CakeRequest implements ArrayAccess {
'J2ME', 'MIDP', 'NetFront', 'Nokia', 'Opera Mini', 'PalmOS', 'PalmSource',
'portalmmm', 'Plucker', 'ReqwirelessWeb', 'SonyEricsson', 'Symbian', 'UP\\.Browser',
'webOS', 'Windows CE', 'Xiino'
- ))
+ )),
+ 'requested' => array('param' => 'requested', 'value' => 1)
);
/**
@@ -435,6 +436,11 @@ public function is($type) {
return (bool)preg_match($pattern, env($detect['env']));
}
}
+ if (isset($detect['param'])) {
+ $key = $detect['param'];
+ $value = $detect['value'];
+ return isset($this->params[$key]) ? $this->params[$key] == $value : false;
+ }
if (isset($detect['callback']) && is_callable($detect['callback'])) {
return call_user_func($detect['callback'], $this);
}
@@ -472,6 +478,12 @@ public function is($type) {
*
* e.g `addDetector('custom', array('callback' => array('SomeClass', 'somemethod')));`
*
+ * ### Request parameter detectors
+ *
+ * Allows for custom detectors on the request parameters.
+ *
+ * e.g `addDetector('post', array('param' => 'requested', 'value' => 1)`
+ *
* @param string $name The name of the detector.
* @param array $options The options for the detector definition. See above.
* @return void
View
33 lib/Cake/Test/Case/Network/CakeRequestTest.php
@@ -744,6 +744,13 @@ public function testAddDetector() {
$request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, '_detectCallback')));
+ $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
+ $request->params['action'] = 'index';
+ $this->assertTrue($request->isIndex());
+
+ $request->params['action'] = 'add';
+ $this->assertFalse($request->isIndex());
+
$request->return = true;
$this->assertTrue($request->isCallMe());
@@ -1566,6 +1573,32 @@ public function testInputDecodeExtraParams() {
}
/**
+ * Test is('requested') and isRequested()
+ *
+ * @return void
+ */
+ public function testIsRequested() {
+ $request = new CakeRequest('/posts/index');
+ $request->addParams(array(
+ 'controller' => 'posts',
+ 'action' => 'index',
+ 'plugin' => null,
+ 'requested' => 1
+ ));
+ $this->assertTrue($request->is('requested'));
+ $this->assertTrue($request->isRequested());
+
+ $request = new CakeRequest('/posts/index');
+ $request->addParams(array(
+ 'controller' => 'posts',
+ 'action' => 'index',
+ 'plugin' => null,
+ ));
+ $this->assertFalse($request->is('requested'));
+ $this->assertFalse($request->isRequested());
+ }
+
+/**
* loadEnvironment method
*
* @param mixed $env
Please sign in to comment.
Something went wrong with that request. Please try again.