You are here

public function ServicesResourceCommentTests::testCommentIndex in Services 7.3

File

tests/functional/ServicesResourceCommentTests.test, line 47
Call the endpoint tests when no authentication is being used.

Class

ServicesResourceCommentTests
Run test cases for the endpoint with no authentication turned on.

Code

public function testCommentIndex() {

  // Create and log in our privileged user.
  $this->privilegedUser = $this
    ->drupalCreateUser(array(
    'administer services',
    'administer comments',
  ));
  $this
    ->drupalLogin($this->privilegedUser);

  // Create a set of comments. The comment resource returns 20 comments at a time,
  // so we create two pages and a half worth.
  $comments = array();
  $count = 50;
  $node = $this
    ->drupalCreateNode();
  $nid = $node->nid;
  for ($i = 0; $i < $count; $i++) {
    $comment = (object) $this
      ->getCommentValues($nid);
    $comment->created = REQUEST_TIME + $i;
    comment_save($comment);
    $comments[$comment->cid] = $comment;
  }

  // Get the content.
  $page_count = ceil(count($comments) / 20);
  $retrieved_comments = array();
  for ($page = 0; $page < $page_count; $page++) {
    $responseArray = $this
      ->servicesGet($this->endpoint->path . '/comment', array(
      'page' => $page,
      'fields' => 'cid,subject',
    ));
    $this
      ->assertTrue(count($responseArray['body']) <= 20, 'Correct number of items returned');

    // Store the returned comment IDs.
    foreach ($responseArray['body'] as $comment) {
      if (isset($retrieved_comments[$comment->cid])) {
        $this
          ->fail(format_string('Duplicate comment @cid returned.', array(
          '@cid' => $comment->cid,
        )));
      }
      $retrieved_comments[$comment->cid] = TRUE;
      $this
        ->assertTrue($comments[$comment->cid]->subject == $comment->subject, 'Successfully received Comment info', 'CommentResource: Index');
    }
  }

  // We should have got all the comments.
  $expected_cids = array_keys($comments);
  sort($expected_cids);
  $retrieved_cids = array_keys($retrieved_comments);
  sort($retrieved_cids);
  $this
    ->assertEqual($expected_cids, $retrieved_cids, 'Retrieved all comments');

  // The n+1 page should be empty.
  $responseArray = $this
    ->servicesGet($this->endpoint->path . '/comment', array(
    'page' => $page_count + 1,
  ));
  $this
    ->assertEqual(count($responseArray['body']), 0, 'The n+1 page is empty');
}