1<?php 2 3class media_searchlist_test extends DokuWikiTest { 4 5 /** 6 * @var string namespace used for testing 7 */ 8 protected $upload_ns = 'media_searchlist_test'; 9 10 /** 11 * Save the file 12 * 13 * @param $name name of saving file 14 * @param $copy file used as a content of uploaded file 15 */ 16 protected function save($name, $copy) { 17 $media_id = $this->upload_ns.':'.$name; 18 media_save(array('name' => $copy), $media_id, true, AUTH_UPLOAD, 'copy'); 19 } 20 21 /** 22 * Called for each test 23 * 24 * @throws Exception 25 */ 26 function setUp() { 27 //create some files to search 28 $png = mediaFN('wiki:kind_zu_katze.png'); 29 $ogv = mediaFN('wiki:kind_zu_katze.ogv'); 30 $webm = mediaFN('wiki:kind_zu_katze.webm'); 31 32 $this->save('a.png', $png); 33 $this->save('aa.png', $png); 34 $this->save('ab.png', $png); 35 36 $this->save('a.ogv', $ogv); 37 $this->save('aa.ogv', $ogv); 38 $this->save('ab.ogv', $ogv); 39 40 $this->save('a:a.png', $png); 41 $this->save('b:a.png', $png); 42 43 $this->save('0.webm', $webm); 44 45 } 46 47 /* 48 * Reset media_printfile static variable $twibble to stat state 49 */ 50 protected function reset_media_printfile() { 51 $reflect = new ReflectionFunction('media_printfile'); 52 $static = $reflect->getStaticVariables(); 53 if ($static['twibble'] == -1) { 54 ob_start(); 55 @media_printfile(array(), 0, ''); 56 ob_end_clean(); 57 } 58 } 59 60 /** 61 * Build search result header as in media_searchlist() with $fullscreen = false 62 * 63 * @param $query search query 64 * @param $ns namespece where we search 65 * 66 * @return string 67 */ 68 protected function media_searchlist_header($query, $ns) { 69 global $lang; 70 71 $header = '<h1 id="media__ns">'.sprintf($lang['searchmedia_in'],hsc($ns).':*').'</h1>'.NL; 72 ob_start(); 73 media_searchform($ns,$query); 74 $header .= ob_get_contents(); 75 ob_end_clean(); 76 77 return $header; 78 } 79 80 /** 81 * Wrap around media_printfile: return the result. 82 * 83 * @param $item 84 * @return string 85 */ 86 protected function media_printfile($item) { 87 ob_start(); 88 media_printfile($item,$item['perm'],'',true); 89 $out = ob_get_contents(); 90 ob_end_clean(); 91 92 return $out; 93 } 94 95 /** 96 * Wrap around media_searchlist: return the result 97 * Reset media_printfile static variables afterwards 98 * 99 * @param $query 100 * @param $ns 101 * @return string 102 */ 103 protected function media_searchlist($query, $ns) { 104 ob_start(); 105 media_searchlist($query, $ns); 106 $out = ob_get_contents(); 107 ob_end_clean(); 108 109 //reset media_printfile static variables 110 $this->reset_media_printfile(); 111 112 return $out; 113 } 114 115 /** 116 * 117 * @param array[string] $rel_ids media ids relative to $this->upload_ns 118 * @return array $items as required by media_printfile 119 */ 120 protected function create_media_items($rel_ids) { 121 $items = array(); 122 foreach ($rel_ids as $rel_id){ 123 $file = mediaFN($this->upload_ns . ':' . $rel_id); 124 $info = array(); 125 $info['id'] = $this->upload_ns . ':' . $rel_id; 126 $info['perm'] = auth_quickaclcheck(getNS($info['id']).':*'); 127 $info['file'] = utf8_basename($file); 128 $info['size'] = filesize($file); 129 $info['mtime'] = filemtime($file); 130 $info['writable'] = is_writable($file); 131 if(preg_match("/\.(jpe?g|gif|png)$/",$file)){ 132 $info['isimg'] = true; 133 $info['meta'] = new JpegMeta($file); 134 }else{ 135 $info['isimg'] = false; 136 } 137 $info['hash'] = md5(io_readFile(mediaFN($info['id']),false)); 138 139 $items[] = $info; 140 } 141 return $items; 142 } 143 144 /** 145 * Output result as in 'media_searchlist' but use an arbitrary media IDs list instead of actual searching 146 * Reset media_printfile static variables afterwards 147 * 148 * @param array[string] $rel_ids media ids relative to $this->upload_ns 149 * @param string $query actual seqrch query (used for filling search filed input) 150 * @param string $ns 151 * @return string 152 */ 153 protected function media_searchlist_except($rel_ids, $query, $ns) { 154 //build a search result header 155 $expect = $this->media_searchlist_header($query, $ns); 156 157 //get the items list 158 $items = $this->create_media_items($rel_ids); 159 foreach ($items as $item) { 160 $expect .= $this->media_printfile($item); 161 } 162 163 //reset media_printfile static variables 164 $this->reset_media_printfile(); 165 166 return $expect; 167 } 168 169 public function test_noglobbing(){ 170 $query = 'a.png'; 171 $ns = $this->upload_ns; 172 173 $result = $this->media_searchlist($query, $ns); 174 $expect = $this->media_searchlist_except(array('a:a.png', 'b:a.png', 'a.png', 'aa.png'), $query, $ns); 175 176 $this->assertEquals($expect, $result); 177 } 178 179 public function test_globbing_asterisk(){ 180 $query = 'a*.png'; 181 $ns = $this->upload_ns; 182 183 $result = $this->media_searchlist($query, $ns); 184 $expect = $this->media_searchlist_except(array('a:a.png', 'b:a.png', 'a.png', 'aa.png', 'ab.png'), $query, $ns); 185 186 $this->assertEquals($expect, $result); 187 } 188 189 public function test_globbing_find_by_ext(){ 190 $query = '*.ogv'; 191 $ns = $this->upload_ns; 192 193 $result = $this->media_searchlist($query, $ns); 194 $expect = $this->media_searchlist_except(array('a.ogv', 'aa.ogv', 'ab.ogv'), $query, $ns); 195 196 $this->assertEquals($expect, $result); 197 } 198 199 public function test_globbing_question_mark(){ 200 $query = 'a?.png'; 201 $ns = $this->upload_ns; 202 203 $result = $this->media_searchlist($query, $ns); 204 $expect = $this->media_searchlist_except(array('aa.png', 'ab.png'), $query, $ns); 205 206 $this->assertEquals($expect, $result); 207 } 208 209 public function test_globbing_question_mark_and_asterisk(){ 210 $query = 'a?.*'; 211 $ns = $this->upload_ns; 212 213 $result = $this->media_searchlist($query, $ns); 214 $expect = $this->media_searchlist_except(array('aa.ogv', 'aa.png', 'ab.ogv', 'ab.png'), $query, $ns); 215 216 $this->assertEquals($expect, $result); 217 } 218 219 public function test_globbing_question_mark_on_the_begining(){ 220 $query = '?.png'; 221 $ns = $this->upload_ns; 222 223 $result = $this->media_searchlist($query, $ns); 224 $expect = $this->media_searchlist_except(array('a:a.png', 'b:a.png', 'a.png'), $query, $ns); 225 226 $this->assertEquals($expect, $result); 227 } 228 229 public function test_globbing_two_question_marks_on_the_begining(){ 230 $query = '??.png'; 231 $ns = $this->upload_ns; 232 233 $result = $this->media_searchlist($query, $ns); 234 $expect = $this->media_searchlist_except(array('aa.png', 'ab.png'), $query, $ns); 235 236 $this->assertEquals($expect, $result); 237 } 238 239 public function test_globbing_two_letter_file_names(){ 240 $query = '??.*'; 241 $ns = $this->upload_ns; 242 243 $result = $this->media_searchlist($query, $ns); 244 $expect = $this->media_searchlist_except(array('aa.ogv', 'aa.png', 'ab.ogv', 'ab.png'), $query, $ns); 245 246 $this->assertEquals($expect, $result); 247 } 248 249 public function test_zero_search(){ 250 $query = '0'; 251 $ns = $this->upload_ns; 252 253 $result = $this->media_searchlist($query, $ns); 254 $expect = $this->media_searchlist_except(array('0.webm'), $query, $ns); 255 256 $this->assertEquals($expect, $result); 257 } 258} 259