Skip to content

Commit 2590555

Browse files
committed
New Tests for Coverage Purposes
1 parent 7cac06e commit 2590555

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

src/PhpWord/Writer/RTF/Element/TextRun.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ public function write()
3434
{
3535
$writer = new Container($this->parentWriter, $this->element);
3636
$this->getStyles();
37+
if ($this->paragraphStyle !== null && $this->paragraphStyle->hasPageBreakBefore()) {
38+
$sect = $this->element->getParent();
39+
if ($sect instanceof \PhpOffice\PhpWord\Element\Section) {
40+
$elems = $sect->getElements();
41+
if ($elems[0] === $this->element) {
42+
$pStyle = clone $this->paragraphStyle;
43+
$pStyle->setPageBreakBefore(false);
44+
$this->paragraphStyle = $pStyle;
45+
}
46+
}
47+
}
3748

3849
$content = '';
3950
$content .= $this->writeOpening();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWordTests\Writer\HTML;
20+
21+
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\SimpleType\Jc;
23+
use PhpOffice\PhpWord\SimpleType\TblWidth;
24+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
25+
use PhpOffice\PhpWord\Writer\HTML;
26+
27+
/**
28+
* Test class for PhpOffice\PhpWord\Writer\RTF\Style subnamespace.
29+
*/
30+
class Sample36RtlTest extends \PHPUnit\Framework\TestCase
31+
{
32+
public function testSample36Rtl(): void
33+
{
34+
$phpWord = new PhpWord();
35+
$section = $phpWord->addSection();
36+
37+
$textrun = $section->addTextRun();
38+
$textrun->addText('This is a Left to Right paragraph.');
39+
40+
$textrun = $section->addTextRun(['alignment' => Jc::END]);
41+
$textrun->addText('سلام این یک پاراگراف راست به چپ است', ['rtl' => true]);
42+
43+
$section->addText('Table visually presented as RTL');
44+
$style = ['rtl' => true, 'size' => 12];
45+
$tableStyle = [
46+
'borderSize' => 6,
47+
'borderColor' => '000000',
48+
'width' => 5000,
49+
'unit' => TblWidth::PERCENT,
50+
'bidiVisual' => true,
51+
];
52+
53+
$table = $section->addTable($tableStyle);
54+
$cellHCentered = ['alignment' => Jc::CENTER];
55+
$cellHEnd = ['alignment' => Jc::END];
56+
$cellVCentered = ['valign' => VerticalJc::CENTER];
57+
58+
//Vidually bidirectinal table
59+
$table->addRow();
60+
$cell = $table->addCell(1500, $cellVCentered);
61+
$textrun = $cell->addTextRun($cellHCentered);
62+
$textrun->addText('ردیف', $style);
63+
64+
$cell = $table->addCell(2000);
65+
$textrun = $cell->addTextRun($cellHEnd);
66+
$textrun->addText('سوالات', $style);
67+
68+
$cell = $table->addCell(1000, $cellVCentered);
69+
$textrun = $cell->addTextRun($cellHCentered);
70+
$textrun->addText('بارم', $style);
71+
$writer = new HTML($phpWord);
72+
$content = $writer->getContent();
73+
$expected = '<table style="table-layout: auto; direction: rtl;';
74+
self::assertStringContainsString($expected, $content);
75+
self::assertSame(6, substr_count($content, 'direction: rtl'));
76+
self::assertSame(1, substr_count($content, '<table'));
77+
}
78+
}

tests/PhpWordTests/Writer/RTF/Sample11Test.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
namespace PhpOffice\PhpWordTests\Writer\RTF;
2020

21+
use PhpOffice\PhpWord\Element\Footer;
2122
use PhpOffice\PhpWord\IOFactory;
23+
use PhpOffice\PhpWord\PhpWord;
2224
use PhpOffice\PhpWord\Writer\RTF;
2325

2426
/**
@@ -39,4 +41,96 @@ public function testSample11(): void
3941
$expected = '\strike even ';
4042
self::assertStringContainsString($expected, $content);
4143
}
44+
45+
public function testBorderColor(): void
46+
{
47+
$phpWord = new PhpWord();
48+
$section = $phpWord->addSection();
49+
$section->getStyle()
50+
->setBorderSize(5)
51+
->setBorderColor('FF00FF');
52+
$writer = new RTF($phpWord);
53+
$content = $writer->getContent();
54+
$expected = '{\colortbl;\red255\green0\blue255;}';
55+
self::assertStringContainsString($expected, $content);
56+
}
57+
58+
public function testFooters(): void
59+
{
60+
$phpWord = new PhpWord();
61+
$section = $phpWord->addSection();
62+
$phpWord->getSettings()->setEvenAndOddHeaders(true);
63+
$section = $phpWord->addSection();
64+
$footerFirst = $section->addFooter(Footer::FIRST);
65+
$textRunFirst = $footerFirst->addTextRun();
66+
$textRunFirst->addText('First Footer');
67+
$footerEven = $section->addFooter(Footer::EVEN);
68+
$textRunEven = $footerEven->addTextRun();
69+
$textRunEven->addText('Even Footer');
70+
$footerAuto = $section->addFooter(Footer::AUTO);
71+
$textRunAuto = $footerAuto->addTextRun();
72+
$textRunAuto->addText('Odd Footer');
73+
$section->addText('This should be page 1');
74+
$section->addPageBreak();
75+
$section->addText('This should be page 2');
76+
$section->addPageBreak();
77+
$section->addText('This should be page 3');
78+
$section->addPageBreak();
79+
$section->addText('This should be page 4');
80+
$section->addPageBreak();
81+
$section->addText('This should be page 5');
82+
$writer = new RTF($phpWord);
83+
$content = $writer->getContent();
84+
$expected = '{\footerf\pard\nowidctlpar {{\cf0\f0 First Footer}}\par';
85+
self::assertStringContainsString($expected, $content);
86+
$expected = '{\footerl\pard\nowidctlpar {{\cf0\f0 Even Footer}}\par';
87+
self::assertStringContainsString($expected, $content);
88+
$expected = '{\footerr\pard\nowidctlpar {{\cf0\f0 Odd Footer}}\par';
89+
self::assertStringContainsString($expected, $content);
90+
}
91+
92+
public function testPageBreakBeforeTextRun(): void
93+
{
94+
$phpWord = new PhpWord();
95+
$phpWord->addParagraphStyle('pbb', [
96+
'pageBreakBefore' => true,
97+
]);
98+
$section1 = $phpWord->addSection();
99+
$textRun1 = $section1->addTextRun();
100+
$textRun1->addText('Section 1 Paragraph 1');
101+
$section2 = $phpWord->addSection();
102+
$textRun2 = $section2->addTextRun('pbb');
103+
$textRun2->addText('Section 2 Paragraph 1');
104+
$textRun3 = $section2->addTextRun('pbb');
105+
$textRun3->addText('Section 2 Paragraph 2');
106+
$writer = new RTF($phpWord);
107+
$content = $writer->getContent();
108+
$expected = '\pard\nowidctlpar {{\cf0\f0 Section 2 Paragraph 1}}\par';
109+
self::assertStringContainsString($expected, $content, 'no page break');
110+
$expected = '\pard\nowidctlpar \page{{\cf0\f0 Section 2 Paragraph 2}}\par';
111+
self::assertStringContainsString($expected, $content, 'page break');
112+
}
113+
114+
public function testPageBreakBeforeTitle(): void
115+
{
116+
$phpWord = new PhpWord();
117+
$pbb = ['pageBreakBefore' => true];
118+
$phpWord->addTitleStyle(
119+
1,
120+
['bold' => true],
121+
$pbb
122+
);
123+
$section1 = $phpWord->addSection();
124+
$textRun1 = $section1->addTextRun();
125+
$textRun1->addText('Section 1 Paragraph 1');
126+
$section2 = $phpWord->addSection();
127+
$section2->addTitle('Heading1 with pbb first element in section', 1);
128+
$section2->addTitle('Heading1 with pbb not first element in section', 1);
129+
$writer = new RTF($phpWord);
130+
$content = $writer->getContent();
131+
$expected = '\pard\nowidctlpar {\outlinelevel0{\cf0\f0\b Heading1 with pbb first element in section}';
132+
self::assertStringContainsString($expected, $content, 'no page break');
133+
$expected = '\pard\nowidctlpar \page{\outlinelevel0{\cf0\f0\b Heading1 with pbb not first element in section}';
134+
self::assertStringContainsString($expected, $content, 'page break');
135+
}
42136
}

0 commit comments

Comments
 (0)