1164faf82SSzymon Olewniczak<?php 2164faf82SSzymon Olewniczak 3*17409362SAndreas Gohrclass media_searchlist_test extends DokuWikiTest 4*17409362SAndreas Gohr{ 5164faf82SSzymon Olewniczak 6164faf82SSzymon Olewniczak /** 7164faf82SSzymon Olewniczak * @var string namespace used for testing 8164faf82SSzymon Olewniczak */ 9164faf82SSzymon Olewniczak protected $upload_ns = 'media_searchlist_test'; 10164faf82SSzymon Olewniczak 11164faf82SSzymon Olewniczak /** 12164faf82SSzymon Olewniczak * Save the file 13164faf82SSzymon Olewniczak * 14*17409362SAndreas Gohr * @param string $name name of saving file 15*17409362SAndreas Gohr * @param string $copy file used as a content of uploaded file 16164faf82SSzymon Olewniczak */ 17*17409362SAndreas Gohr protected function save($name, $copy) 18*17409362SAndreas Gohr { 19164faf82SSzymon Olewniczak $media_id = $this->upload_ns . ':' . $name; 20164faf82SSzymon Olewniczak media_save(array('name' => $copy), $media_id, true, AUTH_UPLOAD, 'copy'); 21164faf82SSzymon Olewniczak } 22164faf82SSzymon Olewniczak 23164faf82SSzymon Olewniczak /** 24164faf82SSzymon Olewniczak * Called for each test 25164faf82SSzymon Olewniczak * 26164faf82SSzymon Olewniczak * @throws Exception 27164faf82SSzymon Olewniczak */ 28*17409362SAndreas Gohr public function setUp() 29*17409362SAndreas Gohr { 30*17409362SAndreas Gohr parent::setUp(); 31*17409362SAndreas Gohr 32164faf82SSzymon Olewniczak //create some files to search 33164faf82SSzymon Olewniczak $png = mediaFN('wiki:kind_zu_katze.png'); 34164faf82SSzymon Olewniczak $ogv = mediaFN('wiki:kind_zu_katze.ogv'); 35164faf82SSzymon Olewniczak $webm = mediaFN('wiki:kind_zu_katze.webm'); 36164faf82SSzymon Olewniczak 37164faf82SSzymon Olewniczak $this->save('a.png', $png); 38164faf82SSzymon Olewniczak $this->save('aa.png', $png); 39164faf82SSzymon Olewniczak $this->save('ab.png', $png); 40164faf82SSzymon Olewniczak 41164faf82SSzymon Olewniczak $this->save('a.ogv', $ogv); 42164faf82SSzymon Olewniczak $this->save('aa.ogv', $ogv); 43164faf82SSzymon Olewniczak $this->save('ab.ogv', $ogv); 44164faf82SSzymon Olewniczak 45164faf82SSzymon Olewniczak $this->save('a:a.png', $png); 46164faf82SSzymon Olewniczak $this->save('b:a.png', $png); 47164faf82SSzymon Olewniczak 48164faf82SSzymon Olewniczak $this->save('0.webm', $webm); 49164faf82SSzymon Olewniczak 50164faf82SSzymon Olewniczak } 51164faf82SSzymon Olewniczak 52164faf82SSzymon Olewniczak /** 53164faf82SSzymon Olewniczak * Wrap around media_searchlist: return the result 54164faf82SSzymon Olewniczak * Reset media_printfile static variables afterwards 55164faf82SSzymon Olewniczak * 56164faf82SSzymon Olewniczak * @param $query 57164faf82SSzymon Olewniczak * @param $ns 58164faf82SSzymon Olewniczak * @return string 59164faf82SSzymon Olewniczak */ 60*17409362SAndreas Gohr protected function media_searchlist($query, $ns) 61*17409362SAndreas Gohr { 62164faf82SSzymon Olewniczak ob_start(); 63164faf82SSzymon Olewniczak media_searchlist($query, $ns); 64164faf82SSzymon Olewniczak $out = ob_get_contents(); 65164faf82SSzymon Olewniczak ob_end_clean(); 66164faf82SSzymon Olewniczak return $out; 67164faf82SSzymon Olewniczak } 68164faf82SSzymon Olewniczak 69164faf82SSzymon Olewniczak /** 70*17409362SAndreas Gohr * @return array[] 71*17409362SAndreas Gohr * @see testSearch 72164faf82SSzymon Olewniczak */ 73*17409362SAndreas Gohr public function provideSearch() 74*17409362SAndreas Gohr { 75*17409362SAndreas Gohr return [ 76*17409362SAndreas Gohr ['a.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png']], // no globbing 77*17409362SAndreas Gohr ['a*.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png', 'ab.png']], // globbing asterisk 78*17409362SAndreas Gohr ['*.ogv', ['a.ogv', 'aa.ogv', 'ab.ogv']], // globbing find by ext 79*17409362SAndreas Gohr ['a?.png', ['aa.png', 'ab.png']], // globbing question mark 80*17409362SAndreas Gohr ['a?.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing question mark and asterisk 81*17409362SAndreas Gohr ['?.png', ['a:a.png', 'b:a.png', 'a.png']], // globbing question mark on the beginning 82*17409362SAndreas Gohr ['??.png', ['aa.png', 'ab.png']], // globbing two question marks on the beginning 83*17409362SAndreas Gohr ['??.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing two letter file names 84*17409362SAndreas Gohr ['0', ['0.webm']], // zero search 85*17409362SAndreas Gohr ]; 86164faf82SSzymon Olewniczak } 87164faf82SSzymon Olewniczak 88164faf82SSzymon Olewniczak /** 89*17409362SAndreas Gohr * @dataProvider provideSearch 90*17409362SAndreas Gohr * @param string $query The query to use 91*17409362SAndreas Gohr * @param string[] $expected The expected media IDs in the result HTML 92*17409362SAndreas Gohr * @throws Exception 93164faf82SSzymon Olewniczak */ 94*17409362SAndreas Gohr public function testSearch($query, $expected) 95*17409362SAndreas Gohr { 96*17409362SAndreas Gohr $result = $this->media_searchlist($query, $this->upload_ns); 97*17409362SAndreas Gohr $pq = phpQuery::newDocument($result); 98164faf82SSzymon Olewniczak 99*17409362SAndreas Gohr $elements = $pq->find('a.mediafile'); 100*17409362SAndreas Gohr $actual = []; 101*17409362SAndreas Gohr foreach ($elements as $element) { 102*17409362SAndreas Gohr $actual[] = $element->textContent; 103164faf82SSzymon Olewniczak } 104164faf82SSzymon Olewniczak 105*17409362SAndreas Gohr $this->assertEquals(count($expected), count($elements)); 106*17409362SAndreas Gohr $this->assertEquals($expected, $actual); 107164faf82SSzymon Olewniczak } 108164faf82SSzymon Olewniczak 109164faf82SSzymon Olewniczak} 110