You are here

public function ServicesResourceTaxonomyTests::testTaxonomyVocabularyIndex in Services 7.3

File

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

Class

ServicesResourceTaxonomyTests
Taxonomy resource test class.

Code

public function testTaxonomyVocabularyIndex() {

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

  // Create a set of taxonomy vocabularys. The taxonomy resource returns 20 vocabularys at a time,
  // so we create two pages and a half worth.
  $vocabularys = array();
  $count = 50;
  for ($i = 0; $i < $count; $i++) {
    $vocabulary = $this
      ->createVocabulary();
    $vocabularys[$vocabulary['vid']] = $vocabulary;
  }
  $vocabulary = taxonomy_vocabulary_load(1);
  $vocabularys[1] = (array) $vocabulary;

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

    // Store the returned comment IDs.
    foreach ($responseArray['body'] as $vocabulary) {
      if (isset($retrieved_vocabularys[$vocabulary->vid])) {
        $this
          ->fail(format_string('Duplicate vocabulary @vid returned.', array(
          '@vid' => $vocabulary->vid,
        )));
      }
      $retrieved_vocabularys[$vocabulary->vid] = TRUE;
      $this
        ->assertTrue($vocabularys[$vocabulary->vid]['name'] == $vocabulary->name, 'Successfully received vocabulary Name info', 'TaxonomyVocabularyResource: Index');
    }
  }

  // We should have got all the comments.
  $expected_vids = array_keys($vocabularys);
  sort($expected_vids);
  $retrieved_vids = array_keys($retrieved_vocabularys);
  sort($retrieved_vids);
  $this
    ->assertEqual($expected_vids, $retrieved_vids, 'Retrieved all vocabularys');

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