From 06742626df2d9bd9c085642c11562d67e701361a Mon Sep 17 00:00:00 2001 From: Sebastian Gundersen Date: Mon, 2 Feb 2026 18:27:11 +0100 Subject: [PATCH] MDL-87825 customfield_number: Handle shared fields in cron --- .../field/number/classes/task/cron.php | 22 +++- .../number/tests/fixtures/test_provider.php | 105 +++++++++++++++ .../field/number/tests/task/cron_test.php | 121 ++++++++++++++++++ 3 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 public/customfield/field/number/tests/fixtures/test_provider.php create mode 100644 public/customfield/field/number/tests/task/cron_test.php diff --git a/public/customfield/field/number/classes/task/cron.php b/public/customfield/field/number/classes/task/cron.php index 28b22e44a3c..aa51cfc9611 100644 --- a/public/customfield/field/number/classes/task/cron.php +++ b/public/customfield/field/number/classes/task/cron.php @@ -18,7 +18,9 @@ namespace customfield_number\task; use core\task\scheduled_task; use core_customfield\category_controller; +use core_customfield\customfield\shared_handler; use core_customfield\field_controller; +use core_customfield\handler; use customfield_number\provider_base; /** @@ -65,10 +67,24 @@ class cron extends scheduled_task { $category = category_controller::create(0, $cat); // Create an instance of field controller for each field and recalculate the value if field provider is available. $field = field_controller::create(0, $row, $category); - if ($provider = provider_base::instance($field)) { - if ($provider->is_available()) { - $provider->recalculate(null, $cat->component, $cat->area, $cat->itemid); + $provider = provider_base::instance($field); + if (!$provider || !$provider->is_available()) { + continue; + } + // For shared custom fields, all other component/areas must be recalculated. + if ($cat->component === 'core_customfield' && $cat->area === 'shared') { + $handlers = \core_component::get_component_classes_in_namespace(null, 'customfield'); + // Remove shared handler since the shared area has no instances. + unset($handlers[shared_handler::class]); + foreach ($handlers as $handlerclass => $path) { + if (!is_subclass_of($handlerclass, handler::class)) { + continue; + } + $handler = $handlerclass::create(); + $provider->recalculate(null, $handler->get_component(), $handler->get_area(), $handler->get_itemid()); } + } else { + $provider->recalculate(null, $cat->component, $cat->area, (int)$cat->itemid); } } } diff --git a/public/customfield/field/number/tests/fixtures/test_provider.php b/public/customfield/field/number/tests/fixtures/test_provider.php new file mode 100644 index 00000000000..cdca42b78a4 --- /dev/null +++ b/public/customfield/field/number/tests/fixtures/test_provider.php @@ -0,0 +1,105 @@ +. + +declare(strict_types=1); + +namespace customfield_number; + +use core_customfield\data_controller; + +/** + * Test provider that sets the field data to the current second of the hour. + * + * @package customfield_number + * @copyright 2026 Sebastian Gundersen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class test_provider extends provider_base { + /** + * Get provider name. + * + * @return string + */ + public function get_name(): string { + return 'Test provider'; + } + + /** + * Check if the provider is available for the field. + * + * @return bool + */ + public function is_available(): bool { + return true; + } + + /** + * Recalculate field value. + * + * @param int|null $instanceid + * @param string $component + * @param string $area + * @param int $itemid + */ + public function recalculate( + ?int $instanceid = null, + string $component = 'core_course', + string $area = 'course', + int $itemid = 0, + ): void { + global $DB; + + $params = [ + 'fieldid' => $this->field->get('id'), + 'component' => $component, + 'area' => $area, + 'itemid' => $itemid, + ]; + + $sql = "SELECT * + FROM {customfield_data} + WHERE fieldid = :fieldid + AND component = :component + AND area = :area + AND itemid = :itemid"; + + if ($instanceid !== null) { + $sql .= " AND instanceid <> :instanceid"; + $params['instanceid'] = $instanceid; + } + + $records = $DB->get_recordset_sql($sql, $params); + if (!$records->valid() && $component === 'core_course' && $area === 'course') { + foreach ($DB->get_records('course') as $course) { + $data = data_controller::create(0, (object)['instanceid' => (int)$course->id], $this->field); + $data->set('contextid', \core\context\system::instance()->id); + $data->set('component', 'core_course'); + $data->set('area', 'course'); + $data->set('decvalue', \core\di::get(\core\clock::class)->time() % 3600); + $data->save(); + } + } + foreach ($records as $record) { + $data = data_controller::create(0, $record, $this->field); + $data->set('contextid', \core\context\system::instance()->id); + $data->set('component', $component); + $data->set('area', $area); + $data->set('decvalue', \core\di::get(\core\clock::class)->time() % 3600); + $data->save(); + } + $records->close(); + } +} diff --git a/public/customfield/field/number/tests/task/cron_test.php b/public/customfield/field/number/tests/task/cron_test.php new file mode 100644 index 00000000000..803e0183a86 --- /dev/null +++ b/public/customfield/field/number/tests/task/cron_test.php @@ -0,0 +1,121 @@ +. + +declare(strict_types=1); + +namespace customfield_number\task; + +use core_customfield\api; +use core_customfield\external\toggle_shared_category; +use core_customfield\field_controller; +use customfield_number\test_provider; + +/** + * Test the cron task. + * + * @package customfield_number + * @covers \customfield_number\task\cron + * @copyright 2026 Sebastian Gundersen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class cron_test extends \advanced_testcase { + /** @var field_controller Field */ + private field_controller $field; + + /** + * Set up. + * + * @return void + */ + protected function setUp(): void { + parent::setUp(); + + $this->resetAfterTest(); + $this->setAdminUser(); + + require_once(__DIR__ . '/../fixtures/test_provider.php'); + + $category = $this->getDataGenerator()->create_custom_field_category([ + 'component' => 'core_customfield', + 'area' => 'shared', + ]); + + toggle_shared_category::execute($category->get('id'), 'core_course', 'course', 0, true); + + $this->field = $this->getDataGenerator()->create_custom_field([ + 'categoryid' => $category->get('id'), + 'shortname' => 'seconds', + 'type' => 'number', + 'configdata' => [ + 'fieldtype' => test_provider::class, + ], + ]); + } + + /** + * Test running the cron task to recalculate number custom field values when data is created by the provider. + * + * @return void + */ + public function test_execute_without_data(): void { + $clock = $this->mock_clock_with_frozen(); + $fieldid = $this->field->get('id'); + $fields = [$fieldid => $this->field]; + $course = $this->getDataGenerator()->create_course(); + $courseid = (int)$course->id; + + // Confirm current value. + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course'); + $this->assertNull($data[$fieldid]->get_value()); + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared'); + $this->assertNull($data[$fieldid]->get_value()); + + // Run the cron task and confirm the value is updated. + (new cron())->execute(); + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course'); + $this->assertEquals($clock->time() % 3600, $data[$fieldid]->get_value()); + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared'); + $this->assertNull($data[$fieldid]->get_value()); + } + + /** + * Test running the cron task to recalculate number custom field values when there is already data. + * + * @return void + */ + public function test_execute_with_existing_data(): void { + $clock = $this->mock_clock_with_frozen(); + $fieldid = $this->field->get('id'); + $fields = [$fieldid => $this->field]; + $course = $this->getDataGenerator()->create_course([ + 'customfield_seconds' => 10, + ]); + $courseid = (int)$course->id; + + // Confirm current value. + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course'); + $this->assertEquals(10, $data[$fieldid]->get_value()); + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared'); + $this->assertNull($data[$fieldid]->get_value()); + + // Run the cron task and confirm the value is updated. + (new cron())->execute(); + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_course', 'course'); + $this->assertEquals($clock->time() % 3600, $data[$fieldid]->get_value()); + $data = api::get_instance_fields_data($fields, $courseid, true, 'core_customfield', 'shared'); + $this->assertNull($data[$fieldid]->get_value()); + } +}