Skip to content

Commit 36d3991

Browse files
author
Stanislav Idolov
authored
ENGCOM-2654: [Forwardport] Remove PDF files after generation #17280
2 parents 32c2a19 + 71a4b93 commit 36d3991

File tree

13 files changed

+119
-15
lines changed

13 files changed

+119
-15
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Creditmemo/AbstractCreditmemo/Pdfcreditmemos.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ public function __construct(
7474
*/
7575
public function massAction(AbstractCollection $collection)
7676
{
77+
$pdf = $this->pdfCreditmemo->getPdf($collection);
78+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
79+
7780
return $this->fileFactory->create(
7881
sprintf('creditmemo%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
79-
$this->pdfCreditmemo->getPdf($collection)->render(),
82+
$fileContent,
8083
DirectoryList::VAR_DIR,
8184
'application/pdf'
8285
);

app/code/Magento/Sales/Controller/Adminhtml/Creditmemo/AbstractCreditmemo/PrintAction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function __construct(
5353

5454
/**
5555
* @return ResponseInterface|\Magento\Backend\Model\View\Result\Forward
56+
* @throws \Exception
5657
*/
5758
public function execute()
5859
{
@@ -69,9 +70,11 @@ public function execute()
6970
$date = $this->_objectManager->get(
7071
\Magento\Framework\Stdlib\DateTime\DateTime::class
7172
)->date('Y-m-d_H-i-s');
73+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
74+
7275
return $this->_fileFactory->create(
7376
\creditmemo::class . $date . '.pdf',
74-
$pdf->render(),
77+
$fileContent,
7578
DirectoryList::VAR_DIR,
7679
'application/pdf'
7780
);

app/code/Magento/Sales/Controller/Adminhtml/Invoice/AbstractInvoice/Pdfinvoices.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ public function __construct(
7575
*/
7676
public function massAction(AbstractCollection $collection)
7777
{
78+
$pdf = $this->pdfInvoice->getPdf($collection);
79+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
80+
7881
return $this->fileFactory->create(
7982
sprintf('invoice%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
80-
$this->pdfInvoice->getPdf($collection)->render(),
83+
$fileContent,
8184
DirectoryList::VAR_DIR,
8285
'application/pdf'
8386
);

app/code/Magento/Sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function __construct(
4545

4646
/**
4747
* @return ResponseInterface|void
48+
* @throws \Exception
4849
*/
4950
public function execute()
5051
{
@@ -58,9 +59,11 @@ public function execute()
5859
$date = $this->_objectManager->get(
5960
\Magento\Framework\Stdlib\DateTime\DateTime::class
6061
)->date('Y-m-d_H-i-s');
62+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
63+
6164
return $this->_fileFactory->create(
6265
'invoice' . $date . '.pdf',
63-
$pdf->render(),
66+
$fileContent,
6467
DirectoryList::VAR_DIR,
6568
'application/pdf'
6669
);

app/code/Magento/Sales/Controller/Adminhtml/Order/Pdfcreditmemos.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function __construct(
7676
*
7777
* @param AbstractCollection $collection
7878
* @return ResponseInterface|ResultInterface
79+
* @throws \Exception
7980
*/
8081
protected function massAction(AbstractCollection $collection)
8182
{
@@ -85,9 +86,12 @@ protected function massAction(AbstractCollection $collection)
8586
$this->messageManager->addError(__('There are no printable documents related to selected orders.'));
8687
return $this->resultRedirectFactory->create()->setPath($this->getComponentRefererUrl());
8788
}
89+
$pdf = $this->pdfCreditmemo->getPdf($creditmemoCollection->getItems());
90+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
91+
8892
return $this->fileFactory->create(
8993
sprintf('creditmemo%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
90-
$this->pdfCreditmemo->getPdf($creditmemoCollection->getItems())->render(),
94+
$fileContent,
9195
DirectoryList::VAR_DIR,
9296
'application/pdf'
9397
);

app/code/Magento/Sales/Controller/Adminhtml/Order/Pdfdocs.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function __construct(
113113
* @return ResponseInterface|\Magento\Backend\Model\View\Result\Redirect
114114
*
115115
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
116+
* @throws \Exception
116117
*/
117118
protected function massAction(AbstractCollection $collection)
118119
{
@@ -142,10 +143,11 @@ protected function massAction(AbstractCollection $collection)
142143
foreach ($documents as $document) {
143144
$pdf->pages = array_merge($pdf->pages, $document->pages);
144145
}
146+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
145147

146148
return $this->fileFactory->create(
147149
sprintf('docs%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
148-
$pdf->render(),
150+
$fileContent,
149151
DirectoryList::VAR_DIR,
150152
'application/pdf'
151153
);

app/code/Magento/Sales/Controller/Adminhtml/Order/Pdfinvoices.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function __construct(
7575
*
7676
* @param AbstractCollection $collection
7777
* @return ResponseInterface|ResultInterface
78+
* @throws \Exception
7879
*/
7980
protected function massAction(AbstractCollection $collection)
8081
{
@@ -83,9 +84,12 @@ protected function massAction(AbstractCollection $collection)
8384
$this->messageManager->addError(__('There are no printable documents related to selected orders.'));
8485
return $this->resultRedirectFactory->create()->setPath($this->getComponentRefererUrl());
8586
}
87+
$pdf = $this->pdfInvoice->getPdf($invoicesCollection->getItems());
88+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
89+
8690
return $this->fileFactory->create(
8791
sprintf('invoice%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
88-
$this->pdfInvoice->getPdf($invoicesCollection->getItems())->render(),
92+
$fileContent,
8993
DirectoryList::VAR_DIR,
9094
'application/pdf'
9195
);

app/code/Magento/Sales/Controller/Adminhtml/Order/Pdfshipments.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function __construct(
7777
*
7878
* @param AbstractCollection $collection
7979
* @return ResponseInterface|\Magento\Backend\Model\View\Result\Redirect
80+
* @throws \Exception
8081
*/
8182
protected function massAction(AbstractCollection $collection)
8283
{
@@ -87,9 +88,13 @@ protected function massAction(AbstractCollection $collection)
8788
$this->messageManager->addError(__('There are no printable documents related to selected orders.'));
8889
return $this->resultRedirectFactory->create()->setPath($this->getComponentRefererUrl());
8990
}
91+
92+
$pdf = $this->pdfShipment->getPdf($shipmentsCollection->getItems());
93+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
94+
9095
return $this->fileFactory->create(
9196
sprintf('packingslip%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
92-
$this->pdfShipment->getPdf($shipmentsCollection->getItems())->render(),
97+
$fileContent,
9398
DirectoryList::VAR_DIR,
9499
'application/pdf'
95100
);

app/code/Magento/Sales/Controller/Adminhtml/Shipment/AbstractShipment/Pdfshipments.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ public function __construct(
7070
*/
7171
public function massAction(AbstractCollection $collection)
7272
{
73+
$pdf = $this->pdfShipment->getPdf($collection);
74+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
75+
7376
return $this->fileFactory->create(
7477
sprintf('packingslip%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
75-
$this->pdfShipment->getPdf($collection)->render(),
78+
$fileContent,
7679
DirectoryList::VAR_DIR,
7780
'application/pdf'
7881
);

app/code/Magento/Sales/Controller/Adminhtml/Shipment/AbstractShipment/PrintAction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848

4949
/**
5050
* @return ResponseInterface|\Magento\Backend\Model\View\Result\Forward
51+
* @throws \Exception
5152
*/
5253
public function execute()
5354
{
@@ -63,9 +64,11 @@ public function execute()
6364
$date = $this->_objectManager->get(
6465
\Magento\Framework\Stdlib\DateTime\DateTime::class
6566
)->date('Y-m-d_H-i-s');
67+
$fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
68+
6669
return $this->_fileFactory->create(
6770
'packingslip' . $date . '.pdf',
68-
$pdf->render(),
71+
$fileContent,
6972
DirectoryList::VAR_DIR,
7073
'application/pdf'
7174
);

app/code/Magento/Sales/Test/Unit/Controller/Adminhtml/Order/Creditmemo/PrintActionTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public function testExecute()
155155
$creditmemoId = 2;
156156
$date = '2015-01-19_13-03-45';
157157
$fileName = 'creditmemo2015-01-19_13-03-45.pdf';
158-
$fileContents = 'pdf0123456789';
158+
$pdfContent = 'pdf0123456789';
159+
$fileData = ['type' => 'string', 'value' => $pdfContent, 'rm' => true];
159160
$this->prepareTestExecute($creditmemoId);
160161

161162
$this->objectManagerMock->expects($this->any())
@@ -184,12 +185,12 @@ public function testExecute()
184185
->willReturn($date);
185186
$this->pdfMock->expects($this->once())
186187
->method('render')
187-
->willReturn($fileContents);
188+
->willReturn($pdfContent);
188189
$this->fileFactoryMock->expects($this->once())
189190
->method('create')
190191
->with(
191192
$fileName,
192-
$fileContents,
193+
$fileData,
193194
\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
194195
'application/pdf'
195196
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\App\Filesystem;
10+
11+
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\App\Response\Http\FileFactory;
13+
use Magento\Framework\Filesystem;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Zend\Http\Header\ContentType;
16+
17+
/**
18+
* Class CreatePdfFileTest
19+
*
20+
* Integration test for testing a file creation from string
21+
*/
22+
class CreatePdfFileTest extends \PHPUnit\Framework\TestCase
23+
{
24+
public function testGenerateFileFromString()
25+
{
26+
$objectManager = Bootstrap::getObjectManager();
27+
/** @var FileFactory $fileFactory */
28+
$fileFactory = $objectManager->get(FileFactory::class);
29+
/** @var Filesystem $filesystem */
30+
$filesystem = $objectManager->get(Filesystem::class);
31+
$filename = 'test.pdf';
32+
$contentType = 'application/pdf';
33+
$fileContent = ['type' => 'string', 'value' => ''];
34+
$response = $fileFactory->create($filename, $fileContent, DirectoryList::VAR_DIR, $contentType);
35+
/** @var ContentType $contentTypeHeader */
36+
$contentTypeHeader = $response->getHeader('Content-type');
37+
38+
/* Check the system returns the correct type */
39+
self::assertEquals("Content-Type: $contentType", $contentTypeHeader->toString());
40+
41+
$varDirectory = $filesystem->getDirectoryRead(DirectoryList::VAR_DIR);
42+
$varDirectory->isFile($filename);
43+
44+
/* Check the file is generated */
45+
self::assertTrue($varDirectory->isFile($filename));
46+
47+
/* Check the file is removed after generation if the corresponding option is set */
48+
$fileContent = ['type' => 'string', 'value' => '', 'rm' => true];
49+
$fileFactory->create($filename, $fileContent, DirectoryList::VAR_DIR, $contentType);
50+
51+
self::assertFalse($varDirectory->isFile($filename));
52+
}
53+
}

lib/internal/Magento/Framework/App/Response/Http/FileFactory.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function create(
5959
$dir = $this->_filesystem->getDirectoryWrite($baseDir);
6060
$isFile = false;
6161
$file = null;
62+
$fileContent = $this->getFileContent($content);
6263
if (is_array($content)) {
6364
if (!isset($content['type']) || !isset($content['value'])) {
6465
throw new \InvalidArgumentException("Invalid arguments. Keys 'type' and 'value' are required.");
@@ -76,7 +77,7 @@ public function create(
7677
->setHeader('Pragma', 'public', true)
7778
->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
7879
->setHeader('Content-type', $contentType, true)
79-
->setHeader('Content-Length', $contentLength === null ? strlen($content) : $contentLength, true)
80+
->setHeader('Content-Length', $contentLength === null ? strlen($fileContent) : $contentLength, true)
8081
->setHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"', true)
8182
->setHeader('Last-Modified', date('r'), true);
8283

@@ -88,7 +89,8 @@ public function create(
8889
echo $stream->read(1024);
8990
}
9091
} else {
91-
$dir->writeFile($fileName, $content);
92+
$dir->writeFile($fileName, $fileContent);
93+
$file = $fileName;
9294
$stream = $dir->openFile($fileName, 'r');
9395
while (!$stream->eof()) {
9496
echo $stream->read(1024);
@@ -102,4 +104,19 @@ public function create(
102104
}
103105
return $this->_response;
104106
}
107+
108+
/**
109+
* Returns file content for writing.
110+
*
111+
* @param string|array $content
112+
* @return string|array
113+
*/
114+
private function getFileContent($content)
115+
{
116+
if (isset($content['type']) && $content['type'] === 'string') {
117+
return $content['value'];
118+
}
119+
120+
return $content;
121+
}
105122
}

0 commit comments

Comments
 (0)