You are here

function ContentTaxonomyAutocompleteTest::testAutocomplete in Content Taxonomy 6

Same name and namespace in other branches
  1. 6.2 tests/content_taxonomy.test \ContentTaxonomyAutocompleteTest::testAutocomplete()

File

tests/content_taxonomy.test, line 372

Class

ContentTaxonomyAutocompleteTest
Test Cases for Content Taxonomy Autocomplete

Code

function testAutocomplete() {
  $type = $this->content_types[1];
  $type_url = str_replace('_', '-', $type->type);
  $terms = $this
    ->createTerms(4);

  //single field
  $settings = array(
    'type' => 'content_taxonomy',
    'widget_type' => 'content_taxonomy_autocomplete',
    'vid' => $terms[0]->vid,
    'parent' => 0,
    'parent_php_code' => '',
    'show_depth' => 0,
    'save_term_node' => FALSE,
    'depth' => NULL,
    'hide_taxonomy_fields' => TRUE,
  );
  $field = $this
    ->createField($settings, 1);
  $field_name = $field['field_name'];

  // Create a node with one value
  $edit = array();
  $edit['title'] = $this
    ->randomName(20);
  $edit['body'] = $this
    ->randomName(20);
  $edit[$field_name . '[value]'] = $terms[0]->name;
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $node = node_load(array(
    'title' => $edit['title'],
  ));
  $this
    ->assertEqual($node->{$field_name}[0]['value'], $terms[0]->tid, 'Terms saved');
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[0]->name, 'Terms displayed');
  $this
    ->assertNoText($terms[1]->name, 'Term not displayed');

  //Edit the node and select a different value
  $edit = array();
  $edit[$field_name . '[value]'] = $terms[1]->name;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertIdentical($node->{$field_name}[0]['value'], $terms[1]->tid, 'Terms updated');
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[1]->name, 'Terms displayed');
  $this
    ->assertNoText($terms[0]->name, 'Term not displayed');

  //Edit the node and select 2 values for single field
  $edit = array();
  $edit[$field_name . '[value]'] = $terms[1]->name . ", " . $terms[0]->name;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
  $this
    ->assertText('You can provide only one value', 'Validated');
  $edit[$field_name . '[value]'] = $terms[1]->name;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertIdentical($node->{$field_name}[0]['value'], $terms[1]->tid, 'Terms updated');
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[1]->name, 'Terms displayed');
  $this
    ->assertNoText($terms[0]->name, 'Term not displayed');

  //Add a new term
  $edit = array();
  $new_term_name = 'test';
  $edit['title'] = $this
    ->randomName(20);
  $edit['body'] = $this
    ->randomName(20);
  $edit[$field_name . '[value]'] = $new_term_name;
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $new_term_tid = db_result(db_query("SELECT tid FROM {term_data} WHERE name = '%s' AND vid = %d", $new_term_name, $settings['vid']));
  $this
    ->assertTrue($new_term_tid > 0, "New term added to vocabulary");
  $node = node_load(array(
    'title' => $edit['title'],
  ));
  $this
    ->assertEqual($node->{$field_name}[0]['value'], $new_term_tid, 'Terms saved');
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($new_term_name, 'Terms displayed');

  //test Multi Field
  $type = $this->content_types[0];
  $type_url = str_replace('_', '-', $type->type);

  //multi field
  $settings = array(
    'type' => 'content_taxonomy',
    'widget_type' => 'content_taxonomy_autocomplete',
    'vid' => $terms[0]->vid,
    'parent' => 0,
    'parent_php_code' => '',
    'show_depth' => 0,
    'save_term_node' => FALSE,
    'depth' => NULL,
    'hide_taxonomy_fields' => TRUE,
    'multiple' => TRUE,
  );
  $field = $this
    ->createField($settings, 0);
  $field_name = $field['field_name'];

  // Create a node with one value
  $edit = array();
  $edit['title'] = $this
    ->randomName(20);
  $edit['body'] = $this
    ->randomName(20);
  $edit[$field_name . '[value]'] = $terms[0]->name;
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $node = node_load(array(
    'title' => $edit['title'],
  ));
  $this
    ->assertNodeMultiValues($node, $field_name, array(
    $terms[0],
  ));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[0]->name, 'Terms displayed');
  $this
    ->assertNoText($terms[1]->name, 'Term not displayed');

  //Edit the node and select a different value
  $edit = array();
  $edit[$field_name . '[value]'] = $terms[1]->name;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertNodeMultiValues($node, $field_name, array(
    $terms[1],
  ), array(
    $terms[0],
  ));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[1]->name, 'Terms displayed');
  $this
    ->assertNoText($terms[0]->name, 'Term not displayed');

  //Edit the node and select a second value
  $edit = array();
  $edit[$field_name . '[value]'] = $terms[1]->name . ", " . $terms[0]->name;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertNodeMultiValues($node, $field_name, array(
    $terms[0],
    $terms[1],
  ));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[1]->name, 'Terms displayed');
  $this
    ->assertText($terms[0]->name, 'Terms displayed');

  // Create a node with one value and test preview
  $edit = array();
  $edit['title'] = $this
    ->randomName(20);
  $edit['body'] = $this
    ->randomName(20);
  $edit[$field_name . '[value]'] = $terms[0]->name;
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Preview');
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $node = node_load(array(
    'title' => $edit['title'],
  ));
  $this
    ->assertNodeMultiValues($node, $field_name, array(
    $terms[0],
  ));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[0]->name, 'Terms displayed');

  // Create a node with one value and test preview with a new term
  $new_term_name = 'test2';
  $edit = array();
  $edit['title'] = $this
    ->randomName(20);
  $edit['body'] = $this
    ->randomName(20);
  $edit[$field_name . '[value]'] = $new_term_name;
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Preview');
  $new_term_tid = db_result(db_query("SELECT tid FROM {term_data} WHERE name = '%s' AND vid = %d", $new_term_name, $settings['vid']));
  $this
    ->assertTrue($new_term_tid > 0, "Term in added to vocabulary");
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $node = node_load(array(
    'title' => $edit['title'],
  ));
  $this
    ->assertNodeMultiValues($node, $field_name, array(
    taxonomy_get_term($new_term_tid),
  ));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($new_term_name, 'Terms displayed');

  // Create a node with one value and test preview diff
  $edit = array();
  $edit['title'] = $this
    ->randomName(20);
  $edit['body'] = $this
    ->randomName(20);
  $edit[$field_name . '[value]'] = $terms[0]->name;
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $node = node_load(array(
    'title' => $edit['title'],
  ));
  $edit = array();
  $edit['title'] = $node->title;
  $edit['body'] = str_replace('<!--break-->', '', $node->body);
  $edit[$field_name . '[value]'] = $terms[0]->name;
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, 'Preview changes');
  $this
    ->assertText('No visible changes', 'No visible changes');
  $this
    ->assertRaw($terms[0]->name, 'Term in field');

  /* $this->drupalPost('node/'. $node->nid .'/edit', $edit, 'Save');
     $node = node_load($node->nid, NULL, TRUE);
     $this->assertNodeMultiValues($node, $field_name, array($terms[0]));
     $this->drupalGet('node/'. $node->nid);
     $this->assertText($terms[0]->name, 'Terms displayed');*/

  //CREATE NEW REQUIRED FIELD
  $settings['required'] = TRUE;
  $field = $this
    ->createField($settings, 0);
  $field_name = $field['field_name'];
  $edit = array();
  $edit['title'] = $this
    ->randomName(20);
  $edit['body'] = $this
    ->randomName(20);
  $edit[$field_name . '[value]'] = '';
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $this
    ->assertText($field_name . ' field is required', 'Validated required field');
  $edit[$field_name . '[value]'] = $terms[1]->name;
  $this
    ->drupalPost('node/add/' . $type_url, $edit, 'Save');
  $this
    ->assertNoText($field_name . ' field is required', 'Validation for required field successfully passed');
  $node = node_load(array(
    'title' => $edit['title'],
  ));
  $this
    ->assertNodeMultiValues($node, $field_name, array(
    $terms[1],
  ));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($terms[1]->name, 'Terms displayed');
}