Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 tool_langimport;
18
 
19
/**
20
 * Tests for \tool_langimport\locale class.
21
 *
22
 * @package    tool_langimport
23
 * @category   test
11 efrain 24
 * @covers \tool_langimport\locale
1 efrain 25
 * @copyright  2018 Université Rennes 2 {@link https://www.univ-rennes2.fr}
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
11 efrain 28
final class locale_test extends \advanced_testcase {
29
    /** @var string Locale */
30
    protected string $locale;
31
 
32
    #[\Override]
33
    public function setUp(): void {
34
        parent::setUp();
35
        $this->locale = \core\locale::get_locale();
36
    }
37
 
38
    #[\Override]
39
    public function tearDown(): void {
40
        parent::tearDown();
41
        \core\locale::set_locale(LC_ALL, $this->locale);
42
    }
43
 
1 efrain 44
    /**
45
     * Test that \tool_langimport\locale::check_locale_availability() works as expected.
46
     */
11 efrain 47
    public function test_check_locale_availability(): void {
48
        // Create a mock of set_locale() method to simulate:
49
        // - get_locale() call which backup current locale
50
        // - first set_locale() call which try to set new 'es' locale
51
        // - second set_locale() call which restore locale.
1 efrain 52
        $mock = $this->getMockBuilder(locale::class)
11 efrain 53
            ->onlyMethods([
54
                'get_locale',
55
                'set_locale',
56
            ])
1 efrain 57
            ->getMock();
11 efrain 58
        $mock->method('get_locale')->will($this->onConsecutiveCalls('en'));
59
        $mock->method('set_locale')->will($this->onConsecutiveCalls('es', 'en'));
1 efrain 60
 
61
        // Test what happen when locale is available on system.
62
        $result = $mock->check_locale_availability('en');
63
        $this->assertTrue($result);
64
 
11 efrain 65
        // Create a mock of set_locale() method to simulate:
66
        // - get_locale() call which backup current locale
67
        // - first set_locale() call which fail to set new locale
68
        // - second set_locale() call which restore locale.
1 efrain 69
        $mock = $this->getMockBuilder(locale::class)
11 efrain 70
            ->onlyMethods([
71
                'get_locale',
72
                'set_locale',
73
            ])
1 efrain 74
            ->getMock();
11 efrain 75
        $mock->method('get_locale')->will($this->onConsecutiveCalls('en'));
76
        $mock->method('set_locale')->will($this->onConsecutiveCalls(false, 'en'));
1 efrain 77
 
78
        // Test what happen when locale is not available on system.
79
        $result = $mock->check_locale_availability('en');
80
        $this->assertFalse($result);
81
 
82
        // Test an invalid parameter.
83
        $locale = new locale();
84
        $this->expectException(\coding_exception::class);
85
        $locale->check_locale_availability('');
86
    }
87
}