MDL-50970 core_block: New Web Service core_block_get_course_blocks
[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');
32
33/**
34 * External block functions unit tests
35 *
36 * @package core_block
37 * @category external
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 3.0
41 */
42class core_block_externallib_testcase extends externallib_advanced_testcase {
43
44 /**
45 * Test get_course_blocks
46 */
47 public function test_get_course_blocks() {
48 global $DB, $FULLME;
49
50 $this->resetAfterTest(true);
51
52 $user = $this->getDataGenerator()->create_user();
53 $course = $this->getDataGenerator()->create_course();
54 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
55 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
56
57 $page = new moodle_page();
58 $page->set_context(context_course::instance($course->id));
59 $page->set_pagelayout('course');
60 $course->format = course_get_format($course)->get_format();
61 $page->set_pagetype('course-view-' . $course->format);
62 $page->blocks->load_blocks();
63 $newblock = 'calendar_upcoming';
64 $page->blocks->add_block_at_end_of_default_region($newblock);
65 $this->setUser($user);
66
67 // Check for the new block.
68 $result = core_block_external::get_course_blocks($course->id);
69 // We need to execute the return values cleaning process to simulate the web service server.
70 $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result);
71
72 // Expect the new block.
73 $this->assertCount(1, $result['blocks']);
74 $this->assertEquals($newblock, $result['blocks'][0]['name']);
75 }
76
77 /**
78 * Test get_course_blocks on site home
79 */
80 public function test_get_course_blocks_site_home() {
81 global $DB, $FULLME;
82
83 $this->resetAfterTest(true);
84
85 $user = $this->getDataGenerator()->create_user();
86
87 $page = new moodle_page();
88 $page->set_context(context_course::instance(SITEID));
89 $page->set_pagelayout('frontpage');
90 $page->set_pagetype('site-index');
91 $page->blocks->load_blocks();
92 $newblock = 'calendar_upcoming';
93 $page->blocks->add_block_at_end_of_default_region($newblock);
94 $this->setUser($user);
95
96 // Check for the new block.
97 $result = core_block_external::get_course_blocks(SITEID);
98 // We need to execute the return values cleaning process to simulate the web service server.
99 $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result);
100
101 // Expect the new block.
102 $this->assertCount(1, $result['blocks']);
103 $this->assertEquals($newblock, $result['blocks'][0]['name']);
104 }
105
106 /**
107 * Test get_course_blocks
108 */
109 public function test_get_course_blocks_overrides() {
110 global $DB, $CFG, $FULLME;
111
112 $this->resetAfterTest(true);
113
114 $CFG->defaultblocks_override = 'participants,search_forums,course_list:calendar_upcoming,recent_activity';
115
116 $user = $this->getDataGenerator()->create_user();
117 $course = $this->getDataGenerator()->create_course();
118 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
119 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
120
121 $this->setUser($user);
122
123 // Try default blocks.
124 $result = core_block_external::get_course_blocks($course->id);
125 // We need to execute the return values cleaning process to simulate the web service server.
126 $result = external_api::clean_returnvalue(core_block_external::get_course_blocks_returns(), $result);
127
128 // Expect 5 default blocks.
129 $this->assertCount(5, $result['blocks']);
130
131 $expectedblocks = array('navigation', 'settings', 'participants', 'search_forums', 'course_list',
132 'calendar_upcoming', 'recent_activity');
133 foreach ($result['blocks'] as $block) {
134 if (!in_array($block['name'], $expectedblocks)) {
135 $this->fail("Unexpected block found: " . $block['name']);
136 }
137 }
138
139 }
140
141}