Tree = new TreeHelper(new View(null)); $this->Table = TableRegistry::getTableLocator()->get('AfterTrees'); $this->Table->addBehavior('Tree'); $connection = ConnectionManager::get('test'); $sql = $this->Table->getSchema()->truncateSql($connection); foreach ($sql as $snippet) { $connection->execute($snippet); } $data = [ ['name' => 'One'], ['name' => 'Two'], ['name' => 'Three'], ['name' => 'Four'], ['name' => 'One-SubA', 'parent_id' => 1], ['name' => 'Two-SubA', 'parent_id' => 2], ['name' => 'Four-SubA', 'parent_id' => 4], ['name' => 'Two-SubA-1', 'parent_id' => 6], ['name' => 'Two-SubA-1-1', 'parent_id' => 8], ]; foreach ($data as $row) { $row = new Entity($row); $this->Table->save($row); } } /** * @return void */ public function tearDown() { unset($this->Table); TableRegistry::clear(); parent::tearDown(); } /** * @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, '
  • ')); } /** * @return void */ public function testGenerateWithFindAll() { $tree = $this->Table->find('all', ['order' => ['lft' => 'ASC']])->toArray(); $output = $this->Tree->generate($tree); $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
        • Two-SubA-1-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $output = str_replace(["\t", "\r", "\n"], '', $output); $expected = str_replace(["\t", "\r", "\n"], '', $expected); $this->assertTextEquals($expected, $output); } /** * @return void */ public function testGenerateWithDepth() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, ['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); } /** * @return void */ public function testGenerateWithSettings() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, ['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); } /** * @return void */ public function testGenerateWithMaxDepth() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, ['maxDepth' => 2]); $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $this->assertTextEquals($expected, $output); } /** * @return void */ public function testGenerateWithAutoPath() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, ['autoPath' => [7, 10]]); // Two-SubA-1 $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, ['autoPath' => [8, 9]]); // Two-SubA-1-1 $expected = <<
  • One
    • One-SubA
  • Two
    • Two-SubA
      • Two-SubA-1
        • Two-SubA-1-1
  • Three
  • Four
    • Four-SubA
  • TEXT; $this->assertTextEquals($expected, $output); } /** * @return void */ public function testGenerateWithAutoPathAsEntity() { $tree = $this->Table->find('threaded')->toArray(); $entity = new Entity(); $entity->lft = 7; $entity->rght = 10; $output = $this->Tree->generate($tree, ['autoPath' => $entity]); $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() { $data = [ ['name' => 'Two-SubB', 'parent_id' => 2], ['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', ['for' => $id]); $path = $nodes->extract('id')->toArray(); $output = $this->Tree->generate($tree, ['autoPath' => [6, 11], 'hideUnrelated' => true, 'treePath' => $path, 'callback' => [$this, '_myCallback']]); // Two-SubA $expected = <<
  • One
  • Two (active)
    • Two-SubA (active)
      • Two-SubA-1
    • Two-SubB
    • Two-SubC
  • Three
  • Four
  • TEXT; $output = str_replace(["\t"], '', $output); $expected = str_replace(["\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() { $data = [ ['name' => 'Two-SubB', 'parent_id' => 2], ['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; // Two-SubA $nodes = $this->Table->find('path', ['for' => $id]); $path = $nodes->extract('id')->toArray(); $output = $this->Tree->generate($tree, [ 'autoPath' => [6, 11], 'hideUnrelated' => true, 'treePath' => $path, 'callback' => [$this, '_myCallbackSiblings']]); $expected = <<
  • One (sibling)
  • Two (active)
    • Two-SubA (active)
      • Two-SubA-1
    • Two-SubB
    • Two-SubC
  • Three (sibling)
  • Four (sibling)
  • TEXT; $output = str_replace(["\t", "\r", "\n"], '', $output); $expected = str_replace(["\t", "\r", "\n"], '', $expected); $this->assertTextEquals($expected, $output); } /** * @param array $data * @return string|null */ public function _myCallback(array $data) { /** @var \Cake\ORM\Entity $entity */ $entity = $data['data']; if (!empty($entity['hide'])) { return null; } return $data['data']['name'] . ($data['activePathElement'] ? ' (active)' : ''); } /** * @param array $data * @return string|null */ public function _myCallbackSiblings(array $data) { /** @var \Cake\ORM\Entity $entity */ $entity = $data['data']; if (!empty($entity['hide'])) { return null; } if ($data['depth'] == 0 && $data['isSibling']) { return $entity['name'] . ' (sibling)'; } return $entity['name'] . ($data['activePathElement'] ? ' (active)' : ''); } /** * @return void */ public function testGenerateProductive() { $tree = $this->Table->find('threaded')->toArray(); $output = $this->Tree->generate($tree, ['indent' => false]); $expected = '
    • One
      • One-SubA
    • Two
      • Two-SubA
        • Two-SubA-1
          • Two-SubA-1-1
    • Three
    • Four
      • Four-SubA
    '; $this->assertTextEquals($expected, $output); } /** * @return void */ public function testGenerateWithEntityUsage() { $data = [ ['name' => 'Two-SubB', 'parent_id' => 2], ['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', ['for' => $id]); $path = $nodes->extract('id')->toArray(); $output = $this->Tree->generate($tree, [ 'autoPath' => [6, 11], 'treePath' => $path, 'callback' => [$this, '_myCallbackEntity']]); // Two-SubA $expected = <<
  • One
    • One-SubA
  • Two (active)
    • Two-SubA (active)
      • Two-SubA-1
        • Two-SubA-1-1
    • Two-SubB
    • Two-SubC
  • Three
  • Four
    • Four-SubA
  • TEXT; $output = str_replace(["\t", "\r", "\n"], '', $output); $expected = str_replace(["\t", "\r", "\n"], '', $expected); $this->assertTextEquals($expected, $output); } /** * @param array $data * @return string|null */ public function _myCallbackEntity(array $data) { /** @var \Cake\ORM\Entity $entity */ $entity = $data['data']; return h($entity->name) . ($data['activePathElement'] ? ' (active)' : ''); } }