MDL-43156 core_form: fix handling of fixture form
authorDavo Smith <moodle@davosmith.co.uk>
Sat, 4 Apr 2020 13:18:56 +0000 (14:18 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 30 Apr 2020 18:39:24 +0000 (20:39 +0200)
Also moving it from behat_forms to behat_navigation

lib/form/tests/behat/fixtures/repeat_defaults_form.php
lib/form/tests/behat/repeat_defaults.feature
lib/tests/behat/behat_forms.php
lib/tests/behat/behat_navigation.php

index 1e88cc4..13748ed 100644 (file)
@@ -18,6 +18,7 @@
  * Test form repeat elements + defaults
  *
  * @copyright 2020 Davo Smith, Synergy Learning
+ * @package   core_form
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
@@ -27,10 +28,18 @@ defined('BEHAT_SITE_RUNNING') || die();
 
 global $CFG, $PAGE, $OUTPUT;
 require_once($CFG->libdir.'/formslib.php');
-$PAGE->set_url('/lib/form/tests/behat/fixtures/formtest.php');
+$PAGE->set_url('/lib/form/tests/behat/fixtures/repeat_defaults_form.php');
+require_login();
 $PAGE->set_context(context_system::instance());
 
+/**
+ * Class repeat_defaults_form
+ * @package core_form
+ */
 class repeat_defaults_form extends moodleform {
+    /**
+     * Form definition
+     */
     public function definition() {
         $mform = $this->_form;
         $repeatcount = $this->_customdata['repeatcount'];
index bb88179..25e0385 100644 (file)
@@ -1,8 +1,9 @@
-@core_form @javascript
+@core_form
 Feature: Newly created repeat elements have the correct default values
 
   Scenario: Clicking button to add repeat elements creates repeat elements with the correct default values
-    Given I am on the repeat defaults form page
+    Given I log in as "admin"
+    And I am on fixture page "/lib/form/tests/behat/fixtures/repeat_defaults_form.php"
     When I press "Add repeats"
     Then the following fields match these values:
       | testcheckbox[1]           | 1           |
index 944c7e1..d81686e 100644 (file)
@@ -731,13 +731,4 @@ class behat_forms extends behat_base {
         $node = $this->get_node_in_container('xpath_element', $xpathtarget, 'form_row', $field);
         $this->ensure_node_is_visible($node);
     }
-
-    /**
-     * Visit the fixture page for testing repeat defaults.
-     * @Given /^I am on the repeat defaults form page$/
-     */
-    public function i_am_on_the_repeat_defaults_form_page() {
-        $url = new moodle_url('/lib/form/tests/behat/fixtures/repeat_defaults_form.php');
-        $this->getSession()->visit($this->locate_path($url->out_as_local_url(false)));
-    }
 }
index 002ab61..a452734 100644 (file)
@@ -962,4 +962,21 @@ class behat_navigation extends behat_base {
         throw new ElementNotFoundException($this->getSession(),
                 'Link "' . join(' > ', $nodelist) . '" in the current page edit menu"');
     }
+
+    /**
+     * Visit a fixture page for testing stuff that is not available in core.
+     *
+     * Please always, to prevent unwanted requests, protect behat fixture files with:
+     *     defined('BEHAT_SITE_RUNNING') || die();
+     *
+     * @Given /^I am on fixture page "(?P<url_string>(?:[^"]|\\")*)"$/
+     * @param string $url local path to fixture page
+     */
+    public function i_am_on_fixture_page($url) {
+        $fixtureregex = '|^/[a-z0-9_\-/]*/tests/behat/fixtures/[a-z0-9_\-]*\.php$|';
+        if (!preg_match($fixtureregex, $url)) {
+            throw new coding_exception("URL {$url} is not a fixture URL");
+        }
+        $this->getSession()->visit($this->locate_path($url));
+    }
 }