public function ServicesResourceTaxonomyTests::testTaxonomyTermIndex in Services 7.3
File
- tests/
functional/ ServicesResourceTaxonomyTests.test, line 90 - Call the endpoint tests when no authentication is being used.
Class
- ServicesResourceTaxonomyTests
- Taxonomy resource test class.
Code
public function testTaxonomyTermIndex() {
// Create and log in our privileged user.
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
));
$this
->drupalLogin($this->privilegedUser);
// Create a set of taxonomy terms. The taxonomy resource returns 20 terms at a time,
// so we create two pages and a half worth.
$terms = array();
$count = 50;
$vocabulary = $this
->createVocabulary();
for ($i = 0; $i < $count; $i++) {
$term = $this
->createTerm($vocabulary['vid']);
$terms[$term['tid']] = $term;
}
// Get the content.
$page_count = ceil(count($terms) / 20);
$retrieved_terms = array();
for ($page = 0; $page < $page_count; $page++) {
$responseArray = $this
->servicesGet($this->endpoint->path . '/taxonomy_term', array(
'page' => $page,
'fields' => 'tid,name',
));
$this
->assertTrue(count($responseArray['body']) <= 20, 'Correct number of items returned');
// Store the returned comment IDs.
foreach ($responseArray['body'] as $term) {
if (isset($retrieved_terms[$term->tid])) {
$this
->fail(format_string('Duplicate term @tid returned.', array(
'@tid' => $term->tid,
)));
}
$retrieved_terms[$term->tid] = TRUE;
$this
->assertTrue($terms[$term->tid]['name'] == $term->name, 'Successfully received Term Name info', 'TaxonomyTermResource: Index');
}
}
// We should have got all the comments.
$expected_tids = array_keys($terms);
sort($expected_tids);
$retrieved_tids = array_keys($retrieved_terms);
sort($retrieved_tids);
$this
->assertEqual($expected_tids, $retrieved_tids, 'Retrieved all terms');
// The n+1 page should be empty.
$responseArray = $this
->servicesGet($this->endpoint->path . '/taxonomy_term', array(
'page' => $page_count + 1,
));
$this
->assertEqual(count($responseArray['body']), 0, 'The n+1 page is empty');
taxonomy_vocabulary_delete($vocabulary['vid']);
}