diff --git a/mod/wiki/locallib.php b/mod/wiki/locallib.php index e30646a61e7..366c1dd3b86 100644 --- a/mod/wiki/locallib.php +++ b/mod/wiki/locallib.php @@ -292,11 +292,27 @@ function wiki_refresh_cachedcontent($page, $newcontent = null) { return array('page' => $page, 'sections' => $parseroutput['repeated_sections'], 'version' => $version->version); } + /** - * Restore a page + * Restore a page with specified version. + * + * @param stdClass $wikipage wiki page record + * @param stdClass $version wiki page version to restore + * @param context_module $context context of wiki module + * @return stdClass restored page */ -function wiki_restore_page($wikipage, $newcontent, $userid) { - $return = wiki_save_page($wikipage, $newcontent, $userid); +function wiki_restore_page($wikipage, $version, $context) { + $return = wiki_save_page($wikipage, $version->content, $version->userid); + $event = \mod_wiki\event\page_version_restored::create( + array( + 'context' => $context, + 'objectid' => $version->id, + 'other' => array( + 'pageid' => $wikipage->id + ) + )); + $event->add_record_snapshot('wiki_versions', $version); + $event->trigger(); return $return['page']; } diff --git a/mod/wiki/pagelib.php b/mod/wiki/pagelib.php index 7309326cc60..867b8aff877 100644 --- a/mod/wiki/pagelib.php +++ b/mod/wiki/pagelib.php @@ -2159,17 +2159,7 @@ class page_wiki_confirmrestore extends page_wiki_save { require_capability('mod/wiki:managewiki', $this->modcontext, NULL, true, 'nomanagewikipermission', 'wiki'); $version = wiki_get_version($this->version->id); - if (wiki_restore_page($this->page, $version->content, $version->userid)) { - $event = \mod_wiki\event\page_version_restored::create( - array( - 'context' => $this->modcontext, - 'objectid' => $version->id, - 'other' => array( - 'pageid' => $this->page->id - ) - )); - $event->add_record_snapshot('wiki_versions', $version); - $event->trigger(); + if (wiki_restore_page($this->page, $version, $this->modcontext)) { redirect($CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $this->page->id, get_string('restoring', 'wiki', $version->version), 3); } else { print_error('restoreerror', 'wiki', $version->version); diff --git a/mod/wiki/tests/events_test.php b/mod/wiki/tests/events_test.php index 45cb154c47d..2534f402c2f 100644 --- a/mod/wiki/tests/events_test.php +++ b/mod/wiki/tests/events_test.php @@ -147,7 +147,7 @@ class mod_wiki_events_testcase extends advanced_testcase { /** * Test instances_list_viewed event. */ - public function test_instances_list_viewed() { + public function test_course_module_instance_list_viewed() { // There is no proper API to call or trigger this event, so simulating event // to check if event returns the right information. @@ -155,7 +155,7 @@ class mod_wiki_events_testcase extends advanced_testcase { $context = context_course::instance($this->course->id); $params = array('context' => $context); - $event = \mod_wiki\event\instances_list_viewed::create($params); + $event = \mod_wiki\event\course_module_instance_list_viewed::create($params); // Triggering and capturing the event. $sink = $this->redirectEvents(); @@ -165,7 +165,7 @@ class mod_wiki_events_testcase extends advanced_testcase { $event = reset($events); // Checking that the event contains the expected values. - $this->assertInstanceOf('\mod_wiki\event\instances_list_viewed', $event); + $this->assertInstanceOf('\mod_wiki\event\course_module_instance_list_viewed', $event); $this->assertEquals($context, $event->get_context()); $expected = array($this->course->id, 'wiki', 'view all', 'index.php?id=' . $this->course->id, ''); $this->assertEventLegacyLogData($expected, $event); @@ -534,29 +534,18 @@ class mod_wiki_events_testcase extends advanced_testcase { * Test page_version_restored event. */ public function test_page_version_restored() { - // There is no proper API to call or trigger this event, so simulating event - // to check if event returns the right information. - $this->setUp(); $page = $this->wikigenerator->create_first_page($this->wiki); $context = context_module::instance($this->wiki->cmid); $version = wiki_get_version(1); - $params = array( - 'context' => $context, - 'objectid' => $version->id, - 'other' => array( - 'pageid' => $page->id - ) - ); - $event = \mod_wiki\event\page_version_restored::create($params); // Triggering and capturing the event. $sink = $this->redirectEvents(); - $event->trigger(); + wiki_restore_page($page, $version, $context); $events = $sink->get_events(); - $this->assertCount(1, $events); - $event = reset($events); + $this->assertCount(2, $events); + $event = array_pop($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\mod_wiki\event\page_version_restored', $event);