<commit_before>@javascript @tool @tool_monitor @tool_monitor_subscriptions
Feature: tool_monitor_subscriptions
  In order to monitor events and receive notifications
  As an user
  I need to create a new rule, subscribe to it, receive notification and delete subscription

  Background:
    Given the following "courses" exist:
      | fullname | shortname |
      | Course 1 | C1        |
      | Course 2 | C2        |
    And the following "users" exist:
      | username | firstname | lastname | email |
      | teacher1 | Teacher | 1 | <EMAIL> |
      | teacher2 | Teacher | 2 | <EMAIL> |
    And the following "course enrolments" exist:
      | user | course | role |
      | teacher1 | C1 | editingteacher |
      | teacher1 | C2 | teacher |
      | teacher2 | C1 | teacher |
      | teacher2 | C2 | editingteacher |
    And I log in as "admin"
    And I navigate to "Event monitoring rules" node in "Site administration > Reports"
    And I click on "Enable" "link"
    And I am on site homepage
    And I follow "Course 1"
    And I navigate to "Event monitoring rules" node in "Course administration > Reports"
    And I press "Add a new rule"
    And I set the following fields to these values:
      | name                 | New rule course level                             |
      | plugin               | Core                                              |
      | eventname            | Course viewed                                     |
      | id_description       | I want a rule to monitor when a course is viewed. |
      | frequency            | 1                                                 |
      | minutes              | 1                                                 |
      | Notification message | The course was viewed. {modulelink}               |
    And I press "Save changes"
    And I navigate to "Event monitoring rules" node in "Site administration > Reports"
    And I press "Add a new rule"
    And I set the following fields to these values:
      | name                 | New rule site level                               |
      | plugin               | Core                                              |
      | eventname            | Course viewed                                     |
      | id_description       | I want a rule to monitor when a course is viewed. |
      | frequency            | 1                                                 |
      | minutes              | 1                                                 |
      | Notification message | The course was viewed. {modulelink}               |
    And I press "Save changes"
    And I navigate to "Define roles" node in "Site administration > Users > Permissions"
    And I follow "Non-editing teacher"
    And I press "Edit"
    And I click on "tool/monitor:managerules" "checkbox"
    And I press "Save changes"
    And I log out

  Scenario: Subscribe to a rule on course level
    Given I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    When I follow "Subscribe to rule \"New rule course level\""
    Then I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist

  Scenario: Delete a subscription on course level
    Given I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I follow "Subscribe to rule \"New rule course level\""
    And I should see "Subscription successfully created"
    When I click on "Delete subscription" "link" in the "New rule course level" "table_row"
    And I should see "Are you sure you want to delete the subscription to the rule \"New rule course level\"?"
    And I press "Continue"
    Then I should see "Subscription successfully removed"
    And "#toolmonitorsubs_r0" "css_element" should not exist

  Scenario: Subscribe to a rule on site level
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Acceptance test site"
    When I follow "Subscribe to rule \"New rule site level\""
    Then I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist

  Scenario: Delete a subscription on site level
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Acceptance test site"
    And I follow "Subscribe to rule \"New rule site level\""
    And I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist
    When I click on "Delete subscription" "link" in the "New rule site level" "table_row"
    And I should see "Are you sure you want to delete the subscription to the rule \"New rule site level\"?"
    And I press "Continue"
    Then I should see "Subscription successfully removed"
    And "#toolmonitorsubs_r0" "css_element" should not exist

  Scenario: Receiving notification on site level
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    And I follow "Messaging"
    And I click on "input[name^=tool_monitor_notification_loggedin]" "css_element"
    And I press "Save changes"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Acceptance test site"
    And I follow "Subscribe to rule \"New rule site level\""
    And I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist
    And I am on site homepage
    And I trigger cron
    And I am on site homepage
    When I follow "Messages" in the user menu
    And I follow "Do not reply to this email (1)"
    Then I should see "The course was viewed."

  Scenario: Receiving notification on course level
    Given I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Messaging"
    And I click on "input[name^=tool_monitor_notification_loggedin]" "css_element"
    And I press "Save changes"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I follow "Subscribe to rule \"New rule course level\""
    And I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist
    And I am on site homepage
    And I follow "Course 1"
    And I trigger cron
    And I am on site homepage
    When I follow "Messages" in the user menu
    And I follow "Do not reply to this email (1)"
    Then I should see "The course was viewed."

  Scenario: Navigating via quick link to rules
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    When I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    Then I should see "You can manage rules from the Event monitoring rules page."
    And I follow "Event monitoring rules"
    And I should see "You can subscribe to rules from the Event monitoring page."
    And I log out
    And I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I should see "You can manage rules from the Event monitoring rules page."
    And I follow "Event monitoring rules"
    And I should see "You can subscribe to rules from the Event monitoring page."
    And I click on "//a[text()='Event monitoring']" "xpath_element"
    And the field "courseid" matches value "Course 1"
    And I set the field "courseid" to "Choose..."
    And I should not see "You can manage rules from the Event monitoring rules page."
    And I log out
    And I log in as "teacher2"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I should not see "You can manage rules the from the Event monitoring rules page."

  Scenario: No manage rules link when user does not have permission
    Given I log in as "admin"<commit_msg>MDL-50039 tool_monitor: Behat update to fix selection error<commit_after>@javascript @tool @tool_monitor @tool_monitor_subscriptions
Feature: tool_monitor_subscriptions
  In order to monitor events and receive notifications
  As an user
  I need to create a new rule, subscribe to it, receive notification and delete subscription

  Background:
    Given the following "courses" exist:
      | fullname | shortname |
      | Course 1 | C1        |
      | Course 2 | C2        |
    And the following "users" exist:
      | username | firstname | lastname | email |
      | teacher1 | Teacher | 1 | <EMAIL> |
      | teacher2 | Teacher | 2 | <EMAIL> |
    And the following "course enrolments" exist:
      | user | course | role |
      | teacher1 | C1 | editingteacher |
      | teacher1 | C2 | teacher |
      | teacher2 | C1 | teacher |
      | teacher2 | C2 | editingteacher |
    And I log in as "admin"
    And I navigate to "Event monitoring rules" node in "Site administration > Reports"
    And I click on "Enable" "link"
    And I am on site homepage
    And I follow "Course 1"
    And I navigate to "Event monitoring rules" node in "Course administration > Reports"
    And I press "Add a new rule"
    And I set the following fields to these values:
      | name                 | New rule course level                             |
      | plugin               | Core                                              |
      | eventname            | Course viewed                                     |
      | id_description       | I want a rule to monitor when a course is viewed. |
      | frequency            | 1                                                 |
      | minutes              | 1                                                 |
      | Notification message | The course was viewed. {modulelink}               |
    And I press "Save changes"
    And I navigate to "Event monitoring rules" node in "Site administration > Reports"
    And I press "Add a new rule"
    And I set the following fields to these values:
      | name                 | New rule site level                               |
      | plugin               | Core                                              |
      | eventname            | Course viewed                                     |
      | id_description       | I want a rule to monitor when a course is viewed. |
      | frequency            | 1                                                 |
      | minutes              | 1                                                 |
      | Notification message | The course was viewed. {modulelink}               |
    And I press "Save changes"
    And I navigate to "Define roles" node in "Site administration > Users > Permissions"
    And I follow "Non-editing teacher"
    And I press "Edit"
    And I click on "tool/monitor:managerules" "checkbox"
    And I press "Save changes"
    And I log out

  Scenario: Subscribe to a rule on course level
    Given I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    When I follow "Subscribe to rule \"New rule course level\""
    Then I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist

  Scenario: Delete a subscription on course level
    Given I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I follow "Subscribe to rule \"New rule course level\""
    And I should see "Subscription successfully created"
    When I click on "Delete subscription" "link" in the "New rule course level" "table_row"
    And I should see "Are you sure you want to delete the subscription to the rule \"New rule course level\"?"
    And I press "Continue"
    Then I should see "Subscription successfully removed"
    And "#toolmonitorsubs_r0" "css_element" should not exist

  Scenario: Subscribe to a rule on site level
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Acceptance test site"
    When I follow "Subscribe to rule \"New rule site level\""
    Then I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist

  Scenario: Delete a subscription on site level
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Acceptance test site"
    And I follow "Subscribe to rule \"New rule site level\""
    And I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist
    When I click on "Delete subscription" "link" in the "New rule site level" "table_row"
    And I should see "Are you sure you want to delete the subscription to the rule \"New rule site level\"?"
    And I press "Continue"
    Then I should see "Subscription successfully removed"
    And "#toolmonitorsubs_r0" "css_element" should not exist

  Scenario: Receiving notification on site level
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    And I follow "Messaging"
    And I click on "input[name^=tool_monitor_notification_loggedin]" "css_element"
    And I press "Save changes"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Acceptance test site"
    And I follow "Subscribe to rule \"New rule site level\""
    And I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist
    And I am on site homepage
    And I trigger cron
    And I am on site homepage
    When I follow "Messages" in the user menu
    And I follow "Do not reply to this email (1)"
    Then I should see "The course was viewed."

  Scenario: Receiving notification on course level
    Given I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Messaging"
    And I click on "input[name^=tool_monitor_notification_loggedin]" "css_element"
    And I press "Save changes"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I follow "Subscribe to rule \"New rule course level\""
    And I should see "Subscription successfully created"
    And "#toolmonitorsubs_r0" "css_element" should exist
    And I am on site homepage
    And I follow "Course 1"
    And I trigger cron
    And I am on site homepage
    When I follow "Messages" in the user menu
    And I follow "Do not reply to this email (1)"
    Then I should see "The course was viewed."

  Scenario: Navigating via quick link to rules
    Given I log in as "admin"
    And I follow "Preferences" in the user menu
    When I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    Then I should see "You can manage rules from the Event monitoring rules page."
    And I follow "Event monitoring rules"
    And I should see "You can subscribe to rules from the Event monitoring page."
    And I log out
    And I log in as "teacher1"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I should see "You can manage rules from the Event monitoring rules page."
    And I follow "Event monitoring rules"
    And I should see "You can subscribe to rules from the Event monitoring page."
    And I click on "//a[text()='Event monitoring']" "xpath_element"
    And the field "courseid" matches value "Course 1"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I should not see "You can manage rules from the Event monitoring rules page."
    And I log out
    And I log in as "teacher2"
    And I follow "Preferences" in the user menu
    And I follow "Event monitoring"
    And I set the field "Select a course" to "Course 1"
    And I should not see "You can manage rules the from the Event monitoring rules page."

  Scenario: No manage rules link when user does not have permission
    Given I log in as "admin"
{'fraction_non_alphanumeric': 0.0584509591024249, 'fraction_numerical': 0.0054892025576064665, 'mean_word_length': 2.300617161059128, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2553884', 'n_tokens_mistral': 4332, 'n_tokens_neox': 4185, 'n_words': 2251}