diff --git a/analytics/classes/admin_setting_predictor.php b/analytics/classes/admin_setting_predictor.php index c523697ef71..9cfbe8a7264 100644 --- a/analytics/classes/admin_setting_predictor.php +++ b/analytics/classes/admin_setting_predictor.php @@ -53,9 +53,13 @@ class admin_setting_predictor extends \admin_setting_configselect { // Calling it here without checking if it is ready because we check it below and show it as a controlled case. $selectedprocessor = \core_analytics\manager::get_predictions_processor($data, false); - $isready = $selectedprocessor->is_ready(); - if ($isready !== true) { - return get_string('errorprocessornotready', 'analytics', $isready); + + if (!during_initial_install() && !moodle_needs_upgrading()) { + // TODO: Do not check if the processor is ready during installation or upgrade. See MDL-84481. + $isready = $selectedprocessor->is_ready(); + if ($isready !== true) { + return get_string('errorprocessornotready', 'analytics', $isready); + } } $currentvalue = get_config('analytics', 'predictionsprocessor'); diff --git a/analytics/tests/classes/mlbackend_configuration_trait.php b/analytics/tests/classes/mlbackend_configuration_trait.php deleted file mode 100644 index 3c2ddc9b22e..00000000000 --- a/analytics/tests/classes/mlbackend_configuration_trait.php +++ /dev/null @@ -1,40 +0,0 @@ -. - -namespace core_analytics\tests; - -/** - * A trait to check machine learning configurations. - * - * @package core_analytics - * @category test - * @copyright 2024 David Woloszyn - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -trait mlbackend_configuration_trait { - /** - * Check if mlbackend_python is configured. - * - * @return bool - */ - public static function is_mlbackend_python_configured(): bool { - if (defined('TEST_MLBACKEND_PYTHON_HOST') && defined('TEST_MLBACKEND_PYTHON_PORT') - && defined('TEST_MLBACKEND_PYTHON_USERNAME') && defined('TEST_MLBACKEND_PYTHON_USERNAME')) { - return true; - } - return false; - } -} diff --git a/analytics/tests/classes/mlbackend_helper_trait.php b/analytics/tests/classes/mlbackend_helper_trait.php new file mode 100644 index 00000000000..eaa33974e6f --- /dev/null +++ b/analytics/tests/classes/mlbackend_helper_trait.php @@ -0,0 +1,77 @@ +. + +namespace core_analytics\tests; + +use phpunit_util; + +/** + * A trait to check machine learning configurations. + * + * @package core_analytics + * @category test + * @copyright 2024 David Woloszyn + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +trait mlbackend_helper_trait { + /** + * Check if mlbackend_python is configured. + * + * @return bool + */ + public static function is_mlbackend_python_configured(): bool { + if (defined('TEST_MLBACKEND_PYTHON_HOST') && defined('TEST_MLBACKEND_PYTHON_PORT') + && defined('TEST_MLBACKEND_PYTHON_USERNAME') && defined('TEST_MLBACKEND_PYTHON_USERNAME')) { + return true; + } + return false; + } + + /** + * Generate test courses. + * + * @param int $ncourses Number of courses to generate. + * @param array $params Additional parameters for the courses. + * @param bool $ismulticlass Whether to generate courses for multi-classification. + */ + public function generate_courses( + int $ncourses, + array $params = [], + bool $ismulticlass = false, + ): void { + $params = $params + [ + 'startdate' => mktime(0, 0, 0, 10, 24, 2015), + 'enddate' => mktime(0, 0, 0, 2, 24, 2016), + ]; + for ($i = 0; $i < $ncourses; $i++) { + $name = 'a' . random_string(10); + $courseparams = ['shortname' => $name, 'fullname' => $name] + $params; + phpunit_util::get_data_generator()->create_course($courseparams); + } + for ($i = 0; $i < $ncourses; $i++) { + $name = 'b' . random_string(10); + $courseparams = ['shortname' => $name, 'fullname' => $name] + $params; + phpunit_util::get_data_generator()->create_course($courseparams); + } + if ($ismulticlass) { + for ($i = 0; $i < $ncourses; $i++) { + $name = 'c' . random_string(10); + $courseparams = ['shortname' => $name, 'fullname' => $name] + $params; + phpunit_util::get_data_generator()->create_course($courseparams); + } + } + } +} diff --git a/analytics/tests/manager_test.php b/analytics/tests/manager_test.php index 9688fa95767..0a1a9b5bcfc 100644 --- a/analytics/tests/manager_test.php +++ b/analytics/tests/manager_test.php @@ -16,7 +16,7 @@ namespace core_analytics; -use core_analytics\tests\mlbackend_configuration_trait; +use core_analytics\tests\mlbackend_helper_trait; defined('MOODLE_INTERNAL') || die(); @@ -34,7 +34,7 @@ require_once(__DIR__ . '/fixtures/test_target_course_level_shortname.php'); * @covers \core_analytics\manager */ final class manager_test extends \advanced_testcase { - use mlbackend_configuration_trait; + use mlbackend_helper_trait; /** * test_deleted_context @@ -50,6 +50,10 @@ final class manager_test extends \advanced_testcase { $this->setAdminuser(); set_config('enabled_stores', 'logstore_standard', 'tool_log'); + // Create some courses. + $this->generate_courses(2, ['visible' => 0]); + $this->generate_courses(2, ['visible' => 1]); + $target = \core_analytics\manager::get_target('test_target_course_level_shortname'); $indicators = ['test_indicator_max', 'test_indicator_min', 'test_indicator_fullname']; foreach ($indicators as $key => $indicator) { @@ -59,11 +63,6 @@ final class manager_test extends \advanced_testcase { $model = \core_analytics\model::create($target, $indicators); $modelobj = $model->get_model_obj(); - $coursepredict1 = $this->getDataGenerator()->create_course(['visible' => 0]); - $coursepredict2 = $this->getDataGenerator()->create_course(['visible' => 0]); - $coursetrain1 = $this->getDataGenerator()->create_course(['visible' => 1]); - $coursetrain2 = $this->getDataGenerator()->create_course(['visible' => 1]); - $model->enable('\core\analytics\time_splitting\no_splitting'); $model->train(); diff --git a/analytics/tests/model_test.php b/analytics/tests/model_test.php index 1ae9fa58e7f..3912a8d11c5 100644 --- a/analytics/tests/model_test.php +++ b/analytics/tests/model_test.php @@ -24,7 +24,7 @@ namespace core_analytics; -use core_analytics\tests\mlbackend_configuration_trait; +use core_analytics\tests\mlbackend_helper_trait; defined('MOODLE_INTERNAL') || die(); @@ -44,7 +44,7 @@ require_once(__DIR__ . '/fixtures/test_analysis.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class model_test extends \advanced_testcase { - use mlbackend_configuration_trait; + use mlbackend_helper_trait; /** @var model Store Model. */ protected $model; @@ -105,10 +105,9 @@ final class model_test extends \advanced_testcase { $this->resetAfterTest(true); set_config('enabled_stores', 'logstore_standard', 'tool_log'); - $coursepredict1 = $this->getDataGenerator()->create_course(array('visible' => 0)); - $coursepredict2 = $this->getDataGenerator()->create_course(array('visible' => 0)); - $coursetrain1 = $this->getDataGenerator()->create_course(array('visible' => 1)); - $coursetrain2 = $this->getDataGenerator()->create_course(array('visible' => 1)); + // Create some courses. + $this->generate_courses(2, ['visible' => 0]); + $this->generate_courses(2, ['visible' => 1]); $this->model->enable('\core\analytics\time_splitting\single_range'); @@ -118,9 +117,6 @@ final class model_test extends \advanced_testcase { // Fake evaluation results record to check that it is actually deleted. $this->add_fake_log(); - $modeloutputdir = $this->model->get_output_dir(array(), true); - $this->assertTrue(is_dir($modeloutputdir)); - // Generate a prediction action to confirm that it is deleted when there is an important update. $predictions = $DB->get_records('analytics_predictions'); $prediction = reset($predictions); @@ -135,7 +131,6 @@ final class model_test extends \advanced_testcase { $this->assertEmpty($DB->count_records('analytics_train_samples')); $this->assertEmpty($DB->count_records('analytics_predict_samples')); $this->assertEmpty($DB->count_records('analytics_used_files')); - $this->assertFalse(is_dir($modeloutputdir)); set_config('enabled_stores', '', 'tool_log'); get_log_manager(true); @@ -154,10 +149,9 @@ final class model_test extends \advanced_testcase { $this->resetAfterTest(true); set_config('enabled_stores', 'logstore_standard', 'tool_log'); - $coursepredict1 = $this->getDataGenerator()->create_course(array('visible' => 0)); - $coursepredict2 = $this->getDataGenerator()->create_course(array('visible' => 0)); - $coursetrain1 = $this->getDataGenerator()->create_course(array('visible' => 1)); - $coursetrain2 = $this->getDataGenerator()->create_course(array('visible' => 1)); + // Create some courses. + $this->generate_courses(2, ['visible' => 0]); + $this->generate_courses(2, ['visible' => 1]); $this->model->enable('\core\analytics\time_splitting\single_range'); @@ -178,7 +172,6 @@ final class model_test extends \advanced_testcase { // Update to an empty time splitting method to force model::clear execution. $this->model->clear(); - $this->assertFalse(is_dir($modelversionoutputdir)); // Check that most of the stuff got deleted. $this->assertEquals(1, $DB->count_records('analytics_models', array('id' => $this->modelobj->id))); diff --git a/analytics/tests/prediction_test.php b/analytics/tests/prediction_test.php index 04f1ee38431..ddbfb933a39 100644 --- a/analytics/tests/prediction_test.php +++ b/analytics/tests/prediction_test.php @@ -31,6 +31,8 @@ require_once(__DIR__ . '/fixtures/test_static_target_shortname.php'); require_once(__DIR__ . '/../../course/lib.php'); +use core_analytics\tests\mlbackend_helper_trait; + /** * Unit tests for evaluation, training and prediction. * @@ -48,6 +50,8 @@ require_once(__DIR__ . '/../../course/lib.php'); */ final class prediction_test extends \advanced_testcase { + use mlbackend_helper_trait; + /** * Purge all the mlbackend outputs. * @@ -546,7 +550,7 @@ final class prediction_test extends \advanced_testcase { } // Generate training courses. $ncourses = 5; - $this->generate_courses_multiclass($ncourses); + $this->generate_courses(ncourses: $ncourses, ismulticlass: true); $model = $this->add_multiclass_model(); $model->update(true, false, $timesplittingid, get_class($predictionsprocessor)); $results = $model->train(); @@ -857,63 +861,6 @@ final class prediction_test extends \advanced_testcase { return new \core_analytics\model($model->get_id()); } - /** - * Generates $ncourses courses - * - * @param int $ncourses The number of courses to be generated. - * @param array $params Course params - * @return null - */ - protected function generate_courses($ncourses, array $params = []) { - - $params = $params + [ - 'startdate' => mktime(0, 0, 0, 10, 24, 2015), - 'enddate' => mktime(0, 0, 0, 2, 24, 2016), - ]; - - for ($i = 0; $i < $ncourses; $i++) { - $name = 'a' . random_string(10); - $courseparams = array('shortname' => $name, 'fullname' => $name) + $params; - $this->getDataGenerator()->create_course($courseparams); - } - for ($i = 0; $i < $ncourses; $i++) { - $name = 'b' . random_string(10); - $courseparams = array('shortname' => $name, 'fullname' => $name) + $params; - $this->getDataGenerator()->create_course($courseparams); - } - } - - /** - * Generates ncourses for multi-classification - * - * @param int $ncourses The number of courses to be generated. - * @param array $params Course params - * @return null - */ - protected function generate_courses_multiclass($ncourses, array $params = []) { - - $params = $params + [ - 'startdate' => mktime(0, 0, 0, 10, 24, 2015), - 'enddate' => mktime(0, 0, 0, 2, 24, 2016), - ]; - - for ($i = 0; $i < $ncourses; $i++) { - $name = 'a' . random_string(10); - $courseparams = array('shortname' => $name, 'fullname' => $name) + $params; - $this->getDataGenerator()->create_course($courseparams); - } - for ($i = 0; $i < $ncourses; $i++) { - $name = 'b' . random_string(10); - $courseparams = array('shortname' => $name, 'fullname' => $name) + $params; - $this->getDataGenerator()->create_course($courseparams); - } - for ($i = 0; $i < $ncourses; $i++) { - $name = 'c' . random_string(10); - $courseparams = array('shortname' => $name, 'fullname' => $name) + $params; - $this->getDataGenerator()->create_course($courseparams); - } - } - /** * Forces some configuration values. * diff --git a/analytics/tests/privacy/provider_test.php b/analytics/tests/privacy/provider_test.php index 14fc1d95b65..a7fbfe6b0e5 100644 --- a/analytics/tests/privacy/provider_test.php +++ b/analytics/tests/privacy/provider_test.php @@ -14,21 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for privacy. - * - * @package core_analytics - * @copyright 2018 David Monllaó {@link http://www.davidmonllao.com} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ namespace core_analytics\privacy; -use core_analytics\privacy\provider; -use core_privacy\local\request\transform; -use core_privacy\local\request\writer; -use core_privacy\local\request\approved_contextlist; use core_privacy\local\request\approved_userlist; -use core_analytics\tests\mlbackend_configuration_trait; +use core_analytics\tests\mlbackend_helper_trait; defined('MOODLE_INTERNAL') || die(); @@ -45,7 +34,7 @@ require_once(__DIR__ . '/../fixtures/test_target_course_users.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class provider_test extends \core_privacy\tests\provider_testcase { - use mlbackend_configuration_trait; + use mlbackend_helper_trait; /** @var \core_analytics\model Store Model 1. */ protected $model1; @@ -83,6 +72,12 @@ final class provider_test extends \core_privacy\tests\provider_testcase { /** @var \stdClass $u8 User 8 record. */ protected $u8; + /** @var \stdClass $u9 User 9 record. */ + protected $u9; + + /** @var \stdClass $u10 User 10 record. */ + protected $u10; + /** @var \stdClass $c1 Course 1 record. */ protected $c1; @@ -124,10 +119,13 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6 = $this->getdatagenerator()->create_user(['firstname' => 'a666666666666', 'lastname' => 'a']); $this->u7 = $this->getdatagenerator()->create_user(['firstname' => 'b777777777777', 'lastname' => 'b']); $this->u8 = $this->getDataGenerator()->create_user(['firstname' => 'b888888888888', 'lastname' => 'b']); + $this->u9 = $this->getDataGenerator()->create_user(['firstname' => 'a999999999999', 'lastname' => 'a']); + $this->u10 = $this->getDataGenerator()->create_user(['firstname' => 'b000000000000', 'lastname' => 'a']); $this->c1 = $this->getDataGenerator()->create_course(['visible' => false]); $this->c2 = $this->getDataGenerator()->create_course(); + // Enrol users to course 1. $this->getDataGenerator()->enrol_user($this->u1->id, $this->c1->id, 'student'); $this->getDataGenerator()->enrol_user($this->u2->id, $this->c1->id, 'student'); $this->getDataGenerator()->enrol_user($this->u3->id, $this->c1->id, 'student'); @@ -136,6 +134,9 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->getDataGenerator()->enrol_user($this->u6->id, $this->c1->id, 'student'); $this->getDataGenerator()->enrol_user($this->u7->id, $this->c1->id, 'student'); $this->getDataGenerator()->enrol_user($this->u8->id, $this->c1->id, 'student'); + $this->getDataGenerator()->enrol_user($this->u9->id, $this->c1->id, 'student'); + $this->getDataGenerator()->enrol_user($this->u10->id, $this->c1->id, 'student'); + // Enrol users to course 2. $this->getDataGenerator()->enrol_user($this->u1->id, $this->c2->id, 'student'); $this->getDataGenerator()->enrol_user($this->u2->id, $this->c2->id, 'student'); $this->getDataGenerator()->enrol_user($this->u3->id, $this->c2->id, 'student'); @@ -144,6 +145,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->getDataGenerator()->enrol_user($this->u6->id, $this->c2->id, 'student'); $this->getDataGenerator()->enrol_user($this->u7->id, $this->c2->id, 'student'); $this->getDataGenerator()->enrol_user($this->u8->id, $this->c2->id, 'student'); + $this->getDataGenerator()->enrol_user($this->u9->id, $this->c2->id, 'student'); + $this->getDataGenerator()->enrol_user($this->u10->id, $this->c2->id, 'student'); $this->setAdminUser(); @@ -174,7 +177,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $course2context = \context_course::instance($this->c2->id); $systemcontext = \context_system::instance(); $expected = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id, - $this->u7->id, $this->u8->id]; + $this->u7->id, $this->u8->id, $this->u9->id, $this->u10->id]; // Check users exist in the relevant contexts. $userlist = new \core_privacy\local\request\userlist($course1context, $component); @@ -210,7 +213,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase { // We have 4 predictions for model1 and 8 predictions for model2. $this->assertEquals(12, $DB->count_records('analytics_predictions')); - $this->assertEquals(26, $DB->count_records('analytics_indicator_calc')); + $this->assertEquals(32, $DB->count_records('analytics_indicator_calc')); // We have 1 prediction action. $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); @@ -285,6 +288,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), + $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(), + $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(), ]; foreach ($actualcontexts as $userid => $unused) { @@ -295,7 +300,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase { // Test initial record counts are as expected. $this->assertEquals(12, $DB->count_records('analytics_predictions')); $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); - $this->assertEquals(26, $DB->count_records('analytics_indicator_calc')); + $this->assertEquals(32, $DB->count_records('analytics_indicator_calc')); // Delete u1 and u3 from system context. $approveduserids = [$this->u1->id, $this->u3->id]; @@ -312,6 +317,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6->id => [$systemcontext->id, $course1context->id, $course2context->id], $this->u7->id => [$systemcontext->id, $course1context->id, $course2context->id], $this->u8->id => [$systemcontext->id, $course1context->id, $course2context->id], + $this->u9->id => [$systemcontext->id, $course1context->id, $course2context->id], + $this->u10->id => [$systemcontext->id, $course1context->id, $course2context->id], ]; $actualcontexts = [ @@ -323,6 +330,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), + $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(), + $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(), ]; foreach ($actualcontexts as $userid => $unused) { @@ -334,11 +343,11 @@ final class provider_test extends \core_privacy\tests\provider_testcase { // Test expected number of records have been deleted. $this->assertEquals(11, $DB->count_records('analytics_predictions')); $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); - $this->assertEquals(24, $DB->count_records('analytics_indicator_calc')); + $this->assertEquals(30, $DB->count_records('analytics_indicator_calc')); // Delete for all 8 users in course 2 context. $approveduserids = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id, - $this->u7->id, $this->u8->id]; + $this->u7->id, $this->u8->id, $this->u9->id, $this->u10->id]; $approvedlist = new approved_userlist($course2context, $component, $approveduserids); provider::delete_data_for_users($approvedlist); @@ -352,6 +361,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6->id => [$systemcontext->id, $course1context->id], $this->u7->id => [$systemcontext->id, $course1context->id], $this->u8->id => [$systemcontext->id, $course1context->id], + $this->u9->id => [$systemcontext->id, $course1context->id], + $this->u10->id => [$systemcontext->id, $course1context->id], ]; $actualcontexts = [ @@ -363,6 +374,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), + $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(), + $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(), ]; foreach ($actualcontexts as $userid => $unused) { @@ -374,7 +387,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase { // Test expected number of records have been deleted. $this->assertEquals(7, $DB->count_records('analytics_predictions')); $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); - $this->assertEquals(16, $DB->count_records('analytics_indicator_calc')); + $this->assertEquals(20, $DB->count_records('analytics_indicator_calc')); $approveduserids = [$this->u3->id]; $approvedlist = new approved_userlist($course1context, $component, $approveduserids); @@ -390,6 +403,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6->id => [$systemcontext->id, $course1context->id], $this->u7->id => [$systemcontext->id, $course1context->id], $this->u8->id => [$systemcontext->id, $course1context->id], + $this->u9->id => [$systemcontext->id, $course1context->id], + $this->u10->id => [$systemcontext->id, $course1context->id], ]; $actualcontexts = [ @@ -401,6 +416,8 @@ final class provider_test extends \core_privacy\tests\provider_testcase { $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), + $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(), + $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(), ]; foreach ($actualcontexts as $userid => $unused) { sort($actualcontexts[$userid]); @@ -411,7 +428,7 @@ final class provider_test extends \core_privacy\tests\provider_testcase { // Test expected number of records have been deleted. $this->assertEquals(6, $DB->count_records('analytics_predictions')); $this->assertEquals(0, $DB->count_records('analytics_prediction_actions')); - $this->assertEquals(15, $DB->count_records('analytics_indicator_calc')); + $this->assertEquals(19, $DB->count_records('analytics_indicator_calc')); } /** diff --git a/analytics/tests/stats_test.php b/analytics/tests/stats_test.php index 3acdfe4bad6..a28ec7fe5f0 100644 --- a/analytics/tests/stats_test.php +++ b/analytics/tests/stats_test.php @@ -16,7 +16,7 @@ namespace core_analytics; -use core_analytics\tests\mlbackend_configuration_trait; +use core_analytics\tests\mlbackend_helper_trait; defined('MOODLE_INTERNAL') || die(); @@ -32,7 +32,7 @@ require_once(__DIR__ . '/fixtures/test_target_shortname.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class stats_test extends \advanced_testcase { - use mlbackend_configuration_trait; + use mlbackend_helper_trait; /** * Set up the test environment. diff --git a/course/tests/analytics/indicators_test.php b/course/tests/analytics/indicators_test.php index a1b633291de..3af150d3a29 100644 --- a/course/tests/analytics/indicators_test.php +++ b/course/tests/analytics/indicators_test.php @@ -16,7 +16,7 @@ namespace core_course\analytics; -use core_analytics\tests\mlbackend_configuration_trait; +use core_analytics\tests\mlbackend_helper_trait; defined('MOODLE_INTERNAL') || die(); @@ -34,7 +34,7 @@ require_once(__DIR__ . '/../../../analytics/tests/fixtures/test_target_course_us * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class indicators_test extends \advanced_testcase { - use mlbackend_configuration_trait; + use mlbackend_helper_trait; /** * test_no_teacher diff --git a/lib/mlbackend/python/classes/processor.php b/lib/mlbackend/python/classes/processor.php index a56a6a8b545..165fb446114 100644 --- a/lib/mlbackend/python/classes/processor.php +++ b/lib/mlbackend/python/classes/processor.php @@ -26,6 +26,11 @@ namespace mlbackend_python; defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->dirroot . '/analytics/tests/classes/mlbackend_helper_trait.php'); + +use core_analytics\tests\mlbackend_helper_trait; + /** * Python predictions processor. * @@ -35,6 +40,8 @@ defined('MOODLE_INTERNAL') || die(); */ class processor implements \core_analytics\classifier, \core_analytics\regressor, \core_analytics\packable { + use mlbackend_helper_trait; + /** * The required version of the python package that performs all calculations. */ @@ -90,21 +97,31 @@ class processor implements \core_analytics\classifier, \core_analytics\regresso public function __construct() { global $CFG; - $config = get_config('mlbackend_python'); - - $this->useserver = !empty($config->useserver); - - if (!$this->useserver) { - // Set the python location if there is a value. - if (!empty($CFG->pathtopython)) { - $this->pathtopython = $CFG->pathtopython; - } + if ((defined('BEHAT_SITE_RUNNING') || (defined('PHPUNIT_TEST') && PHPUNIT_TEST)) && + self::is_mlbackend_python_configured()) { + $this->useserver = true; + $this->host = TEST_MLBACKEND_PYTHON_HOST; + $this->port = TEST_MLBACKEND_PYTHON_PORT; + $this->secure = false; + $this->username = TEST_MLBACKEND_PYTHON_USERNAME; + $this->password = TEST_MLBACKEND_PYTHON_PASSWORD; } else { - $this->host = $config->host ?? ''; - $this->port = $config->port ?? ''; - $this->secure = $config->secure ?? false; - $this->username = $config->username ?? ''; - $this->password = $config->password ?? ''; + $config = get_config('mlbackend_python'); + + $this->useserver = !empty($config->useserver); + + if (!$this->useserver) { + // Set the python location if there is a value. + if (!empty($CFG->pathtopython)) { + $this->pathtopython = $CFG->pathtopython; + } + } else { + $this->host = $config->host ?? ''; + $this->port = $config->port ?? ''; + $this->secure = $config->secure ?? false; + $this->username = $config->username ?? ''; + $this->password = $config->password ?? ''; + } } }