diff --git a/core/modules/history/src/Tests/HistoryTest.php b/core/modules/history/tests/src/Functional/HistoryTest.php similarity index 52% rename from core/modules/history/src/Tests/HistoryTest.php rename to core/modules/history/tests/src/Functional/HistoryTest.php index 17bba34..74d3628 100644 --- a/core/modules/history/src/Tests/HistoryTest.php +++ b/core/modules/history/tests/src/Functional/HistoryTest.php @@ -1,16 +1,21 @@ user = $this->drupalCreateUser(array('create page content', 'access content')); $this->drupalLogin($this->user); $this->testNode = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->user->id())); + + $domain = parse_url($this->getUrl(), PHP_URL_HOST); + $session_id = $this->getSession()->getCookie($this->getSessionName()); + $this->cookies = CookieJar::fromArray([$this->getSessionName() => $session_id], $domain); + $this->client = $this->getSession()->getDriver()->getClient()->getClient(); } /** @@ -49,35 +73,23 @@ protected function setUp() { * @param array $node_ids * An array of node IDs. * - * @return string - * The response body. + * @return \Psr\Http\Message\ResponseInterface + * The response object. */ protected function getNodeReadTimestamps(array $node_ids) { - // Build POST values. - $post = array(); - for ($i = 0; $i < count($node_ids); $i++) { - $post['node_ids[' . $i . ']'] = $node_ids[$i]; - } - - // Serialize POST values. - foreach ($post as $key => $value) { - // Encode according to application/x-www-form-urlencoded - // Both names and values needs to be urlencoded, according to - // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1 - $post[$key] = urlencode($key) . '=' . urlencode($value); - } - $post = implode('&', $post); - // Perform HTTP request. - return $this->curlExec(array( - CURLOPT_URL => \Drupal::url('history.get_last_node_view', array(), array('absolute' => TRUE)), - CURLOPT_POST => TRUE, - CURLOPT_POSTFIELDS => $post, - CURLOPT_HTTPHEADER => array( - 'Accept: application/json', - 'Content-Type: application/x-www-form-urlencoded', - ), - )); + $url = Url::fromRoute('history.get_last_node_view') + ->setAbsolute() + ->toString(); + return $this->client->post($url, [ + 'body' => http_build_query(['node_ids' => $node_ids]), + 'cookies' => $this->cookies, + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + 'http_errors' => FALSE, + ]); } /** @@ -86,28 +98,30 @@ protected function getNodeReadTimestamps(array $node_ids) { * @param int $node_id * A node ID. * - * @return string + * @return \Psr\Http\Message\ResponseInterface * The response body. */ protected function markNodeAsRead($node_id) { - return $this->curlExec(array( - CURLOPT_URL => \Drupal::url('history.read_node', array('node' => $node_id), array('absolute' => TRUE)), - CURLOPT_HTTPHEADER => array( - 'Accept: application/json', - ), - )); + $url = Url::fromRoute('history.read_node', array('node' => $node_id), array('absolute' => TRUE))->toString(); + return $this->client->post($url, [ + 'cookies' => $this->cookies, + 'headers' => [ + 'Accept' => 'application/json', + ], + 'http_errors' => FALSE, + ]); } /** * Verifies that the history endpoints work. */ - function testHistory() { + public function testHistory() { $nid = $this->testNode->id(); // Retrieve "last read" timestamp for test node, for the current user. $response = $this->getNodeReadTimestamps(array($nid)); - $this->assertResponse(200); - $json = Json::decode($response); + $this->assertEquals(200, $response->getStatusCode()); + $json = Json::decode($response->getBody()); $this->assertIdentical(array(1 => 0), $json, 'The node has not yet been read.'); // View the node. @@ -121,28 +135,28 @@ function testHistory() { // Simulate JavaScript: perform HTTP request to mark node as read. $response = $this->markNodeAsRead($nid); - $this->assertResponse(200); - $timestamp = Json::decode($response); + $this->assertEquals(200, $response->getStatusCode()); + $timestamp = Json::decode($response->getBody()); $this->assertTrue(is_numeric($timestamp), 'Node has been marked as read. Timestamp received.'); // Retrieve "last read" timestamp for test node, for the current user. $response = $this->getNodeReadTimestamps(array($nid)); - $this->assertResponse(200); - $json = Json::decode($response); + $this->assertEquals(200, $response->getStatusCode()); + $json = Json::decode($response->getBody()); $this->assertIdentical(array(1 => $timestamp), $json, 'The node has been read.'); // Failing to specify node IDs for the first endpoint should return a 404. - $this->getNodeReadTimestamps(array()); - $this->assertResponse(404); + $response = $this->getNodeReadTimestamps(array()); + $this->assertEquals(404, $response->getStatusCode()); // Accessing either endpoint as the anonymous user should return a 403. $this->drupalLogout(); - $this->getNodeReadTimestamps(array($nid)); - $this->assertResponse(403); - $this->getNodeReadTimestamps(array()); - $this->assertResponse(403); - $this->markNodeAsRead($nid); - $this->assertResponse(403); + $response = $this->getNodeReadTimestamps(array($nid)); + $this->assertEquals(403, $response->getStatusCode()); + $response = $this->getNodeReadTimestamps(array()); + $this->assertEquals(403, $response->getStatusCode()); + $response = $this->markNodeAsRead($nid); + $this->assertEquals(403, $response->getStatusCode()); } }