Skip to content

Commit b1c4f41

Browse files
committed
Tweaking some of the table classes related to reordering, but mostly adding a unit test for reordering.
1 parent 3220893 commit b1c4f41

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

lib/model/doctrine/PluginioDoctrineMenuItemTable.class.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function fetchRootByName($name)
119119
* @return void
120120
* @author Brent Shaffer
121121
*/
122-
public function clearTree($root)
122+
public function clearTree(ioDoctrineMenuItem $root)
123123
{
124124
$nodes = $this->createQuery()
125125
->where('root_id = ?', $root['id'])
@@ -142,34 +142,46 @@ public function clearTree($root)
142142
* @return void
143143
* @author Brent Shaffer
144144
*/
145-
public function restoreTreeFromNestedArray($arr, $name)
145+
public function restoreTreeFromNestedArray($arr, ioDoctrineMenuItem $root)
146146
{
147-
$root = $this->fetchRootByName($name);
147+
if (!$root->getNode()->isRoot())
148+
{
149+
throw new sfException('ioDoctrineMenuItemTable::restoreTreeFromNestedArray() must be called using a root node.');
150+
}
148151

152+
// remove all of the nodes from the tree
149153
$this->clearTree($root);
150154

151-
Doctrine::getTable('ioDoctrineMenuItem')->getTree()->createRoot($root);
152-
153-
$this->restoreBranchFromNestedArray(array('id' => $root['id'], 'children' => $arr));
155+
// reestablish the root
156+
$this->getTree()->createRoot($root);
157+
158+
// put the nodes back on
159+
$this->restoreBranchFromNestedArray(array('menu' => $root, 'children' => $arr));
154160
}
155-
161+
156162
/**
157-
* recursive function to create a nested set from an array
163+
* recursive function to create a nested set sourced from an array
158164
*
159-
* @param string $arr
160-
* @return void
165+
* @param array $arr The source array with keys menu and children
166+
* @return ioDoctrineMenuItem
161167
* @author Brent Shaffer
162168
*/
163169
public function restoreBranchFromNestedArray($arr)
164170
{
165-
$parent = Doctrine::getTable('ioDoctrineMenuItem')->findOneById($arr['id']);
171+
$parent = $arr['menu'];
166172

167-
if (isset($arr['children']))
173+
if (isset($arr['children']))
168174
{
169-
foreach ($arr['children'] as $childArr)
175+
foreach ($arr['children'] as $childArr)
170176
{
171-
$child = Doctrine::getTable('ioDoctrineMenuItem')->findOneById($childArr['id']);
177+
$child = $this->find($childArr['id']);
172178
$child->getNode()->insertAsLastChildOf($parent);
179+
180+
// put the child object into the array
181+
$childArr['menu'] = $child;
182+
unset($childArr['id']);
183+
184+
// recurse down and ultimately refresh the parent
173185
$this->restoreBranchFromNestedArray($childArr);
174186
$parent->refresh();
175187
}

test/unit/model/doctrine/PluginioDoctrineMenuItemTableTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require_once $_SERVER['SYMFONY'].'/vendor/lime/lime.php';
55
require_once sfConfig::get('sf_lib_dir').'/test/unitHelper.php';
66

7-
$t = new lime_test(60);
7+
$t = new lime_test(68);
88
$tbl = Doctrine_Core::getTable('ioDoctrineMenuItem');
99

1010
$t->info('1 - Add a tree to an existing node. This should have the same effect as using ioDoctrineMenuItem::persistFromMenuArray()');
@@ -73,4 +73,36 @@
7373
$children = $rt->getNode()->getChildren();
7474
$children[0]->setRoute('http://www.doctrine-project.org');
7575
$children[0]->save();
76-
$t->is($manager->getCacheDriver()->has($cacheKey), false, 'The cache is now unset.');
76+
$t->is($manager->getCacheDriver()->has($cacheKey), false, 'The cache is now unset.');
77+
78+
$t->info('6 - Test restoreTreeFromNestedArray()');
79+
$tbl->createQuery()->delete()->execute();
80+
extract(create_doctrine_test_tree($t));
81+
82+
$newOrder = array(
83+
array(
84+
'id' => $pt2->id,
85+
'children' => array(
86+
array(
87+
'id' => $ch4->id,
88+
'children' => array(
89+
array('id' => $gc1->id),
90+
)
91+
),
92+
)
93+
),
94+
array(
95+
'id' => $pt1->id,
96+
'children' => array(
97+
array('id' => $ch3->id),
98+
array('id' => $ch1->id),
99+
array('id' => $ch2->id),
100+
)
101+
)
102+
);
103+
104+
$tbl->restoreTreeFromNestedArray($newOrder, $rt);
105+
root_sanity_check($t, $rt);
106+
check_child_ordering($t, $rt, array(), array('Parent 2', 'Parent 1'));
107+
check_child_ordering($t, $rt, array(0), array('Child 4'));
108+
check_child_ordering($t, $rt, array(1), array('Child 3', 'Child 1', 'Child 2'));

0 commit comments

Comments
 (0)