3131use Behat \Mink \Exception \ElementNotFoundException ;
3232use Behat \Mink \Exception \ExpectationException ;
3333use 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 /**
0 commit comments