xref: /dokuwiki/_test/tests/inc/pageutils_clean_id.test.php (revision 25fd365a2038255d9dfd20c4ba0770e6cd704e49)
1f8369d7dSTobias Sarnowski<?php
2f8369d7dSTobias Sarnowski
324486f8cSAndreas Gohrclass init_clean_id_test extends DokuWikiTest
424486f8cSAndreas Gohr{
5f8369d7dSTobias Sarnowski
624486f8cSAndreas Gohr    /** @inheritDoc */
71c33cec3SAndreas Gohr    function teardown() : void
824486f8cSAndreas Gohr    {
9f8369d7dSTobias Sarnowski        global $cache_cleanid;
10f8369d7dSTobias Sarnowski        $cache_cleanid = array();
11f8369d7dSTobias Sarnowski    }
12f8369d7dSTobias Sarnowski
1324486f8cSAndreas Gohr    /**
1424486f8cSAndreas Gohr     * DataProvider
1524486f8cSAndreas Gohr     *
1624486f8cSAndreas Gohr     * @return Generator|array
1724486f8cSAndreas Gohr     * @see testCleanId
1824486f8cSAndreas Gohr     */
1924486f8cSAndreas Gohr    public function provideTestData()
2024486f8cSAndreas Gohr    {
2124486f8cSAndreas Gohr        // input, expected, optional options array
2224486f8cSAndreas Gohr        $tests = [
2324486f8cSAndreas Gohr            ['page', 'page'],
2424486f8cSAndreas Gohr            ['pa_ge', 'pa_ge'],
2524486f8cSAndreas Gohr            ['pa%ge', 'pa_ge'],
2624486f8cSAndreas Gohr            ['pa#ge', 'pa_ge'],
2724486f8cSAndreas Gohr            ['pàge', 'page'],
2824486f8cSAndreas Gohr            ['pagĖ', 'page'],
2924486f8cSAndreas Gohr            ['pa$%^*#ge', 'pa_ge'],
3024486f8cSAndreas Gohr            ['*page*', 'page'],
3124486f8cSAndreas Gohr            ['ښ', 'ښ'],
3224486f8cSAndreas Gohr            ['päge', 'paege'],
3324486f8cSAndreas Gohr            ['foo bar', 'foo_bar'],
3424486f8cSAndreas Gohr            ['PÄGÖ', 'paegoe'],
3524486f8cSAndreas Gohr            ['Faß', 'fass'],
3624486f8cSAndreas Gohr            ['ښ侧化并곦  β', 'ښ侧化并곦_β'],
3724486f8cSAndreas Gohr            ['page:page', 'page:page'],
3824486f8cSAndreas Gohr            ['page;page', 'page:page'],
3924486f8cSAndreas Gohr            ['page:page 1.2', 'page:page_1.2'],
4024486f8cSAndreas Gohr            ['page._#!', 'page'],
4124486f8cSAndreas Gohr            ['._#!page', 'page'],
4224486f8cSAndreas Gohr            ['page._#!page', 'page._page'],
4324486f8cSAndreas Gohr            ['ns._#!:page', 'ns:page'],
4424486f8cSAndreas Gohr            ['ns:._#!page', 'ns:page'],
4524486f8cSAndreas Gohr            ['ns._#!ns:page', 'ns._ns:page'],
4624486f8cSAndreas Gohr            ['ns_:page', 'ns:page'],
4724486f8cSAndreas Gohr            ['page...page', 'page...page'],
4824486f8cSAndreas Gohr            ['page---page', 'page---page'],
4924486f8cSAndreas Gohr            ['page___page', 'page_page'],
5024486f8cSAndreas Gohr            ['page_-.page', 'page_-.page'],
5124486f8cSAndreas Gohr            [':page', 'page'],
5224486f8cSAndreas Gohr            [':ns:page', 'ns:page'],
5324486f8cSAndreas Gohr            ['page:', 'page'],
5424486f8cSAndreas Gohr            ['ns:page:', 'ns:page'],
55*25fd365aSAndreas Gohr            ['Łł', 'll'],
56f8369d7dSTobias Sarnowski
5724486f8cSAndreas Gohr            // use-slash handling
5824486f8cSAndreas Gohr            ['page/page', 'page_page', ['useslash' => 0]],
5924486f8cSAndreas Gohr            ['page/page', 'page:page', ['useslash' => 1]],
6024486f8cSAndreas Gohr
6124486f8cSAndreas Gohr            // different sep-char
6224486f8cSAndreas Gohr            ['pa-ge', 'pa-ge', ['sepchar' => '-']],
6324486f8cSAndreas Gohr            ['pa%ge', 'pa-ge', ['sepchar' => '-']],
6424486f8cSAndreas Gohr
6524486f8cSAndreas Gohr            // no deaccenting
6624486f8cSAndreas Gohr            ['pàge', 'pàge', ['deaccent' => 0]],
6724486f8cSAndreas Gohr            ['pagĖ', 'pagė', ['deaccent' => 0]],
6824486f8cSAndreas Gohr            ['pagĒēĔĕĖėĘęĚě', 'pagēēĕĕėėęęěě', ['deaccent' => 0]],
6924486f8cSAndreas Gohr            ['ښ', 'ښ', ['deaccent' => 0]],
7024486f8cSAndreas Gohr            ['ښ侧化并곦ঝഈ', 'ښ侧化并곦ঝഈ', ['deaccent' => 0]],
7124486f8cSAndreas Gohr
7224486f8cSAndreas Gohr            // romanize
7324486f8cSAndreas Gohr            ['pàge', 'page', ['deaccent' => 2]],
7424486f8cSAndreas Gohr            ['pagĖ', 'page', ['deaccent' => 2]],
7524486f8cSAndreas Gohr            ['pagĒēĔĕĖėĘęĚě', 'pageeeeeeeeee', ['deaccent' => 2]],
7624486f8cSAndreas Gohr            ['ښ', 'ښ', ['deaccent' => 2]],
7724486f8cSAndreas Gohr            ['ښ侧化并곦ঝഈ', 'ښ侧化并곦ঝഈ', ['deaccent' => 2]],
7824486f8cSAndreas Gohr
7924486f8cSAndreas Gohr            // deaccent and force ascii
8024486f8cSAndreas Gohr            ['pàge', 'page', ['deaccent' => 1, 'ascii' => true]],
8124486f8cSAndreas Gohr            ['pagĖ', 'page', ['deaccent' => 1, 'ascii' => true]],
8224486f8cSAndreas Gohr            ['pagĒēĔĕĖėĘęĚě', 'pageeeeeeeeee', ['deaccent' => 1, 'ascii' => true]],
8324486f8cSAndreas Gohr            ['ښ', '', ['deaccent' => 1, 'ascii' => true]],
8424486f8cSAndreas Gohr            ['ښ侧化并곦ঝഈ', '', ['deaccent' => 1, 'ascii' => true]],
8524486f8cSAndreas Gohr
8624486f8cSAndreas Gohr            // romanize and force ascii
8724486f8cSAndreas Gohr            ['pàge', 'page', ['deaccent' => 2, 'ascii' => true]],
8824486f8cSAndreas Gohr            ['pagĖ', 'page', ['deaccent' => 2, 'ascii' => true]],
8924486f8cSAndreas Gohr            ['pagĒēĔĕĖėĘęĚě', 'pageeeeeeeeee', ['deaccent' => 2, 'ascii' => true]],
9024486f8cSAndreas Gohr            ['ښ', '', ['deaccent' => 2, 'ascii' => true]],
9124486f8cSAndreas Gohr            ['ښ侧化并곦ঝഈ', '', ['deaccent' => 2, 'ascii' => true]],
9224486f8cSAndreas Gohr        ];
9324486f8cSAndreas Gohr
9424486f8cSAndreas Gohr        foreach ($tests as $test) {
9524486f8cSAndreas Gohr            // defaults
963366d071SAndreas Gohr            $sepchar = isset($test[2]['sepchar']) ? $test[2]['sepchar'] :  '_';
9724486f8cSAndreas Gohr            $deaccent = isset($test[2]['deaccent']) ? $test[2]['deaccent'] : 1;
983366d071SAndreas Gohr            $ascii = isset($test[2]['ascii']) ? $test[2]['ascii'] : false;
9924486f8cSAndreas Gohr
10024486f8cSAndreas Gohr            // unless set, test both useslash settings
10124486f8cSAndreas Gohr            if (isset($test[2]['useslash'])) {
10224486f8cSAndreas Gohr                yield([$test[0], $test[1], $ascii, $sepchar, $deaccent, $test[2]['useslash']]);
10324486f8cSAndreas Gohr            } else {
10424486f8cSAndreas Gohr                yield([$test[0], $test[1], $ascii, $sepchar, $deaccent, 0]);
10524486f8cSAndreas Gohr                yield([$test[0], $test[1], $ascii, $sepchar, $deaccent, 1]);
10624486f8cSAndreas Gohr            }
10724486f8cSAndreas Gohr        }
10824486f8cSAndreas Gohr    }
10924486f8cSAndreas Gohr
11024486f8cSAndreas Gohr    /**
11124486f8cSAndreas Gohr     * @dataProvider provideTestData
11224486f8cSAndreas Gohr     * @param string $input
11324486f8cSAndreas Gohr     * @param string $expected
11424486f8cSAndreas Gohr     * @param bool $ascii
11524486f8cSAndreas Gohr     * @param string $sepchar
11624486f8cSAndreas Gohr     * @param int $deaccent
11724486f8cSAndreas Gohr     * @param int $useslash
11824486f8cSAndreas Gohr     */
11924486f8cSAndreas Gohr    function testCleanId($input, $expected, $ascii, $sepchar, $deaccent, $useslash)
12024486f8cSAndreas Gohr    {
121f8369d7dSTobias Sarnowski        // set dokuwiki defaults
122f8369d7dSTobias Sarnowski        global $conf;
12324486f8cSAndreas Gohr        $conf['sepchar'] = $sepchar;
12424486f8cSAndreas Gohr        $conf['deaccent'] = $deaccent;
12524486f8cSAndreas Gohr        $conf['useslash'] = $useslash;
126f8369d7dSTobias Sarnowski
12724486f8cSAndreas Gohr        $result = cleanID($input, $ascii);
12824486f8cSAndreas Gohr        $this->assertEquals($expected, $result);
129f8369d7dSTobias Sarnowski    }
130f8369d7dSTobias Sarnowski
131f8369d7dSTobias Sarnowski
132f8369d7dSTobias Sarnowski
13324486f8cSAndreas Gohr    function test_caching_ascii()
13424486f8cSAndreas Gohr    {
13530f3bd15SMichael Grosse        global $conf;
13630f3bd15SMichael Grosse        $conf['deaccent'] = 0;
13730f3bd15SMichael Grosse        $this->assertEquals('pàge', cleanID('pàge', false));
13830f3bd15SMichael Grosse        $this->assertEquals('page', cleanID('pàge', true));
13930f3bd15SMichael Grosse
14030f3bd15SMichael Grosse        $this->assertEquals('page', cleanID('pagĖ', true));
14130f3bd15SMichael Grosse        $this->assertEquals('pagė', cleanID('pagĖ', false));
14230f3bd15SMichael Grosse    }
14330f3bd15SMichael Grosse
144f8369d7dSTobias Sarnowski}
145