Unverified Commit 03b18875 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3513856 by danielveza, smustgrave, riyas_nr, kim.pepper, mstrelan,...

Issue #3513856 by danielveza, smustgrave, riyas_nr, kim.pepper, mstrelan, alexpott: Make UserSession::name protected
parent 804ed494
Loading
Loading
Loading
Loading
Loading
+50 −3
Original line number Diff line number Diff line
@@ -4,8 +4,6 @@

/**
 * An implementation of the user account interface for the global user.
 *
 * @todo Change all properties to protected.
 */
#[\AllowDynamicProperties]
class UserSession implements AccountInterface {
@@ -38,7 +36,7 @@ class UserSession implements AccountInterface {
   *
   * @var string
   */
  public $name = '';
  protected $name = '';

  /**
   * The preferred language code of the account.
@@ -212,4 +210,53 @@ protected function getRoleStorage() {
    return \Drupal::entityTypeManager()->getStorage('user_role');
  }

  /**
   * Implements magic __get() method.
   */
  public function __get($name): mixed {
    if ($name === 'name') {
      @trigger_error("Getting the name property is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use \Drupal\Core\Session\UserSession::getAccountName() instead. See https://www.drupal.org/node/3513856", E_USER_DEPRECATED);
      return $this->getAccountName();
    }
    $class = get_class($this);
    $properties = get_class_vars($class);
    if (\array_key_exists($name, $properties)) {
      throw new \LogicException("Cannot access protected property $name in " . $class);
    }
    return $this->$name ?? NULL;
  }

  /**
   * Implements magic __isset() method.
   */
  public function __isset($name): bool {
    if ($name === 'name') {
      @trigger_error("Checking for the name property is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use \Drupal\Core\Session\UserSession::getAccountName() instead. See https://www.drupal.org/node/3513856", E_USER_DEPRECATED);
      return isset($this->name);
    }
    $class = get_class($this);
    $properties = get_class_vars($class);
    if (\array_key_exists($name, $properties)) {
      throw new \LogicException("Cannot access protected property $name in " . $class);
    }
    return isset($this->$name);
  }

  /**
   * Implements magic __set() method.
   */
  public function __set($name, $value): void {
    if ($name === 'name') {
      @trigger_error("Setting the name property is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Set the name via the constructor when creating the UserSession instance. See https://www.drupal.org/node/3513856", E_USER_DEPRECATED);
      $this->name = $value;
      return;
    }
    $class = get_class($this);
    $properties = get_class_vars($class);
    if (\array_key_exists($name, $properties)) {
      throw new \LogicException("Cannot set protected property $name in " . $class);
    }
    $this->$name = $value;
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -582,7 +582,7 @@ protected function installParameters() {
          'site_name' => 'Drupal',
          'site_mail' => 'simpletest@example.com',
          'account' => [
            'name' => $this->rootUser->name,
            'name' => $this->rootUser->getAccountName(),
            'mail' => $this->rootUser->getEmail(),
            'pass' => [
              'pass1' => $this->rootUser->pass_raw ?? $this->rootUser->passRaw,
+32 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Session\UserSession;
use Drupal\Tests\UnitTestCase;
use Drupal\user\RoleInterface;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

@@ -84,4 +85,35 @@ public function testHasRole(): void {
    $this->assertTrue($user4->hasRole(RoleInterface::ANONYMOUS_ID));
  }

  /**
   * Tests the name property deprecation.
   *
   * @legacy-covers ::__get
   * @legacy-covers ::__isset
   * @legacy-covers ::__set
   */
  #[IgnoreDeprecations]
  public function testNamePropertyDeprecation(): void {
    $user = new UserSession([
      'name' => 'test',
    ]);
    $this->expectDeprecation('Getting the name property is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use \Drupal\Core\Session\UserSession::getAccountName() instead. See https://www.drupal.org/node/3513856');
    self::assertEquals($user->name, $user->getAccountName());
    $this->expectDeprecation('Checking for the name property is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use \Drupal\Core\Session\UserSession::getAccountName() instead. See https://www.drupal.org/node/3513856');
    self::assertTrue(isset($user->name));

    // Test setting the name property.
    $this->expectDeprecation('Setting the name property is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Set the name via the constructor when creating the UserSession instance. See https://www.drupal.org/node/3513856');
    $user->name = 'test new';
    $this->assertEquals('test new', $user->getAccountName());

    // Verify protected properties cannot be accessed.
    $this->expectExceptionMessage('Cannot access protected property mail in Drupal\Core\Session\UserSession');
    $user->mail;

    // Verify dynamic properties can be set and accessed.
    $user->foo = 'bar';
    $this->assertEquals('bar', $user->foo);
  }

}