jenkins-bot has submitted this change and it was merged.
Change subject: Make timestamp in consistent format after loaded from database
......................................................................
Make timestamp in consistent format after loaded from database
Notification timestamp is generated in MW format (YYYYMMDDHHMMSS)
inside the model and saved to the database in db specific format,
We need to convert it back to MW format when loading the data
from the database, it just happens that MW format is the same
as MySQL timestamp format
Change-Id: Ie881b66c8c24d57a8933c0153e9e7db5fe6aa017
---
M model/Notification.php
A tests/phpunit/model/NotificationTest.php
2 files changed, 91 insertions(+), 2 deletions(-)
Approvals:
EBernhardson: Looks good to me, approved
jenkins-bot: Verified
diff --git a/model/Notification.php b/model/Notification.php
index 24aad9d..d502770 100644
--- a/model/Notification.php
+++ b/model/Notification.php
@@ -185,8 +185,13 @@
$notification->targetPage = EchoTargetPage::newFromRow(
$row );
}
$notification->user = User::newFromId( $row->notification_user
);
- $notification->timestamp = $row->notification_timestamp;
- $notification->readTimestamp =
$row->notification_read_timestamp;
+ // Notification timestamp should never be empty
+ $notification->timestamp = wfTimestamp( TS_MW,
$row->notification_timestamp );
+ // Only convert to MW format if it is not empty, otherwise
+ // wfTimestamp would use current timestamp for empty cases
+ if ( $row->notification_read_timestamp ) {
+ $notification->readTimestamp = wfTimestamp( TS_MW,
$row->notification_read_timestamp );
+ }
$notification->bundleBase = $row->notification_bundle_base;
$notification->bundleHash = $row->notification_bundle_hash;
$notification->bundleDisplayHash =
$row->notification_bundle_display_hash;
diff --git a/tests/phpunit/model/NotificationTest.php
b/tests/phpunit/model/NotificationTest.php
new file mode 100644
index 0000000..8bb4cf2
--- /dev/null
+++ b/tests/phpunit/model/NotificationTest.php
@@ -0,0 +1,84 @@
+<?php
+
+class EchoNotificationTest extends MediaWikiTestCase {
+
+ public function testNewFromRow() {
+ $row = $this->mockNotificationRow() + $this->mockEventRow();
+
+ $notif = EchoNotification::newFromRow( (object)$row );
+ $this->assertInstanceOf( 'EchoNotification', $notif );
+ // getReadTimestamp() should return null
+ $this->assertNull( $notif->getReadTimestamp() );
+ $this->assertEquals(
+ $notif->getTimestamp(),
+ wfTimestamp( TS_MW, $row['notification_timestamp'] )
+ );
+ $this->assertInstanceOf( 'EchoEvent', $notif->getEvent() );
+ $this->assertNull( $notif->getTargetPage() );
+
+ // Provide a read timestamp
+ $row['notification_read_timestamp'] = time() + 1000;
+ $notif = EchoNotification::newFromRow( (object)$row );
+ // getReadTimestamp() should return the timestamp in MW format
+ $this->assertEquals(
+ $notif->getReadTimestamp(),
+ wfTimestamp( TS_MW, $row['notification_read_timestamp']
)
+ );
+
+ $row += $this->mockTargetPageRow();
+ $notif = EchoNotification::newFromRow( (object)$row );
+ $this->assertInstanceOf( 'EchoTargetPage',
$notif->getTargetPage() );
+ }
+
+ /**
+ * @expectedException MWException
+ */
+ public function testNewFromRowWithException() {
+ $row = $this->mockNotificationRow();
+ // Provide an invalid event id
+ $row['notification_event'] = -1;
+ $noitf = EchoNotification::newFromRow( (object)$row );
+ }
+
+ /**
+ * Mock a notification row from database
+ */
+ protected function mockNotificationRow() {
+ return array (
+ 'notification_user' => 1,
+ 'notification_event' => 1,
+ 'notification_timestamp' => time(),
+ 'notification_read_timestamp' => '',
+ 'notification_bundle_base' => 1,
+ 'notification_bundle_hash' => 'testhash',
+ 'notification_bundle_display_hash' => 'testdisplayhash'
+ );
+ }
+
+ /**
+ * Mock an event row from database
+ */
+ protected function mockEventRow() {
+ return array (
+ 'event_id' => 1,
+ 'event_type' => 'test_event',
+ 'event_variant' => '',
+ 'event_extra' => '',
+ 'event_page_id' => '',
+ 'event_agent_id' => '',
+ 'event_agent_ip' => ''
+ );
+ }
+
+ /**
+ * Mock a target page row
+ */
+ protected function mockTargetPageRow() {
+ return array (
+ 'etp_user' => 1,
+ 'etp_page' => 2,
+ 'etp_event' => 1
+ );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/155842
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie881b66c8c24d57a8933c0153e9e7db5fe6aa017
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits