Merge branch 'MDL-64285-34' of git://github.com/junpataleta/moodle into MOODLE_34_STABLE
[moodle.git] / blocks / tests / externallib_test.php
blobfdf3bea560b744e7a7a1af631600e562636e8c4e
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.
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/>.
17 /**
18 * External block functions unit tests
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
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
33 /**
34 * External block functions unit tests
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
42 class core_block_externallib_testcase extends externallib_advanced_testcase {
44 /**
45 * Test get_course_blocks
47 public function test_get_course_blocks() {
48 global $DB, $FULLME;
50 $this->resetAfterTest(true);
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);
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);
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);
72 // Expect the new block.
73 $this->assertCount(1, $result['blocks']);
74 $this->assertEquals($newblock, $result['blocks'][0]['name']);
77 /**
78 * Test get_course_blocks on site home
80 public function test_get_course_blocks_site_home() {
81 global $DB, $FULLME;
83 $this->resetAfterTest(true);
85 $user = $this->getDataGenerator()->create_user();
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);
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);
101 // Expect the new block.
102 $this->assertCount(1, $result['blocks']);
103 $this->assertEquals($newblock, $result['blocks'][0]['name']);
107 * Test get_course_blocks
109 public function test_get_course_blocks_overrides() {
110 global $DB, $CFG, $FULLME;
112 $this->resetAfterTest(true);
114 $CFG->defaultblocks_override = 'participants,search_forums,course_list:calendar_upcoming,recent_activity';
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);
121 $this->setUser($user);
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);
128 // Expect 5 default blocks.
129 $this->assertCount(5, $result['blocks']);
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']);