Verified Commit 5af6f438 authored by godotislate's avatar godotislate
Browse files

fix: #2954725 AccountInterface::getLastAccessedTime() implementors return incorrect data type

By: dpi
By: acbramley
By: manuel.adan
By: dcam
By: smustgrave
By: godotislate
parent 21292df8
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ class UserSession implements AccountInterface {
  /**
   * The Unix timestamp when the user last accessed the site.
   *
   * @var string
   * @var int
   */
  protected $access;

@@ -194,7 +194,7 @@ public function getTimeZone() {
   * {@inheritdoc}
   */
  public function getLastAccessedTime() {
    return $this->access;
    return (int) $this->access;
  }

  /**
+1 −1
Original line number Diff line number Diff line
@@ -289,7 +289,7 @@ public function getCreatedTime() {
   * {@inheritdoc}
   */
  public function getLastAccessedTime() {
    return $this->getFieldValue('access', 'value');
    return (int) $this->getFieldValue('access', 'value');
  }

  /**
+1 −1
Original line number Diff line number Diff line
@@ -105,7 +105,7 @@ public function testUser(): void {
      $this->assertSame($source->name, $user->label());
      $this->assertSame($source->mail, $user->getEmail());
      $this->assertSame($source->created, $user->getCreatedTime());
      $this->assertSame($source->access, $user->getLastAccessedTime());
      $this->assertSame((int) $source->access, $user->getLastAccessedTime());
      $this->assertSame($source->login, $user->getLastLoginTime());
      $is_blocked = $source->status == 0;
      $this->assertSame($is_blocked, $user->isBlocked());
+18 −0
Original line number Diff line number Diff line
@@ -125,4 +125,22 @@ public function testPasswordProperty(): void {
    $this->assertEquals('password', $user->password);
  }

  /**
   * Tests that ::getLastAccessedTime() returns an integer for a loaded user.
   *
   * @legacy-covers ::getLastAccessedTime
   */
  public function testGetLastAccessedTime(): void {
    $access = 100;
    /** @var \Drupal\user\Entity\User $user */
    $user = User::create([
      'name' => $this->randomMachineName(),
      'access' => $access,
    ]);
    $id = $user->save();

    $loaded_user = User::load($id);
    $this->assertSame($access, $loaded_user->getLastAccessedTime());
  }

}
+13 −0
Original line number Diff line number Diff line
@@ -114,4 +114,17 @@ public function testNamePropertyDeprecation(): void {
    $this->assertEquals('bar', $user->foo);
  }

  /**
   * Tests that ::getLastAccessedTime() returns an integer.
   *
   * @legacy-covers ::getLastAccessedTime
   */
  public function testGetLastAccessedTime(): void {
    $user = new UserSession([
      'access' => '1234567890',
    ]);

    $this->assertSame(1234567890, $user->getLastAccessedTime());
  }

}