You are here

public function RestfulCommentTestCase::testCreateComment in RESTful 7.2

Test creating a comment (POST method).

File

tests/RestfulCommentTestCase.test, line 55
Contains RestfulCommentTestCase

Class

RestfulCommentTestCase
@file Contains RestfulCommentTestCase

Code

public function testCreateComment() {
  $resource_manager = restful()
    ->getResourceManager();
  $this
    ->drupalLogin($this->account);
  $handler = $resource_manager
    ->getPlugin('comments:1.0');

  // @todo Remove "administer comments" when https://www.drupal.org/node/2236229 is fixed.
  $permissions = array(
    'post comments',
    'administer comments',
  );

  // Set a different user from the logged in user, to assert the comment's
  // author is set correctly.
  $user = $this
    ->drupalCreateUser($permissions);
  $handler
    ->setAccount($user);
  $label = $this
    ->randomName();
  $request = array(
    'label' => $label,
    'nid' => $this->node->nid,
  );
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->doPost($request), 'json'));
  $result = $result['data'];
  $comment = comment_load($result[0]['id']);
  $this
    ->assertEqual($comment->uid, $user->uid, 'Correct user was set to be the author of the comment.');
  $this
    ->assertEqual($comment->nid, $this->node->nid, 'Correct nid set.');
  $this
    ->assertEqual($comment->subject, $label, 'Correct subject set.');
}