Marking a number of Helper properties as deprecated as they are contained within the CakeRequest object.

Adding a new property for the new request object.
Updating the internals of Helper to use the request object.
Updating the tests to use the request object.
This commit is contained in:
Mark Story 2010-05-14 00:05:45 -04:00
parent 4b637a0f70
commit 657073aa42
2 changed files with 52 additions and 50 deletions

View file

@ -39,6 +39,7 @@ class Helper extends Object {
/**
* Base URL
*
* @deprecated use $request->base instead
* @var string
*/
public $base = null;
@ -46,6 +47,7 @@ class Helper extends Object {
/**
* Webroot path
*
* @deprecated use $request->webroot instead
* @var string
*/
public $webroot = null;
@ -60,6 +62,7 @@ class Helper extends Object {
/**
* URL to current action.
*
* @deprecated use $request->here instead
* @var string
*/
public $here = null;
@ -67,13 +70,22 @@ class Helper extends Object {
/**
* Parameter array.
*
* @deprecated use $request instead
* @var array
*/
public $params = array();
/**
* Request object
*
* @var CakeRequest
*/
public $request = null;
/**
* Current action.
*
* @deprecated use $request->action instead
* @var string
*/
public $action = null;
@ -88,24 +100,11 @@ class Helper extends Object {
/**
* POST data for models
*
* @deprecated use $request->data instead
* @var array
*/
public $data = null;
/**
* List of named arguments
*
* @var array
*/
public $namedArgs = null;
/**
* URL argument separator character
*
* @var string
*/
public $argSeparator = null;
/**
* Contains model validation errors of form post-backs
*
@ -195,7 +194,7 @@ class Helper extends Object {
public function webroot($file) {
$asset = explode('?', $file);
$asset[1] = isset($asset[1]) ? '?' . $asset[1] : null;
$webPath = "{$this->webroot}" . $asset[0];
$webPath = "{$this->request->webroot}" . $asset[0];
$file = $asset[0];
if (!empty($this->theme)) {
@ -207,7 +206,7 @@ class Helper extends Object {
}
if (file_exists(Configure::read('App.www_root') . 'theme' . DS . $this->theme . DS . $file)) {
$webPath = "{$this->webroot}theme/" . $theme . $asset[0];
$webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
} else {
$viewPaths = App::path('views');
@ -215,7 +214,7 @@ class Helper extends Object {
$path = $viewPath . 'themed'. DS . $this->theme . DS . 'webroot' . DS . $file;
if (file_exists($path)) {
$webPath = "{$this->webroot}theme/" . $theme . $asset[0];
$webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
break;
}
}
@ -241,7 +240,7 @@ class Helper extends Object {
Configure::read('Asset.timestamp') === 'force'
);
if (strpos($path, '?') === false && $timestampEnabled) {
$filepath = preg_replace('/^' . preg_quote($this->webroot, '/') . '/', '', $path);
$filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', $path);
$path .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $filepath));
}
return $path;
@ -671,19 +670,20 @@ class Helper extends Object {
$view =& ClassRegistry::getObject('view');
$result = null;
$data = $this->request->data;
$entity = $view->entity();
if (!empty($this->data) && !empty($entity)) {
$result = Set::extract($this->data, join('.', $entity));
if (!empty($data) && !empty($entity)) {
$result = Set::extract($data, join('.', $entity));
}
$habtmKey = $this->field();
if (empty($result) && isset($this->data[$habtmKey][$habtmKey])) {
if (empty($result) && isset($data[$habtmKey][$habtmKey])) {
$result = $this->data[$habtmKey][$habtmKey];
} elseif (empty($result) && isset($this->data[$habtmKey]) && is_array($this->data[$habtmKey])) {
} elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {
if (ClassRegistry::isKeySet($habtmKey)) {
$model =& ClassRegistry::getObject($habtmKey);
$result = $this->__selectedArray($this->data[$habtmKey], $model->primaryKey);
$result = $this->__selectedArray($data[$habtmKey], $model->primaryKey);
}
}
@ -814,11 +814,11 @@ class Helper extends Object {
function __selectedArray($data, $key = 'id') {
if (!is_array($data)) {
$model = $data;
if (!empty($this->data[$model][$model])) {
return $this->data[$model][$model];
if (!empty($this->request->data[$model][$model])) {
return $this->request->data[$model][$model];
}
if (!empty($this->data[$model])) {
$data = $this->data[$model];
if (!empty($this->request->data[$model])) {
$data = $this->request->data[$model];
}
}
$array = array();

View file

@ -182,6 +182,8 @@ class HelperTest extends CakeTestCase {
$null = null;
$this->View = new View($null);
$this->Helper = new Helper();
$this->Helper->request = new CakeRequest(null, false);
ClassRegistry::addObject('HelperTestPost', new HelperTestPost());
ClassRegistry::addObject('HelperTestComment', new HelperTestComment());
ClassRegistry::addObject('HelperTestTag', new HelperTestTag());
@ -341,42 +343,42 @@ class HelperTest extends CakeTestCase {
* @return void
*/
function testValue() {
$this->Helper->data = array('fullname' => 'This is me');
$this->Helper->request->data = array('fullname' => 'This is me');
$this->Helper->setEntity('fullname');
$result = $this->Helper->value('fullname');
$this->assertEqual($result, 'This is me');
$this->Helper->data = array('Post' => array('name' => 'First Post'));
$this->Helper->request->data = array('Post' => array('name' => 'First Post'));
$this->Helper->setEntity('Post.name');
$result = $this->Helper->value('Post.name');
$this->assertEqual($result, 'First Post');
$this->Helper->data = array('Post' => array(2 => array('name' => 'First Post')));
$this->Helper->request->data = array('Post' => array(2 => array('name' => 'First Post')));
$this->Helper->setEntity('Post.2.name');
$result = $this->Helper->value('Post.2.name');
$this->assertEqual($result, 'First Post');
$this->Helper->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
$this->Helper->request->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
$this->Helper->setEntity('Post.2.created');
$result = $this->Helper->value('Post.2.created');
$this->assertEqual($result, array('year' => '2008'));
$this->Helper->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
$this->Helper->request->data = array('Post' => array(2 => array('created' => array('year' => '2008'))));
$this->Helper->setEntity('Post.2.created.year');
$result = $this->Helper->value('Post.2.created.year');
$this->assertEqual($result, '2008');
$this->Helper->data = array('HelperTestTag' => array('HelperTestTag' => ''));
$this->Helper->request->data = array('HelperTestTag' => array('HelperTestTag' => ''));
$this->Helper->setEntity('HelperTestTag.HelperTestTag');
$result = $this->Helper->value('HelperTestTag.HelperTestTag');
$this->assertEqual($result, '');
$this->Helper->data = array('HelperTestTag' => array('HelperTestTag' => array(2, 3, 4)));
$this->Helper->request->data = array('HelperTestTag' => array('HelperTestTag' => array(2, 3, 4)));
$this->Helper->setEntity('HelperTestTag.HelperTestTag');
$result = $this->Helper->value('HelperTestTag.HelperTestTag');
$this->assertEqual($result, array(2, 3, 4));
$this->Helper->data = array(
$this->Helper->request->data = array(
'HelperTestTag' => array(
array('id' => 3),
array('id' => 5)
@ -386,12 +388,12 @@ class HelperTest extends CakeTestCase {
$result = $this->Helper->value('HelperTestTag.HelperTestTag');
$this->assertEqual($result, array(3 => 3, 5 => 5));
$this->Helper->data = array('zero' => 0);
$this->Helper->request->data = array('zero' => 0);
$this->Helper->setEntity('zero');
$result = $this->Helper->value(array('default' => 'something'), 'zero');
$this->assertEqual($result, array('value' => 0));
$this->Helper->data = array('zero' => '0');
$this->Helper->request->data = array('zero' => '0');
$result = $this->Helper->value(array('default' => 'something'), 'zero');
$this->assertEqual($result, array('value' => '0'));
@ -467,7 +469,7 @@ class HelperTest extends CakeTestCase {
$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css?someparam');
$this->assertEqual($result, CSS_URL . 'cake.generic.css?someparam');
$this->Helper->webroot = '/some/dir/';
$this->Helper->request->webroot = '/some/dir/';
$result = $this->Helper->assetTimestamp('/some/dir/' . CSS_URL . 'cake.generic.css');
$this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
@ -568,27 +570,27 @@ class HelperTest extends CakeTestCase {
function testMulitDimensionValue() {
$this->Helper->data = array();
for ($i = 0; $i < 2; $i++) {
$this->Helper->data['Model'][$i] = 'what';
$this->Helper->request->data['Model'][$i] = 'what';
$result[] = $this->Helper->value("Model.{$i}");
$this->Helper->data['Model'][$i] = array();
$this->Helper->request->data['Model'][$i] = array();
for ($j = 0; $j < 2; $j++) {
$this->Helper->data['Model'][$i][$j] = 'how';
$this->Helper->request->data['Model'][$i][$j] = 'how';
$result[] = $this->Helper->value("Model.{$i}.{$j}");
}
}
$expected = array('what', 'how', 'how', 'what', 'how', 'how');
$this->assertEqual($result, $expected);
$this->Helper->data['HelperTestComment']['5']['id'] = 'ok';
$this->Helper->request->data['HelperTestComment']['5']['id'] = 'ok';
$result = $this->Helper->value('HelperTestComment.5.id');
$this->assertEqual($result, 'ok');
$this->Helper->setEntity('HelperTestPost', true);
$this->Helper->data['HelperTestPost']['5']['created']['month'] = '10';
$this->Helper->request->data['HelperTestPost']['5']['created']['month'] = '10';
$result = $this->Helper->value('5.created.month');
$this->assertEqual($result, 10);
$this->Helper->data['HelperTestPost']['0']['id'] = 100;
$this->Helper->request->data['HelperTestPost']['0']['id'] = 100;
$result = $this->Helper->value('0.id');
$this->assertEqual($result, 100);
}
@ -658,29 +660,29 @@ class HelperTest extends CakeTestCase {
$this->assertEqual($this->View->modelId,1);
$this->assertEqual($this->View->fieldSuffix,'year');
$this->Helper->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
$this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
$result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.title');
$this->assertEqual($result,'My Title');
$this->Helper->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
$this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
$result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
$this->assertEqual($result,2008);
$this->Helper->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
$this->Helper->request->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
$result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
$this->assertEqual($result,2008);
$this->Helper->data['HelperTestPost']['title'] = 'My Title';
$this->Helper->request->data['HelperTestPost']['title'] = 'My Title';
$result = $this->Helper->value('title');
$this->assertEqual($result,'My Title');
$this->Helper->data['My']['title'] = 'My Title';
$this->Helper->request->data['My']['title'] = 'My Title';
$result = $this->Helper->value('My.title');
$this->assertEqual($result,'My Title');
}
function testWebrootPaths() {
$this->Helper->webroot = '/';
$this->Helper->request->webroot = '/';
$result = $this->Helper->webroot('/img/cake.power.gif');
$expected = '/img/cake.power.gif';
$this->assertEqual($result, $expected);