Skip to content

Commit

Permalink
MAGETWO-33727: Wrong parameter for getting base url for 'media' path …
Browse files Browse the repository at this point in the history
…in "Image" form element. #1025

- Import UrlInterface to short line to under 120 characters
- Add integration test to catch regression
- Modify unit test to catch regression
  • Loading branch information
otoolec committed Feb 7, 2015
1 parent 59c1d9d commit 360364f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Tests for \Magento\Framework\Data\Form\Element\Image
*/
namespace Magento\Framework\Data\Form\Element;

class ImageTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\Data\Form\Element\Image
*/
protected $imageElement;

protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var $elementFactory \Magento\Framework\Data\Form\ElementFactory */
$elementFactory = $objectManager->create('Magento\Framework\Data\Form\ElementFactory');
$this->imageElement = $elementFactory->create('Magento\Framework\Data\Form\Element\Image', []);
$form = $objectManager->create('Magento\Framework\Data\Form');
$this->imageElement->setForm($form);
}

public function testGetElementHtml()
{
$filePath = 'some/path/to/file.jpg';
$this->imageElement->setValue($filePath);
$html = $this->imageElement->getElementHtml();

$this->assertContains('media/' . $filePath, $html);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
*/
namespace Magento\Framework\Data\Form\Element;

use Magento\Framework\UrlInterface;

class ImageTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $_objectManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $urlBuilder;

/**
* @var \Magento\Framework\Data\Form\Element\Image
*/
Expand All @@ -32,12 +39,12 @@ protected function setUp()
false
);
$escaperMock = $this->getMock('\Magento\Framework\Escaper', [], [], '', false);
$urlBuilderMock = $this->getMock('\Magento\Framework\Url', [], [], '', false);
$this->urlBuilder = $this->getMock('\Magento\Framework\Url', [], [], '', false);
$this->_image = new \Magento\Framework\Data\Form\Element\Image(
$factoryMock,
$collectionFactoryMock,
$escaperMock,
$urlBuilderMock
$this->urlBuilder
);
$formMock = new \Magento\Framework\Object();
$formMock->getHtmlIdPrefix('id_prefix');
Expand Down Expand Up @@ -81,12 +88,16 @@ public function testGetElementHtmlWithoutValue()
public function testGetElementHtmlWithValue()
{
$this->_image->setValue('test_value');
$this->urlBuilder->expects($this->once())
->method('getBaseUrl')
->with(['_type' => UrlInterface::URL_TYPE_MEDIA])
->willReturn('http://localhost/media/');
$html = $this->_image->getElementHtml();
$this->assertContains('class="input-file"', $html);
$this->assertContains('<input', $html);
$this->assertContains('type="file"', $html);
$this->assertContains('value="test_value"', $html);
$this->assertContains('<a href="test_value" onclick="imagePreview(\'_image\'); return false;"', $html);
$this->assertContains('<a href="http://localhost/media/test_value" onclick="imagePreview(\'_image\'); return false;"', $html);
$this->assertContains('<input type="checkbox"', $html);
}
}
10 changes: 6 additions & 4 deletions lib/internal/Magento/Framework/Data/Form/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,27 @@
*/
namespace Magento\Framework\Data\Form\Element;

use Magento\Framework\UrlInterface;

class Image extends \Magento\Framework\Data\Form\Element\AbstractElement
{
/**
* @var \Magento\Framework\UrlInterface
* @var UrlInterface
*/
protected $_urlBuilder;

/**
* @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
* @param \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param UrlInterface $urlBuilder
* @param array $data
*/
public function __construct(
\Magento\Framework\Data\Form\Element\Factory $factoryElement,
\Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
\Magento\Framework\Escaper $escaper,
\Magento\Framework\UrlInterface $urlBuilder,
UrlInterface $urlBuilder,
$data = []
) {
$this->_urlBuilder = $urlBuilder;
Expand All @@ -50,7 +52,7 @@ public function getElementHtml()
$url = $this->_getUrl();

if (!preg_match("/^http\:\/\/|https\:\/\//", $url)) {
$url = $this->_urlBuilder->getBaseUrl(['_type' => \Magento\Framework\UrlInterface::URL_TYPE_MEDIA]) . $url;
$url = $this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]) . $url;
}

$html = '<a href="' .
Expand Down

0 comments on commit 360364f

Please sign in to comment.