namespace_includes.test.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. if (!defined('DOKU_INC')) die();
  3. /**
  4. * Test namespace includes
  5. *
  6. * @group plugin_include
  7. * @group plugins
  8. */
  9. class plugin_include_namespaces_includes_test extends DokuWikiTest {
  10. /**
  11. * @var helper_plugin_include $helper
  12. */
  13. private $helper;
  14. /**
  15. * Setup - enable and load the include plugin and create the test pages
  16. */
  17. public function setUp() : void
  18. {
  19. $this->pluginsEnabled[] = 'include';
  20. parent::setUp(); // this enables the include plugin
  21. $this->helper = plugin_load('helper', 'include');
  22. global $conf;
  23. $conf['hidepages'] = 'inclhidden:hidden';
  24. // for testing hidden pages
  25. saveWikiText('inclhidden:hidden', 'Hidden page', 'Created hidden page');
  26. saveWikiText('inclhidden:visible', 'Visible page', 'Created visible page');
  27. // pages on different levels
  28. saveWikiText('incltest:level1', 'Page on level 1', 'Created page on level 1');
  29. saveWikiText('incltest:ns:level2', 'Page on level 2', 'Created page on level 2');
  30. saveWikiText('incltest:ns:ns:level3', 'Page on level 3', 'Created page on level 3');
  31. // for page ordering
  32. saveWikiText('inclorder:page1', 'Page 1', 'Created page 1');
  33. saveWikiText('inclorder:page2', 'Page 2', 'Created page 2');
  34. saveWikiText('inclorder:page3', '{{include_n>10}} Page 3/10', 'created page 3/1');
  35. saveWikiText('inclorder:page4', '{{include_n>2}} Page 4/2', 'created page 4/0');
  36. }
  37. /**
  38. * Helper function to read dir content
  39. */
  40. protected function getDirContent ($dir) {
  41. if (is_dir($dir)) {
  42. $pages = array();
  43. if ($handle = opendir($dir)) {
  44. while (($file = readdir($handle)) !== false) {
  45. if ($file != '.' && $file != '..') {
  46. $pages [] = $file;
  47. }
  48. }
  49. closedir($handle);
  50. return $pages;
  51. }
  52. }
  53. return null;
  54. }
  55. /**
  56. * Test hiding of hidden pages in namespace includes
  57. */
  58. public function test_hidden() {
  59. $flags = $this->helper->get_flags(array());
  60. $pages = $this->helper->_get_included_pages('namespace', 'inclhidden:', '', '', $flags);
  61. $this->assertEquals(array(
  62. array('id' => 'inclhidden:visible', 'exists' => true, 'parent_id' => ''),
  63. ), $pages);
  64. }
  65. /**
  66. * Test include depth limit
  67. */
  68. public function test_depth() {
  69. $flags = $this->helper->get_flags(array());
  70. $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags);
  71. $this->assertEquals(array(
  72. array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
  73. ), $pages);
  74. $flags = $this->helper->get_flags(array('depth=2'));
  75. $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags);
  76. $this->assertEquals(array(
  77. array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
  78. array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''),
  79. ), $pages);
  80. $flags = $this->helper->get_flags(array('depth=2'));
  81. $pages = $this->helper->_get_included_pages('namespace', 'incltest:ns', '', '', $flags);
  82. $this->assertEquals(array(
  83. array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''),
  84. array('id' => 'incltest:ns:ns:level3', 'exists' => true, 'parent_id' => ''),
  85. ), $pages);
  86. $flags = $this->helper->get_flags(array('depth=0'));
  87. $pages = $this->helper->_get_included_pages('namespace', 'incltest:', '', '', $flags);
  88. $this->assertEquals(array(
  89. array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
  90. array('id' => 'incltest:ns:level2', 'exists' => true, 'parent_id' => ''),
  91. array('id' => 'incltest:ns:ns:level3', 'exists' => true, 'parent_id' => ''),
  92. ), $pages);
  93. // test include of the root namespace
  94. $flags = $this->helper->get_flags(array());
  95. $pages = $this->helper->_get_included_pages('namespace', ':', '', '', $flags);
  96. $this->assertEquals(array(array('id' => 'mailinglist', 'exists' => true, 'parent_id' => '')), $pages);
  97. $flags = $this->helper->get_flags(array('depth=2'));
  98. $pages = $this->helper->_get_included_pages('namespace', ':', '', '', $flags);
  99. $expected = array(
  100. array('id' => 'inclhidden:visible', 'exists' => true, 'parent_id' => ''),
  101. array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
  102. array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
  103. array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
  104. array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
  105. array('id' => 'incltest:level1', 'exists' => true, 'parent_id' => ''),
  106. array('id' => 'mailinglist', 'exists' => true, 'parent_id' => ''),
  107. //array('id' => 'wiki:dokuwiki', 'exists' => true, 'parent_id' => ''),
  108. //array('id' => 'wiki:syntax', 'exists' => true, 'parent_id' => ''),
  109. //$wikiPages,
  110. );
  111. // page int:editandsavetest exists in DokuWiki after September 2017
  112. if (page_exists('int:editandsavetest')) {
  113. $expected [] = array('id' => 'int:editandsavetest', 'exists' => true, 'parent_id' => '');
  114. }
  115. // Add pages in namespace wiki
  116. $dir = $this->getDirContent(dirname(__FILE__).'/../../../../_test/data/pages/wiki');
  117. $this->assertTrue($dir !== null);
  118. foreach ($dir as $page) {
  119. $page = substr($page, 0, -4);
  120. $expected [] = array('id' => 'wiki:'.$page, 'exists' => true, 'parent_id' => '');
  121. }
  122. array_multisort($expected);
  123. array_multisort($pages);
  124. $this->assertEquals($expected, $pages);
  125. }
  126. /**
  127. * Test ordering of namespace includes
  128. */
  129. public function test_order() {
  130. $flags = $this->helper->get_flags(array());
  131. $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
  132. $this->assertEquals(array(
  133. array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
  134. array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
  135. array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
  136. array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
  137. ), $pages);
  138. $flags = $this->helper->get_flags(array('rsort'));
  139. $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
  140. $this->assertEquals(array(
  141. array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
  142. array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
  143. array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
  144. array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
  145. ), $pages);
  146. $flags = $this->helper->get_flags(array('order=custom'));
  147. $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
  148. $this->assertEquals(array(
  149. array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
  150. array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
  151. array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
  152. array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
  153. ), $pages);
  154. $flags = $this->helper->get_flags(array('order=custom', 'rsort'));
  155. $pages = $this->helper->_get_included_pages('namespace', 'inclorder:', '', '', $flags);
  156. $this->assertEquals(array(
  157. array('id' => 'inclorder:page2', 'exists' => true, 'parent_id' => ''),
  158. array('id' => 'inclorder:page1', 'exists' => true, 'parent_id' => ''),
  159. array('id' => 'inclorder:page3', 'exists' => true, 'parent_id' => ''),
  160. array('id' => 'inclorder:page4', 'exists' => true, 'parent_id' => ''),
  161. ), $pages);
  162. }
  163. }