Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\output;
18
 
19
use ReflectionMethod;
20
 
21
/**
22
 * Primary navigation renderable test
23
 *
24
 * @package     core
25
 * @category    output
26
 * @copyright   2021 onwards Peter Dias
27
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
1441 ariadna 29
final class language_menu_test extends \advanced_testcase {
1 efrain 30
    /**
31
     * Basic setup to make sure the nav objects gets generated without any issues.
32
     */
33
    public function setUp(): void {
34
        global $PAGE;
1441 ariadna 35
        parent::setUp();
1 efrain 36
        $this->resetAfterTest();
37
        $PAGE->set_url('/');
38
    }
39
    /**
40
     * Test the get_lang_menu
41
     *
42
     * @dataProvider get_lang_menu_provider
43
     * @param bool $withadditionallangs
44
     * @param string $language
45
     * @param array $expected
46
     */
11 efrain 47
    public function test_get_lang_menu(bool $withadditionallangs, string $language, array $expected): void {
1 efrain 48
        global $CFG, $PAGE;
49
 
50
        // Mimic multiple langs installed. To trigger responses 'get_list_of_translations'.
51
        // Note: The text/title of the nodes generated will be 'English(fr), English(de)' but we don't care about this.
52
        // We are testing whether the nodes gets generated when the lang menu is available.
53
        if ($withadditionallangs) {
54
            mkdir("$CFG->dataroot/lang/de", 0777, true);
55
            mkdir("$CFG->dataroot/lang/fr", 0777, true);
56
            // Ensure the new langs are picked up and not taken from the cache.
57
            $stringmanager = get_string_manager();
58
            $stringmanager->reset_caches(true);
59
        }
60
 
61
        force_current_language($language);
62
 
63
        $output = new language_menu($PAGE);
64
        $method = new ReflectionMethod('\core\output\language_menu', 'export_for_template');
65
        $renderer = $PAGE->get_renderer('core');
66
 
67
        $response = $method->invoke($output, $renderer);
68
 
69
        if ($withadditionallangs) { // If there are multiple languages installed.
70
            // Assert that the title of the language menu matches the expected one.
71
            $this->assertEquals($expected['title'], $response['title']);
72
            // Assert that the number of language menu items matches the number of the expected items.
73
            $this->assertEquals(count($expected['items']), count($response['items']));
74
            foreach ($expected['items'] as $expecteditem) {
75
                $lang = $expecteditem['lang'];
76
                // We need to manually generate the url key and its value in the expected item array as this cannot
77
                // be done in the data provider due to the change of the state of $PAGE.
78
                if ($expecteditem['isactive']) {
79
                    $expecteditem['url'] = new \moodle_url('#');
80
                } else {
81
                    $expecteditem['url'] = new \moodle_url($PAGE->url, ['lang' => $lang]);
82
                    // When the language menu item is not the current language, it will contain the lang attribute.
83
                    $expecteditem['attributes'][] = [
84
                        'key' => 'lang',
85
                        'value' => $lang
86
                    ];
87
                }
88
                // The lang value is only used to generate the url, so this key can be removed.
89
                unset($expecteditem['lang']);
90
 
91
                // Assert that the given expected item exists in the returned items.
92
                $this->assertTrue(in_array($expecteditem, $response['items']));
93
            }
94
        } else { // No multiple languages.
95
            $this->assertEquals($expected, $response);
96
        }
97
    }
98
 
99
    /**
100
     * Provider for test_get_lang_menu
101
     *
102
     * @return array
103
     */
1441 ariadna 104
    public static function get_lang_menu_provider(): array {
1 efrain 105
        return [
106
            'Lang menu with only the current language' => [
107
                false, 'en', []
108
            ],
109
            'Lang menu with only multiple languages installed' => [
110
                true, 'en', [
111
                    'title' => 'English ‎(en)‎',
112
                    'items' => [
113
                        [
114
                            'title' => 'English ‎(en)‎',
115
                            'text' => 'English ‎(en)‎',
116
                            'link' => true,
117
                            'isactive' => true,
118
                            'lang' => 'en'
119
                        ],
120
                        [
121
                            'title' => 'English ‎(de)‎',
122
                            'text' => 'English ‎(de)‎',
123
                            'link' => true,
124
                            'isactive' => false,
125
                            'lang' => 'de'
126
                        ],
127
 
128
                        [
129
                            'title' => 'English ‎(fr)‎',
130
                            'text' => 'English ‎(fr)‎',
131
                            'link' => true,
132
                            'isactive' => false,
133
                            'lang' => 'fr'
134
                        ],
135
                    ],
136
                ],
137
            ],
138
            'Lang menu with only multiple languages installed and other than EN set active.' => [
139
                true, 'de', [
140
                    'title' => 'English ‎(de)‎',
141
                    'items' => [
142
                        [
143
                            'title' => 'English ‎(en)‎',
144
                            'text' => 'English ‎(en)‎',
145
                            'link' => true,
146
                            'isactive' => false,
147
                            'lang' => 'en'
148
                        ],
149
                        [
150
                            'title' => 'English ‎(de)‎',
151
                            'text' => 'English ‎(de)‎',
152
                            'link' => true,
153
                            'isactive' => true,
154
                            'lang' => 'de'
155
                        ],
156
                        [
157
                            'title' => 'English ‎(fr)‎',
158
                            'text' => 'English ‎(fr)‎',
159
                            'link' => true,
160
                            'isactive' => false,
161
                            'lang' => 'fr'
162
                        ],
163
                    ],
164
                ],
165
            ],
166
        ];
167
    }
168
}