MDL-66804 user: consistent user access times in privacy export.
authorPaul Holden <paulh@moodle.com>
Mon, 30 Sep 2019 22:02:50 +0000 (23:02 +0100)
committerPaul Holden <paulh@moodle.com>
Mon, 30 Sep 2019 22:02:50 +0000 (23:02 +0100)
user/classes/privacy/provider.php
user/tests/privacy_test.php

index b31b2e6..839026e 100644 (file)
@@ -385,10 +385,10 @@ class provider implements
             'calendartype' => $user->calendartype,
             'theme' => $user->theme,
             'timezone' => $user->timezone,
-            'firstaccess' => transform::datetime($user->firstaccess),
-            'lastaccess' => transform::datetime($user->lastaccess),
-            'lastlogin' => transform::datetime($user->lastlogin),
-            'currentlogin' => $user->currentlogin,
+            'firstaccess' => $user->firstaccess ? transform::datetime($user->firstaccess) : null,
+            'lastaccess' => $user->lastaccess ? transform::datetime($user->lastaccess) : null,
+            'lastlogin' => $user->lastlogin ? transform::datetime($user->lastlogin) : null,
+            'currentlogin' => $user->currentlogin ? transform::datetime($user->currentlogin) : null,
             'lastip' => $user->lastip,
             'secret' => $user->secret,
             'picture' => $user->picture,
index bd630f1..8a1a9f8 100644 (file)
@@ -28,6 +28,7 @@ global $CFG;
 use \core_privacy\tests\provider_testcase;
 use \core_user\privacy\provider;
 use \core_privacy\local\request\approved_userlist;
+use \core_privacy\local\request\transform;
 
 require_once($CFG->dirroot . "/user/lib.php");
 
@@ -59,7 +60,11 @@ class core_user_privacy_testcase extends provider_testcase {
      */
     public function test_export_user_data() {
         $this->resetAfterTest();
-        $user = $this->getDataGenerator()->create_user();
+        $user = $this->getDataGenerator()->create_user([
+            'firstaccess' => 1535760000,
+            'lastaccess' => 1541030400,
+            'currentlogin' => 1541030400,
+        ]);
         $course = $this->getDataGenerator()->create_course();
         $context = \context_user::instance($user->id);
 
@@ -125,6 +130,11 @@ class core_user_privacy_testcase extends provider_testcase {
         $this->assertTrue(array_key_exists('firstname', $userdata));
         $this->assertTrue(array_key_exists('lastname', $userdata));
         $this->assertTrue(array_key_exists('email', $userdata));
+        // Check access times.
+        $this->assertEquals(transform::datetime($user->firstaccess), $userdata['firstaccess']);
+        $this->assertEquals(transform::datetime($user->lastaccess), $userdata['lastaccess']);
+        $this->assertNull($userdata['lastlogin']);
+        $this->assertEquals(transform::datetime($user->currentlogin), $userdata['currentlogin']);
     }
 
     /**