diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index a477f391041..bc3d23ba331 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -82,32 +82,6 @@ class behat_course extends behat_base { ]; } - /** - * Turns editing mode on. - * @Given /^I turn editing mode on$/ - */ - public function i_turn_editing_mode_on() { - - try { - $this->execute("behat_forms::press_button", get_string('turneditingon')); - } catch (Exception $e) { - $this->execute("behat_navigation::i_navigate_to_in_current_page_administration", [get_string('turneditingon')]); - } - } - - /** - * Turns editing mode off. - * @Given /^I turn editing mode off$/ - */ - public function i_turn_editing_mode_off() { - - try { - $this->execute("behat_forms::press_button", get_string('turneditingoff')); - } catch (Exception $e) { - $this->execute("behat_navigation::i_navigate_to_in_current_page_administration", [get_string('turneditingoff')]); - } - } - /** * Creates a new course with the provided table data matching course settings names with the desired values. * @@ -733,9 +707,7 @@ class behat_course extends behat_base { * @throws ExpectationException */ public function activity_should_be_hidden($activityname) { - if ($this->is_course_editor()) { - // The activity should exist. $activitynode = $this->get_activity_node($activityname); @@ -758,7 +730,6 @@ class behat_course extends behat_base { } } else { - // It should not exist at all. try { $this->get_activity_node($activityname); @@ -1310,15 +1281,13 @@ class behat_course extends behat_base { * * @return bool */ - protected function is_course_editor() { - - // We don't need to behat_base::spin() here as all is already loaded. - if (!$this->getSession()->getPage()->findButton(get_string('turneditingoff')) && - !$this->getSession()->getPage()->findButton(get_string('turneditingon'))) { + protected function is_course_editor(): bool { + try { + $this->find('field', get_string('editmode'), false, false, 0); + return true; + } catch (ElementNotFoundException $e) { return false; } - - return true; } /** @@ -1327,7 +1296,8 @@ class behat_course extends behat_base { * @return bool */ protected function is_editing_on() { - return $this->getSession()->getPage()->findButton(get_string('turneditingoff')) ? true : false; + $body = $this->find('xpath', "//body", false, false, 0); + return $body->hasClass('editing'); } /** diff --git a/lib/behat/classes/behat_session_trait.php b/lib/behat/classes/behat_session_trait.php index 399a882c46a..775c5db06be 100644 --- a/lib/behat/classes/behat_session_trait.php +++ b/lib/behat/classes/behat_session_trait.php @@ -134,7 +134,7 @@ trait behat_session_trait { } // How much we will be waiting for the element to appear. - if (!$timeout) { + if ($timeout === false) { $timeout = self::get_timeout(); $microsleep = false; } else { @@ -339,7 +339,7 @@ trait behat_session_trait { protected function spin($lambda, $args = false, $timeout = false, $exception = false, $microsleep = false) { // Using default timeout which is pretty high. - if (!$timeout) { + if ($timeout === false) { $timeout = self::get_timeout(); } diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index f40bb4058c8..a6ea480ab95 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -833,33 +833,38 @@ class behat_navigation extends behat_base { /** * Open the course homepage with editing mode enabled. * - * @Given /^I am on "(?P(?:[^"]|\\")*)" course homepage with editing mode on$/ - * @throws coding_exception * @param string $coursefullname The course full name of the course. - * @return void */ public function i_am_on_course_homepage_with_editing_mode_on($coursefullname) { + $this->i_am_on_course_homepage_with_editing_mode_set_to($coursefullname, 'on'); + } + + /** + * Open the course homepage with editing mode set to either on, or off. + * + * @Given I am on :coursefullname course homepage with editing mode :onoroff + * @throws coding_exception + * @param string $coursefullname The course full name of the course. + * @param string $onoroff Whehter to switch editing on, or off. + */ + public function i_am_on_course_homepage_with_editing_mode_set_to(string $coursefullname, string $onoroff): void { global $DB; $course = $DB->get_record("course", array("fullname" => $coursefullname), 'id', MUST_EXIST); $url = new moodle_url('/course/view.php', ['id' => $course->id]); - if ($this->running_javascript() && $sesskey = $this->get_sesskey()) { - // Javascript is running so it is possible to grab the session ket and jump straight to editing mode. - $url->param('edit', 1); - $url->param('sesskey', $sesskey); - $this->execute('behat_general::i_visit', [$url]); - - return; - } - // Visit the course page. $this->execute('behat_general::i_visit', [$url]); - try { - $this->execute("behat_forms::press_button", get_string('turneditingon')); - } catch (Exception $e) { - $this->execute("behat_navigation::i_navigate_to_in_current_page_administration", [get_string('turneditingon')]); + switch ($onoroff) { + case 'on': + $this->execute('behat_navigation::i_turn_editing_mode_on'); + break; + case 'off': + $this->execute('behat_navigation::i_turn_editing_mode_off'); + break; + default: + throw new \coding_exception("Unknown editing mode '{$onoroff}'. Accepted values are 'on' and 'off'"); } } @@ -1103,7 +1108,6 @@ class behat_navigation extends behat_base { $this->execute('behat_general::i_visit', [$url]); } - /** * First checks to see if we are on this page via the breadcrumb. If not we then attempt to follow the link name given. * @@ -1193,4 +1197,54 @@ class behat_navigation extends behat_base { "//div[contains(concat(' ', @class, ' '), ' dropdown-menu ')]" . "//div[contains(concat(' ', @class, ' '), ' submenu ')][@aria-label='" . $submenuname . "']"; } + + /** + * Returns whether the user can edit the current page. + * + * @return bool + */ + protected function is_editing_on() { + $body = $this->find('xpath', "//body", false, false, 0); + return $body->hasClass('editing'); + } + + /** + * Turns editing mode on. + * @Given I switch editing mode on + * @Given I turn editing mode on + */ + public function i_turn_editing_mode_on() { + $this->execute('behat_forms::i_set_the_field_to', [get_string('editmode'), 1]); + + if (!$this->running_javascript()) { + $this->execute('behat_general::i_click_on', [ + get_string('setmode', 'core'), + 'button', + ]); + } + + if (!$this->is_editing_on()) { + throw new ExpectationException('The edit mode could not be turned on', $this->getSession()); + } + } + + /** + * Turns editing mode off. + * @Given I switch editing mode off + * @Given I turn editing mode off + */ + public function i_turn_editing_mode_off() { + $this->execute('behat_forms::i_set_the_field_to', [get_string('editmode'), 0]); + + if (!$this->running_javascript()) { + $this->execute('behat_general::i_click_on', [ + get_string('setmode', 'core'), + 'button', + ]); + } + + if ($this->is_editing_on()) { + throw new ExpectationException('The edit mode could not be turned off', $this->getSession()); + } + } } diff --git a/lib/tests/behat/switch_editing_mode.feature b/lib/tests/behat/switch_editing_mode.feature new file mode 100644 index 00000000000..d8df9d44d55 --- /dev/null +++ b/lib/tests/behat/switch_editing_mode.feature @@ -0,0 +1,79 @@ +@core @turn_edit_mode_on @javascript +Feature: Turn editing mode on + Users should be able to turn editing mode on and off + + Background: + Given the following "courses" exist: + | fullname | shortname | + | Course 1 | C1 | + And the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "teacher1" + And I am on "Course 1" course homepage with editing mode on + And I turn editing mode off + And I log out + + Scenario: Edit mode on page Gradebook + Given the following "activities" exist: + | activity | course | idnumber | name | intro | + | assign | C1 | assign1 | Test Assignment 1 | Test Assignment 1 | + And I log in as "teacher1" + And I am on "Course 1" course homepage + And I navigate to "View > Grader report" in the course gradebook + And I turn editing mode on + And "Edit assign Test Assignment 1" "link" should exist + And I turn editing mode off + Then "Edit assign Test Assignment 1" "link" should not exist + + Scenario: Edit mode on page Course + And I log in as "teacher1" + And I am on "Course 1" course homepage + And I turn editing mode on + And I should see "Add an activity or resource" + And I turn editing mode off + Then I should not see "Add an activity or resource" + + Scenario: Edit mode on page Homepage + Given I log in as "admin" + And I am on site homepage + And I turn editing mode on + And I should see "Add an activity or resource" + And I turn editing mode off + Then I should not see "Add an activity or resource" + + Scenario: Edit mode on page Default profile + Given I log in as "admin" + And I navigate to "Appearance > Default profile page" in site administration + And I turn editing mode on + And I should see "Add a block" + And I turn editing mode off + Then I should not see "Add a block" + + Scenario: Edit mode on page Profile + Given I log in as "admin" + And I follow "View profile" + And I turn editing mode on + And I should see "Add a block" + And I turn editing mode off + Then I should not see "Add a block" + + Scenario: Edit mode on page Default dashboard + Given I log in as "admin" + And I navigate to "Appearance > Default Dashboard page" in site administration + And I turn editing mode on + And I should see "Add a block" + And I turn editing mode off + Then I should not see "Add a block" + + Scenario: Edit mode on page Dashboard + And I log in as "teacher1" + And I turn editing mode on + And I should see "Add a block" + Then I turn editing mode off + Then I should not see "Add a block" diff --git a/theme/classic/tests/behat/behat_theme_classic_behat_navigation.php b/theme/classic/tests/behat/behat_theme_classic_behat_navigation.php index d975b8b74ab..f664bbb913e 100644 --- a/theme/classic/tests/behat/behat_theme_classic_behat_navigation.php +++ b/theme/classic/tests/behat/behat_theme_classic_behat_navigation.php @@ -226,4 +226,38 @@ class behat_theme_classic_behat_navigation extends behat_navigation { return $menuxpath; } + + /** + * Turns editing mode off. + */ + public function i_turn_editing_mode_off(): void { + $buttonnames = [get_string('turneditingoff'), get_string('updatemymoodleoff'), get_string('blockseditoff')]; + foreach ($buttonnames as $buttonname) { + if ($editbutton = $this->getSession()->getPage()->findButton($buttonname)) { + $this->execute('behat_general::i_click_on', [$editbutton, 'NodeElement']); + return; + } + } + // Click the turneditingoff link in the Site Administration block. + if ($this->is_editing_on()) { + $this->execute('behat_general::i_click_on', [get_string('turneditingoff'), "link"]); + } + } + + /** + * Turns editing mode on. + */ + public function i_turn_editing_mode_on(): void { + $buttonnames = [get_string('turneditingon'), get_string('updatemymoodleon'), get_string('blocksediton')]; + foreach ($buttonnames as $buttonname) { + if ($editbutton = $this->getSession()->getPage()->findButton($buttonname)) { + $this->execute('behat_general::i_click_on', [$editbutton, 'NodeElement']); + return; + } + } + + if (!$this->is_editing_on()) { + $this->execute('behat_general::i_click_on', [get_string('turneditingon'), "link"]); + } + } }