xref: /dokuwiki/_test/tests/inc/media_searchlist.test.php (revision acdf738a61532360131323876e96f504db0a2b0a)
1164faf82SSzymon Olewniczak<?php
2164faf82SSzymon Olewniczak
3*acdf738aSAndreas Gohruse DOMWrap\Document;
4*acdf738aSAndreas Gohr
517409362SAndreas Gohrclass media_searchlist_test extends DokuWikiTest
617409362SAndreas Gohr{
7164faf82SSzymon Olewniczak
8164faf82SSzymon Olewniczak    /**
9164faf82SSzymon Olewniczak     * @var string namespace used for testing
10164faf82SSzymon Olewniczak     */
11164faf82SSzymon Olewniczak    protected $upload_ns = 'media_searchlist_test';
12164faf82SSzymon Olewniczak
13164faf82SSzymon Olewniczak    /**
14164faf82SSzymon Olewniczak     * Save the file
15164faf82SSzymon Olewniczak     *
1617409362SAndreas Gohr     * @param string $name name of saving file
1717409362SAndreas Gohr     * @param string $copy file used as a content of uploaded file
18164faf82SSzymon Olewniczak     */
1917409362SAndreas Gohr    protected function save($name, $copy)
2017409362SAndreas Gohr    {
21164faf82SSzymon Olewniczak        $media_id = $this->upload_ns . ':' . $name;
22164faf82SSzymon Olewniczak        media_save(array('name' => $copy), $media_id, true, AUTH_UPLOAD, 'copy');
23164faf82SSzymon Olewniczak    }
24164faf82SSzymon Olewniczak
25164faf82SSzymon Olewniczak    /**
26164faf82SSzymon Olewniczak     * Called for each test
27164faf82SSzymon Olewniczak     *
28164faf82SSzymon Olewniczak     * @throws Exception
29164faf82SSzymon Olewniczak     */
30dfe72b68SAndreas Gohr    function setUp() : void
3117409362SAndreas Gohr    {
3217409362SAndreas Gohr        parent::setUp();
3317409362SAndreas Gohr
34164faf82SSzymon Olewniczak        //create some files to search
35164faf82SSzymon Olewniczak        $png = mediaFN('wiki:kind_zu_katze.png');
36164faf82SSzymon Olewniczak        $ogv = mediaFN('wiki:kind_zu_katze.ogv');
37164faf82SSzymon Olewniczak        $webm = mediaFN('wiki:kind_zu_katze.webm');
38164faf82SSzymon Olewniczak
39164faf82SSzymon Olewniczak        $this->save('a.png', $png);
40164faf82SSzymon Olewniczak        $this->save('aa.png', $png);
41164faf82SSzymon Olewniczak        $this->save('ab.png', $png);
42164faf82SSzymon Olewniczak
43164faf82SSzymon Olewniczak        $this->save('a.ogv', $ogv);
44164faf82SSzymon Olewniczak        $this->save('aa.ogv', $ogv);
45164faf82SSzymon Olewniczak        $this->save('ab.ogv', $ogv);
46164faf82SSzymon Olewniczak
47164faf82SSzymon Olewniczak        $this->save('a:a.png', $png);
48164faf82SSzymon Olewniczak        $this->save('b:a.png', $png);
49164faf82SSzymon Olewniczak
50164faf82SSzymon Olewniczak        $this->save('0.webm', $webm);
51164faf82SSzymon Olewniczak
52164faf82SSzymon Olewniczak    }
53164faf82SSzymon Olewniczak
54164faf82SSzymon Olewniczak    /**
55164faf82SSzymon Olewniczak     * Wrap around media_searchlist: return the result
56164faf82SSzymon Olewniczak     * Reset media_printfile static variables afterwards
57164faf82SSzymon Olewniczak     *
58164faf82SSzymon Olewniczak     * @param $query
59164faf82SSzymon Olewniczak     * @param $ns
60164faf82SSzymon Olewniczak     * @return string
61164faf82SSzymon Olewniczak     */
6217409362SAndreas Gohr    protected function media_searchlist($query, $ns)
6317409362SAndreas Gohr    {
64164faf82SSzymon Olewniczak        ob_start();
65164faf82SSzymon Olewniczak        media_searchlist($query, $ns);
66164faf82SSzymon Olewniczak        $out = ob_get_contents();
67164faf82SSzymon Olewniczak        ob_end_clean();
68164faf82SSzymon Olewniczak        return $out;
69164faf82SSzymon Olewniczak    }
70164faf82SSzymon Olewniczak
71164faf82SSzymon Olewniczak    /**
7217409362SAndreas Gohr     * @return array[]
7317409362SAndreas Gohr     * @see testSearch
74164faf82SSzymon Olewniczak     */
7517409362SAndreas Gohr    public function provideSearch()
7617409362SAndreas Gohr    {
7717409362SAndreas Gohr        return [
7817409362SAndreas Gohr            ['a.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png']], // no globbing
7917409362SAndreas Gohr            ['a*.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png', 'ab.png']], // globbing asterisk
8017409362SAndreas Gohr            ['*.ogv', ['a.ogv', 'aa.ogv', 'ab.ogv']], // globbing find by ext
8117409362SAndreas Gohr            ['a?.png', ['aa.png', 'ab.png']], // globbing question mark
8217409362SAndreas Gohr            ['a?.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing question mark and asterisk
8317409362SAndreas Gohr            ['?.png', ['a:a.png', 'b:a.png', 'a.png']], // globbing question mark on the beginning
8417409362SAndreas Gohr            ['??.png', ['aa.png', 'ab.png']], // globbing two question marks on the beginning
8517409362SAndreas Gohr            ['??.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing two letter file names
8617409362SAndreas Gohr            ['0', ['0.webm']], // zero search
8717409362SAndreas Gohr        ];
88164faf82SSzymon Olewniczak    }
89164faf82SSzymon Olewniczak
90164faf82SSzymon Olewniczak    /**
9117409362SAndreas Gohr     * @dataProvider provideSearch
9217409362SAndreas Gohr     * @param string $query The query to use
9317409362SAndreas Gohr     * @param string[] $expected The expected media IDs in the result HTML
9417409362SAndreas Gohr     * @throws Exception
95164faf82SSzymon Olewniczak     */
9617409362SAndreas Gohr    public function testSearch($query, $expected)
9717409362SAndreas Gohr    {
9817409362SAndreas Gohr        $result = $this->media_searchlist($query, $this->upload_ns);
99*acdf738aSAndreas Gohr        $pq = (new Document())->html($result);
100164faf82SSzymon Olewniczak
10117409362SAndreas Gohr        $elements = $pq->find('a.mediafile');
10217409362SAndreas Gohr        $actual = [];
10317409362SAndreas Gohr        foreach ($elements as $element) {
10417409362SAndreas Gohr            $actual[] = $element->textContent;
105164faf82SSzymon Olewniczak        }
106164faf82SSzymon Olewniczak
10717409362SAndreas Gohr        $this->assertEquals(count($expected), count($elements));
10817409362SAndreas Gohr        $this->assertEquals($expected, $actual);
109164faf82SSzymon Olewniczak    }
110164faf82SSzymon Olewniczak
111164faf82SSzymon Olewniczak}
112