diff --git a/mod/forum/classes/local/entities/discussion.php b/mod/forum/classes/local/entities/discussion.php index 68c3b1d58e8..bbc21edb208 100644 --- a/mod/forum/classes/local/entities/discussion.php +++ b/mod/forum/classes/local/entities/discussion.php @@ -239,7 +239,7 @@ class discussion { } /** - * Check if the discussion has started yet. + * Check if the discussion has started yet. DEFAULTS: true if not set * * @return bool */ @@ -249,13 +249,13 @@ class discussion { } /** - * Check if the discussion has ended. + * Check if the discussion has ended. DEFAULTS: false if not set * * @return bool */ public function has_ended() : bool { $endtime = $this->get_time_end(); - return !empty($endtime) && $endtime >= time(); + return !empty($endtime) && $endtime < time(); } /** diff --git a/mod/forum/tests/entities_discussion_test.php b/mod/forum/tests/entities_discussion_test.php index 20570b0f378..1c663faeb5c 100644 --- a/mod/forum/tests/entities_discussion_test.php +++ b/mod/forum/tests/entities_discussion_test.php @@ -113,4 +113,66 @@ class mod_forum_entities_discussion_testcase extends advanced_testcase { $this->assertEquals(true, $discussion->has_started()); $this->assertEquals(true, $discussion->has_group()); } -} + + /** + * Test the display period settings for discussions. + * This covers each individual date function as well as the combination of the 2. + * + * @dataProvider test_diplay_period_options_provider + * @param string $testdescription A basic description of the base assertions. + * @param int $basetime + * @param int $timestart + * @param int $timeend + * @param bool $timestartresult Expected result from the has_started function + * @param bool $timeendresult Expected result from the has_ended function + * @param bool $isvisible Expected result from the is_timed_discussion_visible function + */ + public function test_display_period_settings($testdescription, $basetime, $timestart, $timeend, + $timestartresult, $timeendresult, $isvisible) { + global $CFG; + $this->resetAfterTest(); + + $discussion = new discussion_entity( + 1, + 2, + 3, + 'test discussion', + 4, + 5, + 6, + false, + $basetime, + $basetime, + $timestart, + $timeend, + false + ); + $originaltimedposts = $CFG->forum_enabletimedposts; + $CFG->forum_enabletimedposts = true; + + $this->assertEquals($timestartresult, $discussion->has_started(), $testdescription); + $this->assertEquals($timeendresult, $discussion->has_ended(), $testdescription); + $this->assertEquals($isvisible, $discussion->is_timed_discussion_visible(), $testdescription); + + $CFG->forum_enabletimedposts = $originaltimedposts; + } + + /** + * Data provider for test_display_period_settings(). + * + * @return array base,start, endtimes and the expected results. + */ + public function test_diplay_period_options_provider() { + $base = time() + 10; + return array( + ["No dates set set", $base, 0, 0, true, false, true], + ["Only started date in the future", $base, $base + 100, 0, false, false, false], + ["Only started date in the past", $base, $base - 100, 0, true, false, true], + ["Only end date in the future", $base, 0, $base + 100, true, false, true], + ["Only end date in the past", $base, 0, $base - 100, true, true, false], + ["Start date in the past, end date in the future", $base, $base - 100, $base + 100, true, false, true], + ["Both dates in the past", $base, $base - 100, $base - 50, true, true, false], + ["Both dates in the future", $base, $base + 100, $base + 150, false, false, false], + ); + } +} \ No newline at end of file