diff --git a/completion/tests/generator/behat_core_completion_generator.php b/completion/tests/generator/behat_core_completion_generator.php new file mode 100644 index 00000000000..903817bafa6 --- /dev/null +++ b/completion/tests/generator/behat_core_completion_generator.php @@ -0,0 +1,59 @@ +. + +/** + * Completion test generator for Behat + * + * @package core_completion + * @copyright 2023 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_core_completion_generator extends behat_generator_base { + + /** + * Get a list of the entities that can be created for completion + * + * @return array[] + */ + protected function get_creatable_entities(): array { + return [ + 'Course defaults' => [ + 'singular' => 'Course default', + 'datagenerator' => 'default_completion', + 'required' => [ + 'course', + 'module', + ], + 'switchids' => [ + 'course' => 'course', + 'module' => 'module', + ], + ], + ]; + } + + /** + * Look up module ID from given name + * + * @param string $name + * @return int + */ + protected function get_module_id(string $name): int { + global $DB; + + return (int) $DB->get_field('modules', 'id', ['name' => $name], MUST_EXIST); + } +} diff --git a/completion/tests/generator/lib.php b/completion/tests/generator/lib.php new file mode 100644 index 00000000000..2b661704403 --- /dev/null +++ b/completion/tests/generator/lib.php @@ -0,0 +1,63 @@ +. + +/** + * Completion test generator + * + * @package core_completion + * @copyright 2023 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_completion_generator extends component_generator_base { + + /** + * Create default completion + * + * @param array|stdClass $record + * @return stdClass + */ + public function create_default_completion($record): stdClass { + global $DB; + + $record = (array) $record; + + if (!array_key_exists('course', $record) || !is_numeric($record['course'])) { + throw new moodle_exception('courserequired'); + } + if (!$DB->get_record('course', ['id' => $record['course']])) { + throw new moodle_exception('invalidcourseid'); + } + + if (!array_key_exists('module', $record) || !is_numeric($record['module'])) { + throw new moodle_exception('modulerequired'); + } + if (!$DB->get_record('modules', ['id' => $record['module']])) { + throw new moodle_exception('invalidmoduleid'); + } + + $record = (object) array_merge([ + 'completion' => 0, + 'completionview' => 0, + 'completionusegrade' => 0, + 'completionpassgrade' => 0, + 'completionexpected' => 0, + 'customrules' => '', + ], $record); + $record->id = $DB->insert_record('course_completion_defaults', $record); + + return $record; + } +} diff --git a/completion/tests/generator_test.php b/completion/tests/generator_test.php new file mode 100644 index 00000000000..7c5f6826bcc --- /dev/null +++ b/completion/tests/generator_test.php @@ -0,0 +1,138 @@ +. + +namespace core_completion; + +/** + * PHPUnit data generator testcase + * + * @package core_completion + * @category test + * @copyright 2023 Amaia Anabitarte + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \core_completion_generator + */ +class generator_test extends \advanced_testcase { + + /** + * Test create_default_completion. + * + * @dataProvider create_default_completion_provider + * + * @param int|null|string $course The course to add the default activities conditions to. + * @param int|null|string $module The module to add the default activities conditions to. + * @param bool $exception Whether an exception is expected or not. + * @param int $count The number of default activity completions to be created. + * @param int $completion The value for completion setting. + * + * @covers ::create_default_completion + */ + public function test_create_default_completion($course, $module, bool $exception, int $count, int $completion = 0) { + global $DB; + + $this->resetAfterTest(true); + $generator = $this->getDataGenerator()->get_plugin_generator('core_completion'); + + $record = [ + 'course' => $course, + 'module' => $module, + 'completion' => $completion, + ]; + $result = (object) array_merge([ + 'completion' => 0, + 'completionview' => 0, + 'completionusegrade' => 0, + 'completionpassgrade' => 0, + 'completionexpected' => 0, + 'customrules' => '', + ], $record); + + if ($exception) { + $this->expectException('moodle_exception'); + } + $defaultcompletion = $generator->create_default_completion($record); + + if (!$exception) { + foreach ($result as $key => $value) { + $this->assertEquals($defaultcompletion->{$key}, $value); + } + } + $this->assertEquals( + $count, + $DB->count_records('course_completion_defaults', ['course' => $course, 'module' => $module]) + ); + } + + /** + * Data provider for test_create_default_completion(). + * @return array[] + */ + public function create_default_completion_provider(): array { + global $SITE; + + return [ + 'Null course' => [ + 'course' => null, + 'module' => null, + 'exception' => true, + 'count' => 0, + ], + 'Empty course' => [ + 'course' => '', + 'module' => null, + 'exception' => true, + 'count' => 0, + ], + 'Invalid course' => [ + 'course' => 0, + 'module' => null, + 'exception' => true, + 'count' => 0, + ], + 'Null module' => [ + 'course' => $SITE->id, + 'module' => null, + 'exception' => true, + 'count' => 0, + ], + 'Empty module' => [ + 'course' => $SITE->id, + 'module' => null, + 'exception' => true, + 'count' => 0, + ], + 'Invalid module' => [ + 'course' => $SITE->id, + 'module' => 0, + 'exception' => true, + 'count' => 0, + ], + 'Default activity completion: NONE' => [ + 'course' => $SITE->id, + 'module' => 1, + 'exception' => false, + 'count' => 1, + ], + 'Default activity completion: AUTOMATIC' => [ + 'course' => $SITE->id, + 'module' => 1, + 'exception' => false, + 'count' => 1, + 'completion' => 2, + ], + ]; + } +} diff --git a/lib/testing/tests/testing_generator_test.php b/lib/testing/tests/testing_generator_test.php index 0291121d49b..46ae0f9e926 100644 --- a/lib/testing/tests/testing_generator_test.php +++ b/lib/testing/tests/testing_generator_test.php @@ -47,8 +47,8 @@ class testing_generator_test extends \advanced_testcase { */ public function test_get_plugin_generator_no_component_dir() { $this->expectException(\coding_exception::class); - $this->expectExceptionMessage('Component core_completion does not support generators yet. Missing tests/generator/lib.php.'); - $generator = $this->getDataGenerator()->get_plugin_generator('core_completion'); + $this->expectExceptionMessage('Component core_cohort does not support generators yet. Missing tests/generator/lib.php.'); + $generator = $this->getDataGenerator()->get_plugin_generator('core_cohort'); } public function test_create_user() {