Merge pull request #4352 from eduardomozart/patch-7
[dokuwiki.git] / _test / tests / inc / media_searchlist.test.php
blob75514b34a3885c9a4fdede1c7971e00fe6694d64
1 <?php
3 use DOMWrap\Document;
5 class media_searchlist_test extends DokuWikiTest
8 /**
9 * @var string namespace used for testing
11 protected $upload_ns = 'media_searchlist_test';
13 /**
14 * Save the file
16 * @param string $name name of saving file
17 * @param string $copy file used as a content of uploaded file
19 protected function save($name, $copy)
21 $media_id = $this->upload_ns . ':' . $name;
22 media_save(array('name' => $copy), $media_id, true, AUTH_UPLOAD, 'copy');
25 /**
26 * Called for each test
28 * @throws Exception
30 function setUp() : void
32 parent::setUp();
34 //create some files to search
35 $png = mediaFN('wiki:kind_zu_katze.png');
36 $ogv = mediaFN('wiki:kind_zu_katze.ogv');
37 $webm = mediaFN('wiki:kind_zu_katze.webm');
39 $this->save('a.png', $png);
40 $this->save('aa.png', $png);
41 $this->save('ab.png', $png);
43 $this->save('a.ogv', $ogv);
44 $this->save('aa.ogv', $ogv);
45 $this->save('ab.ogv', $ogv);
47 $this->save('a:a.png', $png);
48 $this->save('b:a.png', $png);
50 $this->save('0.webm', $webm);
54 /**
55 * Wrap around media_searchlist: return the result
56 * Reset media_printfile static variables afterwards
58 * @param $query
59 * @param $ns
60 * @return string
62 protected function media_searchlist($query, $ns)
64 ob_start();
65 media_searchlist($query, $ns);
66 $out = ob_get_contents();
67 ob_end_clean();
68 return $out;
71 /**
72 * @return array[]
73 * @see testSearch
75 public function provideSearch()
77 return [
78 ['a.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png']], // no globbing
79 ['a*.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png', 'ab.png']], // globbing asterisk
80 ['*.ogv', ['a.ogv', 'aa.ogv', 'ab.ogv']], // globbing find by ext
81 ['a?.png', ['aa.png', 'ab.png']], // globbing question mark
82 ['a?.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing question mark and asterisk
83 ['?.png', ['a:a.png', 'b:a.png', 'a.png']], // globbing question mark on the beginning
84 ['??.png', ['aa.png', 'ab.png']], // globbing two question marks on the beginning
85 ['??.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing two letter file names
86 ['0', ['0.webm']], // zero search
90 /**
91 * @dataProvider provideSearch
92 * @param string $query The query to use
93 * @param string[] $expected The expected media IDs in the result HTML
94 * @throws Exception
96 public function testSearch($query, $expected)
98 $result = $this->media_searchlist($query, $this->upload_ns);
99 $pq = (new Document())->html($result);
101 $elements = $pq->find('a.mediafile');
102 $actual = [];
103 foreach ($elements as $element) {
104 $actual[] = $element->textContent;
107 $this->assertEquals(count($expected), count($elements));
108 $this->assertEquals($expected, $actual);