TYPO3 CMS  TYPO3_7-6
TreeNodeTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
23  // Utility functions
25 
30  private function determineFixturesPath()
31  {
32  // We have to take the whole relative path as otherwise this test fails on Windows systems
33  return PATH_site . 'typo3/sysext/backend/Tests/Unit/Tree/Fixtures/';
34  }
35 
36  protected function setUpNodeTestData()
37  {
38  $fixture = new \TYPO3\CMS\Backend\Tree\TreeNode();
39  $fixture->setId('Root');
40  $nodeCollection = new \TYPO3\CMS\Backend\Tree\TreeNodeCollection();
41  for ($i = 0; $i < 10; ++$i) {
42  $node = new \TYPO3\CMS\Backend\Tree\TreeNode();
43  $node->setId($i);
44  $node->setParentNode($fixture);
45  $subNodeCollection = new \TYPO3\CMS\Backend\Tree\TreeNodeCollection();
46  for ($j = 0; $j < 5; ++$j) {
47  $subNode = new \TYPO3\CMS\Backend\Tree\TreeRepresentationNode();
48  $subNode->setId($j);
49  $subNode->setLabel('SubTest');
50  $subNode->setType('Type');
51  $subNode->setClass('Class');
52  $subNode->setIcon('Icon');
53  $subNode->setCallbackAction('Callback Action');
54  $subNode->setParentNode($node);
55  $subNodeCollection->append($subNode);
56  }
57  $node->setChildNodes($subNodeCollection);
58  $nodeCollection->append($node);
59  }
60  $fixture->setChildNodes($nodeCollection);
61  return $fixture;
62  }
63 
65  // Test cases
67 
70  public function serializeFixture()
71  {
72  $expected = trim(file_get_contents($this->determineFixturesPath() . 'serialized.txt'));
73  $fixture = $this->setUpNodeTestData();
74  $serializedString = trim($fixture->serialize());
75  $this->assertSame($expected, $serializedString);
76  }
77 
81  public function deserializeFixture()
82  {
83  $source = trim(file_get_contents($this->determineFixturesPath() . 'serialized.txt'));
84  $node = new \TYPO3\CMS\Backend\Tree\TreeNode();
85  $node->unserialize($source);
86  $serializedString = $node->serialize();
87  $this->assertSame($source, $serializedString);
88  }
89 
93  public function compareNodes()
94  {
95  $node = new \TYPO3\CMS\Backend\Tree\TreeNode(['id' => '15']);
96  $otherNode = new \TYPO3\CMS\Backend\Tree\TreeNode(['id' => '5']);
97  $compareResult = $node->compareTo($otherNode);
98  $otherNode->setId('25');
99  $compareResult = $node->compareTo($otherNode);
100  $this->assertSame(-1, $compareResult);
101  $otherNode->setId('15');
102  $compareResult = $node->compareTo($otherNode);
103  $this->assertSame(0, $compareResult);
104  }
105 }