You are here

function TaxonomyTestNodeApi::testTaxonomyNode in SimpleTest 6

Same name and namespace in other branches
  1. 5 tests/taxonomy.module.test \TaxonomyTestNodeApi::testTaxonomyNode()

File

tests/taxonomy.module.test, line 241

Class

TaxonomyTestNodeApi

Code

function testTaxonomyNode() {

  //preparing data

  // vocabulary hierarchy->single, multiple -> on
  $edit = array();
  $_t = array(
    'vid',
    'name',
    'description',
    'help',
    'relations',
    'hierarchy',
    'multiple',
    'required',
    'tags',
    'module',
    'weight',
    'nodes',
  );
  foreach ($_t as $key) {
    $edit[$key] = 0;
  }
  $name = $this
    ->randomName(20);
  $edit['hierarchy'] = 1;
  $edit['multiple'] = 1;
  $edit['name'] = $name;
  $edit['nodes'] = array(
    'story' => 'story',
  );
  taxonomy_save_vocabulary($edit);
  $vid = $edit['vid'];

  // we need to persist vid after $edit is unset()
  $parent = array();
  $patternArray = array();

  // create 1st term
  $termname = $this
    ->randomName(20);
  $data = array(
    'name' => $termname,
    'description' => '',
    'weight' => 0,
    'synonyms' => '',
    'vid' => $vid,
    'tid' => 0,
    'relations' => 0,
  );
  taxonomy_save_term($data);
  $_tArray = taxonomy_get_term_by_name($termname);
  $parent[$_tArray[0]->tid] = $_tArray[0]->tid;
  $patternArray['term name 1'] = $termname;

  // create 2nd term
  $termname = $this
    ->randomName(20);
  $data = array(
    'name' => $termname,
    'description' => '',
    'weight' => 0,
    'synonyms' => '',
    'vid' => $vid,
    'tid' => 0,
    'relations' => 0,
  );
  taxonomy_save_term($data);
  $_tArray = taxonomy_get_term_by_name($termname);
  $parent[$_tArray[0]->tid] = $_tArray[0]->tid;
  $patternArray['term name 2'] = $termname;

  // create test user and login
  $perm = array(
    'access content',
    'create story content',
    'edit own story content',
    'delete own story content',
  );
  $account = $this
    ->drupalCreateUserRolePerm($perm);
  $this
    ->drupalLoginUser($account);

  // why is this printing out the user profile page?
  // go to node/add/story
  $this
    ->drupalGet('node/add/story');
  $req = $this->_browser
    ->getRequest();
  $headers = $this->_browser
    ->getHeaders();
  $content = $this
    ->drupalGetContent();

  //    print($content). "\n\n\n all done \n\n";
  // try to create story
  $title = $this
    ->randomName();
  $body = $this
    ->randomName(100);
  $edit = array(
    'title' => $title,
    'body' => $body,
    'taxonomy-' . $vid => $parent,
  );

  // multiple slect box was failing through drupalPost. Use raw POST instead
  // Failing because they were being sent/handled wrong (earnest.berry@gmail.com <Souvent22>)
  $action = url('node/add/story', array(
    'absolute' => TRUE,
  ));
  $this
    ->drupalPost($action, $edit, 'Save');
  $content = $this
    ->drupalGetContent();
  $patternArray['body text'] = $body;
  $patternArray['title'] = $title;

  //    $node = array2object(node_load(array('title' => $title)));
  $node = node_load(array(
    'title' => $title,
  ));
  $this->_browser
    ->get(url("node/{$node->nid}"));
  foreach ($patternArray as $name => $termPattern) {
    $this
      ->assertText($termPattern, "Checking {$name}");
  }

  // checking database fields
  $result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $node->nid);
  while ($nodeRow = db_fetch_array($result)) {
    $this
      ->assertTrue(in_array($nodeRow['tid'], $parent), 'Checking database record');
  }

  // ok, lets create new terms, and change this node

  //pop array
  array_pop($parent);

  // create 1st term
  $termname = $this
    ->randomName(20);
  $data = array(
    'name' => $termname,
    'description' => '',
    'weight' => 0,
    'synonyms' => '',
    'vid' => $vid,
    'tid' => 0,
    'relations' => 0,
  );
  taxonomy_save_term($data);
  $_tArray = taxonomy_get_term_by_name($termname);
  $parent[] = $_tArray[0]->tid;
  $patternArray['term name 2'] = $termname;

  // create 2nd term
  $termname = $this
    ->randomName(20);
  $data = array(
    'name' => $termname,
    'description' => '',
    'weight' => 0,
    'synonyms' => '',
    'vid' => $vid,
    'tid' => 0,
    'relations' => 0,
  );
  taxonomy_save_term($data);
  $_tArray = taxonomy_get_term_by_name($termname);
  $parent[] = $_tArray[0]->tid;
  $patternArray['term name 3'] = $termname;
  $edit = array(
    'title' => $title,
    'body' => $body,
    'taxonomy-' . $vid => $parent,
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, 'Save');

  // TODO Do a MUCH better check here of the information msg
  $patternArray['information message'] = 'been updated';
  foreach ($patternArray as $name => $termPattern) {
    $this
      ->assertText($termPattern, "Checking {$name}");
  }

  // checking database fields
  $result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $node->nid);
  while ($nodeRow = db_fetch_array($result)) {
    $this
      ->assertTrue(in_array($nodeRow['tid'], $parent), 'Checking database field');
  }

  // delete node through browser
  $this
    ->drupalPost('node/' . $node->nid . '/delete', array(), 'Delete');

  // checking after delete
  $this->_browser
    ->get(url("node/" . $node->nid));
  $this
    ->assertNoUnwantedText($termname, "Checking if node exists");

  // checking database fields
  $num_rows = db_result(db_query('SELECT COUNT(*) FROM {term_node} WHERE nid = %d', $node->nid));
  $this
    ->assertEqual($num_rows, 0, 'Checking database field after deletion');

  // delete vocabulary
  // to avoid exception messages create array with empty fields
  $edit = array();
  foreach ($_t as $key) {
    $edit[$key] = 0;
  }
  $edit['name'] = 0;
  $edit['vid'] = $vid;
  taxonomy_save_vocabulary($edit);

  // restoring status
  $this
    ->drupalModuleDisable('story');
}