2017-08-28 13:24:09 +02:00
|
|
|
<?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/>.
|
|
|
|
|
MDL-75111 phpunit: Move tests to use correct names and ns (take#4)
Applied the following changes to various testcase classes:
- Namespaced with component[\level2-API]
- Moved to level2-API subdirectory when required.
- Fixed incorrect use statements with leading backslash.
- Remove file phpdoc block
- Remove MOODLE_INTERNAL if not needed.
- Changed code to point to global scope when needed.
- Fix some relative paths and comments here and there.
- All them passing individually.
- Complete runs passing too.
Special mention to:
- The following task tests have been moved within the level2 directory:
- \core\adhoc_task_test => \core\task\adhoc_task_test
- \core\scheduled_task_test => \core\task\scheduled_task_test
- \core\calendar_cron_task_test => \core\task\calendar_cron_task_test
- \core\h5p_get_content_types_task_test => \core\task\h5p_get_content_types_task_test
- \core\task_database_logger_test => \core\task\database_logger_test
- \core\task_logging_test => \core\task\logging_test
- The following event tests have been moved within level2 directory:
- \core\event_context_locked_test => \core\event\context_locked_test
- \core\event_deprecated_test => \core\event\deprecated_test
- \core\event_grade_deleted_test => \core\event\grade_deleted_test
- \core\event_profile_field_test => \core\event\profile_field_test
- \core\event_unknown_logged_test => \core\event\unknown_logged_test
- \core\event_user_graded_test => \core\event\user_graded_test
- \core\event_user_password_updated_test => \core\event\user_password_updated_test
- The following output tests have been moved within level2 directory:
- \core\mustache_template_finder_test => \core\output\mustache_template_finder_test
- \core\mustache_template_source_loader_test => \core\output\mustache_template_source_loader_test
- \core\output_mustache_helper_collection_test => \core\output\mustache_helper_collection_test
- The following tests have been moved to their correct tests directories:
- lib/tests/time_splittings_test.php => analytics/tests/time_splittings_test.php
- All the classes and tests under lib/filebrowser and lib/filestorage
belong to core, not to core_files. Some day we should move
them to their correct subsystem.
- All the classes and tests under lib/grade belong to core, not
to core_grades. Some day we should move them to their correct
subsystem.
- The core_grades_external class and its \core\grades_external_test
unit test should belong to the grades subsystem or, alternatively,
to \core\external, they both should be moved together.
- The core_grading_external class and its \core\grading_external_test
unit test should belong to the grading subsystem or, alternatively,
to \core\external, they both should be moved together.
- The \core\message\message and \core\message\inbound (may be others)
classes, and their associated tests should go to the core_message
subsystem.
- The core_user class, and its associated tests should go to the
core_user subsystem.
- The \core\update namespace is plain wrong (update is not valid API)
and needs action 1) create it or 2) move elsewhere.
2022-07-01 16:43:34 +02:00
|
|
|
namespace core_analytics;
|
2017-08-28 13:24:09 +02:00
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
require_once(__DIR__ . '/fixtures/test_indicator_max.php');
|
|
|
|
require_once(__DIR__ . '/fixtures/test_target_shortname.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for prediction actions.
|
|
|
|
*
|
|
|
|
* @package core_analytics
|
|
|
|
* @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
MDL-75111 phpunit: Move tests to use correct names and ns (take#4)
Applied the following changes to various testcase classes:
- Namespaced with component[\level2-API]
- Moved to level2-API subdirectory when required.
- Fixed incorrect use statements with leading backslash.
- Remove file phpdoc block
- Remove MOODLE_INTERNAL if not needed.
- Changed code to point to global scope when needed.
- Fix some relative paths and comments here and there.
- All them passing individually.
- Complete runs passing too.
Special mention to:
- The following task tests have been moved within the level2 directory:
- \core\adhoc_task_test => \core\task\adhoc_task_test
- \core\scheduled_task_test => \core\task\scheduled_task_test
- \core\calendar_cron_task_test => \core\task\calendar_cron_task_test
- \core\h5p_get_content_types_task_test => \core\task\h5p_get_content_types_task_test
- \core\task_database_logger_test => \core\task\database_logger_test
- \core\task_logging_test => \core\task\logging_test
- The following event tests have been moved within level2 directory:
- \core\event_context_locked_test => \core\event\context_locked_test
- \core\event_deprecated_test => \core\event\deprecated_test
- \core\event_grade_deleted_test => \core\event\grade_deleted_test
- \core\event_profile_field_test => \core\event\profile_field_test
- \core\event_unknown_logged_test => \core\event\unknown_logged_test
- \core\event_user_graded_test => \core\event\user_graded_test
- \core\event_user_password_updated_test => \core\event\user_password_updated_test
- The following output tests have been moved within level2 directory:
- \core\mustache_template_finder_test => \core\output\mustache_template_finder_test
- \core\mustache_template_source_loader_test => \core\output\mustache_template_source_loader_test
- \core\output_mustache_helper_collection_test => \core\output\mustache_helper_collection_test
- The following tests have been moved to their correct tests directories:
- lib/tests/time_splittings_test.php => analytics/tests/time_splittings_test.php
- All the classes and tests under lib/filebrowser and lib/filestorage
belong to core, not to core_files. Some day we should move
them to their correct subsystem.
- All the classes and tests under lib/grade belong to core, not
to core_grades. Some day we should move them to their correct
subsystem.
- The core_grades_external class and its \core\grades_external_test
unit test should belong to the grades subsystem or, alternatively,
to \core\external, they both should be moved together.
- The core_grading_external class and its \core\grading_external_test
unit test should belong to the grading subsystem or, alternatively,
to \core\external, they both should be moved together.
- The \core\message\message and \core\message\inbound (may be others)
classes, and their associated tests should go to the core_message
subsystem.
- The core_user class, and its associated tests should go to the
core_user subsystem.
- The \core\update namespace is plain wrong (update is not valid API)
and needs action 1) create it or 2) move elsewhere.
2022-07-01 16:43:34 +02:00
|
|
|
class prediction_actions_test extends \advanced_testcase {
|
2017-08-28 13:24:09 +02:00
|
|
|
|
2023-03-15 03:24:46 +07:00
|
|
|
/** @var model Store Model. */
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/** @var \stdClass Store model object. */
|
|
|
|
protected $modelobj;
|
|
|
|
|
|
|
|
/** @var \stdClass Course 1 record. */
|
|
|
|
protected $course1;
|
|
|
|
|
|
|
|
/** @var \stdClass Course 2 record. */
|
|
|
|
protected $course2;
|
|
|
|
|
|
|
|
/** @var \context_course Store Model. */
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
/** @var \stdClass Teacher 1 user record. */
|
|
|
|
protected $teacher1;
|
|
|
|
|
|
|
|
/** @var \stdClass Teacher 2 user record. */
|
|
|
|
protected $teacher2;
|
|
|
|
|
|
|
|
/** @var \stdClass Teacher 3 user record. */
|
|
|
|
protected $teacher3;
|
|
|
|
|
2017-08-28 13:24:09 +02:00
|
|
|
/**
|
|
|
|
* Common startup tasks
|
|
|
|
*/
|
2020-08-02 10:59:12 +02:00
|
|
|
public function setUp(): void {
|
2017-08-28 13:24:09 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->setAdminUser();
|
|
|
|
$target = \core_analytics\manager::get_target('test_target_shortname');
|
|
|
|
$indicators = array('test_indicator_max');
|
|
|
|
foreach ($indicators as $key => $indicator) {
|
|
|
|
$indicators[$key] = \core_analytics\manager::get_indicator($indicator);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->model = \core_analytics\model::create($target, $indicators);
|
|
|
|
$this->modelobj = $this->model->get_model_obj();
|
|
|
|
$this->model->enable('\core\analytics\time_splitting\single_range');
|
|
|
|
|
|
|
|
$this->resetAfterTest(true);
|
|
|
|
|
2019-11-04 09:30:36 +08:00
|
|
|
$this->course1 = $this->getDataGenerator()->create_course();
|
|
|
|
$this->course2 = $this->getDataGenerator()->create_course();
|
|
|
|
$this->context = \context_course::instance($this->course1->id);
|
2017-08-28 13:24:09 +02:00
|
|
|
|
|
|
|
$this->teacher1 = $this->getDataGenerator()->create_user();
|
|
|
|
$this->teacher2 = $this->getDataGenerator()->create_user();
|
2019-11-04 09:30:36 +08:00
|
|
|
$this->teacher3 = $this->getDataGenerator()->create_user();
|
2017-08-28 13:24:09 +02:00
|
|
|
|
2019-11-04 09:30:36 +08:00
|
|
|
$this->getDataGenerator()->enrol_user($this->teacher1->id, $this->course1->id, 'editingteacher');
|
|
|
|
$this->getDataGenerator()->enrol_user($this->teacher2->id, $this->course1->id, 'editingteacher');
|
|
|
|
$this->getDataGenerator()->enrol_user($this->teacher3->id, $this->course1->id, 'editingteacher');
|
2017-08-28 13:24:09 +02:00
|
|
|
|
|
|
|
// The only relevant fields are modelid, contextid and sampleid. I'm cheating and setting
|
|
|
|
// contextid as the course context so teachers can access these predictions.
|
|
|
|
$pred = new \stdClass();
|
|
|
|
$pred->modelid = $this->model->get_id();
|
|
|
|
$pred->contextid = $this->context->id;
|
2019-11-04 09:30:36 +08:00
|
|
|
$pred->sampleid = $this->course1->id;
|
2017-08-28 13:24:09 +02:00
|
|
|
$pred->rangeindex = 1;
|
|
|
|
$pred->prediction = 1;
|
|
|
|
$pred->predictionscore = 1;
|
|
|
|
$pred->calculations = json_encode(array('test_indicator_max' => 1));
|
|
|
|
$pred->timecreated = time();
|
|
|
|
$DB->insert_record('analytics_predictions', $pred);
|
|
|
|
|
2019-11-04 09:30:36 +08:00
|
|
|
$pred->sampleid = $this->course2->id;
|
2017-08-28 13:24:09 +02:00
|
|
|
$DB->insert_record('analytics_predictions', $pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test_get_predictions
|
|
|
|
*/
|
|
|
|
public function test_action_executed() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
|
|
|
|
|
|
|
|
// Teacher 2 flags a prediction (it doesn't matter which one) as fixed.
|
|
|
|
$this->setUser($this->teacher2);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
|
|
|
|
$prediction = reset($predictions);
|
|
|
|
$prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
|
|
|
|
|
2019-05-14 12:04:51 +02:00
|
|
|
$recordset = $this->model->get_prediction_actions($this->context);
|
|
|
|
$this->assertCount(1, $recordset);
|
|
|
|
$recordset->close();
|
2017-08-28 13:24:09 +02:00
|
|
|
$this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
|
|
|
|
$action = $DB->get_record('analytics_prediction_actions', array('userid' => $this->teacher2->id));
|
|
|
|
$this->assertEquals(\core_analytics\prediction::ACTION_FIXED, $action->actionname);
|
|
|
|
|
2019-09-26 09:50:04 +08:00
|
|
|
$prediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model->get_target());
|
2019-05-14 12:04:51 +02:00
|
|
|
$recordset = $this->model->get_prediction_actions($this->context);
|
|
|
|
$this->assertCount(2, $recordset);
|
|
|
|
$recordset->close();
|
2017-08-28 13:24:09 +02:00
|
|
|
$this->assertEquals(2, $DB->count_records('analytics_prediction_actions'));
|
|
|
|
}
|
|
|
|
|
2021-02-16 13:17:51 +01:00
|
|
|
/**
|
|
|
|
* Data provider for test_get_executed_actions.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function execute_actions_provider(): array {
|
|
|
|
return [
|
|
|
|
'Empty actions with no filter' => [
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
0
|
|
|
|
],
|
|
|
|
'Empty actions with filter' => [
|
|
|
|
[],
|
|
|
|
[\core_analytics\prediction::ACTION_FIXED],
|
|
|
|
0
|
|
|
|
],
|
|
|
|
'Multiple actions with no filter' => [
|
|
|
|
[
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
3
|
|
|
|
],
|
|
|
|
'Multiple actions applying filter' => [
|
|
|
|
[
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
|
|
|
|
],
|
|
|
|
[\core_analytics\prediction::ACTION_FIXED],
|
|
|
|
2
|
|
|
|
],
|
|
|
|
'Multiple actions not applying filter' => [
|
|
|
|
[
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
|
|
|
|
],
|
|
|
|
[\core_analytics\prediction::ACTION_NOT_APPLICABLE],
|
|
|
|
0
|
|
|
|
],
|
|
|
|
'Multiple actions with multiple filter' => [
|
|
|
|
[
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_FIXED,
|
|
|
|
\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED
|
|
|
|
],
|
|
|
|
[\core_analytics\prediction::ACTION_FIXED, \core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED],
|
|
|
|
3
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for get_executed_actions() function.
|
|
|
|
*
|
|
|
|
* @dataProvider execute_actions_provider
|
|
|
|
* @param array $actionstoexecute An array of actions to execute
|
|
|
|
* @param array $actionnamefilter Actions to filter
|
|
|
|
* @param int $returned Number of actions returned
|
|
|
|
*
|
|
|
|
* @covers \core_analytics\prediction::get_executed_actions
|
|
|
|
*/
|
|
|
|
public function test_get_executed_actions(array $actionstoexecute, array $actionnamefilter, int $returned) {
|
|
|
|
|
|
|
|
$this->setUser($this->teacher2);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
|
|
|
|
$prediction = reset($predictions);
|
|
|
|
$target = $this->model->get_target();
|
|
|
|
foreach($actionstoexecute as $action) {
|
|
|
|
$prediction->action_executed($action, $target);
|
|
|
|
}
|
|
|
|
|
|
|
|
$filteredactions = $prediction->get_executed_actions($actionnamefilter);
|
|
|
|
$this->assertCount($returned, $filteredactions);
|
|
|
|
}
|
|
|
|
|
2017-08-28 13:24:09 +02:00
|
|
|
/**
|
|
|
|
* test_get_predictions
|
|
|
|
*/
|
|
|
|
public function test_get_predictions() {
|
2019-11-04 09:30:36 +08:00
|
|
|
global $DB;
|
2017-08-28 13:24:09 +02:00
|
|
|
|
|
|
|
// Already logged in as admin.
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
|
|
|
|
$this->assertCount(2, $predictions);
|
|
|
|
|
|
|
|
$this->setUser($this->teacher1);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
|
|
|
|
$this->assertCount(2, $predictions);
|
|
|
|
|
|
|
|
$this->setUser($this->teacher2);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, false);
|
|
|
|
$this->assertCount(2, $predictions);
|
|
|
|
|
2019-09-26 09:50:04 +08:00
|
|
|
// Teacher 2 flags a prediction (it doesn't matter which one).
|
2017-08-28 13:24:09 +02:00
|
|
|
$prediction = reset($predictions);
|
|
|
|
$prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
|
2019-09-26 09:50:04 +08:00
|
|
|
$prediction->action_executed(\core_analytics\prediction::ACTION_NOT_APPLICABLE, $this->model->get_target());
|
|
|
|
$prediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model->get_target());
|
2017-08-28 13:24:09 +02:00
|
|
|
|
2019-05-14 12:04:51 +02:00
|
|
|
$recordset = $this->model->get_prediction_actions($this->context);
|
2019-09-26 09:50:04 +08:00
|
|
|
$this->assertCount(3, $recordset);
|
2019-05-14 12:04:51 +02:00
|
|
|
$recordset->close();
|
|
|
|
|
2017-08-28 13:24:09 +02:00
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
|
|
|
|
$this->assertCount(1, $predictions);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, false);
|
|
|
|
$this->assertCount(2, $predictions);
|
|
|
|
|
|
|
|
// Teacher 1 can still see both predictions.
|
|
|
|
$this->setUser($this->teacher1);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, true);
|
|
|
|
$this->assertCount(2, $predictions);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context, false);
|
|
|
|
$this->assertCount(2, $predictions);
|
2019-05-14 12:04:51 +02:00
|
|
|
|
|
|
|
$recordset = $this->model->get_prediction_actions($this->context);
|
2019-09-26 09:50:04 +08:00
|
|
|
$this->assertCount(3, $recordset);
|
2019-05-14 12:04:51 +02:00
|
|
|
$recordset->close();
|
2019-11-04 09:30:36 +08:00
|
|
|
|
|
|
|
// Trying with a deleted course.
|
|
|
|
$DB->delete_records('course', ['id' => $this->course2->id]);
|
|
|
|
$this->setUser($this->teacher3);
|
|
|
|
list($ignored, $predictions) = $this->model->get_predictions($this->context);
|
|
|
|
$this->assertCount(1, $predictions);
|
|
|
|
reset($predictions)->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
|
|
|
|
$this->assertEmpty($this->model->get_predictions($this->context));
|
2017-08-28 13:24:09 +02:00
|
|
|
}
|
|
|
|
}
|