Skip to content

Commit

Permalink
Merge branch 'MDL-49791-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Aug 4, 2015
2 parents b7fafb5 + 0918811 commit 673b3c7
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@
'core_user_add_user_private_files',
'mod_assign_view_grading_table',
'mod_scorm_view_scorm',
'mod_page_view_page',
),
'enabled' => 0,
'restrictedusers' => 0,
Expand Down
107 changes: 107 additions & 0 deletions mod/page/classes/external.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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/>.

/**
* Page external API
*
* @package mod_page
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/

defined('MOODLE_INTERNAL') || die;

require_once("$CFG->libdir/externallib.php");

/**
* Page external functions
*
* @package mod_page
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
class mod_page_external extends external_api {

/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function view_page_parameters() {
return new external_function_parameters(
array(
'pageid' => new external_value(PARAM_INT, 'page instance id')
)
);
}

/**
* Simulate the page/view.php web interface page: trigger events, completion, etc...
*
* @param int $pageid the page instance id
* @return array of warnings and status result
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function view_page($pageid) {
global $DB, $CFG;
require_once($CFG->dirroot . "/mod/page/lib.php");

$params = self::validate_parameters(self::view_page_parameters(),
array(
'pageid' => $pageid
));
$warnings = array();

// Request and permission validation.
$page = $DB->get_record('page', array('id' => $params['pageid']), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($page, 'page');

$context = context_module::instance($cm->id);
self::validate_context($context);

require_capability('mod/page:view', $context);

// Call the page/lib API.
page_view($page, $course, $cm, $context);

$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}

/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.0
*/
public static function view_page_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
)
);
}

}
39 changes: 39 additions & 0 deletions mod/page/db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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/>.

/**
* Page external functions and service definitions.
*
* @package mod_page
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/

defined('MOODLE_INTERNAL') || die;

$functions = array(

'mod_page_view_page' => array(
'classname' => 'mod_page_external',
'methodname' => 'view_page',
'description' => 'Simulate the view.php web interface page: trigger events, completion, etc...',
'type' => 'write',
'capabilities' => 'mod/page:view'
),

);
28 changes: 28 additions & 0 deletions mod/page/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,31 @@ function page_dndupload_handle($uploadinfo) {

return page_add_instance($data, null);
}

/**
* Mark the activity completed (if required) and trigger the course_module_viewed event.
*
* @param stdClass $page page object
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @since Moodle 3.0
*/
function page_view($page, $course, $cm, $context) {

// Trigger course_module_viewed event.
$params = array(
'context' => $context,
'objectid' => $page->id
);

$event = \mod_page\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('page', $page);
$event->trigger();

// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
}
111 changes: 111 additions & 0 deletions mod/page/tests/externallib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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/>.

/**
* External mod_page functions unit tests
*
* @package mod_page
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot . '/webservice/tests/helpers.php');

/**
* External mod_page functions unit tests
*
* @package mod_page
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
class mod_page_external_testcase extends externallib_advanced_testcase {

/**
* Test view_page
*/
public function test_view_page() {
global $DB;

$this->resetAfterTest(true);

// Setup test data.
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
$context = context_module::instance($page->cmid);
$cm = get_coursemodule_from_instance('page', $page->id);

// Test invalid instance id.
try {
mod_page_external::view_page(0);
$this->fail('Exception expected due to invalid mod_page instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}

// Test not-enrolled user.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
mod_page_external::view_page($page->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}

// Test user with full capabilities.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);

// Trigger and capture the event.
$sink = $this->redirectEvents();

$result = mod_page_external::view_page($page->id);
$result = external_api::clean_returnvalue(mod_page_external::view_page_returns(), $result);

$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_page\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodlepage = new \moodle_url('/mod/page/view.php', array('id' => $cm->id));
$this->assertEquals($moodlepage, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());

// Test user with no capabilities.
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
assign_capability('mod/page:view', CAP_PROHIBIT, $studentrole->id, $context->id);
accesslib_clear_all_caches_for_unit_testing();

try {
mod_page_external::view_page($page->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('nopermissions', $e->errorcode);
}

}
}
92 changes: 92 additions & 0 deletions mod/page/tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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/>.

/**
* Unit tests for mod_page lib
*
* @package mod_page
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/

defined('MOODLE_INTERNAL') || die();


/**
* Unit tests for mod_page lib
*
* @package mod_page
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
class mod_page_lib_testcase extends advanced_testcase {

/**
* Prepares things before this test case is initialised
* @return void
*/
public static function setUpBeforeClass() {
global $CFG;
require_once($CFG->dirroot . '/mod/page/lib.php');
}

/**
* Test page_view
* @return void
*/
public function test_page_view() {
global $CFG;

$CFG->enablecompletion = 1;
$this->resetAfterTest();

// Setup test data.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
array('completion' => 2, 'completionview' => 1));
$context = context_module::instance($page->cmid);
$cm = get_coursemodule_from_instance('page', $page->id);

// Trigger and capture the event.
$sink = $this->redirectEvents();

$this->setAdminUser();
page_view($page, $course, $cm, $context);

$events = $sink->get_events();
// 2 additional events thanks to completion.
$this->assertCount(3, $events);
$event = array_shift($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_page\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/page/view.php', array('id' => $cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());

// Check completion status.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);

}
}
Loading

0 comments on commit 673b3c7

Please sign in to comment.