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
+177
View File
@@ -0,0 +1,177 @@
<?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/>.
/**
* Grading methods steps definitions.
*
* @package core_grading
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
use Behat\Gherkin\Node\TableNode as TableNode;
/**
* Generic grading methods step definitions.
*
* @package core_grading
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_grading extends behat_base {
/**
* Goes to the selected advanced grading page. You should be in the course page when this step begins.
*
* @Given /^I go to "(?P<activity_name_string>(?:[^"]|\\")*)" advanced grading page$/
* @param string $activityname
*/
public function i_go_to_advanced_grading_page($activityname) {
try {
$this->execute("behat_general::i_click_on_in_the", [$this->escape($activityname), 'link', 'page', 'region']);
} catch (Exception $e) {
$this->execute('behat_navigation::go_to_breadcrumb_location', $this->escape($activityname));
}
$this->execute('behat_navigation::i_navigate_to_in_current_page_administration',
get_string('gradingmanagement', 'grading'));
}
/**
* Goes to the selected advanced grading definition page. You should be in the course page when this step begins.
*
* @Given /^I go to "(?P<activity_name_string>(?:[^"]|\\")*)" advanced grading definition page$/
* @param string $activityname
*/
public function i_go_to_advanced_grading_definition_page($activityname) {
// Transforming to literals, probably not necessary, just in case.
$newactionliteral = behat_context_helper::escape(get_string("manageactionnew", "grading"));
$editactionliteral = behat_context_helper::escape(get_string("manageactionedit", "grading"));
// Working both when adding and editing.
$definitionxpath = "//a[@class='action btn btn-lg']" .
"[./descendant::*[contains(., $newactionliteral) or contains(., $editactionliteral)]]";
$this->execute('behat_grading::i_go_to_advanced_grading_page', $this->escape($activityname));
$this->execute("behat_general::i_click_on", array($this->escape($definitionxpath), "xpath_element"));
}
/**
* Goes to the student's advanced grading page.
*
* @Given /^I go to "(?P<user_fullname_string>(?:[^"]|\\")*)" "(?P<activity_name_string>(?:[^"]|\\")*)" activity advanced grading page$/
* @param string $userfullname The user full name including firstname and lastname.
* @param string $activityname The activity name
*/
public function i_go_to_activity_advanced_grading_page($userfullname, $activityname) {
// Step to access the user grade page from the grading page.
$gradetext = get_string('gradeverb');
$this->execute('behat_navigation::go_to_breadcrumb_location', $this->escape($activityname));
$this->execute('behat_general::click_link', get_string('viewgrading', 'mod_assign'));
$this->execute('behat_general::i_click_on_in_the',
array(
$this->escape($gradetext),
'link',
$this->escape($userfullname),
'table_row'
));
}
/**
* Publishes current activity grading defined form as a public template.
*
* @Given /^I publish "(?P<activity_name_string>(?:[^"]|\\")*)" grading form definition as a public template$/
* @param string $activityname
*/
public function i_publish_grading_form_definition_as_a_public_template($activityname) {
$this->execute('behat_grading::i_go_to_advanced_grading_page', $this->escape($activityname));
$this->execute("behat_general::i_click_on", array($this->escape(get_string("manageactionshare", "grading")), "link"));
$this->execute('behat_forms::press_button', get_string('continue'));
}
/**
* Sets a previously created grading form as the activity grading form.
*
* @Given /^I set "(?P<activity_name_string>(?:[^"]|\\")*)" activity to use "(?P<grading_form_template_string>(?:[^"]|\\")*)" grading form$/
* @param string $activityname
* @param string $templatename
*/
public function i_set_activity_to_use_grading_form($activityname, $templatename) {
$templateliteral = behat_context_helper::escape($templatename);
$templatexpath = "//h2[@class='template-name'][contains(., $templateliteral)]/" .
"following-sibling::div[contains(concat(' ', normalize-space(@class), ' '), ' template-actions ')]";
// Should work with both templates and own forms.
$literaltemplate = behat_context_helper::escape(get_string('templatepick', 'grading'));
$literalownform = behat_context_helper::escape(get_string('templatepickownform', 'grading'));
$usetemplatexpath = "/a[./descendant::div[text()=$literaltemplate]]|" .
"/a[./descendant::div[text()=$literalownform]]";
$this->execute('behat_grading::i_go_to_advanced_grading_page', $this->escape($activityname));
$this->execute('behat_general::click_link', $this->escape(get_string('manageactionclone', 'grading')));
$this->execute('behat_forms::i_set_the_field_to', array(get_string('searchownforms', 'grading'), 1));
$this->execute('behat_general::i_click_on_in_the',
array(get_string('search'), "button", "region-main", "region")
);
$this->execute('behat_general::i_click_on_in_the',
array($this->escape($usetemplatexpath), "xpath_element", $this->escape($templatexpath), "xpath_element")
);
$this->execute('behat_forms::press_button', get_string('continue'));
}
/**
* Saves the current page advanced grading form.
*
* @When /^I save the advanced grading form$/
*/
public function i_save_the_advanced_grading_form() {
$this->execute('behat_forms::press_button', get_string('savechanges'));
$this->execute('behat_general::i_click_on', array($this->escape(get_string('editsettings')), 'link'));
$this->execute('behat_forms::press_button', get_string('cancel'));
$this->execute('behat_general::click_link', get_string('viewgrading', 'mod_assign'));
}
/**
* Grades an activity using advanced grading. Note the grade is set by other steps, depending on the grading method.
*
* @Given /^I complete the advanced grading form with these values:$/
* @param TableNode $data
*/
public function i_complete_the_advanced_grading_form_with_these_values(TableNode $data) {
$this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
$this->execute('behat_grading::i_save_the_advanced_grading_form');
}
}
+56
View File
@@ -0,0 +1,56 @@
<?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/>.
/**
* Generator for the core_grading subsystem generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generator for the core_grading subsystem generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_grading_generator extends component_generator_base {
/**
* Create an instance of an advanced grading area.
*
* @param context $context
* @param string $component
* @param string $areaname An area belonging to the specified component
* @param string $method An available gradingform type
* @return gradingform_controller The controller for the created instance
*/
public function create_instance(context $context, string $component, string $areaname, string $method): gradingform_controller {
require_once(__DIR__ . '/../../lib.php');
$manager = get_grading_manager($context, $component, $areaname);
$manager->set_active_method($method);
return $manager->get_controller($method);
}
}
+72
View File
@@ -0,0 +1,72 @@
<?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/>.
/**
* Generator testcase for the gradingforum_rubric generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_grading;
use advanced_testcase;
use context_module;
use gradingform_controller;
use gradingform_rubric_controller;
/**
* Generator testcase for the core_grading generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_test extends advanced_testcase {
/**
* Test gradingform controller creation.
*/
public function test_create_instance(): void {
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$gradinggenerator = $generator->get_plugin_generator('core_grading');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
// The assignment module has an itemumber 0 which is an advanced grading area called 'submissions'.
$component = 'mod_assign';
$area = 'submissions';
$controller = $gradinggenerator->create_instance($context, $component, $area, 'rubric');
// This should be a rubric.
$this->assertInstanceOf(gradingform_controller::class, $controller);
$this->assertInstanceOf(gradingform_rubric_controller::class, $controller);
$this->assertEquals($context, $controller->get_context());
$this->assertEquals($component, $controller->get_component());
$this->assertEquals($area, $controller->get_area());
}
}
@@ -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 core_grading;
use grading_manager;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/grade/grading/lib.php'); // Include the code to test
/**
* Test cases for the grading manager API
*
* @package core_grading
* @category test
* @copyright 2011 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class grading_manager_test extends \advanced_testcase {
public function test_basic_instantiation(): void {
$manager1 = get_grading_manager();
$fakecontext = (object)array(
'id' => 42,
'contextlevel' => CONTEXT_MODULE,
'instanceid' => 22,
'path' => '/1/3/15/42',
'depth' => 4);
$manager2 = get_grading_manager($fakecontext);
$manager3 = get_grading_manager($fakecontext, 'assignment_upload');
$manager4 = get_grading_manager($fakecontext, 'assignment_upload', 'submission');
}
/**
* Unit test to set and get grading areas
*/
public function test_set_and_get_grading_area(): void {
global $DB;
$this->resetAfterTest(true);
//sleep(2); // to make sure the microtime will always return unique values // No sleeping in tests!!! --skodak
$areaname1 = 'area1-' . (string)microtime(true);
$areaname2 = 'area2-' . (string)microtime(true);
$fakecontext = (object)array(
'id' => 42,
'contextlevel' => CONTEXT_MODULE,
'instanceid' => 22,
'path' => '/1/3/15/42',
'depth' => 4);
// non-existing area
$gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname1);
$this->assertNull($gradingman->get_active_method());
// creates area implicitly and sets active method
$this->assertTrue($gradingman->set_active_method('rubric'));
$this->assertEquals('rubric', $gradingman->get_active_method());
// repeat setting of already set active method
$this->assertFalse($gradingman->set_active_method('rubric'));
// switch the manager to another area
$gradingman->set_area($areaname2);
$this->assertNull($gradingman->get_active_method());
// switch back and ask again
$gradingman->set_area($areaname1);
$this->assertEquals('rubric', $gradingman->get_active_method());
// attempting to set an invalid method
$this->expectException(\moodle_exception::class);
$gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name');
}
/**
* Unit test to check the tokenize method
*/
public function test_tokenize(): void {
$UTFfailuremessage = 'A test using UTF-8 characters has failed. Consider updating PHP and PHP\'s PCRE or INTL extensions (MDL-30494)';
$needle = " šašek, \n\n \r a král; \t";
$tokens = grading_manager::tokenize($needle);
$this->assertEquals(2, count($tokens), $UTFfailuremessage);
$this->assertTrue(in_array('šašek', $tokens), $UTFfailuremessage);
$this->assertTrue(in_array('král', $tokens), $UTFfailuremessage);
$needle = ' " šašek a král " ';
$tokens = grading_manager::tokenize($needle);
$this->assertEquals(1, count($tokens));
$this->assertTrue(in_array('šašek a král', $tokens));
$needle = '""';
$tokens = grading_manager::tokenize($needle);
$this->assertTrue(empty($tokens));
$needle = '"0"';
$tokens = grading_manager::tokenize($needle);
$this->assertEquals(1, count($tokens));
$this->assertTrue(in_array('0', $tokens));
$needle = '<span>Aha</span>, then who\'s a bad guy here he?';
$tokens = grading_manager::tokenize($needle);
$this->assertEquals(8, count($tokens));
$this->assertTrue(in_array('span', $tokens)); // Extracted the tag name
$this->assertTrue(in_array('Aha', $tokens));
$this->assertTrue(in_array('who', $tokens)); // Removed the trailing 's
$this->assertTrue(!in_array('a', $tokens)); //Single letter token was dropped
$this->assertTrue(in_array('he', $tokens)); // Removed the trailing ?
$needle = 'grammar, "english language"';
$tokens = grading_manager::tokenize($needle);
$this->assertTrue(in_array('grammar', $tokens));
$this->assertTrue(in_array('english', $tokens));
$this->assertTrue(in_array('language', $tokens));
$this->assertTrue(!in_array('english language', $tokens)); // Quoting part of the string is not supported
}
}
@@ -0,0 +1,130 @@
<?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 core_grading\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for the Grading API's privacy legacy_polyfill.
*
* @package core_grading
* @category test
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class legacy_polyfill_test extends \advanced_testcase {
/**
* Test that the core_grading\privacy\legacy_polyfill works and that the static _export_gradingform_instance_data can be called.
*/
public function test_export_gradingform_instance_data(): void {
$context = \context_system::instance();
$mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class);
$mock->expects($this->once())
->method('get_return_value')
->with('_export_gradingform_instance_data', [$context, 3, ['subcontext']]);
test_legacy_polyfill_gradingform_provider::$mock = $mock;
test_legacy_polyfill_gradingform_provider::export_gradingform_instance_data($context, 3, ['subcontext']);
}
/**
* Test for _get_metadata shim.
*/
public function test_get_metadata(): void {
$collection = new \core_privacy\local\metadata\collection('core_gradingform');
$this->assertSame($collection, test_legacy_polyfill_gradingform_provider::get_metadata($collection));
}
/**
* Test the _delete_gradingform_for_instances shim.
*/
public function test_delete_gradingform_for_instances(): void {
$context = \context_system::instance();
$mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class);
$mock->expects($this->once())
->method('get_return_value')
->with('_delete_gradingform_for_instances', [[3, 17]]);
test_legacy_polyfill_gradingform_provider::$mock = $mock;
test_legacy_polyfill_gradingform_provider::delete_gradingform_for_instances([3, 17]);
}
}
/**
* Legacy polyfill test class for the gradingform_provider.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_legacy_polyfill_gradingform_provider implements
\core_privacy\local\metadata\provider,
\core_grading\privacy\gradingform_provider_v2 {
use \core_grading\privacy\gradingform_legacy_polyfill;
use \core_privacy\local\legacy_polyfill;
/**
* @var test_legacy_polyfill_gradingform_provider $mock.
*/
public static $mock = null;
/**
* Export user data relating to an instance ID.
*
* @param \context $context Context to use with the export writer.
* @param int $instanceid The instance ID to export data for.
* @param array $subcontext The directory to export this data to.
*/
protected static function _export_gradingform_instance_data(\context $context, $instanceid, $subcontext) {
static::$mock->get_return_value(__FUNCTION__, func_get_args());
}
/**
* Deletes all user data related to the provided instance IDs.
*
* @param array $instanceids The instance IDs to delete information from.
*/
protected static function _delete_gradingform_for_instances($instanceids) {
static::$mock->get_return_value(__FUNCTION__, func_get_args());
}
/**
* Returns metadata about this plugin.
*
* @param \core_privacy\local\metadata\collection $collection The initialised collection to add items to.
* @return \core_privacy\local\metadata\collection A listing of user data stored through this system.
*/
protected static function _get_metadata(\core_privacy\local\metadata\collection $collection) {
return $collection;
}
}
/**
* Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params.
*
* @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_gradingform_legacy_polyfill_mock_wrapper {
/**
* Get the return value for the specified item.
*/
public function get_return_value() {
}
}
@@ -0,0 +1,502 @@
<?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/>.
/**
* Privacy tests for core_grading.
*
* @package core_grading
* @category test
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_grading\privacy;
defined('MOODLE_INTERNAL') || die();
global $CFG;
use core_privacy\tests\provider_testcase;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\transform;
use core_privacy\local\request\writer;
use core_grading\privacy\provider;
/**
* Privacy tests for core_grading.
*
* @copyright 2018 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends provider_testcase {
/** @var stdClass User without data. */
protected $user0;
/** @var stdClass User with data. */
protected $user1;
/** @var stdClass User with data. */
protected $user2;
/** @var context context_module of an activity without grading definitions. */
protected $instancecontext0;
/** @var context context_module of the activity where the grading definitions are. */
protected $instancecontext1;
/** @var context context_module of the activity where the grading definitions are. */
protected $instancecontext2;
/**
* Test getting the context for the user ID related to this plugin.
*/
public function test_get_contexts_for_userid(): void {
global $DB;
$this->resetAfterTest();
$this->grading_setup_test_scenario_data();
$this->assertCount(2, $DB->get_records('grading_definitions'));
// User1 has created grading definitions for instance1 and instance2.
$contextlist = provider::get_contexts_for_userid($this->user1->id);
$this->assertCount(2, $contextlist);
$this->assertContainsEquals($this->instancecontext1->id, $contextlist->get_contextids());
$this->assertContainsEquals($this->instancecontext2->id, $contextlist->get_contextids());
$this->assertNotContainsEquals($this->instancecontext0->id, $contextlist->get_contextids());
// User2 has only modified grading definitions for instance2.
$contextlist = provider::get_contexts_for_userid($this->user2->id);
$this->assertCount(1, $contextlist);
$this->assertContainsEquals($this->instancecontext2->id, $contextlist->get_contextids());
// User0 hasn't created or modified any grading definition.
$contextlist = provider::get_contexts_for_userid($this->user0->id);
$this->assertCount(0, $contextlist);
}
/**
* Test retrieval of user ids in a given context.
*/
public function test_get_users_in_context(): void {
$this->resetAfterTest();
$this->grading_setup_test_scenario_data();
// Instance two has one user who created the definitions and another who modified it.
$userlist = new \core_privacy\local\request\userlist($this->instancecontext2, 'core_grading');
provider::get_users_in_context($userlist);
// Check that we get both.
$this->assertCount(2, $userlist->get_userids());
}
/**
* Export for a user with no grading definitions created or modified will not have any data exported.
*/
public function test_export_user_data_no_content(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$context = \context_system::instance();
/** @var \core_privacy\tests\request\content_writer $writer */
$writer = writer::with_context($context);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($user->id, $context, 'core_grading');
$this->assertFalse(writer::with_context($context)->has_any_data());
}
/**
* Test that data is exported correctly for this plugin.
*/
public function test_export_user_data(): void {
global $DB;
$this->resetAfterTest();
$now = time();
$defnameprefix = 'fakename';
$this->grading_setup_test_scenario_data($defnameprefix, $now);
$this->assertCount(2, $DB->get_records('grading_definitions'));
// Validate exported data: instance1 - user0 has NO data.
$this->setUser($this->user0);
writer::reset();
$writer = writer::with_context($this->instancecontext1);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($this->user0->id, $this->instancecontext1, 'core_grading');
$data = $writer->get_data([get_string('gradingmethod', 'grading')]);
$this->assertEmpty($data);
// Validate exported data: instance0 - user1 has NO data.
$this->setUser($this->user1);
writer::reset();
$writer = writer::with_context($this->instancecontext0);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($this->user1->id, $this->instancecontext0, 'core_grading');
$data = $writer->get_data([get_string('gradingmethod', 'grading')]);
$this->assertEmpty($data);
// Validate exported data: instance1 - user1 has data (user has created and modified it).
writer::reset();
$writer = writer::with_context($this->instancecontext1);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($this->user1->id, $this->instancecontext1, 'core_grading');
$data = $writer->get_data([get_string('gradingmethod', 'grading')]);
$this->assertCount(1, $data->definitions);
$firstkey = reset($data->definitions);
$this->assertNotEmpty($firstkey->name);
$this->assertEquals('test_method', $firstkey->method);
$this->assertEquals(transform::datetime($now), $firstkey->timecreated);
$this->assertEquals($this->user1->id, $firstkey->usercreated);
$this->assertEquals($defnameprefix.'1', $firstkey->name);
// Validate exported data: instance2 - user1 has data (user has created it).
writer::reset();
$writer = writer::with_context($this->instancecontext2);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($this->user1->id, $this->instancecontext2, 'core_grading');
$data = $writer->get_data([get_string('gradingmethod', 'grading')]);
$this->assertCount(1, $data->definitions);
$firstkey = reset($data->definitions);
$this->assertNotEmpty($firstkey->name);
$this->assertEquals('test_method', $firstkey->method);
$this->assertEquals(transform::datetime($now), $firstkey->timecreated);
$this->assertEquals($this->user1->id, $firstkey->usercreated);
$this->assertEquals($defnameprefix.'2', $firstkey->name);
// Validate exported data: instance1 - user2 has NO data.
$this->setUser($this->user2);
writer::reset();
$writer = writer::with_context($this->instancecontext1);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($this->user2->id, $this->instancecontext1, 'core_grading');
$data = $writer->get_data([get_string('gradingmethod', 'grading')]);
$this->assertEmpty($data);
// Validate exported data: instance2 - user2 has data (user has modified it).
$this->setUser($this->user2);
writer::reset();
$writer = writer::with_context($this->instancecontext2);
$this->assertFalse($writer->has_any_data());
$this->export_context_data_for_user($this->user2->id, $this->instancecontext2, 'core_grading');
$data = $writer->get_data([get_string('gradingmethod', 'grading')]);
$this->assertCount(1, $data->definitions);
}
/**
* Test for provider::delete_data_for_all_users_in_context().
*/
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
$this->resetAfterTest();
$this->grading_setup_test_scenario_data();
// Before deletion, we should have 2 grading_definitions.
$this->assertCount(2, $DB->get_records('grading_definitions'));
// Delete data.
provider::delete_data_for_all_users_in_context($this->instancecontext0);
provider::delete_data_for_all_users_in_context($this->instancecontext1);
provider::delete_data_for_all_users_in_context($this->instancecontext2);
// Before deletion, we should have same grading_definitions (nothing was deleted).
$this->assertCount(2, $DB->get_records('grading_definitions'));
}
/**
* Test for provider::delete_data_for_user().
*/
public function test_delete_data_for_user(): void {
global $DB;
$this->resetAfterTest();
$this->grading_setup_test_scenario_data();
// Before deletion, we should have 2 grading_definitions.
$this->assertCount(2, $DB->get_records('grading_definitions'));
// Delete data for $user0.
$contextlist = provider::get_contexts_for_userid($this->user0->id);
$approvedcontextlist = new approved_contextlist(
$this->user0,
'core_grading',
$contextlist->get_contextids()
);
provider::delete_data_for_user($approvedcontextlist);
// Delete data for $user1.
$contextlist = provider::get_contexts_for_userid($this->user1->id);
$approvedcontextlist = new approved_contextlist(
$this->user1,
'core_grading',
$contextlist->get_contextids()
);
provider::delete_data_for_user($approvedcontextlist);
// Delete data for $user2.
$contextlist = provider::get_contexts_for_userid($this->user2->id);
$approvedcontextlist = new approved_contextlist(
$this->user2,
'core_grading',
$contextlist->get_contextids()
);
provider::delete_data_for_user($approvedcontextlist);
// Before deletion, we should have same grading_definitions (nothing was deleted).
$this->assertCount(2, $DB->get_records('grading_definitions'));
}
/**
* Test exporting user data relating to an item ID.
*/
public function test_export_item_data(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$user = $this->getDataGenerator()->create_user();
$guidegenerator = \testing_util::get_data_generator()->get_plugin_generator('gradingform_guide');
$this->setUser($user);
$modulecontext = \context_module::instance($module->cmid);
$controller = $guidegenerator->get_test_guide($modulecontext);
// In the situation of mod_assign this would be the id from assign_grades.
$itemid = 1;
$instance = $controller->create_instance($user->id, $itemid);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);
$instance->update($data);
$instanceid = $instance->get_data('id');
provider::export_item_data($modulecontext, $itemid, ['Test']);
$data = (array) writer::with_context($modulecontext)->get_data(['Test', 'Marking guide', $instance->get_data('id')]);
$this->assertCount(2, $data);
$this->assertEquals('This user made several mistakes.', $data['Spelling mistakes']->remark);
$this->assertEquals(5, $data['Spelling mistakes']->score);
$this->assertEquals('This user has two pictures.', $data['Pictures']->remark);
$this->assertEquals(10, $data['Pictures']->score);
}
/**
* Test deleting user data related to a context and item ID.
*/
public function test_delete_instance_data(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$user = $this->getDataGenerator()->create_user();
$guidegenerator = \testing_util::get_data_generator()->get_plugin_generator('gradingform_guide');
$this->setUser($user);
$modulecontext = \context_module::instance($module->cmid);
$controller = $guidegenerator->get_test_guide($modulecontext);
// In the situation of mod_assign this would be the id from assign_grades.
$itemid = 1;
$instance = $controller->create_instance($user->id, $itemid);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);
$instance->update($data);
$itemid = 2;
$instance = $controller->create_instance($user->id, $itemid);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid,
25, 'This user made no mistakes.',
5, 'This user has one picture.'
);
$instance->update($data);
// Check how many records we have in the fillings table.
$records = $DB->get_records('gradingform_guide_fillings');
$this->assertCount(4, $records);
// Let's delete one of the instances (the last one would be the easiest).
provider::delete_instance_data($modulecontext, $itemid);
$records = $DB->get_records('gradingform_guide_fillings');
$this->assertCount(2, $records);
foreach ($records as $record) {
$this->assertNotEquals($instance->get_id(), $record->instanceid);
}
// This will delete all the rest of the instances for this context.
provider::delete_instance_data($modulecontext);
$records = $DB->get_records('gradingform_guide_fillings');
$this->assertEmpty($records);
}
/**
* Test the deletion of multiple instances at once.
*/
public function test_delete_data_for_instances(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$guidegenerator = \testing_util::get_data_generator()->get_plugin_generator('gradingform_guide');
$this->setUser($user1);
$modulecontext = \context_module::instance($module->cmid);
$controller = $guidegenerator->get_test_guide($modulecontext);
// In the situation of mod_assign this would be the id from assign_grades.
$itemid1 = 1;
$instance1 = $controller->create_instance($user1->id, $itemid1);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid1,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);
$instance1->update($data);
$itemid2 = 2;
$instance2 = $controller->create_instance($user2->id, $itemid2);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid2,
15, 'This user made a couple of mistakes.',
10, 'This user has one picture.'
);
$instance2->update($data);
$itemid3 = 3;
$instance3 = $controller->create_instance($user3->id, $itemid3);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid3,
20, 'This user made one mistakes.',
10, 'This user has one picture.'
);
$instance3->update($data);
$records = $DB->get_records('gradingform_guide_fillings');
$this->assertCount(6, $records);
// Delete all user data for items 1 and 3.
provider::delete_data_for_instances($modulecontext, [$itemid1, $itemid3]);
$records = $DB->get_records('gradingform_guide_fillings');
$this->assertCount(2, $records);
$instanceid = $instance2->get_data('id');
// The instance id should match for all remaining records.
foreach ($records as $record) {
$this->assertEquals($instanceid, $record->instanceid);
}
}
/**
* Helper function to setup the environment.
*
* course
* |
* +--instance0 (assignment)
* | |
* +--instance1 (assignment)
* | |
* | +--grading_definition1 (created and modified by user1)
* | |
* +--instance2 (assignment)
* | |
* | +--grading_definition2 (created by user1 and modified by user2)
*
*
* user0 hasn't any data.
*
* @param string $defnameprefix
* @param timestamp $now
*/
protected function grading_setup_test_scenario_data($defnameprefix = null, $now = null) {
global $DB;
$this->user0 = $this->getDataGenerator()->create_user();
$this->user1 = $this->getDataGenerator()->create_user();
$this->user2 = $this->getDataGenerator()->create_user();
// Create a course.
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
// Create some assignment instances.
$params = (object)array(
'course' => $course->id,
'name' => 'Testing instance'
);
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$instance0 = $generator->create_instance($params);
$cm0 = get_coursemodule_from_instance('assign', $instance0->id);
$this->instancecontext0 = \context_module::instance($cm0->id);
$instance1 = $generator->create_instance($params);
$cm1 = get_coursemodule_from_instance('assign', $instance1->id);
$this->instancecontext1 = \context_module::instance($cm1->id);
$instance2 = $generator->create_instance($params);
$cm2 = get_coursemodule_from_instance('assign', $instance2->id);
$this->instancecontext2 = \context_module::instance($cm2->id);
// Create fake grading areas.
$fakearea1 = (object)array(
'contextid' => $this->instancecontext1->id,
'component' => 'mod_assign',
'areaname' => 'submissions',
'activemethod' => 'test_method'
);
$fakeareaid1 = $DB->insert_record('grading_areas', $fakearea1);
$fakearea2 = clone($fakearea1);
$fakearea2->contextid = $this->instancecontext2->id;
$fakeareaid2 = $DB->insert_record('grading_areas', $fakearea2);
// Create fake grading definitions.
if (empty($now)) {
$now = time();
}
if (empty($defnameprefix)) {
$defnameprefix = 'fakename';
}
$fakedefinition1 = (object)array(
'areaid' => $fakeareaid1,
'method' => 'test_method',
'name' => $defnameprefix.'1',
'status' => 0,
'timecreated' => $now,
'usercreated' => $this->user1->id,
'timemodified' => $now + 1,
'usermodified' => $this->user1->id,
);
$fakedefid1 = $DB->insert_record('grading_definitions', $fakedefinition1);
$fakedefinition2 = clone($fakedefinition1);
$fakedefinition2->areaid = $fakeareaid2;
$fakedefinition2->name = $defnameprefix.'2';
$fakedefinition2->usermodified = $this->user2->id;
$fakedefid2 = $DB->insert_record('grading_definitions', $fakedefinition2);
}
}