-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backup and external function test
- Loading branch information
Daniel Thies
committed
Jan 13, 2025
1 parent
a95037f
commit 411dc7c
Showing
5 changed files
with
355 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace block_deft\backup; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use block_deft\task; | ||
|
||
global $CFG; | ||
require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php"); | ||
|
||
/** | ||
* Restore tasks tests. | ||
* | ||
* @package block_deft | ||
* @copyright 2024 Daniel Thies <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* @covers \backup_deft_block_structure_step | ||
* @covers \restore_deft_block_structure_step | ||
* @group block_deft | ||
*/ | ||
final class restore_tasks_test extends \restore_date_testcase { | ||
/** | ||
* Test restore dates. | ||
*/ | ||
public function test_restore_tasks(): void { | ||
global $DB; | ||
|
||
$this->resetAfterTest(true); | ||
|
||
// Create courses. | ||
$course1 = $this->getDataGenerator()->create_course(); | ||
|
||
/** @var \block_deft_generator $generator */ | ||
$generator = $this->getDataGenerator()->get_plugin_generator('block_deft'); | ||
|
||
$block = $this->add_deft_block_in_context(\core\context\course::instance($course1->id)); | ||
$task = $generator->create_task($block->instance->id); | ||
|
||
$this->assertEquals(1, count(task::get_records(['instance' => $block->instance->id]))); | ||
|
||
// Do backup and restore. | ||
$newcourseid = $this->backup_and_restore($course1); | ||
|
||
$this->assertEquals(2, count(task::get_records([]))); | ||
} | ||
|
||
/** | ||
* Creates a Deft response block on a context. | ||
* | ||
* @param \context $context The context on which we want to put the block. | ||
* @return \block_base The created block instance. | ||
* @throws \coding_exception | ||
*/ | ||
protected function add_deft_block_in_context(\context $context) { | ||
global $DB; | ||
|
||
$course = null; | ||
|
||
$page = new \moodle_page(); | ||
$page->set_context($context); | ||
|
||
switch ($context->contextlevel) { | ||
case CONTEXT_SYSTEM: | ||
$page->set_pagelayout('frontpage'); | ||
$page->set_pagetype('site-index'); | ||
break; | ||
case CONTEXT_COURSE: | ||
$page->set_pagelayout('standard'); | ||
$page->set_pagetype('course-view'); | ||
$course = $DB->get_record('course', ['id' => $context->instanceid]); | ||
$page->set_course($course); | ||
break; | ||
case CONTEXT_MODULE: | ||
$page->set_pagelayout('standard'); | ||
$mod = $DB->get_field_sql("SELECT m.name | ||
FROM {modules} m | ||
JOIN {course_modules} cm on cm.module = m.id | ||
WHERE cm.id = ?", [$context->instanceid]); | ||
$page->set_pagetype("mod-$mod-view"); | ||
break; | ||
case CONTEXT_USER: | ||
$page->set_pagelayout('mydashboard'); | ||
$page->set_pagetype('my-index'); | ||
break; | ||
default: | ||
throw new coding_exception('Unsupported context for test'); | ||
} | ||
|
||
$page->blocks->load_blocks(); | ||
|
||
$page->blocks->add_block_at_end_of_default_region('deft'); | ||
|
||
// We need to use another page object as load_blocks() only loads the blocks once. | ||
$page2 = new \moodle_page(); | ||
$page2->set_context($page->context); | ||
$page2->set_pagelayout($page->pagelayout); | ||
$page2->set_pagetype($page->pagetype); | ||
if ($course) { | ||
$page2->set_course($course); | ||
} | ||
|
||
$page->blocks->load_blocks(); | ||
$page2->blocks->load_blocks(); | ||
$blocks = $page2->blocks->get_blocks_for_region($page2->blocks->get_default_region()); | ||
$block = end($blocks); | ||
|
||
$block = block_instance('deft', $block->instance); | ||
|
||
return $block; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
namespace block_deft\external; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
|
||
require_once($CFG->dirroot . '/webservice/tests/helpers.php'); | ||
|
||
use core_external\external_api; | ||
use externallib_advanced_testcase; | ||
use stdClass; | ||
use context_block; | ||
use course_modinfo; | ||
use block_deft\venue_manager; | ||
|
||
/** | ||
* External function test for raise_hand | ||
* | ||
* @package block_deft | ||
* @copyright 2024 Daniel Thies <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* @covers \block_deft\external\raise_hand | ||
* @group mod_plenum | ||
*/ | ||
final class raise_hand_test extends externallib_advanced_testcase { | ||
/** | ||
* Test test_raise_hand invalid id. | ||
*/ | ||
public function test_raise_hand_invalid_id(): void { | ||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
// Setup scenario. | ||
$scenario = $this->setup_scenario(); | ||
|
||
$result = raise_hand::execute(true, ''); | ||
$result = external_api::clean_returnvalue(raise_hand::execute_returns(), $result); | ||
$this->assertFalse($result['status']); | ||
|
||
$result = raise_hand::execute(false, ''); | ||
$result = external_api::clean_returnvalue(raise_hand::execute_returns(), $result); | ||
$this->assertFalse($result['status']); | ||
} | ||
|
||
/** | ||
* Test test_raise_hand user not enrolled. | ||
*/ | ||
public function test_raise_hand_user_not_enrolled(): void { | ||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
// Setup scenario. | ||
$scenario = $this->setup_scenario(); | ||
|
||
// Test not-enrolled user. | ||
$usernotenrolled = self::getDataGenerator()->create_user(); | ||
$this->setUser($usernotenrolled); | ||
$this->expectException('moodle_exception'); | ||
$manager = new venue_manager($scenario->contextblock, $scenario->task); | ||
raise_hand::execute($scenario->contextblock->id, true); | ||
$this->assertTrue($result['status']); | ||
} | ||
|
||
/** | ||
* Test test_raise_hand user student. | ||
*/ | ||
public function test_raise_hand_user_student(): void { | ||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
// Setup scenario. | ||
$scenario = $this->setup_scenario(); | ||
|
||
$this->setUser($scenario->student); | ||
|
||
$manager = new venue_manager($scenario->contextblock, $scenario->task); | ||
|
||
// Trigger and capture the event. | ||
$sink = $this->redirectEvents(); | ||
|
||
$result = raise_hand::execute(true, ''); | ||
$result = external_api::clean_returnvalue(raise_hand::execute_returns(), $result); | ||
$this->assertTrue($result['status']); | ||
|
||
$events = $sink->get_events(); | ||
$this->assertCount(1, $events); | ||
$event = array_shift($events); | ||
|
||
// Checking that the event contains the expected values. | ||
$this->assertInstanceOf('\block_deft\event\hand_raise_sent', $event); | ||
$this->assertEquals($scenario->contextblock, $event->get_context()); | ||
$venue = new \moodle_url('/blocks/deft/venue.php', ['task' => $scenario->task->get('id')]); | ||
$this->assertEquals($venue, $event->get_url()); | ||
$this->assertEventContextNotUsed($event); | ||
$this->assertNotEmpty($event->get_name()); | ||
|
||
$data = $event->get_data(); | ||
$this->assertEquals($scenario->student->id, $data['userid']); | ||
} | ||
|
||
/** | ||
* Test test_lower_hand user student. | ||
*/ | ||
public function test_lower_hand_user_student(): void { | ||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
// Setup scenario. | ||
$scenario = $this->setup_scenario(); | ||
|
||
$this->setUser($scenario->student); | ||
|
||
$manager = new venue_manager($scenario->contextblock, $scenario->task); | ||
|
||
// Trigger and capture the event. | ||
$sink = $this->redirectEvents(); | ||
|
||
$result = raise_hand::execute(false, ''); | ||
$result = external_api::clean_returnvalue(raise_hand::execute_returns(), $result); | ||
$this->assertTrue($result['status']); | ||
|
||
$events = $sink->get_events(); | ||
$this->assertCount(1, $events); | ||
$event = array_shift($events); | ||
|
||
// Checking that the event contains the expected values. | ||
$this->assertInstanceOf('\block_deft\event\hand_lower_sent', $event); | ||
$this->assertEquals($scenario->contextblock, $event->get_context()); | ||
$venue = new \moodle_url('/blocks/deft/venue.php', ['task' => $scenario->task->get('id')]); | ||
$this->assertEquals($venue, $event->get_url()); | ||
$this->assertEventContextNotUsed($event); | ||
$this->assertNotEmpty($event->get_name()); | ||
|
||
$data = $event->get_data(); | ||
$this->assertEquals($scenario->student->id, $data['userid']); | ||
} | ||
|
||
/** | ||
* Test test_raise_hand user missing capabilities. | ||
*/ | ||
public function test_raise_hand_user_missing_capabilities(): void { | ||
global $DB; | ||
|
||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
// Setup scenario. | ||
$scenario = $this->setup_scenario(); | ||
|
||
$studentrole = $DB->get_record('role', ['shortname' => 'student']); | ||
// Test user with no capabilities. | ||
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. | ||
assign_capability('block/deft:joinvenue', CAP_PROHIBIT, $studentrole->id, $scenario->contextcourse->id); | ||
// Empty all the caches that may be affected by this change. | ||
accesslib_clear_all_caches_for_unit_testing(); | ||
course_modinfo::clear_instance_cache(); | ||
|
||
$this->setUser($scenario->student); | ||
$manager = new venue_manager($scenario->contextblock, $scenario->task); | ||
|
||
$result = raise_hand::execute(true, ''); | ||
$result = external_api::clean_returnvalue(raise_hand::execute_returns(), $result); | ||
$this->assertFalse($result['status']); | ||
|
||
$result = raise_hand::execute(false, ''); | ||
$result = external_api::clean_returnvalue(raise_hand::execute_returns(), $result); | ||
$this->assertFalse($result['status']); | ||
} | ||
|
||
/** | ||
* Create a scenario to use into the tests. | ||
* | ||
* @return stdClass $scenario | ||
*/ | ||
protected function setup_scenario() { | ||
|
||
$course = $this->getDataGenerator()->create_course(); | ||
$student = $this->getDataGenerator()->create_and_enrol($course, 'student'); | ||
$contextcourse = \core\context\course::instance($course->id); | ||
|
||
$scenario = new stdClass(); | ||
$scenario->contextcourse = $contextcourse; | ||
$scenario->student = $student; | ||
|
||
/** @var \block_deft_generator $generator */ | ||
$generator = $this->getDataGenerator()->get_plugin_generator('block_deft'); | ||
|
||
$this->instance = $generator->create_instance(['parentcontextid' => $scenario->contextcourse->id]); | ||
$scenario->contextblock = context_block::instance($this->instance->id); | ||
$scenario->task = $generator->create_task($this->instance->id, [ | ||
'type' => 'venue', | ||
'visible' => 1, | ||
], [ | ||
'intro' => [ | ||
'text' => '', | ||
'format' => FORMAT_MOODLE, | ||
], | ||
'connection' => 'peer', | ||
'content' => '', | ||
'limit' => 10, | ||
'windowoption' => 'openinpopup', | ||
]); | ||
|
||
return $scenario; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters