1<?php 2 3namespace dokuwiki\plugin\simplenavi\test; 4 5use dokuwiki\TreeBuilder\PageTreeBuilder; 6use DokuWikiTest; 7 8/** 9 * General tests for the simplenavi plugin 10 * 11 * @author Michael Große <grosse@cosmocode.de> 12 * 13 * @group plugin_simplenavi 14 * @group plugins 15 */ 16class SimplenaviTest extends DokuWikiTest 17{ 18 19 protected $pluginsEnabled = array('simplenavi'); 20 21 public function setUp(): void 22 { 23 parent::setUp(); 24 saveWikiText('sidebar', '{{simplenavi>}}', 'create test sidebar'); 25 26 $pages = [ 27 ['foo', 'Foo Page'], 28 ['simplenavi', 'Self Start'], 29 ['namespace1:start', 'ZZZ Namespace 1 Start'], 30 ['namespace2:foo', 'Namespace 2 Foo'], 31 ['namespace2', 'Namespace 2 Start'], 32 ['namespace12:foo', 'Namespace 12 Foo'], 33 ['namespace12:start', 'Namespace 12 Start'], 34 ['namespace123:namespace123', 'AAA Namespace 123 Start'], 35 ['namespace123:foo', 'Namespace 123 Foo'], 36 ['namespace123:deep:start', 'Namespace 123 Deep Start'], 37 ['namespace123:deep:foo', 'Namespace 123 Deep Foo'], 38 ['namespace21:foo', 'Namespace 21 Foo'], 39 ['namespace21:start', 'Namespace 21 Start'], 40 ]; 41 42 foreach ($pages as $page) { 43 saveWikiText('simplenavi:' . $page[0], '====== ' . $page[1] . ' ======', 'create test page'); 44 } 45 46 } 47 48 public function dataProvider() 49 { 50 51 yield [ 52 'set' => 'by ID, all branches closed', 53 'sort' => 'id', 54 'usetitle' => false, 55 'home' => false, 56 'current' => 'simplenavi:page', 57 'expect' => [ 58 '+simplenavi:foo', 59 '+simplenavi:namespace1:start', 60 '+simplenavi:namespace12:start', 61 '+simplenavi:namespace123:namespace123', 62 '+simplenavi:namespace2', 63 '+simplenavi:namespace21:start', 64 ] 65 ]; 66 67 yield [ 68 'set' => 'by ID, branch open', 69 'sort' => 'id', 70 'usetitle' => false, 71 'home' => false, 72 'current' => 'simplenavi:namespace123:deep:foo', 73 'expect' => [ 74 '+simplenavi:foo', 75 '+simplenavi:namespace1:start', 76 '+simplenavi:namespace12:start', 77 '+simplenavi:namespace123:namespace123', 78 '++simplenavi:namespace123:deep:start', 79 '+++simplenavi:namespace123:deep:foo', 80 '++simplenavi:namespace123:foo', 81 '+simplenavi:namespace2', 82 '+simplenavi:namespace21:start', 83 ] 84 ]; 85 86 87 yield [ 88 'set' => 'by Title, Natural Search, all branches closed', 89 'sort' => 'title', 90 'usetitle' => true, 91 'home' => false, 92 'current' => 'simplenavi:page', 93 'expect' => [ 94 '+simplenavi:namespace123:namespace123', 95 '+simplenavi:foo', 96 '+simplenavi:namespace2', 97 '+simplenavi:namespace12:start', 98 '+simplenavi:namespace21:start', 99 '+simplenavi:namespace1:start', 100 ] 101 ]; 102 103 yield [ 104 'set' => 'by Title, Natural Sort, branch open', 105 'sort' => 'title', 106 'usetitle' => true, 107 'home' => false, 108 'current' => 'simplenavi:namespace123:deep:foo', 109 'expect' => [ 110 '+simplenavi:namespace123:namespace123', 111 '++simplenavi:namespace123:deep:start', 112 '+++simplenavi:namespace123:deep:foo', 113 '++simplenavi:namespace123:foo', 114 '+simplenavi:foo', 115 '+simplenavi:namespace2', 116 '+simplenavi:namespace12:start', 117 '+simplenavi:namespace21:start', 118 '+simplenavi:namespace1:start', 119 ] 120 ]; 121 122 yield [ 123 'set' => 'by Title, Natural Sort, NS first, branch open', 124 'sort' => 'ns_title', 125 'usetitle' => true, 126 'home' => false, 127 'current' => 'simplenavi:namespace123:deep:foo', 128 'expect' => [ 129 '+simplenavi:namespace123:namespace123', 130 '++simplenavi:namespace123:deep:start', 131 '+++simplenavi:namespace123:deep:foo', 132 '++simplenavi:namespace123:foo', 133 '+simplenavi:namespace2', 134 '+simplenavi:namespace12:start', 135 '+simplenavi:namespace21:start', 136 '+simplenavi:namespace1:start', 137 '+simplenavi:foo', 138 ] 139 ]; 140 141 yield [ 142 'set' => 'by ID, branch open with home level', 143 'sort' => 'id', 144 'usetitle' => false, 145 'home' => true, 146 'current' => 'simplenavi:namespace123:deep:foo', 147 'expect' => [ 148 '+simplenavi:simplenavi', 149 '++simplenavi:foo', 150 '++simplenavi:namespace1:start', 151 '++simplenavi:namespace12:start', 152 '++simplenavi:namespace123:namespace123', 153 '+++simplenavi:namespace123:deep:start', 154 '++++simplenavi:namespace123:deep:foo', 155 '+++simplenavi:namespace123:foo', 156 '++simplenavi:namespace2', 157 '++simplenavi:namespace21:start', 158 ] 159 ]; 160 } 161 162 /** 163 * @dataProvider dataProvider 164 */ 165 public function testSorting($set, $sort, $usetitle, $home, $current, $expect) 166 { 167 $simpleNavi = new \syntax_plugin_simplenavi(); 168 169 $simpleNavi->initState('simplenavi', $current, $usetitle, $sort, $home); 170 171 /** @var PageTreeBuilder $tree */ 172 $tree = $this->callInaccessibleMethod($simpleNavi, 'getTree', []); 173 174 175 $this->assertSame(join("\n", $expect), (string) $tree, $set); 176 } 177 178 /** 179 * Data provider for isParent test 180 */ 181 public function isParentDataProvider(): array 182 { 183 return [ 184 // Test cases where parent is a parent of child 185 'parent of deep child' => [true, 'namespace1:namespace2:page', 'namespace1'], 186 'direct parent' => [true, 'namespace1:namespace2:page', 'namespace1:namespace2'], 187 'parent of page' => [true, 'namespace1:page', 'namespace1'], 188 189 // Test cases where parent is not a parent of child 190 'different namespace' => [false, 'namespace1:page', 'namespace2'], 191 'sibling namespace' => [false, 'namespace1:namespace2:page', 'namespace1:namespace3'], 192 193 // Test edge cases 194 'empty parent' => [true, 'page', ''], // Empty parent is parent of all 195 'self as parent' => [true, 'page', 'page'], // Page is parent of itself 196 ]; 197 } 198 199 /** 200 * Test the isParent method 201 * @dataProvider isParentDataProvider 202 */ 203 public function testIsParent(bool $expected, string $child, string $parent): void 204 { 205 $simpleNavi = new \syntax_plugin_simplenavi(); 206 $result = $this->callInaccessibleMethod($simpleNavi, 'isParent', [$child, $parent]); 207 $this->assertSame($expected, $result); 208 } 209 210} 211 212 213 214