Add support for comments

This commit is contained in:
antoine 2017-05-15 22:49:02 +02:00
parent 0e0817c972
commit 1cfb62de0d
21 changed files with 724 additions and 8 deletions

View File

@ -379,4 +379,27 @@ Available line style attributes:
- ``endArrow``. End type of arrow: block, open, classic, diamond, oval.
- ``width``. Line-object width in pt.
- ``height``. Line-object height in pt.
- ``flip``. Flip the line element: true, false.
- ``flip``. Flip the line element: true, false.
Comments
---------
Comments can be added to a document by using ``addComment``.
The comment can contain formatted text. Once the comment has been added, it can be linked to any to any element.
.. code-block:: php
// first create a comment
$comment= new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment->addText('Test', array('bold' => true));
// add it to the document
$phpWord->addComment($comment);
$textrun = $section->addTextRun();
$textrun->addText('This ');
$text = $textrun->addText('is');
// link the comment to the text you just created
$text->setCommentStart($comment);
If no end is set for a comment using the ``setCommentEnd``, the comment will be ended automatically at the end of the element it is started on.

View File

@ -49,6 +49,7 @@ Features
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
- Insert form fields (textinput, checkbox, and dropdown)
- Insert comments
- Create document from templates
- Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template
- ... and many more features on progress
@ -102,6 +103,8 @@ Writers
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Endnote | ✓ | | | ✓ | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Comments | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| **Graphs** | 2D basic graphs | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | 2D advanced graphs | | | | | |
@ -161,6 +164,8 @@ Readers
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | Endnote | ✓ | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | Comments | | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| **Graphs** | 2D basic graphs | | | | | |
+---------------------------+----------------------+--------+-------+-------+-------+-------+
| | 2D advanced graphs | | | | | |

View File

@ -0,0 +1,63 @@
<?php
include_once 'Sample_Header.php';
// New Word Document
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// A comment
$comment = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment->addText('Test', array('bold' => true));
$phpWord->addComment($comment);
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText('This ');
$text = $textrun->addText('is');
$text->setCommentStart($comment);
$textrun->addText(' a test');
$section->addTextBreak(2);
// Let's create a comment that we will link to a start element and an end element
$commentWithStartAndEnd = new \PhpOffice\PhpWord\Element\Comment('Foo Bar', new \DateTime(), '');
$commentWithStartAndEnd->addText('A comment with a start and an end');
$phpWord->addComment($commentWithStartAndEnd);
$textrunWithEnd = $section->addTextRun();
$textrunWithEnd->addText('This ');
$textToStartOn = $textrunWithEnd->addText('is', array('bold' => true));
$textToStartOn->setCommentStart($commentWithStartAndEnd);
$textrunWithEnd->addText(' another', array('italic' => true));
$textToEndOn = $textrunWithEnd->addText(' test');
$textToEndOn->setCommentEnd($commentWithStartAndEnd);
$section->addTextBreak(2);
// Let's add a comment on an image
$commentOnImage = new \PhpOffice\PhpWord\Element\Comment('Mr Smart', new \DateTime(), '');
$imageComment = $commentOnImage->addTextRun();
$imageComment->addText('Hey, Mars does look ');
$imageComment->addText('red', array('color' => 'FF0000'));
$phpWord->addComment($commentOnImage);
$image = $section->addImage('resources/_mars.jpg');
$image->setCommentStart($commentOnImage);
$section->addTextBreak(2);
// We can also do things the other way round, link the comment to the element
$anotherText = $section->addText("another text");
$comment1 = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment1->addText('Test', array('bold' => true));
$comment1->setStartElement($anotherText);
$comment1->setEndElement($anotherText);
$phpWord->addComment($comment1);
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}

View File

@ -0,0 +1,27 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Collection;
/**
* Comments collection
*
* @since 0.12.0
*/
class Comments extends AbstractCollection
{
}

View File

@ -58,7 +58,7 @@ abstract class AbstractContainer extends AbstractElement
protected $elements = array();
/**
* Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun
* Container type Section|Header|Footer|Footnote|Endnote|Cell|TextRun|TextBox|ListItemRun|TrackChange
*
* @var string
*/
@ -83,7 +83,7 @@ abstract class AbstractContainer extends AbstractElement
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object',
'Footnote', 'Endnote', 'CheckBox', 'TextBox', 'Field',
'Line', 'Shape', 'Title', 'TOC', 'PageBreak',
'Chart', 'FormField', 'SDT'
'Chart', 'FormField', 'SDT', 'Comment'
);
$functions = array();
foreach ($elements as $element) {
@ -188,7 +188,7 @@ abstract class AbstractContainer extends AbstractElement
private function checkValidity($method)
{
$generalContainers = array(
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun',
'Section', 'Header', 'Footer', 'Footnote', 'Endnote', 'Cell', 'TextRun', 'TextBox', 'ListItemRun', 'TrackChange',
);
$validContainers = array(
@ -203,7 +203,8 @@ abstract class AbstractContainer extends AbstractElement
'Shape' => $generalContainers,
'FormField' => $generalContainers,
'SDT' => $generalContainers,
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'TrackChange' => $generalContainers,
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox', 'TrackChange'),
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'Table' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

View File

@ -108,12 +108,26 @@ abstract class AbstractElement
protected $mediaRelation = false;
/**
* Is part of collection; true for Title, Footnote, Endnote, and Chart
* Is part of collection; true for Title, Footnote, Endnote, Chart, and Comment
*
* @var bool
*/
protected $collectionRelation = false;
/**
* The start position for the linked comment
*
* @var Comment
*/
protected $commentStart;
/**
* The end position for the linked comment
*
* @var Comment
*/
protected $commentEnd;
/**
* Get PhpWord
*
@ -265,6 +279,55 @@ abstract class AbstractElement
return $this->nestedLevel;
}
/**
* Get comment start
*
* @return Comment
*/
public function getCommentStart()
{
return $this->commentStart;
}
/**
* Set comment start
*
* @param Comment $value
*/
public function setCommentStart(Comment $value)
{
if ($this instanceof Comment) {
throw new \InvalidArgumentException("Cannot set a Comment on a Comment");
}
$this->commentStart = $value;
$this->commentStart->setStartElement($this);
}
/**
* Get comment end
*
* @return Comment
*/
public function getCommentEnd()
{
return $this->commentEnd;
}
/**
* Set comment end
*
* @param Comment $value
* @return void
*/
public function setCommentEnd(Comment $value)
{
if ($this instanceof Comment) {
throw new \InvalidArgumentException("Cannot set a Comment on a Comment");
}
$this->commentEnd = $value;
$this->commentEnd->setEndElement($this);
}
/**
* Set parent container
*

View File

@ -0,0 +1,122 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Element;
/**
* Comment element
*/
class Comment extends TrackChange
{
/**
* Initials
*
* @var string
*/
private $initials;
/**
* The Element where this comment starts
*
* @var AbstractElement
*/
private $startElement;
/**
* The Element where this comment ends
*
* @var AbstractElement
*/
private $endElement;
/**
* Is part of collection
*
* @var bool
*/
protected $collectionRelation = true;
/**
* Create a new Comment Element
*
* @param string $author
* @param \DateTime $date
* @param string $initials
*/
public function __construct($author, $date, $initials)
{
parent::__construct($author, $date);
$this->initials = $initials;
return $this;
}
/**
* Get Initials
*
* @return string
*/
public function getInitials()
{
return $this->initials;
}
/**
* Sets the element where this comment starts
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $value
*/
public function setStartElement(AbstractElement $value)
{
$this->startElement = $value;
if ($value->getCommentStart() == null) {
$value->setCommentStart($this);
}
}
/**
* Get the element where this comment starts
*
* @return \PhpOffice\PhpWord\Element\AbstractElement
*/
public function getStartElement()
{
return $this->startElement;
}
/**
* Sets the element where this comment ends
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $value
*/
public function setEndElement(AbstractElement $value)
{
$this->endElement = $value;
if ($value->getCommentEnd() == null) {
$value->setCommentEnd($this);
}
}
/**
* Get the element where this comment ends
*
* @return \PhpOffice\PhpWord\Element\AbstractElement
*/
public function getEndElement()
{
return $this->endElement;
}
}

View File

@ -0,0 +1,77 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Element;
/**
* TrackChange element
*/
class TrackChange extends AbstractContainer
{
/**
* @var string Container type
*/
protected $container = 'TrackChange';
/**
* Author
*
* @var string
*/
private $author;
/**
* Date
*
* @var DateTime
*/
private $date;
/**
* Create a new TrackChange Element
*
* @param string $author
* @param DateTime $date
*/
public function __construct($author, \DateTime $date)
{
$this->author = $author;
$this->date = $date;
return $this;
}
/**
* Get TrackChange Author
*
* @return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Get TrackChange Date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
}

View File

@ -27,11 +27,13 @@ use PhpOffice\PhpWord\Exception\Exception;
* @method Collection\Footnotes getFootnotes()
* @method Collection\Endnotes getEndnotes()
* @method Collection\Charts getCharts()
* @method Collection\Comments getComments()
* @method int addBookmark(Element\Bookmark $bookmark)
* @method int addTitle(Element\Title $title)
* @method int addFootnote(Element\Footnote $footnote)
* @method int addEndnote(Element\Endnote $endnote)
* @method int addChart(Element\Chart $chart)
* @method int addComment(Element\Comment $comment)
*
* @method Style\Paragraph addParagraphStyle(string $styleName, array $styles)
* @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null)
@ -84,7 +86,7 @@ class PhpWord
public function __construct()
{
// Collection
$collections = array('Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts');
$collections = array('Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts', 'Comments');
foreach ($collections as $collection) {
$class = 'PhpOffice\\PhpWord\\Collection\\' . $collection;
$this->collections[$collection] = new $class();
@ -118,7 +120,7 @@ class PhpWord
$addCollection = array();
$addStyle = array();
$collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart');
$collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart', 'Comment');
foreach ($collections as $collection) {
$getCollection[] = strtolower("get{$collection}s");
$addCollection[] = strtolower("add{$collection}");

View File

@ -60,6 +60,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
'DocPropsCustom' => 'docProps/custom.xml',
'RelsDocument' => 'word/_rels/document.xml.rels',
'Document' => 'word/document.xml',
'Comments' => 'word/comments.xml',
'Styles' => 'word/styles.xml',
'Numbering' => 'word/numbering.xml',
'Settings' => 'word/settings.xml',
@ -129,6 +130,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
$this->addNotes($zip, $rId, 'footnote');
$this->addNotes($zip, $rId, 'endnote');
$this->addComments($zip, $rId);
$this->addChart($zip, $rId);
// Write parts
@ -255,6 +257,30 @@ class Word2007 extends AbstractWriter implements WriterInterface
}
}
/**
* Add comments
*
* @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
* @param integer &$rId
* @return void
*/
private function addComments(ZipArchive $zip, &$rId)
{
$phpWord = $this->getPhpWord();
$collection = $phpWord->getComments();
$partName = "comments";
// Add comment relations and contents
/** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collection Type hint */
if ($collection->countItems() > 0) {
$this->relationships[] = array('target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId);
// Write content file, e.g. word/comments.xml
$writerPart = $this->getWriterPart($partName)->setElements($collection->getItems());
$zip->addFromString("word/{$partName}.xml", $writerPart->write());
}
}
/**
* Add chart.
*

View File

@ -103,6 +103,7 @@ abstract class AbstractElement
$this->writeParagraphStyle();
}
}
$this->writeCommentRangeStart();
}
/**
@ -112,11 +113,63 @@ abstract class AbstractElement
*/
protected function endElementP()
{
$this->writeCommentRangeEnd();
if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p
}
}
/**
* Writes the w:commentRangeStart DOM element
*
* @return void
*/
protected function writeCommentRangeStart()
{
if ($this->element->getCommentStart() != null) {
$comment = $this->element->getCommentStart();
//only set the ID if it is not yet set, otherwise it will overwrite it
if ($comment->getElementId() == null) {
$comment->setElementId();
}
$this->xmlWriter->writeElementBlock('w:commentRangeStart', array('w:id' => $comment->getElementId()));
}
}
/**
* Writes the w:commentRangeEnd DOM element
*
* @return void
*/
protected function writeCommentRangeEnd()
{
if ($this->element->getCommentEnd() != null) {
$comment = $this->element->getCommentEnd();
//only set the ID if it is not yet set, otherwise it will overwrite it
if ($comment->getElementId() == null) {
$comment->setElementId();
}
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
$this->xmlWriter->endElement();
} elseif ($this->element->getCommentStart() != null && $this->element->getCommentStart()->getEndElement() == null) {
$comment = $this->element->getCommentStart();
//only set the ID if it is not yet set, otherwise it will overwrite it
if ($comment->getElementId() == null) {
$comment->setElementId();
}
$this->xmlWriter->writeElementBlock('w:commentRangeEnd', array('w:id' => $comment->getElementId()));
$this->xmlWriter->startElement('w:r');
$this->xmlWriter->writeElementBlock('w:commentReference', array('w:id' => $comment->getElementId()));
$this->xmlWriter->endElement();
}
}
/**
* Write ending.
*

View File

@ -45,6 +45,7 @@ class Chart extends AbstractElement
if (!$this->withoutP) {
$xmlWriter->startElement('w:p');
}
$this->writeCommentRangeStart();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:drawing');

View File

@ -63,6 +63,7 @@ class Image extends AbstractElement
$xmlWriter->startElement('w:p');
$styleWriter->writeAlignment();
}
$this->writeCommentRangeStart();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:pict');

View File

@ -48,6 +48,7 @@ class Line extends AbstractElement
$xmlWriter->startElement('w:p');
$styleWriter->writeAlignment();
}
$this->writeCommentRangeStart();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:pict');

View File

@ -51,6 +51,7 @@ class Object extends AbstractElement
$xmlWriter->startElement('w:p');
$styleWriter->writeAlignment();
}
$this->writeCommentRangeStart();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:object');

View File

@ -55,6 +55,7 @@ class Shape extends AbstractElement
if (!$this->withoutP) {
$xmlWriter->startElement('w:p');
}
$this->writeCommentRangeStart();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:pict');

View File

@ -45,6 +45,7 @@ class TextBox extends Image
$xmlWriter->startElement('w:p');
$styleWriter->writeAlignment();
}
$this->writeCommentRangeStart();
$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:pict');

View File

@ -0,0 +1,103 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\Common\XMLWriter;
use PhpOffice\PhpWord\Element\Comment;
use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
/**
* Word2007 comments part writer: word/comments.xml
*/
class Comments extends AbstractPart
{
/**
* Comments collection to be written
*
* @var \PhpOffice\PhpWord\Collection\Comments
*/
protected $elements;
/**
* Write part
*
* @return string
*/
public function write()
{
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('w:comments');
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
$xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
$xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
if ($this->elements !== null) {
foreach ($this->elements as $element) {
if ($element instanceof Comment) {
$this->writeComment($xmlWriter, $element);
}
}
}
$xmlWriter->endElement(); // w:comments
return $xmlWriter->getData();
}
/**
* Write comment item.
*
* @param \PhpOffice\Common\XMLWriter $xmlWriter
* @param \PhpOffice\PhpWord\Element\Comment $comment
* @return void
*/
protected function writeComment(XMLWriter $xmlWriter, Comment $comment)
{
$xmlWriter->startElement('w:comment');
$xmlWriter->writeAttribute('w:id', $comment->getElementId());
$xmlWriter->writeAttribute('w:author', $comment->getAuthor());
$xmlWriter->writeAttribute('w:date', $comment->getDate()->format($this->dateFormat));
$xmlWriter->writeAttribute('w:initials', $comment->getInitials());
$containerWriter = new Container($xmlWriter, $comment);
$containerWriter->write();
$xmlWriter->endElement(); // w:comment
}
/**
* Set element
*
* @param \PhpOffice\PhpWord\Collection\Comments $elements
* @return self
*/
public function setElements($elements)
{
$this->elements = $elements;
return $this;
}
}

View File

@ -49,6 +49,7 @@ class ContentTypes extends AbstractPart
'/word/theme/theme1.xml' => $openXMLPrefix . 'officedocument.theme+xml',
'/word/webSettings.xml' => $wordMLPrefix . 'webSettings+xml',
'/word/fontTable.xml' => $wordMLPrefix . 'fontTable+xml',
'/word/comments.xml' => $wordMLPrefix . 'comments+xml',
);
$defaults = $contentTypes['default'];

View File

@ -0,0 +1,83 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Element;
/**
* Test class for PhpOffice\PhpWord\Element\Header
*
* @runTestsInSeparateProcesses
*/
class CommentTest extends \PHPUnit_Framework_TestCase
{
/**
* New instance
*/
public function testConstructDefault()
{
$author = 'Test User';
$date = new \DateTime('2000-01-01');
$initials = 'default_user';
$oComment = new Comment($author, $date, $initials);
$oText = new Text('dummy text');
$oComment->setStartElement($oText);
$oComment->setEndElement($oText);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Comment', $oComment);
$this->assertEquals($author, $oComment->getAuthor());
$this->assertEquals($date, $oComment->getDate());
$this->assertEquals($initials, $oComment->getInitials());
$this->assertEquals($oText, $oComment->getStartElement());
$this->assertEquals($oText, $oComment->getEndElement());
}
/**
* Add text
*/
public function testAddText()
{
$oComment = new Comment('Test User', new \DateTime(), 'my_initials');
$element = $oComment->addText('text');
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
$this->assertCount(1, $oComment->getElements());
$this->assertEquals('text', $element->getText());
}
/**
* Get elements
*/
public function testGetElements()
{
$oComment = new Comment('Test User', new \DateTime(), 'my_initials');
$this->assertInternalType('array', $oComment->getElements());
}
/**
* Set/get relation Id
*/
public function testRelationId()
{
$oComment = new Comment('Test User', new \DateTime(), 'my_initials');
$iVal = rand(1, 1000);
$oComment->setRelationId($iVal);
$this->assertEquals($iVal, $oComment->getRelationId());
}
}

View File

@ -0,0 +1,61 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\TestHelperDOCX;
use PhpOffice\PhpWord\Writer\Word2007;
/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Comment
*
* @runTestsInSeparateProcesses
*/
class CommentsTest extends \PHPUnit_Framework_TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()
{
TestHelperDOCX::clear();
}
/**
* Write comments
*/
public function testWriteComments()
{
$comment = new \PhpOffice\PhpWord\Element\Comment('Authors name', new \DateTime(), 'my_initials');
$comment->addText('Test');
$phpWord = new PhpWord();
$phpWord->addComment($comment);
$doc = TestHelperDOCX::getDocument($phpWord);
$path = '/w:comments/w:comment';
$file = 'word/comments.xml';
$this->assertTrue($doc->elementExists($path, $file));
$element = $doc->getElement($path, $file);
$this->assertNotNull($element->getAttribute('w:id'));
$this->assertEquals("Authors name", $element->getAttribute('w:author'));
$this->assertEquals("my_initials", $element->getAttribute('w:initials'));
}
}