-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathWord2007Test.php
264 lines (225 loc) · 9.78 KB
/
Word2007Test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* 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.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWordTests\Reader;
use DateTime;
use PhpOffice\Math\Element;
use PhpOffice\PhpWord\Element\Comment;
use PhpOffice\PhpWord\Element\Formula;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWordTests\TestHelperDOCX;
/**
* Test class for PhpOffice\PhpWord\Reader\Word2007.
*
* @coversDefaultClass \PhpOffice\PhpWord\Reader\Word2007
*
* @runTestsInSeparateProcesses
*/
class Word2007Test extends \PHPUnit\Framework\TestCase
{
/**
* Tear down after each test.
*/
protected function tearDown(): void
{
TestHelperDOCX::clear();
}
/**
* Test canRead() method.
*/
public function testCanRead(): void
{
$object = new Word2007();
self::assertTrue($object->canRead(dirname(__DIR__, 1) . '/_files/documents/reader.docx'));
}
/**
* Can read exception.
*/
public function testCanReadFailed(): void
{
$object = new Word2007();
self::assertFalse($object->canRead(dirname(__DIR__, 1) . '/_files/documents/foo.docx'));
}
/**
* Load.
*/
public function testLoad(): void
{
$phpWord = IOFactory::load(dirname(__DIR__, 1) . '/_files/documents/reader.docx', 'Word2007');
self::assertInstanceOf(PhpWord::class, $phpWord);
self::assertTrue($phpWord->getSettings()->hasDoNotTrackMoves());
self::assertFalse($phpWord->getSettings()->hasDoNotTrackFormatting());
self::assertEquals(100, $phpWord->getSettings()->getZoom());
$doc = TestHelperDOCX::getDocument($phpWord);
self::assertEquals('0', $doc->getElementAttribute('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b', 'w:val'));
}
public function testLoadStyles(): void
{
$phpWord = IOFactory::load(dirname(__DIR__, 1) . '/_files/documents/reader-styles.docx', 'Word2007');
self::assertInstanceOf(PhpWord::class, $phpWord);
$section2 = $phpWord->getSection(2);
self::assertInstanceOf(Section::class, $section2);
$element2e31 = $section2->getElement(31);
self::assertInstanceOf(TextRun::class, $element2e31);
self::assertEquals('This is a paragraph with border differents', $element2e31->getText());
/** @var Paragraph $element2e31pStyle */
$element2e31pStyle = $element2e31->getParagraphStyle();
self::assertInstanceOf(Paragraph::class, $element2e31pStyle);
// Top
self::assertEquals('FFFF00', $element2e31pStyle->getBorderTopColor());
self::assertEquals('10', $element2e31pStyle->getBorderTopSize());
self::assertEquals('dotted', $element2e31pStyle->getBorderTopStyle());
// Right
self::assertEquals('00A933', $element2e31pStyle->getBorderRightColor());
self::assertEquals('4', $element2e31pStyle->getBorderRightSize());
self::assertEquals('dashed', $element2e31pStyle->getBorderRightStyle());
// Bottom
self::assertEquals('F10D0C', $element2e31pStyle->getBorderBottomColor());
self::assertEquals('8', $element2e31pStyle->getBorderBottomSize());
self::assertEquals('dashSmallGap', $element2e31pStyle->getBorderBottomStyle());
// Left
self::assertEquals('3465A4', $element2e31pStyle->getBorderLeftColor());
self::assertEquals('8', $element2e31pStyle->getBorderLeftSize());
self::assertEquals('dashed', $element2e31pStyle->getBorderLeftStyle());
}
/**
* Load a Word 2011 file.
*/
public function testLoadWord2011(): void
{
$reader = new Word2007();
$phpWord = $reader->load(dirname(__DIR__, 1) . '/_files/documents/reader-2011.docx');
self::assertInstanceOf(PhpWord::class, $phpWord);
$doc = TestHelperDOCX::getDocument($phpWord);
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[3]/w:r/w:pict/v:shape/v:imagedata'));
}
/**
* Load a Word without/withoutImages.
*
* @dataProvider providerSettingsImageLoading
*/
public function testLoadWord2011SettingsImageLoading(bool $hasImageLoading): void
{
$reader = new Word2007();
$reader->setImageLoading($hasImageLoading);
$phpWord = $reader->load(dirname(__DIR__, 1) . '/_files/documents/reader-2011.docx');
self::assertInstanceOf(PhpWord::class, $phpWord);
$sections = $phpWord->getSections();
self::assertCount(1, $sections);
$section = $sections[0];
$elements = $section->getElements();
self::assertCount(3, $elements);
$element = $elements[2];
self::assertInstanceOf(TextRun::class, $element);
$subElements = $element->getElements();
if ($hasImageLoading) {
self::assertCount(1, $subElements);
$subElement = $subElements[0];
self::assertInstanceOf(Image::class, $subElement);
} else {
self::assertCount(0, $subElements);
}
}
public static function providerSettingsImageLoading(): iterable
{
return [
[true],
[false],
];
}
public function testLoadComments(): void
{
$phpWord = IOFactory::load(dirname(__DIR__, 1) . '/_files/documents/reader-comments.docx');
self::assertInstanceOf(PhpWord::class, $phpWord);
self::assertEquals(2, $phpWord->getComments()->countItems());
/** @var Comment $comment */
$comment = $phpWord->getComments()->getItem(0);
self::assertInstanceOf(Comment::class, $comment);
self::assertEquals('shaedrich', $comment->getAuthor());
self::assertEquals(new DateTime('2021-10-28T13:56:55Z'), $comment->getDate());
self::assertEquals('SH', $comment->getInitials());
self::assertCount(1, $comment->getElements());
self::assertInstanceOf(Text::class, $comment->getElement(0));
self::assertEquals('This this be lowercase', $comment->getElement(0)->getText());
/** @var Font $fontStyle */
$fontStyle = $comment->getElement(0)->getFontStyle();
self::assertInstanceOf(Font::class, $fontStyle);
self::assertEquals('de-DE', $fontStyle->getLang()->getLatin());
/** @var Comment $comment */
$comment = $phpWord->getComments()->getItem(1);
self::assertInstanceOf(Comment::class, $comment);
self::assertEquals('shaedrich', $comment->getAuthor());
self::assertEquals(new DateTime('2021-11-02T19:10:00Z'), $comment->getDate());
self::assertEquals('SH', $comment->getInitials());
self::assertCount(1, $comment->getElements());
self::assertInstanceOf(Text::class, $comment->getElement(0));
self::assertEquals('But this should be uppercase', $comment->getElement(0)->getText());
/** @var Font $fontStyle */
$fontStyle = $comment->getElement(0)->getFontStyle();
self::assertInstanceOf(Font::class, $fontStyle);
self::assertEquals('de-DE', $fontStyle->getLang()->getLatin());
}
public function testLoadFormula(): void
{
$phpWord = IOFactory::load(dirname(__DIR__, 1) . '/_files/documents/reader-formula.docx');
self::assertInstanceOf(PhpWord::class, $phpWord);
$sections = $phpWord->getSections();
self::assertCount(1, $sections);
$section = $sections[0];
self::assertInstanceOf(Section::class, $section);
$elements = $section->getElements();
self::assertCount(1, $elements);
$element = $elements[0];
self::assertInstanceOf(Formula::class, $element);
$elements = $element->getMath()->getElements();
self::assertCount(5, $elements);
/** @var Element\Fraction $element */
$element = $elements[0];
self::assertInstanceOf(Element\Fraction::class, $element);
/** @var Element\Identifier $numerator */
$numerator = $element->getNumerator();
self::assertInstanceOf(Element\Identifier::class, $numerator);
self::assertEquals('π', $numerator->getValue());
/** @var Element\Numeric $denominator */
$denominator = $element->getDenominator();
self::assertInstanceOf(Element\Numeric::class, $denominator);
self::assertEquals(2, $denominator->getValue());
/** @var Element\Operator $element */
$element = $elements[1];
self::assertInstanceOf(Element\Operator::class, $element);
self::assertEquals('+', $element->getValue());
/** @var Element\Identifier $element */
$element = $elements[2];
self::assertInstanceOf(Element\Identifier::class, $element);
self::assertEquals('a', $element->getValue());
/** @var Element\Operator $element */
$element = $elements[3];
self::assertInstanceOf(Element\Operator::class, $element);
self::assertEquals('∗', $element->getValue());
/** @var Element\Numeric $element */
$element = $elements[4];
self::assertInstanceOf(Element\Numeric::class, $element);
self::assertEquals(2, $element->getValue());
}
}