$ch4Count = Doctrine_Query::create()->from('ioDoctrineMenuItem m')->where('m.name = ?', 'Child 4')->count();
$gc1Count = Doctrine_Query::create()->from('ioDoctrineMenuItem m')->where('m.name = ?', 'Grandchild 1')->count();
$t->is($ch4Count, null, 'The ch4 menu item was deleted entirely.');
$t->is($gc1Count, null, 'The gc1 menu item was deleted entirely.');
$t->info('    2.5.5 - Add a new child (ch6) to pt2.');
$menu['Parent 2']->addChild('Child 6');
persist_menu($t, $rt, $menu);
$t->info('  Check the integrity of the tree.');
test_total_nodes($t, array(0 => 1, 1 => 2, 2 => 4, 3 => 0));
root_sanity_check($t, $rt);
check_child_ordering($t, $rt, array(1), array('Child 6'));
$t->info('    2.5.6 - Add a pt3 under root after pt2');
$menu->addChild('Parent 3', 'http://www.doctrine-project.org');
persist_menu($t, $rt, $menu);
$t->info('  Check the integrity of the tree.');
test_total_nodes($t, array(0 => 1, 1 => 3, 2 => 4, 3 => 0));
root_sanity_check($t, $rt);
check_child_ordering($t, $rt, array(), array('Parent 1', 'Parent 2', 'Parent 3'));
$t->info('3 - Test createMenu() to create a new ioMenuItem tree from the database.');
// clear out the data
Doctrine_Query::create()->from('ioDoctrineMenuItem')->delete()->execute();
Doctrine_Query::create()->from('sfGuardPermission')->delete()->execute();
// create the tree and make its vars accessible
extract(create_doctrine_test_tree($t));
print_test_tree($t);
$t->info('  3.1 - Adding some Permissions for testing');
$c1 = new sfGuardPermission();
$c1->name = 'c1';
$c1->save();
$c2 = new sfGuardPermission();
$c2->name = 'c2';
$rt = create_root('rt');
$arr = create_test_tree($t);
$menu = $arr['menu'];
$menu['Parent 1']->setAttributes(array('class' => 'parent1'));
$menu['Parent 1']['Child 2']->setCredentials(array('c2', 'c3'));
print_test_tree($t);
$tbl->persist($menu, $rt);
complex_root_menu_check($rt, $t, 2);
test_total_nodes($t, array(0 => 1, 1 => 2, 2 => 4, 3 => 1));
root_sanity_check($t, $rt);
$t->info('2 - Persist an entire menu to a new root. This should have the same effect as above, but creates the root for us.');
$tbl->createQuery()->delete()->execute();
$tbl->persist($menu);
$rt = $tbl->findOneByLevel(0);
complex_root_menu_check($rt, $t, 2);
test_total_nodes($t, array(0 => 1, 1 => 2, 2 => 4, 3 => 1));
root_sanity_check($t, $rt);
$t->is($rt->getName(), 'Root li', 'The created root has the correct name.');
$t->is($rt->getAttributes(), 'class="root"', 'The created root has the correct attributes.');
$t->info('3 - Test ->fetchMenu()');
$t->info('  3.1 - Test a string name to the function.');
$tbl->createQuery()->delete()->execute();
create_doctrine_test_tree($t);
$fromDbMenu = $tbl->fetchMenu('Root li');
$arr = create_test_tree($t);
$menu = $arr['menu'];
$t->is($menu->toArray(), $fromDbMenu->toArray(), '->fetchMenu() retrieves the full, correct menu.');
$t->info('4 - Test the whole process. Persist a menu to the database and fetch it back out.');
Doctrine_Query::create()->from('ioDoctrineMenuItem')->delete()->execute();
$arr = create_test_tree($t);
$menu = $arr['menu'];