Skip to content

Commit

Permalink
MDL-72246 core_completion: Test coverage improvements for completion
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Feb 8, 2022
1 parent 1330fad commit 1142892
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 2 deletions.
47 changes: 47 additions & 0 deletions completion/tests/coverage.php
@@ -0,0 +1,47 @@
<?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/>.

defined('MOODLE_INTERNAL') || die();

/**
* Coverage information for the core_completion.
*
* @package core
* @category phpunit
* @copyright 2022 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Coverage information for the core subsystem.
*
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
return new class extends phpunit_coverage_info {
/** @var array The list of folders relative to the plugin root to include in coverage generation. */
protected $includelistfolders = [
'criteria',
];

/** @var array The list of files relative to the plugin root to include in coverage generation. */
protected $includelistfiles = [
'completion_aggregation.php',
'completion_completion.php',
'completion_criteria_completion.php',
'data_object.php'
];
};
77 changes: 75 additions & 2 deletions lib/tests/completionlib_test.php
Expand Up @@ -29,7 +29,17 @@
global $CFG;
require_once($CFG->libdir.'/completionlib.php');

class core_completionlib_testcase extends advanced_testcase {
/**
* Completion tests.
*
* @package core_completion
* @category phpunit
* @copyright 2008 Sam Marshall
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \completion_info
*/
class completionlib_test extends advanced_testcase {
protected $course;
protected $user;
protected $module1;
Expand Down Expand Up @@ -90,6 +100,10 @@ public static function assertEquals($expected, $actual, string $message = '', fl
parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
}

/**
* @covers ::is_enabled_for_site
* @covers ::is_enabled
*/
public function test_is_enabled() {
global $CFG;
$this->mock_setup();
Expand Down Expand Up @@ -125,6 +139,9 @@ public function test_is_enabled() {
$this->assertEquals(COMPLETION_TRACKING_AUTOMATIC, $c->is_enabled($cm));
}

/**
* @covers ::update_state
*/
public function test_update_state() {
$this->mock_setup();

Expand Down Expand Up @@ -348,6 +365,7 @@ public function internal_get_state_provider() {
* @param int $completionusegrade
* @param string $unsetfield
* @param int $expectedstate
* @covers ::internal_get_state
*/
public function test_internal_get_state(int $completionview, int $completionusegrade, string $unsetfield, int $expectedstate) {
$this->setup_data();
Expand Down Expand Up @@ -377,6 +395,8 @@ public function test_internal_get_state(int $completionview, int $completionuseg

/**
* Covers the case where internal_get_state() is being called for a user different from the logged in user.
*
* @covers ::internal_get_state
*/
public function test_internal_get_state_with_different_user() {
$this->setup_data();
Expand Down Expand Up @@ -419,6 +439,8 @@ public function test_internal_get_state_with_different_user() {

/**
* Test for internal_get_state() for an activity that supports custom completion.
*
* @covers ::internal_get_state
*/
public function test_internal_get_state_with_custom_completion() {
$this->setup_data();
Expand All @@ -445,6 +467,9 @@ public function test_internal_get_state_with_custom_completion() {
$this->assertEquals(COMPLETION_COMPLETE, $completion);
}

/**
* @covers ::set_module_viewed
*/
public function test_set_module_viewed() {
$this->mock_setup();

Expand Down Expand Up @@ -500,6 +525,9 @@ public function test_set_module_viewed() {
$c->set_module_viewed($cm, 1337);
}

/**
* @covers ::count_user_data
*/
public function test_count_user_data() {
global $DB;
$this->mock_setup();
Expand All @@ -516,6 +544,9 @@ public function test_count_user_data() {
$this->assertEquals(666, $c->count_user_data($cm));
}

/**
* @covers ::delete_all_state
*/
public function test_delete_all_state() {
global $DB;
$this->mock_setup();
Expand All @@ -533,6 +564,9 @@ public function test_delete_all_state() {
$c->delete_all_state($cm);
}

/**
* @covers ::reset_all_state
*/
public function test_reset_all_state() {
global $DB;
$this->mock_setup();
Expand Down Expand Up @@ -608,6 +642,7 @@ public function get_data_provider() {
* @param bool $sameuser Whether the user calling get_data() is the user itself.
* @param bool $hasrecord Whether to create a course_modules_completion record.
* @param int $completion The completion state expected.
* @covers ::get_data
*/
public function test_get_data(bool $wholecourse, bool $sameuser, bool $hasrecord, int $completion) {
global $DB;
Expand Down Expand Up @@ -674,6 +709,8 @@ public function test_get_data(bool $wholecourse, bool $sameuser, bool $hasrecord

/**
* Tests for completion_info::get_other_cm_completion_data().
*
* @covers ::get_other_cm_completion_data
*/
public function test_get_other_cm_completion_data() {
global $DB;
Expand Down Expand Up @@ -744,6 +781,9 @@ public function test_get_other_cm_completion_data() {
$this->assertEmpty($choice2completiondata);
}

/**
* @covers ::internal_set_data
*/
public function test_internal_set_data() {
global $DB;
$this->setup_data();
Expand Down Expand Up @@ -808,6 +848,9 @@ public function test_internal_set_data() {
$c->internal_set_data($cm, $data);
}

/**
* @covers ::get_progress_all
*/
public function test_get_progress_all_few() {
global $DB;
$this->mock_setup();
Expand Down Expand Up @@ -842,6 +885,9 @@ public function test_get_progress_all_few() {
), $c->get_progress_all(false));
}

/**
* @covers ::get_progress_all
*/
public function test_get_progress_all_lots() {
global $DB;
$this->mock_setup();
Expand Down Expand Up @@ -897,6 +943,9 @@ public function test_get_progress_all_lots() {
$this->assertCount(count($tracked), $result);
}

/**
* @covers ::inform_grade_changed
*/
public function test_inform_grade_changed() {
$this->mock_setup();

Expand Down Expand Up @@ -965,6 +1014,9 @@ public function test_inform_grade_changed() {
$c->inform_grade_changed($cm, $item, $grade, true);
}

/**
* @covers ::internal_get_grade_state
*/
public function test_internal_get_grade_state() {
$this->mock_setup();

Expand Down Expand Up @@ -1007,6 +1059,9 @@ public function test_internal_get_grade_state() {
completion_info::internal_get_grade_state($item, $grade));
}

/**
* @test ::get_activities
*/
public function test_get_activities() {
global $CFG;
$this->resetAfterTest();
Expand Down Expand Up @@ -1048,6 +1103,9 @@ public function test_get_activities() {
$this->assertFalse(isset($activities[$data2->cmid]));
}

/**
* @test ::has_activities
*/
public function test_has_activities() {
global $CFG;
$this->resetAfterTest();
Expand All @@ -1073,7 +1131,8 @@ public function test_has_activities() {
/**
* Test that data is cleaned up when we delete courses that are set as completion criteria for other courses
*
* @return void
* @covers ::delete_course_completion_data
* @covers ::delete_all_completion_data
*/
public function test_course_delete_prerequisite() {
global $DB;
Expand Down Expand Up @@ -1110,6 +1169,8 @@ public function test_course_delete_prerequisite() {

/**
* Test course module completion update event.
*
* @covers \core\event\course_module_completion_updated
*/
public function test_course_module_completion_updated_event() {
global $USER, $CFG;
Expand Down Expand Up @@ -1147,6 +1208,8 @@ public function test_course_module_completion_updated_event() {

/**
* Test course completed event.
*
* @covers \core\event\course_completed
*/
public function test_course_completed_event() {
global $USER;
Expand Down Expand Up @@ -1176,6 +1239,8 @@ public function test_course_completed_event() {

/**
* Test course completed message.
*
* @covers \core\event\course_completed
*/
public function test_course_completed_message() {
$this->setup_data();
Expand All @@ -1202,6 +1267,8 @@ public function test_course_completed_message() {

/**
* Test course completed event.
*
* @covers \core\event\course_completion_updated
*/
public function test_course_completion_updated_event() {
$this->setup_data();
Expand All @@ -1228,6 +1295,9 @@ public function test_course_completion_updated_event() {
$this->assertEventLegacyLogData($expectedlegacylog, $event);
}

/**
* @covers \completion_can_view_data
*/
public function test_completion_can_view_data() {
$this->setup_data();

Expand Down Expand Up @@ -1263,6 +1333,7 @@ public function get_grade_completion_provider() {
* @param int|null $passinggrade Passing grade to set for the test activity.
* @param string|null $expectedexception Expected exception.
* @param int|null $expectedresult The expected completion status.
* @covers ::get_grade_completion
*/
public function test_get_grade_completion(bool $completionusegrade, bool $hasgrade, ?int $passinggrade,
?string $expectedexception, ?int $expectedresult) {
Expand Down Expand Up @@ -1295,6 +1366,8 @@ public function test_get_grade_completion(bool $completionusegrade, bool $hasgra

/**
* Test the return value for cases when the activity module does not have associated grade_item.
*
* @covers ::get_grade_completion
*/
public function test_get_grade_completion_without_grade_item() {
global $DB;
Expand Down

0 comments on commit 1142892

Please sign in to comment.