From c1baf89e5734e3575cf501505798834eb5b01e91 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 23 Oct 2015 11:25:47 +0200 Subject: [PATCH] MDL-51886 wiki: Add unit tests for view_wiki and view_page --- mod/wiki/tests/externallib_test.php | 117 ++++++++++++++++++++++++ mod/wiki/tests/lib_test.php | 132 ++++++++++++++++++++++++++++ 2 files changed, 249 insertions(+) create mode 100644 mod/wiki/tests/lib_test.php diff --git a/mod/wiki/tests/externallib_test.php b/mod/wiki/tests/externallib_test.php index aa929590ac9..978a8410f7d 100644 --- a/mod/wiki/tests/externallib_test.php +++ b/mod/wiki/tests/externallib_test.php @@ -196,6 +196,123 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase { $wikis = mod_wiki_external::get_wikis_by_courses(array($this->course->id)); $wikis = external_api::clean_returnvalue(mod_wiki_external::get_wikis_by_courses_returns(), $wikis); $this->assertFalse($wikis['wikis'][0]['cancreatepages']); + + } + + /** + * Test view_wiki. + */ + public function test_view_wiki() { + + // Test invalid instance id. + try { + mod_wiki_external::view_wiki(0); + $this->fail('Exception expected due to invalid mod_wiki instance id.'); + } catch (moodle_exception $e) { + $this->assertEquals('incorrectwikiid', $e->errorcode); + } + + // Test not-enrolled user. + $usernotenrolled = self::getDataGenerator()->create_user(); + $this->setUser($usernotenrolled); + try { + mod_wiki_external::view_wiki($this->wiki->id); + $this->fail('Exception expected due to not enrolled user.'); + } catch (moodle_exception $e) { + $this->assertEquals('requireloginerror', $e->errorcode); + } + + // Test user with full capabilities. + $this->setUser($this->student); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $result = mod_wiki_external::view_wiki($this->wiki->id); + $result = external_api::clean_returnvalue(mod_wiki_external::view_wiki_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_wiki\event\course_module_viewed', $event); + $this->assertEquals($this->context, $event->get_context()); + $moodlewiki = new \moodle_url('/mod/wiki/view.php', array('id' => $this->cm->id)); + $this->assertEquals($moodlewiki, $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 allowed for students by default. + assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); + accesslib_clear_all_caches_for_unit_testing(); + + try { + mod_wiki_external::view_wiki($this->wiki->id); + $this->fail('Exception expected due to missing capability.'); + } catch (moodle_exception $e) { + $this->assertEquals('cannotviewpage', $e->errorcode); + } + + } + + /** + * Test view_page. + */ + public function test_view_page() { + + // Test invalid page id. + try { + mod_wiki_external::view_page(0); + $this->fail('Exception expected due to invalid view_page page id.'); + } catch (moodle_exception $e) { + $this->assertEquals('incorrectpageid', $e->errorcode); + } + + // Test not-enrolled user. + $usernotenrolled = self::getDataGenerator()->create_user(); + $this->setUser($usernotenrolled); + try { + mod_wiki_external::view_page($this->firstpage->id); + $this->fail('Exception expected due to not enrolled user.'); + } catch (moodle_exception $e) { + $this->assertEquals('requireloginerror', $e->errorcode); + } + + // Test user with full capabilities. + $this->setUser($this->student); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $result = mod_wiki_external::view_page($this->firstpage->id); + $result = external_api::clean_returnvalue(mod_wiki_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_wiki\event\page_viewed', $event); + $this->assertEquals($this->context, $event->get_context()); + $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $this->firstpage->id)); + $this->assertEquals($pageurl, $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 allowed for students by default. + assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); + accesslib_clear_all_caches_for_unit_testing(); + + try { + mod_wiki_external::view_page($this->firstpage->id); + $this->fail('Exception expected due to missing capability.'); + } catch (moodle_exception $e) { + $this->assertEquals('cannotviewpage', $e->errorcode); + } + } } diff --git a/mod/wiki/tests/lib_test.php b/mod/wiki/tests/lib_test.php new file mode 100644 index 00000000000..941c096092d --- /dev/null +++ b/mod/wiki/tests/lib_test.php @@ -0,0 +1,132 @@ +. + +/** + * Unit tests for mod_wiki lib + * + * @package mod_wiki + * @category external + * @copyright 2015 Dani Palou + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.1 + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/mod/wiki/lib.php'); +require_once($CFG->libdir . '/completionlib.php'); + +/** + * Unit tests for mod_wiki lib + * + * @package mod_wiki + * @category external + * @copyright 2015 Dani Palou + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.1 + */ +class mod_wiki_lib_testcase extends advanced_testcase { + + /** + * Test wiki_view. + * + * @return void + */ + public function test_wiki_view() { + global $CFG; + + $CFG->enablecompletion = COMPLETION_ENABLED; + $this->resetAfterTest(); + + $this->setAdminUser(); + // Setup test data. + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => COMPLETION_ENABLED)); + $options = array('completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => COMPLETION_VIEW_REQUIRED); + $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id), $options); + $context = context_module::instance($wiki->cmid); + $cm = get_coursemodule_from_instance('wiki', $wiki->id); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + wiki_view($wiki, $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_wiki\event\course_module_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $moodleurl = new \moodle_url('/mod/wiki/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); + + } + + /** + * Test wiki_page_view. + * + * @return void + */ + public function test_wiki_page_view() { + global $CFG; + + $CFG->enablecompletion = COMPLETION_ENABLED; + $this->resetAfterTest(); + + $this->setAdminUser(); + // Setup test data. + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => COMPLETION_ENABLED)); + $options = array('completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => COMPLETION_VIEW_REQUIRED); + $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id), $options); + $context = context_module::instance($wiki->cmid); + $cm = get_coursemodule_from_instance('wiki', $wiki->id); + $firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($wiki); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + wiki_page_view($wiki, $firstpage, $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_wiki\event\page_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $firstpage->id)); + $this->assertEquals($pageurl, $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); + + } +} -- 2.17.1