From ebe942c757b47434eb57c0d68e5cdb0497b9d7c1 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 2 Jul 2024 09:30:57 +0100 Subject: [PATCH] Issue #3440169 by pooja_sharma, marcoliver, smustgrave, DYdave, alexpott: When using drupalGet(), provide an associative array for $headers --- .../LanguageBrowserDetectionAcceptLanguageTest.php | 2 +- core/modules/menu_ui/tests/src/Functional/MenuUiTest.php | 4 ++-- .../page_cache/tests/src/Functional/PageCacheTest.php | 2 +- .../src/Functional/Views/StyleSerializerEntityTest.php | 2 +- .../system/tests/src/Functional/Ajax/FrameworkTest.php | 2 +- .../src/Functional/DrupalKernel/ContentNegotiationTest.php | 2 +- .../system/tests/src/Functional/Form/StorageTest.php | 4 ++-- .../system/tests/src/Functional/Routing/RouterTest.php | 2 +- .../Session/StackSessionHandlerIntegrationTest.php | 2 +- .../toolbar/tests/src/Functional/ToolbarAdminMenuTest.php | 2 +- core/tests/Drupal/Tests/UiHelperTrait.php | 6 ++---- 11 files changed, 14 insertions(+), 16 deletions(-) diff --git a/core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php b/core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php index 25ea1a105424..a6d9d84b2d73 100644 --- a/core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php +++ b/core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php @@ -102,7 +102,7 @@ public function testAcceptLanguageEmptyDefault(): void { $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache'); // Check with browser without language settings - should return fallback language. - $this->drupalGet('/system-test/echo/language test', [], ['Accept-Language' => NULL]); + $this->drupalGet('/system-test/echo/language test', [], ['Accept-Language' => '']); $this->assertSession()->responseHeaderEquals('Content-Language', 'en'); $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache'); diff --git a/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php b/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php index c7cca3ed5d5f..becdbd90b4b8 100644 --- a/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php +++ b/core/modules/menu_ui/tests/src/Functional/MenuUiTest.php @@ -1059,13 +1059,13 @@ public function enableMenuLink(MenuLinkContent $item) { public function testMenuParentsJsAccess(): void { $this->drupalLogin($this->drupalCreateUser(['administer menu'])); // Just check access to the callback overall, the POST data is irrelevant. - $this->drupalGet('admin/structure/menu/parents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With: XMLHttpRequest']); + $this->drupalGet('admin/structure/menu/parents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With' => 'XMLHttpRequest']); $this->assertSession()->statusCodeEquals(200); // Log in as authenticated user. $this->drupalLogin($this->drupalCreateUser()); // Check that a simple user is not able to access the menu. - $this->drupalGet('admin/structure/menu/parents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With: XMLHttpRequest']); + $this->drupalGet('admin/structure/menu/parents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With' => 'XMLHttpRequest']); $this->assertSession()->statusCodeEquals(403); } diff --git a/core/modules/page_cache/tests/src/Functional/PageCacheTest.php b/core/modules/page_cache/tests/src/Functional/PageCacheTest.php index c26b5d76012c..6d12f1b09c85 100644 --- a/core/modules/page_cache/tests/src/Functional/PageCacheTest.php +++ b/core/modules/page_cache/tests/src/Functional/PageCacheTest.php @@ -194,7 +194,7 @@ public function testConditionalRequests(): void { $this->assertSession()->statusCodeEquals(304); // Ensure a conditional request without If-None-Match returns 200 OK. - $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => NULL]); + $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => '']); // Verify the page is not printed twice when the cache is warm. $this->assertSession()->responseNotMatches('#<html.*<html#'); $this->assertSession()->statusCodeEquals(200); diff --git a/core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php b/core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php index 2a4005a510b5..f00d1b15aced 100644 --- a/core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php +++ b/core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php @@ -338,7 +338,7 @@ public function testResponseFormatConfiguration(): void { $this->submitForm([], 'Save'); // Should return a 406. Emulates a sample Firefox header. - $this->drupalGet('test/serialize/field', [], ['Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']); + $this->drupalGet('test/serialize/field', [], ['Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']); $this->assertSession()->responseHeaderEquals('content-type', 'text/html; charset=UTF-8'); $this->assertSession()->statusCodeEquals(406); diff --git a/core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php b/core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php index 037e0de5115b..4e5695141b6a 100644 --- a/core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php +++ b/core/modules/system/tests/src/Functional/Ajax/FrameworkTest.php @@ -149,7 +149,7 @@ protected function assertCommand(array $haystack, array $needle): void { * Decoded JSON. */ protected function drupalGetAjax($path, array $options = [], array $headers = []) { - $headers[] = 'X-Requested-With: XMLHttpRequest'; + $headers = ['X-Requested-With' => 'XMLHttpRequest']; if (!isset($options['query'][MainContentViewSubscriber::WRAPPER_FORMAT])) { $options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax'; } diff --git a/core/modules/system/tests/src/Functional/DrupalKernel/ContentNegotiationTest.php b/core/modules/system/tests/src/Functional/DrupalKernel/ContentNegotiationTest.php index 3e49149c53f0..cb9806f8b093 100644 --- a/core/modules/system/tests/src/Functional/DrupalKernel/ContentNegotiationTest.php +++ b/core/modules/system/tests/src/Functional/DrupalKernel/ContentNegotiationTest.php @@ -45,7 +45,7 @@ public function testBogusAcceptHeader(): void { 'Android #2 (2012)' => 'text/xml,text/html,application/xhtml+xml,image/png,text/plain,*/*;q=0.8', ]; foreach ($tests as $case => $header) { - $this->drupalGet('', [], ['Accept: ' . $header]); + $this->drupalGet('', [], ['Accept' => $header]); $this->assertSession()->pageTextNotContains('Unsupported Media Type'); $this->assertSession()->pageTextContains('Log in'); } diff --git a/core/modules/system/tests/src/Functional/Form/StorageTest.php b/core/modules/system/tests/src/Functional/Form/StorageTest.php index 9b3997c2b23c..dcc7ffc594b4 100644 --- a/core/modules/system/tests/src/Functional/Form/StorageTest.php +++ b/core/modules/system/tests/src/Functional/Form/StorageTest.php @@ -191,7 +191,7 @@ public function testImmutableFormLegacyProtection(): void { $build_id = $this->assertSession()->hiddenFieldExists('form_build_id')->getValue(); // Try to poison the form cache. - $response = $this->drupalGet('form-test/form-storage-legacy/' . $build_id, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With: XMLHttpRequest']); + $response = $this->drupalGet('form-test/form-storage-legacy/' . $build_id, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With' => 'XMLHttpRequest']); $original = json_decode($response, TRUE); $this->assertEquals($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded'); @@ -208,7 +208,7 @@ public function testImmutableFormLegacyProtection(): void { $this->assertTrue($status, 'A watchdog message was logged by \Drupal::formBuilder()->setCache'); // Ensure that the form state was not poisoned by the preceding call. - $response = $this->drupalGet('form-test/form-storage-legacy/' . $build_id, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With: XMLHttpRequest']); + $response = $this->drupalGet('form-test/form-storage-legacy/' . $build_id, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With' => 'XMLHttpRequest']); $original = json_decode($response, TRUE); $this->assertEquals($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded'); $this->assertNotEquals($original['form']['#build_id'], $build_id, 'New build_id was generated'); diff --git a/core/modules/system/tests/src/Functional/Routing/RouterTest.php b/core/modules/system/tests/src/Functional/Routing/RouterTest.php index ead1cd8956ac..5208c73b873c 100644 --- a/core/modules/system/tests/src/Functional/Routing/RouterTest.php +++ b/core/modules/system/tests/src/Functional/Routing/RouterTest.php @@ -298,7 +298,7 @@ public function testControllerResolutionAjax(): void { // This will fail with a JSON parse error if the request is not routed to // The correct controller. $options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax'; - $headers[] = 'X-Requested-With: XMLHttpRequest'; + $headers = ['X-Requested-With' => 'XMLHttpRequest']; $this->drupalGet('/router_test/test10', $options, $headers); $this->assertSession()->responseHeaderEquals('Content-Type', 'application/json'); diff --git a/core/modules/system/tests/src/Functional/Session/StackSessionHandlerIntegrationTest.php b/core/modules/system/tests/src/Functional/Session/StackSessionHandlerIntegrationTest.php index 61cb40d64a4c..0f3856140cf9 100644 --- a/core/modules/system/tests/src/Functional/Session/StackSessionHandlerIntegrationTest.php +++ b/core/modules/system/tests/src/Functional/Session/StackSessionHandlerIntegrationTest.php @@ -29,7 +29,7 @@ class StackSessionHandlerIntegrationTest extends BrowserTestBase { */ public function testRequest(): void { $options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax'; - $headers[] = 'X-Requested-With: XMLHttpRequest'; + $headers = ['X-Requested-With' => 'XMLHttpRequest']; $actual_trace = json_decode($this->drupalGet('session-test/trace-handler', $options, $headers)); $sessionId = $this->getSessionCookies()->getCookieByName($this->getSessionName())->getValue(); $expect_trace = [ diff --git a/core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php b/core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php index 39faebd2bb9f..7565eeb83c4d 100644 --- a/core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php +++ b/core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php @@ -385,7 +385,7 @@ public function testSubtreesJsonRequest(): void { // Request a new page to refresh the drupalSettings object. $subtrees_hash = $this->getSubtreesHash(); - $this->drupalGet('toolbar/subtrees/' . $subtrees_hash, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With: XMLHttpRequest']); + $this->drupalGet('toolbar/subtrees/' . $subtrees_hash, ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With' => 'XMLHttpRequest']); $ajax_result = json_decode($this->getSession()->getPage()->getContent(), TRUE); $this->assertEquals('setToolbarSubtrees', $ajax_result[0]['command'], 'Subtrees response uses the correct command.'); $this->assertEquals(['system-admin_content', 'system-admin_structure', 'system-themes_page', 'system-modules_list', 'system-admin_config', 'entity-user-collection', 'front'], array_keys($ajax_result[0]['subtrees']), 'Correct subtrees returned.'); diff --git a/core/tests/Drupal/Tests/UiHelperTrait.php b/core/tests/Drupal/Tests/UiHelperTrait.php index 5e6e9176b100..a1ea81aec367 100644 --- a/core/tests/Drupal/Tests/UiHelperTrait.php +++ b/core/tests/Drupal/Tests/UiHelperTrait.php @@ -245,13 +245,11 @@ protected function drupalGet($path, array $options = [], array $headers = []) { $this->prepareRequest(); foreach ($headers as $header_name => $header_value) { if (is_int($header_name)) { - // @todo Trigger deprecation in - // https://www.drupal.org/project/drupal/issues/3421105. + @trigger_error('Passing an integer as header name to ' . __METHOD__ . '() is deprecated in drupal:11.1.0 and will be removed from drupal:12.0.0. Update the calling code to pass the header name as a key. See https://www.drupal.org/node/3456178', E_USER_DEPRECATED); [$header_name, $header_value] = explode(':', $header_value); } if (is_null($header_value)) { - // @todo Trigger deprecation in - // https://www.drupal.org/project/drupal/issues/3421105. + @trigger_error('Using null as a header value to ' . __METHOD__ . '() is deprecated in drupal:11.1.0 and will be removed from drupal:12.0.0. Use an empty string instead. See https://www.drupal.org/node/3456233', E_USER_DEPRECATED); $header_value = ''; } $session->setRequestHeader($header_name, $header_value); -- GitLab