2013-12-11 16:09:58 -05:00
|
|
|
<?php
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2014-03-30 14:15:23 +07:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
2022-09-16 11:45:45 +02:00
|
|
|
*
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-03-30 14:15:23 +07:00
|
|
|
*/
|
|
|
|
|
|
2022-09-16 14:09:17 +02:00
|
|
|
namespace PhpOffice\PhpWordTests;
|
2013-12-11 16:09:58 -05:00
|
|
|
|
2014-08-10 20:30:14 +04:00
|
|
|
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
2022-09-16 14:09:17 +02:00
|
|
|
use PhpOffice\PhpWord\IOFactory;
|
|
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
|
|
|
|
use PhpOffice\PhpWord\Settings;
|
2022-09-16 11:45:45 +02:00
|
|
|
use ZipArchive;
|
2013-12-11 16:09:58 -05:00
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Test helper class.
|
2014-03-30 14:15:23 +07:00
|
|
|
*/
|
2014-02-24 19:17:06 +01:00
|
|
|
class TestHelperDOCX
|
2013-12-11 16:09:58 -05:00
|
|
|
{
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Temporary file name.
|
2014-03-30 14:15:23 +07:00
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2017-11-04 22:44:12 +01:00
|
|
|
protected static $file;
|
2014-02-24 19:17:06 +01:00
|
|
|
|
2013-12-11 16:09:58 -05:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Get document content.
|
2014-03-30 14:15:23 +07:00
|
|
|
*
|
2014-08-11 22:30:46 +04:00
|
|
|
* @since 0.12.0 Throws CreateTemporaryFileException.
|
|
|
|
|
*
|
2014-03-23 12:07:29 -04:00
|
|
|
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
|
|
|
|
* @param string $writerName
|
2015-10-10 19:06:23 +04:00
|
|
|
*
|
2022-09-16 14:09:17 +02:00
|
|
|
* @return \PhpOffice\PhpWordTests\XmlDocument
|
2013-12-11 16:09:58 -05:00
|
|
|
*/
|
2014-03-20 16:54:12 +04:00
|
|
|
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
|
2013-12-11 16:09:58 -05:00
|
|
|
{
|
2014-08-10 20:00:19 +04:00
|
|
|
self::$file = tempnam(Settings::getTempDir(), 'PhpWord');
|
2014-08-10 20:30:14 +04:00
|
|
|
if (false === self::$file) {
|
|
|
|
|
throw new CreateTemporaryFileException();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 20:00:19 +04:00
|
|
|
if (!is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
|
|
|
|
|
mkdir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
|
2014-02-24 19:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-20 16:54:12 +04:00
|
|
|
$xmlWriter = IOFactory::createWriter($phpWord, $writerName);
|
2014-03-19 11:04:48 +04:00
|
|
|
$xmlWriter->save(self::$file);
|
2013-12-11 16:09:58 -05:00
|
|
|
|
2022-09-16 11:45:45 +02:00
|
|
|
$zip = new ZipArchive();
|
2014-02-24 19:17:06 +01:00
|
|
|
$res = $zip->open(self::$file);
|
2015-02-06 22:28:31 +04:00
|
|
|
if (true === $res) {
|
2014-08-10 20:00:19 +04:00
|
|
|
$zip->extractTo(Settings::getTempDir() . '/PhpWord_Unit_Test/');
|
2013-12-11 16:09:58 -05:00
|
|
|
$zip->close();
|
|
|
|
|
}
|
|
|
|
|
|
ODT Changes
Implement a number of features implemented in PhpWord,
but not yet supported in PhpWord ODT Writer.
1. Add default file to tests/PhpWord/_includes/XmlDocument.php to make it
considerably easier to test ODT changes (and Word2007 changes involving
files other that document.xml).
2. Page break before each section.
3. Page numbering start.
4. Font style for Headings.
5. Alignment for images.
6. Paragraph style for TextRun.
7. "Hide grammatical errors" for whole document.
8. Page layout for each section.
9. For each page layout, support user-specified page width, page height,
orientation, margin top, margin bottom, margin left, margin right.
10. Page header and footer.
11. Named colors.
12. NoProof font style.
13. Paragraph Style - spaceBefore, spaceAfter, lineHeight, pageBreakBefore,
indentation, text alignment.
14. Tab stops.
15. Basic support for some Fields (DATE, PAGE, NUMPAGES).
16. Link had an error in how it was handling internal links (needs leading #).
17. In addition to tests for all the above, added some tests for Tables.
Item 11 above needs 1 module from Pull Request 1775, which is targeted
for v0.18.0 but not yet merged, so the relevant module is also here.
Item 15 above needs 1 module from Pull Request 1774, which is targeted
for v0.18.0 but not yet merged, so the relevant module is also here.
Testing change from Pull Request 1771 is included here, but was
merged after my fork.
2020-01-05 10:27:45 -08:00
|
|
|
$doc = new XmlDocument(Settings::getTempDir() . '/PhpWord_Unit_Test/');
|
|
|
|
|
if ($writerName === 'ODText') {
|
|
|
|
|
$doc->setDefaultFile('content.xml');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $doc;
|
2013-12-11 16:09:58 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Clear document.
|
2014-03-30 14:15:23 +07:00
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public static function clear(): void
|
2013-12-11 16:09:58 -05:00
|
|
|
{
|
2022-09-16 11:27:29 +02:00
|
|
|
if (self::$file && file_exists(self::$file)) {
|
2014-03-11 14:19:33 -04:00
|
|
|
unlink(self::$file);
|
2014-02-24 19:17:06 +01:00
|
|
|
}
|
2014-08-10 20:00:19 +04:00
|
|
|
if (is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
|
|
|
|
|
self::deleteDir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
|
2014-02-24 19:17:06 +01:00
|
|
|
}
|
2013-12-11 16:09:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Delete directory.
|
2014-03-30 14:15:23 +07:00
|
|
|
*
|
2013-12-11 16:09:58 -05:00
|
|
|
* @param string $dir
|
|
|
|
|
*/
|
2022-09-16 11:45:45 +02:00
|
|
|
public static function deleteDir($dir): void
|
2013-12-11 16:09:58 -05:00
|
|
|
{
|
|
|
|
|
foreach (scandir($dir) as $file) {
|
2015-02-06 22:28:31 +04:00
|
|
|
if ('.' === $file || '..' === $file) {
|
2013-12-11 16:09:58 -05:00
|
|
|
continue;
|
2015-02-06 22:28:31 +04:00
|
|
|
} elseif (is_file($dir . '/' . $file)) {
|
|
|
|
|
unlink($dir . '/' . $file);
|
|
|
|
|
} elseif (is_dir($dir . '/' . $file)) {
|
|
|
|
|
self::deleteDir($dir . '/' . $file);
|
2013-12-11 16:09:58 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rmdir($dir);
|
|
|
|
|
}
|
2014-03-12 11:22:41 -04:00
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Get file.
|
2014-03-30 14:15:23 +07:00
|
|
|
*
|
2014-03-12 11:22:41 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function getFile()
|
|
|
|
|
{
|
|
|
|
|
return self::$file;
|
|
|
|
|
}
|
2014-03-24 00:26:10 +07:00
|
|
|
}
|