1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Online users tests
19 *
20 * @package    block_myoverview
21 * @category   test
22 * @copyright  2019 Juan Leyva <juan@moodle.com>
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Online users testcase
30 *
31 * @package    block_myoverview
32 * @category   test
33 * @copyright  2019 Juan Leyva <juan@moodle.com>
34 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class block_myoverview_testcase extends advanced_testcase {
37
38    /**
39     * Test getting block configuration
40     */
41    public function test_get_block_config_for_external() {
42        global $PAGE, $CFG, $OUTPUT;
43        require_once($CFG->dirroot . '/my/lib.php');
44
45        $this->resetAfterTest(true);
46
47        $user = $this->getDataGenerator()->create_user();
48
49        $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']);
50
51        $customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text',
52            'categoryid' => $fieldcategory->get('id')];
53        $field = self::getDataGenerator()->create_custom_field($customfield);
54
55        $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value I'];
56        $course1  = self::getDataGenerator()->create_course(['customfields' => [$customfieldvalue]]);
57        $customfieldvalue = ['shortname' => 'test', 'value' => 'Test value II'];
58        $course2  = self::getDataGenerator()->create_course(['customfields' => [$customfieldvalue]]);
59        $this->getDataGenerator()->enrol_user($user->id, $course1->id, 'student');
60        $this->getDataGenerator()->enrol_user($user->id, $course2->id, 'student');
61
62        // Force a setting change to check the returned blocks settings.
63        set_config('displaygroupingcustomfield', 1, 'block_myoverview');
64        set_config('customfiltergrouping', $field->get('shortname'), 'block_myoverview');
65
66        $this->setUser($user);
67        $context = context_user::instance($user->id);
68
69        if (!$currentpage = my_get_page($user->id, MY_PAGE_PRIVATE)) {
70            throw new moodle_exception('mymoodlesetup');
71        }
72
73        $PAGE->set_url('/my/index.php');    // Need this because some internal API calls require the $PAGE url to be set.
74        $PAGE->set_context($context);
75        $PAGE->set_pagelayout('mydashboard');
76        $PAGE->set_pagetype('my-index');
77        $PAGE->blocks->add_region('content');   // Need to add this special regition to retrieve the central blocks.
78        $PAGE->set_subpage($currentpage->id);
79
80        // Load the block instances for all the regions.
81        $PAGE->blocks->load_blocks();
82        $PAGE->blocks->create_all_block_instances();
83
84        $blocks = $PAGE->blocks->get_content_for_all_regions($OUTPUT);
85        $configs = null;
86        foreach ($blocks as $region => $regionblocks) {
87            $regioninstances = $PAGE->blocks->get_blocks_for_region($region);
88
89            foreach ($regioninstances as $ri) {
90                // Look for myoverview block only.
91                if ($ri->instance->blockname == 'myoverview') {
92                    $configs = $ri->get_config_for_external();
93                    break 2;
94                }
95            }
96        }
97
98        // Test we receive all we expect (exact number and values of settings).
99        $this->assertNotEmpty($configs);
100        $this->assertEmpty((array) $configs->instance);
101        $this->assertCount(13, (array) $configs->plugin);
102        $this->assertEquals('test', $configs->plugin->customfiltergrouping);
103        // Test default values.
104        $this->assertEquals(1, $configs->plugin->displaycategories);
105        $this->assertEquals(1, $configs->plugin->displaygroupingall);
106        $this->assertEquals(0, $configs->plugin->displaygroupingallincludinghidden);
107        $this->assertEquals(1, $configs->plugin->displaygroupingcustomfield);
108        $this->assertEquals(1, $configs->plugin->displaygroupingfuture);
109        $this->assertEquals(1, $configs->plugin->displaygroupinghidden);
110        $this->assertEquals(1, $configs->plugin->displaygroupinginprogress);
111        $this->assertEquals(1, $configs->plugin->displaygroupingpast);
112        $this->assertEquals(1, $configs->plugin->displaygroupingfavourites);
113        $this->assertEquals('card,list,summary', $configs->plugin->layouts);
114        $this->assertEquals(get_config('block_myoverview', 'version'), $configs->plugin->version);
115        // Test custom fields.
116        $this->assertJson($configs->plugin->customfieldsexport);
117        $fields = json_decode($configs->plugin->customfieldsexport);
118        $this->assertEquals('Test value I', $fields[0]->name);
119        $this->assertEquals('Test value I', $fields[0]->value);
120        $this->assertFalse($fields[0]->active);
121        $this->assertEquals('Test value II', $fields[1]->name);
122        $this->assertEquals('Test value II', $fields[1]->value);
123        $this->assertFalse($fields[1]->active);
124        $this->assertEquals('No Custom field', $fields[2]->name);
125        $this->assertFalse($fields[2]->active);
126    }
127}
128