diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index 1a83d4ab62d70718d2e3200f7b1fc2ba36630f5d..c98b77fd9fef8c64e5fbfb2f2b53c4c7fcdde4cc 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -24,7 +24,6 @@ use Drupal\Tests\Traits\PhpUnitWarnings; use Drupal\TestTools\Comparator\MarkupInterfaceComparator; use Drupal\TestTools\Extension\SchemaInspector; -use Drupal\TestTools\Random; use Drupal\TestTools\TestVarDumper; use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestCase; @@ -267,7 +266,9 @@ protected function setUp(): void { */ public function __get(string $name) { if ($name === 'randomGenerator') { - return Random::getGenerator(); + @trigger_error('Accessing the randomGenerator property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getRandomGenerator() instead. See https://www.drupal.org/node/3358445', E_USER_DEPRECATED); + + return $this->getRandomGenerator(); } } diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php index 5baab80ede80e05c6b969709f8660b47a231423a..689cf8b953e073e50b867d75b27aeb4502b7ccd4 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php +++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php @@ -3,6 +3,7 @@ namespace Drupal\KernelTests; use Drupal\Component\FileCache\FileCacheFactory; +use Drupal\Component\Utility\Random; use Drupal\Core\Database\Database; use GuzzleHttp\Exception\GuzzleException; use Drupal\Tests\StreamCapturer; @@ -365,4 +366,17 @@ public function testDatabaseDriverModuleEnabled() { $this->assertSame(1, \Drupal::service('extension.list.module')->get($module)->status); } + /** + * Tests the deprecation of accessing the randomGenerator property directly. + * + * @group legacy + */ + public function testGetRandomGeneratorPropertyDeprecation() { + $this->expectDeprecation('Accessing the randomGenerator property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getRandomGenerator() instead. See https://www.drupal.org/node/3358445'); + // We purposely test accessing an undefined property here. We need to tell + // PHPStan to ignore that. + // @phpstan-ignore-next-line + $this->assertInstanceOf(Random::class, $this->randomGenerator); + } + } diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index afc09febcacb3c1625224032087e45ade0bacbba..3d3e41c51d945c9d6e134ef7d504088115be23bc 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -19,7 +19,6 @@ use Drupal\Tests\Traits\PhpUnitWarnings; use Drupal\Tests\user\Traits\UserCreationTrait; use Drupal\TestTools\Comparator\MarkupInterfaceComparator; -use Drupal\TestTools\Random; use Drupal\TestTools\TestVarDumper; use GuzzleHttp\Cookie\CookieJar; use PHPUnit\Framework\TestCase; @@ -383,7 +382,9 @@ protected function setUp(): void { */ public function __get(string $name) { if ($name === 'randomGenerator') { - return Random::getGenerator(); + @trigger_error('Accessing the randomGenerator property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getRandomGenerator() instead. See https://www.drupal.org/node/3358445', E_USER_DEPRECATED); + + return $this->getRandomGenerator(); } } diff --git a/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php b/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php index b32a8b396f894768d05b7942c031349141a22e0c..1463b4b075b760cd94507210771036ea83176a7d 100644 --- a/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php +++ b/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\Core\Test; +use Drupal\Component\Utility\Random; use Drupal\Tests\DrupalTestBrowser; use Drupal\Tests\UnitTestCase; use Drupal\Tests\BrowserTestBase; @@ -92,4 +93,17 @@ public function testTearDownWithoutSetUp() { $ref_tearDown->invoke($btb); } + /** + * Tests the deprecation of accessing the randomGenerator property directly. + * + * @group legacy + */ + public function testGetRandomGeneratorPropertyDeprecation() { + $this->expectDeprecation('Accessing the randomGenerator property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getRandomGenerator() instead. See https://www.drupal.org/node/3358445'); + // We purposely test accessing an undefined property here. We need to tell + // PHPStan to ignore that. + // @phpstan-ignore-next-line + $this->assertInstanceOf(Random::class, $this->randomGenerator); + } + } diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index d47eef9373d0a7e6cd8cc69962848af0c84a4426..0e1773deac4758bb1bb3b597058403bf5d19dfb1 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -9,7 +9,6 @@ use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Drupal\Tests\Traits\PhpUnitWarnings; -use Drupal\TestTools\Random; use Drupal\TestTools\TestVarDumper; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; @@ -70,7 +69,9 @@ protected function setUp(): void { */ public function __get(string $name) { if ($name === 'randomGenerator') { - return Random::getGenerator(); + @trigger_error('Accessing the randomGenerator property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getRandomGenerator() instead. See https://www.drupal.org/node/3358445', E_USER_DEPRECATED); + + return $this->getRandomGenerator(); } } diff --git a/core/tests/Drupal/Tests/UnitTestCaseTest.php b/core/tests/Drupal/Tests/UnitTestCaseTest.php index b5fd12cde8833ccb5d58ffaa964b0a2773903aaf..375d2e5dba1cfa9e45bf5467a17df2b6b80b334f 100644 --- a/core/tests/Drupal/Tests/UnitTestCaseTest.php +++ b/core/tests/Drupal/Tests/UnitTestCaseTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests; +use Drupal\Component\Utility\Random; + /** * Tests for the UnitTestCase class. * @@ -53,4 +55,17 @@ public function testVarDumpSeparateProcess() { $this->assertStringContainsString('banana', StreamCapturer::$cache); } + /** + * Tests the deprecation of accessing the randomGenerator property directly. + * + * @group legacy + */ + public function testGetRandomGeneratorPropertyDeprecation() { + $this->expectDeprecation('Accessing the randomGenerator property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use getRandomGenerator() instead. See https://www.drupal.org/node/3358445'); + // We purposely test accessing an undefined property here. We need to tell + // PHPStan to ignore that. + // @phpstan-ignore-next-line + $this->assertInstanceOf(Random::class, $this->randomGenerator); + } + }