View source
<?php
class TaxonomyEdgeTreeTestCase extends DrupalWebTestCase {
private $terms = array();
private $vocabulary;
function setUp() {
parent::setUp('taxonomy_edge');
unset($_SESSION['taxonomy_edge_rebuild_module_enabled']);
$this
->treeInit();
module_load_include('core.inc', 'taxonomy_edge');
module_load_include('rebuild.inc', 'taxonomy_edge');
}
function tearDown() {
taxonomy_del_vocabulary($this->vocabulary['vid']);
parent::tearDown();
}
public static function getInfo() {
return array(
'name' => 'Tree',
'description' => 'Test tree handling functions.',
'group' => 'Taxonomy Edge',
);
}
function testInit() {
$terms =& $this->terms;
$vocabulary =& $this->vocabulary;
$vid = $vocabulary['vid'];
$tree = $this
->formatTree();
$expect = "test1\n- test2\n-- test3\n- test4\n-- test5\nsub1\nsub2\nsub3\n";
$match = $tree == $expect;
$message = "Tree initialized" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
}
function testTerm() {
$terms =& $this->terms;
$term = $terms['sub3'];
unset($term['tid']);
$term['name'] = 'sub4';
$term['weight']--;
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "test1\n- test2\n-- test3\n- test4\n-- test5\nsub1\nsub2\nsub4\nsub3\n";
$match = $tree == $expect;
$message = "Add 'sub4' to tree" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
taxonomy_del_term($terms['test1']['tid']);
$tree = $this
->formatTree();
$expect = "sub1\nsub2\nsub4\nsub3\n";
$match = $tree == $expect;
$message = "Remove 'sub1' from tree" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
}
function testTree() {
$terms =& $this->terms;
$vocabulary =& $this->vocabulary;
$vid = $vocabulary['vid'];
$term = $terms['test1'];
$term['parent'] = $terms['sub1']['tid'];
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "sub1\n- test1\n-- test2\n--- test3\n-- test4\n--- test5\nsub2\nsub3\n";
$match = $tree == $expect;
$message = "Move 'test1' to 'sub1'" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
$term = $terms['test1'];
$term['parent'] = 0;
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "test1\n- test2\n-- test3\n- test4\n-- test5\nsub1\nsub2\nsub3\n";
$match = $tree == $expect;
$message = "Move 'test1' back to root" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
$term = $terms['test1'];
$term['parent'] = $terms['sub3']['tid'];
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "sub1\nsub2\nsub3\n- test1\n-- test2\n--- test3\n-- test4\n--- test5\n";
$match = $tree == $expect;
$message = "Move 'test1' to 'sub3'" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
$term = $terms['test1'];
$term['parent'] = 0;
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "test1\n- test2\n-- test3\n- test4\n-- test5\nsub1\nsub2\nsub3\n";
$match = $tree == $expect;
$message = "Move 'test1' back to root" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
$term = $terms['test1'];
$term['parent'] = array(
0,
$terms['sub2']['tid'],
);
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "test1\n- test2\n-- test3\n- test4\n-- test5\nsub1\nsub2\n- test1\n-- test2\n--- test3\n-- test4\n--- test5\nsub3\n";
$match = $tree == $expect;
$message = "Copy 'test1' to 'sub2'" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
$term = $terms['test1'];
$term['parent'] = array(
$terms['sub2']['tid'],
);
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "sub1\nsub2\n- test1\n-- test2\n--- test3\n-- test4\n--- test5\nsub3\n";
$match = $tree == $expect;
$message = "Remove 'test1' from root" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
$term = $terms['test1'];
$term['parent'] = array(
0,
$terms['sub2']['tid'],
);
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "test1\n- test2\n-- test3\n- test4\n-- test5\nsub1\nsub2\n- test1\n-- test2\n--- test3\n-- test4\n--- test5\nsub3\n";
$match = $tree == $expect;
$message = "Copy 'test1' to root" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
$term = $terms['test1'];
$term['parent'] = array(
$terms['sub2']['tid'],
);
taxonomy_save_term($term);
$tree = $this
->formatTree();
$expect = "sub1\nsub2\n- test1\n-- test2\n--- test3\n-- test4\n--- test5\nsub3\n";
$match = $tree == $expect;
$message = "Remove 'test1' from root" . ($match ? "" : "\n<pre>Assumed:\n{$expect}\nGot:\n{$tree}");
$this
->assertTRUE($match, $message);
}
function testOrder() {
$valid_order = taxonomy_edge_is_order_valid($this->vocabulary['vid'], TRUE);
$this
->assertFALSE($valid_order, 'Order is invalid before sort');
$invalid_order = taxonomy_edge_is_order_invalid($this->vocabulary['vid'], TRUE);
$this
->assertTRUE($invalid_order, 'At least one order is invalid before sort');
$context = array();
taxonomy_edge_rebuild_order($this->vocabulary['vid'], $context);
$valid_order = taxonomy_edge_is_order_valid($this->vocabulary['vid'], TRUE);
$this
->assertTRUE($valid_order, 'Order is valid after sort');
$invalid_order = taxonomy_edge_is_order_invalid($this->vocabulary['vid'], TRUE);
$this
->assertFALSE($invalid_order, 'All orders are valid after sort');
taxonomy_edge_invalidate_order($this->vocabulary['vid']);
$valid_order = taxonomy_edge_is_order_valid($this->vocabulary['vid'], TRUE);
$this
->assertFALSE($valid_order, 'Order is invalid after invalidation');
}
function testParents() {
$terms =& $this->terms;
$parents = taxonomy_edge_get_parents($terms['test5']['tid']);
$expect = array(
1 => $terms['test4']['tid'],
2 => $terms['test1']['tid'],
);
$match = $expect == $parents;
$message = "Get parents for 'test5'" . ($match ? "" : "\n<pre>Assumed:\n" . print_r($expect, TRUE) . "\nGot:\n" . print_r($parents, TRUE));
$this
->assertTRUE($match, $message);
$tids = taxonomy_edge_get_top_tids($terms['test3']['tid']);
$expect = array(
$terms['test1']['tid'] => $terms['test1']['tid'],
);
$match = $expect == $tids;
$message = "Get top tids for 'test5'" . ($match ? "" : "\n<pre>Assumed:\n" . print_r($expect, TRUE) . "\nGot:\n" . print_r($tids, TRUE));
$this
->assertTRUE($match, $message);
$tid = taxonomy_edge_get_top_tid($terms['test3']['tid']);
$expect = $terms['test1']['tid'];
$match = $expect == $tid;
$message = "Get top tid for 'test3'" . ($match ? "" : "\n<pre>Assumed:\n" . print_r($expect, TRUE) . "\nGot:\n" . print_r($tids, TRUE));
$this
->assertTRUE($match, $message);
}
function formatTree() {
$vid = $this->vocabulary['vid'];
global $conf;
$conf['taxonomy_edge_exclude_root'] = FALSE;
$conf['taxonomy_edge_static_caching'] = FALSE;
$tree = taxonomy_edge_get_tree_generic($vid);
$output = '';
foreach ($tree as $term) {
$output .= str_repeat('-', $term->depth);
$output .= $term->depth ? ' ' : '';
$output .= $term->name . "\n";
}
return $output;
}
private function treeInit() {
$vocabulary = new stdClass();
$vocabulary->name = 'taxonomy_edge_test1';
$vocabulary->description = 'taxonomy_edge_test1';
$vocabulary->help = '';
$vocabulary = (array) $vocabulary;
taxonomy_save_vocabulary($vocabulary);
$this->vocabulary = $vocabulary;
$tree = array();
$tree[] = "test1";
$tree[] = "test1/test2";
$tree[] = "test1/test2/test3";
$tree[] = "test1/test4";
$tree[] = "test1/test4/test5";
$tree[] = "sub1";
$tree[] = "sub2";
$tree[] = "sub3";
$terms = array();
$weight = 0;
foreach ($tree as $path) {
$parts = explode('/', $path);
$name = array_pop($parts);
$parent = array_pop($parts);
$term = array();
$term['name'] = $name;
$term['vid'] = $vocabulary['vid'];
$term['parent'] = $parent ? $terms[$parent]['tid'] : 0;
$term['weight'] = $weight++;
taxonomy_save_term($term);
$terms[$name] = $term;
}
$this->terms = $terms;
}
function myLog($queries) {
$o = '';
foreach ($queries as $query) {
$o .= sprintf("[%.08f] %s\n", $query['time'], strtr($query['query'], $query['args'])) . "\n";
}
return $o;
}
}