|
| 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\Shared; |
| 20 | + |
| 21 | +use Exception; |
| 22 | +use PhpOffice\PhpWord\PhpWord; |
| 23 | +use PhpOffice\PhpWord\Shared\Html; |
| 24 | +use PhpOffice\PhpWordTests\AbstractWebServerEmbedded; |
| 25 | +use PhpOffice\PhpWordTests\TestHelperDOCX; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test class for PhpOffice\PhpWord\Shared\Html. |
| 29 | + * |
| 30 | + * @coversDefaultClass \PhpOffice\PhpWord\Shared\Html |
| 31 | + */ |
| 32 | +class Html2Test extends AbstractWebServerEmbedded |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Tear down after each test. |
| 36 | + */ |
| 37 | + protected function tearDown(): void |
| 38 | + { |
| 39 | + TestHelperDOCX::clear(); |
| 40 | + } |
| 41 | + |
| 42 | + public function testException(): void |
| 43 | + { |
| 44 | + $this->expectException(Exception::class); |
| 45 | + $this->expectExceptionMessage('loadHTML'); |
| 46 | + $phpWord = new PhpWord(); |
| 47 | + $section = $phpWord->addSection(); |
| 48 | + Html::addHtml($section, ''); |
| 49 | + } |
| 50 | + |
| 51 | + public function testCssOnIdElement(): void |
| 52 | + { |
| 53 | + $phpWord = new PhpWord(); |
| 54 | + $section = $phpWord->addSection(); |
| 55 | + $html = '<html>' |
| 56 | + . '<head>' |
| 57 | + . '<title>Id Test</title>' |
| 58 | + . '<style>#bold1 {font-weight: bold; margin: 10px;}</style>' |
| 59 | + . '</head><body>' |
| 60 | + . '<p id="bold1">test1.</p>' |
| 61 | + . '</body></html>'; |
| 62 | + Html::addHtml($section, $html); |
| 63 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 64 | + $marginPath = '/w:document/w:body/w:p/w:pPr/w:spacing'; |
| 65 | + self::assertSame('150', $doc->getElement($marginPath)->getAttribute('w:before')); |
| 66 | + self::assertSame('150', $doc->getElement($marginPath)->getAttribute('w:after')); |
| 67 | + $path = '/w:document/w:body/w:p/w:r'; |
| 68 | + self::assertSame('test1.', $doc->getElement($path)->nodeValue); |
| 69 | + $boldPath = $path . '/w:rPr/w:b'; |
| 70 | + self::assertSame('1', $doc->getElement($boldPath)->getAttribute('w:val')); |
| 71 | + } |
| 72 | + |
| 73 | + public function testListTypes(): void |
| 74 | + { |
| 75 | + $phpWord = new PhpWord(); |
| 76 | + $section = $phpWord->addSection(); |
| 77 | + $html = '<ol type="1"><li>Decimal number first</li><li>second</li></ol>' |
| 78 | + . '<ol type="a"><li>Lowercase first</li><li>second</li></ol>' |
| 79 | + . '<ol type="A"><li>Uppercase first</li><li>second</li></ol>' |
| 80 | + . '<ol type="i"><li>Lower roman first</li><li>second</li></ol>' |
| 81 | + . '<ol type="I"><li>Upper roman first</li><li>second</li></ol>'; |
| 82 | + Html::addHtml($section, $html); |
| 83 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 84 | + |
| 85 | + $item = 1; |
| 86 | + $expected = '1'; |
| 87 | + $path = "/w:document/w:body/w:p[$item]"; |
| 88 | + self::assertSame('Decimal number first', $doc->getElement("$path/w:r")->nodeValue); |
| 89 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 90 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 91 | + ++$item; |
| 92 | + $path = "/w:document/w:body/w:p[$item]"; |
| 93 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 94 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 95 | + |
| 96 | + ++$item; |
| 97 | + $expected = '2'; |
| 98 | + $path = "/w:document/w:body/w:p[$item]"; |
| 99 | + self::assertSame('Lowercase first', $doc->getElement("$path/w:r")->nodeValue); |
| 100 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 101 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 102 | + ++$item; |
| 103 | + $path = "/w:document/w:body/w:p[$item]"; |
| 104 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 105 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 106 | + |
| 107 | + ++$item; |
| 108 | + $expected = '3'; |
| 109 | + $path = "/w:document/w:body/w:p[$item]"; |
| 110 | + self::assertSame('Uppercase first', $doc->getElement("$path/w:r")->nodeValue); |
| 111 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 112 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 113 | + ++$item; |
| 114 | + $path = "/w:document/w:body/w:p[$item]"; |
| 115 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 116 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 117 | + |
| 118 | + ++$item; |
| 119 | + $expected = '4'; |
| 120 | + $path = "/w:document/w:body/w:p[$item]"; |
| 121 | + self::assertSame('Lower roman first', $doc->getElement("$path/w:r")->nodeValue); |
| 122 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 123 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 124 | + ++$item; |
| 125 | + $path = "/w:document/w:body/w:p[$item]"; |
| 126 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 127 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 128 | + |
| 129 | + ++$item; |
| 130 | + $expected = '5'; |
| 131 | + $path = "/w:document/w:body/w:p[$item]"; |
| 132 | + self::assertSame('Upper roman first', $doc->getElement("$path/w:r")->nodeValue); |
| 133 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 134 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 135 | + ++$item; |
| 136 | + $path = "/w:document/w:body/w:p[$item]"; |
| 137 | + $numIdPath = $path . '/w:pPr/w:numPr/w:numId'; |
| 138 | + self::assertSame($expected, $doc->getElement($numIdPath)->getAttribute('w:val')); |
| 139 | + } |
| 140 | +} |
0 commit comments