Skip to content

Commit e8c14d2

Browse files
author
Graham Wharton
committed
Updated AbstractPdf insertLogo function to be Database Media Storage aware.
1 parent 0b968b8 commit e8c14d2

File tree

2 files changed

+151
-5
lines changed

2 files changed

+151
-5
lines changed

app/code/Magento/Sales/Model/Order/Pdf/AbstractPdf.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Sales\Model\Order\Pdf;
88

99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\MediaStorage\Helper\File\Storage\Database;
1011

1112
/**
1213
* Sales Order PDF abstract model
@@ -15,6 +16,7 @@
1516
* @api
1617
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
1718
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19+
* @SuppressWarnings(PHPMD.TooManyFields)
1820
* @since 100.0.2
1921
*/
2022
abstract class AbstractPdf extends \Magento\Framework\DataObject
@@ -122,6 +124,11 @@ abstract public function getPdf();
122124
*/
123125
private $pageSettings;
124126

127+
/**
128+
* @var Database
129+
*/
130+
private $fileStorageDatabase;
131+
125132
/**
126133
* @param \Magento\Payment\Helper\Data $paymentData
127134
* @param \Magento\Framework\Stdlib\StringUtils $string
@@ -134,6 +141,7 @@ abstract public function getPdf();
134141
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
135142
* @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
136143
* @param array $data
144+
* @param Database $fileStorageDatabase
137145
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
138146
*/
139147
public function __construct(
@@ -147,7 +155,8 @@ public function __construct(
147155
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
148156
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
149157
\Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
150-
array $data = []
158+
array $data = [],
159+
Database $fileStorageDatabase = null
151160
) {
152161
$this->addressRenderer = $addressRenderer;
153162
$this->_paymentData = $paymentData;
@@ -160,6 +169,8 @@ public function __construct(
160169
$this->_pdfTotalFactory = $pdfTotalFactory;
161170
$this->_pdfItemsFactory = $pdfItemsFactory;
162171
$this->inlineTranslation = $inlineTranslation;
172+
$this->fileStorageDatabase = $fileStorageDatabase ?:
173+
\Magento\Framework\App\ObjectManager::getInstance()->get(Database::class);
163174
parent::__construct($data);
164175
}
165176

@@ -254,6 +265,11 @@ protected function insertLogo(&$page, $store = null)
254265
);
255266
if ($image) {
256267
$imagePath = '/sales/store/logo/' . $image;
268+
if ($this->fileStorageDatabase->checkDbUsage() &&
269+
!$this->_mediaDirectory->isFile($imagePath)
270+
) {
271+
$this->fileStorageDatabase->saveFileToFilesystem($imagePath);
272+
}
257273
if ($this->_mediaDirectory->isFile($imagePath)) {
258274
$image = \Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
259275
$top = 830;

app/code/Magento/Sales/Test/Unit/Model/Order/Pdf/InvoiceTest.php

Lines changed: 134 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Model\Order\Pdf;
77

8+
use Magento\MediaStorage\Helper\File\Storage\Database;
9+
use Magento\Sales\Model\Order\Invoice;
10+
use Magento\Sales\Model\Order;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Sales\Model\Order\Address;
13+
use Magento\Sales\Model\Order\Address\Renderer;
14+
15+
/**
16+
* Class InvoiceTest
17+
*
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19+
*/
820
class InvoiceTest extends \PHPUnit\Framework\TestCase
921
{
1022
/**
@@ -17,29 +29,68 @@ class InvoiceTest extends \PHPUnit\Framework\TestCase
1729
*/
1830
protected $_pdfConfigMock;
1931

32+
/**
33+
* @var Database|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected $databaseMock;
36+
37+
/**
38+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
protected $scopeConfigMock;
41+
42+
/**
43+
* @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
protected $directoryMock;
46+
47+
/**
48+
* @var Renderer|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
protected $addressRendererMock;
51+
52+
/**
53+
* @var \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
protected $paymentDataMock;
56+
2057
protected function setUp()
2158
{
2259
$this->_pdfConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Pdf\Config::class)
2360
->disableOriginalConstructor()
2461
->getMock();
25-
$directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
26-
$directoryMock->expects($this->any())->method('getAbsolutePath')->will(
62+
$this->directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
63+
$this->directoryMock->expects($this->any())->method('getAbsolutePath')->will(
2764
$this->returnCallback(
2865
function ($argument) {
2966
return BP . '/' . $argument;
3067
}
3168
)
3269
);
3370
$filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
34-
$filesystemMock->expects($this->any())->method('getDirectoryRead')->will($this->returnValue($directoryMock));
35-
$filesystemMock->expects($this->any())->method('getDirectoryWrite')->will($this->returnValue($directoryMock));
71+
$filesystemMock->expects($this->any())
72+
->method('getDirectoryRead')
73+
->will($this->returnValue($this->directoryMock));
74+
$filesystemMock->expects($this->any())
75+
->method('getDirectoryWrite')
76+
->will($this->returnValue($this->directoryMock));
77+
78+
$this->databaseMock = $this->createMock(Database::class);
79+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
80+
$this->addressRendererMock = $this->createMock(Renderer::class);
81+
$this->paymentDataMock = $this->createMock(\Magento\Payment\Helper\Data::class);
3682

3783
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3884
$this->_model = $helper->getObject(
3985
\Magento\Sales\Model\Order\Pdf\Invoice::class,
4086
[
4187
'filesystem' => $filesystemMock,
4288
'pdfConfig' => $this->_pdfConfigMock,
89+
'fileStorageDatabase' => $this->databaseMock,
90+
'scopeConfig' => $this->scopeConfigMock,
91+
'addressRenderer' => $this->addressRendererMock,
92+
'string' => new \Magento\Framework\Stdlib\StringUtils(),
93+
'paymentData' => $this->paymentDataMock
4394
]
4495
);
4596
}
@@ -72,4 +123,83 @@ public function testGetPdfInitRenderer()
72123
$renderers->getValue($this->_model)
73124
);
74125
}
126+
127+
public function testInsertLogoDatabaseMediaStorage()
128+
{
129+
$filename = 'image.jpg';
130+
$path = '/sales/store/logo/';
131+
132+
$this->_pdfConfigMock->expects($this->once())
133+
->method('getRenderersPerProduct')
134+
->with('invoice')
135+
->will($this->returnValue(['product_type_one' => 'Renderer_Type_One_Product_One']));
136+
$this->_pdfConfigMock->expects($this->any())
137+
->method('getTotals')
138+
->will($this->returnValue([]));
139+
140+
$block = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class)
141+
->disableOriginalConstructor()
142+
->setMethods(['setIsSecureMode','toPdf'])
143+
->getMock();
144+
$block->expects($this->any())
145+
->method('setIsSecureMode')
146+
->willReturn($block);
147+
$block->expects($this->any())
148+
->method('toPdf')
149+
->will($this->returnValue(''));
150+
$this->paymentDataMock->expects($this->any())
151+
->method('getInfoBlock')
152+
->willReturn($block);
153+
154+
$this->addressRendererMock->expects($this->any())
155+
->method('format')
156+
->will($this->returnValue(''));
157+
158+
$this->databaseMock->expects($this->any())
159+
->method('checkDbUsage')
160+
->will($this->returnValue(true));
161+
162+
$invoiceMock = $this->createMock(Invoice::class);
163+
$orderMock = $this->createMock(Order::class);
164+
$addressMock = $this->createMock(Address::class);
165+
$orderMock->expects($this->any())
166+
->method('getBillingAddress')
167+
->willReturn($addressMock);
168+
$orderMock->expects($this->any())
169+
->method('getIsVirtual')
170+
->will($this->returnValue(true));
171+
$infoMock = $this->createMock(\Magento\Payment\Model\InfoInterface::class);
172+
$orderMock->expects($this->any())
173+
->method('getPayment')
174+
->willReturn($infoMock);
175+
$invoiceMock->expects($this->any())
176+
->method('getOrder')
177+
->willReturn($orderMock);
178+
$invoiceMock->expects($this->any())
179+
->method('getAllItems')
180+
->willReturn([]);
181+
182+
$this->scopeConfigMock->expects($this->at(0))
183+
->method('getValue')
184+
->with('sales/identity/logo', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)
185+
->will($this->returnValue($filename));
186+
$this->scopeConfigMock->expects($this->at(1))
187+
->method('getValue')
188+
->with('sales/identity/address', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)
189+
->will($this->returnValue(''));
190+
191+
$this->directoryMock->expects($this->any())
192+
->method('isFile')
193+
->with($path . $filename)
194+
->willReturnOnConsecutiveCalls(
195+
$this->returnValue(false),
196+
$this->returnValue(false)
197+
);
198+
199+
$this->databaseMock->expects($this->once())
200+
->method('saveFileToFilesystem')
201+
->with($path . $filename);
202+
203+
$this->_model->getPdf([$invoiceMock]);
204+
}
75205
}

0 commit comments

Comments
 (0)