MDL-80187 core_courseformat: format action classes

This commit is contained in:
Ferran Recio
2024-01-17 10:39:12 +01:00
parent 580c009cac
commit 141568c30d
14 changed files with 718 additions and 1 deletions
@@ -0,0 +1,29 @@
<?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 format_theunittest\courseformat;
use core_courseformat\local\cmactions as core_cm_actions;
/**
* Fixture for fake course module actions testing.
*
* @package core_course
* @copyright 2023 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cmactions extends core_cm_actions {
}
@@ -0,0 +1,29 @@
<?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 format_theunittest\courseformat;
use core_courseformat\local\courseactions as core_course_actions;
/**
* Fixture for fake course actions testing.
*
* @package core_course
* @copyright 2023 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class courseactions extends core_course_actions {
}
@@ -0,0 +1,29 @@
<?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 format_theunittest\courseformat;
use core_courseformat\local\sectionactions as core_section_actions;
/**
* Fixture for fake section actions testing.
*
* @package core_course
* @copyright 2023 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sectionactions extends core_section_actions {
}
+146
View File
@@ -0,0 +1,146 @@
<?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_courseformat;
/**
* Course format actions class tests.
*
* @package core_courseformat
* @copyright 2023 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_courseformat\base
*/
class formatactions_test extends \advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_courseactions.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_sectionactions.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_cmactions.php');
}
/**
* Test for get_instance static method.
* @dataProvider provider_classname_action
* @covers ::instance
* @param string $format
* @param array $classnames
*/
public function test_instance(string $format, array $classnames): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['format' => $format]);
$instance1 = formatactions::instance($course);
$this->assertInstanceOf('\core_courseformat\formatactions', $instance1);
$instance2 = formatactions::instance($course->id);
$this->assertInstanceOf('\core_courseformat\formatactions', $instance2);
// Validate the method is caching the result.
$this->assertEquals($instance1, $instance2);
// Validate public attribute classes.
$this->assertInstanceOf($classnames['course'], $instance1->course);
$this->assertInstanceOf($classnames['section'], $instance1->section);
$this->assertInstanceOf($classnames['cm'], $instance1->cm);
}
/**
* Test that the course action instance is created correctly.
* @dataProvider provider_classname_action
* @covers ::course
* @param string $format
* @param array $classnames
*/
public function test_course_action_instance(string $format, array $classnames): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['format' => $format]);
$instance1 = formatactions::course($course);
$this->assertInstanceOf($classnames['course'], $instance1);
$instance2 = formatactions::course($course->id);
$this->assertInstanceOf($classnames['course'], $instance2);
}
/**
* Test that the section action instance is created correctly.
* @dataProvider provider_classname_action
* @covers ::section
*
* @param string $format
* @param array $classnames
*/
public function test_static_sectionactions_instance(string $format, array $classnames): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['format' => $format]);
$instance1 = formatactions::section($course);
$this->assertInstanceOf($classnames['section'], $instance1);
$instance2 = formatactions::section($course->id);
$this->assertInstanceOf($classnames['section'], $instance2);
}
/**
* Test that the cm action instance is created correctly.
* @dataProvider provider_classname_action
* @covers ::cm
*
* @param string $format
* @param array $classnames
*/
public function test_static_cmactions_instance(string $format, array $classnames): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['format' => $format]);
$instance1 = formatactions::cm($course);
$this->assertInstanceOf($classnames['cm'], $instance1);
$instance2 = formatactions::cm($course->id);
$this->assertInstanceOf($classnames['cm'], $instance2);
}
/**
* Data provider for format class names scenarios.
* @return array
*/
public static function provider_classname_action(): array {
return [
'Topics format' => [
'format' => 'topics',
'classnames' => [
'course' => '\core_courseformat\local\courseactions',
'section' => '\core_courseformat\local\sectionactions',
'cm' => '\core_courseformat\local\cmactions',
],
],
'The unit test fixture format' => [
'format' => 'theunittest',
'classnames' => [
'course' => '\format_theunittest\courseformat\courseactions',
'section' => '\format_theunittest\courseformat\sectionactions',
'cm' => '\format_theunittest\courseformat\cmactions',
],
],
];
}
}
@@ -0,0 +1,149 @@
<?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_courseformat\local;
use ReflectionMethod;
use section_info;
use cm_info;
/**
* Base format actions class tests.
*
* @package core_courseformat
* @copyright 2023 Ferran Recio <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_courseformat\base
*/
class baseactions_test extends \advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
}
/**
* Get the reflection method for a base class instance.
* @param baseactions $baseinstance
* @param string $methodname
* @return ReflectionMethod
*/
private function get_base_reflection_method(baseactions $baseinstance, string $methodname): ReflectionMethod {
$reflectionclass = new \reflectionclass($baseinstance);
$method = $reflectionclass->getMethod($methodname);
$method->setAccessible(true);
return $method;
}
/**
* Test for get_instance static method.
* @covers ::get_format
*/
public function test_get_format(): void {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['format' => 'topics']);
$baseactions = new baseactions($course);
$method = $this->get_base_reflection_method($baseactions, 'get_format');
$format = $method->invoke($baseactions);
$this->assertEquals('topics', $format->get_format());
$this->assertEquals('format_topics', $format::class);
// Format should be always the most updated one.
$course->format = 'weeks';
$DB->update_record('course', $course);
$format = $method->invoke($baseactions);
$this->assertEquals('weeks', $format->get_format());
$this->assertEquals('format_weeks', $format::class);
}
/**
* Test for get_instance static method.
* @covers ::get_section_info
*/
public function test_get_section_info(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(
['format' => 'topics', 'numsections' => 4],
['createsections' => true]
);
$modinfo = get_fast_modinfo($course->id);
$originalsection = $modinfo->get_section_info(1);
$baseactions = new baseactions($course);
$method = $this->get_base_reflection_method($baseactions, 'get_section_info');
$sectioninfo = $method->invoke($baseactions, $originalsection->id);
$this->assertInstanceOf(section_info::class, $sectioninfo);
$this->assertEquals($originalsection->id, $sectioninfo->id);
$this->assertEquals($originalsection->section, $sectioninfo->section);
// Section info should be always the most updated one.
course_update_section($course, $originalsection, (object)['name' => 'New name']);
move_section_to($course, 1, 3);
$sectioninfo = $method->invoke($baseactions, $originalsection->id);
$this->assertInstanceOf(section_info::class, $sectioninfo);
$this->assertEquals($originalsection->id, $sectioninfo->id);
$this->assertEquals(3, $sectioninfo->section);
$this->assertEquals('New name', $sectioninfo->name);
}
/**
* Test for get_instance static method.
* @covers ::get_cm_info
*/
public function test_get_cm_info(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course(
['format' => 'topics', 'numsections' => 4],
['createsections' => true]
);
$activity = $this->getDataGenerator()->create_module('label', ['course' => $course->id]);
$modinfo = get_fast_modinfo($course->id);
$destinationsection = $modinfo->get_section_info(3);
$originalcm = $modinfo->get_cm($activity->cmid);
$baseactions = new baseactions($course);
$method = $this->get_base_reflection_method($baseactions, 'get_cm_info');
$cm = $method->invoke($baseactions, $originalcm->id);
$this->assertInstanceOf(cm_info::class, $cm);
$this->assertEquals($originalcm->id, $cm->id);
$this->assertEquals($originalcm->sectionnum, $cm->sectionnum);
$this->assertEquals($originalcm->name, $cm->name);
// CM info should be always the most updated one.
moveto_module($originalcm, $destinationsection);
$cm = $method->invoke($baseactions, $originalcm->id);
$this->assertInstanceOf(cm_info::class, $cm);
$this->assertEquals($originalcm->id, $cm->id);
$this->assertEquals($destinationsection->section, $cm->sectionnum);
}
}