1<?php
2
3namespace dokuwiki\plugin\translation\test;
4
5use dokuwiki\Extension\EventHandler;
6use DokuWikiTest;
7use TestRequest;
8
9/**
10 * General tests for the translation plugin
11 *
12 * @group plugin_translation
13 * @group plugins
14 */
15class BasicTest extends DokuWikiTest
16{
17
18    protected $pluginsEnabled = ['translation'];
19
20    /**
21     * Test provider
22     * @return array[]
23     * @see testBuildTransID
24     */
25    public static function provideBuildTransID()
26    {
27        return [
28            [
29                'en',
30                'ns:page',
31                'de es',
32                [':ns:page', 'en'],
33            ],
34            [
35                '',
36                'ns:page',
37                'de es',
38                [':ns:page', 'en'],
39            ],
40            [
41                'de',
42                'ns:page',
43                'de es',
44                [':de:ns:page', 'de'],
45            ],
46        ];
47    }
48
49    /**
50     * @dataProvider provideBuildTransID
51     *
52     * @param $inputLang
53     * @param $inputID
54     * @param $translationsOption
55     * @param $expected
56     */
57    public function testBuildTransID($inputLang, $inputID, $translationsOption, $expected)
58    {
59        global $conf;
60        $conf['plugin']['translation']['translations'] = $translationsOption;
61        /** @var \helper_plugin_translation $helper */
62        $helper = plugin_load('helper', 'translation', true);
63
64        $actual_result = $helper->buildTransID($inputLang, $inputID);
65
66        $this->assertEquals($expected, $actual_result);
67    }
68
69    /**
70     * Test provider
71     * @return array[]
72     * @see testRedirectStart
73     */
74    public static function provideRedirectStart()
75    {
76        return [
77            [
78                'start',
79                'de es',
80                'de,en-US;q=0.8,en;q=0.5,fr;q=0.3',
81                'de:start',
82                'redirect to translated page',
83            ],
84            [
85                'start',
86                'de es',
87                'en-US,de;q=0.8,en;q=0.5,fr;q=0.3',
88                [],
89                'do not redirect if basic namespace is correct lang',
90            ],
91            [
92                'de:start',
93                'en de es',
94                'en-US,en;q=0.8,fr;q=0.5',
95                [],
96                'do not redirect anything other than exactly $conf[\'start\']',
97            ],
98        ];
99    }
100
101    /**
102     * @dataProvider provideRedirectStart
103     *
104     * @param $input
105     * @param $translationsOption
106     * @param $httpAcceptHeader
107     * @param $expected
108     */
109    public function testRedirectStart($input, $translationsOption, $httpAcceptHeader, $expected, $msg)
110    {
111        global $conf;
112        $conf['plugin']['translation']['translations'] = $translationsOption;
113        $conf['plugin']['translation']['redirectstart'] = 1;
114
115        // reset event handler (this should be done by the TestRequest, but it doesn't)
116        global $EVENT_HANDLER;
117        $EVENT_HANDLER = new EventHandler();
118
119        /** @var \helper_plugin_translation $helper */
120        $helper = plugin_load('helper', 'translation');
121        $helper->loadTranslationNamespaces();
122
123        $request = new TestRequest();
124        $request->setServer('HTTP_ACCEPT_LANGUAGE', $httpAcceptHeader);
125
126        $response = $request->get(array('id' => $input));
127        $actual = $response->getHeader('Location');
128
129        if (is_string($actual)) {
130            list(, $actual) = explode('doku.php?id=', $actual);
131        }
132
133        $this->assertEquals($expected, $actual, $msg);
134    }
135
136}
137