Skip to content

Commit f77babb

Browse files
committed
Add Behat test that covers review functionality
1 parent c267358 commit f77babb

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

tests/behat/behat_mod_moodleoverflow.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Behat\Mink\Exception\ExpectationException;
3333
use mod_moodleoverflow\post\post_control;
3434
use mod_moodleoverflow\readtracking;
35+
use mod_moodleoverflow\review;
3536
use mod_moodleoverflow\subscriptions;
3637

3738
/**
@@ -136,6 +137,88 @@ public function i_add_a_moodleoverflow_discussion_with_posts_from_different_user
136137
]);
137138
}
138139

140+
/**
141+
* Build a background to test the reviewing (approve/reject) feature.
142+
* Builds:
143+
* - A course with one teacher and one student, both enrolled.
144+
* - A moodleoverflow that requires every new post to be reviewed.
145+
* - A discussion started by the teacher whose first post is already reviewed.
146+
* - A reply by the student that is still pending review.
147+
*
148+
* The student post is created in the past so that it is already past the review waiting
149+
* period (reviewpossibleaftertime), which makes the approve/reject buttons show immediately.
150+
*
151+
* @Given /^I set up a moodleoverflow with a student post pending review$/
152+
* @return void
153+
*/
154+
public function i_set_up_a_moodleoverflow_with_a_student_post_pending_review(): void {
155+
global $DB;
156+
157+
$this->i_prepare_a_moodleoverflow_background(new TableNode([
158+
['username', 'firstname', 'lastname', 'email', 'idnumber', 'role'],
159+
['teacher1', 'Tamaro', 'Walter', 'tamaro@mail.com', '10', 'teacher'],
160+
['student1', 'John', 'Smith', 'john@mail.com', '11', 'student'],
161+
]));
162+
163+
$course = $DB->get_record('course', ['shortname' => 'C1']);
164+
$teacher = $DB->get_record('user', ['username' => 'teacher1']);
165+
$student = $DB->get_record('user', ['username' => 'student1']);
166+
$time = time();
167+
// Age the pending post so it is past the review waiting period and the review buttons are shown.
168+
$pasttime = $time - DAYSECS;
169+
170+
// Create a moodleoverflow that requires every post to be reviewed.
171+
$this->execute('behat_data_generators::the_following_entities_exist', ['activities', new TableNode([
172+
['activity', 'course', 'name', 'needsreview'],
173+
['moodleoverflow', 'C1', 'Moodleoverflow 1', review::EVERYTHING],
174+
])]);
175+
$moodleoverflow = $DB->get_record('moodleoverflow', ['name' => 'Moodleoverflow 1']);
176+
177+
// Create the discussion with the teacher's already reviewed first post.
178+
$discussionid = $DB->insert_record('moodleoverflow_discussions', ['course' => $course->id,
179+
'moodleoverflow' => $moodleoverflow->id, 'name' => 'Discussion 1', 'firstpost' => 1, 'userid' => $teacher->id,
180+
'timemodified' => $time, 'timestart' => $time, 'usermodified' => $teacher->id,
181+
]);
182+
$teacherpostid = $DB->insert_record('moodleoverflow_posts', ['discussion' => $discussionid,
183+
'moodleoverflow' => $moodleoverflow->id, 'parent' => 0, 'userid' => $teacher->id, 'created' => $time,
184+
'modified' => $time, 'message' => 'Message from teacher', 'messageformat' => 1, 'attachment' => '', 'mailed' => 1,
185+
'reviewed' => 1, 'timereviewed' => $time,
186+
]);
187+
188+
// Update firstpost field in discussion.
189+
$record = $DB->get_record('moodleoverflow_discussions', ['id' => $discussionid]);
190+
$record->firstpost = $teacherpostid;
191+
$DB->update_record('moodleoverflow_discussions', $record);
192+
193+
// Create the student reply that is still pending review.
194+
$DB->insert_record('moodleoverflow_posts', ['discussion' => $discussionid, 'moodleoverflow' => $moodleoverflow->id,
195+
'parent' => $teacherpostid, 'userid' => $student->id, 'created' => $pasttime, 'modified' => $pasttime,
196+
'message' => 'Answer from student', 'messageformat' => 1, 'attachment' => '', 'mailed' => 1, 'reviewed' => 0,
197+
'timereviewed' => null,
198+
]);
199+
}
200+
201+
/**
202+
* Clicks a moodleoverflow review action button (e.g. "approve", "reject", "reject-submit").
203+
*
204+
* The button is scrolled into the centre of the viewport first so that it is not covered by
205+
* sticky page elements such as the course activity navigation footer, which would otherwise
206+
* intercept the click.
207+
*
208+
* @When /^I click on the "(?P<action_string>[^"]*)" moodleoverflow review button$/
209+
* @param string $action The value of the data-moodleoverflow-action attribute.
210+
* @return void
211+
*/
212+
public function i_click_on_the_moodleoverflow_review_button(string $action): void {
213+
$selector = "[data-moodleoverflow-action='" . $action . "']";
214+
$node = $this->find('css', $selector);
215+
$this->getSession()->executeScript(
216+
"document.querySelector(\"" . $selector . "\").scrollIntoView({block: 'center'});"
217+
);
218+
$this->ensure_node_is_visible($node);
219+
$node->click();
220+
}
221+
139222
/**
140223
* The admin adds a moodleoverflow discussions with a tracking type. Used in the track_read_posts_feature.
141224
*

tests/behat/review_post.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@mod @mod_moodleoverflow @javascript
2+
Feature: Teachers can review posts in a moodleoverflow that requires reviewing
3+
In order to control which posts become visible
4+
As a teacher
5+
I need to approve or reject posts that are pending review
6+
7+
Background:
8+
Given I set up a moodleoverflow with a student post pending review
9+
10+
Scenario: A teacher approves a student post that is pending review
11+
Given I navigate as "teacher1" to "Course 1" "Moodleoverflow 1" "Discussion 1"
12+
Then I should see "Answer from student"
13+
And I should see "Approve"
14+
When I click on the "approve" moodleoverflow review button
15+
Then I should see "The post was approved."
16+
And I should see "There are no more posts in this forum that need to be reviewed."
17+
18+
Scenario: A teacher rejects a student post that is pending review
19+
Given I navigate as "teacher1" to "Course 1" "Moodleoverflow 1" "Discussion 1"
20+
Then I should see "Reject"
21+
When I click on the "reject" moodleoverflow review button
22+
And I set the field with xpath "//textarea[contains(@class, 'reject-reason')]" to "This post was not good!"
23+
And I click on the "reject-submit" moodleoverflow review button
24+
Then I should see "The post was rejected."
25+
And I should see "There are no more posts in this forum that need to be reviewed."

0 commit comments

Comments
 (0)