Skip to content

Commit b385e6a

Browse files
committed
add automatic behat script for creating discussions
1 parent 99b8df3 commit b385e6a

File tree

9 files changed

+94
-107
lines changed

9 files changed

+94
-107
lines changed

classes/post/post_control.php

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ public function detect_interaction(stdClass $urlparameter): void {
135135
/**
136136
* Controls the execution of an interaction.
137137
* @param object $form The results from the post_form.
138+
* @return object An object with information for the redirect
138139
* @throws coding_exception
139140
* @throws moodle_exception
140141
*/
141-
public function execute_interaction(object $form): void {
142+
public function execute_interaction(object $form): object {
142143
global $SESSION;
143144
// Redirect url in case of occurring errors.
144145
$SESSION->errorreturnurl = new moodle_url('/mod/moodleoverflow/view.php?', ['m' => $this->prepost->moodleoverflowid]);
@@ -154,11 +155,11 @@ public function execute_interaction(object $form): void {
154155

155156
// Execute the right function.
156157
if ($this->interaction == 'create' && $form->moodleoverflow == $this->prepost->moodleoverflowid) {
157-
$this->execute_create($form);
158+
return $this->execute_create($form);
158159
} else if ($this->interaction == 'reply' && $form->reply == $this->prepost->parentid) {
159-
$this->execute_reply($form);
160+
return $this->execute_reply($form);
160161
} else if ($this->interaction == 'edit' && $form->edit == $this->prepost->postid) {
161-
$this->execute_edit($form);
162+
return $this->execute_edit($form);
162163
} else {
163164
throw new coding_exception(get_string('errorunexpectedinteraction', 'moodleoverflow'));
164165
}
@@ -354,8 +355,9 @@ private function build_prepost_delete(int $deletepostid): void {
354355
*
355356
* @param object $form The results from the post_form.
356357
* @throws moodle_exception if the discussion could not be added.
358+
* @return object An object with all information to redirect.
357359
*/
358-
private function execute_create(object $form): void {
360+
private function execute_create(object $form): object {
359361
global $USER;
360362
// Check if the user is allowed to post.
361363
$this->check_user_can_create_discussion();
@@ -388,9 +390,6 @@ private function execute_create(object $form): void {
388390
throw new moodle_exception('couldnotadd', 'moodleoverflow');
389391
}
390392

391-
// The creation was successful.
392-
$redirectmessage = html_writer::tag('p', get_string("postaddedsuccess", "moodleoverflow"));
393-
394393
// Trigger the discussion created event.
395394
$params = ['context' => $this->info->modulecontext, 'objectid' => $discussion->get_id()];
396395
$event = discussion_created::create($params);
@@ -407,7 +406,8 @@ private function execute_create(object $form): void {
407406

408407
// Define the location to redirect the user after successfully posting.
409408
$redirectto = new moodle_url('/mod/moodleoverflow/view.php', ['m' => $form->moodleoverflow]);
410-
redirect(moodleoverflow_go_back_to($redirectto), $redirectmessage, null, \core\output\notification::NOTIFY_SUCCESS);
409+
$redirectmessage = html_writer::tag('p', get_string("postaddedsuccess", "moodleoverflow"));
410+
return (object) ['redirectto' => $redirectto, 'redirectmessage' => $redirectmessage];
411411
}
412412

413413
/**
@@ -416,7 +416,7 @@ private function execute_create(object $form): void {
416416
* @param object $form The results from the post_form.
417417
* @throws moodle_exception if the reply could not be added.
418418
*/
419-
private function execute_reply(object $form): void {
419+
private function execute_reply(object $form): object {
420420
// Check if the user has the capability to write a reply.
421421
$this->check_user_can_create_reply();
422422

@@ -435,14 +435,6 @@ private function execute_reply(object $form): void {
435435
throw new moodle_exception('couldnotadd', 'moodleoverflow');
436436
}
437437

438-
// The creation was successful.
439-
$redirectmessage = html_writer::tag('p', get_string("postaddedsuccess", "moodleoverflow"));
440-
$redirectmessage .= html_writer::tag('p', get_string(
441-
"postaddedtimeleft",
442-
"moodleoverflow",
443-
format_time(get_config('moodleoverflow', 'maxeditingtime'))
444-
));
445-
446438
// Trigger the post created event.
447439
$params = ['context' => $this->info->modulecontext, 'objectid' => $newpostid,
448440
'other' => ['discussionid' => $this->prepost->discussionid,
@@ -466,7 +458,13 @@ private function execute_reply(object $form): void {
466458
'/mod/moodleoverflow/discussion.php',
467459
['d' => $this->prepost->discussionid, 'p' => $newpostid]
468460
);
469-
redirect(\moodleoverflow_go_back_to($redirectto), $redirectmessage, null, \core\output\notification::NOTIFY_SUCCESS);
461+
$redirectmessage = html_writer::tag('p', get_string("postaddedsuccess", "moodleoverflow"));
462+
$redirectmessage .= html_writer::tag('p', get_string(
463+
"postaddedtimeleft",
464+
"moodleoverflow",
465+
format_time(get_config('moodleoverflow', 'maxeditingtime'))
466+
));
467+
return (object) ['redirectto' => $redirectto, 'redirectmessage' => $redirectmessage];
470468
}
471469

472470
/**
@@ -475,7 +473,7 @@ private function execute_reply(object $form): void {
475473
* @param object $form The results from the post_form.
476474
* @throws moodle_exception if the post could not be updated.
477475
*/
478-
private function execute_edit(object $form): void {
476+
private function execute_edit(object $form): object {
479477
global $USER, $DB;
480478
// Check if the user has the capability to edit his post.
481479
$this->check_user_can_edit_post();
@@ -490,18 +488,6 @@ private function execute_edit(object $form): void {
490488
throw new moodle_exception('couldnotupdate', 'moodleoverflow');
491489
}
492490

493-
// The edit was successful.
494-
$redirectmessage = get_string('postupdated', 'moodleoverflow');
495-
if ($this->prepost->userid != $USER->id) {
496-
if (anonymous::is_post_anonymous($this->info->discussion, $this->info->moodleoverflow, $this->prepost->userid)) {
497-
$name = get_string('anonymous', 'moodleoverflow');
498-
} else {
499-
$realuser = $DB->get_record('user', ['id' => $this->prepost->userid]);
500-
$name = fullname($realuser);
501-
}
502-
$redirectmessage = get_string('editedpostupdated', 'moodleoverflow', $name);
503-
}
504-
505491
// Trigger the post updated event.
506492
$params = ['context' => $this->info->modulecontext, 'objectid' => $form->edit,
507493
'other' => ['discussionid' => $this->prepost->discussionid,
@@ -517,15 +503,28 @@ private function execute_edit(object $form): void {
517503
'/mod/moodleoverflow/discussion.php',
518504
['d' => $this->prepost->discussionid, 'p' => $form->edit]
519505
);
520-
redirect(moodleoverflow_go_back_to($redirectto), $redirectmessage, null, \core\output\notification::NOTIFY_SUCCESS);
506+
// The edit was successful.
507+
$redirectmessage = get_string('postupdated', 'moodleoverflow');
508+
if ($this->prepost->userid != $USER->id) {
509+
if (anonymous::is_post_anonymous($this->info->discussion, $this->info->moodleoverflow, $this->prepost->userid)) {
510+
$name = get_string('anonymous', 'moodleoverflow');
511+
} else {
512+
$realuser = $DB->get_record('user', ['id' => $this->prepost->userid]);
513+
$name = fullname($realuser);
514+
}
515+
$redirectmessage = get_string('editedpostupdated', 'moodleoverflow', $name);
516+
}
517+
518+
return (object) ['redirectto' => $redirectto, 'redirectmessage' => $redirectmessage];
521519
}
522520

523521
/**
524522
* Executes the deletion of a post.
525523
*
524+
* @return mixed redirect information
526525
* @throws moodle_exception if the post could not be deleted.
527526
*/
528-
public function execute_delete(): void {
527+
public function execute_delete(): mixed {
529528
global $SESSION;
530529
$this->check_interaction('delete');
531530

@@ -551,11 +550,11 @@ public function execute_delete(): void {
551550
$this->info->discussion->moodleoverflow_delete_discussion($this->prepost);
552551

553552
// Redirect the user back to the start page of the moodleoverflow instance.
554-
redirect('view.php?m=' . $moodleoverflowid);
553+
return 'view.php?m=' . $moodleoverflowid;
555554
} else {
556555
$this->info->discussion->moodleoverflow_delete_post_from_discussion($this->prepost);
557556
$discussionurl = new moodle_url('/mod/moodleoverflow/discussion.php', ['d' => $this->info->discussion->get_id()]);
558-
redirect(moodleoverflow_go_back_to($discussionurl));
557+
return moodleoverflow_go_back_to($discussionurl);
559558
}
560559
}
561560

post.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
use core\output\notification;
2526
use mod_moodleoverflow\post\post_control;
2627
// Include config and locallib.
2728
use mod_moodleoverflow\anonymous;
@@ -54,11 +55,7 @@
5455
$postcontrol = new post_control();
5556

5657
// Put all interaction parameters in one object for the post_control.
57-
$urlparameter = new stdClass();
58-
$urlparameter->create = $moodleoverflow;
59-
$urlparameter->reply = $reply;
60-
$urlparameter->edit = $edit;
61-
$urlparameter->delete = $delete;
58+
$urlparameter = (object) ['create' => $moodleoverflow, 'reply' => $reply, 'edit' => $edit, 'delete' => $delete];
6259

6360
// Catch guests.
6461
if (!isloggedin() || isguestuser()) {
@@ -97,7 +94,8 @@
9794
// Has the user confirmed the deletion?
9895
if (!empty($confirm) && confirm_sesskey()) {
9996
try {
100-
$postcontrol->execute_delete();
97+
$redirect = $postcontrol->execute_delete();
98+
redirect($redirect);
10199
} catch (moodle_exception $e) {
102100
$postcontrol->error_handling($e->getMessage());
103101
}
@@ -145,7 +143,8 @@
145143
// If the post_form is submitted, the post_control executes the right function.
146144
if ($fromform = $mformpost->get_data()) {
147145
try {
148-
$postcontrol->execute_interaction($fromform);
146+
$redirect = $postcontrol->execute_interaction($fromform);
147+
redirect(moodleoverflow_go_back_to($redirect->redirectto), $redirect->redirectmessage, null, notification::NOTIFY_SUCCESS);
149148
} catch (moodle_exception $e) {
150149
$postcontrol->error_handling($e->getMessage());
151150
}

tests/behat/behat_mod_moodleoverflow.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Behat\Gherkin\Node\TableNode;
3131
use Behat\Mink\Exception\ElementNotFoundException;
3232
use Behat\Mink\Exception\ExpectationException;
33+
use mod_moodleoverflow\post\post_control;
3334

3435
/**
3536
* moodleoverflow-related steps definitions.
@@ -145,12 +146,7 @@ public function admin_adds_moodleoverflow_with_tracking_type(string $subject, st
145146
['activity', 'course', 'name', 'intro', 'trackingtype'],
146147
['moodleoverflow', 'C1', $name, 'Test moodleoverflow description', $trackingtype],
147148
])]);
148-
$this->execute('behat_auth::i_log_in_as', 'admin');
149-
$this->execute('behat_navigation::i_am_on_course_homepage', 'Course 1');
150-
$this->i_add_a_moodleoverflow_discussion_to_moodleoverflow_with($name, new TableNode([
151-
['Subject', $subject],
152-
['Message', 'Test post message'],
153-
]));
149+
$this->automatic_add_discussion('admin', $name, $subject, 'Test post message');
154150
}
155151

156152
/**
@@ -163,12 +159,7 @@ public function admin_adds_moodleoverflow_with_tracking_type(string $subject, st
163159
* @return void
164160
*/
165161
public function admin_adds_discussion(string $subject, string $name): void {
166-
$this->execute('behat_auth::i_log_in_as', 'admin');
167-
$this->execute('behat_navigation::i_am_on_course_homepage', 'Course 1');
168-
$this->i_add_a_moodleoverflow_discussion_to_moodleoverflow_with($name, new TableNode([
169-
['Subject', $subject],
170-
['Message', 'Test post message'],
171-
]));
162+
$this->automatic_add_discussion('admin', $name, $subject, 'Test post message');
172163
}
173164

174165
/**
@@ -414,6 +405,39 @@ public function i_see_elements_in_moodleoverflow(string $text, TableNode $data):
414405
}
415406
}
416407

408+
/**
409+
* Automatically adds a discussion to the DB without clicking all over the behat site. Used to minimize test cases that test
410+
* other things than the post.php itself.
411+
*
412+
* @Given User :username adds to :modflowname a discussion with topic :subject and message :message automatically
413+
* @param string $username User that adds the discussin
414+
* @param string $modflowname Name of the moodleoverflow
415+
* @param string $subject Topic of the discussion
416+
* @param string $message Message of the first post in the discussion
417+
* @return void
418+
*/
419+
public function automatic_add_discussion(string $username, string $modflowname, string $subject, string $message): void {
420+
global $DB;
421+
$user = $DB->get_record('user', ['username' => $username]);
422+
$moodleoverflow = $DB->get_record('moodleoverflow', ['name' => $modflowname]);
423+
$this->set_user($user);
424+
425+
$postcontrol = new post_control();
426+
$postcontrol->detect_interaction((object) ['create' => $moodleoverflow->id, 'reply' => 0, 'edit' => 0, 'delete' => 0]);
427+
428+
// Create the form the user filled in.
429+
$form = (object) [
430+
'attachments' => 0,
431+
'subject' => $subject,
432+
'message' => [
433+
'text' => $message,
434+
'format' => editors_get_preferred_format(),
435+
],
436+
'moodleoverflow' => $moodleoverflow->id,
437+
];
438+
$postcontrol->execute_interaction($form);
439+
}
440+
417441
// Internal helper functions.
418442

419443
/**

tests/behat/discussion_subscriptions.feature

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ Feature: A user can control their own moodleoverflow subscription preferences fo
1515
Given the following "activities" exist:
1616
| activity | name | intro | course | idnumber | forcesubscribe |
1717
| moodleoverflow | Test moodleoverflow name | Test moodleoverflow description | C1 | moodleoverflow | 0 |
18-
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:
19-
| Subject | Test post subject one |
20-
| Message | Test post message one |
21-
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:
22-
| Subject | Test post subject two |
23-
| Message | Test post message two |
18+
And User "admin" adds to "Test moodleoverflow name" a discussion with topic "Test post subject one" and message "Test post message one" automatically
19+
And User "admin" adds to "Test moodleoverflow name" a discussion with topic "Test post subject two" and message "Test post message two" automatically
2420
And I log out
2521
When I navigate as "student1" to "Course 1" "Test moodleoverflow name" ""
2622
Then I should see "Subscribe to this forum"
@@ -52,12 +48,8 @@ Feature: A user can control their own moodleoverflow subscription preferences fo
5248
Given the following "activities" exist:
5349
| activity | name | intro | course | idnumber | forcesubscribe |
5450
| moodleoverflow | Test moodleoverflow name | Test moodleoverflow description | C1 | moodleoverflow | 2 |
55-
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:
56-
| Subject | Test post subject one |
57-
| Message | Test post message one |
58-
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:
59-
| Subject | Test post subject two |
60-
| Message | Test post message two |
51+
And User "admin" adds to "Test moodleoverflow name" a discussion with topic "Test post subject one" and message "Test post message one" automatically
52+
And User "admin" adds to "Test moodleoverflow name" a discussion with topic "Test post subject two" and message "Test post message two" automatically
6153
And I log out
6254
When I navigate as "student1" to "Course 1" "Test moodleoverflow name" ""
6355
Then I should see "Unsubscribe from this forum"

tests/behat/limitedanswer.feature

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ Feature: Moodleoverflows can start in a limited answer mode, where answers from
1212
Given the following "activities" exist:
1313
| activity | name | intro | course | idnumber | la_starttime |
1414
| moodleoverflow | Test Moodleoverflow | Test moodleoverflow description | C1 | 1 | ##now +1 day## |
15-
And I navigate as "teacher1" to "Course 1" "Test Moodleoverflow" ""
16-
And I add a new discussion to "Test Moodleoverflow" moodleoverflow with:
17-
| Subject | Forum post 1 |
18-
| Message | This is the question message |
15+
And User "admin" adds to "Test moodleoverflow" a discussion with topic "Forum post 1" and message "This is the question message" automatically
1916
And I log out
2017
When I navigate as "student1" to "Course 1" "Test Moodleoverflow" "Forum post 1"
2118
And I click on "Answer" "text"

tests/behat/moodleoverflow_subscriptions.feature

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@ Feature: A user can control their own subscription preferences for a moodleoverf
88
Given I prepare a moodleoverflow feature background with users:
99
| username | firstname | lastname | email | idnumber | role |
1010
| student1 | Student | One | student.one@example.com | 10 | student |
11-
And I log in as "admin"
12-
And I am on "Course 1" course homepage with editing mode "on"
1311

1412
Scenario: A disallowed subscription moodleoverflow cannot be subscribed to
1513
Given the following "activities" exist:
1614
| activity | name | intro | course | idnumber | forcesubscribe |
1715
| moodleoverflow | Test moodleoverflow name | Test moodleoverflow description | C1 | moodleoverflow | 3 |
18-
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:
19-
| Subject | Test post subject |
20-
| Message | Test post message |
21-
And I log out
16+
And User "admin" adds to "Test moodleoverflow name" a discussion with topic "Test post subject" and message "Test post message" automatically
2217
When I navigate as "student1" to "Course 1" "Test moodleoverflow name" ""
2318
Then I should "not" see the elements:
2419
| Subscribe to this forum | Unsubscribe from this forum |
@@ -29,10 +24,7 @@ Feature: A user can control their own subscription preferences for a moodleoverf
2924
Given the following "activities" exist:
3025
| activity | name | intro | course | idnumber | forcesubscribe |
3126
| moodleoverflow | Test moodleoverflow name | Test moodleoverflow description | C1 | moodleoverflow | 1 |
32-
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:
33-
| Subject | Test post subject |
34-
| Message | Test post message |
35-
And I log out
27+
And User "admin" adds to "Test moodleoverflow name" a discussion with topic "Test post subject" and message "Test post message" automatically
3628
When I navigate as "student1" to "Course 1" "Test moodleoverflow name" ""
3729
Then I should "not" see the elements:
3830
| Subscribe to this forum | Unsubscribe from this forum |
@@ -43,10 +35,7 @@ Feature: A user can control their own subscription preferences for a moodleoverf
4335
Given the following "activities" exist:
4436
| activity | name | intro | course | idnumber | forcesubscribe |
4537
| moodleoverflow | Test moodleoverflow name | Test moodleoverflow description | C1 | moodleoverflow | 0 |
46-
And I add a new discussion to "Test moodleoverflow name" moodleoverflow with:
47-
| Subject | Test post subject |
48-
| Message | Test post message |
49-
And I log out
38+
And User "admin" adds to "Test moodleoverflow name" a discussion with topic "Test post subject" and message "Test post message" automatically
5039
When I navigate as "student1" to "Course 1" "Test moodleoverflow name" ""
5140
Then I should see "Subscribe to this forum"
5241
And I should not see "Unsubscribe from this forum"

0 commit comments

Comments
 (0)