From f5b4320ec50ef80f47b52a87e7d8a4dc238db9e5 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 8 Apr 2019 10:15:15 +0800 Subject: [PATCH] MDL-65032 mod_forum: Behat test for locking --- mod/forum/tests/behat/behat_mod_forum.php | 15 ++++++ mod/forum/tests/behat/discussion_lock.feature | 50 +++++++++++++++++++ .../entities_discussion_summary_test.php | 3 +- mod/forum/tests/entities_discussion_test.php | 3 +- mod/forum/tests/entities_forum_test.php | 3 +- mod/forum/tests/exporters_discussion_test.php | 3 +- mod/forum/tests/externallib_test.php | 48 ++++++++++++++++++ mod/forum/tests/generator/lib.php | 2 +- 8 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 mod/forum/tests/behat/discussion_lock.feature diff --git a/mod/forum/tests/behat/behat_mod_forum.php b/mod/forum/tests/behat/behat_mod_forum.php index 6ad16bfbb95..ecebde1279c 100644 --- a/mod/forum/tests/behat/behat_mod_forum.php +++ b/mod/forum/tests/behat/behat_mod_forum.php @@ -101,6 +101,21 @@ class behat_mod_forum extends behat_base { $this->execute('behat_forms::press_button', get_string('submit', 'core')); } + /** + * Navigates to a particular discussion page + * + * @Given /^I navigate to post "(?P(?:[^"]|\\")*)" in "(?P(?:[^"]|\\")*)" forum$/ + * @param string $postsubject The subject of the post + * @param string $forumname The forum name + */ + public function i_navigate_to_post_in_forum($postsubject, $forumname) { + + // Navigate to forum discussion. + $this->execute('behat_general::click_link', $this->escape($forumname)); + $this->execute('behat_general::click_link', $this->escape($postsubject)); + } + + /** * Returns the steps list to add a new discussion to a forum. * diff --git a/mod/forum/tests/behat/discussion_lock.feature b/mod/forum/tests/behat/discussion_lock.feature new file mode 100644 index 00000000000..3c33f8c7bd1 --- /dev/null +++ b/mod/forum/tests/behat/discussion_lock.feature @@ -0,0 +1,50 @@ +@mod @mod_forum @javascript +Feature: As a teacher, you can manually lock individual discussions when viewing the discussion + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | student1 | C1 | student | + And I log in as "admin" + And I am on "Course 1" course homepage with editing mode on + And I add a "Forum" to section "1" and I fill the form with: + | Forum name | Test forum name | + | Description | Test forum description | + And I add a new discussion to "Test forum name" forum with: + | Subject | Discussion 1 | + | Message | Discussion contents 1, first message | + And I reply "Discussion 1" post from "Test forum name" forum with: + | Subject | Reply 1 to discussion 1 | + | Message | Discussion contents 1, second message | + And I add a new discussion to "Test forum name" forum with: + | Subject | Discussion 2 | + | Message | Discussion contents 2, first message | + And I reply "Discussion 2" post from "Test forum name" forum with: + | Subject | Reply 1 to discussion 2 | + | Message | Discussion contents 2, second message | + And I log out + + Scenario: Lock a discussion and view + Given I log in as "admin" + And I am on "Course 1" course homepage + And I navigate to post "Discussion 1" in "Test forum name" forum + Then "Lock" "link" should be visible + And I follow "Lock" + Then "a[@title='Lock']" "css_element" should not be visible + Then "Locked" "link" should be visible + And I reload the page + Then I should see "This discussion has been locked so you can no longer reply to it." + And I follow "Discussion 2" + Then I should not see "This discussion has been locked so you can no longer reply to it." + And I log out + And I log in as "student1" + And I am on "Course 1" course homepage + And I navigate to post "Discussion 1" in "Test forum name" forum + Then I should see "This discussion has been locked so you can no longer reply to it." + And "Reply" "link" should not be visible diff --git a/mod/forum/tests/entities_discussion_summary_test.php b/mod/forum/tests/entities_discussion_summary_test.php index 1cb03f67b88..5674237829a 100644 --- a/mod/forum/tests/entities_discussion_summary_test.php +++ b/mod/forum/tests/entities_discussion_summary_test.php @@ -72,7 +72,8 @@ class mod_forum_entities_discussion_summary_testcase extends advanced_testcase { time(), 0, 0, - false + false, + 0 ); $firstpost = new post_entity( 1, diff --git a/mod/forum/tests/entities_discussion_test.php b/mod/forum/tests/entities_discussion_test.php index 356aae2f9d6..9c01ac0ab2d 100644 --- a/mod/forum/tests/entities_discussion_test.php +++ b/mod/forum/tests/entities_discussion_test.php @@ -56,7 +56,8 @@ class mod_forum_entities_discussion_testcase extends advanced_testcase { $time, 0, 0, - false + false, + 0 ); $firstpost = new post_entity( 4, diff --git a/mod/forum/tests/entities_forum_test.php b/mod/forum/tests/entities_forum_test.php index 0bbfb62efaf..829d29f6263 100644 --- a/mod/forum/tests/entities_forum_test.php +++ b/mod/forum/tests/entities_forum_test.php @@ -58,7 +58,8 @@ class mod_forum_entities_forum_testcase extends advanced_testcase { $time, 0, 0, - false + false, + 0 ); $past = time() - 100; diff --git a/mod/forum/tests/exporters_discussion_test.php b/mod/forum/tests/exporters_discussion_test.php index 7eb28b6e5e8..7290395fc85 100644 --- a/mod/forum/tests/exporters_discussion_test.php +++ b/mod/forum/tests/exporters_discussion_test.php @@ -87,7 +87,8 @@ class mod_forum_exporters_discussion_testcase extends advanced_testcase { $now, 0, 0, - false + false, + 0 ); $exporter = new discussion_exporter($discussion, [ diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 96a4eebf690..7db80130d7d 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -1422,6 +1422,54 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { } + /* + * Test set_lock_state. + */ + public function test_set_lock_state() { + global $DB; + $this->resetAfterTest(true); + + // Create courses to add the modules. + $course = self::getDataGenerator()->create_course(); + $user = self::getDataGenerator()->create_user(); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + + // First forum with tracking off. + $record = new stdClass(); + $record->course = $course->id; + $record->type = 'news'; + $forum = self::getDataGenerator()->create_module('forum', $record); + + $record = new stdClass(); + $record->course = $course->id; + $record->userid = $user->id; + $record->forum = $forum->id; + $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); + + // User who is a student. + self::setUser($user); + $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id, 'manual'); + + // Only a teacher should be able to lock a discussion. + $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0); + $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result); + $this->assertFalse($result['userstate']['locked']); + $this->assertEquals('0', $result['times']['locked']); + + // Set the lock. + self::setAdminUser(); + $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0); + $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result); + $this->assertTrue($result['userstate']['locked']); + $this->assertNotEquals(0, $result['times']['locked']); + + // Unset the lock. + $result = mod_forum_external::set_lock_state($forum->id, $discussion->id, time()); + $result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result); + $this->assertFalse($result['userstate']['locked']); + $this->assertEquals('0', $result['times']['locked']); + } + /* * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests. */ diff --git a/mod/forum/tests/generator/lib.php b/mod/forum/tests/generator/lib.php index 4550c106001..5cae1678363 100644 --- a/mod/forum/tests/generator/lib.php +++ b/mod/forum/tests/generator/lib.php @@ -194,7 +194,7 @@ class mod_forum_generator extends testing_module_generator { } if (!isset($record['locked'])) { - $record['locked'] = "0"; + $record['locked'] = 0; } if (isset($record['mailed'])) { -- 2.43.0