View source
<?php
class TaxonomyVocabularyFunctions extends DrupalTestCase {
function get_info() {
return array(
'name' => 'Vocabulary functions',
'desc' => "Create/Edit/Delete a vocabulary and assert that all fields were properly saved",
'group' => 'Taxonomy',
);
}
function testVocabularyFunctions() {
$vid = 0;
$name = $this
->randomName(200);
$description = $this
->randomName(200);
$help = $this
->randomName(200);
$hierarchy = rand(0, 2);
$multiple = rand(0, 1);
$required = rand(0, 1);
$relations = rand(0, 1);
$tags = rand(0, 1);
$weight = rand(-9, 9);
$module = 'taxonomy';
$nodesList = array_keys(node_get_types());
$maxNodes = rand(1, count($nodesList));
$nodes = array();
for ($i = 0; $i < $maxNodes; $i++) {
$nodes[$nodesList[$i]] = $nodesList[$i];
$nodesBak[$nodesList[$i]] = $nodesList[$i];
}
$_t = array(
'vid',
'name',
'description',
'help',
'relations',
'hierarchy',
'multiple',
'required',
'tags',
'module',
'weight',
'nodes',
);
$edit = array();
foreach ($_t as $key) {
$edit[$key] = ${$key};
}
taxonomy_save_vocabulary($edit);
ksort($nodesBak);
$edit['nodes'] = $nodesBak;
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $voc) {
if ($voc->name == $name) {
$vid = $voc->vid;
break;
}
}
$edit['vid'] = $vid;
$getEdit = taxonomy_vocabulary_load($vid);
foreach ($getEdit as $key => $value) {
$this
->assertEqual($value, $edit[$key], "Checking value of {$key}");
}
$deleteArray = array();
foreach ($getEdit as $key => $v) {
$deleteArray[$key] = 0;
}
$deleteArray['vid'] = $vid;
taxonomy_save_vocabulary($deleteArray);
$vocabularies = taxonomy_get_vocabularies();
$vid = 0;
foreach ($vocabularies as $voc) {
if ($voc->name == $name) {
$vid = $voc->vid;
break;
}
}
$this
->assertEqual($vid, 0, "Deleted vocabulary ({$vid})");
}
}
class TaxonomyTermFunctions extends DrupalTestCase {
function get_info() {
return array(
'name' => 'Term functions',
'desc' => "Testing save/update/delete terms",
'group' => 'Taxonomy',
);
}
function testTermsFunctions() {
$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);
$relation = 1;
$edit['name'] = $name;
taxonomy_save_vocabulary($edit);
$termname = $this
->randomName(20);
$termdesc = $this
->randomName(200);
$termweight = rand(-9, 9);
$randSyn = rand(0, 9);
$synonyms = array();
for ($i = 0; $i < $randSyn; $i++) {
$synonyms[] = $this
->randomName(20);
}
$termsyn = implode("\n", $synonyms);
$data = array(
'name' => $termname,
'description' => $termdesc,
'weight' => $termweight,
'synonyms' => $termsyn,
'vid' => $edit['vid'],
'tid' => 0,
'relations' => 0,
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$getTerm = $_tArray[0];
$checkField = array(
'name',
'description',
'weight',
'vid',
);
foreach ($checkField as $v) {
$this
->assertEqual($data[$v], $getTerm->{$v}, "Checking value of the term ({$v})");
}
$getSynonyms = taxonomy_get_synonyms($getTerm->tid);
$this
->assertEqual(sort($synonyms), sort($getSynonyms), 'Checking synonyms');
$relations = array();
$staryTid = $getTerm->tid;
$relations[] = $staryTid;
$termname = $this
->randomName(20);
$data = array(
'name' => $termname,
'description' => '',
'weight' => 0,
'synonyms' => 0,
'vid' => $edit['vid'],
'tid' => 0,
'relations' => array(
$staryTid,
),
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$getTerm = $_tArray[0];
$relations[] = $getTerm->tid;
$termname = $this
->randomName(20);
$data = array(
'name' => $termname,
'description' => '',
'weight' => 0,
'synonyms' => 0,
'vid' => $edit['vid'],
'tid' => 0,
'relations' => array(
$staryTid,
$getTerm->tid,
),
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$getTerm = $_tArray[0];
$related = taxonomy_get_related($getTerm->tid);
foreach ($relations as $rTid) {
$this
->assertTrue(array_key_exists($rTid, $related), "Checking relations ({$rTid})");
}
$edit['name'] = 0;
taxonomy_save_vocabulary($edit);
}
function testTermsFunctionsSingleHierarchy() {
$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['name'] = $name;
taxonomy_save_vocabulary($edit);
$termname = $this
->randomName(20);
$data = array(
'name' => $termname,
'description' => '',
'weight' => 0,
'synonyms' => '',
'vid' => $edit['vid'],
'tid' => 0,
'relations' => 0,
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$parent = $_tArray[0];
$termname = $this
->randomName(20);
$data = array(
'name' => $termname,
'description' => '',
'weight' => 0,
'synonyms' => '',
'vid' => $edit['vid'],
'tid' => 0,
'relations' => 0,
'parent' => array(
$parent->tid,
),
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$children = $_tArray[0];
$getChildren = taxonomy_get_children($parent->tid);
$getParent = taxonomy_get_parents($children->tid);
$this
->assertEqual($parent, $getParent[$parent->tid], 'Checking parents');
$this
->assertEqual($children, $getChildren[$children->tid], 'Checking children');
$edit['name'] = 0;
taxonomy_save_vocabulary($edit);
}
function testTermsFunctionsMultipleHierarchy() {
$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['name'] = $name;
taxonomy_save_vocabulary($edit);
$parent = array();
$termname = $this
->randomName(20);
$data = array(
'name' => $termname,
'description' => '',
'weight' => 0,
'synonyms' => '',
'vid' => $edit['vid'],
'tid' => 0,
'relations' => 0,
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$parent[] = $_tArray[0]->tid;
$termname = $this
->randomName(20);
$data = array(
'name' => $termname,
'description' => '',
'weight' => 0,
'synonyms' => '',
'vid' => $edit['vid'],
'tid' => 0,
'relations' => 0,
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$parent[] = $_tArray[0]->tid;
$termname = $this
->randomName(20);
$data = array(
'name' => $termname,
'description' => '',
'weight' => 0,
'synonyms' => '',
'vid' => $edit['vid'],
'tid' => 0,
'relations' => 0,
'parent' => array(
$parent,
),
);
taxonomy_save_term($data);
$_tArray = taxonomy_get_term_by_name($termname);
$children = $_tArray[0];
$getParent = taxonomy_get_parents($children->tid);
foreach ($parent as $p) {
$this
->assertTrue(array_key_exists($p, $getParent), "Checking parents ({$p})");
}
$edit['name'] = 0;
taxonomy_save_vocabulary($edit);
}
}
class TaxonomyTestNodeApi extends DrupalTestCase {
function get_info() {
return array(
'name' => 'Taxonomy nodeapi',
'desc' => "Save & edit a node and assert that taxonomy terms are saved/loaded properly.",
'group' => 'Taxonomy',
);
}
function testTaxonomyNode() {
$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'];
$parent = array();
$patternArray = array();
$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;
$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;
$perm = array(
'access content',
'create story content',
'edit own story content',
'delete own story content',
);
$account = $this
->drupalCreateUserRolePerm($perm);
$this
->drupalLoginUser($account);
$this
->drupalGet('node/add/story');
$req = $this->_browser
->getRequest();
$headers = $this->_browser
->getHeaders();
$content = $this
->drupalGetContent();
$title = $this
->randomName();
$body = $this
->randomName(100);
$edit = array(
'title' => $title,
'body' => $body,
'taxonomy-' . $vid => $parent,
);
$action = url('node/add/story', array(
'absolute' => TRUE,
));
$this
->drupalPost($action, $edit, 'Save');
$content = $this
->drupalGetContent();
$patternArray['body text'] = $body;
$patternArray['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}");
}
$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');
}
array_pop($parent);
$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;
$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');
$patternArray['information message'] = 'been updated';
foreach ($patternArray as $name => $termPattern) {
$this
->assertText($termPattern, "Checking {$name}");
}
$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');
}
$this
->drupalPost('node/' . $node->nid . '/delete', array(), 'Delete');
$this->_browser
->get(url("node/" . $node->nid));
$this
->assertNoUnwantedText($termname, "Checking if node exists");
$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');
$edit = array();
foreach ($_t as $key) {
$edit[$key] = 0;
}
$edit['name'] = 0;
$edit['vid'] = $vid;
taxonomy_save_vocabulary($edit);
$this
->drupalModuleDisable('story');
}
}