Skip to content

Commit

Permalink
MDL-57412 course: Unit test get_view_url for course formats
Browse files Browse the repository at this point in the history
  • Loading branch information
xow committed Aug 21, 2017
1 parent 1832b68 commit 52fe3c4
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
37 changes: 37 additions & 0 deletions course/format/topics/tests/format_topics_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,41 @@ public function test_default_course_enddate() {
$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]));
}
}
37 changes: 37 additions & 0 deletions course/format/weeks/tests/format_weeks_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,41 @@ public function test_default_course_enddate() {
$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]));
}

}
38 changes: 38 additions & 0 deletions course/tests/courseformat_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,44 @@ public function test_supports_news_legacy() {
$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]));
}
}

/**
Expand Down

0 comments on commit 52fe3c4

Please sign in to comment.