From 52fe3c4683e3cda4591132c56b8d80bfbcf07a34 Mon Sep 17 00:00:00 2001 From: John Okely Date: Thu, 27 Jul 2017 15:29:53 +0800 Subject: [PATCH] MDL-57412 course: Unit test get_view_url for course formats --- .../topics/tests/format_topics_test.php | 37 ++++++++++++++++++ .../format/weeks/tests/format_weeks_test.php | 37 ++++++++++++++++++ course/tests/courseformat_test.php | 38 +++++++++++++++++++ 3 files changed, 112 insertions(+) diff --git a/course/format/topics/tests/format_topics_test.php b/course/format/topics/tests/format_topics_test.php index 527c44e81ae..796996005c0 100644 --- a/course/format/topics/tests/format_topics_test.php +++ b/course/format/topics/tests/format_topics_test.php @@ -224,4 +224,41 @@ class format_topics_testcase extends advanced_testcase { $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form())); } + + /** + * Test for get_view_url() to ensure that the url is only given for the correct cases + */ + public function test_get_view_url() { + global $CFG; + $this->resetAfterTest(); + + $linkcoursesections = $CFG->linkcoursesections; + + // Generate a course with two sections (0 and 1) and two modules. + $generator = $this->getDataGenerator(); + $course1 = $generator->create_course(array('format' => 'topics')); + course_create_sections_if_missing($course1, array(0, 1)); + + $data = (object)['id' => $course1->id]; + $format = course_get_format($course1); + $format->update_course_format_options($data); + + // In page. + $CFG->linkcoursesections = 0; + $this->assertNotEmpty($format->get_view_url(null)); + $this->assertNotEmpty($format->get_view_url(0)); + $this->assertNotEmpty($format->get_view_url(1)); + $CFG->linkcoursesections = 1; + $this->assertNotEmpty($format->get_view_url(null)); + $this->assertNotEmpty($format->get_view_url(0)); + $this->assertNotEmpty($format->get_view_url(1)); + + // Navigation. + $CFG->linkcoursesections = 0; + $this->assertNull($format->get_view_url(1, ['navigation' => 1])); + $this->assertNull($format->get_view_url(0, ['navigation' => 1])); + $CFG->linkcoursesections = 1; + $this->assertNotEmpty($format->get_view_url(1, ['navigation' => 1])); + $this->assertNotEmpty($format->get_view_url(0, ['navigation' => 1])); + } } diff --git a/course/format/weeks/tests/format_weeks_test.php b/course/format/weeks/tests/format_weeks_test.php index 32a25f6106b..522ff22549e 100644 --- a/course/format/weeks/tests/format_weeks_test.php +++ b/course/format/weeks/tests/format_weeks_test.php @@ -232,4 +232,41 @@ class format_weeks_testcase extends advanced_testcase { $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form())); } + /** + * Test for get_view_url() to ensure that the url is only given for the correct cases + */ + public function test_get_view_url() { + global $CFG; + $this->resetAfterTest(); + + $linkcoursesections = $CFG->linkcoursesections; + + // Generate a course with two sections (0 and 1) and two modules. + $generator = $this->getDataGenerator(); + $course1 = $generator->create_course(array('format' => 'weeks')); + course_create_sections_if_missing($course1, array(0, 1)); + + $data = (object)['id' => $course1->id]; + $format = course_get_format($course1); + $format->update_course_format_options($data); + + // In page. + $CFG->linkcoursesections = 0; + $this->assertNotEmpty($format->get_view_url(null)); + $this->assertNotEmpty($format->get_view_url(0)); + $this->assertNotEmpty($format->get_view_url(1)); + $CFG->linkcoursesections = 1; + $this->assertNotEmpty($format->get_view_url(null)); + $this->assertNotEmpty($format->get_view_url(0)); + $this->assertNotEmpty($format->get_view_url(1)); + + // Navigation. + $CFG->linkcoursesections = 0; + $this->assertNull($format->get_view_url(1, ['navigation' => 1])); + $this->assertNull($format->get_view_url(0, ['navigation' => 1])); + $CFG->linkcoursesections = 1; + $this->assertNotEmpty($format->get_view_url(1, ['navigation' => 1])); + $this->assertNotEmpty($format->get_view_url(0, ['navigation' => 1])); + } + } diff --git a/course/tests/courseformat_test.php b/course/tests/courseformat_test.php index 5f37025ff83..dd7b1a94dfe 100644 --- a/course/tests/courseformat_test.php +++ b/course/tests/courseformat_test.php @@ -162,6 +162,44 @@ class core_course_courseformat_testcase extends advanced_testcase { $format = course_get_format((object)['format' => 'testlegacy']); $this->assertTrue($format->supports_news()); } + + /** + * Test for get_view_url() to ensure that the url is only given for the correct cases + */ + public function test_get_view_url() { + global $CFG; + $this->resetAfterTest(); + + $linkcoursesections = $CFG->linkcoursesections; + + // Generate a course with two sections (0 and 1) and two modules. Course format is set to 'testformat'. + // This will allow us to test the default implementation of get_view_url. + $generator = $this->getDataGenerator(); + $course1 = $generator->create_course(array('format' => 'testformat')); + course_create_sections_if_missing($course1, array(0, 1)); + + $data = (object)['id' => $course1->id]; + $format = course_get_format($course1); + $format->update_course_format_options($data); + + // In page. + $CFG->linkcoursesections = 0; + $this->assertNotEmpty($format->get_view_url(null)); + $this->assertNotEmpty($format->get_view_url(0)); + $this->assertNotEmpty($format->get_view_url(1)); + $CFG->linkcoursesections = 1; + $this->assertNotEmpty($format->get_view_url(null)); + $this->assertNotEmpty($format->get_view_url(0)); + $this->assertNotEmpty($format->get_view_url(1)); + + // Navigation. + $CFG->linkcoursesections = 0; + $this->assertNull($format->get_view_url(1, ['navigation' => 1])); + $this->assertNull($format->get_view_url(0, ['navigation' => 1])); + $CFG->linkcoursesections = 1; + $this->assertNotEmpty($format->get_view_url(1, ['navigation' => 1])); + $this->assertNotEmpty($format->get_view_url(0, ['navigation' => 1])); + } } /**