From a370c290685615a79c38aac98b5a811ca0e7bf1d Mon Sep 17 00:00:00 2001
From: Joas Schilling <nickvergessen@owncloud.com>
Date: Wed, 18 Nov 2015 16:27:48 +0100
Subject: [PATCH] Use a DateTime object instead of a timestamp

---
 lib/private/notification/inotification.php  | 14 ++---
 lib/private/notification/notification.php   | 31 +++++------
 tests/lib/notification/notificationtest.php | 60 ++++++++++++---------
 3 files changed, 58 insertions(+), 47 deletions(-)

diff --git a/lib/private/notification/inotification.php b/lib/private/notification/inotification.php
index a8bf5b110a..b79b7f6d9d 100644
--- a/lib/private/notification/inotification.php
+++ b/lib/private/notification/inotification.php
@@ -61,18 +61,18 @@ interface INotification {
 	public function getUser();
 
 	/**
-	 * @param int $timestamp
+	 * @param \DateTime $dateTime
 	 * @return $this
-	 * @throws \InvalidArgumentException if the timestamp are invalid
-	 * @since 8.2.0
+	 * @throws \InvalidArgumentException if the $dateTime is invalid
+	 * @since 9.0.0
 	 */
-	public function setTimestamp($timestamp);
+	public function setDateTime(\DateTime $dateTime);
 
 	/**
-	 * @return int
-	 * @since 8.2.0
+	 * @return \DateTime
+	 * @since 9.0.0
 	 */
-	public function getTimestamp();
+	public function getDateTime();
 
 	/**
 	 * @param string $type
diff --git a/lib/private/notification/notification.php b/lib/private/notification/notification.php
index 01df659d4a..a22d5446f4 100644
--- a/lib/private/notification/notification.php
+++ b/lib/private/notification/notification.php
@@ -29,8 +29,8 @@ class Notification implements INotification {
 	/** @var string */
 	protected $user;
 
-	/** @var int */
-	protected $timestamp;
+	/** @var \DateTime */
+	protected $dateTime;
 
 	/** @var string */
 	protected $objectType;
@@ -80,7 +80,8 @@ class Notification implements INotification {
 	public function __construct() {
 		$this->app = '';
 		$this->user = '';
-		$this->timestamp = 0;
+		$this->dateTime = new \DateTime();
+		$this->dateTime->setTimestamp(0);
 		$this->objectType = '';
 		$this->objectId = 0;
 		$this->subject = '';
@@ -140,25 +141,25 @@ class Notification implements INotification {
 	}
 
 	/**
-	 * @param int $timestamp
+	 * @param \DateTime $dateTime
 	 * @return $this
-	 * @throws \InvalidArgumentException if the timestamp is invalid
-	 * @since 8.2.0
+	 * @throws \InvalidArgumentException if the $dateTime is invalid
+	 * @since 9.0.0
 	 */
-	public function setTimestamp($timestamp) {
-		if (!is_int($timestamp)) {
-			throw new \InvalidArgumentException('The given timestamp is invalid');
+	public function setDateTime(\DateTime $dateTime) {
+		if ($dateTime->getTimestamp() === 0) {
+			throw new \InvalidArgumentException('The given date time is invalid');
 		}
-		$this->timestamp = $timestamp;
+		$this->dateTime = $dateTime;
 		return $this;
 	}
 
 	/**
-	 * @return int
-	 * @since 8.2.0
+	 * @return \DateTime
+	 * @since 9.0.0
 	 */
-	public function getTimestamp() {
-		return $this->timestamp;
+	public function getDateTime() {
+		return $this->dateTime;
 	}
 
 	/**
@@ -438,7 +439,7 @@ class Notification implements INotification {
 			&&
 			$this->getUser() !== ''
 			&&
-			$this->getTimestamp() !== 0
+			$this->getDateTime()->getTimestamp() !== 0
 			&&
 			$this->getObjectType() !== ''
 			&&
diff --git a/tests/lib/notification/notificationtest.php b/tests/lib/notification/notificationtest.php
index 662dc5a617..da3ada440e 100644
--- a/tests/lib/notification/notificationtest.php
+++ b/tests/lib/notification/notificationtest.php
@@ -62,14 +62,6 @@ class NotificationTest extends TestCase {
 		return $dataSets;
 	}
 
-	protected function dataValidInt() {
-		return [
-			[0],
-			[1],
-			[time()],
-		];
-	}
-
 	protected function dataInvalidInt() {
 		return [
 			[true],
@@ -139,32 +131,47 @@ class NotificationTest extends TestCase {
 		$this->notification->setUser($user);
 	}
 
-	public function dataSetTimestamp() {
-		return $this->dataValidInt();
+	public function dataSetDateTime() {
+		$past = new \DateTime();
+		$past->sub(new \DateInterval('P1Y'));
+		$current = new \DateTime();
+		$future = new \DateTime();
+		$future->add(new \DateInterval('P1Y'));
+
+		return [
+			[$past],
+			[$current],
+			[$future],
+		];
 	}
 
 	/**
-	 * @dataProvider dataSetTimestamp
-	 * @param int $timestamp
+	 * @dataProvider dataSetDateTime
+	 * @param \DateTime $dateTime
 	 */
-	public function testSetTimestamp($timestamp) {
-		$this->assertSame(0, $this->notification->getTimestamp());
-		$this->assertSame($this->notification, $this->notification->setTimestamp($timestamp));
-		$this->assertSame($timestamp, $this->notification->getTimestamp());
+	public function testSetDateTime(\DateTime $dateTime) {
+		$this->assertSame(0, $this->notification->getDateTime()->getTimestamp());
+		$this->assertSame($this->notification, $this->notification->setDateTime($dateTime));
+		$this->assertSame($dateTime, $this->notification->getDateTime());
 	}
 
-	public function dataSetTimestampInvalid() {
-		return $this->dataInvalidInt();
+	public function dataSetDateTimeZero() {
+		$nineTeenSeventy = new \DateTime();
+		$nineTeenSeventy->setTimestamp(0);
+		return [
+			[$nineTeenSeventy],
+		];
 	}
 
 	/**
-	 * @dataProvider dataSetTimestampInvalid
-	 * @param mixed $timestamp
+	 * @dataProvider dataSetDateTimeZero
+	 * @param \DateTime $dateTime
 	 *
 	 * @expectedException \InvalidArgumentException
+	 * @expectedMessage 'The given date time is invalid'
 	 */
-	public function testSetTimestampInvalid($timestamp) {
-		$this->notification->setTimestamp($timestamp);
+	public function testSetDateTimeZero($dateTime) {
+		$this->notification->setDateTime($dateTime);
 	}
 
 	public function dataSetObject() {
@@ -578,7 +585,7 @@ class NotificationTest extends TestCase {
 			->setMethods([
 				'getApp',
 				'getUser',
-				'getTimestamp',
+				'getDateTime',
 				'getObjectType',
 				'getObjectId',
 			])
@@ -592,9 +599,12 @@ class NotificationTest extends TestCase {
 			->method('getUser')
 			->willReturn($user);
 
+		$dateTime = new \DateTime();
+		$dateTime->setTimestamp($timestamp);
+
 		$notification->expects($this->any())
-			->method('getTimestamp')
-			->willReturn($timestamp);
+			->method('getDateTime')
+			->willReturn($dateTime);
 
 		$notification->expects($this->any())
 			->method('getObjectType')
-- 
GitLab