<?php

// functional test for a few special things in the admin module
require_once dirname(__FILE__) . '/../bootstrap/functional.php';
require_once sfConfig::get('sf_lib_dir') . '/test/unitHelper.php';
$browser = new sfTestFunctional(new sfBrowser());
$browser->setTester('doctrine', 'sfTesterDoctrine');
$arr = create_doctrine_test_tree($browser->test());
$root = $arr['rt'];
$rootForm = new ioDoctrineMenuItemForm($root);
$browser->info('1 - Edit an existing, you cannot place it via parent_id')->get(sprintf('/test/menu/%d/edit', $root->id))->with('request')->begin()->isParameter('module', 'io_doctrine_menu')->isParameter('action', 'edit')->end()->with('response')->begin()->checkForm($rootForm)->info('  1.1 - The root menu has no parent_id field')->checkElement('#io_doctrine_menu_item_parent_id', 0)->end()->click('Save', array('io_doctrine_menu_item' => array('parent_id' => $root->id)))->with('form')->begin()->hasErrors(1)->hasGlobalError('extra_fields')->end();
$browser->info('2 - Create a new menu item, set it as a child of something')->get('/test/menu/new')->with('request')->begin()->isParameter('module', 'io_doctrine_menu')->isParameter('action', 'new')->end()->with('response')->begin()->checkForm('ioDoctrineMenuItemForm')->info('  2.1 - The root menu has a parent_id field')->checkElement('#io_doctrine_menu_item_parent_id', 1)->end()->click('Save', array('io_doctrine_menu_item' => array('parent_id' => $root->id, 'name' => 'new child')))->with('form')->begin()->hasErrors(0)->end()->with('doctrine')->begin()->check('ioDoctrineMenuItem', array('name' => 'new child', 'root_id' => $root->id, 'lft' => 16, 'rgt' => 17, 'level' => 1))->end();
die;
$browser->info('3 - Create a new menu item, make it root')->get('/test/menu/new')->with('request')->begin()->isParameter('module', 'io_doctrine_menu')->isParameter('action', 'new')->end()->click('Save', array('io_doctrine_menu_item' => array('name' => 'new root')))->with('form')->begin()->hasErrors(0)->end()->with('doctrine')->begin()->check('ioDoctrineMenuItem', array('name' => 'new root', 'lft' => 18, 'rgt' => 19, 'level' => 0))->end();
$t->is($menu->getCredentials(), array('c1', 'c2'), 'The menu rt has the correct credentials array');
$t->is(count($menu['Parent 1']->getChildren()), 3, 'pt1 has 3 children.');
$t->is(array_keys($menu['Parent 1']->getChildren()), array('Child 1', 'Child 2', 'Child 3'), 'pt1\'s children are array(Child 1, Child 2, Child 3)');
$t->is(count($menu['Parent 2']->getChildren()), 1, 'pt2 has 1 child.');
$t->is(array_keys($menu['Parent 2']->getChildren()), array('Child 4'), 'pt2\'s children are array(Child 4)');
$t->info('  3.4 - Compare the created meno to that of the menu from create_test_tree(). They should be identical.');
$arr = create_test_tree($t);
$matchingMenu = $arr['menu'];
// update its credentials to match
$matchingMenu->setCredentials(array('c1', 'c2'));
// just doing in stages so its more obvious when something fails
$t->is($menu->toArray(false), $matchingMenu->toArray(false), 'The menus match non-recursively.');
$t->is($menu->toArray(), $matchingMenu->toArray(), 'The full menus match recursively.');
$t->info('4 - Test generateNestedSortableArray()');
Doctrine_Query::create()->from('ioDoctrineMenuItem')->delete()->execute();
$arr = create_doctrine_test_tree($t);
$rt = $arr['rt'];
$expected = array('requestFirstIndex' => 0, 'firstIndex' => 0, 'count' => 7, 'columns' => array('&ldquo;Root li&rdquo;'), 'items' => array(array('id' => $arr['pt1']->id, 'level' => '1', 'info' => array('<strong>Parent 1</strong>'), 'children' => array(array('id' => $arr['ch1']->id, 'level' => '2', 'info' => array('<strong>Child 1</strong>')), array('id' => $arr['ch2']->id, 'level' => '2', 'info' => array('<strong>Child 2</strong>')), array('id' => $arr['ch3']->id, 'level' => '2', 'info' => array('<strong>Child 3</strong>')))), array('id' => $arr['pt2']->id, 'level' => '1', 'info' => array('<strong>Parent 2</strong>'), 'children' => array(array('id' => $arr['ch4']->id, 'level' => '2', 'info' => array('<strong>Child 4</strong>'), 'children' => array(array('id' => $arr['gc1']->id, 'level' => '3', 'info' => array('<strong>Grandchild 1</strong>'))))))));
$result = $rt->generateNestedSortableArray();
/**
 * If the final test doesn't pass, these could be helpful for debugging why:
$t->is($result['count'], 7, '$result[count] = 7 for the 7 children under root.');
$t->is($result['columns'], array('&ldquo;Root li&rdquo;'), '$result[columns] = array(Root li) .');

$t->is(count($result['items']), 2, '$result[items] count is 2 (pt1, pt2).');
$t->is(count($result['items'][0]['children']), 3, '$result[items][0][items] count is 3 (ch1, ch2, ch3).');
$t->is(count($result['items'][1]['children']), 1, '$result[items][1][items] count is 1 (ch4).');

$t->is($result['items'][0]['id'], $arr['pt1']->id, '$result[items][0][id] is pt1\'s id');
$t->is($result['items'][0]['level'], 1, '$result[items][0][level] is 1');
$t->is($result['items'][0]['info'], array('<strong>Parent 1</strong>'), '$result[items][0][info] is array(Parent 1)');