mirror of
https://github.com/moodle/moodle.git
synced 2025-08-11 20:06:46 +02:00
MDL-74054 qbank_columnsortorder: Improve and expand unit tests
This commit is contained in:
parent
330908868b
commit
4630043f46
1 changed files with 128 additions and 63 deletions
|
@ -39,62 +39,60 @@ require_once($CFG->dirroot . '/question/classes/external.php');
|
||||||
* @covers \qbank_columnsortorder\column_manager
|
* @covers \qbank_columnsortorder\column_manager
|
||||||
*/
|
*/
|
||||||
class column_manager_test extends advanced_testcase {
|
class column_manager_test extends advanced_testcase {
|
||||||
/** @var \stdClass course record. */
|
|
||||||
protected $course;
|
|
||||||
|
|
||||||
/** @var \core_question\local\bank\view */
|
|
||||||
protected $questionbank;
|
|
||||||
|
|
||||||
/** @var array */
|
|
||||||
protected $columns;
|
|
||||||
|
|
||||||
/** @var \qbank_columnsortorder\column_manager */
|
|
||||||
protected $columnmanager;
|
|
||||||
|
|
||||||
/** @var string */
|
|
||||||
protected $randomstring;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup testcase.
|
* Generate a course and return a question bank view for the course context.
|
||||||
|
*
|
||||||
|
* @return view
|
||||||
*/
|
*/
|
||||||
public function setUp(): void {
|
protected static function get_question_bank(): view {
|
||||||
$this->resetAfterTest(true);
|
$course = self::getDataGenerator()->create_course();
|
||||||
$this->setAdminUser();
|
$questionbank = new view(
|
||||||
$this->course = $this->getDataGenerator()->create_course();
|
new question_edit_contexts(context_course::instance($course->id)),
|
||||||
// Creates question bank view.
|
|
||||||
$this->questionbank = new view(
|
|
||||||
new question_edit_contexts(context_course::instance($this->course->id)),
|
|
||||||
new moodle_url('/'),
|
new moodle_url('/'),
|
||||||
$this->course
|
$course
|
||||||
);
|
);
|
||||||
|
return $questionbank;
|
||||||
// Get current view columns.
|
|
||||||
$this->columns = [];
|
|
||||||
foreach ($this->questionbank->get_visiblecolumns() as $column) {
|
|
||||||
$this->columns[] = $column->get_column_id();
|
|
||||||
}
|
|
||||||
$this->columnmanager = new column_manager(true);
|
|
||||||
$this->randomstring = random_string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array of visible columns for the question bank.
|
||||||
|
*
|
||||||
|
* @param view $questionbank
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected static function get_columns(view $questionbank): array {
|
||||||
|
$columns = [];
|
||||||
|
foreach ($questionbank->get_visiblecolumns() as $column) {
|
||||||
|
$columns[] = $column->get_column_id();
|
||||||
|
}
|
||||||
|
return $columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provide examples for testing each column setting function, with test data and data format.
|
||||||
|
*
|
||||||
|
* @return array[]
|
||||||
|
*/
|
||||||
public static function settings_provider(): array {
|
public static function settings_provider(): array {
|
||||||
|
$questionbank = self::get_question_bank();
|
||||||
return [
|
return [
|
||||||
'Test set_column_order' => [
|
'Test set_column_order' => [
|
||||||
'setting' => 'enabledcol',
|
'setting' => 'enabledcol',
|
||||||
'function' => 'set_column_order',
|
'function' => 'set_column_order',
|
||||||
'dataproperty' => 'columns',
|
'data' => self::get_columns($questionbank),
|
||||||
'csv' => true,
|
'csv' => true,
|
||||||
],
|
],
|
||||||
'Test set_hidden_columns' => [
|
'Test set_hidden_columns' => [
|
||||||
'setting' => 'hiddencols',
|
'setting' => 'hiddencols',
|
||||||
'function' => 'set_hidden_columns',
|
'function' => 'set_hidden_columns',
|
||||||
'dataproperty' => 'columns',
|
'data' => self::get_columns($questionbank),
|
||||||
'csv' => true,
|
'csv' => true,
|
||||||
],
|
],
|
||||||
'Test set_column_size' => [
|
'Test set_column_size' => [
|
||||||
'setting' => 'colsize',
|
'setting' => 'colsize',
|
||||||
'function' => 'set_column_size',
|
'function' => 'set_column_size',
|
||||||
'dataproperty' => 'randomstring',
|
'data' => random_string(),
|
||||||
'csv' => false,
|
'csv' => false,
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
@ -106,12 +104,13 @@ class column_manager_test extends advanced_testcase {
|
||||||
* @dataProvider settings_provider
|
* @dataProvider settings_provider
|
||||||
* @param string $setting The name of the setting being saved
|
* @param string $setting The name of the setting being saved
|
||||||
* @param string $function The name of the function being called
|
* @param string $function The name of the function being called
|
||||||
* @param string $dataproperty The property of the test class to pass to the function.
|
* @param mixed $data The property of the test class to pass to the function.
|
||||||
* @param bool $csv True of the data is stored as a comma-separated list.
|
* @param bool $csv True of the data is stored as a comma-separated list.
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_settings(string $setting, string $function, string $dataproperty, bool $csv): void {
|
public function test_settings(string $setting, string $function, mixed $data, bool $csv): void {
|
||||||
$data = $this->{$dataproperty};
|
$this->setAdminUser();
|
||||||
|
$this->resetAfterTest(true);
|
||||||
$this->assertFalse(get_config('qbank_columnsortorder', $setting));
|
$this->assertFalse(get_config('qbank_columnsortorder', $setting));
|
||||||
$this->assertEmpty(get_user_preferences('qbank_columnsortorder_' . $setting));
|
$this->assertEmpty(get_user_preferences('qbank_columnsortorder_' . $setting));
|
||||||
column_manager::{$function}($data, true);
|
column_manager::{$function}($data, true);
|
||||||
|
@ -120,18 +119,38 @@ class column_manager_test extends advanced_testcase {
|
||||||
$this->assertEmpty(get_user_preferences('qbank_columnsortorder_' . $setting));
|
$this->assertEmpty(get_user_preferences('qbank_columnsortorder_' . $setting));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test passing null clears the corresponding config setting.
|
||||||
|
*
|
||||||
|
* @dataProvider settings_provider
|
||||||
|
* @param string $setting The name of the setting being saved
|
||||||
|
* @param string $function The name of the function being called
|
||||||
|
* @param mixed $data The property of the test class to pass to the function.
|
||||||
|
* @param bool $csv True of the data is stored as a comma-separated list.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function test_reset_settings(string $setting, string $function, mixed $data, bool $csv): void {
|
||||||
|
$this->setAdminUser();
|
||||||
|
$this->resetAfterTest(true);
|
||||||
|
$initial = $csv ? implode(',', $data) : $data;
|
||||||
|
set_config($setting, $initial, 'qbank_columnsortorder');
|
||||||
|
$this->assertEquals($initial, get_config('qbank_columnsortorder', $setting));
|
||||||
|
column_manager::{$function}(null, true);
|
||||||
|
$this->assertFalse(get_config('qbank_columnsortorder', $setting));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test setting user preferences
|
* Test setting user preferences
|
||||||
*
|
*
|
||||||
* @dataProvider settings_provider
|
* @dataProvider settings_provider
|
||||||
* @param string $setting The name of the setting being saved
|
* @param string $setting The name of the setting being saved
|
||||||
* @param string $function The name of the function being called
|
* @param string $function The name of the function being called
|
||||||
* @param string $dataproperty The property of the test class to pass to the function.
|
* @param mixed $data The data to pass to the function.
|
||||||
* @param bool $csv True of the data is stored as a comma-separated list.
|
* @param bool $csv True of the data is stored as a comma-separated list.
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_settings_user(string $setting, string $function, string $dataproperty, bool $csv): void {
|
public function test_settings_user(string $setting, string $function, mixed $data, bool $csv): void {
|
||||||
$data = $this->{$dataproperty};
|
$this->resetAfterTest(true);
|
||||||
$this->assertFalse(get_config('qbank_columnsortorder', $setting));
|
$this->assertFalse(get_config('qbank_columnsortorder', $setting));
|
||||||
$this->assertEmpty(get_user_preferences('qbank_columnsortorder_' . $setting));
|
$this->assertEmpty(get_user_preferences('qbank_columnsortorder_' . $setting));
|
||||||
column_manager::{$function}($data);
|
column_manager::{$function}($data);
|
||||||
|
@ -140,13 +159,36 @@ class column_manager_test extends advanced_testcase {
|
||||||
$this->assertEquals($expected, get_user_preferences('qbank_columnsortorder_' . $setting));
|
$this->assertEquals($expected, get_user_preferences('qbank_columnsortorder_' . $setting));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test passing null clears the corresponding user preference.
|
||||||
|
*
|
||||||
|
* @dataProvider settings_provider
|
||||||
|
* @param string $setting The name of the setting being saved
|
||||||
|
* @param string $function The name of the function being called
|
||||||
|
* @param mixed $data The property of the test class to pass to the function.
|
||||||
|
* @param bool $csv True of the data is stored as a comma-separated list.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function test_reset_user_settings(string $setting, string $function, mixed $data, bool $csv): void {
|
||||||
|
$this->setAdminUser();
|
||||||
|
$this->resetAfterTest(true);
|
||||||
|
$initial = $csv ? implode(',', $data) : $data;
|
||||||
|
set_user_preference('qbank_columnsortorder_' . $setting, $initial);
|
||||||
|
$this->assertEquals($initial, get_user_preferences('qbank_columnsortorder_' . $setting));
|
||||||
|
column_manager::{$function}(null);
|
||||||
|
$this->assertEmpty(get_user_preferences('qbank_columnsortorder_' . $setting));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test function get_columns in helper class, that proper data is returned.
|
* Test function get_columns in helper class, that proper data is returned.
|
||||||
*
|
*
|
||||||
* @covers ::get_columns
|
* @covers ::get_columns
|
||||||
*/
|
*/
|
||||||
public function test_getcolumns_function(): void {
|
public function test_getcolumns_function(): void {
|
||||||
$questionlistcolumns = $this->columnmanager->get_columns();
|
$this->resetAfterTest(true);
|
||||||
|
$this->setAdminUser();
|
||||||
|
$columnmanager = new column_manager(true);
|
||||||
|
$questionlistcolumns = $columnmanager->get_columns();
|
||||||
$this->assertIsArray($questionlistcolumns);
|
$this->assertIsArray($questionlistcolumns);
|
||||||
foreach ($questionlistcolumns as $columnnobject) {
|
foreach ($questionlistcolumns as $columnnobject) {
|
||||||
$this->assertObjectHasAttribute('class', $columnnobject);
|
$this->assertObjectHasAttribute('class', $columnnobject);
|
||||||
|
@ -161,17 +203,21 @@ class column_manager_test extends advanced_testcase {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_get_sorted_columns(): void {
|
public function test_get_sorted_columns(): void {
|
||||||
$neworder = $this->columns;
|
$this->resetAfterTest(true);
|
||||||
|
$this->setAdminUser();
|
||||||
|
$questionbank = $this->get_question_bank();
|
||||||
|
$columns = $this->get_columns($questionbank);
|
||||||
|
$neworder = $columns;
|
||||||
shuffle($neworder);
|
shuffle($neworder);
|
||||||
set_config('enabledcol', implode(',', $neworder), 'qbank_columnsortorder');
|
set_config('enabledcol', implode(',', $neworder), 'qbank_columnsortorder');
|
||||||
|
|
||||||
$this->columnmanager = new column_manager(true);
|
$columnmanager = new column_manager(true);
|
||||||
$columnstosort = [];
|
$columnstosort = [];
|
||||||
foreach ($this->columns as $column) {
|
foreach ($columns as $column) {
|
||||||
$columnstosort[$column] = $column;
|
$columnstosort[$column] = $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sortedcolumns = $this->columnmanager->get_sorted_columns($columnstosort);
|
$sortedcolumns = $columnmanager->get_sorted_columns($columnstosort);
|
||||||
|
|
||||||
$expectedorder = ['core_question\local\bank\checkbox_column' . column_base::ID_SEPARATOR . 'checkbox_column' => 0];
|
$expectedorder = ['core_question\local\bank\checkbox_column' . column_base::ID_SEPARATOR . 'checkbox_column' => 0];
|
||||||
foreach ($neworder as $columnid) {
|
foreach ($neworder as $columnid) {
|
||||||
|
@ -186,19 +232,25 @@ class column_manager_test extends advanced_testcase {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_disable_columns(): void {
|
public function test_disable_columns(): void {
|
||||||
|
$this->resetAfterTest(true);
|
||||||
|
$this->setAdminUser();
|
||||||
|
$questionbank = $this->get_question_bank();
|
||||||
|
$columns = $this->get_columns($questionbank);
|
||||||
// Set up enabledcol with all plugins.
|
// Set up enabledcol with all plugins.
|
||||||
set_config('enabledcol', implode(',', $this->columns), 'qbank_columnsortorder');
|
set_config('enabledcol', implode(',', $columns), 'qbank_columnsortorder');
|
||||||
$this->columnmanager = new column_manager();
|
$questionbank = $this->get_question_bank();
|
||||||
|
$columns = $this->get_columns($questionbank);
|
||||||
|
$columnmanager = new column_manager(true);
|
||||||
$this->assertFalse(get_config('qbank_columnsortorder', 'disabledcol'));
|
$this->assertFalse(get_config('qbank_columnsortorder', 'disabledcol'));
|
||||||
|
|
||||||
// Disable a random plugin.
|
// Disable a random plugin.
|
||||||
$plugincolumns = array_filter($this->columns, fn($column) => str_starts_with($column, 'qbank_'));
|
$plugincolumns = array_filter($columns, fn($column) => str_starts_with($column, 'qbank_'));
|
||||||
$randomcolumn = $plugincolumns[array_rand($plugincolumns, 1)];
|
$randomcolumn = $plugincolumns[array_rand($plugincolumns, 1)];
|
||||||
$randomplugin = explode('\\', $randomcolumn)[0];
|
$randomplugin = explode('\\', $randomcolumn)[0];
|
||||||
$this->columnmanager->disable_columns($randomplugin);
|
$columnmanager->disable_columns($randomplugin);
|
||||||
|
|
||||||
// The enabledcol setting should now contain all columns except the disabled plugin.
|
// The enabledcol setting should now contain all columns except the disabled plugin.
|
||||||
$expectedconfig = array_filter($this->columns, fn($column) => !str_starts_with($column, $randomplugin));
|
$expectedconfig = array_filter($columns, fn($column) => !str_starts_with($column, $randomplugin));
|
||||||
sort($expectedconfig);
|
sort($expectedconfig);
|
||||||
$newconfig = explode(',', get_config('qbank_columnsortorder', 'enabledcol'));
|
$newconfig = explode(',', get_config('qbank_columnsortorder', 'enabledcol'));
|
||||||
sort($newconfig);
|
sort($newconfig);
|
||||||
|
@ -215,9 +267,14 @@ class column_manager_test extends advanced_testcase {
|
||||||
* @covers \qbank_columnsortorder\event\plugin_observer
|
* @covers \qbank_columnsortorder\event\plugin_observer
|
||||||
*/
|
*/
|
||||||
public function test_plugin_enabled_disabled_observers(): void {
|
public function test_plugin_enabled_disabled_observers(): void {
|
||||||
$neworder = $this->columnmanager->get_sorted_columns($this->columns);
|
$this->resetAfterTest(true);
|
||||||
|
$this->setAdminUser();
|
||||||
|
$questionbank = $this->get_question_bank();
|
||||||
|
$columns = $this->get_columns($questionbank);
|
||||||
|
$columnmanager = new column_manager(true);
|
||||||
|
$neworder = $columnmanager->get_sorted_columns($columns);
|
||||||
shuffle($neworder);
|
shuffle($neworder);
|
||||||
$this->columnmanager::set_column_order($neworder, true);
|
$columnmanager::set_column_order($neworder, true);
|
||||||
// Get the list of enabled columns, excluding core columns (we can't disable those).
|
// Get the list of enabled columns, excluding core columns (we can't disable those).
|
||||||
$currentconfig = get_config('qbank_columnsortorder', 'enabledcol');
|
$currentconfig = get_config('qbank_columnsortorder', 'enabledcol');
|
||||||
$currentconfig = array_filter(explode(',', $currentconfig), fn($class) => !str_starts_with($class, 'core'));
|
$currentconfig = array_filter(explode(',', $currentconfig), fn($class) => !str_starts_with($class, 'core'));
|
||||||
|
@ -244,27 +301,31 @@ class column_manager_test extends advanced_testcase {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_enable_columns() {
|
public function test_enable_columns() {
|
||||||
|
$this->resetAfterTest(true);
|
||||||
|
$this->setAdminUser();
|
||||||
|
$questionbank = $this->get_question_bank();
|
||||||
|
$columns = $this->get_columns($questionbank);
|
||||||
// Set up disablecol with columns from 2 random plugins, and enabledcol with all other columns.
|
// Set up disablecol with columns from 2 random plugins, and enabledcol with all other columns.
|
||||||
$plugincolumns = array_filter($this->columns, fn($column) => str_starts_with($column, 'qbank_'));
|
$plugincolumns = array_filter($columns, fn($column) => str_starts_with($column, 'qbank_'));
|
||||||
$plugins = array_unique(array_map(fn($column) => explode('\\', $column)[0], $plugincolumns));
|
$plugins = array_unique(array_map(fn($column) => explode('\\', $column)[0], $plugincolumns));
|
||||||
$randomplugins = array_rand($plugins, 2);
|
$randomplugins = array_rand($plugins, 2);
|
||||||
$randomplugin1 = $plugins[$randomplugins[0]];
|
$randomplugin1 = $plugins[$randomplugins[0]];
|
||||||
$randomplugin2 = $plugins[$randomplugins[1]];
|
$randomplugin2 = $plugins[$randomplugins[1]];
|
||||||
|
|
||||||
$disabledcols = array_filter(
|
$disabledcols = array_filter(
|
||||||
$this->columns,
|
$columns,
|
||||||
fn($column) => str_starts_with($column, $randomplugin1) || str_starts_with($column, $randomplugin2)
|
fn($column) => str_starts_with($column, $randomplugin1) || str_starts_with($column, $randomplugin2)
|
||||||
);
|
);
|
||||||
$enabledcols = array_diff($this->columns, $disabledcols);
|
$enabledcols = array_diff($columns, $disabledcols);
|
||||||
|
|
||||||
set_config('enabledcol', implode(',', $enabledcols), 'qbank_columnsortorder');
|
set_config('enabledcol', implode(',', $enabledcols), 'qbank_columnsortorder');
|
||||||
set_config('disabledcol', implode(',', $disabledcols), 'qbank_columnsortorder');
|
set_config('disabledcol', implode(',', $disabledcols), 'qbank_columnsortorder');
|
||||||
|
|
||||||
// Enable one of the disabled plugins.
|
// Enable one of the disabled plugins.
|
||||||
$this->columnmanager = new column_manager(true);
|
$columnmanager = new column_manager(true);
|
||||||
$this->columnmanager->enable_columns($randomplugin1);
|
$columnmanager->enable_columns($randomplugin1);
|
||||||
// The enabledcol setting should now contain all columns except the remaining disabled plugin.
|
// The enabledcol setting should now contain all columns except the remaining disabled plugin.
|
||||||
$expectedenabled = array_filter($this->columns, fn($column) => !str_starts_with($column, $randomplugin2));
|
$expectedenabled = array_filter($columns, fn($column) => !str_starts_with($column, $randomplugin2));
|
||||||
$expecteddisabled = array_filter($disabledcols, fn($column) => str_starts_with($column, $randomplugin2));
|
$expecteddisabled = array_filter($disabledcols, fn($column) => str_starts_with($column, $randomplugin2));
|
||||||
sort($expectedenabled);
|
sort($expectedenabled);
|
||||||
sort($expecteddisabled);
|
sort($expecteddisabled);
|
||||||
|
@ -283,25 +344,29 @@ class column_manager_test extends advanced_testcase {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_get_disabled_columns(): void {
|
public function test_get_disabled_columns(): void {
|
||||||
|
$this->resetAfterTest(true);
|
||||||
|
$this->setAdminUser();
|
||||||
|
$questionbank = $this->get_question_bank();
|
||||||
|
$columns = $this->get_columns($questionbank);
|
||||||
// Set up disablecol with columns from 2 random plugins, and enabledcol with all other columns.
|
// Set up disablecol with columns from 2 random plugins, and enabledcol with all other columns.
|
||||||
$plugincolumns = array_filter($this->columns, fn($column) => str_starts_with($column, 'qbank_'));
|
$plugincolumns = array_filter($columns, fn($column) => str_starts_with($column, 'qbank_'));
|
||||||
$randomcolumn = $plugincolumns[array_rand($plugincolumns, 1)];
|
$randomcolumn = $plugincolumns[array_rand($plugincolumns, 1)];
|
||||||
$randomplugin = explode('\\', $randomcolumn)[0];
|
$randomplugin = explode('\\', $randomcolumn)[0];
|
||||||
|
|
||||||
$disabledcols = array_filter($this->columns, fn($column) => str_starts_with($column, $randomplugin));
|
$disabledcols = array_filter($columns, fn($column) => str_starts_with($column, $randomplugin));
|
||||||
|
|
||||||
set_config('disabledcol', implode(',', $disabledcols), 'qbank_columnsortorder');
|
set_config('disabledcol', implode(',', $disabledcols), 'qbank_columnsortorder');
|
||||||
|
|
||||||
$this->columnmanager = new column_manager(true);
|
$columnmanager = new column_manager(true);
|
||||||
$expecteddisablednames = [];
|
$expecteddisablednames = [];
|
||||||
foreach ($disabledcols as $disabledcolid) {
|
foreach ($disabledcols as $disabledcolid) {
|
||||||
[$columnclass, $columnname] = explode(column_base::ID_SEPARATOR, $disabledcolid, 2);
|
[$columnclass, $columnname] = explode(column_base::ID_SEPARATOR, $disabledcolid, 2);
|
||||||
$columnobject = $columnclass::from_column_name($this->questionbank, $columnname);
|
$columnobject = $columnclass::from_column_name($questionbank, $columnname);
|
||||||
$expecteddisablednames[$disabledcolid] = (object) [
|
$expecteddisablednames[$disabledcolid] = (object) [
|
||||||
'disabledname' => $columnobject->get_title(),
|
'disabledname' => $columnobject->get_title(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$disablednames = $this->columnmanager->get_disabled_columns();
|
$disablednames = $columnmanager->get_disabled_columns();
|
||||||
$this->assertEquals($expecteddisablednames, $disablednames);
|
$this->assertEquals($expecteddisablednames, $disablednames);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue