1<?php
2
3use DOMWrap\Document;
4
5class media_searchlist_test extends DokuWikiTest
6{
7
8    /**
9     * @var string namespace used for testing
10     */
11    protected $upload_ns = 'media_searchlist_test';
12
13    /**
14     * Save the file
15     *
16     * @param string $name name of saving file
17     * @param string $copy file used as a content of uploaded file
18     */
19    protected function save($name, $copy)
20    {
21        $media_id = $this->upload_ns . ':' . $name;
22        media_save(array('name' => $copy), $media_id, true, AUTH_UPLOAD, 'copy');
23    }
24
25    /**
26     * Called for each test
27     *
28     * @throws Exception
29     */
30    function setUp() : void
31    {
32        parent::setUp();
33
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');
38
39        $this->save('a.png', $png);
40        $this->save('aa.png', $png);
41        $this->save('ab.png', $png);
42
43        $this->save('a.ogv', $ogv);
44        $this->save('aa.ogv', $ogv);
45        $this->save('ab.ogv', $ogv);
46
47        $this->save('a:a.png', $png);
48        $this->save('b:a.png', $png);
49
50        $this->save('0.webm', $webm);
51
52    }
53
54    /**
55     * Wrap around media_searchlist: return the result
56     * Reset media_printfile static variables afterwards
57     *
58     * @param $query
59     * @param $ns
60     * @return string
61     */
62    protected function media_searchlist($query, $ns)
63    {
64        ob_start();
65        media_searchlist($query, $ns);
66        $out = ob_get_contents();
67        ob_end_clean();
68        return $out;
69    }
70
71    /**
72     * @return array[]
73     * @see testSearch
74     */
75    public function provideSearch()
76    {
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
87        ];
88    }
89
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
95     */
96    public function testSearch($query, $expected)
97    {
98        $result = $this->media_searchlist($query, $this->upload_ns);
99        $pq = (new Document())->html($result);
100
101        $elements = $pq->find('a.mediafile');
102        $actual = [];
103        foreach ($elements as $element) {
104            $actual[] = $element->textContent;
105        }
106
107        $this->assertEquals(count($expected), count($elements));
108        $this->assertEquals($expected, $actual);
109    }
110
111}
112