MDL-372 forum: tests for pinned discussions

This commit is contained in:
Ryan Wyllie 2016-01-07 08:09:57 +00:00
parent 5f219cf181
commit f9621c56c4
8 changed files with 291 additions and 26 deletions

View File

@ -39,11 +39,9 @@ Feature: A user can navigate to previous and next discussions
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 1 |
| Message | Test post message |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 2 |
| Message | Test post message |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 3 |
| Message | Test post message |
@ -56,7 +54,6 @@ Feature: A user can navigate to previous and next discussions
And I follow "Discussion 1"
And I should see "Discussion 2"
And I should not see "Discussion 3"
And I wait "1" seconds
And I follow "Reply"
And I set the following fields to these values:
| Message | Answer to discussion |
@ -81,26 +78,21 @@ Feature: A user can navigate to previous and next discussions
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 1 Group 0 |
| Message | Test post message |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 2 Group 0 |
| Message | Test post message |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 1 Group 1 |
| Message | Test post message |
| Group | Group 1 |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 2 Group 1 |
| Message | Test post message |
| Group | Group 1 |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 1 Group 2 |
| Message | Test post message |
| Group | Group 2 |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 2 Group 2 |
| Message | Test post message |
@ -143,26 +135,21 @@ Feature: A user can navigate to previous and next discussions
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 1 Group 0 |
| Message | Test post message |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 2 Group 0 |
| Message | Test post message |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 1 Group 1 |
| Message | Test post message |
| Group | Group 1 |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 2 Group 1 |
| Message | Test post message |
| Group | Group 1 |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 1 Group 2 |
| Message | Test post message |
| Group | Group 2 |
And I wait "1" seconds
And I add a new discussion to "Test forum name" forum with:
| Subject | Discussion 2 Group 2 |
| Message | Test post message |

View File

@ -28,7 +28,6 @@ Feature: A teacher can move discussions between forums
And I add a new discussion to "Test forum 1" forum with:
| Subject | Discussion 1 |
| Message | Test post message |
And I wait "1" seconds
And I log out
And I log in as "teacher1"
And I follow "Course 1"

View File

@ -41,11 +41,9 @@ Feature: Blog posts are always displayed in reverse chronological order
When I add a new topic to "Course blog forum" forum with:
| Subject | Blog post 1 |
| Message | This is the first post |
And I wait "1" seconds
And I add a new topic to "Course blog forum" forum with:
| Subject | Blog post 2 |
| Message | This is the second post |
And I wait "1" seconds
And I add a new topic to "Course blog forum" forum with:
| Subject | Blog post 3 |
| Message | This is the third post |

View File

@ -41,11 +41,9 @@ Feature: New discussions and discussions with recently added replies are display
When I add a new discussion to "Course general forum" forum with:
| Subject | Forum post 1 |
| Message | This is the first post |
And I wait "1" seconds
And I add a new discussion to "Course general forum" forum with:
| Subject | Forum post 2 |
| Message | This is the second post |
And I wait "1" seconds
And I add a new discussion to "Course general forum" forum with:
| Subject | Forum post 3 |
| Message | This is the third post |

View File

@ -696,7 +696,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
'usermodifiedpictureurl' => '',
'numreplies' => 3,
'numunread' => 0,
'pinned' => 0
'pinned' => FORUM_DISCUSSION_UNPINNED
);
// Call the external function passing forum id.

View File

@ -190,7 +190,7 @@ class mod_forum_generator extends testing_module_generator {
}
if (!isset($record['pinned'])) {
$record['pinned'] = "0";
$record['pinned'] = FORUM_DISCUSSION_UNPINNED;
}
$record = (object) $record;

View File

@ -108,9 +108,9 @@ class mod_forum_generator_testcase extends advanced_testcase {
$record['course'] = $course->id;
$record['forum'] = $forum->id;
$record['userid'] = $user->id;
$record['pinned'] = 1; // Pin one discussion.
$record['pinned'] = FORUM_DISCUSSION_PINNED; // Pin one discussion.
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
$record['pinned'] = 0; // No pin for others.
$record['pinned'] = FORUM_DISCUSSION_UNPINNED; // No pin for others.
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

View File

@ -1010,7 +1010,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
$DB->update_record('forum_discussions', (object) array('id' => $disc2->id, 'timemodified' => $record->timemodified - 1));
// Test pinned posts behave correctly.
$disc8->pinned = 1;
$disc8->pinned = FORUM_DISCUSSION_PINNED;
$DB->update_record('forum_discussions', (object) array('id' => $disc8->id, 'pinned' => $disc8->pinned));
$neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
$this->assertEquals($disc3->id, $neighbours['prev']->id);
@ -1021,9 +1021,9 @@ class mod_forum_lib_testcase extends advanced_testcase {
$this->assertEquals($disc8->id, $neighbours['next']->id);
// Test 3 pinned posts.
$disc6->pinned = 1;
$disc6->pinned = FORUM_DISCUSSION_PINNED;
$DB->update_record('forum_discussions', (object) array('id' => $disc6->id, 'pinned' => $disc6->pinned));
$disc4->pinned = 1;
$disc4->pinned = FORUM_DISCUSSION_PINNED;
$DB->update_record('forum_discussions', (object) array('id' => $disc4->id, 'pinned' => $disc4->pinned));
$neighbours = forum_get_discussion_neighbours($cm, $disc6, $forum);
@ -1875,7 +1875,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
$record->course = $forum->course;
$record->forum = $forum->id;
$record->userid = $user->id;
$record->pinned = 1;
$record->pinned = FORUM_DISCUSSION_PINNED;
$discussion = $generator->create_discussion($record);
// Retrieve the first post.
@ -2549,4 +2549,287 @@ class mod_forum_lib_testcase extends advanced_testcase {
),
);
}
/**
* Test test_pinned_discussion_with_group.
*/
public function test_pinned_discussion_with_group() {
global $SESSION;
$this->resetAfterTest();
$course1 = $this->getDataGenerator()->create_course();
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
// Create an author user.
$author = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($author->id, $course1->id);
// Create two viewer users - one in a group, one not.
$viewer1 = $this->getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer1->id, $course1->id);
$viewer2 = $this->getDataGenerator()->create_user((object) array('trackforums' => 1));
$this->getDataGenerator()->enrol_user($viewer2->id, $course1->id);
$this->getDataGenerator()->create_group_member(array('userid' => $viewer2->id, 'groupid' => $group1->id));
$forum1 = $this->getDataGenerator()->create_module('forum', (object) array(
'course' => $course1->id,
'groupmode' => SEPARATEGROUPS,
));
$coursemodule = get_coursemodule_from_instance('forum', $forum1->id);
$alldiscussions = array();
$group1discussions = array();
// Create 4 discussions in all participants group and group1, where the first
// discussion is pinned in each group.
$allrecord = new stdClass();
$allrecord->course = $course1->id;
$allrecord->userid = $author->id;
$allrecord->forum = $forum1->id;
$allrecord->pinned = FORUM_DISCUSSION_PINNED;
$group1record = new stdClass();
$group1record->course = $course1->id;
$group1record->userid = $author->id;
$group1record->forum = $forum1->id;
$group1record->groupid = $group1->id;
$group1record->pinned = FORUM_DISCUSSION_PINNED;
$alldiscussions[] = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($allrecord);
$group1discussions[] = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($group1record);
// Create unpinned discussions.
$allrecord->pinned = FORUM_DISCUSSION_UNPINNED;
$group1record->pinned = FORUM_DISCUSSION_UNPINNED;
for ($i = 0; $i < 3; $i++) {
$alldiscussions[] = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($allrecord);
$group1discussions[] = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($group1record);
}
// As viewer1 (no group). This user shouldn't see any of group1's discussions
// so their expected discussion order is (where rightmost is highest priority):
// Ad1, ad2, ad3, ad0.
$this->setUser($viewer1->id);
// CHECK 1.
// Take the neighbours of ad3, which should be prev: ad2 and next: ad0.
$neighbours = forum_get_discussion_neighbours($coursemodule, $alldiscussions[3], $forum1);
// Ad2 check.
$this->assertEquals($alldiscussions[2]->id, $neighbours['prev']->id);
// Ad0 check.
$this->assertEquals($alldiscussions[0]->id, $neighbours['next']->id);
// CHECK 2.
// Take the neighbours of ad0, which should be prev: ad3 and next: null.
$neighbours = forum_get_discussion_neighbours($coursemodule, $alldiscussions[0], $forum1);
// Ad3 check.
$this->assertEquals($alldiscussions[3]->id, $neighbours['prev']->id);
// Null check.
$this->assertEmpty($neighbours['next']);
// CHECK 3.
// Take the neighbours of ad1, which should be prev: null and next: ad2.
$neighbours = forum_get_discussion_neighbours($coursemodule, $alldiscussions[1], $forum1);
// Null check.
$this->assertEmpty($neighbours['prev']);
// Ad2 check.
$this->assertEquals($alldiscussions[2]->id, $neighbours['next']->id);
// Temporary hack to workaround for MDL-52656.
$SESSION->currentgroup = null;
// As viewer2 (group1). This user should see all of group1's posts and the all participants group.
// The expected discussion order is (rightmost is highest priority):
// Ad1, gd1, ad2, gd2, ad3, gd3, ad0, gd0.
$this->setUser($viewer2->id);
// CHECK 1.
// Take the neighbours of ad1, which should be prev: null and next: gd1.
$neighbours = forum_get_discussion_neighbours($coursemodule, $alldiscussions[1], $forum1);
// Null check.
$this->assertEmpty($neighbours['prev']);
// Gd1 check.
$this->assertEquals($group1discussions[1]->id, $neighbours['next']->id);
// CHECK 2.
// Take the neighbours of ad3, which should be prev: gd2 and next: gd3.
$neighbours = forum_get_discussion_neighbours($coursemodule, $alldiscussions[3], $forum1);
// Gd2 check.
$this->assertEquals($group1discussions[2]->id, $neighbours['prev']->id);
// Gd3 check.
$this->assertEquals($group1discussions[3]->id, $neighbours['next']->id);
// CHECK 3.
// Take the neighbours of gd3, which should be prev: ad3 and next: ad0.
$neighbours = forum_get_discussion_neighbours($coursemodule, $group1discussions[3], $forum1);
// Ad3 check.
$this->assertEquals($alldiscussions[3]->id, $neighbours['prev']->id);
// Ad0 check.
$this->assertEquals($alldiscussions[0]->id, $neighbours['next']->id);
// CHECK 4.
// Take the neighbours of gd0, which should be prev: ad0 and next: null.
$neighbours = forum_get_discussion_neighbours($coursemodule, $group1discussions[0], $forum1);
// Ad0 check.
$this->assertEquals($alldiscussions[0]->id, $neighbours['prev']->id);
// Null check.
$this->assertEmpty($neighbours['next']);
}
/**
* Test test_pinned_with_timed_discussions.
*/
public function test_pinned_with_timed_discussions() {
global $CFG;
$CFG->forum_enabletimedposts = true;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
// Create an user.
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
// Create a forum.
$record = new stdClass();
$record->course = $course->id;
$forum = $this->getDataGenerator()->create_module('forum', (object) array(
'course' => $course->id,
'groupmode' => SEPARATEGROUPS,
));
$coursemodule = get_coursemodule_from_instance('forum', $forum->id);
$now = time();
$discussions = array();
$discussiongenerator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
$record = new stdClass();
$record->course = $course->id;
$record->userid = $user->id;
$record->forum = $forum->id;
$record->pinned = FORUM_DISCUSSION_PINNED;
$record->timemodified = $now;
$discussions[] = $discussiongenerator->create_discussion($record);
$record->pinned = FORUM_DISCUSSION_UNPINNED;
$record->timestart = $now + 10;
$discussions[] = $discussiongenerator->create_discussion($record);
$record->timestart = $now;
$discussions[] = $discussiongenerator->create_discussion($record);
// Expected order of discussions:
// D2, d1, d0.
$this->setUser($user->id);
// CHECK 1.
$neighbours = forum_get_discussion_neighbours($coursemodule, $discussions[2], $forum);
// Null check.
$this->assertEmpty($neighbours['prev']);
// D1 check.
$this->assertEquals($discussions[1]->id, $neighbours['next']->id);
// CHECK 2.
$neighbours = forum_get_discussion_neighbours($coursemodule, $discussions[1], $forum);
// D2 check.
$this->assertEquals($discussions[2]->id, $neighbours['prev']->id);
// D0 check.
$this->assertEquals($discussions[0]->id, $neighbours['next']->id);
// CHECK 3.
$neighbours = forum_get_discussion_neighbours($coursemodule, $discussions[0], $forum);
// D2 check.
$this->assertEquals($discussions[1]->id, $neighbours['prev']->id);
// Null check.
$this->assertEmpty($neighbours['next']);
}
/**
* Test test_pinned_timed_discussions_with_timed_discussions.
*/
public function test_pinned_timed_discussions_with_timed_discussions() {
global $CFG;
$CFG->forum_enabletimedposts = true;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
// Create an user.
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
// Create a forum.
$record = new stdClass();
$record->course = $course->id;
$forum = $this->getDataGenerator()->create_module('forum', (object) array(
'course' => $course->id,
'groupmode' => SEPARATEGROUPS,
));
$coursemodule = get_coursemodule_from_instance('forum', $forum->id);
$now = time();
$discussions = array();
$discussiongenerator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
$record = new stdClass();
$record->course = $course->id;
$record->userid = $user->id;
$record->forum = $forum->id;
$record->pinned = FORUM_DISCUSSION_PINNED;
$record->timemodified = $now;
$record->timestart = $now + 10;
$discussions[] = $discussiongenerator->create_discussion($record);
$record->pinned = FORUM_DISCUSSION_UNPINNED;
$discussions[] = $discussiongenerator->create_discussion($record);
$record->timestart = $now;
$discussions[] = $discussiongenerator->create_discussion($record);
$record->pinned = FORUM_DISCUSSION_PINNED;
$discussions[] = $discussiongenerator->create_discussion($record);
// Expected order of discussions:
// D2, d1, d3, d0.
$this->setUser($user->id);
// CHECK 1.
$neighbours = forum_get_discussion_neighbours($coursemodule, $discussions[2], $forum);
// Null check.
$this->assertEmpty($neighbours['prev']);
// D1 check.
$this->assertEquals($discussions[1]->id, $neighbours['next']->id);
// CHECK 2.
$neighbours = forum_get_discussion_neighbours($coursemodule, $discussions[1], $forum);
// D2 check.
$this->assertEquals($discussions[2]->id, $neighbours['prev']->id);
// D3 check.
$this->assertEquals($discussions[3]->id, $neighbours['next']->id);
// CHECK 3.
$neighbours = forum_get_discussion_neighbours($coursemodule, $discussions[3], $forum);
// D1 check.
$this->assertEquals($discussions[1]->id, $neighbours['prev']->id);
// D0 check.
$this->assertEquals($discussions[0]->id, $neighbours['next']->id);
// CHECK 4.
$neighbours = forum_get_discussion_neighbours($coursemodule, $discussions[0], $forum);
// D3 check.
$this->assertEquals($discussions[3]->id, $neighbours['prev']->id);
// Null check.
$this->assertEmpty($neighbours['next']);
}
}