664 lines
24 KiB
PHP
Raw Normal View History

2014-02-24 19:17:06 +01:00
<?php
2014-03-27 23:55:06 +07: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-27 23:55:06 +07: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-27 23:55:06 +07:00
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Writer\Word2007\Part;
2014-02-24 19:17:06 +01:00
2022-09-16 11:45:45 +02:00
use DateTime;
use PhpOffice\PhpWord\ComplexType\FootnoteProperties;
2017-11-15 22:58:28 +01:00
use PhpOffice\PhpWord\Metadata\DocInfo;
use PhpOffice\PhpWord\PhpWord;
2015-10-10 19:22:19 +04:00
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\SimpleType\NumberFormat;
2017-11-22 08:14:22 +01:00
use PhpOffice\PhpWord\Style\Cell;
use PhpOffice\PhpWord\Style\Font;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWordTests\TestHelperDOCX;
2014-02-24 19:17:06 +01:00
/**
2022-09-16 11:45:45 +02:00
* Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Document.
2014-03-27 23:55:06 +07:00
*
2014-02-24 19:17:06 +01:00
* @runTestsInSeparateProcesses
*/
class DocumentTest extends \PHPUnit\Framework\TestCase
{
/**
2022-09-16 11:45:45 +02:00
* Executed before each method of the class.
*/
2022-09-16 11:45:45 +02:00
protected function tearDown(): void
{
TestHelperDOCX::clear();
}
2017-11-15 22:58:28 +01:00
/**
2022-09-16 11:45:45 +02:00
* Write custom properties.
2017-11-15 22:58:28 +01:00
*/
2022-09-16 11:45:45 +02:00
public function testWriteCustomProps(): void
2017-11-15 22:58:28 +01:00
{
$phpWord = new PhpWord();
$docInfo = $phpWord->getDocInfo();
$docInfo->setCustomProperty('key1', null);
$docInfo->setCustomProperty('key2', true);
$docInfo->setCustomProperty('key3', 3);
$docInfo->setCustomProperty('key4', 4.4);
$docInfo->setCustomProperty('key5', 'value5');
2022-09-16 11:45:45 +02:00
$docInfo->setCustomProperty('key6', new DateTime());
2017-11-15 22:58:28 +01:00
$docInfo->setCustomProperty('key7', time(), DocInfo::PROPERTY_TYPE_DATE);
$doc = TestHelperDOCX::getDocument($phpWord);
2022-09-16 11:45:45 +02:00
self::assertNotNull($doc);
2017-11-15 22:58:28 +01:00
// $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key3"]/vt:i4'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key4"]/vt:r8'));
// $this->assertTrue($doc->elementExists('/Properties/property[name="key5"]/vt:lpwstr'));
}
/**
2022-09-16 11:45:45 +02:00
* Write end section page numbering.
*/
2022-09-16 11:45:45 +02:00
public function testWriteEndSectionPageNumbering(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2014-05-31 17:39:54 +07:00
$section->addHeader();
$section->addHeader('first');
$style = $section->getStyle();
$style->setLandscape();
$style->setPageNumberingStart(2);
$style->setBorderSize(240);
$style->setBreakType('nextPage');
$doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:sectPr/w:pgNumType');
2022-09-16 11:45:45 +02:00
self::assertEquals(2, $element->getAttribute('w:start'));
}
/**
2022-09-16 11:45:45 +02:00
* Write section footnote properties.
*/
2022-09-16 11:45:45 +02:00
public function testSectionFootnoteProperties(): void
{
$properties = new FootnoteProperties();
$properties->setPos(FootnoteProperties::POSITION_DOC_END);
$properties->setNumFmt(NumberFormat::LOWER_ROMAN);
$properties->setNumStart(1);
$properties->setNumRestart(FootnoteProperties::RESTART_NUMBER_EACH_PAGE);
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->setFootnoteProperties($properties);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:pos');
2022-09-16 11:45:45 +02:00
self::assertEquals(FootnoteProperties::POSITION_DOC_END, $element->getAttribute('w:val'));
$element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:numFmt');
2022-09-16 11:45:45 +02:00
self::assertEquals(NumberFormat::LOWER_ROMAN, $element->getAttribute('w:val'));
$element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:numStart');
2022-09-16 11:45:45 +02:00
self::assertEquals(1, $element->getAttribute('w:val'));
$element = $doc->getElement('/w:document/w:body/w:sectPr/w:footnotePr/w:numRestart');
2022-09-16 11:45:45 +02:00
self::assertEquals(FootnoteProperties::RESTART_NUMBER_EACH_PAGE, $element->getAttribute('w:val'));
}
/**
2022-09-16 11:45:45 +02:00
* Write elements.
*/
2022-09-16 11:45:45 +02:00
public function testElements(): void
{
$objectSrc = __DIR__ . '/../../../_files/documents/sheet.xls';
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$phpWord->addTitleStyle(1, ['color' => '333333', 'bold' => true]);
$phpWord->addTitleStyle(2, ['color' => '666666']);
$section = $phpWord->addSection();
$section->addTOC();
$section->addPageBreak();
2016-06-04 20:06:37 +04:00
$section->addText('After page break.');
$section->addTitle('Title 1', 1);
$section->addListItem('List Item 1', 0);
$section->addListItem('List Item 2', 0);
$section->addListItem('List Item 3', 0);
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addTitle('Title 2', 2);
$section->addObject($objectSrc);
2022-09-16 11:45:45 +02:00
$section->addTextBox([]);
$section->addTextBox(
2022-09-16 11:45:45 +02:00
[
'wrappingStyle' => 'square',
'positioning' => 'relative',
'posHorizontalRel' => 'margin',
2022-09-16 11:45:45 +02:00
'posVerticalRel' => 'margin',
'innerMargin' => 10,
'borderSize' => 1,
'borderColor' => '#FF0',
]
);
2022-09-16 11:45:45 +02:00
$section->addTextBox(['wrappingStyle' => 'tight', 'positioning' => 'absolute', 'alignment' => Jc::CENTER]);
2016-06-04 20:06:37 +04:00
$section->addListItemRun()->addText('List item run 1');
$section->addField(
'DATE',
2022-09-16 11:45:45 +02:00
['dateformat' => 'dddd d MMMM yyyy H:mm:ss'],
['PreserveFormat', 'LunarCalendar']
);
$section->addField(
'DATE',
2022-09-16 11:45:45 +02:00
['dateformat' => 'dddd d MMMM yyyy H:mm:ss'],
['PreserveFormat', 'SakaEraCalendar']
);
$section->addField(
'DATE',
2022-09-16 11:45:45 +02:00
['dateformat' => 'dddd d MMMM yyyy H:mm:ss'],
['PreserveFormat', 'LastUsedFormat']
);
2022-09-16 11:45:45 +02:00
$section->addField('PAGE', ['format' => 'ArabicDash']);
$section->addLine(
2022-09-16 11:45:45 +02:00
[
'width' => 10,
'height' => 10,
'positioning' => 'absolute',
2022-09-16 11:45:45 +02:00
'beginArrow' => 'block',
'endArrow' => 'open',
'dash' => 'rounddot',
'weight' => 10,
]
);
2014-05-10 23:40:34 +07:00
$doc = TestHelperDOCX::getDocument($phpWord);
// TOC
$element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:tabs/w:tab');
2022-09-16 11:45:45 +02:00
self::assertEquals('right', $element->getAttribute('w:val'));
self::assertEquals('dot', $element->getAttribute('w:leader'));
self::assertEquals(9062, $element->getAttribute('w:pos'));
// Page break
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:br');
2022-09-16 11:45:45 +02:00
self::assertEquals('page', $element->getAttribute('w:type'));
// Title
2014-06-12 02:25:34 +07:00
$element = $doc->getElement('/w:document/w:body/w:p[6]/w:pPr/w:pStyle');
2022-09-16 11:45:45 +02:00
self::assertEquals('Heading1', $element->getAttribute('w:val'));
// List item
2014-06-12 02:25:34 +07:00
$element = $doc->getElement('/w:document/w:body/w:p[7]/w:pPr/w:numPr/w:numId');
2022-09-16 11:45:45 +02:00
self::assertEquals(3, $element->getAttribute('w:val'));
// Object
2014-06-12 02:25:34 +07:00
$element = $doc->getElement('/w:document/w:body/w:p[12]/w:r/w:object/o:OLEObject');
2022-09-16 11:45:45 +02:00
self::assertEquals('Embed', $element->getAttribute('Type'));
}
/**
2022-09-16 11:45:45 +02:00
* Write element with some styles.
*/
2022-09-16 11:45:45 +02:00
public function testElementStyles(): void
{
$objectSrc = __DIR__ . '/../../../_files/documents/sheet.xls';
2022-09-16 11:45:45 +02:00
$tabs = [new \PhpOffice\PhpWord\Style\Tab('right', 9090)];
$phpWord = new PhpWord();
$phpWord->addParagraphStyle(
'pStyle',
2022-09-16 11:45:45 +02:00
[
'alignment' => Jc::CENTER,
'tabs' => $tabs,
'shading' => ['fill' => 'FFFF99'],
'borderSize' => 4,
2022-09-16 11:45:45 +02:00
]
); // Style #1
$phpWord->addFontStyle(
'fStyle',
2022-09-16 11:45:45 +02:00
[
'size' => '20',
'bold' => true,
'allCaps' => true,
2022-09-16 11:45:45 +02:00
'scale' => 200,
'spacing' => 240,
'kerning' => 10,
2022-09-16 11:45:45 +02:00
]
); // Style #2
2022-09-16 11:45:45 +02:00
$phpWord->addTitleStyle(1, ['color' => '333333', 'doubleStrikethrough' => true]); // Style #3
$phpWord->addTableStyle('tStyle', ['borderSize' => 1]);
$fontStyle = new Font('text', ['alignment' => Jc::CENTER]);
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addListItem('List Item', 0, null, null, 'pStyle'); // Style #5
2022-09-16 11:45:45 +02:00
$section->addObject($objectSrc, ['alignment' => Jc::CENTER]);
$section->addTOC($fontStyle);
2016-06-04 20:06:37 +04:00
$section->addTitle('Title 1', 1);
$section->addTOC('fStyle');
$table = $section->addTable('tStyle');
$table->setWidth(100);
$doc = TestHelperDOCX::getDocument($phpWord);
// List item
$element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:numPr/w:numId');
2022-09-16 11:45:45 +02:00
self::assertEquals(5, $element->getAttribute('w:val'));
// Object
$element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:object/o:OLEObject');
2022-09-16 11:45:45 +02:00
self::assertEquals('Embed', $element->getAttribute('Type'));
// TOC
$element = $doc->getElement('/w:document/w:body/w:p[3]/w:pPr/w:tabs/w:tab');
2022-09-16 11:45:45 +02:00
self::assertEquals('right', $element->getAttribute('w:val'));
self::assertEquals('dot', $element->getAttribute('w:leader'));
self::assertEquals(9062, $element->getAttribute('w:pos'));
}
2014-03-14 04:25:09 +07:00
/**
2022-09-16 11:45:45 +02:00
* Test write text element.
2014-03-14 04:25:09 +07:00
*/
2022-09-16 11:45:45 +02:00
public function testWriteText(): void
{
$rStyle = 'rStyle';
$pStyle = 'pStyle';
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$phpWord->addFontStyle($rStyle, ['bold' => true]);
$phpWord->addParagraphStyle($pStyle, ['hanging' => 120, 'indent' => 120]);
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addText('Test', $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = '/w:document/w:body/w:p/w:r/w:rPr/w:rStyle';
2022-09-16 11:45:45 +02:00
self::assertEquals($rStyle, $doc->getElementAttribute($element, 'w:val'));
$element = '/w:document/w:body/w:p/w:pPr/w:pStyle';
2022-09-16 11:45:45 +02:00
self::assertEquals($pStyle, $doc->getElementAttribute($element, 'w:val'));
}
/**
2022-09-16 11:45:45 +02:00
* Test write textrun element.
*/
2022-09-16 11:45:45 +02:00
public function testWriteTextRun(): void
{
$pStyle = 'pStyle';
2022-09-16 11:45:45 +02:00
$aStyle = ['alignment' => Jc::BOTH, 'spaceBefore' => 120, 'spaceAfter' => 120];
$imageSrc = __DIR__ . '/../../../_files/images/earth.jpg';
$phpWord = new PhpWord();
$phpWord->addParagraphStyle($pStyle, $aStyle);
2016-06-04 20:06:37 +04:00
$section = $phpWord->addSection('Test');
$textrun = $section->addTextRun($pStyle);
2016-06-04 20:06:37 +04:00
$textrun->addText('Test');
$textrun->addTextBreak();
$textrun = $section->addTextRun($aStyle);
$textrun->addLink('https://github.com/PHPOffice/PHPWord');
2022-09-16 11:45:45 +02:00
$textrun->addImage($imageSrc, ['alignment' => Jc::CENTER]);
$textrun->addFootnote();
$doc = TestHelperDOCX::getDocument($phpWord);
$parent = '/w:document/w:body/w:p';
2022-09-16 11:45:45 +02:00
self::assertTrue($doc->elementExists("{$parent}/w:pPr/w:pStyle[@w:val='{$pStyle}']"));
}
/**
2022-09-16 11:45:45 +02:00
* Test write link element.
*/
2022-09-16 11:45:45 +02:00
public function testWriteLink(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2022-09-16 11:45:45 +02:00
$fontStyleArray = ['bold' => true];
2014-03-29 22:26:00 +07:00
$fontStyleName = 'Font Style';
2022-09-16 11:45:45 +02:00
$paragraphStyleArray = ['alignment' => Jc::CENTER];
2014-03-29 22:26:00 +07:00
$paragraphStyleName = 'Paragraph Style';
$expected = 'PHPWord on GitHub';
2016-06-04 20:06:37 +04:00
$section->addLink('https://github.com/PHPOffice/PHPWord', $expected);
$section->addLink('https://github.com/PHPOffice/PHPWord', 'Test', $fontStyleArray, $paragraphStyleArray);
$section->addLink('https://github.com/PHPOffice/PHPWord', 'Test', $fontStyleName, $paragraphStyleName);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:hyperlink/w:r/w:t');
2022-09-16 11:45:45 +02:00
self::assertEquals($expected, $element->nodeValue);
}
/**
2022-09-16 11:45:45 +02:00
* Test write preserve text element.
*/
2022-09-16 11:45:45 +02:00
public function testWritePreserveText(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$footer = $section->addFooter();
2022-09-16 11:45:45 +02:00
$fontStyleArray = ['bold' => true];
$fontStyleName = 'Font';
2022-09-16 11:45:45 +02:00
$paragraphStyleArray = ['alignment' => Jc::END];
$paragraphStyleName = 'Paragraph';
2016-06-04 20:06:37 +04:00
$footer->addPreserveText('Page {PAGE}');
$footer->addPreserveText('{PAGE}', $fontStyleArray, $paragraphStyleArray);
$footer->addPreserveText('{PAGE}', $fontStyleName, $paragraphStyleName);
$doc = TestHelperDOCX::getDocument($phpWord);
$preserve = $doc->getElement('w:p/w:r[2]/w:instrText', 'word/footer1.xml');
2022-09-16 11:45:45 +02:00
self::assertEquals('PAGE', $preserve->nodeValue);
self::assertEquals('preserve', $preserve->getAttribute('xml:space'));
}
/**
2022-09-16 11:45:45 +02:00
* Test write text break.
*/
2022-09-16 11:45:45 +02:00
public function testWriteTextBreak(): void
{
2022-09-16 11:45:45 +02:00
$fArray = ['size' => 12];
$pArray = ['spacing' => 240];
$fName = 'fStyle';
$pName = 'pStyle';
$phpWord = new PhpWord();
$phpWord->addFontStyle($fName, $fArray);
$phpWord->addParagraphStyle($pName, $pArray);
$section = $phpWord->addSection();
$section->addTextBreak();
$section->addTextBreak(1, $fArray, $pArray);
$section->addTextBreak(1, $fName, $pName);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:rPr/w:rStyle');
2022-09-16 11:45:45 +02:00
self::assertEquals($fName, $element->getAttribute('w:val'));
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:pStyle');
2022-09-16 11:45:45 +02:00
self::assertEquals($pName, $element->getAttribute('w:val'));
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeImage.
*/
2022-09-16 11:45:45 +02:00
public function testWriteImage(): void
{
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$styles = ['alignment' => Jc::START, 'width' => 40, 'height' => 40, 'marginTop' => -1, 'marginLeft' => -1];
$wraps = ['inline', 'behind', 'infront', 'square', 'tight'];
$section = $phpWord->addSection();
foreach ($wraps as $wrap) {
$styles['wrappingStyle'] = $wrap;
$section->addImage(__DIR__ . '/../../../_files/images/earth.jpg', $styles);
}
$archiveFile = realpath(__DIR__ . '/../../../_files/documents/reader.docx');
$imageFile = 'word/media/image1.jpeg';
2014-04-16 08:04:18 +07:00
$source = 'zip://' . $archiveFile . '#' . $imageFile;
$section->addImage($source);
$doc = TestHelperDOCX::getDocument($phpWord);
// behind
$element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape');
$style = $element->getAttribute('style');
2022-09-25 21:43:00 +02:00
self::assertRegExp('/z\-index:\-[0-9]*/', $style);
// square
$element = $doc->getElement('/w:document/w:body/w:p[4]/w:r/w:pict/v:shape/w10:wrap');
2022-09-16 11:45:45 +02:00
self::assertEquals('square', $element->getAttribute('type'));
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeWatermark.
*/
2022-09-16 11:45:45 +02:00
public function testWriteWatermark(): void
{
$imageSrc = __DIR__ . '/../../../_files/images/earth.jpg';
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$header = $section->addHeader();
$header->addWatermark($imageSrc);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = $doc->getElement('/w:document/w:body/w:sectPr/w:headerReference');
2022-09-16 11:45:45 +02:00
self::assertStringStartsWith('rId', $element->getAttribute('r:id'));
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeTitle.
*/
2022-09-16 11:45:45 +02:00
public function testWriteTitle(): void
{
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$phpWord->addTitleStyle(1, ['bold' => true], ['spaceAfter' => 240]);
2016-06-04 20:06:37 +04:00
$phpWord->addSection()->addTitle('Test', 1);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = '/w:document/w:body/w:p/w:pPr/w:pStyle';
2022-09-16 11:45:45 +02:00
self::assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeCheckbox.
*/
2022-09-16 11:45:45 +02:00
public function testWriteCheckbox(): void
{
$rStyle = 'rStyle';
$pStyle = 'pStyle';
$phpWord = new PhpWord();
// $phpWord->addFontStyle($rStyle, array('bold' => true));
// $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addCheckBox('Check1', 'Test', $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name';
2022-09-16 11:45:45 +02:00
self::assertEquals('Check1', $doc->getElementAttribute($element, 'w:val'));
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeParagraphStyle.
*/
2022-09-16 11:45:45 +02:00
public function testWriteParagraphStyle(): void
{
// Create the doc
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2022-09-16 11:45:45 +02:00
$attributes = [
'alignment' => Jc::END,
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
2022-09-16 11:45:45 +02:00
];
foreach ($attributes as $attribute => $value) {
2022-09-16 11:45:45 +02:00
$section->addText('Test', null, [$attribute => $value]);
}
$doc = TestHelperDOCX::getDocument($phpWord);
// Test the attributes
$attributeCount = 0;
foreach ($attributes as $key => $value) {
++$attributeCount;
$nodeName = ($key == 'alignment') ? 'jc' : $key;
$path = "/w:document/w:body/w:p[{$attributeCount}]/w:pPr/w:{$nodeName}";
if ('alignment' != $key) {
$value = $value ? 1 : 0;
}
$element = $doc->getElement($path);
2022-09-16 11:45:45 +02:00
self::assertEquals($value, $element->getAttribute('w:val'));
}
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeTextStyle.
*/
2022-09-16 11:45:45 +02:00
public function testWriteFontStyle(): void
{
$phpWord = new PhpWord();
$styles['name'] = 'Verdana';
$styles['size'] = 14;
$styles['bold'] = true;
$styles['italic'] = true;
$styles['underline'] = 'dash';
$styles['strikethrough'] = true;
$styles['superScript'] = true;
$styles['color'] = 'FF0000';
$styles['fgColor'] = 'yellow';
$styles['bgColor'] = 'FFFF00';
$styles['hint'] = 'eastAsia';
$styles['smallCaps'] = true;
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addText('Test', $styles);
$doc = TestHelperDOCX::getDocument($phpWord);
$parent = '/w:document/w:body/w:p/w:r/w:rPr';
2022-09-16 11:45:45 +02:00
self::assertEquals($styles['name'], $doc->getElementAttribute("{$parent}/w:rFonts", 'w:ascii'));
self::assertEquals($styles['size'] * 2, $doc->getElementAttribute("{$parent}/w:sz", 'w:val'));
self::assertTrue($doc->elementExists("{$parent}/w:b"));
self::assertTrue($doc->elementExists("{$parent}/w:i"));
self::assertEquals($styles['underline'], $doc->getElementAttribute("{$parent}/w:u", 'w:val'));
self::assertTrue($doc->elementExists("{$parent}/w:strike"));
self::assertEquals('superscript', $doc->getElementAttribute("{$parent}/w:vertAlign", 'w:val'));
self::assertEquals($styles['color'], $doc->getElementAttribute("{$parent}/w:color", 'w:val'));
self::assertEquals($styles['fgColor'], $doc->getElementAttribute("{$parent}/w:highlight", 'w:val'));
self::assertTrue($doc->elementExists("{$parent}/w:smallCaps"));
}
2017-11-22 08:14:22 +01:00
/**
2022-09-16 11:45:45 +02:00
* Tests that if no color is set on a cell a border gets writen with the default color.
2017-11-22 08:14:22 +01:00
*/
2022-09-16 11:45:45 +02:00
public function testWriteDefaultColor(): void
2017-11-22 08:14:22 +01:00
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$cStyles['borderTopSize'] = 120;
$table = $section->addTable();
$table->addRow();
$cell = $table->addCell(null, $cStyles);
$cell->addText('Test');
$doc = TestHelperDOCX::getDocument($phpWord);
2022-09-16 11:45:45 +02:00
self::assertEquals(Cell::DEFAULT_BORDER_COLOR, $doc->getElementAttribute('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:tcBorders/w:top', 'w:color'));
2017-11-22 08:14:22 +01:00
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeTableStyle.
*/
2022-09-16 11:45:45 +02:00
public function testWriteTableStyle(): void
{
$phpWord = new PhpWord();
$rHeight = 120;
$cWidth = 120;
$imageSrc = __DIR__ . '/../../../_files/images/earth.jpg';
$objectSrc = __DIR__ . '/../../../_files/documents/sheet.xls';
$tStyles['width'] = 50;
$tStyles['cellMarginTop'] = 120;
$tStyles['cellMarginRight'] = 120;
$tStyles['cellMarginBottom'] = 120;
$tStyles['cellMarginLeft'] = 120;
$rStyles['tblHeader'] = true;
$rStyles['cantSplit'] = true;
$cStyles['valign'] = 'top';
$cStyles['textDirection'] = 'btLr';
$cStyles['bgColor'] = 'FF0000';
$cStyles['borderTopSize'] = 120;
$cStyles['borderBottomSize'] = 120;
$cStyles['borderLeftSize'] = 120;
$cStyles['borderRightSize'] = 120;
$cStyles['borderTopColor'] = 'FF0000';
$cStyles['borderBottomColor'] = 'FF0000';
$cStyles['borderLeftColor'] = 'FF0000';
$cStyles['borderRightColor'] = 'FF0000';
$cStyles['vMerge'] = 'restart';
$section = $phpWord->addSection();
$table = $section->addTable($tStyles);
2017-11-22 08:14:22 +01:00
$table->setWidth(100);
$table->addRow($rHeight, $rStyles);
$cell = $table->addCell($cWidth, $cStyles);
2016-06-04 20:06:37 +04:00
$cell->addText('Test');
$cell->addTextBreak();
$cell->addLink('https://github.com/PHPOffice/PHPWord');
2016-06-04 20:06:37 +04:00
$cell->addListItem('Test');
2014-03-29 22:26:00 +07:00
$cell->addImage($imageSrc);
$cell->addObject($objectSrc);
$textrun = $cell->addTextRun();
2016-06-04 20:06:37 +04:00
$textrun->addText('Test');
$doc = TestHelperDOCX::getDocument($phpWord);
$parent = '/w:document/w:body/w:tbl/w:tblPr/w:tblCellMar';
$parent = '/w:document/w:body/w:tbl/w:tr/w:trPr';
2022-09-16 11:45:45 +02:00
self::assertEquals($rHeight, $doc->getElementAttribute("{$parent}/w:trHeight", 'w:val'));
self::assertEquals($rStyles['tblHeader'], $doc->getElementAttribute("{$parent}/w:tblHeader", 'w:val'));
self::assertEquals($rStyles['cantSplit'], $doc->getElementAttribute("{$parent}/w:cantSplit", 'w:val'));
$parent = '/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr';
2022-09-16 11:45:45 +02:00
self::assertEquals($cWidth, $doc->getElementAttribute("{$parent}/w:tcW", 'w:w'));
self::assertEquals($cStyles['valign'], $doc->getElementAttribute("{$parent}/w:vAlign", 'w:val'));
self::assertEquals($cStyles['textDirection'], $doc->getElementAttribute("{$parent}/w:textDirection", 'w:val'));
}
/**
2022-09-16 11:45:45 +02:00
* covers ::_writeCellStyle.
*/
2022-09-16 11:45:45 +02:00
public function testWriteCellStyleCellGridSpan(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
2014-02-24 19:38:44 +01:00
$table = $section->addTable();
$table->addRow();
$cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);
$table->addRow();
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$table->addCell(40);
$table->addRow();
2022-09-16 11:45:45 +02:00
$cell = $table->addCell(200, ['borderRightColor' => 'FF0000']);
$cell->getStyle()->setGridSpan(5);
$doc = TestHelperDOCX::getDocument($phpWord);
2014-02-24 19:38:44 +01:00
$element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan');
2022-09-16 11:45:45 +02:00
self::assertEquals(5, $element->getAttribute('w:val'));
}
/**
2022-09-16 11:45:45 +02:00
* Test write gutter and line numbering.
*/
2022-09-16 11:45:45 +02:00
public function testWriteGutterAndLineNumbering(): void
{
$pageMarginPath = '/w:document/w:body/w:sectPr/w:pgMar';
$lineNumberingPath = '/w:document/w:body/w:sectPr/w:lnNumType';
$phpWord = new PhpWord();
2022-09-16 11:45:45 +02:00
$phpWord->addSection(['gutter' => 240, 'lineNumbering' => []]);
$doc = TestHelperDOCX::getDocument($phpWord);
2022-09-16 11:45:45 +02:00
self::assertEquals(240, $doc->getElement($pageMarginPath)->getAttribute('w:gutter'));
self::assertTrue($doc->elementExists($lineNumberingPath));
}
}