You are here

public function ServicesResourceNodetests::testNewEndpointResourceNodeIndex in Services 6.3

Same name and namespace in other branches
  1. 7.3 tests/functional/ServicesResourceNodeTests.test \ServicesResourceNodetests::testNewEndpointResourceNodeIndex()

testing node_resource Index

File

tests/functional/ServicesResourceNodeTests.test, line 53

Class

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

Code

public function testNewEndpointResourceNodeIndex() {

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

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

  // Get the content.
  $page_count = ceil(count($nodes) / 20);
  $retrieved_nodes = array();
  for ($page = 0; $page < $page_count; $page++) {
    $responseArray = $this
      ->servicesGet($this->endpoint->path . '/node', array(
      'page' => $page,
      'fields' => 'nid,title',
    ));
    $this
      ->assertTrue(count($responseArray['body']) <= 20, t('Correct number of items returned'));

    // Store the returned node IDs.
    foreach ($responseArray['body'] as $node) {
      if (isset($retrieved_nodes[$node->nid])) {
        $this
          ->fail(t('Duplicate node @nid returned.', array(
          '@nid' => $node->nid,
        )));
      }
      $retrieved_nodes[$node->nid] = TRUE;
      $this
        ->assertTrue($nodes[$node->nid]->title == $node->title, t('Successfully received Node info'), 'NodeResource: Index');
    }
  }

  // We should have got all the nodes.
  $expected_nids = array_keys($nodes);
  sort($expected_nids);
  $retrieved_nids = array_keys($retrieved_nodes);
  sort($retrieved_nids);
  $this
    ->assertEqual($expected_nids, $retrieved_nids, t('Retrieved all nodes'));

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

  // Adjust the pager size.
  $responseArray = $this
    ->servicesGet($this->endpoint->path . '/node', array(
    'fields' => 'nid,title',
    'pagesize' => 40,
  ));
  $this
    ->assertTrue(count($responseArray['body']) == 40, t('Correct number of items returned'));

  // Swap to user that can only use the default pager size.
  $this->lessPrivilegedUser = $this
    ->drupalCreateUser(array(
    'administer services',
  ));
  $this
    ->drupalLogin($this->lessPrivilegedUser);
  $responseArray = $this
    ->servicesGet($this->endpoint->path . '/node', array(
    'fields' => 'nid,title',
    'pagesize' => 40,
  ));
  $this
    ->assertTrue(count($responseArray['body']) == 20, t('Correct number of items returned'));
}