forked from PHPOffice/PHPWord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Table Cell if Row Doesn't Have Any (PHPOffice#2516)
* Generate Table Cell if Row Doesn't Have Any Fix PHPOffice#2505. Word treats file as corrupt if a table row does not contain a cell (documentation for why this is so is included in the issue). Person reporting the issue suggests that dropping such a row from the output file is preferred. However, I think generating an empty cell instead is closer to the user's expectation. For example, as demonstrated in the unit tests added with this PR, if a table has row 1 and 3 which contain cells, but row 2 does not, the table as written to the file will have 3 rows, with the second containing an empty cell. * Remove Commented-Out Code in Tests
- Loading branch information
Showing
3 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
tests/PhpWordTests/Writer/Word2007/Element/TableTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?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 | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpWordTests\Writer\Word2007\Element; | ||
|
||
use PhpOffice\PhpWord\PhpWord; | ||
use PhpOffice\PhpWord\SimpleType\TblWidth; | ||
use PhpOffice\PhpWordTests\TestHelperDOCX; | ||
|
||
/** | ||
* Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace. | ||
*/ | ||
class TableTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Executed after each method of the class. | ||
*/ | ||
protected function tearDown(): void | ||
{ | ||
TestHelperDOCX::clear(); | ||
} | ||
|
||
public static function testTableNormal(): void | ||
{ | ||
$phpWord = new PhpWord(); | ||
$section = $phpWord->addSection(); | ||
$section->addText('Before table (normal).'); | ||
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]); | ||
$row = $table->addRow(); | ||
$tc = $table->addCell(); | ||
$tc->addText('R1C1'); | ||
$tc = $table->addCell(); | ||
$tc->addText('R1C2'); | ||
$row = $table->addRow(); | ||
$tc = $table->addCell(); | ||
$tc->addText('R2C1'); | ||
$tc = $table->addCell(); | ||
$tc->addText('R2C2'); | ||
$row = $table->addRow(); | ||
$tc = $table->addCell(); | ||
$tc->addText('R3C1'); | ||
$tc = $table->addCell(); | ||
$tc->addText('R3C2'); | ||
$section->addText('After table.'); | ||
|
||
$doc = TestHelperDOCX::getDocument($phpWord); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[2]'), 'should be only 1 table'); | ||
|
||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]')); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]')); | ||
|
||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc[2]')); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc[3]')); | ||
|
||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[2]')); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[3]')); | ||
} | ||
|
||
public static function testSomeRowWithNoCells(): void | ||
{ | ||
$phpWord = new PhpWord(); | ||
$section = $phpWord->addSection(); | ||
$section->addText('Before table (row 2 has no cells).'); | ||
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]); | ||
$row = $table->addRow(); | ||
$tc = $table->addCell(); | ||
$tc->addText('R1C1'); | ||
$tc = $table->addCell(); | ||
$tc->addText('R1C2'); | ||
$row = $table->addRow(); | ||
$row = $table->addRow(); | ||
$tc = $table->addCell(); | ||
$tc->addText('R3C1'); | ||
$tc = $table->addCell(); | ||
$tc->addText('R3C2'); | ||
$section->addText('After table.'); | ||
|
||
$doc = TestHelperDOCX::getDocument($phpWord); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[2]'), 'should be only 1 table'); | ||
|
||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]')); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[3]')); | ||
|
||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc')); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]/w:tc[2]')); | ||
|
||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[2]')); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[3]/w:tc[3]')); | ||
} | ||
|
||
public static function testOnly1RowWithNoCells(): void | ||
{ | ||
$phpWord = new PhpWord(); | ||
$section = $phpWord->addSection(); | ||
$section->addText('Before table (only 1 row and it has no cells).'); | ||
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]); | ||
$row = $table->addRow(); | ||
$section->addText('After table.'); | ||
|
||
$doc = TestHelperDOCX::getDocument($phpWord); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[2]'), 'only 1 table should be written'); | ||
|
||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]')); | ||
self::assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc')); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]')); | ||
|
||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl/w:tr[2]')); | ||
} | ||
|
||
public static function testNoRows(): void | ||
{ | ||
$phpWord = new PhpWord(); | ||
$section = $phpWord->addSection(); | ||
$section->addText('Before table (no rows therefore omitted).'); | ||
$table = $section->addTable(['width' => 5000, 'unit' => TblWidth::PERCENT]); | ||
$section->addText('After table.'); | ||
|
||
$doc = TestHelperDOCX::getDocument($phpWord); | ||
self::assertFalse($doc->elementExists('/w:document/w:body/w:tbl[1]'), 'no table should be written'); | ||
} | ||
} |