Tree = new TreeHelper(new View(null)); $this->Table = TableRegistry::get('AfterTrees'); $this->Table->addBehavior('Tree'); //$this->Table->truncate(); $connection = ConnectionManager::get('test'); $sql = $this->Table->schema()->truncateSql($connection); foreach ($sql as $snippet) { $connection->execute($snippet); } //$this->Table->deleteAll(array()); $data = array( array('name' => 'One'), array('name' => 'Two'), array('name' => 'Three'), array('name' => 'Four'), array('name' => 'One-SubA', 'parent_id' => 1), array('name' => 'Two-SubA', 'parent_id' => 2), array('name' => 'Four-SubA', 'parent_id' => 4), array('name' => 'Two-SubA-1', 'parent_id' => 6), array('name' => 'Two-SubA-1-1', 'parent_id' => 8), ); foreach ($data as $row) { $row = new Entity($row); $this->Table->save($row); } } public function tearDown() { unset($this->Table); TableRegistry::clear(); parent::tearDown(); } public function testObject() { $this->assertInstanceOf('Tools\View\Helper\TreeHelper', $this->Tree); } /** * TreeHelperTest::testGenerate() * * @return void */ public function testGenerate() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree); $expected = <<
  • One
  • Two
  • Three
  • Four
  • TEXT; $this->assertTextEquals($expected, $output); $this->assertTrue(substr_count($output, '')); $this->assertTrue(substr_count($output, '
  • ') === substr_count($output, '
  • ')); } /** * TreeHelperTest::testGenerateWithFindAll() * * @return void */ public function testGenerateWithFindAll() { $tree = $this->Table->find('all', array('order' => array('lft' => 'ASC')))->toArray(); $output = $this->Tree->generate($tree); //debug($output); return; $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
        • Two-SubA-1-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $output = str_replace(array("\t", "\r", "\n"), '', $output); $expected = str_replace(array("\t", "\r", "\n"), '', $expected); $this->assertTextEquals($expected, $output); } /** * TreeHelperTest::testGenerateWithDepth() * * @return void */ public function testGenerateWithDepth() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, array('depth' => 1)); $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
        • Two-SubA-1-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $this->assertTextEquals($expected, $output); } /** * TreeHelperTest::testGenerateWithSettings() * * @return void */ public function testGenerateWithSettings() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, array('class' => 'navi', 'id' => 'main', 'type' => 'ol')); $expected = <<
  • One
    1. One-SubA
  • Two
    1. Two-SubA
      1. Two-SubA-1
        1. Two-SubA-1-1
  • Three
  • Four
    1. Four-SubA
  • TEXT; $this->assertTextEquals($expected, $output); } /** * TreeHelperTest::testGenerateWithMaxDepth() * * @return void */ public function testGenerateWithMaxDepth() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, array('maxDepth' => 2)); $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $this->assertTextEquals($expected, $output); } /** * TreeHelperTest::testGenerateWithAutoPath() * * @return void */ public function testGenerateWithAutoPath() { $tree = $this->Table->find('threaded')->toArray(); //debug($tree); $output = $this->Tree->generate($tree, array('autoPath' => array(7, 10))); // Two-SubA-1 //debug($output); $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
        • Two-SubA-1-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $this->assertTextEquals($expected, $output); $output = $this->Tree->generate($tree, array('autoPath' => array(8, 9))); // Two-SubA-1-1 //debug($output); $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
        • Two-SubA-1-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $this->assertTextEquals($expected, $output); } /** * * - One * -- One-SubA * - Two * -- Two-SubA * --- Two-SubA-1 * ---- Two-SubA-1-1 * -- Two-SubB * -- Two-SubC * - Three * - Four * -- Four-SubA * * @return void */ public function testGenerateWithAutoPathAndHideUnrelated() { $this->skipIf(true, 'FIXME'); $data = array( array('name' => 'Two-SubB', 'parent_id' => 2), array('name' => 'Two-SubC', 'parent_id' => 2), ); foreach ($data as $row) { $row = new Entity($row); $this->Table->save($row); } $tree = $this->Table->find('threaded')->toArray(); $id = 6; $nodes = $this->Table->find('path', array('for' => $id)); $path = $nodes->extract('id')->toArray(); $output = $this->Tree->generate($tree, array('autoPath' => array(6, 11), 'hideUnrelated' => true, 'treePath' => $path, 'callback' => array($this, '_myCallback'))); // Two-SubA //debug($output); $expected = <<
  • One
  • Two (active)
    • Two-SubA (active)
      • Two-SubA-1
    • Two-SubB
    • Two-SubC
  • Three
  • Four
  • TEXT; $output = str_replace(array("\t"), '', $output); $expected = str_replace(array("\t"), '', $expected); $this->assertTextEquals($expected, $output); } /** * * - One * -- One-SubA * - Two * -- Two-SubA * --- Two-SubA-1 * ---- Two-SubA-1-1 * -- Two-SubB * -- Two-SubC * - Three * - Four * -- Four-SubA * * @return void */ public function testGenerateWithAutoPathAndHideUnrelatedAndSiblings() { $this->skipIf(true, 'FIXME'); $data = array( array('name' => 'Two-SubB', 'parent_id' => 2), array('name' => 'Two-SubC', 'parent_id' => 2), ); foreach ($data as $row) { $row = new Entity($row); $this->Table->save($row); } $tree = $this->Table->find('threaded')->toArray(); $id = 6; $nodes = $this->Table->find('path', array('for' => $id)); $path = $nodes->extract('id')->toArray(); $output = $this->Tree->generate($tree, array( 'autoPath' => array(6, 11), 'hideUnrelated' => true, 'treePath' => $path, 'callback' => array($this, '_myCallbackSiblings'))); // Two-SubA //debug($output); $expected = <<
  • One (sibling)
  • Two (active)
    • Two-SubA (active)
      • Two-SubA-1
    • Two-SubB
    • Two-SubC
  • Three (sibling)
  • Four (sibling)
  • TEXT; $output = str_replace(array("\t", "\r", "\n"), '', $output); $expected = str_replace(array("\t", "\r", "\n"), '', $expected); //debug($output); //debug($expected); $this->assertTextEquals($expected, $output); } public function _myCallback($data) { if (!empty($data['data']['hide'])) { return; } return $data['data']['name'] . ($data['activePathElement'] ? ' (active)' : ''); } public function _myCallbackSiblings($data) { if (!empty($data['data']['hide'])) { return; } if ($data['depth'] == 0 && $data['isSibling']) { return $data['data']['name'] . ' (sibling)'; } return $data['data']['name'] . ($data['activePathElement'] ? ' (active)' : ''); } /** * TreeHelperTest::testGenerateProductive() * * @return void */ public function testGenerateProductive() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, array('indent' => false)); $expected = '
    • One
      • One-SubA
    • Two
      • Two-SubA
        • Two-SubA-1
          • Two-SubA-1-1
    • Three
    • Four
      • Four-SubA
    '; $this->assertTextEquals($expected, $output); } }