Skip to content

magento/magento2#24730: Fix Frontend Invoice PDF configured Logo image #30632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 13, 2020
Merged
77 changes: 77 additions & 0 deletions app/code/Magento/Sales/Model/Order/Invoice/GetLogoFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Model\Order\Invoice;

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Get Custom Logo File for Invoice HTML print
*/
class GetLogoFile
{
private const XML_PATH_SALES_IDENTITY_LOGO_HTML = 'sales/identity/logo_html';
private const LOGO_BASE_DIR = 'sales/store/logo_html/';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var UrlInterface
*/
private $urlBuilder;

/**
* @param ScopeConfigInterface $scopeConfig
* @param UrlInterface $urlBuilder
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
UrlInterface $urlBuilder
) {
$this->scopeConfig = $scopeConfig;
$this->urlBuilder = $urlBuilder;
}

/**
* Return Custom Invoice Logo file url if configured in admin
*
* @return string|null
*/
public function execute(): ?string
{
$invoiceLogoPath = $this->getIdentityLogoHtml();
if (!$invoiceLogoPath) {
return null;
}

return sprintf(
"%s%s%s",
$this->urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]),
self::LOGO_BASE_DIR,
$invoiceLogoPath
);
}

/**
* Get Admin Configuration for Invoice Logo HTML
*
* @return null|string
*/
private function getIdentityLogoHtml(): ?string
{
return $this->scopeConfig->getValue(
self::XML_PATH_SALES_IDENTITY_LOGO_HTML,
ScopeInterface::SCOPE_STORE,
null
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
<update handle="print" />
<body>
<attribute name="class" value="account"/>
<referenceContainer name="header-wrapper">
<referenceBlock name="logo">
<arguments>
<argument name="logo_src" xsi:type="helper" helper="Magento\Sales\Model\Order\Invoice\GetLogoFile::execute"/>
</arguments>
</referenceBlock>
</referenceContainer>
<referenceContainer name="page.main.title">
<block class="Magento\Sales\Block\Order\PrintOrder\Invoice" name="order.status" template="Magento_Sales::order/order_status.phtml" />
<block class="Magento\Sales\Block\Order\PrintOrder\Invoice" name="order.date" template="Magento_Sales::order/order_date.phtml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Model\Order\Invoice;

use Magento\Store\Model\ScopeInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\Framework\App\Config\MutableScopeConfigInterface;

/**
* Test class for \Magento\Sales\Model\Order\Invoice\GetLogoFile
*/
class GetLogoFileTest extends \PHPUnit\Framework\TestCase
{
private const XML_PATH_SALES_IDENTITY_LOGO_HTML = 'sales/identity/logo_html';
private const DUMP_IMAGE = 'my_dump_logo.png';

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var MutableScopeConfigInterface
*/
private $scopeConfig;

/**
* @var GetLogoFile
*/
private $getLogoFile;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->scopeConfig = $this->objectManager->get(MutableScopeConfigInterface::class);
$this->getLogoFile = $this->objectManager->get(GetLogoFile::class);
}

/**
* Check that GetLogoFile return image after Admin configuration is changed
*
* @return void
*/
public function testExecute(): void
{
$this->assertNull($this->getLogoFile->execute());

$this->applyImage();

$this->assertIsString($this->getLogoFile->execute());
$this->assertStringContainsString(self::DUMP_IMAGE, $this->getLogoFile->execute());
}

/**
* Set Invoice Custom Logo HTML Image configuration
*
* @return void
*/
private function applyImage(): void
{
$this->scopeConfig->setValue(
self::XML_PATH_SALES_IDENTITY_LOGO_HTML,
self::DUMP_IMAGE,
ScopeInterface::SCOPE_STORE
);
}
}