diff --git a/core/modules/contextual/src/Tests/ContextualDynamicContextTest.php b/core/modules/contextual/tests/src/Functional/ContextualDynamicContextTest.php similarity index 75% rename from core/modules/contextual/src/Tests/ContextualDynamicContextTest.php rename to core/modules/contextual/tests/src/Functional/ContextualDynamicContextTest.php index 00ade5a..52ca648 100644 --- a/core/modules/contextual/src/Tests/ContextualDynamicContextTest.php +++ b/core/modules/contextual/tests/src/Functional/ContextualDynamicContextTest.php @@ -1,12 +1,13 @@ editorUser = $this->drupalCreateUser(['access content', 'access contextual links', 'edit any article content']); $this->authenticatedUser = $this->drupalCreateUser(['access content', 'access contextual links']); $this->anonymousUser = $this->drupalCreateUser(['access content']); + $this->client = \Drupal::httpClient(); } /** @@ -89,12 +98,12 @@ public function testDifferentPermissions() { for ($i = 0; $i < count($ids); $i++) { $this->assertContextualLinkPlaceHolder($ids[$i]); } - $this->renderContextualLinks([], 'node'); - $this->assertResponse(400); - $this->assertRaw('No contextual ids specified.'); + $response = $this->renderContextualLinks([], 'node'); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertContains('No contextual ids specified.', (string) $response->getBody()); $response = $this->renderContextualLinks($ids, 'node'); - $this->assertResponse(200); - $json = Json::decode($response); + $this->assertEquals(200, $response->getStatusCode()); + $json = Json::decode((string) $response->getBody()); $this->assertIdentical($json[$ids[0]], ''); $this->assertIdentical($json[$ids[1]], ''); $this->assertIdentical($json[$ids[2]], ''); @@ -112,12 +121,12 @@ public function testDifferentPermissions() { for ($i = 0; $i < count($ids); $i++) { $this->assertContextualLinkPlaceHolder($ids[$i]); } - $this->renderContextualLinks([], 'node'); - $this->assertResponse(400); - $this->assertRaw('No contextual ids specified.'); + $response = $this->renderContextualLinks([], 'node'); + $this->assertEquals(400, $response->getStatusCode()); + $this->assertContains('No contextual ids specified.', (string) $response->getBody()); $response = $this->renderContextualLinks($ids, 'node'); - $this->assertResponse(200); - $json = Json::decode($response); + $this->assertEquals(200, $response->getStatusCode()); + $json = Json::decode((string) $response->getBody()); $this->assertIdentical($json[$ids[0]], ''); $this->assertIdentical($json[$ids[1]], ''); $this->assertIdentical($json[$ids[2]], ''); @@ -129,10 +138,10 @@ public function testDifferentPermissions() { for ($i = 0; $i < count($ids); $i++) { $this->assertNoContextualLinkPlaceHolder($ids[$i]); } - $this->renderContextualLinks([], 'node'); - $this->assertResponse(403); - $this->renderContextualLinks($ids, 'node'); - $this->assertResponse(403); + $response = $this->renderContextualLinks([], 'node'); + $this->assertEquals(403, $response->getStatusCode()); + $response = $this->renderContextualLinks($ids, 'node'); + $this->assertEquals(403, $response->getStatusCode()); // Get a page where contextual links are directly rendered. $this->drupalGet(Url::fromRoute('menu_test.contextual_test')); @@ -174,15 +183,38 @@ protected function assertNoContextualLinkPlaceHolder($id) { * @param string $current_path * The Drupal path for the page for which the contextual links are rendered. * - * @return string - * The response body. + * @return \Psr\Http\Message\ResponseInterface + * The response object. */ protected function renderContextualLinks($ids, $current_path) { $post = []; for ($i = 0; $i < count($ids); $i++) { - $post['ids[' . $i . ']'] = $ids[$i]; + $post[] = urlencode('ids[' . $i . ']') . '=' . urlencode($ids[$i]); } - return $this->drupalPostWithFormat('contextual/render', 'json', $post, ['query' => ['destination' => $current_path]]); + $url = Url::fromRoute('contextual.render') + ->setOptions(['query' => ['_format' => 'json', 'destination' => $current_path]]) + ->setAbsolute() + ->toString(); + + return $this->client->post($url, [ + 'http_errors' => FALSE, + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + 'cookies' => $this->getCookies(), + 'body' => implode('&', $post), + ]); + } + + /** + * Obtain the cookie jar. + * + * @return \GuzzleHttp\Cookie\CookieJar + */ + protected function getCookies() { + $domain = parse_url($this->getUrl(), PHP_URL_HOST); + $session_id = $this->getSession()->getCookie($this->getSessionName()); + return CookieJar::fromArray([$this->getSessionName() => $session_id], $domain); } }