1<?php 2 3namespace dokuwiki\plugin\filelist\test; 4 5use DokuWikiTest; 6use DOMWrap\Document; 7 8 9/** 10 * Tests for the filelist plugin. 11 * 12 * These test assume that the directory filelist has the following content: 13 * - exampledir (directory) 14 * - example2.txt (text file) 15 * - example.txt (text file) 16 * - exampleimage.png (image file) 17 * 18 * @group plugin_filelist 19 * @group plugins 20 */ 21class plugin_filelist_test extends DokuWikiTest 22{ 23 24 public function setUp(): void 25 { 26 global $conf; 27 28 $this->pluginsEnabled[] = 'filelist'; 29 parent::setUp(); 30 31 // Setup config so that access to the TMP directory will be allowed 32 $conf ['plugin']['filelist']['paths'] = TMP_DIR . '/filelistdata/' . "\n" . 'W> http://localhost/'; 33 34 } 35 36 public static function setUpBeforeClass(): void 37 { 38 parent::setUpBeforeClass(); 39 40 // copy test files to test directory 41 \TestUtils::rcopy(TMP_DIR, dirname(__FILE__) . '/filelistdata'); 42 } 43 44 /** 45 * Run a list of checks on the given document 46 * 47 * @param Document $doc 48 * @param array $structure Array of selectors and expected count or content 49 * @return void 50 */ 51 protected function structureCheck(Document $doc, $structure) 52 { 53 foreach ($structure as $selector => $expected) { 54 if (is_numeric($expected)) { 55 $this->assertEquals( 56 $expected, 57 $doc->find($selector)->count(), 58 'Selector ' . $selector . ' not found' 59 ); 60 } else { 61 $this->assertStringContainsString( 62 $expected, 63 $doc->find($selector)->text(), 64 'Selector ' . $selector . ' not found' 65 ); 66 }; 67 } 68 } 69 70 71 /** 72 * This function checks that all files are listed in not recursive mode. 73 */ 74 public function test_not_recursive() 75 { 76 global $conf; 77 78 // Render filelist 79 $instructions = p_get_instructions('{{filelist>' . TMP_DIR . '/filelistdata/*&style=list&direct=1}}'); 80 $xhtml = p_render('xhtml', $instructions, $info); 81 82 // We should find: 83 // - example.txt 84 // - exampleimage.png 85 $result = strpos($xhtml, 'example.txt'); 86 $this->assertFalse($result === false, '"example.txt" not listed'); 87 $result = strpos($xhtml, 'exampleimage.png'); 88 $this->assertFalse($result === false, '"exampleimage.png" not listed'); 89 } 90 91 /** 92 * This function checks that all files are listed in recursive mode. 93 */ 94 public function test_recursive() 95 { 96 // Render filelist 97 $instructions = p_get_instructions('{{filelist>' . TMP_DIR . '/filelistdata/*&style=list&direct=1&recursive=1}}'); 98 $xhtml = p_render('xhtml', $instructions, $info); 99 100 // We should find: 101 // - exampledir 102 // - example2.txt 103 // - example.txt 104 // - exampleimage.png 105 $result = strpos($xhtml, 'exampledir'); 106 $this->assertFalse($result === false, '"exampledir" not listed'); 107 $result = strpos($xhtml, 'example2.txt'); 108 $this->assertFalse($result === false, '"example2.txt" not listed'); 109 $result = strpos($xhtml, 'example.txt'); 110 $this->assertFalse($result === false, '"example.txt" not listed'); 111 $result = strpos($xhtml, 'exampleimage.png'); 112 $this->assertFalse($result === false, '"exampleimage.png" not listed'); 113 } 114 115 /** 116 * This function checks that the unordered list mode 117 * generates the expected XHTML structure. 118 */ 119 public function testUnorderedList() 120 { 121 // Render filelist 122 $instructions = p_get_instructions('{{filelist>' . TMP_DIR . '/filelistdata/*&style=list&direct=1&recursive=1}}'); 123 $xhtml = p_render('xhtml', $instructions, $info); 124 125 $doc = new Document(); 126 $doc->html($xhtml); 127 128 $structure = [ 129 'div.filelist-plugin' => 1, 130 'div.filelist-plugin > ul' => 1, 131 'div.filelist-plugin > ul > li' => 3, 132 'div.filelist-plugin > ul > li:nth-child(1)' => 1, 133 'div.filelist-plugin > ul > li:nth-child(1) a' => 'example.txt', 134 'div.filelist-plugin > ul > li:nth-child(2) ul' => 1, 135 'div.filelist-plugin > ul > li:nth-child(2) ul > li' => 1, 136 'div.filelist-plugin > ul > li:nth-child(2) ul > li a' => 'example2.txt', 137 ]; 138 139 $this->structureCheck($doc, $structure); 140 } 141 142 /** 143 * This function checks that the ordered list mode 144 * generates the expected XHTML structure. 145 */ 146 public function testOrderedList() 147 { 148 // Render filelist 149 $instructions = p_get_instructions('{{filelist>' . TMP_DIR . '/filelistdata/*&style=olist&direct=1&recursive=1}}'); 150 $xhtml = p_render('xhtml', $instructions, $info); 151 152 $doc = new Document(); 153 $doc->html($xhtml); 154 155 $structure = [ 156 'div.filelist-plugin' => 1, 157 'div.filelist-plugin > ol' => 1, 158 'div.filelist-plugin > ol > li' => 3, 159 'div.filelist-plugin > ol > li:nth-child(1)' => 1, 160 'div.filelist-plugin > ol > li:nth-child(1) a' => 'example.txt', 161 'div.filelist-plugin > ol > li:nth-child(2) ol' => 1, 162 'div.filelist-plugin > ol > li:nth-child(2) ol > li' => 1, 163 'div.filelist-plugin > ol > li:nth-child(2) ol > li a' => 'example2.txt', 164 ]; 165 166 $this->structureCheck($doc, $structure); 167 } 168 169 /** 170 * This function checks that the table mode 171 * generates the expected XHTML structure. 172 */ 173 public function test_table() 174 { 175 global $conf; 176 177 // Render filelist 178 $instructions = p_get_instructions('{{filelist>' . TMP_DIR . '/filelistdata/*&style=table&direct=1&recursive=1}}'); 179 $xhtml = p_render('xhtml', $instructions, $info); 180 181 $doc = new Document(); 182 $doc->html($xhtml); 183 184 $structure = [ 185 'div.filelist-plugin' => 1, 186 'div.filelist-plugin table' => 1, 187 'div.filelist-plugin table > tbody > tr' => 3, 188 'div.filelist-plugin table > tbody > tr:nth-child(1) a' => 'example.txt', 189 'div.filelist-plugin table > tbody > tr:nth-child(2) a' => 'exampledir/example2.txt', 190 'div.filelist-plugin table > tbody > tr:nth-child(3) a' => 'exampleimage.png', 191 ]; 192 193 $this->structureCheck($doc, $structure); 194 } 195} 196