MDL-63694 block_html: Implement get_content_for_external
[moodle.git] / blocks / tests / externallib_test.php
CommitLineData
8446ba36
JL
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 * External block functions unit tests
19 *
20 * @package core_block
21 * @category external
22 * @copyright 2017 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.3
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
0f8b2604 32require_once($CFG->dirroot . '/my/lib.php');
8446ba36
JL
33
34/**
35 * External block functions unit tests
36 *
37 * @package core_block
38 * @category external
39 * @copyright 2015 Juan Leyva <juan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 * @since Moodle 3.0
42 */
43class core_block_externallib_testcase extends externallib_advanced_testcase {
44
45 /**
46 * Test get_course_blocks
47 */
48 public function test_get_course_blocks() {
49 global $DB, $FULLME;
50
51 $this->resetAfterTest(true);
52
53 $user = $this->getDataGenerator()->create_user();
54 $course = $this->getDataGenerator()->create_course();
55 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
56 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
57
58 $page = new moodle_page();
59 $page->set_context(context_course::instance($course->id));
60 $page->set_pagelayout('course');
61 $course->format = course_get_format($course)->get_format();
62 $page->set_pagetype('course-view-' . $course->format);
63 $page->blocks->load_blocks();
64 $newblock = 'calendar_upcoming';
65 $page->blocks->add_block_at_end_of_default_region($newblock);
66 $this->setUser($user);
67
68 // Check for the new block.
69 $result = core_block_external::get_course_blocks($course->id);
70 // We need to execute the return values cleaning process to simulate the web service server.
71 $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result);
72
73 // Expect the new block.
74 $this->assertCount(1, $result['blocks']);
75 $this->assertEquals($newblock, $result['blocks'][0]['name']);
76 }
77
78 /**
79 * Test get_course_blocks on site home
80 */
81 public function test_get_course_blocks_site_home() {
82 global $DB, $FULLME;
83
84 $this->resetAfterTest(true);
85
86 $user = $this->getDataGenerator()->create_user();
87
88 $page = new moodle_page();
89 $page->set_context(context_course::instance(SITEID));
90 $page->set_pagelayout('frontpage');
91 $page->set_pagetype('site-index');
92 $page->blocks->load_blocks();
93 $newblock = 'calendar_upcoming';
94 $page->blocks->add_block_at_end_of_default_region($newblock);
95 $this->setUser($user);
96
97 // Check for the new block.
98 $result = core_block_external::get_course_blocks(SITEID);
99 // We need to execute the return values cleaning process to simulate the web service server.
100 $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result);
101
102 // Expect the new block.
103 $this->assertCount(1, $result['blocks']);
104 $this->assertEquals($newblock, $result['blocks'][0]['name']);
105 }
106
107 /**
108 * Test get_course_blocks
109 */
110 public function test_get_course_blocks_overrides() {
111 global $DB, $CFG, $FULLME;
112
113 $this->resetAfterTest(true);
114
115 $CFG->defaultblocks_override = 'participants,search_forums,course_list:calendar_upcoming,recent_activity';
116
117 $user = $this->getDataGenerator()->create_user();
118 $course = $this->getDataGenerator()->create_course();
119 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
120 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
121
122 $this->setUser($user);
123
124 // Try default blocks.
125 $result = core_block_external::get_course_blocks($course->id);
126 // We need to execute the return values cleaning process to simulate the web service server.
127 $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result);
128
129 // Expect 5 default blocks.
130 $this->assertCount(5, $result['blocks']);
131
132 $expectedblocks = array('navigation', 'settings', 'participants', 'search_forums', 'course_list',
133 'calendar_upcoming', 'recent_activity');
134 foreach ($result['blocks'] as $block) {
135 if (!in_array($block['name'], $expectedblocks)) {
136 $this->fail("Unexpected block found: " . $block['name']);
137 }
138 }
139
140 }
141
0f8b2604
JL
142 /**
143 * Test user get default dashboard blocks.
144 */
145 public function test_get_dashboard_blocks_default_dashboard() {
146 global $PAGE, $DB;
147 $this->resetAfterTest(true);
148
149 $user = $this->getDataGenerator()->create_user();
150 $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set.
151
152 // Get the expected default blocks.
153 $alldefaultblocksordered = $DB->get_records_menu('block_instances',
154 array('pagetypepattern' => 'my-index'), 'defaultregion, defaultweight ASC', 'id, blockname');
155
156 $this->setUser($user);
157
158 // Check for the default blocks.
159 $result = core_block_external::get_dashboard_blocks($user->id);
160 // We need to execute the return values cleaning process to simulate the web service server.
161 $result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result);
162 // Expect all blogs except learning plans one (no learning plans to show).
163 $this->assertCount(count($alldefaultblocksordered) - 1, $result['blocks']);
164 $returnedblocks = array();
165 foreach ($result['blocks'] as $block) {
166 // Check all the returned blocks are in the expected blocks array.
167 $this->assertContains($block['name'], $alldefaultblocksordered);
168 $returnedblocks[] = $block['name'];
169 }
170 // Remove lp block.
171 array_shift($alldefaultblocksordered);
172 // Check that we received the blocks in the expected order.
173 $this->assertEquals(array_values($alldefaultblocksordered), $returnedblocks);
174 }
175
176 /**
177 * Test user get default dashboard blocks including a sticky block.
178 */
179 public function test_get_dashboard_blocks_default_dashboard_including_sticky_block() {
180 global $PAGE, $DB;
181 $this->resetAfterTest(true);
182
183 $user = $this->getDataGenerator()->create_user();
184 $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set.
185
186 // Get the expected default blocks.
187 $alldefaultblocks = $DB->get_records_menu('block_instances', array('pagetypepattern' => 'my-index'), '', 'id, blockname');
188
189 // Now, add a sticky block.
190 $page = new moodle_page();
191 $page->set_context(context_system::instance());
192 $page->set_pagetype('my-index');
193 $page->set_url(new moodle_url('/'));
194 $page->blocks->add_region('side-pre');
195 $page->blocks->load_blocks();
196 $page->blocks->add_block('myprofile', 'side-pre', 0, true, '*');
197
198 $this->setUser($user);
199
200 // Check for the default blocks plus the sticky.
201 $result = core_block_external::get_dashboard_blocks($user->id);
202 // We need to execute the return values cleaning process to simulate the web service server.
203 $result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result);
204 // Expect all blogs plus sticky one except learning plans one (no learning plans to show).
205 $this->assertCount(count($alldefaultblocks), $result['blocks']);
206 $found = false;
207 foreach ($result['blocks'] as $block) {
208 if ($block['name'] == 'myprofile') {
209 $this->assertEquals('side-pre', $block['region']);
210 $found = true;
211 continue;
212 }
213 // Check that the block is in the expected blocks array.
214 $this->assertContains($block['name'], $alldefaultblocks);
215 }
216 $this->assertTrue($found);
217 }
218
219 /**
220 * Test admin get user's custom dashboard blocks.
221 */
222 public function test_get_dashboard_blocks_custom_user_dashboard() {
223 global $PAGE, $DB;
224 $this->resetAfterTest(true);
225
226 $user = $this->getDataGenerator()->create_user();
227 $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set.
228
229 // Get the expected default blocks.
230 $alldefaultblocks = $DB->get_records_menu('block_instances', array('pagetypepattern' => 'my-index'), '', 'id, blockname');
231
232 // Add a custom block.
233 $page = new moodle_page();
234 $page->set_context(context_user::instance($user->id));
235 $page->set_pagelayout('mydashboard');
236 $page->set_pagetype('my-index');
237 $page->blocks->add_region('content');
238 $currentpage = my_get_page($user->id, MY_PAGE_PRIVATE);
239 $page->set_subpage($currentpage->id);
240 $page->blocks->load_blocks();
241 $page->blocks->add_block('myprofile', 'content', 0, false);
242
243 $this->setAdminUser();
244
245 // Check for the new block as admin for a user.
246 $result = core_block_external::get_dashboard_blocks($user->id);
247 // We need to execute the return values cleaning process to simulate the web service server.
248 $result = external_api::clean_returnvalue(core_block_external::get_dashboard_blocks_returns(), $result);
249 // Expect all default blogs plys the one we added except learning plans one (no learning plans to show).
250 $this->assertCount(count($alldefaultblocks), $result['blocks']);
251 $found = false;
252 foreach ($result['blocks'] as $block) {
253 if ($block['name'] == 'myprofile') {
254 $this->assertEquals('content', $block['region']);
255 $found = true;
256 continue;
257 }
258 // Check that the block is in the expected blocks array.
259 $this->assertContains($block['name'], $alldefaultblocks);
260 }
261 $this->assertTrue($found);
262 }
263
264 /**
265 * Test user tries to get other user blocks not having permission.
266 */
267 public function test_get_dashboard_blocks_other_user_missing_permissions() {
268 $this->resetAfterTest(true);
269
270 $user1 = $this->getDataGenerator()->create_user();
271 $user2 = $this->getDataGenerator()->create_user();
272
273 $this->setUser($user1);
274
275 $this->expectException('moodle_exception');
276 core_block_external::get_dashboard_blocks($user2->id);
277 }
8446ba36 278}