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

Add tests and new detector type for request parameters
Fixes #1995
This commit is contained in:
mark_story 2011-10-20 21:11:42 -04:00
parent 53bc963315
commit eb17653965
2 changed files with 46 additions and 1 deletions

View file

@ -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 @@ class CakeRequest implements ArrayAccess {
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 @@ class CakeRequest implements ArrayAccess {
*
* 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 file

@ -744,6 +744,13 @@ class CakeRequestTest extends CakeTestCase {
$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());
@ -1565,6 +1572,32 @@ XML;
);
}
/**
* 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
*