mirror of
https://github.com/moodle/moodle.git
synced 2025-08-07 09:56:38 +02:00

Applied the following changes to various testcase classes: - Namespaced with component[\level2-API] - Moved to level2-API subdirectory when required. - Fixed incorrect use statements with leading backslash. - Remove file phpdoc block - Remove MOODLE_INTERNAL if not needed. - Changed code to point to global scope when needed. - Fix some relative paths and comments here and there. - All them passing individually. - Complete runs passing too. Special mention to: - The following task tests have been moved within the level2 directory: - \core\adhoc_task_test => \core\task\adhoc_task_test - \core\scheduled_task_test => \core\task\scheduled_task_test - \core\calendar_cron_task_test => \core\task\calendar_cron_task_test - \core\h5p_get_content_types_task_test => \core\task\h5p_get_content_types_task_test - \core\task_database_logger_test => \core\task\database_logger_test - \core\task_logging_test => \core\task\logging_test - The following event tests have been moved within level2 directory: - \core\event_context_locked_test => \core\event\context_locked_test - \core\event_deprecated_test => \core\event\deprecated_test - \core\event_grade_deleted_test => \core\event\grade_deleted_test - \core\event_profile_field_test => \core\event\profile_field_test - \core\event_unknown_logged_test => \core\event\unknown_logged_test - \core\event_user_graded_test => \core\event\user_graded_test - \core\event_user_password_updated_test => \core\event\user_password_updated_test - The following output tests have been moved within level2 directory: - \core\mustache_template_finder_test => \core\output\mustache_template_finder_test - \core\mustache_template_source_loader_test => \core\output\mustache_template_source_loader_test - \core\output_mustache_helper_collection_test => \core\output\mustache_helper_collection_test - The following tests have been moved to their correct tests directories: - lib/tests/time_splittings_test.php => analytics/tests/time_splittings_test.php - All the classes and tests under lib/filebrowser and lib/filestorage belong to core, not to core_files. Some day we should move them to their correct subsystem. - All the classes and tests under lib/grade belong to core, not to core_grades. Some day we should move them to their correct subsystem. - The core_grades_external class and its \core\grades_external_test unit test should belong to the grades subsystem or, alternatively, to \core\external, they both should be moved together. - The core_grading_external class and its \core\grading_external_test unit test should belong to the grading subsystem or, alternatively, to \core\external, they both should be moved together. - The \core\message\message and \core\message\inbound (may be others) classes, and their associated tests should go to the core_message subsystem. - The core_user class, and its associated tests should go to the core_user subsystem. - The \core\update namespace is plain wrong (update is not valid API) and needs action 1) create it or 2) move elsewhere.
319 lines
12 KiB
PHP
319 lines
12 KiB
PHP
<?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\event;
|
|
|
|
use profile_define_base;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
global $CFG;
|
|
|
|
require_once($CFG->dirroot . '/user/profile/definelib.php');
|
|
|
|
/**
|
|
* Tests the events related to the user profile fields and categories.
|
|
*
|
|
* @package core
|
|
* @category test
|
|
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class profile_field_test extends \advanced_testcase {
|
|
|
|
/**
|
|
* Test set up.
|
|
*/
|
|
public function setUp(): void {
|
|
$this->resetAfterTest();
|
|
}
|
|
|
|
/**
|
|
* Test that triggering the user_info_category_created event works as expected.
|
|
*/
|
|
public function test_user_info_category_created_event() {
|
|
// Create a new profile category.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
|
|
// Trigger the event.
|
|
$sink = $this->redirectEvents();
|
|
\core\event\user_info_category_created::create_from_category($cat1)->trigger();
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Confirm we got the right number of events.
|
|
$this->assertCount(1, $events);
|
|
|
|
// Validate that the event was correctly triggered.
|
|
$event = reset($events);
|
|
$this->assertInstanceOf('\core\event\user_info_category_created', $event);
|
|
$this->assertEquals($event->objectid, $cat1->id);
|
|
$this->assertEquals($event->other['name'], $cat1->name);
|
|
}
|
|
|
|
/**
|
|
* Test that moving a user info category triggers an updated event.
|
|
*/
|
|
public function test_user_info_category_updated_event() {
|
|
global $DB;
|
|
|
|
// Create new profile categories.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
$cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category 2']);
|
|
|
|
// Trigger the events.
|
|
$sink = $this->redirectEvents();
|
|
profile_move_category($cat1->id, 'down');
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Should now have two events.
|
|
$this->assertCount(2, $events);
|
|
$event1 = array_shift($events);
|
|
$event2 = array_shift($events);
|
|
|
|
// Validate that the events were correctly triggered.
|
|
$this->assertInstanceOf('\core\event\user_info_category_updated', $event1);
|
|
$this->assertEquals($event1->objectid, $cat1->id);
|
|
$this->assertEquals($event1->other['name'], $cat1->name);
|
|
|
|
$this->assertInstanceOf('\core\event\user_info_category_updated', $event2);
|
|
$this->assertEquals($event2->objectid, $cat2->id);
|
|
$this->assertEquals($event2->other['name'], $cat2->name);
|
|
}
|
|
|
|
/**
|
|
* Test that deleting a user info category triggers a delete event.
|
|
*/
|
|
public function test_user_info_category_deleted_event() {
|
|
// Create new profile categories.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
$cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category 2']);
|
|
|
|
// Trigger the event.
|
|
$sink = $this->redirectEvents();
|
|
profile_delete_category($cat2->id);
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Confirm we got the right number of events.
|
|
$this->assertCount(1, $events);
|
|
|
|
// Validate that the event was correctly triggered.
|
|
$event = reset($events);
|
|
$this->assertInstanceOf('\core\event\user_info_category_deleted', $event);
|
|
$this->assertEquals($event->objectid, $cat2->id);
|
|
$this->assertEquals($event->other['name'], $cat2->name);
|
|
}
|
|
|
|
/**
|
|
* Test that creating a user info field triggers a create event.
|
|
*/
|
|
public function test_user_info_field_created_event() {
|
|
global $DB;
|
|
|
|
// Create a new profile category.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
|
|
// Create a new profile field.
|
|
$data = new \stdClass();
|
|
$data->datatype = 'text';
|
|
$data->shortname = 'example';
|
|
$data->name = 'Example field';
|
|
$data->description = 'Hello this is an example.';
|
|
$data->required = false;
|
|
$data->locked = false;
|
|
$data->forceunique = false;
|
|
$data->signup = false;
|
|
$data->visible = '0';
|
|
$data->categoryid = $cat1->id;
|
|
|
|
// Trigger the event.
|
|
$sink = $this->redirectEvents();
|
|
$field = new profile_define_base();
|
|
$field->define_save($data);
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Get the field that was created.
|
|
$field = $DB->get_record('user_info_field', array('shortname' => $data->shortname));
|
|
|
|
// Confirm we got the right number of events.
|
|
$this->assertCount(1, $events);
|
|
|
|
// Validate that the event was correctly triggered.
|
|
$event = reset($events);
|
|
$this->assertInstanceOf('\core\event\user_info_field_created', $event);
|
|
$this->assertEquals($event->objectid, $field->id);
|
|
$this->assertEquals($event->other['shortname'], $field->shortname);
|
|
$this->assertEquals($event->other['name'], $field->name);
|
|
$this->assertEquals($event->other['datatype'], $field->datatype);
|
|
}
|
|
|
|
/**
|
|
* Test that updating a user info field triggers an update event.
|
|
*/
|
|
public function test_user_info_field_updated_event() {
|
|
// Create a new profile category.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
|
|
// Create a new profile field.
|
|
$data = $this->getDataGenerator()->create_custom_profile_field([
|
|
'datatype' => 'text',
|
|
'shortname' => 'example',
|
|
'name' => 'Example field',
|
|
'description' => 'Hello this is an example.',
|
|
'categoryid' => $cat1->id,
|
|
]);
|
|
|
|
// Trigger the event.
|
|
$sink = $this->redirectEvents();
|
|
$field = new profile_define_base();
|
|
$field->define_save($data);
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Confirm we got the right number of events.
|
|
$this->assertCount(1, $events);
|
|
|
|
// Validate that the event was correctly triggered.
|
|
$event = reset($events);
|
|
$this->assertInstanceOf('\core\event\user_info_field_updated', $event);
|
|
$this->assertEquals($event->objectid, $data->id);
|
|
$this->assertEquals($event->other['shortname'], $data->shortname);
|
|
$this->assertEquals($event->other['name'], $data->name);
|
|
$this->assertEquals($event->other['datatype'], $data->datatype);
|
|
}
|
|
|
|
/**
|
|
* Test that moving a field triggers update events.
|
|
*/
|
|
public function test_user_info_field_updated_event_move_field() {
|
|
// Create a new profile category.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
|
|
// Create a new profile field.
|
|
$field1 = $this->getDataGenerator()->create_custom_profile_field([
|
|
'datatype' => 'text',
|
|
'shortname' => 'example',
|
|
'name' => 'Example field',
|
|
'description' => 'Hello this is an example.',
|
|
'categoryid' => $cat1->id,
|
|
]);
|
|
|
|
// Create another that we will be moving.
|
|
$field2 = $this->getDataGenerator()->create_custom_profile_field([
|
|
'datatype' => 'text',
|
|
'shortname' => 'example2',
|
|
'name' => 'Example field 2',
|
|
'categoryid' => $cat1->id,
|
|
]);
|
|
|
|
// Trigger the events.
|
|
$sink = $this->redirectEvents();
|
|
profile_move_field($field2->id, 'up');
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Should now have two events.
|
|
$this->assertCount(2, $events);
|
|
$event1 = array_shift($events);
|
|
$event2 = array_shift($events);
|
|
|
|
// Validate that the events were correctly triggered.
|
|
$this->assertInstanceOf('\core\event\user_info_field_updated', $event1);
|
|
$this->assertEquals($event1->objectid, $field2->id);
|
|
$this->assertEquals($event1->other['shortname'], $field2->shortname);
|
|
$this->assertEquals($event1->other['name'], $field2->name);
|
|
$this->assertEquals($event1->other['datatype'], $field2->datatype);
|
|
|
|
$this->assertInstanceOf('\core\event\user_info_field_updated', $event2);
|
|
$this->assertEquals($event2->objectid, $field1->id);
|
|
$this->assertEquals($event2->other['shortname'], $field1->shortname);
|
|
$this->assertEquals($event2->other['name'], $field1->name);
|
|
$this->assertEquals($event2->other['datatype'], $field1->datatype);
|
|
}
|
|
|
|
/**
|
|
* Test that when we delete a category that contains a field, that the field being moved to
|
|
* another category triggers an update event.
|
|
*/
|
|
public function test_user_info_field_updated_event_delete_category() {
|
|
// Create profile categories.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
$cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
|
|
// Create a new profile field.
|
|
$field = $this->getDataGenerator()->create_custom_profile_field([
|
|
'datatype' => 'text',
|
|
'shortname' => 'example',
|
|
'name' => 'Example field',
|
|
'description' => 'Hello this is an example.',
|
|
'categoryid' => $cat1->id,
|
|
]);
|
|
|
|
// Trigger the event.
|
|
$sink = $this->redirectEvents();
|
|
profile_delete_category($cat1->id);
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Check we got the right number of events.
|
|
$this->assertCount(2, $events);
|
|
|
|
// Validate that the event was correctly triggered.
|
|
$event = reset($events);
|
|
$this->assertInstanceOf('\core\event\user_info_field_updated', $event);
|
|
$this->assertEquals($event->objectid, $field->id);
|
|
$this->assertEquals($event->other['shortname'], $field->shortname);
|
|
$this->assertEquals($event->other['name'], $field->name);
|
|
$this->assertEquals($event->other['datatype'], $field->datatype);
|
|
}
|
|
|
|
/**
|
|
* Test that deleting a user info field triggers a delete event.
|
|
*/
|
|
public function test_user_info_field_deleted_event() {
|
|
// Create a new profile category.
|
|
$cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
|
|
|
|
// Create a new profile field.
|
|
$data = $this->getDataGenerator()->create_custom_profile_field([
|
|
'datatype' => 'text',
|
|
'shortname' => 'delete',
|
|
'name' => 'Example field for delete',
|
|
'description' => 'Hello this is an example.',
|
|
'categoryid' => $cat1->id,
|
|
]);
|
|
|
|
// Trigger the event.
|
|
$sink = $this->redirectEvents();
|
|
profile_delete_field($data->id);
|
|
$events = $sink->get_events();
|
|
$sink->close();
|
|
|
|
// Confirm we got the right number of events.
|
|
$this->assertCount(1, $events);
|
|
|
|
// Validate that the event was correctly triggered.
|
|
$event = reset($events);
|
|
$this->assertInstanceOf('\core\event\user_info_field_deleted', $event);
|
|
$this->assertEquals($event->objectid, $data->id);
|
|
$this->assertEquals($event->other['shortname'], $data->shortname);
|
|
$this->assertEquals($event->other['name'], $data->name);
|
|
$this->assertEquals($event->other['datatype'], $data->datatype);
|
|
}
|
|
}
|