Fix double base dir in image() with fullBase.

Fixes #2991
This commit is contained in:
mark_story 2012-07-03 20:48:10 -04:00
parent 22373868bb
commit e61f636bc7
2 changed files with 21 additions and 3 deletions

View file

@ -361,7 +361,14 @@ class HtmlHelperTest extends CakeTestCase {
$result = $this->Html->image('test.gif?one=two&three=four'); $result = $this->Html->image('test.gif?one=two&three=four');
$this->assertTags($result, array('img' => array('src' => 'img/test.gif?one=two&three=four', 'alt' => ''))); $this->assertTags($result, array('img' => array('src' => 'img/test.gif?one=two&three=four', 'alt' => '')));
}
/**
* Test that image() works with fullBase and a webroot not equal to /
*
* @return void
*/
public function testImageWithFullBase() {
$result = $this->Html->image('test.gif', array('fullBase' => true)); $result = $this->Html->image('test.gif', array('fullBase' => true));
$here = $this->Html->url('/', true); $here = $this->Html->url('/', true);
$this->assertTags($result, array('img' => array('src' => $here . 'img/test.gif', 'alt' => ''))); $this->assertTags($result, array('img' => array('src' => $here . 'img/test.gif', 'alt' => '')));
@ -369,6 +376,15 @@ class HtmlHelperTest extends CakeTestCase {
$result = $this->Html->image('sub/test.gif', array('fullBase' => true)); $result = $this->Html->image('sub/test.gif', array('fullBase' => true));
$here = $this->Html->url('/', true); $here = $this->Html->url('/', true);
$this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => ''))); $this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
$request = $this->Html->request;
$request->webroot = '/myproject/';
$request->base = '/myproject';
Router::setRequestInfo($request);
$result = $this->Html->image('sub/test.gif', array('fullBase' => true));
$here = $this->Html->url('/', true);
$this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
} }
/** /**

View file

@ -313,10 +313,12 @@ class Helper extends Object {
$path = h($this->assetTimestamp($this->webroot($path))); $path = h($this->assetTimestamp($this->webroot($path)));
if (!empty($options['fullBase'])) { if (!empty($options['fullBase'])) {
if ($path[0] == '/') { $base = $this->url('/', true);
$path = substr($path, 1); $len = strlen($this->request->webroot);
if ($len) {
$base = substr($base, 0, -$len);
} }
$path = $this->url('/', true) . $path; $path = $base . $path;
} }
} }