Skip to content

Commit cac6e20

Browse files
committed
add behat test for activity overview
1 parent f0eb179 commit cac6e20

File tree

3 files changed

+107
-16
lines changed

3 files changed

+107
-16
lines changed

classes/courseformat/overview.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ private function render_toggle_template(array $templateparams): bool|string {
172172
return $renderer->render_from_template(
173173
'core/toggle',
174174
[
175+
'extraclasses' => $templateparams['datatype'],
175176
'id' => $templateparams['itemid'],
176177
'checked' => $templateparams['checked'],
177178
'disabled' => $templateparams['disabled'],

tests/behat/behat_mod_moodleoverflow.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Behat\Mink\Exception\ElementNotFoundException;
3232
use Behat\Mink\Exception\ExpectationException;
3333
use mod_moodleoverflow\post\post_control;
34+
use mod_moodleoverflow\readtracking;
35+
use mod_moodleoverflow\subscriptions;
3436

3537
/**
3638
* moodleoverflow-related steps definitions.
@@ -420,12 +422,15 @@ public function i_see_elements_in_moodleoverflow(string $text, TableNode $data):
420422
* Automatically adds a discussion to the DB without clicking all over the behat site. Used to minimize test cases that test
421423
* other things than the post.php itself.
422424
*
425+
* IMPORTANT!: Please note that this function is for testing purposes only, as the objects get searched by string values that
426+
* need to be unique in the testing scenario
423427
* @Given User :username adds to :modflowname a discussion with topic :subject and message :message automatically
424428
* @param string $username User that adds the discussin
425429
* @param string $modflowname Name of the moodleoverflow
426430
* @param string $subject Topic of the discussion
427431
* @param string $message Message of the first post in the discussion
428432
* @return void
433+
* @throws dml_exception
429434
*/
430435
public function automatic_add_discussion(string $username, string $modflowname, string $subject, string $message): void {
431436
global $DB;
@@ -449,6 +454,88 @@ public function automatic_add_discussion(string $username, string $modflowname,
449454
$postcontrol->execute_interaction($form);
450455
}
451456

457+
/**
458+
* Automatically adds a reply to a post the DB without clicking all over the behat site. Used to minimize test cases that test
459+
* other things than the post.php itself.
460+
*
461+
* IMPORTANT!: Please note that this function is for testing purposes only, as the objects get searched by string values that
462+
* need to be unique in the testing scenario
463+
* @Given User :username replies :parentmessage with :message automatically
464+
* @param string $username User that adds the discussin
465+
* @param string $parentmessage Name of the moodleoverflow
466+
* @param string $message Message of the first post in the discussion
467+
* @return void
468+
*/
469+
public function automatic_add_reply(string $username, string $parentmessage, string $message): void {
470+
global $DB;
471+
$user = $DB->get_record('user', ['username' => $username]);
472+
$this->set_user($user);
473+
$sql = "SELECT * FROM {moodleoverflow_posts} WHERE " .
474+
$DB->sql_compare_text('message') . " = " .
475+
$DB->sql_compare_text(':message');
476+
$parentpost = $DB->get_record_sql($sql, ['message' => $parentmessage]);
477+
$discussion = $DB->get_record('moodleoverflow_discussions', ['id' => $parentpost->discussion]);
478+
$postcontrol = new post_control();
479+
$postcontrol->detect_interaction((object) ['create' => 0, 'reply' => $parentpost->id, 'edit' => 0, 'delete' => 0]);
480+
481+
$form = (object) [
482+
'attachments' => 0,
483+
'subject' => $discussion->name,
484+
'message' => [
485+
'text' => $message,
486+
'format' => editors_get_preferred_format(),
487+
],
488+
'reply' => $parentpost->id,
489+
'parent' => $parentpost->id,
490+
'discussion' => $discussion->id,
491+
];
492+
$postcontrol->execute_interaction($form);
493+
}
494+
495+
/**
496+
* Checks if the current user is subscribed to a moodleoverflow.
497+
* @Given I should :type be subscribed to :modflowname
498+
* @param string $type "not" is the user should not be subscribed, empty if user should be subscribed
499+
* @param string $modflowname
500+
* @return void
501+
*/
502+
public function should_be_subscribed(string $type, string $modflowname) {
503+
global $DB, $USER;
504+
$moodleoverflow = $DB->get_record('moodleoverflow', ['name' => $modflowname]);
505+
$cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id);
506+
if ($type == 'not') {
507+
if (subscriptions::is_subscribed($USER->id, $moodleoverflow, context_module::instance($cm->id))) {
508+
throw new Exception("User should not be subscribed but is already subscribed");
509+
}
510+
} else {
511+
$params = ['moodleoverflow' => $moodleoverflow->id, 'userid' => $USER->id];
512+
if ($DB->count_records('moodleoverflow_subscriptions', $params) != 1) {
513+
throw new Exception("User should be subscribed but is not");
514+
}
515+
}
516+
}
517+
518+
/**
519+
* Checks if the current user has the readtracking on in a moodleoverflow.
520+
* @Given I should :type have readtracking on in :modflowname
521+
* @param string $type "not" is the user should not, empty if user should have readtracking on
522+
* @param string $modflowname
523+
* @return void
524+
*/
525+
public function should_be_tracking(string $type, string $modflowname) {
526+
global $DB;
527+
$moodleoverflow = $DB->get_record('moodleoverflow', ['name' => $modflowname]);
528+
if ($type == 'not') {
529+
if (readtracking::moodleoverflow_is_tracked($moodleoverflow)) {
530+
throw new Exception("User should not have readtracking on but it is on");
531+
}
532+
} else {
533+
if (!readtracking::moodleoverflow_is_tracked($moodleoverflow)) {
534+
throw new Exception("User should have readtracking on but it is off");
535+
}
536+
}
537+
}
538+
452539
// Internal helper functions.
453540

454541
/**

tests/behat/overview_report.feature

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,32 @@ Feature: Testing overview integration in moodleoverflow activity
55
I need to be able to see the moodleoverflow activity overview
66

77
Background:
8-
Given I prepare a moodleoverflow feature background with users:
8+
Given the site is running Moodle version 5.0 or higher
9+
And I prepare a moodleoverflow feature background with users:
910
| username | firstname | lastname | email | idnumber | role |
1011
| student1 | Student | 1 | student1@example.com | 10 | student |
1112
| teacher1 | Teacher | 1 | teacher1@example.com | 11 | editingteacher |
1213
And the following "activities" exist:
1314
| activity | name | intro | course | idnumber |
1415
| moodleoverflow | Test Moodleoverflow | Test moodleoverflow description | C1 | 1 |
1516
And User "teacher1" adds to "Test Moodleoverflow" a discussion with topic "Topic1" and message "message1" automatically
17+
And User "student1" replies "message1" with "message2" automatically
1618
And I log in as "teacher1"
1719

18-
Scenario: The moodleoverflow activity overview report should generate log events
19-
And the site is running Moodle version 5.0 or higher
20-
And I am on the "Course 1" "course > activities > moodleoverflow" page logged in as "teacher1"
21-
When I am on the "Course 1" "course" page logged in as "teacher1"
22-
And I navigate to "Reports" in current page administration
23-
And I click on "Logs" "link"
24-
And I click on "Get these logs" "button"
25-
Then I should see "Course activities overview page viewed"
26-
And I should see "viewed the instance list for the module 'moodleoverflow'"
20+
Scenario: The bin to mark all unread posts as read should work
21+
When I am on the "C1" "course > activities > moodleoverflow" page logged in as "teacher1"
22+
Then I should see "1" in the ".unread-bubble" "css_element"
23+
When I click on ".mark-read" "css_element"
24+
Then I should see "0" in the ".unread-bubble" "css_element"
2725

28-
Scenario: The moodleoverflow activity index redirect to the activities overview
29-
Given the site is running Moodle version 5.0 or higher
30-
When I am on the "C1" "course > activities > moodleoverflow" page logged in as "admin"
31-
Then I should see "Name" in the "moodleoverflow_overview_collapsible" "region"
32-
And I should see "Unread posts" in the "moodleoverflow_overview_collapsible" "region"
33-
And I should see "Action" in the "moodleoverflow_overview_collapsible" "region"
26+
Scenario: The subscription toggle item should work
27+
Given I am on the "C1" "course > activities > moodleoverflow" page logged in as "student1"
28+
And I should not be subscribed to "Test Moodleoverflow"
29+
When I click on ".moodleoverflow-subscription-toggle .form-check-input" "css_element"
30+
Then I should " " be subscribed to "Test Moodleoverflow"
31+
32+
Scenario: The readtracking toggle item should work
33+
Given I am on the "C1" "course > activities > moodleoverflow" page logged in as "student1"
34+
And I should " " have readtracking on in "Test Moodleoverflow"
35+
When I click on ".moodleoverflow-readtracking-toggle .form-check-input" "css_element"
36+
Then I should not have readtracking on in "Test Moodleoverflow"

0 commit comments

Comments
 (0)