Skip to content

Commit e2730d6

Browse files
committed
UnitTest and fixes
1 parent f2c266c commit e2730d6

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/PhpWord/Writer/Word2007/Element/Field.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,22 @@ private function convertRefOption($optionKey, $optionValue)
296296
}
297297

298298
switch ($optionValue) {
299-
case 'IncrementAndInsertText': return '\\f';
300-
case 'CreateHyperLink': return '\\h';
301-
case 'NoTrailingPeriod': return '\\n';
302-
case 'IncludeAboveOrBelow': return '\\p';
303-
case 'InsertParagraphNumberRelativeContext': return '\\r';
304-
case 'SuppressNonDelimiterNonNumericalText': return '\\t';
305-
case 'InsertParagraphNumberFullContext': return '\\w';
306-
default: return '';
299+
case 'IncrementAndInsertText':
300+
return '\\f';
301+
case 'CreateHyperLink':
302+
return '\\h';
303+
case 'NoTrailingPeriod':
304+
return '\\n';
305+
case 'IncludeAboveOrBelow':
306+
return '\\p';
307+
case 'InsertParagraphNumberRelativeContext':
308+
return '\\r';
309+
case 'SuppressNonDelimiterNonNumericalText':
310+
return '\\t';
311+
case 'InsertParagraphNumberFullContext':
312+
return '\\w';
313+
default:
314+
return '';
307315
}
308316
}
309317
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
4+
5+
use PhpOffice\PhpWord\PhpWord;
6+
use PhpOffice\PhpWord\TestHelperDOCX;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* Test class for PhpOffice\PhpWord\Writer\Word2007\Field
11+
*/
12+
class FieldTest extends TestCase
13+
{
14+
/**
15+
* Executed before each method of the class
16+
*/
17+
public function tearDown()
18+
{
19+
TestHelperDOCX::clear();
20+
}
21+
22+
/**
23+
* Test Field write
24+
*/
25+
public function testWriteWithRefType()
26+
{
27+
$phpWord = new PhpWord();
28+
$section = $phpWord->addSection();
29+
$section->addField(
30+
'REF',
31+
array(
32+
'name' => 'my-bookmark',
33+
),
34+
array(
35+
'InsertParagraphNumberRelativeContext',
36+
'CreateHyperLink',
37+
)
38+
);
39+
40+
$section->addListItem('line one item');
41+
$section->addListItem('line two item');
42+
$section->addBookmark('my-bookmark');
43+
$section->addListItem('line three item');
44+
45+
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
46+
47+
$refFieldPath = '/w:document/w:body/w:p[1]/w:r[2]/w:instrText';
48+
49+
$this->assertTrue($doc->elementExists($refFieldPath));
50+
51+
$bookMarkElement = $doc->getElement($refFieldPath);
52+
53+
$this->assertNotNull($bookMarkElement);
54+
55+
$this->assertEquals(' REF my-bookmark \r \h ', $bookMarkElement->textContent);
56+
57+
$bookmarkPath = '/w:document/w:body/w:bookmarkStart';
58+
59+
$this->assertTrue($doc->elementExists($bookmarkPath));
60+
$this->assertEquals('my-bookmark', $doc->getElementAttribute("$bookmarkPath", 'w:name'));
61+
}
62+
}

0 commit comments

Comments
 (0)