You are here

function ServicesResourceTaxonomyTests::testTermSelectNodes in Services 7.3

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

Test taxonomy term selectNodes method.

File

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

Class

ServicesResourceTaxonomyTests
Taxonomy resource test class.

Code

function testTermSelectNodes() {
  $path = $this->endpoint->path;
  $vocabulary = (object) array(
    'name' => $this
      ->randomName(),
    'machine_name' => 'text_vocabulary',
    'description' => $this
      ->randomString(),
    'help' => $this
      ->randomString(),
    'relations' => 1,
    'hierarchy' => 1,
    'multiple' => 1,
    'required' => 0,
    'module' => 'services',
    'weight' => 0,
    'nodes' => array(
      'page' => 'page',
    ),
  );
  taxonomy_vocabulary_save($vocabulary);
  $query = db_select('taxonomy_vocabulary', 'v')
    ->fields('v', array(
    'vid',
  ))
    ->condition('v.name', $vocabulary->name);
  $vid = $query
    ->execute()
    ->fetchField();
  $term1 = $this
    ->createTerm($vid);
  $term2 = $this
    ->createTerm($vid);
  $nodes = array();
  $nodes_term1 = array();
  $nodes_term2 = array();
  $nodes_term1_term2 = array();
  $nodes_noterm = array();
  $field_name = 'taxonomy_' . $vocabulary->machine_name;

  // Create field for term.
  $field = array(
    'field_name' => $field_name,
    'type' => 'taxonomy_term_reference',
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'label' => $vocabulary->name,
    'bundle' => 'page',
    'required' => TRUE,
    'widget' => array(
      'type' => 'options_select',
    ),
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link',
        'weight' => 10,
      ),
      'teaser' => array(
        'type' => 'taxonomy_term_reference_link',
        'weight' => 10,
      ),
    ),
  );
  field_create_instance($instance);
  node_types_rebuild();

  // Create 7 page nodes with term1 attached.
  for ($i = 0; $i < 7; $i++) {
    $node = $this
      ->drupalCreateNode(array(
      $field_name => array(
        LANGUAGE_NONE => array(
          array(
            'tid' => $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(
      $field_name => array(
        LANGUAGE_NONE => array(
          array(
            'tid' => $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(
      $field_name => array(
        LANGUAGE_NONE => array(
          array(
            'tid' => $term1['tid'],
          ),
          array(
            '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(
    'tid' => $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, 'selectNodes selected proper nodes by one term.', 'TaxonomyTermResource: selectNodes');

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

  // AND or OR tests are not applicable as taxonomy_select_nodes() does not accept operators.
}