Fixed bug #16778: Test failures in t3lib_tree_NodeTest if tests are in typo3_src...
[Packages/TYPO3.CMS.git] / tests / t3lib / tree / t3lib_tree_nodeTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Stefan Galinski <stefan.galinski@gmail.com>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for class t3lib_tree_Node.
27 *
28 * @author Stefan Galinski <stefan.galinski@gmail.com>
29 * @package TYPO3
30 * @subpackage t3lib
31 */
32 class t3lib_tree_NodeTest extends tx_phpunit_testcase {
33 //////////////////////
34 // Utility functions
35 //////////////////////
36
37 /**
38 * Returns the absolute fixtures path for this testcase.
39 *
40 * @return string the absolute fixtures path for this testcase, will not be empty
41 */
42 private function determineFixturesPath() {
43 $possibleFixturesPath1 = PATH_site . 'tests/t3lib/tree/fixtures/';
44 $possibleFixturesPath2 = PATH_site . 'typo3_src/tests/t3lib/tree/fixtures/';
45
46 if (file_exists($possibleFixturesPath1)) {
47 $fixturesPath = $possibleFixturesPath1;
48 } elseif (file_exists($possibleFixturesPath2)) {
49 $fixturesPath = $possibleFixturesPath2;
50 } else {
51 $this->fail('The fixtures path could not be determined.');
52 }
53
54 return $fixturesPath;
55 }
56
57 protected function setUpNodeTestData() {
58 $fixture = new t3lib_tree_Node;
59 $fixture->setId('Root');
60
61 $nodeCollection = new t3lib_tree_NodeCollection;
62 for ($i = 0; $i < 10; ++$i) {
63 $node = new t3lib_tree_Node;
64 $node->setId($i);
65 $node->setParentNode($fixture);
66
67 $subNodeCollection = new t3lib_tree_NodeCollection;
68 for ($j = 0; $j < 5; ++$j) {
69 $subNode = new t3lib_tree_RepresentationNode;
70 $subNode->setId($j);
71 $subNode->setLabel('SubTest');
72 $subNode->setType('Type');
73 $subNode->setClass('Class');
74 $subNode->setIcon('Icon');
75 $subNode->setCallbackAction('Callback Action');
76 $subNode->setParentNode($node);
77 $subNodeCollection->append($subNode);
78 }
79 $node->setChildNodes($subNodeCollection);
80 $nodeCollection->append($node);
81 }
82 $fixture->setChildNodes($nodeCollection);
83
84
85 return $fixture;
86 }
87
88
89 ///////////////
90 // Test cases
91 ///////////////
92
93 /**
94 * @test
95 */
96 public function serializeFixture() {
97 $expected = trim(file_get_contents($this->determineFixturesPath() . 'serialized.txt'));
98 $fixture = $this->setUpNodeTestData();
99 $serializedString = trim($fixture->serialize());
100 $this->assertSame($expected, $serializedString);
101 }
102
103 /**
104 * @test
105 */
106 public function deserializeFixture() {
107 $source = trim(file_get_contents($this->determineFixturesPath() . 'serialized.txt'));
108 $node = new t3lib_tree_Node();
109 $node->unserialize($source);
110 $serializedString = $node->serialize();
111 $this->assertSame($source, $serializedString);
112 }
113
114 /**
115 * @test
116 */
117 public function compareNodes() {
118 $node = new t3lib_tree_Node(array('id' => '15'));
119 $otherNode = new t3lib_tree_Node(array('id' => '5'));
120 $compareResult = $node->compareTo($otherNode);
121
122 $otherNode->setId('25');
123 $compareResult = $node->compareTo($otherNode);
124 $this->assertSame(-1, $compareResult);
125
126 $otherNode->setId('15');
127 $compareResult = $node->compareTo($otherNode);
128 $this->assertSame(0, $compareResult);
129 }
130 }
131 ?>