diff --git a/core/modules/filter/tests/src/Unit/FilterUninstallValidatorTest.php b/core/modules/filter/tests/src/Unit/FilterUninstallValidatorTest.php index b3319192cc60135d7d3b638e0c5d745f83b55322..85fcbe3184866a169d84b7e2035f7da911fb5a3e 100644 --- a/core/modules/filter/tests/src/Unit/FilterUninstallValidatorTest.php +++ b/core/modules/filter/tests/src/Unit/FilterUninstallValidatorTest.php @@ -4,6 +4,8 @@ namespace Drupal\Tests\filter\Unit; +use Drupal\filter\FilterProcessResult; +use Drupal\filter\Plugin\FilterBase; use Drupal\Tests\UnitTestCase; /** @@ -92,8 +94,8 @@ public function testValidateNoMatchingFormats() { ], ]); - $filter_plugin_enabled = $this->getMockForAbstractClass('Drupal\filter\Plugin\FilterBase', [['status' => TRUE], '', ['provider' => 'filter_test']]); - $filter_plugin_disabled = $this->getMockForAbstractClass('Drupal\filter\Plugin\FilterBase', [['status' => FALSE], '', ['provider' => 'filter_test']]); + $filter_plugin_enabled = new FilterBaseTestableClass(['status' => TRUE], '', ['provider' => 'filter_test']); + $filter_plugin_disabled = new FilterBaseTestableClass(['status' => FALSE], '', ['provider' => 'filter_test']); // The first format has 2 matching and enabled filters, but the loop breaks // after finding the first one. @@ -163,3 +165,14 @@ public function testValidateNoMatchingFormats() { } } + +/** + * A class extending FilterBase for testing purposes. + */ +class FilterBaseTestableClass extends FilterBase { + + public function process($text, $langcode) { + return new FilterProcessResult(); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php index fffbee4f8f0873eb2dc1e128280f8acba17df8c4..35316ac0dcf501a3ec2e354a5263648fd4841776 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php @@ -160,7 +160,10 @@ protected function setUp(): void { $container->set('theme_handler', $this->themeHandler->reveal()); \Drupal::setContainer($container); - $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', [$values, $this->entityTypeId]); + $this->entity = $this->getMockBuilder(ConfigEntityBaseMockableClass::class) + ->setConstructorArgs([$values, $this->entityTypeId]) + ->onlyMethods([]) + ->getMock(); } /** @@ -332,7 +335,10 @@ public static function providerCalculateDependenciesWithPluginCollections(): arr * @covers ::onDependencyRemoval */ public function testCalculateDependenciesWithThirdPartySettings() { - $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', [[], $this->entityTypeId]); + $this->entity = $this->getMockBuilder(ConfigEntityBaseMockableClass::class) + ->setConstructorArgs([[], $this->entityTypeId]) + ->onlyMethods([]) + ->getMock(); $this->entity->setThirdPartySetting('test_provider', 'test', 'test'); $this->entity->setThirdPartySetting('test_provider2', 'test', 'test'); $this->entity->setThirdPartySetting(static::PROVIDER, 'test', 'test'); @@ -564,7 +570,10 @@ public function testToArray() { * @covers ::toArray */ public function testToArrayIdKey() { - $entity = $this->getMockForAbstractClass('\Drupal\Core\Config\Entity\ConfigEntityBase', [[], $this->entityTypeId], '', TRUE, TRUE, TRUE, ['id', 'get']); + $entity = $this->getMockBuilder(ConfigEntityBaseMockableClass::class) + ->setConstructorArgs([[], $this->entityTypeId]) + ->onlyMethods(['id', 'get']) + ->getMock(); $entity->expects($this->atLeastOnce()) ->method('id') ->willReturn($this->id); @@ -739,3 +748,10 @@ public function getPluginCollections() { } } + +/** + * A class extending ConfigEntityBase for testing purposes. + */ +class ConfigEntityBaseMockableClass extends ConfigEntityBase { + +} diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php index 13163a10828e508a23c4798edca9a2b89998d069..010bc319174c5d7a8b940839dbe99ae31be126ba 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php @@ -4,7 +4,9 @@ namespace Drupal\Tests\Core\Config\Entity; +use Drupal\Core\Entity\EntityDisplayBase; use Drupal\Tests\UnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; /** * @coversDefaultClass \Drupal\Core\Entity\EntityDisplayBase @@ -13,54 +15,80 @@ */ class EntityDisplayBaseTest extends UnitTestCase { + /** + * The mocked EntityDisplay object for testing. + */ + protected EntityDisplayBaseMockableClass&MockObject $entityDisplay; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + $this->entityDisplay = $this->getMockBuilder(EntityDisplayBaseMockableClass::class) + ->disableOriginalConstructor() + ->onlyMethods([]) + ->getMock(); + } + /** * @covers ::getTargetEntityTypeId */ public function testGetTargetEntityTypeId() { - $mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE); - $reflection = new \ReflectionProperty($mock, 'targetEntityType'); - $reflection->setValue($mock, 'test'); - $this->assertEquals('test', $mock->getTargetEntityTypeId()); + $reflection = new \ReflectionProperty($this->entityDisplay, 'targetEntityType'); + $reflection->setValue($this->entityDisplay, 'test'); + $this->assertEquals('test', $this->entityDisplay->getTargetEntityTypeId()); } /** * @covers ::getMode */ public function testGetMode() { - $mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE); - $reflection = new \ReflectionProperty($mock, 'mode'); - $reflection->setValue($mock, 'test'); - $this->assertEquals('test', $mock->getMode()); + $reflection = new \ReflectionProperty($this->entityDisplay, 'mode'); + $reflection->setValue($this->entityDisplay, 'test'); + $this->assertEquals('test', $this->entityDisplay->getMode()); } /** * @covers ::getOriginalMode */ public function testGetOriginalMode() { - $mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE); - $reflection = new \ReflectionProperty($mock, 'originalMode'); - $reflection->setValue($mock, 'test'); - $this->assertEquals('test', $mock->getOriginalMode()); + $reflection = new \ReflectionProperty($this->entityDisplay, 'originalMode'); + $reflection->setValue($this->entityDisplay, 'test'); + $this->assertEquals('test', $this->entityDisplay->getOriginalMode()); } /** * @covers ::getTargetBundle */ public function testGetTargetBundle() { - $mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE); - $reflection = new \ReflectionProperty($mock, 'bundle'); - $reflection->setValue($mock, 'test'); - $this->assertEquals('test', $mock->getTargetBundle()); + $reflection = new \ReflectionProperty($this->entityDisplay, 'bundle'); + $reflection->setValue($this->entityDisplay, 'test'); + $this->assertEquals('test', $this->entityDisplay->getTargetBundle()); } /** * @covers ::setTargetBundle */ public function testSetTargetBundle() { - $mock = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityDisplayBase', [], '', FALSE); - $reflection = new \ReflectionProperty($mock, 'bundle'); - $mock->setTargetBundle('test'); - $this->assertEquals('test', $reflection->getValue($mock)); + $reflection = new \ReflectionProperty($this->entityDisplay, 'bundle'); + $this->entityDisplay->setTargetBundle('test'); + $this->assertEquals('test', $reflection->getValue($this->entityDisplay)); + } + +} + +/** + * A class extending EntityDisplayBase for testing purposes. + */ +class EntityDisplayBaseMockableClass extends EntityDisplayBase { + + public function getPluginCollections() { + return []; + } + + public function getRenderer($field_name) { + return NULL; } } diff --git a/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php b/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php index 80f325231ebc3af291eaf9e802a9c32c42cee5f4..db5d7c0e7e04f21453346f77c378758902ec2330 100644 --- a/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php +++ b/core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php @@ -26,10 +26,10 @@ protected function mockBrowserTestBaseWithDriver($driver) { ->method('getDriver') ->willReturn($driver); - $btb = $this->getMockBuilder(BrowserTestBase::class) + $btb = $this->getMockBuilder(BrowserTestBaseMockableClass::class) ->disableOriginalConstructor() ->onlyMethods(['getSession']) - ->getMockForAbstractClass(); + ->getMock(); $btb->expects($this->any()) ->method('getSession') ->willReturn($session); @@ -46,7 +46,7 @@ public function testGetHttpClient() { $browserkit_client = $this->getMockBuilder(DrupalTestBrowser::class) ->onlyMethods(['getClient']) - ->getMockForAbstractClass(); + ->getMock(); $browserkit_client->expects($this->once()) ->method('getClient') ->willReturn($expected); @@ -83,10 +83,10 @@ public function testGetHttpClientException() { public function testTearDownWithoutSetUp() { $method = 'cleanupEnvironment'; $this->assertTrue(method_exists(BrowserTestBase::class, $method)); - $btb = $this->getMockBuilder(BrowserTestBase::class) + $btb = $this->getMockBuilder(BrowserTestBaseMockableClass::class) ->disableOriginalConstructor() ->onlyMethods([$method]) - ->getMockForAbstractClass(); + ->getMock(); $btb->expects($this->never())->method($method); $ref_tearDown = new \ReflectionMethod($btb, 'tearDown'); $ref_tearDown->invoke($btb); @@ -106,3 +106,10 @@ public function testGetRandomGeneratorPropertyDeprecation() { } } + +/** + * A class extending BrowserTestBase for testing purposes. + */ +class BrowserTestBaseMockableClass extends BrowserTestBase { + +} diff --git a/core/tests/Drupal/Tests/Core/Test/WebDriverTestBaseTest.php b/core/tests/Drupal/Tests/Core/Test/WebDriverTestBaseTest.php index d457f21e81e7a7efb044a8ad2d627e5247e26089..4fa6d8c76a0a8454d5a2177569607515d1928c4d 100644 --- a/core/tests/Drupal/Tests/Core/Test/WebDriverTestBaseTest.php +++ b/core/tests/Drupal/Tests/Core/Test/WebDriverTestBaseTest.php @@ -31,7 +31,8 @@ public function testCapabilities($expected, ?string $mink_driver_args_webdriver, $this->putEnv("MINK_DRIVER_ARGS_WEBDRIVER", $mink_driver_args_webdriver); $this->putEnv("MINK_DRIVER_ARGS", $mink_driver_args); - $object = $this->getMockForAbstractClass(WebDriverTestBase::class); + $object = new class('test') extends WebDriverTestBase { + }; $method = new \ReflectionMethod($object, 'getMinkDriverArgs'); $this->assertSame($expected, $method->invoke($object)); } @@ -67,7 +68,8 @@ public function testChromeOptions(): void { $this->expectDeprecation('The "chromeOptions" array key is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use "goog:chromeOptions instead. See https://www.drupal.org/node/3422624'); putenv('MINK_DRIVER_ARGS_WEBDRIVER=["chrome",{"browserName":"chrome","chromeOptions":{"args":["--headless"]}},"http://localhost:4444"]'); - $object = $this->getMockForAbstractClass(WebDriverTestBase::class); + $object = new class('test') extends WebDriverTestBase { + }; $method = new \ReflectionMethod($object, 'getMinkDriverArgs'); $this->assertSame('["chrome",{"browserName":"chrome","goog:chromeOptions":{"args":["--headless"],"w3c":false}},"http:\\/\\/localhost:4444"]', $method->invoke($object)); }