Issue #3309745 by andypost, Berdir, longwave, Wim Leers, Taran2L, mondrake, alexpott: Fix dynamic property deprecations and other unit test failures for PHP 8.2

merge-requests/2894/merge
catch 2022-10-28 13:41:59 +01:00
parent 05e6abf5cc
commit 88ca0c51a2
17 changed files with 115 additions and 54 deletions

View File

@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
/**
* @coversDefaultClass \Drupal\contact\MailHandler
@ -292,7 +293,7 @@ class MailHandlerTest extends UnitTestCase {
* Mock sender for testing.
*/
protected function getMockSender($anonymous = TRUE, $mail_address = 'anonymous@drupal.org') {
$sender = $this->createMock('\Drupal\Core\Session\AccountInterface');
$sender = $this->createMock(User::class);
$sender->expects($this->once())
->method('isAnonymous')
->willReturn($anonymous);

View File

@ -262,20 +262,16 @@ class EntityDisplayTest extends KernelTestBase {
$this->assertEquals($default_formatter, $formatter->getPluginId());
$this->assertEquals($formatter_settings, $formatter->getSettings());
// Check that the formatter is statically persisted, by assigning an
// arbitrary property and reading it back.
$random_value = $this->randomString();
$formatter->randomValue = $random_value;
$formatter = $display->getRenderer($field_name);
$this->assertEquals($random_value, $formatter->randomValue);
// Check that the formatter is statically persisted.
$this->assertSame($formatter, $display->getRenderer($field_name));
// Check that changing the definition creates a new formatter.
$display->setComponent($field_name, [
'type' => 'field_test_multiple',
]);
$formatter = $display->getRenderer($field_name);
$this->assertEquals('field_test_multiple', $formatter->getPluginId());
$this->assertFalse(isset($formatter->randomValue));
$renderer = $display->getRenderer($field_name);
$this->assertEquals('field_test_multiple', $renderer->getPluginId());
$this->assertNotSame($formatter, $renderer);
// Check that the display has dependencies on the field and the module that
// provides the formatter.

View File

@ -103,20 +103,16 @@ class EntityFormDisplayTest extends KernelTestBase {
$this->assertEquals($default_widget, $widget->getPluginId());
$this->assertEquals($widget_settings, $widget->getSettings());
// Check that the widget is statically persisted, by assigning an
// arbitrary property and reading it back.
$random_value = $this->randomString();
$widget->randomValue = $random_value;
$widget = $form_display->getRenderer($field_name);
$this->assertEquals($random_value, $widget->randomValue);
// Check that the widget is statically persisted.
$this->assertSame($widget, $form_display->getRenderer($field_name));
// Check that changing the definition creates a new widget.
$form_display->setComponent($field_name, [
'type' => 'field_test_multiple',
]);
$widget = $form_display->getRenderer($field_name);
$this->assertEquals('test_field_widget', $widget->getPluginId());
$this->assertFalse(isset($widget->randomValue));
$renderer = $form_display->getRenderer($field_name);
$this->assertEquals('test_field_widget', $renderer->getPluginId());
$this->assertNotSame($widget, $renderer);
// Check that specifying an unknown widget (e.g. case of a disabled module)
// gets stored as is in the display, but results in the default widget being

View File

@ -7,7 +7,6 @@ use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableEntityBundleInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
@ -118,11 +117,6 @@ class NodeOperationAccessTest extends UnitTestCase {
$accessControl = new NodeAccessControlHandler($entityType, $grants, $entityTypeManager);
$accessControl->setModuleHandler($moduleHandler);
$nodeType = $this->createMock(RevisionableEntityBundleInterface::class);
$typeProperty = new \stdClass();
$typeProperty->entity = $nodeType;
$node->type = $typeProperty;
$access = $accessControl->access($node, $operation, $account, FALSE);
$this->assertEquals($assertAccess, $access);
}

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\serialization\Unit\Normalizer;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
@ -187,7 +188,7 @@ class EntityNormalizerTest extends UnitTestCase {
$key_1 = $this->createMock(FieldItemListInterface::class);
$key_2 = $this->createMock(FieldItemListInterface::class);
$entity = $this->createMock(FieldableEntityInterface::class);
$entity = $this->createMock(ContentEntityBase::class);
$entity->expects($this->exactly(2))
->method('get')
->willReturnMap([
@ -340,7 +341,7 @@ class EntityNormalizerTest extends UnitTestCase {
$key_1 = $this->createMock(FieldItemListInterface::class);
$key_2 = $this->createMock(FieldItemListInterface::class);
$entity = $this->createMock(FieldableEntityInterface::class);
$entity = $this->createMock(ContentEntityBase::class);
$entity->expects($this->exactly(2))
->method('get')
->willReturnMap([
@ -409,7 +410,7 @@ class EntityNormalizerTest extends UnitTestCase {
$storage->expects($this->once())
->method('create')
->with($test_data)
->willReturn($this->createMock('Drupal\Core\Entity\EntityInterface'));
->willReturn($this->createMock(ContentEntityBase::class));
$this->entityTypeManager->expects($this->once())
->method('getStorage')

View File

@ -11,6 +11,13 @@ use Drupal\Core\TypedData\TypedData;
*/
class ComputedString extends TypedData implements CacheableDependencyInterface {
/**
* The data value.
*
* @var mixed
*/
protected $value;
/**
* {@inheritdoc}
*/

View File

@ -18,6 +18,24 @@ class FakeRecord {
*/
public $fakeArg;
/**
* The property used in tests.
*
* @see \Drupal\KernelTests\Core\Database\FetchTest
*
* @var string
*/
public string $name;
/**
* The property used in tests.
*
* @see \Drupal\KernelTests\Core\Database\DatabaseTestBase
*
* @var string
*/
public string $job;
/**
* Constructs a FakeRecord object with an optional constructor argument.
*

View File

@ -82,7 +82,7 @@ class FetchTest extends DatabaseTestBase {
/**
* Confirms that we can fetch a record into a class using fetchObject.
*
* @see \Drupal\system\Tests\Database\FakeRecord
* @see \Drupal\Tests\system\Functional\Database\FakeRecord
* @see \Drupal\Core\Database\StatementPrefetch::fetchObject
*/
public function testQueryFetchObjectClass() {

View File

@ -168,7 +168,7 @@ class ContainerTest extends TestCase {
$this->assertEquals($some_parameter, $service->getSomeParameter(), '%some_config% was injected via constructor.');
$this->assertEquals($this->container, $service->getContainer(), 'Container was injected via setter injection.');
$this->assertEquals($some_other_parameter, $service->getSomeOtherParameter(), '%some_other_config% was injected via setter injection.');
$this->assertEquals('foo', $service->_someProperty, 'Service has added properties.');
$this->assertEquals('foo', $service->someProperty, 'Service has added properties.');
}
/**
@ -741,7 +741,7 @@ class ContainerTest extends TestCase {
$this->getServiceCall('other.service'),
$this->getParameterCall('some_config'),
]),
'properties' => $this->getCollection(['_someProperty' => 'foo']),
'properties' => $this->getCollection(['someProperty' => 'foo']),
'calls' => [
[
'setContainer',
@ -1103,6 +1103,11 @@ class MockService {
*/
protected $someOtherParameter;
/**
* @var string
*/
public string $someProperty;
/**
* Constructs a MockService object.
*

View File

@ -559,6 +559,7 @@ class CallableClass {
class TestEventListener {
public $name;
public $preFooInvoked = FALSE;
public $postFooInvoked = FALSE;

View File

@ -162,7 +162,10 @@ class VariableTest extends TestCase {
new \stdClass(),
],
[
// A not-stdClass object.
// A not-stdClass object. Since PHP 8.2 exported namespace is prefixed,
// see https://github.com/php/php-src/pull/8233 for reasons.
PHP_VERSION_ID >= 80200 ?
"\Drupal\Tests\Component\Utility\StubVariableTestClass::__set_state(array(\n))" :
"Drupal\Tests\Component\Utility\StubVariableTestClass::__set_state(array(\n))",
new StubVariableTestClass(),
],

View File

@ -8,6 +8,7 @@
namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
@ -517,11 +518,11 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
],
]);
$entity_a = $this->createMock('\Drupal\Core\Config\Entity\ConfigEntityInterface');
$entity_a = $this->createMock(ConfigEntityBase::class);
$entity_a->expects($this->atLeastOnce())
->method('label')
->willReturn('foo');
$entity_b = $this->createMock('\Drupal\Core\Config\Entity\ConfigEntityInterface');
$entity_b = $this->createMock(ConfigEntityBase::class);
$entity_b->expects($this->atLeastOnce())
->method('label')
->willReturn('bar');
@ -648,6 +649,8 @@ class TestConfigEntityWithPluginCollections extends ConfigEntityBaseWithPluginCo
protected $pluginManager;
protected $the_plugin_collection_config;
public function setPluginManager(PluginManagerInterface $plugin_manager) {
$this->pluginManager = $plugin_manager;
}

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
@ -93,7 +94,7 @@ class EntityLinkTest extends UnitTestCase {
->willReturn($entity_type);
/** @var \Drupal\Core\Entity\Entity $entity */
$entity = $this->getMockForAbstractClass('Drupal\Core\Entity\EntityBase', [
$entity = $this->getMockForAbstractClass(ConfigEntityBase::class, [
['id' => $entity_id, 'label' => $entity_label, 'langcode' => 'es'],
$entity_type_id,
]);

View File

@ -5,6 +5,7 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
@ -121,7 +122,7 @@ class EntityUnitTest extends UnitTestCase {
$container->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
\Drupal::setContainer($container);
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\EntityBase', [$this->values, $this->entityTypeId]);
$this->entity = new EntityBaseTest($this->values, $this->entityTypeId);
}
/**
@ -618,3 +619,11 @@ class EntityUnitTest extends UnitTestCase {
}
}
class EntityBaseTest extends EntityBase {
public $id;
public $langcode;
public $uuid;
public $label;
}

View File

@ -100,7 +100,7 @@ class EntityUrlTest extends UnitTestCase {
* @covers ::toUrl
*/
public function testToUrlNoId() {
$entity = $this->getEntity(EntityBase::class, []);
$entity = $this->getEntity(UrlTestEntity::class, []);
$this->expectException(EntityMalformedException::class);
$this->expectExceptionMessage('The "' . $this->entityTypeId . '" entity cannot have a URI as it does not have an ID');
@ -123,7 +123,7 @@ class EntityUrlTest extends UnitTestCase {
*/
public function testToUrlLinkTemplates($link_template, $expected_route_name) {
$values = ['id' => $this->entityId, 'langcode' => $this->langcode];
$entity = $this->getEntity(EntityBase::class, $values);
$entity = $this->getEntity(UrlTestEntity::class, $values);
$this->registerLinkTemplate($link_template);
/** @var \Drupal\Core\Url $url */
@ -220,7 +220,7 @@ class EntityUrlTest extends UnitTestCase {
* @covers ::urlRouteParameters
*/
public function testToUrlLinkTemplateNoId($link_template, $expected_route_name) {
$entity = $this->getEntity(EntityBase::class, ['id' => $this->entityId]);
$entity = $this->getEntity(UrlTestEntity::class, ['id' => $this->entityId]);
$this->registerLinkTemplate($link_template);
/** @var \Drupal\Core\Url $url */
@ -265,7 +265,7 @@ class EntityUrlTest extends UnitTestCase {
*/
public function testToUrlLinkTemplateAddForm($has_bundle_key, $bundle_entity_type, $bundle_key, $expected_route_parameters) {
$values = ['id' => $this->entityId, 'langcode' => $this->langcode];
$entity = $this->getEntity(EntityBase::class, $values);
$entity = $this->getEntity(UrlTestEntity::class, $values);
$this->entityType->hasKey('bundle')->willReturn($has_bundle_key);
$this->entityType->getBundleEntityType()->willReturn($bundle_entity_type);
$this->entityType->getKey('bundle')->willReturn($bundle_key);
@ -310,7 +310,7 @@ class EntityUrlTest extends UnitTestCase {
* @covers ::linkTemplates
*/
public function testToUrlUriCallbackUndefined(array $bundle_info, $uri_callback) {
$entity = $this->getEntity(EntityBase::class, ['id' => $this->entityId]);
$entity = $this->getEntity(UrlTestEntity::class, ['id' => $this->entityId]);
$this->registerBundleInfo($bundle_info);
$this->entityType->getUriCallback()->willReturn($uri_callback);
@ -351,7 +351,7 @@ class EntityUrlTest extends UnitTestCase {
* @dataProvider providerTestToUrlUriCallback
*/
public function testToUrlUriCallback(array $bundle_info, $uri_callback) {
$entity = $this->getEntity(EntityBase::class, ['id' => $this->entityId, 'langcode' => $this->langcode]);
$entity = $this->getEntity(UrlTestEntity::class, ['id' => $this->entityId, 'langcode' => $this->langcode]);
$this->registerBundleInfo($bundle_info);
$this->entityType->getUriCallback()->willReturn($uri_callback);
@ -385,7 +385,7 @@ class EntityUrlTest extends UnitTestCase {
* @covers ::uriRelationships
*/
public function testUriRelationships() {
$entity = $this->getEntity(EntityBase::class, ['id' => $this->entityId]);
$entity = $this->getEntity(UrlTestEntity::class, ['id' => $this->entityId]);
$container_builder = new ContainerBuilder();
$url_generator = $this->createMock(UrlGeneratorInterface::class);
@ -505,4 +505,12 @@ class EntityUrlTest extends UnitTestCase {
}
abstract class RevisionableEntity extends EntityBase implements RevisionableInterface {}
class UrlTestEntity extends EntityBase {
public $id;
public $langcode;
public $uuid;
public $label;
}
abstract class RevisionableEntity extends UrlTestEntity implements RevisionableInterface {}

View File

@ -5,6 +5,7 @@ namespace Drupal\Tests\Core\Entity\KeyValueStore;
use Drupal\Core\Cache\MemoryCache\MemoryCache;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException;
@ -197,7 +198,7 @@ class KeyValueEntityStorageTest extends UnitTestCase {
* @return \Drupal\Core\Entity\EntityInterface
*/
public function testCreate() {
$entity = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [], ['toArray']);
$entity = $this->getMockEntity(EntityBaseTest::class, [], ['toArray']);
$this->entityType->expects($this->once())
->method('getClass')
->willReturn(get_class($entity));
@ -442,7 +443,7 @@ class KeyValueEntityStorageTest extends UnitTestCase {
public function testSaveDuplicate() {
$this->setUpKeyValueEntityStorage();
$entity = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]);
$entity = $this->getMockEntity(EntityBaseTest::class, [['id' => 'foo']]);
$entity->enforceIsNew();
$this->keyValueStore->expects($this->once())
->method('has')
@ -497,8 +498,8 @@ class KeyValueEntityStorageTest extends UnitTestCase {
* @covers ::doLoadMultiple
*/
public function testLoadMultipleAll() {
$expected['foo'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]);
$expected['bar'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'bar']]);
$expected['foo'] = $this->getMockEntity(EntityBaseTest::class, [['id' => 'foo']]);
$expected['bar'] = $this->getMockEntity(EntityBaseTest::class, [['id' => 'bar']]);
$this->entityType->expects($this->once())
->method('getClass')
->willReturn(get_class(reset($expected)));
@ -522,7 +523,7 @@ class KeyValueEntityStorageTest extends UnitTestCase {
* @covers ::doLoadMultiple
*/
public function testLoadMultipleIds() {
$entity = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]);
$entity = $this->getMockEntity(EntityBaseTest::class, [['id' => 'foo']]);
$this->entityType->expects($this->once())
->method('getClass')
->willReturn(get_class($entity));
@ -563,8 +564,8 @@ class KeyValueEntityStorageTest extends UnitTestCase {
* @covers ::doDelete
*/
public function testDelete() {
$entities['foo'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'foo']]);
$entities['bar'] = $this->getMockEntity('Drupal\Core\Entity\EntityBase', [['id' => 'bar']]);
$entities['foo'] = $this->getMockEntity(EntityBaseTest::class, [['id' => 'foo']]);
$entities['bar'] = $this->getMockEntity(EntityBaseTest::class, [['id' => 'bar']]);
$this->setUpKeyValueEntityStorage();
$this->moduleHandler->expects($this->exactly(8))
@ -611,7 +612,7 @@ class KeyValueEntityStorageTest extends UnitTestCase {
*
* @return \Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject
*/
public function getMockEntity($class = 'Drupal\Core\Entity\EntityBase', array $arguments = [], $methods = []) {
public function getMockEntity($class = EntityBaseTest::class, array $arguments = [], $methods = []) {
// Ensure the entity is passed at least an array of values and an entity
// type ID
if (!isset($arguments[0])) {
@ -625,6 +626,15 @@ class KeyValueEntityStorageTest extends UnitTestCase {
}
class EntityBaseTest extends EntityBase {
public $id;
public $langcode;
public $uuid;
public $label;
public $original;
}
namespace Drupal\Core\Entity\KeyValueStore;
if (!defined('SAVED_NEW')) {

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Form;
use Drupal\Core\Form\ConfigFormBaseTrait;
use Drupal\Tests\UnitTestCase;
/**
@ -15,7 +16,7 @@ class ConfigFormBaseTraitTest extends UnitTestCase {
*/
public function testConfig() {
$trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait');
$trait = $this->createPartialMock(ConfiguredTrait::class, ['getEditableConfigNames']);
// Set up some configuration in a mocked config factory.
$trait->configFactory = $this->getConfigFactoryStub([
'editable.config' => [],
@ -58,7 +59,6 @@ class ConfigFormBaseTraitTest extends UnitTestCase {
*/
public function testConfigFactoryExceptionInvalidProperty() {
$trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait');
$trait->configFactory = TRUE;
$config_method = new \ReflectionMethod($trait, 'config');
$config_method->setAccessible(TRUE);
@ -69,3 +69,11 @@ class ConfigFormBaseTraitTest extends UnitTestCase {
}
}
class ConfiguredTrait {
use ConfigFormBaseTrait;
public $configFactory;
protected function getEditableConfigNames() {}
}