From 955418c2df6ae78b5093deeef783dad350749de0 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 9 Apr 2024 11:44:18 +0100 Subject: [PATCH] Issue #3402294 by mstrelan, quietone, dww, smustgrave: Fix strict type errors: Convert FormattableMarkup to strings (complex replacement) in core Kernel tests --- .../tests/src/Kernel/EntityDisplayTest.php | 5 ++- .../src/Kernel/FileManagedUnitTestBase.php | 6 ++-- .../file/tests/src/Kernel/MoveTest.php | 3 +- .../tests/src/Kernel/FilterKernelTest.php | 33 +++++++++---------- .../tests/src/Kernel/MenuLinksTest.php | 4 +-- .../tests/src/Kernel/NodeAccessTestBase.php | 13 +++----- .../src/Kernel/Common/SystemListingTest.php | 4 +-- .../src/Kernel/Form/ProgrammaticTest.php | 11 +++---- .../src/Kernel/Handler/FieldKernelTest.php | 31 ++++++++--------- .../Drupal/KernelTests/ConfigFormTestBase.php | 10 ++---- .../Core/Database/InsertLobTest.php | 4 +-- .../Core/Database/UpdateLobTest.php | 4 +-- .../Core/Entity/ContentEntityCloneTest.php | 33 +++++++++---------- .../KernelTests/Core/File/FileTestBase.php | 4 +-- .../KernelTests/Core/File/HtaccessTest.php | 11 +++---- 15 files changed, 77 insertions(+), 99 deletions(-) diff --git a/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php b/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php index a0002ba70c5f..d9b76b4fe050 100644 --- a/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php +++ b/core/modules/field_ui/tests/src/Kernel/EntityDisplayTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\field_ui\Kernel; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Cache\Cache; use Drupal\Core\Database\Database; use Drupal\Core\Entity\Display\EntityDisplayInterface; @@ -703,8 +702,8 @@ protected function assertDependencyHelper(bool $assertion, string $type, string $dependencies = !empty($all_dependencies[$type]) ? $all_dependencies[$type] : []; $context = $display instanceof EntityViewDisplayInterface ? 'View' : 'Form'; $value = $assertion ? in_array($key, $dependencies) : !in_array($key, $dependencies); - $args = ['@context' => $context, '@id' => $display->id(), '@type' => $type, '@key' => $key]; - $message = $assertion ? new FormattableMarkup("@context display '@id' depends on @type '@key'.", $args) : new FormattableMarkup("@context display '@id' do not depend on @type '@key'.", $args); + $display_id = $display->id(); + $message = $assertion ? "$context display '$display_id' depends on $type '$key'." : "$context display '$display_id' do not depend on $type '$key'."; $this->assertTrue($value, $message); } diff --git a/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php b/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php index 19ca6f584a01..2ee17ddb73d4 100644 --- a/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php +++ b/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php @@ -57,16 +57,16 @@ public function assertFileHooksCalled($expected) { // Determine if there were any expected that were not called. $uncalled = array_diff($expected, $actual); if (count($uncalled)) { - $this->assertTrue(FALSE, new FormattableMarkup('Expected hooks %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)])); + $this->assertTrue(FALSE, sprintf('Expected hooks %s to be called but %s was not called.', implode(', ', $expected), implode(', ', $uncalled))); } else { - $this->assertTrue(TRUE, new FormattableMarkup('All the expected hooks were called: %expected', ['%expected' => empty($expected) ? '(none)' : implode(', ', $expected)])); + $this->assertTrue(TRUE, sprintf('All the expected hooks were called: %s', empty($expected) ? '(none)' : implode(', ', $expected))); } // Determine if there were any unexpected calls. $unexpected = array_diff($actual, $expected); if (count($unexpected)) { - $this->assertTrue(FALSE, new FormattableMarkup('Unexpected hooks were called: %unexpected.', ['%unexpected' => empty($unexpected) ? '(none)' : implode(', ', $unexpected)])); + $this->assertTrue(FALSE, sprintf('Unexpected hooks were called: %s.', empty($unexpected) ? '(none)' : implode(', ', $unexpected))); } else { $this->assertTrue(TRUE, 'No unexpected hooks were called.'); diff --git a/core/modules/file/tests/src/Kernel/MoveTest.php b/core/modules/file/tests/src/Kernel/MoveTest.php index bcd38e27c0dc..58b4c1a20f2b 100644 --- a/core/modules/file/tests/src/Kernel/MoveTest.php +++ b/core/modules/file/tests/src/Kernel/MoveTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\file\Kernel; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\File\Exception\FileExistsException; @@ -57,7 +56,7 @@ public function testNormal() { $this->assertFileHooksCalled(['move', 'load', 'update']); // Make sure we got the same file back. - $this->assertEquals($source->id(), $result->id(), new FormattableMarkup("Source file id's' %fid is unchanged after move.", ['%fid' => $source->id()])); + $this->assertEquals($source->id(), $result->id(), "Source file ID {$source->id()} should be unchanged after move."); // Reload the file from the database and check that the changes were // actually saved. diff --git a/core/modules/filter/tests/src/Kernel/FilterKernelTest.php b/core/modules/filter/tests/src/Kernel/FilterKernelTest.php index a16c65f37f03..a2cce36b180c 100644 --- a/core/modules/filter/tests/src/Kernel/FilterKernelTest.php +++ b/core/modules/filter/tests/src/Kernel/FilterKernelTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\filter\Kernel; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Html; use Drupal\Core\Language\Language; use Drupal\Core\Render\RenderContext; @@ -992,18 +991,18 @@ public function assertFilteredString(FilterInterface $filter, array $tests): voi $result = $filter->process($source, $filter)->getProcessedText(); foreach ($tasks as $value => $is_expected) { if ($is_expected) { - $this->assertStringContainsString($value, $result, new FormattableMarkup('@source: @value found. Filtered result: @result.', [ - '@source' => var_export($source, TRUE), - '@value' => var_export($value, TRUE), - '@result' => var_export($result, TRUE), - ])); + $this->assertStringContainsString($value, $result, sprintf('%s: %s found. Filtered result: %s.', + var_export($source, TRUE), + var_export($value, TRUE), + var_export($result, TRUE), + )); } else { - $this->assertStringNotContainsString($value, $result, new FormattableMarkup('@source: @value not found. Filtered result: @result.', [ - '@source' => var_export($source, TRUE), - '@value' => var_export($value, TRUE), - '@result' => var_export($result, TRUE), - ])); + $this->assertStringNotContainsString($value, $result, sprintf('%s: %s not found. Filtered result: %s.', + var_export($source, TRUE), + var_export($value, TRUE), + var_export($result, TRUE), + )); } } } @@ -1176,7 +1175,7 @@ public function testHtmlCorrectorFilter() { body {color:red} /*]]>*/ </style></p>'; - $this->assertEquals($html, Html::normalize($html), new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', ['@pattern_name' => '/*<![CDATA[*/'])); + $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section /*<![CDATA[*/ properly escaped'); $html = '<p><style> /*<![CDATA[*/ @@ -1184,28 +1183,28 @@ public function testHtmlCorrectorFilter() { body {color:red} /*]]>*/ </style></p>'; - $this->assertEquals($html, Html::normalize($html), new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', ['@pattern_name' => '<!--/*--><![CDATA[/* ><!--*/'])); + $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section <!--/*--><![CDATA[/* ><!--*/ properly escaped'); $html = '<p><script> //<![CDATA[ alert("test"); //]]> </script></p>'; - $this->assertEquals($html, Html::normalize($html), new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', ['@pattern_name' => '<!--//--><![CDATA[// ><!--'])); + $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section <!--//--><![CDATA[// ><!-- properly escaped'); $html = '<p><script> // <![CDATA[ alert("test"); //]]> </script></p>'; - $this->assertEquals($html, Html::normalize($html), new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', ['@pattern_name' => '// <![CDATA['])); + $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section // <![CDATA[ properly escaped'); $html = '<p><script> // <![CDATA[![CDATA[![CDATA[ alert("test"); //]]]]]]> </script></p>'; - $this->assertEquals($html, Html::normalize($html), new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', ['@pattern_name' => '// <![CDATA[![CDATA[![CDATA['])); + $this->assertEquals($html, Html::normalize($html), 'HTML corrector -- Existing cdata section // <![CDATA[![CDATA[![CDATA[ properly escaped'); // Test calling Html::normalize() twice. $html = '<p><script> @@ -1213,7 +1212,7 @@ public function testHtmlCorrectorFilter() { alert("test"); //]]]]]]> </script></p>'; - $this->assertEquals($html, Html::normalize(Html::normalize($html)), new FormattableMarkup('HTML corrector -- Existing cdata section @pattern_name properly escaped', ['@pattern_name' => '// <![CDATA[![CDATA[![CDATA['])); + $this->assertEquals($html, Html::normalize(Html::normalize($html)), 'HTML corrector -- Existing cdata section // <![CDATA[![CDATA[![CDATA[ properly escaped'); } /** diff --git a/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php b/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php index b39df7f9aeae..2632111cfa20 100644 --- a/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php +++ b/core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\menu_link_content\Kernel; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Menu\MenuTreeParameters; use Drupal\entity_test\Entity\EntityTestExternal; use Drupal\KernelTests\KernelTestBase; @@ -129,7 +128,8 @@ public function assertMenuLinkParents(array $links, array $expected_hierarchy): $menu_link_plugin = $this->menuLinkManager->createInstance($links[$id]); $expected_parent = $links[$parent] ?? ''; - $this->assertEquals($expected_parent, $menu_link_plugin->getParent(), new FormattableMarkup('Menu link %id has parent of %parent, expected %expected_parent.', ['%id' => $id, '%parent' => $menu_link_plugin->getParent(), '%expected_parent' => $expected_parent])); + $link_parent = $menu_link_plugin->getParent(); + $this->assertEquals($expected_parent, $link_parent, "Menu link $id has parent of $link_parent, expected $expected_parent."); } } diff --git a/core/modules/node/tests/src/Kernel/NodeAccessTestBase.php b/core/modules/node/tests/src/Kernel/NodeAccessTestBase.php index bce5391b1922..e96c45538a87 100644 --- a/core/modules/node/tests/src/Kernel/NodeAccessTestBase.php +++ b/core/modules/node/tests/src/Kernel/NodeAccessTestBase.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\node\Kernel; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Session\AccountInterface; use Drupal\KernelTests\KernelTestBase; use Drupal\node\NodeInterface; @@ -137,13 +136,11 @@ public function assertNodeCreateAccess(string $bundle, bool $result, AccountInte * about the node access permission test that was performed. */ public function nodeAccessAssertMessage($operation, $result, $langcode = NULL) { - return new FormattableMarkup( - 'Node access returns @result with operation %op, language code %langcode.', - [ - '@result' => $result ? 'true' : 'false', - '%op' => $operation, - '%langcode' => !empty($langcode) ? $langcode : 'empty', - ] + return sprintf( + 'Node access returns %s with operation %s, language code %s.', + $result ? 'true' : 'false', + $operation, + !empty($langcode) ? $langcode : 'empty', ); } diff --git a/core/modules/system/tests/src/Kernel/Common/SystemListingTest.php b/core/modules/system/tests/src/Kernel/Common/SystemListingTest.php index b48da4ed26a3..4c595cef2f8b 100644 --- a/core/modules/system/tests/src/Kernel/Common/SystemListingTest.php +++ b/core/modules/system/tests/src/Kernel/Common/SystemListingTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\system\Kernel\Common; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\KernelTests\KernelTestBase; @@ -46,7 +45,8 @@ public function testDirectoryPrecedence() { foreach ($expected_directories as $module => $directories) { $expected_directory = array_shift($directories); $expected_uri = "$expected_directory/$module/$module.info.yml"; - $this->assertEquals($expected_uri, $files[$module]->getPathname(), new FormattableMarkup('Module @actual was found at @expected.', ['@actual' => $files[$module]->getPathname(), '@expected' => $expected_uri])); + $module_path = $files[$module]->getPathname(); + $this->assertEquals($expected_uri, $module_path, "Module $module_path was found at $expected_uri."); } } diff --git a/core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php b/core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php index 535ed462aa1c..2f62d85eea3f 100644 --- a/core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php +++ b/core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\system\Kernel\Form; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Form\FormState; use Drupal\KernelTests\KernelTestBase; @@ -73,18 +72,16 @@ protected function doSubmitForm($values, $valid_input) { // Check that the form returns an error when expected, and vice versa. $errors = $form_state->getErrors(); $valid_form = empty($errors); - $args = [ - '%values' => print_r($values, TRUE), - '%errors' => $valid_form ? 'None' : implode(' ', $errors), - ]; - $this->assertSame($valid_form, $valid_input, new FormattableMarkup('Input values: %values<br />Validation handler errors: %errors', $args)); + $input_values = print_r($values, TRUE); + $validation_errors = $valid_form ? t('None') : implode(' ', $errors); + $this->assertSame($valid_form, $valid_input, sprintf('Input values: %s<br />Validation handler errors: %s', $input_values, $validation_errors)); // We check submitted values only if we have a valid input. if ($valid_input) { // Fetching the values that were set in the submission handler. $stored_values = $form_state->get('programmatic_form_submit'); foreach ($values as $key => $value) { - $this->assertEquals($value, $stored_values[$key], new FormattableMarkup('Submission handler correctly executed: %stored_key is %stored_value', ['%stored_key' => $key, '%stored_value' => print_r($value, TRUE)])); + $this->assertEquals($value, $stored_values[$key], sprintf('Submission handler correctly executed: %s is %s', $key, print_r($value, TRUE))); } } } diff --git a/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php b/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php index 6e630d043382..4dce202cd15b 100644 --- a/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php +++ b/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\views\Kernel\Handler; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Render\RenderContext; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; use Drupal\views\Plugin\views\field\FieldPluginBase; @@ -102,9 +101,8 @@ public function testQuery() { * The value to search for. * @param string $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Render\FormattableMarkup to embed - * variables in the message text, not t(). If left blank, a default message - * will be displayed. + * messages: use string interpolation to embed variables in the message + * text, not t(). If left blank, a default message will be displayed. * * @internal */ @@ -121,9 +119,8 @@ protected function assertSubString(string $haystack, string $needle, string $mes * The value to search for. * @param string $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Render\FormattableMarkup to embed - * variables in the message text, not t(). If left blank, a default message - * will be displayed. + * messages: use string interpolation to embed variables in the message + * text, not t(). If left blank, a default message will be displayed. * * @internal */ @@ -288,17 +285,17 @@ public function testFieldTokens() { $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field_0, $row) { return $name_field_0->advancedRender($row); }); - $this->assertEquals($expected_output_0, $output, new FormattableMarkup('Test token replacement: "@token" gave "@output"', ['@token' => $name_field_0->options['alter']['text'], '@output' => $output])); + $this->assertEquals($expected_output_0, $output, sprintf('Test token replacement: "%s" gave "%s"', $name_field_0->options['alter']['text'], $output)); $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field_1, $row) { return $name_field_1->advancedRender($row); }); - $this->assertEquals($expected_output_1, $output, new FormattableMarkup('Test token replacement: "@token" gave "@output"', ['@token' => $name_field_1->options['alter']['text'], '@output' => $output])); + $this->assertEquals($expected_output_1, $output, sprintf('Test token replacement: "%s" gave "%s"', $name_field_1->options['alter']['text'], $output)); $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field_2, $row) { return $name_field_2->advancedRender($row); }); - $this->assertEquals($expected_output_2, $output, new FormattableMarkup('Test token replacement: "@token" gave "@output"', ['@token' => $name_field_2->options['alter']['text'], '@output' => $output])); + $this->assertEquals($expected_output_2, $output, sprintf('Test token replacement: "%s" gave %s"', $name_field_2->options['alter']['text'], $output)); } $job_field = $view->field['job']; @@ -310,11 +307,11 @@ public function testFieldTokens() { $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($job_field, $row) { return $job_field->advancedRender($row); }); - $this->assertSubString($output, $random_text, new FormattableMarkup('Make sure the self token (@token => @value) appears in the output (@output)', [ - '@value' => $random_text, - '@output' => $output, - '@token' => $job_field->options['alter']['text'], - ])); + $this->assertSubString($output, $random_text, sprintf('Make sure the self token (%s => %s) appears in the output (%s)', + $job_field->options['alter']['text'], + $random_text, + $output, + )); // Verify the token format used in D7 and earlier does not get substituted. $old_token = '[job]'; @@ -324,7 +321,7 @@ public function testFieldTokens() { $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($job_field, $row) { return $job_field->advancedRender($row); }); - $this->assertEquals($old_token, $output, new FormattableMarkup('Make sure the old token style (@token => @value) is not changed in the output (@output)', ['@value' => $random_text, '@output' => $output, '@token' => $job_field->options['alter']['text']])); + $this->assertEquals($old_token, $output, sprintf('Make sure the old token style (%s => %s) is not changed in the output (%s)', $job_field->options['alter']['text'], $random_text, $output)); // Verify HTML tags are allowed in rewrite templates while token // replacements are escaped. @@ -353,7 +350,7 @@ public function testFieldTokens() { $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($job_field, $row) { return $job_field->advancedRender($row); }); - $this->assertEquals($random_text, $output, new FormattableMarkup('Make sure a script tag in the template (@template) is removed, leaving only the replaced token in the output (@output)', ['@output' => $output, '@template' => $rewrite_template])); + $this->assertEquals($random_text, $output, "Make sure a script tag in the template ($rewrite_template) is removed, leaving only the replaced token in the output ($output)"); } /** diff --git a/core/tests/Drupal/KernelTests/ConfigFormTestBase.php b/core/tests/Drupal/KernelTests/ConfigFormTestBase.php index e042045dcfb4..a314a8f1febd 100644 --- a/core/tests/Drupal/KernelTests/ConfigFormTestBase.php +++ b/core/tests/Drupal/KernelTests/ConfigFormTestBase.php @@ -2,7 +2,6 @@ namespace Drupal\KernelTests; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Form\FormState; /** @@ -53,12 +52,9 @@ public function testConfigForm() { // Check that the form returns an error when expected, and vice versa. $errors = $form_state->getErrors(); $valid_form = empty($errors); - $args = [ - '%values' => print_r($values, TRUE), - '%errors' => $valid_form ? t('None') : implode(' ', $errors), - ]; - $this->assertTrue($valid_form, new FormattableMarkup('Input values: %values<br/>Validation handler errors: %errors', $args)); - + $values = print_r($values, TRUE); + $errors = $valid_form ? t('None') : implode(' ', $errors); + $this->assertTrue($valid_form, sprintf('Input values: %s<br/>Validation handler errors: %s', $values, $errors)); foreach ($this->values as $data) { $this->assertEquals($this->config($data['#config_name'])->get($data['#config_key']), $data['#value']); } diff --git a/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php b/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php index 45f635ca41e3..0022f24f68db 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php @@ -2,8 +2,6 @@ namespace Drupal\KernelTests\Core\Database; -use Drupal\Component\Render\FormattableMarkup; - /** * Tests the Insert query builder with LOB fields. * @@ -21,7 +19,7 @@ public function testInsertOneBlob() { ->fields(['blob1' => $data]) ->execute(); $r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id])->fetchAssoc(); - $this->assertSame($data, $r['blob1'], new FormattableMarkup('Can insert a blob: id @id, @data.', ['@id' => $id, '@data' => serialize($r)])); + $this->assertSame($data, $r['blob1'], "Can insert a blob: id $id, " . serialize($r)); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php b/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php index c97b8ae93f77..06c7d5ffe0be 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php @@ -2,8 +2,6 @@ namespace Drupal\KernelTests\Core\Database; -use Drupal\Component\Render\FormattableMarkup; - /** * Tests the Update query builder with LOB fields. * @@ -28,7 +26,7 @@ public function testUpdateOneBlob() { ->execute(); $r = $this->connection->query('SELECT * FROM {test_one_blob} WHERE [id] = :id', [':id' => $id])->fetchAssoc(); - $this->assertSame($data, $r['blob1'], new FormattableMarkup('Can update a blob: id @id, @data.', ['@id' => $id, '@data' => serialize($r)])); + $this->assertSame($data, $r['blob1'], "Can update a blob: id $id, " . serialize($r)); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php index 74cbc4eeff4a..515aee921b46 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php @@ -2,7 +2,6 @@ namespace Drupal\KernelTests\Core\Entity; -use Drupal\Component\Render\FormattableMarkup; use Drupal\entity_test\Entity\EntityTestMul; use Drupal\entity_test\Entity\EntityTestMulRev; use Drupal\language\Entity\ConfigurableLanguage; @@ -61,14 +60,12 @@ public function testFieldEntityReferenceAfterClone() { $translation = $clone->getTranslation($langcode); foreach ($translation->getFields() as $field_name => $field) { if ($field->getFieldDefinition()->isTranslatable()) { - $args = ['%field_name' => $field_name, '%langcode' => $langcode]; - $this->assertEquals($langcode, $field->getEntity()->language()->getId(), new FormattableMarkup('Translatable field %field_name on translation %langcode has correct entity reference in translation %langcode after cloning.', $args)); - $this->assertSame($translation, $field->getEntity(), new FormattableMarkup('Translatable field %field_name on translation %langcode has correct reference to the cloned entity object.', $args)); + $this->assertEquals($langcode, $field->getEntity()->language()->getId(), "Translatable field $field_name on translation $langcode has correct entity reference in translation $langcode after cloning."); + $this->assertSame($translation, $field->getEntity(), "Translatable field $field_name on translation $langcode has correct reference to the cloned entity object."); } else { - $args = ['%field_name' => $field_name, '%langcode' => $langcode, '%default_langcode' => $default_langcode]; - $this->assertEquals($default_langcode, $field->getEntity()->language()->getId(), new FormattableMarkup('Non translatable field %field_name on translation %langcode has correct entity reference in the default translation %default_langcode after cloning.', $args)); - $this->assertSame($translation->getUntranslated(), $field->getEntity(), new FormattableMarkup('Non translatable field %field_name on translation %langcode has correct reference to the cloned entity object in the default translation %default_langcode.', $args)); + $this->assertEquals($default_langcode, $field->getEntity()->language()->getId(), "Non translatable field $field_name on translation $langcode has correct entity reference in the default translation $default_langcode after cloning."); + $this->assertSame($translation->getUntranslated(), $field->getEntity(), "Non translatable field $field_name on translation $langcode has correct reference to the cloned entity object in the default translation $default_langcode."); } } } @@ -300,6 +297,8 @@ public function testEntityPropertiesModifications() { $translation_unique_properties = ['activeLangcode', 'translationInitialize', 'fieldDefinitions', 'languages', 'langcodeKey', 'defaultLangcode', 'defaultLangcodeKey', 'revisionTranslationAffectedKey', 'validated', 'validationRequired', 'entityTypeId', 'typedData', 'cacheContexts', 'cacheTags', 'cacheMaxAge', '_serviceIds', '_entityStorages', 'enforceDefaultTranslation']; foreach ($properties as $property) { + $property_name = $property->getName(); + // Modify each entity property on the clone and assert that the change is // not propagated to the original entity. $property->setValue($entity, 'default-value'); @@ -307,14 +306,14 @@ public function testEntityPropertiesModifications() { $property->setValue($clone, 'test-entity-cloning'); // Static properties remain the same across all instances of the class. if ($property->isStatic()) { - $this->assertEquals('test-entity-cloning', $property->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); - $this->assertEquals('test-entity-cloning', $property->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); - $this->assertEquals('test-entity-cloning', $property->getValue($clone), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); + $this->assertEquals('test-entity-cloning', $property->getValue($entity), "Entity property $property_name is not cloned properly."); + $this->assertEquals('test-entity-cloning', $property->getValue($translation), "Entity property $property_name is not cloned properly."); + $this->assertEquals('test-entity-cloning', $property->getValue($clone), "Entity property $property_name is not cloned properly."); } else { - $this->assertEquals('default-value', $property->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); - $this->assertEquals('default-value', $property->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); - $this->assertEquals('test-entity-cloning', $property->getValue($clone), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); + $this->assertEquals('default-value', $property->getValue($entity), "Entity property $property_name is not cloned properly."); + $this->assertEquals('default-value', $property->getValue($translation), "Entity property $property_name is not cloned properly."); + $this->assertEquals('test-entity-cloning', $property->getValue($clone), "Entity property $property_name is not cloned properly."); } // Modify each entity property on the translation entity object and assert @@ -330,12 +329,12 @@ public function testEntityPropertiesModifications() { // not be able to properly access all properties and this will cause // exceptions without a proper backtrace. if (in_array($property->getName(), $translation_unique_properties)) { - $this->assertEquals('default-value', $property->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); - $this->assertEquals('test-translation-cloning', $property->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); + $this->assertEquals('default-value', $property->getValue($entity), "Entity property $property_name is not cloned properly."); + $this->assertEquals('test-translation-cloning', $property->getValue($translation), "Entity property $property_name is not cloned properly."); } else { - $this->assertEquals('test-translation-cloning', $property->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); - $this->assertEquals('test-translation-cloning', $property->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', ['%property_name' => $property->getName()])); + $this->assertEquals('test-translation-cloning', $property->getValue($entity), "Entity property $property_name is not cloned properly."); + $this->assertEquals('test-translation-cloning', $property->getValue($translation), "Entity property $property_name is not cloned properly."); } } } diff --git a/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php b/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php index b41787f452ad..2875d43aff15 100644 --- a/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php +++ b/core/tests/Drupal/KernelTests/Core/File/FileTestBase.php @@ -106,7 +106,7 @@ public function assertFilePermissions($filepath, $expected_mode, $message = NULL } if (!isset($message)) { - $message = t('Expected file permission to be %expected, actually were %actual.', ['%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)]); + $message = sprintf('Expected file permission to be %s, actually were %s.', decoct($actual_mode), decoct($expected_mode)); } $this->assertEquals($expected_mode, $actual_mode, $message); } @@ -142,7 +142,7 @@ public function assertDirectoryPermissions($directory, $expected_mode, $message } if (!isset($message)) { - $message = t('Expected directory permission to be %expected, actually were %actual.', ['%actual' => decoct($actual_mode), '%expected' => decoct($expected_mode)]); + $message = sprintf('Expected directory permission to be %s, actually were %s.', decoct($expected_mode), decoct($actual_mode)); } $this->assertEquals($expected_mode, $actual_mode, $message); } diff --git a/core/tests/Drupal/KernelTests/Core/File/HtaccessTest.php b/core/tests/Drupal/KernelTests/Core/File/HtaccessTest.php index e49ff3b4c39c..58a6d44a110f 100644 --- a/core/tests/Drupal/KernelTests/Core/File/HtaccessTest.php +++ b/core/tests/Drupal/KernelTests/Core/File/HtaccessTest.php @@ -2,7 +2,6 @@ namespace Drupal\KernelTests\Core\File; -use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Site\Settings; use Drupal\KernelTests\KernelTestBase; @@ -102,11 +101,11 @@ public function testHtaccessSave() { */ protected function assertFilePermissions(string $uri, int $expected): void { $actual = fileperms($uri) & 0777; - $this->assertSame($actual, $expected, new FormattableMarkup('@uri file permissions @actual are identical to @expected.', [ - '@uri' => $uri, - '@actual' => 0 . decoct($actual), - '@expected' => 0 . decoct($expected), - ])); + $this->assertSame($actual, $expected, sprintf('%s file permissions %s are identical to %s.', + $uri, + 0 . decoct($actual), + 0 . decoct($expected), + )); } } -- GitLab