Skip to content

Commit

Permalink
Tests update
Browse files Browse the repository at this point in the history
- Fix some small issues (like naming)
- add noinspection for linting rules that are not valid for moodle
- add requirement ids (internal stuff)
  • Loading branch information
Glutamat42 committed Jul 11, 2024
1 parent 6b8a5f0 commit c63063d
Show file tree
Hide file tree
Showing 19 changed files with 279 additions and 41 deletions.
15 changes: 13 additions & 2 deletions tests/adler_score_helpers_test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */

namespace local_adler;

Expand All @@ -16,6 +16,7 @@

class adler_score_helpers_adler_score_mock extends adler_score {
use static_mock_utilities_trait;

public function __construct(object $course_module, int $user_id = null) {
return static::mock_this_function(__FUNCTION__, func_get_args());
}
Expand All @@ -32,6 +33,9 @@ public function tearDown(): void {
$param_adler_score_class->setValue(adler_score::class);
}

/**
* ANF-ID: [MVP9, MVP8, MVP7]
*/
public function test_get_adler_score_objects() {
// setup
// set param $adler_score_class
Expand Down Expand Up @@ -67,7 +71,6 @@ public function test_get_adler_score_objects() {
$this->expectException(moodle_exception::class);

adler_score_helpers::get_adler_score_objects([$cmids[0]]);

}

public function provide_test_get_achieved_scores_data(): array {
Expand Down Expand Up @@ -98,6 +101,8 @@ public function provide_test_get_achieved_scores_data(): array {

/**
* @dataProvider provide_test_get_achieved_scores_data
*
* ANF-ID: [MVP9, MVP8, MVP7]
*/
public function test_get_achieved_scores($data) {
// create 3 adler_score objects and mock get_score
Expand Down Expand Up @@ -133,6 +138,9 @@ public function test_get_achieved_scores($data) {
$this->assertEquals($data['expected_result'], $result);
}

/**
* ANF-ID: [MVP9, MVP8, MVP7]
*/
public function test_get_achieved_scores_with_module_ids() {
// setup
$module_ids = [1, 2, 3];
Expand Down Expand Up @@ -161,6 +169,9 @@ public function test_get_achieved_scores_with_module_ids() {
$this->assertEquals([$module_ids, $user_id], adler_score_helpers_mock::get_calls('get_adler_score_objects')[0]);
}

/**
* ANF-ID: [MVP12, MVP10, MVP9, MVP8, MVP7]
*/
public function test_get_adler_score_record() {
// create course
$course = $this->getDataGenerator()->create_course();
Expand Down
11 changes: 10 additions & 1 deletion tests/adler_score_test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */

namespace local_adler;

Expand Down Expand Up @@ -146,6 +146,8 @@ public function provide_test_construct_data() {

/**
* @dataProvider provide_test_construct_data
*
* ANF-ID: [MVP12, MVP10, MVP9, MVP8, MVP7]
*/
public function test_construct($test) {
// reset
Expand Down Expand Up @@ -232,6 +234,8 @@ public function provide_test_get_primitive_score_data() {

/**
* @dataProvider provide_test_get_primitive_score_data
*
* ANF-ID: [MVP10]
*/
public function test_get_primitive_score($data) {
// create primitive activity
Expand Down Expand Up @@ -315,6 +319,8 @@ private function fix_scaled_attribute_of_h5pactivity_attempts() {

/**
* @medium
*
* ANF-ID: [MVP7]
*/
public function test_get_score_for_h5p_learning_element() {
global $CFG;
Expand Down Expand Up @@ -431,6 +437,9 @@ public function test_get_score_for_h5p_learning_element() {
}
}

/**
* ANF-ID: [MVP10, MVP9, MVP8, MVP7]
*/
public function test_calculate_percentage_achieved() {
// test setup
// create adler_score object without constructor call
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
// When using namespaces, the namespace of the test class should match the namespace of the code under test
// -> no namespace for this test as backup/restore is not namespaced
use local_adler\lib\adler_testcase;
Expand All @@ -14,7 +14,7 @@
* The only way to reliably recreate this object would be using the same code as the original class.
* Therefore, this test is implemented as an integration test.
*/
class backup__adler_plugin_test extends adler_testcase {
class backup_adler_plugin_test extends adler_testcase {
private stdClass $course;
private stdClass $module;

Expand All @@ -36,7 +36,7 @@ public function setUp(): void {
* @param $type string type of backup, one of 'module', 'course'
* @return false|SimpleXMLElement
*/
private function get_xml_from_backup(backup_controller $bc, string $type='module') {
private function get_xml_from_backup(backup_controller $bc, string $type = 'module') {
// Get the backup file.
$file = $bc->get_results();
$file = reset($file);
Expand Down Expand Up @@ -75,6 +75,8 @@ private function verify_points($expected, $actual) {
/**
* Test the backup score logic.
* @medium
*
* ANF-ID: [MVP1]
*/
public function test_backup_score() {
// Create score item with generator
Expand Down Expand Up @@ -102,6 +104,8 @@ public function test_backup_score() {
/**
* Test the backup of module without score data.
* @medium
*
* ANF-ID: [MVP1]
*/
public function test_backup_no_score() {
// Create a backup of the module.
Expand Down Expand Up @@ -136,6 +140,9 @@ private function verify_course($expected, $actual) {
$this->assertEquals((int)$expected->timemodified, (int)$actual->timemodified);
}

/**
* ANF-ID: [MVP1]
*/
public function test_backup_course() {
// Create score item with generator
$adler_course_object = $this
Expand All @@ -162,6 +169,9 @@ public function test_backup_course() {
$this->verify_course($adler_course_object, $xml->plugin_local_adler_course->adler_course);
}

/**
* ANF-ID: [MVP1]
*/
public function test_backup_course_not_adler_course() {
// Create a backup of the course.
$bc = new backup_controller(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */
// When using namespaces, the namespace of the test class should match the namespace of the code under test
// -> no namespace for this test as backup/restore is not namespaced

Expand All @@ -10,10 +10,7 @@
require_once($CFG->dirroot . '/local/adler/backup/moodle2/restore_local_adler_plugin.class.php');


/**
* PHPunit test for class restore_local_adler_plugin
*/
class restore__adler_plugin_test extends adler_testcase {
class restore_adler_plugin_test extends adler_testcase {
public function setUpModule(): array {
// stub the get_task() method to return a mock task object
$stub_task = $this->getMockBuilder(restore_activity_task::class)
Expand Down Expand Up @@ -47,6 +44,8 @@ public function setUpModule(): array {
* @param $name string Name of method to set as public
* @return ReflectionMethod
* @throws ReflectionException
*
* ANF-ID: [MVP2]
*/
protected static function getMethodAsPublic(string $name): ReflectionMethod {
$class = new ReflectionClass(restore_local_adler_plugin::class);
Expand All @@ -55,6 +54,9 @@ protected static function getMethodAsPublic(string $name): ReflectionMethod {
return $method;
}

/**
* ANF-ID: [MVP2]
*/
public function test_process_adler_module_one_element() {
// setup
global $DB;
Expand All @@ -78,6 +80,9 @@ public function test_process_adler_module_one_element() {
$this->assertEquals($data[0]->timemodified, $db_record->timemodified);
}

/**
* ANF-ID: [MVP2]
*/
public function test_process_adler_module_one_element_default_values() {
// test without optional fields (timecreated, timemodified)
global $DB;
Expand All @@ -101,6 +106,9 @@ public function test_process_adler_module_one_element_default_values() {
$this->assertTrue($db_record->timemodified > 0 && $db_record->timemodified <= time());
}

/**
* ANF-ID: [MVP2]
*/
public function test_process_adler_module_multiple_elements() {
list($data, $stub) = $this->setUpModule();

Expand All @@ -111,6 +119,9 @@ public function test_process_adler_module_multiple_elements() {
$plugin->process_adler_module($data[1]);
}

/**
* ANF-ID: [MVP2]
*/
public function test_process_adler_module_invalid_datatype() {
// setup
global $DB;
Expand All @@ -137,6 +148,9 @@ public function test_process_adler_module_invalid_datatype() {
$this->assertEquals(0, $DB->count_records('local_adler_course_modules'));
}

/**
* ANF-ID: [MVP2]
*/
public function test_process_adler_module_missing_fields() {
// setup
global $DB;
Expand All @@ -159,7 +173,9 @@ public function test_process_adler_module_missing_fields() {
$this->assertEquals(0, $DB->count_records('local_adler_course_modules'));
}

/** Test define_module_plugin_structure() */
/**
* ANF-ID: [MVP2]
*/
public function test_define_module_plugin_structure() {
list($data, $stub) = $this->setUpModule();

Expand All @@ -186,7 +202,9 @@ public function test_define_module_plugin_structure() {
$this->assertStringContainsString('adler_module', $paths[0]->get_path());
}

/** Test course restore */
/**
* ANF-ID: [MVP2]
*/
public function setUpCourse(): array {
$data = $this->getDataGenerator()->get_plugin_generator('local_adler')->create_adler_course_object(1, [], false);

Expand All @@ -203,6 +221,9 @@ public function setUpCourse(): array {
return [$data, $stub];
}

/**
* ANF-ID: [MVP2]
*/
public function test_define_course_plugin_structure() {
list($data, $stub) = $this->setUpCourse();

Expand All @@ -229,6 +250,9 @@ public function test_define_course_plugin_structure() {
$this->assertStringContainsString('adler_course', $paths[0]->get_path());
}

/**
* ANF-ID: [MVP2]
*/
public function test_process_adler_course() {
// setup
global $DB;
Expand All @@ -248,6 +272,9 @@ public function test_process_adler_course() {
$this->assertEquals(7, $db_record->course_id);
}

/**
* ANF-ID: [MVP2]
*/
public function test_process_adler_course_optional_fields() {
// setup
global $DB;
Expand All @@ -272,6 +299,9 @@ public function test_process_adler_course_optional_fields() {
$this->assertTrue($db_record->timemodified > 0 && $db_record->timemodified <= time());
}

/**
* ANF-ID: [MVP2]
*/
public function test_define_section_plugin_structure() {
$restore_mock = $this
->getMockBuilder(restore_local_adler_plugin::class)
Expand Down Expand Up @@ -312,6 +342,8 @@ public function provide_test_process_adler_section_data() {

/**
* @dataProvider provide_test_process_adler_section_data
*
* ANF-ID: [MVP2]
*/
public function test_process_adler_section($restore_data) {
$restore_mock = $this
Expand Down
17 changes: 16 additions & 1 deletion tests/cli/create_course_cat_and_assign_user_role_test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php /** @noinspection PhpIllegalPsrClassPathInspection */

global $CFG;

Expand All @@ -12,6 +12,8 @@ class create_course_cat_and_assign_user_role_test extends adler_testcase {
/**
* @dataProvider provide_test_create_course_cat_and_assign_user_role_data
* @runInSeparateProcess
*
* ANF-ID: [MVP20, MVP21]
*/
public function test_create_course_cat_and_assign_user_role($username, $role, $category_path, $expected_parameters, $expected_exit_code) {
global $CFG;
Expand Down Expand Up @@ -82,6 +84,9 @@ public function provide_test_create_course_cat_and_assign_user_role_data() {
];
}

/**
* ANF-ID: [MVP20, MVP21]
*/
public function test_script_with_unknown_parameter() {
global $CFG;

Expand All @@ -98,6 +103,9 @@ public function test_script_with_unknown_parameter() {
require $CFG->dirroot . '/local/adler/cli/create_course_cat_and_assign_user_role.php';
}

/**
* ANF-ID: [MVP20, MVP21]
*/
public function test_script_with_help_parameter() {
global $CFG;

Expand All @@ -118,6 +126,8 @@ public function test_script_with_help_parameter() {

/**
* integration test
*
* ANF-ID: [MVP20, MVP21]
*/
public function test_script_with_valid_parameters() {
$category_path = 'test / valid_category_path';
Expand Down Expand Up @@ -152,6 +162,8 @@ public function test_script_with_valid_parameters() {

/**
* integration test
*
* ANF-ID: [MVP20, MVP21]
*/
public function test_script_without_category_path() {
global $CFG, $DB;
Expand All @@ -177,6 +189,9 @@ public function test_script_without_category_path() {
$this->assertEquals($category_count_before + 2, count(core_course_category::make_categories_list()));
}

/**
* ANF-ID: [MVP20, MVP21]
*/
public function test_with_role_that_cannot_be_assigned_to_course_category() {
global $CFG, $DB;

Expand Down
Loading

0 comments on commit c63063d

Please sign in to comment.