MDL-57412 course: Unit test get_view_url for course formats

This commit is contained in:
John Okely 2017-07-27 15:29:53 +08:00
parent 1832b68142
commit 52fe3c4683
3 changed files with 112 additions and 0 deletions

View File

@ -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]));
}
}

View File

@ -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]));
}
}

View File

@ -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]));
}
}
/**