From 0edfc3257f9f2e530e04d8e8b8acd9dd2a2accb4 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Sun, 14 Mar 2021 20:34:23 +0530 Subject: [PATCH 1/6] Update the fopen mode for writer --- src/PhpSpreadsheet/Writer/BaseWriter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Writer/BaseWriter.php b/src/PhpSpreadsheet/Writer/BaseWriter.php index afda5c433e..d52b0d4ad8 100644 --- a/src/PhpSpreadsheet/Writer/BaseWriter.php +++ b/src/PhpSpreadsheet/Writer/BaseWriter.php @@ -108,7 +108,7 @@ public function openFileHandle($filename): void return; } - $fileHandle = $filename ? fopen($filename, 'wb+') : false; + $fileHandle = $filename ? fopen($filename, 'wb') : false; if ($fileHandle === false) { throw new Exception('Could not open file "' . $filename . '" for writing.'); } From 5686453bcce92a31fcbe566c087bf063f0bf9941 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Sun, 14 Mar 2021 20:48:10 +0530 Subject: [PATCH 2/6] Add test case for excel with media --- .../Writer/Xlsx/DrawingsTest.php | 22 ++++++++++++++++++ .../XLSX/saving_drawing_with_same_path.xlsx | Bin 0 -> 11717 bytes 2 files changed, 22 insertions(+) create mode 100644 tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php index d6ad77c63f..58e3be573a 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php @@ -42,4 +42,26 @@ public function testSaveLoadWithDrawingOn2ndWorksheet(): void // Fake assert. The only thing we need is to ensure the file is loaded without exception self::assertNotNull($reloadedSpreadsheet); } + + /** + * Test save and load XLSX file with drawing with the same file name. + */ + public function testSaveLoadWithDrawingWithSamePath(): void + { + // Read spreadsheet from file + $filePath = 'tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx'; + $reader = new Xlsx(); + $spreadsheet = $reader->load($filePath); + + $spreadsheet->getActiveSheet()->setCellValue('D5', 'foo'); + // Save spreadsheet to file to the same path. Success test case won't + // throw exception here + $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); + $writer->save($filePath); + + $reloadedSpreadsheet = $reader->load($filePath); + + // Fake assert. The only thing we need is to ensure the file is loaded without exception + self::assertNotNull($reloadedSpreadsheet); + } } diff --git a/tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx b/tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..53f9a6b9a46d3a7358770e05e5cd2a6c9e40fa10 GIT binary patch literal 11717 zcmb_?1zgly)BjS^r6AoP(kb2D4We|Hbcu8$-O`|x3P^W{l(dwfwDh`k$Ga;RFW2YZ z|I2%y|L|FUyPt*cIcMfOXXeZtC0Q6)EC590!faH2zWL+V9RMjn&%xxGBa^xsDgb`% zSYH47<@_8O00p}b1pxf~U!|WI2ml0t=nV{6XaE5D7X~8)=_kl~ue+W<2J<$l`w9NX(jYd;$;YnsX2dk#QzrKX-lNzni@u8W77Nc?d}&CQ zp>ttDDHp%PRP*h6s%7+sF3uQq#!6whXY=uxv zib`%Om$CyWS$ps~`xpH5Lb5Ce4zu=WEq6--<9Zn#4p_XUNi_YGHEAl!qsxg%Q^Rc~ zcGKbIq@J;usfS<0IMUNHL$j^4j--yjQIPWo!5zNQ429dOZ=K7)8IUMpiKv7ph?)r@ zi4QP-AmV`?#OzVvM#*Vj+#Y_WxigAn@Q>wpbp_{BhiY(kRrT`>3kKHar7h;AJ zG<5@el+uVMh`h%t_VNZL6;D;Ayq=z;ye_@NrK2LKd5`}%v|LZpKO%NFPey{5y?A&> zdPQbHwvugV?g8Eg621u4EP`|J(BLyO0-8oBQXm%gcjZSr#;Dw=IiEzV7AF(dXIQZ}7Pl+rk-ep>O8H}<} z9!^3Y8QfStd&J?x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tJiK zyC&83AP}(M2?*&hT}z8FIqJ{p-`M|us^(fPkmo-v0D|#5_D$8a2-{p?|JpZii+s(% zn|%aG2kKgKd~2?dZ{Lsle<|dz9*KhSQz5r)OI*N$2!Jlpexwm<<#tErAUQ3Q|+ZF5$-u;Fit}?Fzef3YtGIc9`)i!* zy28IA{p|~S*DShr*qa9$$T{#@a(owwelFl@-ZwYme-*HArnZpd(Y56GvXk6k>;IR- z((h}$nKD9>=WEIFjV1egvi(Wyf3;Z08)?>G2dpChJ67Ce;4KG)y8~GTKKQ2r9L;tq HAfNsRwQ3$l literal 0 HcmV?d00001 From 51abdf0b8f7b406bab07e4d954e0e8454a8f1f8f Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Sun, 14 Mar 2021 22:20:11 +0530 Subject: [PATCH 3/6] Refactor xlsx writer * Move file handler creation and file addition to the end --- src/PhpSpreadsheet/Writer/BaseWriter.php | 2 +- src/PhpSpreadsheet/Writer/Xlsx.php | 93 ++++++++++-------- .../Writer/Xlsx/DrawingsTest.php | 1 + .../XLSX/saving_drawing_with_same_path.xlsx | Bin 11717 -> 7936 bytes 4 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/BaseWriter.php b/src/PhpSpreadsheet/Writer/BaseWriter.php index d52b0d4ad8..afda5c433e 100644 --- a/src/PhpSpreadsheet/Writer/BaseWriter.php +++ b/src/PhpSpreadsheet/Writer/BaseWriter.php @@ -108,7 +108,7 @@ public function openFileHandle($filename): void return; } - $fileHandle = $filename ? fopen($filename, 'wb') : false; + $fileHandle = $filename ? fopen($filename, 'wb+') : false; if ($fileHandle === false) { throw new Exception('Could not open file "' . $filename . '" for writing.'); } diff --git a/src/PhpSpreadsheet/Writer/Xlsx.php b/src/PhpSpreadsheet/Writer/Xlsx.php index d71541c805..10fc50c9d4 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx.php +++ b/src/PhpSpreadsheet/Writer/Xlsx.php @@ -182,8 +182,6 @@ public function save($pFilename): void $this->pathNames = []; $this->spreadSheet->garbageCollect(); - $this->openFileHandle($pFilename); - $saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog(); Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog(false); $saveDateReturnType = Functions::getReturnDateType(); @@ -206,77 +204,72 @@ public function save($pFilename): void // Create drawing dictionary $this->drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->spreadSheet)); - $options = new Archive(); - $options->setEnableZip64(false); - $options->setOutputStream($this->fileHandle); - - $this->zip = new ZipStream(null, $options); - + $zipContent = []; // Add [Content_Types].xml to ZIP file - $this->addZipFile('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->spreadSheet, $this->includeCharts)); + $zipContent['[Content_Types].xml'] = $this->getWriterPart('ContentTypes')->writeContentTypes($this->spreadSheet, $this->includeCharts); //if hasMacros, add the vbaProject.bin file, Certificate file(if exists) if ($this->spreadSheet->hasMacros()) { $macrosCode = $this->spreadSheet->getMacrosCode(); if ($macrosCode !== null) { // we have the code ? - $this->addZipFile('xl/vbaProject.bin', $macrosCode); //allways in 'xl', allways named vbaProject.bin + $zipContent['xl/vbaProject.bin'] = $macrosCode; //allways in 'xl', allways named vbaProject.bin if ($this->spreadSheet->hasMacrosCertificate()) { //signed macros ? // Yes : add the certificate file and the related rels file - $this->addZipFile('xl/vbaProjectSignature.bin', $this->spreadSheet->getMacrosCertificate()); - $this->addZipFile('xl/_rels/vbaProject.bin.rels', $this->getWriterPart('RelsVBA')->writeVBARelationships($this->spreadSheet)); + $zipContent['xl/vbaProjectSignature.bin'] = $this->spreadSheet->getMacrosCertificate(); + $zipContent['xl/_rels/vbaProject.bin.rels'] = $this->getWriterPart('RelsVBA')->writeVBARelationships($this->spreadSheet); } } } //a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels) if ($this->spreadSheet->hasRibbon()) { $tmpRibbonTarget = $this->spreadSheet->getRibbonXMLData('target'); - $this->addZipFile($tmpRibbonTarget, $this->spreadSheet->getRibbonXMLData('data')); + $zipContent[$tmpRibbonTarget] = $this->spreadSheet->getRibbonXMLData('data'); if ($this->spreadSheet->hasRibbonBinObjects()) { $tmpRootPath = dirname($tmpRibbonTarget) . '/'; $ribbonBinObjects = $this->spreadSheet->getRibbonBinObjects('data'); //the files to write foreach ($ribbonBinObjects as $aPath => $aContent) { - $this->addZipFile($tmpRootPath . $aPath, $aContent); + $zipContent[$tmpRootPath . $aPath] = $aContent; } //the rels for files - $this->addZipFile($tmpRootPath . '_rels/' . basename($tmpRibbonTarget) . '.rels', $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->spreadSheet)); + $zipContent[$tmpRootPath . '_rels/' . basename($tmpRibbonTarget) . '.rels'] = $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->spreadSheet); } } // Add relationships to ZIP file - $this->addZipFile('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->spreadSheet)); - $this->addZipFile('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->spreadSheet)); + $zipContent['_rels/.rels'] = $this->getWriterPart('Rels')->writeRelationships($this->spreadSheet); + $zipContent['xl/_rels/workbook.xml.rels'] = $this->getWriterPart('Rels')->writeWorkbookRelationships($this->spreadSheet); // Add document properties to ZIP file - $this->addZipFile('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->spreadSheet)); - $this->addZipFile('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->spreadSheet)); + $zipContent['docProps/app.xml'] = $this->getWriterPart('DocProps')->writeDocPropsApp($this->spreadSheet); + $zipContent['docProps/core.xml'] = $this->getWriterPart('DocProps')->writeDocPropsCore($this->spreadSheet); $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->spreadSheet); if ($customPropertiesPart !== null) { - $this->addZipFile('docProps/custom.xml', $customPropertiesPart); + $zipContent['docProps/custom.xml'] = $customPropertiesPart; } // Add theme to ZIP file - $this->addZipFile('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->spreadSheet)); + $zipContent['xl/theme/theme1.xml'] = $this->getWriterPart('Theme')->writeTheme($this->spreadSheet); // Add string table to ZIP file - $this->addZipFile('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->stringTable)); + $zipContent['xl/sharedStrings.xml'] = $this->getWriterPart('StringTable')->writeStringTable($this->stringTable); // Add styles to ZIP file - $this->addZipFile('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->spreadSheet)); + $zipContent['xl/styles.xml'] = $this->getWriterPart('Style')->writeStyles($this->spreadSheet); // Add workbook to ZIP file - $this->addZipFile('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas)); + $zipContent['xl/workbook.xml'] = $this->getWriterPart('Workbook')->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas); $chartCount = 0; // Add worksheets for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { - $this->addZipFile('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts)); + $zipContent['xl/worksheets/sheet' . ($i + 1) . '.xml'] = $this->getWriterPart('Worksheet')->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts); if ($this->includeCharts) { $charts = $this->spreadSheet->getSheet($i)->getChartCollection(); if (count($charts) > 0) { foreach ($charts as $chart) { - $this->addZipFile('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas)); + $zipContent['xl/charts/chart' . ($chartCount + 1) . '.xml'] = $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas); ++$chartCount; } } @@ -287,19 +280,19 @@ public function save($pFilename): void // Add worksheet relationships (drawings, ...) for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) { // Add relationships - $this->addZipFile('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts)); + $zipContent['xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels'] = $this->getWriterPart('Rels')->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts); // Add unparsedLoadedData $sheetCodeName = $this->spreadSheet->getSheet($i)->getCodeName(); $unparsedLoadedData = $this->spreadSheet->getUnparsedLoadedData(); if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'])) { foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'] as $ctrlProp) { - $this->addZipFile($ctrlProp['filePath'], $ctrlProp['content']); + $zipContent[$ctrlProp['filePath']] = $ctrlProp['content']; } } if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'])) { foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'] as $ctrlProp) { - $this->addZipFile($ctrlProp['filePath'], $ctrlProp['content']); + $zipContent[$ctrlProp['filePath']] = $ctrlProp['content']; } } @@ -312,13 +305,13 @@ public function save($pFilename): void // Add drawing and image relationship parts if (($drawingCount > 0) || ($chartCount > 0)) { // Drawing relationships - $this->addZipFile('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts)); + $zipContent['xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels'] = $this->getWriterPart('Rels')->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts); // Drawings - $this->addZipFile('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts)); + $zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts); } elseif (isset($unparsedLoadedData['sheets'][$sheetCodeName]['drawingAlternateContents'])) { // Drawings - $this->addZipFile('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts)); + $zipContent['xl/drawings/drawing' . ($i + 1) . '.xml'] = $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts); } // Add unparsed drawings @@ -327,7 +320,7 @@ public function save($pFilename): void $drawingFile = array_search($relId, $unparsedLoadedData['sheets'][$sheetCodeName]['drawingOriginalIds']); if ($drawingFile !== false) { $drawingFile = ltrim($drawingFile, '.'); - $this->addZipFile('xl' . $drawingFile, $drawingXml); + $zipContent['xl' . $drawingFile] = $drawingXml; } } } @@ -335,30 +328,30 @@ public function save($pFilename): void // Add comment relationship parts if (count($this->spreadSheet->getSheet($i)->getComments()) > 0) { // VML Comments - $this->addZipFile('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->spreadSheet->getSheet($i))); + $zipContent['xl/drawings/vmlDrawing' . ($i + 1) . '.vml'] = $this->getWriterPart('Comments')->writeVMLComments($this->spreadSheet->getSheet($i)); // Comments - $this->addZipFile('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->spreadSheet->getSheet($i))); + $zipContent['xl/comments' . ($i + 1) . '.xml'] = $this->getWriterPart('Comments')->writeComments($this->spreadSheet->getSheet($i)); } // Add unparsed relationship parts if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'])) { foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'] as $vmlDrawing) { - $this->addZipFile($vmlDrawing['filePath'], $vmlDrawing['content']); + $zipContent[$vmlDrawing['filePath']] = $vmlDrawing['content']; } } // Add header/footer relationship parts if (count($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) { // VML Drawings - $this->addZipFile('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i))); + $zipContent['xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml'] = $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i)); // VML Drawing relationships - $this->addZipFile('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i))); + $zipContent['xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels'] = $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i)); // Media foreach ($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) { - $this->addZipFile('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath())); + $zipContent['xl/media/' . $image->getIndexedFilename()] = file_get_contents($image->getPath()); } } } @@ -381,7 +374,7 @@ public function save($pFilename): void $imageContents = file_get_contents($imagePath); } - $this->addZipFile('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); + $zipContent['xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())] = $imageContents; } elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) { ob_start(); call_user_func( @@ -391,13 +384,23 @@ public function save($pFilename): void $imageContents = ob_get_contents(); ob_end_clean(); - $this->addZipFile('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); + $zipContent['xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())] = $imageContents; } } Functions::setReturnDateType($saveDateReturnType); Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); + $this->openFileHandle($pFilename); + + $options = new Archive(); + $options->setEnableZip64(false); + $options->setOutputStream($this->fileHandle); + + $this->zip = new ZipStream(null, $options); + + $this->addZipFiles($zipContent); + // Close file try { $this->zip->finish(); @@ -545,4 +548,12 @@ private function addZipFile(string $path, string $content): void $this->zip->addFile($path, $content); } } + + private function addZipFiles(array $zipContent): void + { + foreach ($zipContent as $path => $content) + { + $this->addZipFile($path, $content); + } + } } diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php index 58e3be573a..0880bde988 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx; +use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Settings; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; diff --git a/tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx b/tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx index 53f9a6b9a46d3a7358770e05e5cd2a6c9e40fa10..a88d43d3dc71f029d530ce805b217e50183b414f 100644 GIT binary patch delta 2615 zcmZ{m2UJs67{^}-fr>N7OaBEC?JA$^d@*pcx{vO?mH*%`+eWL-@X6)Pix>wdvkAh zq_i>wL7zYtX>^kA@?K0F0)j4}AV>-P8tBBJv#E4;h|jTDDl6C|J|?=dAHhO=Dha99 zSXfSgV0S49S`Y3aOlmaC)I>C9^Cs0LLsN7s{6wL3AXE*4zQ(KbZSa#uk|F(_(l64$ zE7%|*2n|BR7>B%>j98W_B{o)Ee+qdDVJu0Qs5SAJfWQC(BS{=$FsWjs3&lp5N+PW6 z`w`E>X(>#EPIWsbL4$%t(4T zOGIILyX9KU(9p~s?UzIJSDM<93Fs5a_L^0J>Jc0oXWN_OV{y2&Ki_?7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W&fY0 zC>%B808W()GaQsym&QJHL30p`C{P%E1x0_0lD7QXyIDcDNQ%HK=ie0j(Hn5<>38KM ztbR1A*oUpT1mnc$cnN7H;6FV6`=?NXXEc^I;6f$nIC0S*X2?Ed&22l^CFp6gVjMGM z-3j}E1 Date: Sun, 14 Mar 2021 22:25:13 +0530 Subject: [PATCH 4/6] Update PHPCS changes --- src/PhpSpreadsheet/Writer/Xlsx.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Xlsx.php b/src/PhpSpreadsheet/Writer/Xlsx.php index 10fc50c9d4..23354f8c3e 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx.php +++ b/src/PhpSpreadsheet/Writer/Xlsx.php @@ -551,8 +551,7 @@ private function addZipFile(string $path, string $content): void private function addZipFiles(array $zipContent): void { - foreach ($zipContent as $path => $content) - { + foreach ($zipContent as $path => $content) { $this->addZipFile($path, $content); } } From 59de56bb62fde390cf52cef69ffab336a84d0850 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Mon, 5 Apr 2021 12:18:11 +0530 Subject: [PATCH 5/6] Move original file to temporary file --- .../Writer/Xlsx/DrawingsTest.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php index 0880bde988..c48c86db10 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php @@ -5,6 +5,7 @@ use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use PhpOffice\PhpSpreadsheet\Settings; +use PhpOffice\PhpSpreadsheet\Shared\File; use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; class DrawingsTest extends AbstractFunctional @@ -50,17 +51,24 @@ public function testSaveLoadWithDrawingOn2ndWorksheet(): void public function testSaveLoadWithDrawingWithSamePath(): void { // Read spreadsheet from file - $filePath = 'tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx'; + $originalFilePath = 'tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx'; + + $originalFile = file_get_contents($originalFilePath); + + $tempFilePath = File::sysGetTempDir() . '/saving_drawing_with_same_path'; + + file_put_contents($tempFilePath, $originalFile); + $reader = new Xlsx(); - $spreadsheet = $reader->load($filePath); + $spreadsheet = $reader->load($tempFilePath); $spreadsheet->getActiveSheet()->setCellValue('D5', 'foo'); // Save spreadsheet to file to the same path. Success test case won't // throw exception here $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); - $writer->save($filePath); + $writer->save($tempFilePath); - $reloadedSpreadsheet = $reader->load($filePath); + $reloadedSpreadsheet = $reader->load($tempFilePath); // Fake assert. The only thing we need is to ensure the file is loaded without exception self::assertNotNull($reloadedSpreadsheet); From dd9cb259d0cd7b26c733322427794810a97fcc8f Mon Sep 17 00:00:00 2001 From: Vivek Kumar Date: Mon, 5 Apr 2021 22:14:50 +0530 Subject: [PATCH 6/6] Unlink temporary file --- .../Writer/Xlsx/DrawingsTest.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php index c48c86db10..ef2002b5d6 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/DrawingsTest.php @@ -51,24 +51,26 @@ public function testSaveLoadWithDrawingOn2ndWorksheet(): void public function testSaveLoadWithDrawingWithSamePath(): void { // Read spreadsheet from file - $originalFilePath = 'tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx'; + $originalFileName = 'tests/data/Writer/XLSX/saving_drawing_with_same_path.xlsx'; - $originalFile = file_get_contents($originalFilePath); + $originalFile = file_get_contents($originalFileName); - $tempFilePath = File::sysGetTempDir() . '/saving_drawing_with_same_path'; + $tempFileName = File::sysGetTempDir() . '/saving_drawing_with_same_path'; - file_put_contents($tempFilePath, $originalFile); + file_put_contents($tempFileName, $originalFile); $reader = new Xlsx(); - $spreadsheet = $reader->load($tempFilePath); + $spreadsheet = $reader->load($tempFileName); $spreadsheet->getActiveSheet()->setCellValue('D5', 'foo'); // Save spreadsheet to file to the same path. Success test case won't // throw exception here $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); - $writer->save($tempFilePath); + $writer->save($tempFileName); - $reloadedSpreadsheet = $reader->load($tempFilePath); + $reloadedSpreadsheet = $reader->load($tempFileName); + + unlink($tempFileName); // Fake assert. The only thing we need is to ensure the file is loaded without exception self::assertNotNull($reloadedSpreadsheet);