diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index 51dae371df0..a95965a5251 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -28,7 +28,9 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); use Behat\Behat\Context\Step\Given as Given, - Behat\Gherkin\Node\TableNode as TableNode; + Behat\Gherkin\Node\TableNode as TableNode, + Behat\Mink\Exception\ExpectationException as ExpectationException, + Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; /** * Course-related steps definitions. @@ -114,4 +116,92 @@ class behat_course extends behat_base { } + /** + * Turns course section highlighting on. + * + * @Given /^I turn section "(?P\d+)" highlighting on$/ + * @param int $sectionnumber The section number + */ + public function i_turn_section_highlighting_on($sectionnumber) { + + // Ensures the section exists. + $xpath = $this->section_exists($sectionnumber); + + return array( + new Given('I click on "' . get_string('markthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'), + new Given('I wait "2" seconds') + ); + } + + /** + * Turns course section highlighting off. + * + * @Given /^I turn section "(?P\d+)" highlighting off$/ + * @param int $sectionnumber The section number + */ + public function i_turn_section_highlighting_off($sectionnumber) { + + // Ensures the section exists. + $xpath = $this->section_exists($sectionnumber); + + return array( + new Given('I click on "' . get_string('markedthistopic') . '" "link" in the "' . $xpath . '" "xpath_element"'), + new Given('I wait "2" seconds') + ); + } + + /** + * Checks if the specified course section hightlighting is turned on. + * + * @throws ElementNotFoundException + * @throws ExpectationException + * @Then /^section "(?P\d+)" should be highlighted$/ + * @param int $sectionnumber The section number + */ + public function section_should_be_highlighted($sectionnumber) { + + // Ensures the section exists. + $xpath = $this->section_exists($sectionnumber); + + // The important checking, we can not check the img. + $xpath = $xpath . "/descendant::img[@alt='" . get_string('markedthistopic') . "'][contains(@src, 'marked')]"; + $exception = new ExpectationException('The "' . $sectionnumber . '" section is not highlighted', $this->getSession()); + $this->find('xpath', $xpath, $exception); + } + + /** + * Checks if the specified course section highlighting is turned off. + * + * @Then /^section "(?P\d+)" should not be highlighted$/ + * @param int $sectionnumber The section number + */ + public function section_should_not_be_highlighted($sectionnumber) { + + // We only catch ExpectationException, ElementNotFoundException should be thrown if the specified section does not exist. + try { + $this->section_should_be_highlighted($sectionnumber); + } catch (ExpectationException $e) { + // ExpectedException means that it is not highlighted. + return; + } + + throw new ExpectationException('The "' . $sectionnumber . '" section is highlighted', $this->getSession()); + } + + /** + * Checks if the course section exists. + * + * @throws ElementNotFoundException Thrown by behat_base::find + * @param int $sectionnumber + * @return string The xpath of the existing section. + */ + protected function section_exists($sectionnumber) { + + // Just to give more info in case it does not exist. + $xpath = "//li[@id='section-" . $sectionnumber . "']"; + $exception = new ElementNotFoundException($this->getSession(), "Section $sectionnumber "); + $this->find('xpath', $xpath, $exception); + + return $xpath; + } } diff --git a/course/tests/behat/section_highlighting.feature b/course/tests/behat/section_highlighting.feature new file mode 100644 index 00000000000..dfb5ab585ec --- /dev/null +++ b/course/tests/behat/section_highlighting.feature @@ -0,0 +1,43 @@ +@core_course @_cross_browser +Feature: Topic's course sections highlighting + In order to highlight parts of the course to students + As a teacher + I need to highlight one specific section + + @javascript + Scenario: Highlight a topic's course section + Given the following "users" exists: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@asd.com | + | student1 | Student | 1 | student1@asd.com | + And the following "courses" exists: + | fullname | shortname | format | + | Course 1 | C1 | topics | + And the following "course enrolments" exists: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + When I turn section "1" highlighting on + Then section "1" should be highlighted + And I turn section "2" highlighting on + And section "2" should be highlighted + And section "1" should not be highlighted + And I am on homepage + And I follow "Course 1" + And section "2" should be highlighted + And section "1" should not be highlighted + And I turn section "2" highlighting off + And section "2" should not be highlighted + And I reload the page + And section "2" should not be highlighted + And I am on homepage + And I follow "Course 1" + And section "2" should not be highlighted + And I log out + And I log in as "student1" + And I follow "Course 1" + And section "1" should not be highlighted + And section "2" should not be highlighted diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 67d773e86c6..9f16a92e6e2 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -53,6 +53,15 @@ class behat_general extends behat_base { $this->getSession()->visit($this->locate_path('/')); } + /** + * Reloads the current page. + * + * @Given /^I reload the page$/ + */ + public function reload() { + $this->getSession()->reload(); + } + /** * Clicks link with specified id|title|alt|text. *