diff --git a/blocks/myoverview/block_myoverview.php b/blocks/myoverview/block_myoverview.php index db02741b00a..09a82fe2a5e 100644 --- a/blocks/myoverview/block_myoverview.php +++ b/blocks/myoverview/block_myoverview.php @@ -93,6 +93,21 @@ class block_myoverview extends block_base { // Return all settings for all users since it is safe (no private keys, etc..). $configs = get_config('block_myoverview'); + // Get the customfield values (if any). + if ($configs->displaygroupingcustomfield) { + $group = get_user_preferences('block_myoverview_user_grouping_preference'); + $sort = get_user_preferences('block_myoverview_user_sort_preference'); + $view = get_user_preferences('block_myoverview_user_view_preference'); + $paging = get_user_preferences('block_myoverview_user_paging_preference'); + $customfieldvalue = get_user_preferences('block_myoverview_user_grouping_customfieldvalue_preference'); + + $renderable = new \block_myoverview\output\main($group, $sort, $view, $paging, $customfieldvalue); + $customfieldsexport = $renderable->get_customfield_values_for_export(); + if (!empty($customfieldsexport)) { + $configs->customfieldsexport = json_encode($customfieldsexport); + } + } + return (object) [ 'instance' => new stdClass(), 'plugin' => $configs, diff --git a/blocks/myoverview/tests/myoverview_test.php b/blocks/myoverview/tests/myoverview_test.php new file mode 100644 index 00000000000..bc80abea62d --- /dev/null +++ b/blocks/myoverview/tests/myoverview_test.php @@ -0,0 +1,127 @@ +. + +/** + * Online users tests + * + * @package block_myoverview + * @category test + * @copyright 2019 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Online users testcase + * + * @package block_myoverview + * @category test + * @copyright 2019 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_myoverview_testcase extends advanced_testcase { + + /** + * Test getting block configuration + */ + public function test_get_block_config_for_external() { + global $PAGE, $CFG, $OUTPUT; + require_once($CFG->dirroot . '/my/lib.php'); + + $this->resetAfterTest(true); + + $user = $this->getDataGenerator()->create_user(); + + $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']); + + $customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text', + 'categoryid' => $fieldcategory->get('id')]; + $field = self::getDataGenerator()->create_custom_field($customfield); + + $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value I']; + $course1 = self::getDataGenerator()->create_course(['customfields' => [$customfieldvalue]]); + $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value II']; + $course2 = self::getDataGenerator()->create_course(['customfields' => [$customfieldvalue]]); + $this->getDataGenerator()->enrol_user($user->id, $course1->id, 'student'); + $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student'); + + // Force a setting change to check the returned blocks settings. + set_config('displaygroupingcustomfield', 1, 'block_myoverview'); + set_config('customfiltergrouping', $field->get('shortname'), 'block_myoverview'); + + $this->setUser($user); + $context = context_user::instance($user->id); + + if (!$currentpage = my_get_page($user->id, MY_PAGE_PRIVATE)) { + throw new moodle_exception('mymoodlesetup'); + } + + $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. + $PAGE->set_context($context); + $PAGE->set_pagelayout('mydashboard'); + $PAGE->set_pagetype('my-index'); + $PAGE->blocks->add_region('content'); // Need to add this special regition to retrieve the central blocks. + $PAGE->set_subpage($currentpage->id); + + // Load the block instances for all the regions. + $PAGE->blocks->load_blocks(); + $PAGE->blocks->create_all_block_instances(); + + $blocks = $PAGE->blocks->get_content_for_all_regions($OUTPUT); + $configs = null; + foreach ($blocks as $region => $regionblocks) { + $regioninstances = $PAGE->blocks->get_blocks_for_region($region); + + foreach ($regioninstances as $ri) { + // Look for myoverview block only. + if ($ri->instance->blockname == 'myoverview') { + $configs = $ri->get_config_for_external(); + break 2; + } + } + } + + // Test we receive all we expect (exact number and values of settings). + $this->assertNotEmpty($configs); + $this->assertEmpty((array) $configs->instance); + $this->assertCount(13, (array) $configs->plugin); + $this->assertEquals('test', $configs->plugin->customfiltergrouping); + // Test default values. + $this->assertEquals(1, $configs->plugin->displaycategories); + $this->assertEquals(1, $configs->plugin->displaygroupingall); + $this->assertEquals(0, $configs->plugin->displaygroupingallincludinghidden); + $this->assertEquals(1, $configs->plugin->displaygroupingcustomfield); + $this->assertEquals(1, $configs->plugin->displaygroupingfuture); + $this->assertEquals(1, $configs->plugin->displaygroupinghidden); + $this->assertEquals(1, $configs->plugin->displaygroupinginprogress); + $this->assertEquals(1, $configs->plugin->displaygroupingpast); + $this->assertEquals(1, $configs->plugin->displaygroupingstarred); + $this->assertEquals('card,list,summary', $configs->plugin->layouts); + $this->assertEquals(get_config('block_myoverview', 'version'), $configs->plugin->version); + // Test custom fields. + $this->assertJson($configs->plugin->customfieldsexport); + $fields = json_decode($configs->plugin->customfieldsexport); + $this->assertEquals('Test value I', $fields[0]->name); + $this->assertEquals('Test value I', $fields[0]->value); + $this->assertFalse($fields[0]->active); + $this->assertEquals('Test value II', $fields[1]->name); + $this->assertEquals('Test value II', $fields[1]->value); + $this->assertFalse($fields[1]->active); + $this->assertEquals('No Custom field', $fields[2]->name); + $this->assertFalse($fields[2]->active); + } +}