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
+135
View File
@@ -0,0 +1,135 @@
<?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 block_rss_client;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_rss_client.php');
/**
* PHPunit tests for rss client cron.
*
* @package block_rss_client
* @copyright 2015 Universit of Nottingham
* @author Neill Magill <neill.magill@nottingham.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cron_test extends \advanced_testcase {
/**
* Test that when a record has a skipuntil time that is greater
* than the current time the attempt is skipped.
*/
public function test_skip(): void {
global $DB, $CFG;
$this->resetAfterTest();
// Create a RSS feed record with a skip until time set to the future.
$record = (object) array(
'userid' => 1,
'title' => 'Skip test feed',
'preferredtitle' => '',
'description' => 'A feed to test the skip time.',
'shared' => 0,
'url' => 'http://example.com/rss',
'skiptime' => 330,
'skipuntil' => time() + 300,
);
$DB->insert_record('block_rss_client', $record);
$task = new \block_rss_client\task\refreshfeeds();
ob_start();
// Silence SimplePie php notices.
$errorlevel = error_reporting($CFG->debug & ~E_USER_NOTICE);
$task->execute();
error_reporting($errorlevel);
$cronoutput = ob_get_clean();
$this->assertStringContainsString('skipping until ' . userdate($record->skipuntil), $cronoutput);
$this->assertStringContainsString('0 feeds refreshed (took ', $cronoutput);
}
/**
* Data provider for skip time tests.
*
* @return array
*/
public function skip_time_increase_provider(): array {
return [
'Never failed' => [
'skiptime' => 0,
'skipuntil' => 0,
'newvalue' => MINSECS * 5,
],
'Failed before' => [
// This should just double the time.
'skiptime' => 330,
'skipuntil' => time(),
'newvalue' => 660,
],
'Near max' => [
'skiptime' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME - 5,
'skipuntil' => time(),
'newvalue' => \block_rss_client\task\refreshfeeds::CLIENT_MAX_SKIPTIME,
],
];
}
/**
* Test that when a feed has an error the skip time is increased correctly.
*
* @dataProvider skip_time_increase_provider
*/
public function test_error($skiptime, $skipuntil, $newvalue): void {
global $DB, $CFG;
$this->resetAfterTest();
require_once("{$CFG->libdir}/simplepie/moodle_simplepie.php");
$time = time();
// A record that has failed before.
$record = (object) [
'userid' => 1,
'title' => 'Skip test feed',
'preferredtitle' => '',
'description' => 'A feed to test the skip time.',
'shared' => 0,
'url' => 'http://example.com/rss',
'skiptime' => $skiptime,
'skipuntil' => $skipuntil,
];
$record->id = $DB->insert_record('block_rss_client', $record);
// Run the scheduled task and have it fail.
$task = $this->getMockBuilder(\block_rss_client\task\refreshfeeds::class)
->onlyMethods(['fetch_feed'])
->getMock();
$piemock = $this->getMockBuilder(\moodle_simplepie::class)
->onlyMethods(['error'])
->getMock();
$piemock->method('error')
->willReturn(true);
$task->method('fetch_feed')
->willReturn($piemock);
// Run the cron and capture its output.
$this->expectOutputRegex("/.*Error: could not load\/find the RSS feed - skipping for {$newvalue} seconds.*/");
$task->execute();
}
}
@@ -0,0 +1,238 @@
<?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/>.
/**
* Base class for unit tests for block_rss_client.
*
* @package block_rss_client
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_rss_client\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\tests\provider_testcase;
use block_rss_client\privacy\provider;
use core_privacy\local\request\approved_userlist;
/**
* Unit tests for blocks\rss_client\classes\privacy\provider.php
*
* @copyright 2018 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends provider_testcase {
/**
* Basic setup for these tests.
*/
public function setUp(): void {
$this->resetAfterTest(true);
}
/**
* Test getting the context for the user ID related to this plugin.
*/
public function test_get_contexts_for_userid(): void {
$user = $this->getDataGenerator()->create_user();
$context = \context_user::instance($user->id);
$this->add_rss_feed($user);
$contextlist = provider::get_contexts_for_userid($user->id);
$this->assertEquals($context, $contextlist->current());
}
/**
* Test that data is exported correctly for this plugin.
*/
public function test_export_user_data(): void {
$user = $this->getDataGenerator()->create_user();
$context = \context_user::instance($user->id);
$this->add_rss_feed($user);
$this->add_rss_feed($user);
$writer = \core_privacy\local\request\writer::with_context($context);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($user->id, $context, 'block_rss_client');
$data = $writer->get_data([get_string('pluginname', 'block_rss_client')]);
$this->assertCount(2, $data->feeds);
$feed1 = reset($data->feeds);
$this->assertEquals('BBC News - World', $feed1->title);
$this->assertEquals('World News', $feed1->preferredtitle);
$this->assertEquals('Description: BBC News - World', $feed1->description);
$this->assertEquals(get_string('no'), $feed1->shared);
$this->assertEquals('http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk', $feed1->url);
}
/**
* Test that only users within a course context are fetched.
*/
public function test_get_users_in_context(): void {
$component = 'block_rss_client';
// Create a user.
$user = $this->getDataGenerator()->create_user();
$usercontext = \context_user::instance($user->id);
$userlist = new \core_privacy\local\request\userlist($usercontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist);
$this->add_rss_feed($user);
// The list of users within the user context should contain user.
provider::get_users_in_context($userlist);
$this->assertCount(1, $userlist);
$expected = [$user->id];
$actual = $userlist->get_userids();
$this->assertEquals($expected, $actual);
// The list of users within the system context should be empty.
$systemcontext = \context_system::instance();
$userlist2 = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(0, $userlist2);
}
/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users(): void {
$component = 'block_rss_client';
$user1 = $this->getDataGenerator()->create_user();
$usercontext1 = \context_user::instance($user1->id);
$user2 = $this->getDataGenerator()->create_user();
$usercontext2 = \context_user::instance($user2->id);
$this->add_rss_feed($user1);
$this->add_rss_feed($user2);
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(1, $userlist1);
$expected = [$user1->id];
$actual = $userlist1->get_userids();
$this->assertEquals($expected, $actual);
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
$expected = [$user2->id];
$actual = $userlist2->get_userids();
$this->assertEquals($expected, $actual);
// Convert $userlist1 into an approved_contextlist.
$approvedlist1 = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist1);
// Re-fetch users in usercontext1.
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
provider::get_users_in_context($userlist1);
// The user data in usercontext1 should be deleted.
$this->assertCount(0, $userlist1);
// Re-fetch users in usercontext2.
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
// The user data in usercontext2 should be still present.
$this->assertCount(1, $userlist2);
// Convert $userlist2 into an approved_contextlist in the system context.
$systemcontext = \context_system::instance();
$approvedlist2 = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist2);
// Re-fetch users in usercontext2.
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
// The user data in systemcontext should not be deleted.
$this->assertCount(1, $userlist2);
}
/**
* Test that user data is deleted using the context.
*/
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
$user = $this->getDataGenerator()->create_user();
$context = \context_user::instance($user->id);
$this->add_rss_feed($user);
// Check that we have an entry.
$rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
$this->assertCount(1, $rssfeeds);
provider::delete_data_for_all_users_in_context($context);
// Check that it has now been deleted.
$rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
$this->assertCount(0, $rssfeeds);
}
/**
* Test that user data is deleted for this user.
*/
public function test_delete_data_for_user(): void {
global $DB;
$user = $this->getDataGenerator()->create_user();
$context = \context_user::instance($user->id);
$this->add_rss_feed($user);
// Check that we have an entry.
$rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
$this->assertCount(1, $rssfeeds);
$approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'block_rss_feed',
[$context->id]);
provider::delete_data_for_user($approvedlist);
// Check that it has now been deleted.
$rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
$this->assertCount(0, $rssfeeds);
}
/**
* Add dummy rss feed.
*
* @param object $user User object
*/
private function add_rss_feed($user) {
global $DB;
$rssfeeddata = array(
'userid' => $user->id,
'title' => 'BBC News - World',
'preferredtitle' => 'World News',
'description' => 'Description: BBC News - World',
'shared' => 0,
'url' => 'http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk',
);
$DB->insert_record('block_rss_client', $rssfeeddata);
}
}