getDepth()) , $item->getId() , ': ' , $item->getTitle() , ' [', $item->getLeftValue(), ':', $item->getRightValue() , ']' . "\n"; } } /** * Adds a new Page row with specified parent Id. * * @param int $parentId */ protected function addNewChildPage($parentId) { $db = Propel::getConnection(PagePeer::DATABASE_NAME); //$db->beginTransaction(); $parent = PagePeer::retrieveByPK($parentId); $page = new Page(); $page->setTitle('new page '.time()); $page->insertAsLastChildOf($parent); $page->save(); //$db->commit(); } /** * Asserts that the Page table tree integrity is intact. */ protected function assertPageTreeIntegrity() { $db = Propel::getConnection(PagePeer::DATABASE_NAME); $values = array(); $log = ''; foreach ($db->query('SELECT Id, LeftChild, RightChild, Title FROM Page', PDO::FETCH_NUM) as $row) { list($id, $leftChild, $rightChild, $title) = $row; if (!in_array($leftChild, $values)) { $values[] = (int) $leftChild; } else { $this->fail('Duplicate LeftChild value '.$leftChild); } if (!in_array($rightChild, $values)) { $values[] = (int) $rightChild; } else { $this->fail('Duplicate RightChild value '.$rightChild); } $log .= "[$id($leftChild:$rightChild)]"; } sort($values); if ($values[count($values)-1] != count($values)) { $message = sprintf("Tree integrity NOT ok (%s)\n", $log); $message .= sprintf('Integrity error: value count: %d, high value: %d', count($values), $values[count($values)-1]); $this->fail($message); } } /** * Tests adding a node to the Page tree. */ public function testAdd() { $db = Propel::getConnection(PagePeer::DATABASE_NAME); // I'm not sure if the specific ID matters, but this should match original // code. The ID will change with subsequent runs (e.g. the first time it will be 11) $startId = $db->query('SELECT MIN(Id) FROM Page')->fetchColumn(); $this->addNewChildPage($startId + 10); $this->assertPageTreeIntegrity(); } }