Merge branch 'MDL-87548-main' of https://github.com/andimendunia/moodle
This commit is contained in:
@@ -33,3 +33,4 @@ $string['privacy:metadata:repository_wikimedia'] = 'The Wikimedia repository plu
|
||||
$string['privacy:metadata:repository_wikimedia:search_text'] = 'The Wikimedia repository user search text query.';
|
||||
$string['privacy:metadata:repository_wikimedia:preference:maxwidth'] = 'The user preference max width configured for the Wikimedia repository';
|
||||
$string['privacy:metadata:repository_wikimedia:preference:maxheight'] = 'The user preference Max Height configured for the Wikimedia repository.';
|
||||
$string['ratelimited'] = 'Wikimedia rate limit exceeded. Please wait a moment and try again.';
|
||||
|
||||
@@ -26,6 +26,11 @@
|
||||
require_once($CFG->dirroot . '/repository/lib.php');
|
||||
require_once(__DIR__ . '/wikimedia.php');
|
||||
|
||||
use core\di;
|
||||
use core\http_client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
/**
|
||||
* repository_wikimedia class
|
||||
* This is a class used to browse images from wikimedia
|
||||
@@ -190,6 +195,58 @@ EOD;
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a file from external repository and saves it in temp dir
|
||||
*
|
||||
* Overrides the base implementation to handle HTTP 429 rate limiting errors
|
||||
* from Wikimedia servers with a user-friendly error message.
|
||||
*
|
||||
* @param string $url the URL of file to download
|
||||
* @param string $filename filename (without path) to save the downloaded file in the
|
||||
* temporary directory, if omitted or file already exists the new filename will be generated
|
||||
* @return array with elements:
|
||||
* path: internal location of the file
|
||||
* url: URL to the source (from parameters)
|
||||
* @throws \repository_exception if rate limited by the Wikimedia server
|
||||
* @throws \moodle_exception if download fails
|
||||
*/
|
||||
#[\Override]
|
||||
public function get_file($url, $filename = '') {
|
||||
global $CFG;
|
||||
|
||||
$path = $this->prepare_file($filename);
|
||||
$client = di::get(http_client::class);
|
||||
|
||||
try {
|
||||
$response = $client->get($url, [
|
||||
RequestOptions::SINK => $path,
|
||||
RequestOptions::TIMEOUT => $CFG->repositorygetfiletimeout,
|
||||
RequestOptions::HTTP_ERRORS => false,
|
||||
]);
|
||||
} catch (RequestException $e) {
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
throw new \moodle_exception('errorwhiledownload', 'repository', '', $e->getMessage());
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() === 429) {
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
throw new \repository_exception('ratelimited', 'repository_wikimedia');
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
throw new \moodle_exception('errorwhiledownload', 'repository', '', $response->getReasonPhrase());
|
||||
}
|
||||
|
||||
return ['path' => $path, 'url' => $url];
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this repository accessing private data?
|
||||
*
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
@repository @repository_wikimedia @javascript
|
||||
Feature: Wikimedia repository
|
||||
In order to update my profile picture
|
||||
As an admin
|
||||
I need to choose a picture from Wikimedia
|
||||
|
||||
Scenario: Users can add profile picture using wikimedia
|
||||
Given I log in as "admin"
|
||||
And I open my profile in edit mode
|
||||
And I click on "Add..." "button" in the "New picture" "form_row"
|
||||
# Upload a new user picture using Wikimedia repository.
|
||||
And I follow "Wikimedia"
|
||||
And I set the field "Search for:" to "cat"
|
||||
And I click on "Submit" "button"
|
||||
# Click on the link of the first search result.
|
||||
And I click on "a.fp-file" "css_element"
|
||||
And I click on "Select this file" "button"
|
||||
When I click on "Update profile" "button"
|
||||
# New profile picture.
|
||||
Then "//img[contains(@class, 'userpicture')]" "xpath_element" should exist
|
||||
# Default profile picture should not exist any more.
|
||||
And "//img[contains(@class, 'defaultuserpic')]" "xpath_element" should not exist
|
||||
@@ -0,0 +1,118 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Unit tests for repository_wikimedia class.
|
||||
*
|
||||
* @package repository_wikimedia
|
||||
* @copyright 2026 Andi Permana
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace repository_wikimedia;
|
||||
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/repository/lib.php');
|
||||
require_once($CFG->dirroot . '/repository/wikimedia/lib.php');
|
||||
|
||||
/**
|
||||
* Unit tests for Wikimedia repository
|
||||
*
|
||||
* @package repository_wikimedia
|
||||
* @copyright 2026 Andi Permana
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \repository_wikimedia
|
||||
*/
|
||||
final class repository_test extends \advanced_testcase {
|
||||
/** @var \repository_wikimedia|null Repository instance */
|
||||
private $repo = null;
|
||||
|
||||
/**
|
||||
* Setup test environment.
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$user = get_admin();
|
||||
$this->setUser($user);
|
||||
|
||||
// Create repository instance.
|
||||
$record = $this->getDataGenerator()->create_repository('wikimedia');
|
||||
|
||||
$this->repo = \repository::get_repository_by_id($record->id, \core\context\system::instance());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a HTTP 429 response from Wikimedia throws a rate limit exception.
|
||||
*/
|
||||
public function test_get_file_rate_limited(): void {
|
||||
['mock' => $mock] = $this->get_mocked_http_client();
|
||||
$mock->append(new Response(429));
|
||||
|
||||
$this->expectException(\repository_exception::class);
|
||||
$this->expectExceptionMessage(get_string('ratelimited', 'repository_wikimedia'));
|
||||
|
||||
$this->repo->get_file('https://upload.wikimedia.org/wikipedia/commons/test.jpg');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a successful HTTP 200 response returns the downloaded file path and URL.
|
||||
*/
|
||||
public function test_get_file_success(): void {
|
||||
['mock' => $mock] = $this->get_mocked_http_client();
|
||||
$mock->append(new Response(200, [], 'fake image content'));
|
||||
|
||||
$result = $this->repo->get_file('https://upload.wikimedia.org/wikipedia/commons/test.jpg');
|
||||
|
||||
$this->assertArrayHasKey('path', $result);
|
||||
$this->assertArrayHasKey('url', $result);
|
||||
$this->assertEquals('https://upload.wikimedia.org/wikipedia/commons/test.jpg', $result['url']);
|
||||
$this->assertFileExists($result['path']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a non-200/non-429 HTTP error response throws a moodle_exception.
|
||||
*/
|
||||
public function test_get_file_http_error(): void {
|
||||
['mock' => $mock] = $this->get_mocked_http_client();
|
||||
$mock->append(new Response(503, [], 'Service Unavailable'));
|
||||
|
||||
$this->expectException(\moodle_exception::class);
|
||||
$this->expectExceptionMessage(get_string('errorwhiledownload', 'repository', 'Service Unavailable'));
|
||||
|
||||
$this->repo->get_file('https://upload.wikimedia.org/wikipedia/commons/test.jpg');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a network-level failure (e.g. connection refused) throws a moodle_exception.
|
||||
*/
|
||||
public function test_get_file_network_error(): void {
|
||||
['mock' => $mock] = $this->get_mocked_http_client();
|
||||
$mock->append(new RequestException('Connection refused', new Request('GET', 'https://upload.wikimedia.org/')));
|
||||
|
||||
$this->expectException(\moodle_exception::class);
|
||||
$this->expectExceptionMessage(get_string('errorwhiledownload', 'repository', 'Connection refused'));
|
||||
|
||||
$this->repo->get_file('https://upload.wikimedia.org/wikipedia/commons/test.jpg');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user