From 09be91a39563bd1ea29ecbfa1276066b7c8279d7 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Thu, 16 Apr 2020 19:40:52 +0100 Subject: [PATCH] Issue #3126564 by longwave, mondrake: Replace usage of assertArraySubset(), that is deprecated --- .../src/Functional/CommentActionsTest.php | 2 +- .../Migrate/d7/MigrateCommentTypeTest.php | 4 ++- .../src/Kernel/FormElementInlineErrorTest.php | 2 +- .../tests/src/Functional/ResourceTestBase.php | 8 ++++-- .../JsonApiDocumentTopLevelNormalizerTest.php | 12 ++++++--- .../ResourceType/RelatedResourceTypesTest.php | 2 +- .../EntityResource/EntityResourceTestBase.php | 27 ++++++++++++++++--- .../Core/Action/DeleteActionTest.php | 4 +-- .../Core/Action/PublishActionTest.php | 19 +++++++------ .../Core/Action/SaveActionTest.php | 17 ++++++------ .../Drupal/Tests/Traits/PHPUnit8Warnings.php | 1 - 11 files changed, 62 insertions(+), 36 deletions(-) diff --git a/core/modules/comment/tests/src/Functional/CommentActionsTest.php b/core/modules/comment/tests/src/Functional/CommentActionsTest.php index aa7b0da40827..ec0a45c809fc 100644 --- a/core/modules/comment/tests/src/Functional/CommentActionsTest.php +++ b/core/modules/comment/tests/src/Functional/CommentActionsTest.php @@ -37,7 +37,7 @@ public function testCommentPublishUnpublishActions() { $action = Action::load('comment_unpublish_action'); $action->execute([$comment]); $this->assertTrue($comment->isPublished() === FALSE, 'Comment was unpublished'); - $this->assertArraySubset(['module' => ['comment']], $action->getDependencies()); + $this->assertSame(['module' => ['comment']], $action->getDependencies()); // Publish a comment. $action = Action::load('comment_publish_action'); $action->execute([$comment]); diff --git a/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTypeTest.php b/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTypeTest.php index 565075a5680a..8345cef94b33 100644 --- a/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTypeTest.php +++ b/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTypeTest.php @@ -54,7 +54,9 @@ public function testMigration() { 'comment_preview' => 'Preview comment', 'comment_form_location' => 'Location of comment submission form', ]; - $this->assertArraySubset($comment_fields, $this->migration->getSourcePlugin()->fields()); + foreach ($comment_fields as $field => $description) { + $this->assertEquals($description, $this->migration->getSourcePlugin()->fields()[$field]); + } $this->assertEntity('comment_node_article', 'Article comment'); $this->assertEntity('comment_node_blog', 'Blog entry comment'); diff --git a/core/modules/inline_form_errors/tests/src/Kernel/FormElementInlineErrorTest.php b/core/modules/inline_form_errors/tests/src/Kernel/FormElementInlineErrorTest.php index 8ce31a2fe978..7fbc9f7c91d6 100644 --- a/core/modules/inline_form_errors/tests/src/Kernel/FormElementInlineErrorTest.php +++ b/core/modules/inline_form_errors/tests/src/Kernel/FormElementInlineErrorTest.php @@ -42,7 +42,7 @@ public function testDisplayErrorMessagesNotInline() { // Just test if the #error_no_message property is TRUE. FormErrorHandlerTest // tests if the property actually hides the error message. - $this->assertArraySubset(['#error_no_message' => TRUE], $form['test']); + $this->assertTrue($form['test']['#error_no_message']); } } diff --git a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php index 4473edb416d7..4b8cd18a679f 100644 --- a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php +++ b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php @@ -2005,7 +2005,9 @@ public function testPostIndividual() { // properties are present, the server could be computing additional // properties. if (is_array($field_normalization)) { - $this->assertArraySubset($field_normalization, $created_entity_document['data']['attributes'][$field_name]); + foreach ($field_normalization as $value) { + $this->assertContains($value, $created_entity_document['data']['attributes'][$field_name]); + } } else { $this->assertSame($field_normalization, $created_entity_document['data']['attributes'][$field_name]); @@ -2259,7 +2261,9 @@ public function testPatchIndividual() { // properties are present, the server could be computing additional // properties. if (is_array($field_normalization)) { - $this->assertArraySubset($field_normalization, $updated_entity_document['data']['attributes'][$field_name]); + foreach ($field_normalization as $value) { + $this->assertContains($value, $updated_entity_document['data']['attributes'][$field_name]); + } } else { $this->assertSame($field_normalization, $updated_entity_document['data']['attributes'][$field_name]); diff --git a/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php b/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php index 2cb4fb43852b..9ca239a615da 100644 --- a/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php +++ b/core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php @@ -288,7 +288,7 @@ public function testNormalize() { $this->assertTrue(!isset($normalized['included'][1]['attributes']['created'])); // Make sure that the cache tags for the includes and the requested entities // are bubbling as expected. - $this->assertArraySubset( + $this->assertSame( ['file:1', 'node:1', 'taxonomy_term:1', 'taxonomy_term:2', 'user:1'], $jsonapi_doc_object->getCacheTags() ); @@ -378,7 +378,7 @@ public function testNormalizeUuid() { $this->assertCount(12, $normalized['included'][1]['attributes']); // Make sure that the cache tags for the includes and the requested entities // are bubbling as expected. - $this->assertArraySubset( + $this->assertSame( ['node:1', 'taxonomy_term:1', 'taxonomy_term:2', 'user:1'], $jsonapi_doc_object->getCacheTags() ); @@ -693,8 +693,12 @@ public function testCacheableMetadata(CacheableMetadata $expected_metadata, $fie 'account' => NULL, ]; $jsonapi_doc_object = $this->getNormalizer()->normalize(new JsonApiDocumentTopLevel(new ResourceObjectData([$resource_object], 1), new NullIncludedData(), new LinkCollection([])), 'api_json', $context); - $this->assertArraySubset($expected_metadata->getCacheTags(), $jsonapi_doc_object->getCacheTags()); - $this->assertArraySubset($expected_metadata->getCacheContexts(), $jsonapi_doc_object->getCacheContexts()); + foreach ($expected_metadata->getCacheTags() as $tag) { + $this->assertContains($tag, $jsonapi_doc_object->getCacheTags()); + } + foreach ($expected_metadata->getCacheContexts() as $context) { + $this->assertContains($context, $jsonapi_doc_object->getCacheContexts()); + } $this->assertSame($expected_metadata->getCacheMaxAge(), $jsonapi_doc_object->getCacheMaxAge()); } diff --git a/core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php b/core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php index 3777200d3d3e..c8afcce764ef 100644 --- a/core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php +++ b/core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php @@ -134,7 +134,7 @@ public function testGetRelatableResourceTypes($resource_type_name, $relatable_ty } } - $this->assertArraySubset($relatable_type_names, $subjects); + $this->assertEquals($relatable_type_names, $subjects); } /** diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index a8034294227d..2628bb544628 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -1439,10 +1439,29 @@ protected function assertStoredEntityMatchesSentNormalization(array $sent_normal // as strings in PHP memory. $expected_stored_data = static::castToString($expected_stored_data); } - // Subset, not same, because we can e.g. send just the target_id for the - // bundle in a PATCH or POST request; the response will include more - // properties. - $this->assertArraySubset($expected_stored_data, $modified_entity->get($field_name)->getValue(), TRUE); + $this->assertEntityArraySubset($expected_stored_data, $modified_entity->get($field_name)->getValue()); + } + } + } + + /** + * Recursively asserts that the expected items are set in the tested entity. + * + * A response may include more properties, we only need to ensure that all + * items in the request exist in the response. + * + * @param $expected + * An array of expected values, may contain further nested arrays. + * @param $actual + * The object to test. + */ + protected function assertEntityArraySubset($expected, $actual) { + foreach ($expected as $key => $value) { + if (is_array($value)) { + $this->assertEntityArraySubset($value, $actual[$key]); + } + else { + $this->assertSame($value, $actual[$key]); } } } diff --git a/core/tests/Drupal/KernelTests/Core/Action/DeleteActionTest.php b/core/tests/Drupal/KernelTests/Core/Action/DeleteActionTest.php index 11cb3708b00f..9d3bd1ac994a 100644 --- a/core/tests/Drupal/KernelTests/Core/Action/DeleteActionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Action/DeleteActionTest.php @@ -80,12 +80,12 @@ public function testDeleteAction() { $action->save(); $action->execute([$entity]); - $this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies()); + $this->assertSame(['module' => ['entity_test']], $action->getDependencies()); /** @var \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store */ $temp_store = \Drupal::service('tempstore.private'); $store_entries = $temp_store->get('entity_delete_multiple_confirm')->get($this->testUser->id() . ':entity_test_mulrevpub'); - $this->assertArraySubset([$this->testUser->id() => ['en' => 'en']], $store_entries); + $this->assertSame([$this->testUser->id() => ['en' => 'en']], $store_entries); } } diff --git a/core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php b/core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php index 0b76d7eea088..159c94318c7f 100644 --- a/core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Action/PublishActionTest.php @@ -30,15 +30,14 @@ protected function setUp(): void { */ public function testGetDerivativeDefinitions() { $deriver = new EntityPublishedActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation()); - $this->assertArraySubset([ - 'entity_test_mulrevpub' => [ - 'type' => 'entity_test_mulrevpub', - 'label' => 'Save test entity - revisions, data table, and published interface', - 'action_label' => 'Save', - ], - ], $deriver->getDerivativeDefinitions([ + $definitions = $deriver->getDerivativeDefinitions([ 'action_label' => 'Save', - ])); + ]); + $this->assertEquals([ + 'type' => 'entity_test_mulrevpub', + 'label' => 'Save test entity - revisions, data table, and published interface', + 'action_label' => 'Save', + ], $definitions['entity_test_mulrevpub']); } /** @@ -56,7 +55,7 @@ public function testPublishAction() { $this->assertFalse($entity->isPublished()); $action->execute([$entity]); $this->assertTrue($entity->isPublished()); - $this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies()); + $this->assertSame(['module' => ['entity_test']], $action->getDependencies()); } /** @@ -74,7 +73,7 @@ public function testUnpublishAction() { $this->assertTrue($entity->isPublished()); $action->execute([$entity]); $this->assertFalse($entity->isPublished()); - $this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies()); + $this->assertSame(['module' => ['entity_test']], $action->getDependencies()); } } diff --git a/core/tests/Drupal/KernelTests/Core/Action/SaveActionTest.php b/core/tests/Drupal/KernelTests/Core/Action/SaveActionTest.php index 903164e1c141..21d511e729f4 100644 --- a/core/tests/Drupal/KernelTests/Core/Action/SaveActionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Action/SaveActionTest.php @@ -30,15 +30,14 @@ protected function setUp(): void { */ public function testGetDerivativeDefinitions() { $deriver = new EntityChangedActionDeriver(\Drupal::entityTypeManager(), \Drupal::translation()); - $this->assertArraySubset([ - 'entity_test_mul_changed' => [ - 'type' => 'entity_test_mul_changed', - 'label' => 'Save test entity - data table', - 'action_label' => 'Save', - ], - ], $deriver->getDerivativeDefinitions([ + $definitions = $deriver->getDerivativeDefinitions([ 'action_label' => 'Save', - ])); + ]); + $this->assertEquals([ + 'type' => 'entity_test_mul_changed', + 'label' => 'Save test entity - data table', + 'action_label' => 'Save', + ], $definitions['entity_test_mul_changed']); } /** @@ -56,7 +55,7 @@ public function testSaveAction() { $action->save(); $action->execute([$entity]); $this->assertNotSame($saved_time, $entity->getChangedTime()); - $this->assertArraySubset(['module' => ['entity_test']], $action->getDependencies()); + $this->assertSame(['module' => ['entity_test']], $action->getDependencies()); } } diff --git a/core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php b/core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php index e011499fabc9..be0f3f1929ee 100644 --- a/core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php +++ b/core/tests/Drupal/Tests/Traits/PHPUnit8Warnings.php @@ -23,7 +23,6 @@ trait PHPUnit8Warnings { * @var string[] */ private static $ignoredWarnings = [ - 'assertArraySubset() is deprecated and will be removed in PHPUnit 9.', 'readAttribute() is deprecated and will be removed in PHPUnit 9.', 'getObjectAttribute() is deprecated and will be removed in PHPUnit 9.', 'assertAttributeEquals() is deprecated and will be removed in PHPUnit 9.', -- GitLab