first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,81 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Behat steps definitions for Language import tool
*
* @package tool_langimport
* @category test
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
use Moodle\BehatExtension\Exception\SkippedException;
/**
* Steps definitions related with the Language import tool
*
* @package tool_langimport
* @category test
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_tool_langimport extends behat_base {
/**
* This step looks to see if the remote language import tests should be run (indicated by
* setting TOOL_LANGIMPORT_REMOTE_TESTS in config.php.
*
* @Given /^remote langimport tests are enabled$/
*/
public function remote_langimport_tests_are_enabled() {
if (!defined('TOOL_LANGIMPORT_REMOTE_TESTS')) {
throw new SkippedException('To run the remote langimport tests you must '.
'define TOOL_LANGIMPORT_REMOTE_TESTS in config.php');
}
}
/**
* Downloads a langpack and fakes it being outdated
*
* @param string $langcode The language code (e.g. en)
* @Given /^outdated langpack \'([^\']*)\' is installed$/
*/
public function outdated_langpack_is_installed($langcode) {
global $CFG;
require_once($CFG->libdir.'/componentlib.class.php');
// Download the langpack.
$dir = make_upload_directory('lang');
$installer = new lang_installer($langcode);
$result = $installer->run();
if ($result[$langcode] !== lang_installer::RESULT_INSTALLED) {
throw new coding_exception("Failed to install langpack '$langcode'");
}
$path = "$dir/$langcode/$langcode.md5";
if (!file_exists($path)) {
throw new coding_exception("Failed to find '$langcode' checksum");
}
file_put_contents($path, '000000');
}
}
@@ -0,0 +1,88 @@
@tool @tool_langimport
Feature: Manage language packs
In order to support different languages
As an administrator
I need to be able to add, update and remove language packs
Background:
Given remote langimport tests are enabled
# The pirate language pack is used for testing because its small to download.
Scenario: Install language pack
Given I log in as "admin"
And I navigate to "Language > Language packs" in site administration
When I set the field "Available language packs" to "en_ar"
And I press "Install selected language pack(s)"
Then I should see "Language pack 'en_ar' was successfully installed"
And the "Installed language packs" select box should contain "en_ar"
And I navigate to "Reports > Live logs" in site administration
And I should see "The language pack 'en_ar' was installed."
Scenario: Install multiple language packs asynchronously in the background
Given I log in as "admin"
And I navigate to "Language > Language packs" in site administration
And I set the field "Available language packs" to "en_us,en_us_k12"
When I press "Install selected language pack(s)"
Then I should see "Language packs scheduled for installation."
And I should see "The following language packs will be installed soon: en_us, en_us_k12."
And I trigger cron
And I am on homepage
And I navigate to "Language > Language packs" in site administration
And the "Installed language packs" select box should contain "en_us"
And the "Installed language packs" select box should contain "en_us_k12"
And I navigate to "Reports > Live logs" in site administration
And I should see "The language pack 'en_us' was installed."
And I should see "The language pack 'en_us_k12' was installed."
@javascript
Scenario: Search for available language pack
Given I log in as "admin"
And I navigate to "Language > Language packs" in site administration
When I set the field "Search available language packs" to "pirate"
Then the "Available language packs" select box should not contain "es"
And I set the field "Available language packs" to "en_ar"
And I press "Install selected language pack(s)"
And I should see "Language pack 'en_ar' was successfully installed"
Scenario: Update language pack
Given outdated langpack 'en_ar' is installed
And I log in as "admin"
And I navigate to "Language > Language packs" in site administration
When I press "Update all installed language packs"
Then I should see "Language pack 'en_ar' was successfully updated"
And I should see "Language pack update completed"
And I navigate to "Reports > Live logs" in site administration
And I should see "The language pack 'en_ar' was updated."
Scenario: Inform admin that there are multiple installed languages and updating them all can take too long
Given outdated langpack 'en_ar' is installed
And outdated langpack 'en_us' is installed
And outdated langpack 'en_us_k12' is installed
When I log in as "admin"
And I navigate to "Language > Language packs" in site administration
Then I should see "Updating all installed language packs by clicking the button can take a long time and lead to timeouts."
Scenario: Try to uninstall language pack
Given I log in as "admin"
And I navigate to "Language > Language packs" in site administration
And I set the field "Available language packs" to "en_ar"
And I press "Install selected language pack(s)"
When I set the field "Installed language packs" to "en_ar"
And I press "Uninstall selected language pack(s)"
And I press "Continue"
Then I should see "Language pack 'en_ar' was uninstalled"
And the "Installed language packs" select box should not contain "en_ar"
And the "Available language packs" select box should contain "en_ar"
And I navigate to "Reports > Live logs" in site administration
And I should see "The language pack 'en_ar' was removed."
And I should see "Language pack uninstalled"
Scenario: Try to uninstall English language pack
Given I log in as "admin"
And I navigate to "Language > Language packs" in site administration
When I set the field "Installed language packs" to "en"
And I press "Uninstall selected language pack(s)"
Then I should see "The English language pack cannot be uninstalled."
And I navigate to "Reports > Live logs" in site administration
And I should not see "Language pack uninstalled"
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_langimport;
/**
* Tests for \tool_langimport\locale class.
*
* @package tool_langimport
* @category test
* @coversDefaultClass \tool_langimport\controller
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class controller_test extends \advanced_testcase {
/**
* Test uninstall of language with invalid values.
*
* @covers ::uninstall_lang
* @dataProvider uninstall_lang_invalid_provider
* @params string $lang
*/
public function test_uninstall_lang_invalid(string $lang): void {
global $CFG;
$controller = new controller();
$this->assertFalse($controller->uninstall_language($lang));
$this->assertFileExists("{$CFG->dataroot}/lang");
$this->assertFileExists("{$CFG->dirroot}/lang/en");
}
/**
* Data provider for uninstall_lang tests with invalid values.
*
* @return array
*/
public function uninstall_lang_invalid_provider(): array {
return [
'Empty string' => [''],
'Meaningless empty string' => [' '],
'Default language' => ['en'],
'Invalid language string' => ['swedish'],
];
}
}
@@ -0,0 +1,105 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests for langimport events.
*
* @package tool_langimport
* @copyright 2014 Dan Poltawski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace tool_langimport\event;
/**
* Test class for langimport events.
*
* @package tool_langimport
* @copyright 2014 Dan Poltawski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class events_test extends \advanced_testcase {
/**
* Setup testcase.
*/
public function setUp(): void {
$this->setAdminUser();
$this->resetAfterTest();
}
public function test_langpack_updated(): void {
global $CFG;
$event = \tool_langimport\event\langpack_updated::event_with_langcode($CFG->lang);
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\tool_langimport\event\langpack_updated', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
}
public function test_langpack_updated_validation(): void {
$this->expectException('coding_exception');
$this->expectExceptionMessage("The 'langcode' value must be set to a valid language code");
\tool_langimport\event\langpack_updated::event_with_langcode('broken langcode');
}
public function test_langpack_installed(): void {
$event = \tool_langimport\event\langpack_imported::event_with_langcode('fr');
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\tool_langimport\event\langpack_imported', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
}
public function test_langpack_installed_validation(): void {
$this->expectException('coding_exception');
$this->expectExceptionMessage("The 'langcode' value must be set to a valid language code");
\tool_langimport\event\langpack_imported::event_with_langcode('broken langcode');
}
public function test_langpack_removed(): void {
$event = \tool_langimport\event\langpack_removed::event_with_langcode('fr');
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\tool_langimport\event\langpack_removed', $event);
$this->assertEquals(\context_system::instance(), $event->get_context());
}
public function test_langpack_removed_validation(): void {
$this->expectException('coding_exception');
$this->expectExceptionMessage("The 'langcode' value must be set to a valid language code");
\tool_langimport\event\langpack_removed::event_with_langcode('broken langcode');
}
}
@@ -0,0 +1,87 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_langimport;
/**
* Tests for \tool_langimport\locale class.
*
* @package tool_langimport
* @category test
* @covers \tool_langimport\locale
* @copyright 2018 Université Rennes 2 {@link https://www.univ-rennes2.fr}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class locale_test extends \advanced_testcase {
/** @var string Locale */
protected string $locale;
#[\Override]
public function setUp(): void {
parent::setUp();
$this->locale = \core\locale::get_locale();
}
#[\Override]
public function tearDown(): void {
parent::tearDown();
\core\locale::set_locale(LC_ALL, $this->locale);
}
/**
* Test that \tool_langimport\locale::check_locale_availability() works as expected.
*/
public function test_check_locale_availability(): void {
// Create a mock of set_locale() method to simulate:
// - get_locale() call which backup current locale
// - first set_locale() call which try to set new 'es' locale
// - second set_locale() call which restore locale.
$mock = $this->getMockBuilder(locale::class)
->onlyMethods([
'get_locale',
'set_locale',
])
->getMock();
$mock->method('get_locale')->will($this->onConsecutiveCalls('en'));
$mock->method('set_locale')->will($this->onConsecutiveCalls('es', 'en'));
// Test what happen when locale is available on system.
$result = $mock->check_locale_availability('en');
$this->assertTrue($result);
// Create a mock of set_locale() method to simulate:
// - get_locale() call which backup current locale
// - first set_locale() call which fail to set new locale
// - second set_locale() call which restore locale.
$mock = $this->getMockBuilder(locale::class)
->onlyMethods([
'get_locale',
'set_locale',
])
->getMock();
$mock->method('get_locale')->will($this->onConsecutiveCalls('en'));
$mock->method('set_locale')->will($this->onConsecutiveCalls(false, 'en'));
// Test what happen when locale is not available on system.
$result = $mock->check_locale_availability('en');
$this->assertFalse($result);
// Test an invalid parameter.
$locale = new locale();
$this->expectException(\coding_exception::class);
$locale->check_locale_availability('');
}
}