Skip to content

Commit

Permalink
refactor to use moodle_core abstraction layer for context_course
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Dec 19, 2024
1 parent 716040c commit ea17b47
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
5 changes: 2 additions & 3 deletions classes/external/score_get_course_scores.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
use core_external\external_value;
use invalid_parameter_exception;
use local_adler\adler_score_helpers;
use local_adler\moodle_core;
use moodle_exception;
use restricted_context_exception;

class score_get_course_scores extends external_api {
private static string $context_course = context_course::class;

public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
array(
Expand All @@ -37,7 +36,7 @@ public static function execute($course_id): array {
$course_id = $params['course_id'];

// Permission check
$context = static::$context_course::instance($course_id);
$context = di::get(moodle_core::class)->context_course_instance($course_id);
static::validate_context($context);

// get cmids of all modules in course
Expand Down
9 changes: 3 additions & 6 deletions tests/external/score_get_course_scores_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use core\di;
use local_adler\adler_score_helpers;
use local_adler\lib\adler_externallib_testcase;
use local_adler\moodle_core;
use Mockery;
use moodle_database;
use ReflectionClass;
Expand Down Expand Up @@ -38,12 +39,8 @@ public function test_execute($element_count) {

// mock context
$context_mock = Mockery::mock(context_module::class);
$context_mock->shouldReceive('instance')->andReturn($context_mock);

$reflected_class = new ReflectionClass(score_get_course_scores::class);
$property = $reflected_class->getProperty('context_course');
$property->setAccessible(true);
$property->setValue(null, $context_mock->mockery_getName());
$moodle_core_mock = Mockery::mock(moodle_core::class);
$moodle_core_mock->shouldReceive('context_course_instance')->andReturn($context_mock);

// mock validate_context
$score_get_course_scores_mock = Mockery::mock(score_get_course_scores::class)->makePartial();
Expand Down

0 comments on commit ea17b47

Please sign in to comment.