You are here

function ServicesResourceTaxonomyTests::testTermSelectNodes in Services 6.3

Same name and namespace in other branches
  1. 7.3 tests/functional/ServicesResourceTaxonomyTests.test \ServicesResourceTaxonomyTests::testTermSelectNodes()

Test taxonomy term selectNodes method.

File

tests/functional/ServicesResourceTaxonomyTests.test, line 380

Class

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

Code

function testTermSelectNodes() {
  $path = $this->endpoint->path;
  $vocabulary = array(
    'name' => $this
      ->randomName(),
    'description' => $this
      ->randomString(),
    'help' => $this
      ->randomString(),
    'relations' => 1,
    'hierarchy' => 1,
    'multiple' => 1,
    'required' => 0,
    'module' => 'services',
    'weight' => 0,
    'nodes' => array(
      'page' => 'page',
    ),
  );
  taxonomy_save_vocabulary($vocabulary);
  $vid = db_result(db_query('SELECT vid FROM {vocabulary} WHERE name = "%s"', $vocabulary['name']));
  $term1 = $this
    ->createTerm($vid);
  $term2 = $this
    ->createTerm($vid);
  $nodes = array();
  $nodes_term1 = array();
  $nodes_term2 = array();
  $nodes_term1_term2 = array();
  $nodes_noterm = array();

  // Create 7 page nodes with term1 attached.
  for ($i = 0; $i < 7; $i++) {
    $node = $this
      ->drupalCreateNode(array(
      'taxonomy' => array(
        $vid => array(
          $term1['tid'],
        ),
      ),
    ));
    $nodes[$node->nid] = $node;
    $nodes_term1[] = $node->nid;
  }

  // Create 7 page nodes with term2 attached.
  for ($i = 0; $i < 7; $i++) {
    $node = $this
      ->drupalCreateNode(array(
      'taxonomy' => array(
        $vid => array(
          $term2['tid'],
        ),
      ),
    ));
    $nodes[$node->nid] = $node;
    $nodes_term2[] = $node->nid;
  }

  // Create 7 page nodes with both term1 and term2 attached.
  for ($i = 0; $i < 7; $i++) {
    $node = $this
      ->drupalCreateNode(array(
      'taxonomy' => array(
        $vid => array(
          $term1['tid'],
          $term2['tid'],
        ),
      ),
    ));
    $nodes[$node->nid] = $node;
    $nodes_term1_term2[] = $node->nid;
  }

  // Create 7 page nodes without any terms.
  for ($i = 0; $i < 7; $i++) {
    $node = $this
      ->drupalCreateNode();
    $nodes[$node->nid] = $node;
    $nodes_noterm[] = $node->nid;
  }

  // If pager is FALSE query is limited by 'feed_default_items' variable.
  variable_set('feed_default_items', 100);

  // Select 14 nodes with term1 attached.
  $response = $this
    ->servicesPost($path . '/taxonomy_term/selectNodes', array(
    'tids' => $term1['tid'],
    'pager' => FALSE,
  ));
  $response_nodes = $this
    ->getNodeNids($response['body']);
  sort($response_nodes);
  $term1_nodes = array_merge($nodes_term1, $nodes_term1_term2);
  sort($term1_nodes);
  $this
    ->assertEqual($response_nodes, $term1_nodes, t('selectNodes selected proper nodes by one term.'), 'TaxonomyTermResource: selectNodes');

  // Ensure pager works.
  $response = $this
    ->servicesPost($path . '/taxonomy_term/selectNodes', array(
    'tids' => $term1['tid'],
    'pager' => TRUE,
  ));
  $this
    ->assertEqual(count($response['body']), 10, t('selectNodes pager works.'), 'TaxonomyTermResource: selectNodes');

  // Test 'or' operator.
  $response = $this
    ->servicesPost($path . '/taxonomy_term/selectNodes', array(
    'tids' => $term1['tid'] . ',' . $term2['tid'],
    'pager' => FALSE,
  ));
  $response_nodes = $this
    ->getNodeNids($response['body']);
  sort($response_nodes);
  $term1_or_term2_nodes = array_merge($nodes_term1, $nodes_term1_term2, $nodes_term2);
  sort($term1_or_term2_nodes);
  $this
    ->assertEqual($response_nodes, $term1_or_term2_nodes, t('selectNodes selected proper nodes "term1 or term2".'), 'TaxonomyTermResource: selectNodes');

  // Test 'and' operator and order.
  $response = $this
    ->servicesPost($path . '/taxonomy_term/selectNodes', array(
    'tids' => array(
      $term1['tid'],
      $term2['tid'],
    ),
    'pager' => FALSE,
    'operator' => 'and',
    'order' => 'n.nid ASC',
  ));
  $response_nodes = $this
    ->getNodeNids($response['body']);
  $this
    ->assertEqual($response_nodes, $nodes_term1_term2, t('selectNodes selected proper nodes "term1 and term2" and proper order.'), 'TaxonomyTermResource: selectNodes');
}