Skip to content

Commit

Permalink
TrackChange doesn't handle all return types of \DateTime::createFromF…
Browse files Browse the repository at this point in the history
…ormat(...) (#1584)

* Added boolean check before setting the date
  • Loading branch information
superhaggis authored and troosan committed Feb 23, 2019
1 parent 9958a48 commit 81a1b2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpWord/Element/TrackChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ class TrackChange extends AbstractContainer
*
* @param string $changeType
* @param string $author
* @param null|int|\DateTime $date
* @param null|int|bool|\DateTime $date
*/
public function __construct($changeType = null, $author = null, $date = null)
{
$this->changeType = $changeType;
$this->author = $author;
if ($date !== null) {
if ($date !== null && $date !== false) {
$this->date = ($date instanceof \DateTime) ? $date : new \DateTime('@' . $date);
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/PhpWord/Element/TrackChangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ public function testConstructDefault()
$this->assertEquals($date, $oTrackChange->getDate());
$this->assertEquals(TrackChange::INSERTED, $oTrackChange->getChangeType());
}

/**
* New instance with invalid \DateTime (produced by \DateTime::createFromFormat(...))
*/
public function testConstructDefaultWithInvalidDate()
{
$author = 'Test User';
$date = false;
$oTrackChange = new TrackChange(TrackChange::INSERTED, $author, $date);

$oText = new Text('dummy text');
$oText->setTrackChange($oTrackChange);

$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TrackChange', $oTrackChange);
$this->assertEquals($author, $oTrackChange->getAuthor());
$this->assertEquals($date, null);
$this->assertEquals(TrackChange::INSERTED, $oTrackChange->getChangeType());
}
}

0 comments on commit 81a1b2a

Please sign in to comment.