Skip to content

Commit d5d6b9c

Browse files
committed
fix: TOC pages numbers
1 parent 79effbe commit d5d6b9c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Enhancements
66

77
- IOFactory : Added extractVariables method to extract variables from a document [@sibalonat](https://github.com/sibalonat) in [#2515](https://github.com/PHPOffice/PHPWord/pull/2515)
8+
- TOC : Corrected generating TOC to fix page number missing issues [@jgiacomello](https://github.com/jgiacomello) in [#2556](https://github.com/PHPOffice/PHPWord/pull/2556)
89

910
### Bug fixes
1011

tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,29 @@ public function testWriteTitlePageNumber(): void
5353
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[1]/w:hyperlink/w:r[6]/w:t'));
5454
self::assertEquals($expectedPageNum, $doc->getElement('/w:document/w:body/w:p[1]/w:hyperlink/w:r[6]/w:t')->textContent);
5555
}
56+
57+
public function testWriteTitleWithoutpageNumber(): void
58+
{
59+
$phpWord = new PhpWord();
60+
61+
$section = $phpWord->addSection();
62+
$section->addTOC();
63+
64+
//more than one title and random text for create more than one page
65+
for ($i = 1; $i <= 10; $i++) {
66+
$section->addTitle('Title ' . $i, 1);
67+
$content = file_get_contents('https://loripsum.net/api/10/long');
68+
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $content ? $content : '', false, false);
69+
$section->addPageBreak();
70+
}
71+
72+
$doc = TestHelperDOCX::getDocument($phpWord);
73+
74+
for ($i = 1; $i <= 1; $i++) {
75+
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[1]/w:t'));
76+
self::assertEquals('Title ' . $i, $doc->getElement('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[1]/w:t')->textContent);
77+
self::assertTrue($doc->elementExists('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText'));
78+
self::assertEquals('preserve', $doc->getElementAttribute('/w:document/w:body/w:p[' . $i . ']/w:hyperlink/w:r[4]/w:instrText', 'xml:space'));
79+
}
80+
}
5681
}

0 commit comments

Comments
 (0)