diff --git a/core/modules/history/src/Tests/HistoryTest.php b/core/modules/history/src/Tests/HistoryTest.php index 17bba34..3f001b7 100644 --- a/core/modules/history/src/Tests/HistoryTest.php +++ b/core/modules/history/src/Tests/HistoryTest.php @@ -3,7 +3,10 @@ namespace Drupal\history\Tests; use Drupal\Component\Serialization\Json; +use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; +use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Serializer; /** * Tests the History endpoints. @@ -33,14 +36,31 @@ class HistoryTest extends WebTestBase { */ protected $testNode; + /** + * The Guzzle HTTP client. + * + * @var \GuzzleHttp\Client; + */ + protected $client; + + /** + * The serializer. + * + * @var \Symfony\Component\Serializer\Serializer + */ + protected $serializer; + protected function setUp() { parent::setUp(); $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); - $this->user = $this->drupalCreateUser(array('create page content', 'access content')); $this->drupalLogin($this->user); $this->testNode = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->user->id())); + $this->client = \Drupal::service('http_client_factory') + ->fromOptions(['config/curl' => [CURLOPT_TIMEOUT => 10]]); + $encoders = [new JsonEncoder()]; + $this->serializer = new Serializer([], $encoders); } /** @@ -49,35 +69,25 @@ 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); + $post['node_ids'] = $node_ids; // 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(); + $this->verbose($this->serializer->encode($post, 'json')); + return $this->client->post($url->toString(), [ + 'body' => $this->serializer->encode($post, 'json'), + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/x-www-form-urlencoded', + ] + ]); } /** @@ -90,24 +100,27 @@ protected function getNodeReadTimestamps(array $node_ids) { * 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, [ + 'headers' => array( + 'Accept' => 'application/json', ), - )); + 'auth' => [$this->user->getUsername(), $this->user->passRaw], + 'cookies' => $this->curlCookies, + ]); } /** * 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->assertEqual(200, $response->getStatusCode()); + $this->verbose($response->getBody()); + $json = $this->serializer->decode($response->getBody()->getContents(), 'json'); $this->assertIdentical(array(1 => 0), $json, 'The node has not yet been read.'); // View the node.