You are here

function TaxonomyTestNodeApi::testTaxonomyNode in SimpleTest 5

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

File

tests/taxonomy.module.test, line 254

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 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(url('node/add/story', NULL, NULL, TRUE));
  $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,
  );
  $this
    ->drupalPostRequest('node/add/story', $edit, 'Submit');
  $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
    ->drupalPostRequest('node/' . $node->nid . '/edit', $edit, 'Submit');

  // 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
    ->drupalPostRequest('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
  $result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $node->nid);
  $this
    ->assertEqual(db_num_rows($result), 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');
}