mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
Merge branch 'MDL-73004-master_access_unit_tests' of https://github.com/brickfield/moodle
This commit is contained in:
commit
10e4cec874
@ -16,6 +16,11 @@
|
||||
|
||||
namespace tool_brickfield;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/admin/tool/brickfield/tests/area_test_base.php');
|
||||
|
||||
/**
|
||||
* Class tool_brickfield_area_testcase
|
||||
*
|
||||
@ -23,8 +28,7 @@ namespace tool_brickfield;
|
||||
* @copyright 2020 onward: Brickfield Education Labs, https://www.brickfield.ie
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class area_test extends \advanced_testcase {
|
||||
|
||||
class area_test extends area_test_base {
|
||||
/**
|
||||
* Test for the area assign intro
|
||||
*/
|
||||
@ -46,7 +50,7 @@ class area_test extends \advanced_testcase {
|
||||
$resultsrs = $c->find_course_areas($course1->id);
|
||||
$resultsrs2 = $c->find_course_areas($course2->id);
|
||||
// Set up a results array from the recordset for easier testing.
|
||||
$results = array_merge(self::array_from_recordset($resultsrs), self::array_from_recordset($resultsrs2));
|
||||
$results = array_merge($this->array_from_recordset($resultsrs), $this->array_from_recordset($resultsrs2));
|
||||
$this->assertEquals([
|
||||
(object)[
|
||||
'type' => area_base::TYPE_FIELD,
|
||||
@ -76,7 +80,7 @@ class area_test extends \advanced_testcase {
|
||||
$event = \core\event\course_module_updated::create_from_cm($cm1);
|
||||
$relevantresultsrs = $c->find_relevant_areas($event);
|
||||
// Set up a relevantresults array from the recordset for easier testing.
|
||||
$relevantresults = self::array_from_recordset($relevantresultsrs);
|
||||
$relevantresults = $this->array_from_recordset($relevantresultsrs);
|
||||
$this->assertEquals([$results[0]], $relevantresults);
|
||||
}
|
||||
|
||||
@ -87,43 +91,57 @@ class area_test extends \advanced_testcase {
|
||||
$this->resetAfterTest();
|
||||
/** @var \core_question_generator $generator */
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||
$component = 'core_question';
|
||||
|
||||
list($category1, $course1, $qcat1, $questions1) = $generator->setup_course_and_questions('course');
|
||||
list($category2, $course2, $qcat2, $questions2) = $generator->setup_course_and_questions('category');
|
||||
list($category3, $course3, $qcat3, $questions3) = $generator->setup_course_and_questions('system');
|
||||
|
||||
$c = new \tool_brickfield\local\areas\core_question\questiontext();
|
||||
$results1 = self::array_from_recordset($c->find_course_areas($course1->id));
|
||||
$results2 = self::array_from_recordset($c->find_course_areas($course2->id));
|
||||
$results3 = self::array_from_recordset($c->find_course_areas($course3->id));
|
||||
$results4 = self::array_from_recordset($c->find_system_areas());
|
||||
// Set up a results array from the recordset for easier testing.
|
||||
// Set up results arrays from the recordset for easier testing.
|
||||
$course1areas = $this->array_from_recordset($c->find_course_areas($course1->id));
|
||||
$course2areas = $c->find_course_areas($course2->id);
|
||||
$course3areas = $c->find_course_areas($course3->id);
|
||||
$sysareas = $this->array_from_recordset($c->find_system_areas());
|
||||
|
||||
$this->assertCount(2, $results1);
|
||||
$this->assertCount(0, $results2);
|
||||
$this->assertCount(0, $results3);
|
||||
$this->assertCount(4, $results4);
|
||||
|
||||
// Validate the contexts, courseid and categoryid of the returned results.
|
||||
$this->assertEquals(\context_course::instance($course1->id)->id, $results1[0]->contextid);
|
||||
$this->assertEquals($course1->id, $results1[0]->courseid);
|
||||
$this->assertEmpty($results1[0]->categoryid);
|
||||
$this->assertEquals(\context_coursecat::instance($category2->id)->id, $results4[0]->contextid);
|
||||
$this->assertEquals(SITEID, $results4[0]->courseid);
|
||||
$this->assertEquals($category2->id, $results4[0]->categoryid);
|
||||
$this->assertEquals(\context_system::instance()->id, $results4[2]->contextid);
|
||||
$this->assertEmpty($results4[2]->categoryid);
|
||||
$this->assertEquals(SITEID, $results4[2]->courseid);
|
||||
// Results4 should contain id's for questions 1 and 2.
|
||||
$this->assertTrue(($questions3[0]->id == $results4[2]->itemid) || ($questions3[0]->id == $results4[3]->itemid));
|
||||
$this->assertTrue(($questions3[1]->id == $results4[2]->itemid) || ($questions3[1]->id == $results4[3]->itemid));
|
||||
// Assert the core_question area exists for the individual question's context, courseid and categoryid.
|
||||
$this->assert_area_in_array(
|
||||
$course1areas,
|
||||
$component,
|
||||
\context_course::instance($course1->id)->id,
|
||||
$questions1[0]->id,
|
||||
$course1->id,
|
||||
null
|
||||
);
|
||||
$this->assert_area_in_array(
|
||||
$sysareas,
|
||||
$component,
|
||||
\context_coursecat::instance($category2->id)->id,
|
||||
$questions2[0]->id,
|
||||
SITEID,
|
||||
$category2->id
|
||||
);
|
||||
$this->assert_area_in_array(
|
||||
$sysareas,
|
||||
$component,
|
||||
\context_system::instance()->id,
|
||||
$questions3[0]->id,
|
||||
SITEID,
|
||||
null
|
||||
);
|
||||
|
||||
// Emulate the question_created event.
|
||||
$event = \core\event\question_created::create_from_question_instance($questions1[1],
|
||||
\context_course::instance($course1->id));
|
||||
$relevantresultsrs = $c->find_relevant_areas($event);
|
||||
// Set up a relevantresults array from the recordset for easier testing.
|
||||
$relevantresults = self::array_from_recordset($relevantresultsrs);
|
||||
$this->assertEquals([$results1[1]], $relevantresults);
|
||||
$relevantresults = $this->array_from_recordset($c->find_relevant_areas($event));
|
||||
$this->assert_area_in_array(
|
||||
$course1areas,
|
||||
$component,
|
||||
\context_course::instance($relevantresults[0]->courseid)->id,
|
||||
$relevantresults[0]->itemid,
|
||||
$relevantresults[0]->courseid,
|
||||
$relevantresults[0]->categoryid
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -147,7 +165,7 @@ class area_test extends \advanced_testcase {
|
||||
|
||||
$c = new \tool_brickfield\local\areas\core_question\questionanswers();
|
||||
$resultsrs = $c->find_course_areas($course->id);
|
||||
$results = self::array_from_recordset($resultsrs);
|
||||
$results = $this->array_from_recordset($resultsrs);
|
||||
|
||||
// There will be the same number of results as the number of records in the question_answers table.
|
||||
$this->assertEquals(count($dbanswers), count($results));
|
||||
@ -157,7 +175,7 @@ class area_test extends \advanced_testcase {
|
||||
\context_course::instance($course->id));
|
||||
$relevantresultsrs = $c->find_relevant_areas($event);
|
||||
// Set up a relevantresults array from the recordset for easier testing.
|
||||
$relevantresults = self::array_from_recordset($relevantresultsrs);
|
||||
$relevantresults = $this->array_from_recordset($relevantresultsrs);
|
||||
|
||||
$dbanswers = array_values($DB->get_records('question_answers', ['question' => $question1->id], 'id'));
|
||||
$this->assertEquals(count($dbanswers), count($relevantresults));
|
||||
@ -190,7 +208,7 @@ class area_test extends \advanced_testcase {
|
||||
$c = new \tool_brickfield\local\areas\mod_choice\intro();
|
||||
$resultsrs = $c->find_course_areas($course->id);
|
||||
// Set up a results array from the recordset for easier testing.
|
||||
$results = self::array_from_recordset($resultsrs);
|
||||
$results = $this->array_from_recordset($resultsrs);
|
||||
|
||||
$this->assertCount(2, $results);
|
||||
$this->assertEquals($cm1->id, $results[0]->cmid);
|
||||
@ -199,14 +217,14 @@ class area_test extends \advanced_testcase {
|
||||
// Emulate the course_module_created event.
|
||||
$event = \core\event\course_module_created::create_from_cm($cm1);
|
||||
$relevantresultsrs = $c->find_relevant_areas($event);
|
||||
$relevantresults = self::array_from_recordset($relevantresultsrs);
|
||||
$relevantresults = $this->array_from_recordset($relevantresultsrs);
|
||||
$this->assertEquals([$results[0]], $relevantresults);
|
||||
|
||||
// Testing the choice options.
|
||||
$c = new \tool_brickfield\local\areas\mod_choice\option();
|
||||
$resultsrs = $c->find_course_areas($course->id);
|
||||
// Set up a results array from the recordset for easier testing.
|
||||
$results = self::array_from_recordset($resultsrs);
|
||||
$results = $this->array_from_recordset($resultsrs);
|
||||
|
||||
$this->assertCount(5, $results);
|
||||
$this->assertEquals($cm2->id, $results[3]->cmid);
|
||||
@ -220,21 +238,7 @@ class area_test extends \advanced_testcase {
|
||||
// Emulate the course_module_updated event.
|
||||
$event = \core\event\course_module_updated::create_from_cm($cm2);
|
||||
$relevantresultsrs = $c->find_relevant_areas($event);
|
||||
$relevantresults = self::array_from_recordset($relevantresultsrs);
|
||||
$relevantresults = $this->array_from_recordset($relevantresultsrs);
|
||||
$this->assertEquals([$results[3], $results[4]], $relevantresults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Array from recordset.
|
||||
* @param \moodle_recordset $rs
|
||||
* @return array
|
||||
*/
|
||||
private static function array_from_recordset($rs) {
|
||||
$records = [];
|
||||
foreach ($rs as $record) {
|
||||
$records[] = $record;
|
||||
}
|
||||
$rs->close();
|
||||
return $records;
|
||||
}
|
||||
}
|
||||
|
94
admin/tool/brickfield/tests/area_test_base.php
Normal file
94
admin/tool/brickfield/tests/area_test_base.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?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 tool_brickfield;
|
||||
|
||||
/**
|
||||
* Class area_test_base provides some utility functions that can be used by testing.
|
||||
*
|
||||
* @package tool_brickfield
|
||||
* @copyright 2020 onward: Brickfield Education Labs, https://www.brickfield.ie
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class area_test_base extends \advanced_testcase {
|
||||
/** @var string Message for failed area test. */
|
||||
protected $areatestmessage = 'Expected %component% area not found';
|
||||
|
||||
/**
|
||||
* Create and return an array from a recordset. Recordset is destroyed.
|
||||
* @param \moodle_recordset $rs
|
||||
* @return array
|
||||
*/
|
||||
public function array_from_recordset(\moodle_recordset $rs): array {
|
||||
$records = [];
|
||||
foreach ($rs as $record) {
|
||||
$records[] = $record;
|
||||
}
|
||||
// Can't rewind a recordset, so might as well close it.
|
||||
$rs->close();
|
||||
return $records;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for specified component information present in area recordset. Recordset cannot be reused.
|
||||
* @param \moodle_recordset $areasrs
|
||||
* @param string $component
|
||||
* @param int $contextid
|
||||
* @param int $itemid
|
||||
* @param int|null $courseid
|
||||
* @param int|null $categoryid
|
||||
* @return void
|
||||
*/
|
||||
public function assert_area_in_recordset(\moodle_recordset $areasrs, string $component, int $contextid, int $itemid,
|
||||
?int $courseid, ?int $categoryid): void {
|
||||
$this->assert_area_in_array(
|
||||
$this->array_from_recordset($areasrs),
|
||||
$component,
|
||||
$contextid,
|
||||
$itemid,
|
||||
$courseid,
|
||||
$categoryid
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for specified component information present in area array.
|
||||
* @param array $areas
|
||||
* @param string $component
|
||||
* @param int $contextid
|
||||
* @param int $itemid
|
||||
* @param int|null $courseid
|
||||
* @param int|null $categoryid
|
||||
* @return void
|
||||
*/
|
||||
public function assert_area_in_array(array $areas, string $component, int $contextid, int $itemid,
|
||||
?int $courseid, ?int $categoryid): void {
|
||||
$found = false;
|
||||
$message = str_replace('%component%', $component, $this->areatestmessage);
|
||||
foreach ($areas as $area) {
|
||||
if (($area->component == $component) &&
|
||||
($area->contextid == $contextid) &&
|
||||
((empty($courseid) ? empty($area->courseid) : ($area->courseid == $courseid))) &&
|
||||
((empty($categoryid) ? empty($area->categoryid) : ($area->categoryid == $categoryid))) &&
|
||||
($area->itemid == $itemid)
|
||||
) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertTrue($found, $message);
|
||||
}
|
||||
}
|
@ -13,9 +13,15 @@
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace tool_brickfield\local\areas\core_question;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/admin/tool/brickfield/tests/area_test_base.php');
|
||||
|
||||
use tool_brickfield\area_test_base;
|
||||
|
||||
/**
|
||||
* Tests for questiontext.
|
||||
*
|
||||
@ -23,8 +29,7 @@ namespace tool_brickfield\local\areas\core_question;
|
||||
* @copyright 2020 onward: Brickfield Education Labs, https://www.brickfield.ie
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class questiontext_test extends \advanced_testcase {
|
||||
|
||||
class questiontext_test extends area_test_base {
|
||||
/**
|
||||
* Set up before class.
|
||||
*/
|
||||
@ -158,30 +163,31 @@ class questiontext_test extends \advanced_testcase {
|
||||
$catcontext = \context_coursecat::instance($category->id);
|
||||
$systemcontext = \context_system::instance();
|
||||
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||
$component = 'core_question';
|
||||
|
||||
$cat = $generator->create_question_category(['contextid' => $catcontext->id]);
|
||||
$cat2 = $generator->create_question_category(['contextid' => $systemcontext->id]);
|
||||
$question = $generator->create_question('multichoice', null, ['category' => $cat2->id]);
|
||||
$question2 = $generator->create_question('multichoice', null, ['category' => $cat->id]);
|
||||
$questiontext = new questiontext();
|
||||
$rs = $questiontext->find_system_areas();
|
||||
$this->assertNotNull($rs);
|
||||
$areas = $this->array_from_recordset($questiontext->find_system_areas());
|
||||
|
||||
$count = 0;
|
||||
foreach ($rs as $rec) {
|
||||
$count++;
|
||||
if ($count <= 1) {
|
||||
$this->assertEquals($systemcontext->id, $rec->contextid);
|
||||
$this->assertEquals(1, $rec->courseid);
|
||||
$this->assertEquals(0, $rec->categoryid);
|
||||
$this->assertEquals($question->id, $rec->itemid);
|
||||
} else {
|
||||
$this->assertEquals($catcontext->id, $rec->contextid);
|
||||
$this->assertEquals(1, $rec->courseid);
|
||||
$this->assertEquals($category->id, $rec->categoryid);
|
||||
$this->assertEquals($question2->id, $rec->itemid);
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
$this->assertEquals(2, $count);
|
||||
// Assert the core_question area exists for the individual question's context, courseid and categoryid.
|
||||
$this->assert_area_in_array(
|
||||
$areas,
|
||||
$component,
|
||||
$systemcontext->id,
|
||||
$question->id,
|
||||
SITEID,
|
||||
null
|
||||
);
|
||||
$this->assert_area_in_array(
|
||||
$areas,
|
||||
$component,
|
||||
$catcontext->id,
|
||||
$question2->id,
|
||||
SITEID,
|
||||
$category->id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user