xref: /dokuwiki/_test/tests/inc/media_searchlist.test.php (revision dfe72b68b7a817ac19a9da0844a6e13600e698b0)
1164faf82SSzymon Olewniczak<?php
2164faf82SSzymon Olewniczak
317409362SAndreas Gohrclass media_searchlist_test extends DokuWikiTest
417409362SAndreas 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     *
1417409362SAndreas Gohr     * @param string $name name of saving file
1517409362SAndreas Gohr     * @param string $copy file used as a content of uploaded file
16164faf82SSzymon Olewniczak     */
1717409362SAndreas Gohr    protected function save($name, $copy)
1817409362SAndreas 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*dfe72b68SAndreas Gohr    function setUp() : void
2917409362SAndreas Gohr    {
3017409362SAndreas Gohr        parent::setUp();
3117409362SAndreas 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     */
6017409362SAndreas Gohr    protected function media_searchlist($query, $ns)
6117409362SAndreas 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    /**
7017409362SAndreas Gohr     * @return array[]
7117409362SAndreas Gohr     * @see testSearch
72164faf82SSzymon Olewniczak     */
7317409362SAndreas Gohr    public function provideSearch()
7417409362SAndreas Gohr    {
7517409362SAndreas Gohr        return [
7617409362SAndreas Gohr            ['a.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png']], // no globbing
7717409362SAndreas Gohr            ['a*.png', ['a:a.png', 'b:a.png', 'a.png', 'aa.png', 'ab.png']], // globbing asterisk
7817409362SAndreas Gohr            ['*.ogv', ['a.ogv', 'aa.ogv', 'ab.ogv']], // globbing find by ext
7917409362SAndreas Gohr            ['a?.png', ['aa.png', 'ab.png']], // globbing question mark
8017409362SAndreas Gohr            ['a?.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing question mark and asterisk
8117409362SAndreas Gohr            ['?.png', ['a:a.png', 'b:a.png', 'a.png']], // globbing question mark on the beginning
8217409362SAndreas Gohr            ['??.png', ['aa.png', 'ab.png']], // globbing two question marks on the beginning
8317409362SAndreas Gohr            ['??.*', ['aa.ogv', 'aa.png', 'ab.ogv', 'ab.png']], // globbing two letter file names
8417409362SAndreas Gohr            ['0', ['0.webm']], // zero search
8517409362SAndreas Gohr        ];
86164faf82SSzymon Olewniczak    }
87164faf82SSzymon Olewniczak
88164faf82SSzymon Olewniczak    /**
8917409362SAndreas Gohr     * @dataProvider provideSearch
9017409362SAndreas Gohr     * @param string $query The query to use
9117409362SAndreas Gohr     * @param string[] $expected The expected media IDs in the result HTML
9217409362SAndreas Gohr     * @throws Exception
93164faf82SSzymon Olewniczak     */
9417409362SAndreas Gohr    public function testSearch($query, $expected)
9517409362SAndreas Gohr    {
9617409362SAndreas Gohr        $result = $this->media_searchlist($query, $this->upload_ns);
9717409362SAndreas Gohr        $pq = phpQuery::newDocument($result);
98164faf82SSzymon Olewniczak
9917409362SAndreas Gohr        $elements = $pq->find('a.mediafile');
10017409362SAndreas Gohr        $actual = [];
10117409362SAndreas Gohr        foreach ($elements as $element) {
10217409362SAndreas Gohr            $actual[] = $element->textContent;
103164faf82SSzymon Olewniczak        }
104164faf82SSzymon Olewniczak
10517409362SAndreas Gohr        $this->assertEquals(count($expected), count($elements));
10617409362SAndreas Gohr        $this->assertEquals($expected, $actual);
107164faf82SSzymon Olewniczak    }
108164faf82SSzymon Olewniczak
109164faf82SSzymon Olewniczak}
110