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,210 @@
<?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 message_airnotifier;
use core_external\external_api;
use externallib_advanced_testcase;
use message_airnotifier_external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External airnotifier functions unit tests
*
* @package message_airnotifier
* @category external
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class externallib_test extends externallib_advanced_testcase {
/**
* Tests set up
*/
protected function setUp(): void {
global $CFG;
require_once($CFG->dirroot . '/message/output/airnotifier/externallib.php');
}
/**
* Test is_system_configured
*/
public function test_is_system_configured(): void {
global $DB;
$this->resetAfterTest(true);
$user = self::getDataGenerator()->create_user();
self::setUser($user);
// In a clean installation, it should be not configured.
$configured = message_airnotifier_external::is_system_configured();
$configured = external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured);
$this->assertEquals(0, $configured);
// Fake configuration.
set_config('airnotifieraccesskey', random_string());
// Enable the plugin.
$DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
$configured = message_airnotifier_external::is_system_configured();
$configured = external_api::clean_returnvalue(message_airnotifier_external::is_system_configured_returns(), $configured);
$this->assertEquals(1, $configured);
}
/**
* Test are_notification_preferences_configured
*/
public function test_are_notification_preferences_configured(): void {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
self::setUser($user1);
set_user_preference('message_provider_moodle_instantmessage_enabled', 'airnotifier', $user1);
set_user_preference('message_provider_moodle_instantmessage_enabled', 'airnotifier', $user2);
$params = array($user1->id, $user2->id, $user3->id);
$preferences = message_airnotifier_external::are_notification_preferences_configured($params);
$returnsdescription = message_airnotifier_external::are_notification_preferences_configured_returns();
$preferences = external_api::clean_returnvalue($returnsdescription, $preferences);
$expected = array(
array(
'userid' => $user1->id,
'configured' => 1
)
);
$this->assertEquals(1, count($preferences['users']));
$this->assertEquals($expected, $preferences['users']);
$this->assertEquals(2, count($preferences['warnings']));
// Now, remove one user.
delete_user($user2);
$preferences = message_airnotifier_external::are_notification_preferences_configured($params);
$preferences = external_api::clean_returnvalue($returnsdescription, $preferences);
$this->assertEquals(1, count($preferences['users']));
$this->assertEquals($expected, $preferences['users']);
$this->assertEquals(2, count($preferences['warnings']));
// Now, remove one user1 preference (the user still has one preference for airnotifier).
unset_user_preference('message_provider_moodle_instantmessage_enabled', $user1);
$preferences = message_airnotifier_external::are_notification_preferences_configured($params);
$preferences = external_api::clean_returnvalue($returnsdescription, $preferences);
$this->assertEquals($expected, $preferences['users']);
}
/**
* Test get_user_devices
*/
public function test_get_user_devices(): void {
global $CFG, $DB;
require_once($CFG->dirroot . '/user/externallib.php');
$this->resetAfterTest(true);
$this->setAdminUser();
// System not configured.
$devices = message_airnotifier_external::get_user_devices('');
$devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices);
$this->assertCount(1, $devices['warnings']);
$this->assertEquals('systemnotconfigured', $devices['warnings'][0]['warningcode']);
// Fake configuration.
set_config('airnotifieraccesskey', random_string());
// Enable the plugin.
$DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
// Get devices.
$devices = message_airnotifier_external::get_user_devices('');
$devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices);
$this->assertCount(0, $devices['warnings']);
// No devices, unfortunatelly we cannot create devices (we can't mock airnotifier server).
$this->assertCount(0, $devices['devices']);
}
/**
* Test get_user_devices permissions
*/
public function test_get_user_devices_permissions(): void {
global $CFG, $DB;
require_once($CFG->dirroot . '/user/externallib.php');
$this->resetAfterTest(true);
$user = self::getDataGenerator()->create_user();
$otheruser = self::getDataGenerator()->create_user();
self::setUser($user);
// No permission to get other users devices.
$this->expectException('required_capability_exception');
$devices = message_airnotifier_external::get_user_devices('', $otheruser->id);
}
/**
* Test enable_device
*/
public function test_enable_device(): void {
global $USER, $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Add fake core device.
$device = array(
'appid' => 'com.moodle.moodlemobile',
'name' => 'occam',
'model' => 'Nexus 4',
'platform' => 'Android',
'version' => '4.2.2',
'pushid' => 'apushdkasdfj4835',
'uuid' => 'asdnfl348qlksfaasef859',
'userid' => $USER->id,
'timecreated' => time(),
'timemodified' => time(),
);
$coredeviceid = $DB->insert_record('user_devices', (object) $device);
$airnotifierdev = array(
'userdeviceid' => $coredeviceid,
'enable' => 1
);
$airnotifierdevid = $DB->insert_record('message_airnotifier_devices', (object) $airnotifierdev);
// Disable and enable.
$result = message_airnotifier_external::enable_device($airnotifierdevid, false);
$result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertTrue($result['success']);
$this->assertEquals(0, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid)));
$result = message_airnotifier_external::enable_device($airnotifierdevid, true);
$result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertTrue($result['success']);
$this->assertEquals(1, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid)));
}
}
@@ -0,0 +1,120 @@
<?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 message_airnotifier;
use message_airnotifier_manager;
/**
* Unit tests for message_airnotifier_manager.
*
* @package message_airnotifier
* @category test
* @copyright 2020 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class manager_test extends \advanced_testcase {
/** Test check_configuration by default **/
public function test_check_configuration_default(): void {
global $CFG;
$this->resetAfterTest(true);
$manager = new message_airnotifier_manager();
// Mock server responses.
$CFG->airnotifierurl = 'localhost';
\curl::mock_response(json_encode(['error' => 'Invalid access key'])); // Mock request to check access key.
$checks = $manager->check_configuration();
$this->assertEquals(\core\check\result::OK, $checks[0]->get_status()); // Mobile service enabled.
$this->assertEquals(\core\check\result::OK, $checks[1]->get_status()); // Message output not disabled in config.php.
$this->assertEquals(\core\check\result::OK, $checks[2]->get_status()); // Mobile notifications enabled.
$this->assertEquals(\core\check\result::ERROR, $checks[3]->get_status()); // Airnotifier NOT configured, missing key.
$this->assertEquals(\core\check\result::OK, $checks[4]->get_status()); // Airnotifier URL available.
$this->assertEquals(\core\check\result::ERROR, $checks[5]->get_status()); // Missing access key.
$this->assertEquals(\core\check\result::ERROR, $checks[7]->get_status()); // No registered devices yet.
}
/** Test check_configuration with token **/
public function test_check_configuration_with_token(): void {
global $CFG;
$this->resetAfterTest(true);
$manager = new message_airnotifier_manager();
// Mock server responses.
$CFG->airnotifierurl = 'localhost';
\curl::mock_response(json_encode(['status' => 'ok'])); // Mock first request to check URL.
\curl::mock_response(json_encode(['error' => 'Invalid access key'])); // Mock second request to check acces key.
$CFG->airnotifieraccesskey = 'test'; // For enabling Airnotifier.
$checks = $manager->check_configuration();
$this->assertEquals(\core\check\result::OK, $checks[0]->get_status()); // Mobile service enabled.
$this->assertEquals(\core\check\result::OK, $checks[1]->get_status()); // Message output not disabled in config.php.
$this->assertEquals(\core\check\result::OK, $checks[2]->get_status()); // Mobile notifications enabled.
$this->assertEquals(\core\check\result::OK, $checks[3]->get_status()); // Airnotifier configured.
$this->assertEquals(\core\check\result::OK, $checks[4]->get_status()); // Airnotifier URL available.
// The original function fourth check (access key valid in the remote Airnotifier server) is not mockable.
$this->assertEquals(\core\check\result::ERROR, $checks[6]->get_status()); // No registered devices yet.
}
/** Test check_configuration bad settings **/
public function test_check_configuration_incorrect_settings(): void {
global $CFG;
$this->resetAfterTest(true);
$manager = new message_airnotifier_manager();
// Mock server responses.
$CFG->airnotifierurl = 'localhost';
\curl::mock_response(json_encode(['status' => 'ok'])); // Mock first request to check URL.
\curl::mock_response(json_encode(['error' => 'Invalid access key'])); // Mock second request to check acces key.
$CFG->airnotifieraccesskey = 'test'; // For enabling Airnotifier.
$CFG->airnotifierappname .= ' ';
$CFG->noemailever = true;
$checks = $manager->check_configuration();
$this->assertEquals(\core\check\result::OK, $checks[0]->get_status()); // Mobile service enabled.
$this->assertEquals(\core\check\result::CRITICAL, $checks[1]->get_status()); // Message output disabled in config.php.
$this->assertEquals(\core\check\result::OK, $checks[2]->get_status()); // Mobile notifications enabled.
$this->assertEquals(\core\check\result::OK, $checks[3]->get_status()); // Airnotifier configured.
$this->assertEquals(\core\check\result::ERROR, $checks[4]->get_status()); // Airnotifier URL available.
$this->assertEquals(\core\check\result::OK, $checks[5]->get_status()); // Invalid setting (empty space).
// The original function fifth check (access key valid in the remote Airnotifier server) is not mockable.
$this->assertEquals(\core\check\result::ERROR, $checks[7]->get_status()); // No registered devices yet.
}
/** Test has_enabled_devices **/
public function test_has_enabled_devices(): void {
global $CFG, $DB, $USER;
$this->resetAfterTest(true);
$CFG->airnotifieraccesskey = 'test'; // For mocking the request.
$manager = new message_airnotifier_manager();
// No devices yet for current user.
$this->assertFalse($manager->has_enabled_devices($CFG->airnotifiermobileappname));
// Add devices.
\curl::mock_response(json_encode(['status' => 'ok']));
$DB->insert_record('user_devices',
['userid' => $USER->id, 'appid' => $CFG->airnotifiermobileappname, 'platform' => 'ios',
'timecreated' => time(), 'timemodified' => time()]);
$this->assertTrue($manager->has_enabled_devices($CFG->airnotifiermobileappname));
}
}
@@ -0,0 +1,252 @@
<?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 message_airnotifier.
*
* @package message_airnotifier
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace message_airnotifier\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\tests\provider_testcase;
use message_airnotifier\privacy\provider;
use core_privacy\local\request\approved_userlist;
/**
* Unit tests for message\output\airnotifier\classes\privacy\provider.php
*
* @copyright 2018 Adrian Greeve <adrian@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);
}
/**
* /
* @param object $user User object
* @param string $pushid unique string
*/
protected function add_device($user, $pushid) {
global $DB;
// Add fake core device.
$device = array(
'appid' => 'com.moodle.moodlemobile',
'name' => 'occam',
'model' => 'Nexus 4',
'platform' => 'Android',
'version' => '4.2.2',
'pushid' => $pushid,
'uuid' => 'asdnfl348qlksfaasef859',
'userid' => $user->id,
'timecreated' => time(),
'timemodified' => time(),
);
$coredeviceid = $DB->insert_record('user_devices', (object) $device);
$airnotifierdev = array(
'userdeviceid' => $coredeviceid,
'enable' => 1
);
$airnotifierdevid = $DB->insert_record('message_airnotifier_devices', (object) $airnotifierdev);
}
/**
* Test returning metadata.
*/
public function test_get_metadata(): void {
$collection = new \core_privacy\local\metadata\collection('message_airnotifier');
$collection = \message_airnotifier\privacy\provider::get_metadata($collection);
$this->assertNotEmpty($collection);
}
/**
* 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_device($user, 'apuJih874kj');
$this->add_device($user, 'bdu09Ikjjsu');
$contextlist = \message_airnotifier\privacy\provider::get_contexts_for_userid($user->id);
$this->assertEquals($context->id, $contextlist->current()->id);
}
/**
* 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_device($user, 'apuJih874kj');
$this->add_device($user, 'bdu09Ikjjsu');
$writer = \core_privacy\local\request\writer::with_context($context);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($user->id, $context, 'message_airnotifier');
// First device.
$data = $writer->get_data([get_string('privacy:subcontext', 'message_airnotifier'), 'Nexus 4_apuJih874kj']);
$this->assertEquals('com.moodle.moodlemobile', $data->appid);
// Second device.
$data = $writer->get_data([get_string('privacy:subcontext', 'message_airnotifier'), 'Nexus 4_bdu09Ikjjsu']);
$this->assertEquals('bdu09Ikjjsu', $data->pushid);
}
/**
* 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_device($user, 'apuJih874kj');
// Check that we have an entry.
$devices = $DB->get_records('message_airnotifier_devices');
$this->assertCount(1, $devices);
\message_airnotifier\privacy\provider::delete_data_for_all_users_in_context($context);
// Check that it has now been deleted.
$devices = $DB->get_records('message_airnotifier_devices');
$this->assertCount(0, $devices);
}
/**
* 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_device($user, 'apuJih874kj');
// Check that we have an entry.
$devices = $DB->get_records('message_airnotifier_devices');
$this->assertCount(1, $devices);
$approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'message_airnotifier', [$context->id]);
\message_airnotifier\privacy\provider::delete_data_for_user($approvedlist);
// Check that it has now been deleted.
$devices = $DB->get_records('message_airnotifier_devices');
$this->assertCount(0, $devices);
}
/**
* Test that only users with a user context are fetched.
*/
public function test_get_users_in_context(): void {
$component = 'message_airnotifier';
// Create user.
$user = $this->getDataGenerator()->create_user();
$usercontext = \context_user::instance($user->id);
// The lists of users for the user context should be empty.
// Related user data have not been created yet.
$userlist = new \core_privacy\local\request\userlist($usercontext, $component);
provider::get_users_in_context($userlist);
$this->assertCount(0, $userlist);
$this->add_device($user, 'apuJih874kj');
$this->add_device($user, 'bdu09Ikjjsu');
// The list of users for userlist should return one user (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 should only return users in the user context.
$systemcontext = \context_system::instance();
$userlist1 = new \core_privacy\local\request\userlist($systemcontext, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
}
/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users(): void {
$component = 'message_airnotifier';
// Create user1.
$user1 = $this->getDataGenerator()->create_user();
$usercontext1 = \context_user::instance($user1->id);
// Create user2.
$user2 = $this->getDataGenerator()->create_user();
$usercontext2 = \context_user::instance($user2->id);
$this->add_device($user1, 'apuJih874kj');
$this->add_device($user1, 'cpuJih874kp');
$this->add_device($user2, 'bdu09Ikjjsu');
// The list of users for usercontext1 should return one user (user1).
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(1, $userlist1);
// The list of users for usercontext2 should return one user (user2).
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
$approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist);
// Re-fetch users in usercontext1 - the user data should now be empty.
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
// The list of users for usercontext2 should still return one user (user2).
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
// User data should only be removed in the user context.
$systemcontext = \context_system::instance();
$approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist);
// Re-fetch users in usercontext2 - the user data should still be present.
$userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
}
}