MDL-78530 core_completion: New default completion behat generator

This commit is contained in:
Amaia Anabitarte 2023-08-02 20:38:48 +02:00
parent e37e0c39e8
commit ded74e1e65
4 changed files with 262 additions and 2 deletions

View File

@ -0,0 +1,59 @@
<?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/>.
/**
* Completion test generator for Behat
*
* @package core_completion
* @copyright 2023 Amaia Anabitarte <amaia@moodle.com>
* @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);
}
}

View File

@ -0,0 +1,63 @@
<?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/>.
/**
* Completion test generator
*
* @package core_completion
* @copyright 2023 Amaia Anabitarte <amaia@moodle.com>
* @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;
}
}

View File

@ -0,0 +1,138 @@
<?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 core_completion;
/**
* PHPUnit data generator testcase
*
* @package core_completion
* @category test
* @copyright 2023 Amaia Anabitarte <amaia@moodle.com>
* @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,
],
];
}
}

View File

@ -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() {