public function i_set_the_following_administration_settings_values(TableNode $table) { if (!($data = $table->getRowsHash())) { return; } foreach ($data as $label => $value) { // We expect admin block to be visible, otherwise go to homepage. if (!$this->getSession()->getPage()->find('css', '.block_settings')) { $this->getSession()->visit($this->locate_path('/')); $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS); } // Search by label. $searchbox = $this->find_field(get_string('searchinsettings', 'admin')); $searchbox->setValue($label); $submitsearch = $this->find('css', 'form.adminsearchform input[type=submit]'); $submitsearch->press(); $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS); // Admin settings does not use the same DOM structure than other moodle forms // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements. $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting '); // The argument should be converted to an xpath literal. $label = behat_context_helper::escape($label); // Single element settings. try { $fieldxpath = "//*[self::input | self::textarea | self::select]" . "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" . "[@id=//label[contains(normalize-space(.), {$label})]/@for or " . "@id=//span[contains(normalize-space(.), {$label})]/preceding-sibling::label[1]/@for]"; $fieldnode = $this->find('xpath', $fieldxpath, $exception); $formfieldtypenode = $this->find('xpath', $fieldxpath . "/ancestor::div[contains(concat(' ', @class, ' '), ' form-setting ')]" . "/child::div[contains(concat(' ', @class, ' '), ' form-')]/child::*/parent::div"); } catch (ElementNotFoundException $e) { // Multi element settings, interacting only the first one. $fieldxpath = "//*[label[contains(., {$label})]|span[contains(., {$label})]]" . "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" . "/descendant::div[contains(concat(' ', @class, ' '), ' form-group ')]" . "/descendant::*[self::input | self::textarea | self::select]" . "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]"; $fieldnode = $this->find('xpath', $fieldxpath); // It is the same one that contains the type. $formfieldtypenode = $fieldnode; } // Getting the class which contains the field type. $classes = explode(' ', $formfieldtypenode->getAttribute('class')); $type = false; foreach ($classes as $class) { if (substr($class, 0, 5) == 'form-') { $type = substr($class, 5); } } // Instantiating the appropiate field type. $field = behat_field_manager::get_field_instance($type, $fieldnode, $this->getSession()); $field->set_value($value); $this->find_button(get_string('savechanges'))->press(); } }
/** * Sets the specified site settings. A table with | Setting label | value | is expected. * * @Given /^I set the following administration settings values:$/ * @param TableNode $table */ public function i_set_the_following_administration_settings_values(TableNode $table) { if (!($data = $table->getRowsHash())) { return; } foreach ($data as $label => $value) { // We expect admin block to be visible, otherwise go to homepage. if (!$this->getSession()->getPage()->find('css', '.block_settings')) { $this->getSession()->visit($this->locate_path('/')); $this->wait(self::TIMEOUT, '(document.readyState === "complete")'); } // Search by label. $searchbox = $this->find_field('Search in settings'); $searchbox->setValue($label); $submitsearch = $this->find('css', 'form.adminsearchform input[type=submit]'); $submitsearch->press(); $this->wait(self::TIMEOUT, '(document.readyState === "complete")'); // Admin settings does not use the same DOM structure than other moodle forms // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements. $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting '); $fieldxpath = "//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" . "[@id=//label[contains(normalize-space(string(.)), '" . $label . "')]/@for]"; $fieldnode = $this->find('xpath', $fieldxpath, $exception); $formfieldtypenode = $this->find('xpath', $fieldxpath . "/ancestor::div[@class='form-setting']" . "/child::div[contains(concat(' ', @class, ' '), ' form-')]/child::*/parent::div"); // Getting the class which contains the field type. $classes = explode(' ', $formfieldtypenode->getAttribute('class')); foreach ($classes as $class) { if (substr($class, 0, 5) == 'form-') { $type = substr($class, 5); } } // Instantiating the appropiate field type. $field = behat_field_manager::get_field_instance($type, $fieldnode, $this->getSession()); $field->set_value($value); $this->find_button('Save changes')->press(); } }
/** * Checks grade values with or without a edit box. * * @Then /^the grade for "([^"]*)" in grade item "([^"]*)" should match "([^"]*)"$/ * @throws Exception * @throws ElementNotFoundException * @param string $student * @param string $itemname * @param string $value * @return Then */ public function the_grade_should_match($student, $itemname, $value) { $xpath = $this->get_student_and_grade_value_selector($student, $itemname); $gradefield = $this->getSession()->getPage()->find('xpath', $xpath); if (!empty($gradefield)) { // Get the field. $fieldtype = behat_field_manager::guess_field_type($gradefield, $this->getSession()); if (!$fieldtype) { throw new Exception('Could not get field type for grade field "' . $itemname . '"'); } $field = behat_field_manager::get_field_instance($fieldtype, $gradefield, $this->getSession()); if (!$field->matches($value)) { $fieldvalue = $field->get_value(); throw new ExpectationException('The "' . $student . '" and "' . $itemname . '" grade is "' . $fieldvalue . '", "' . $value . '" expected', $this->getSession()); } } else { // If there isn't a form field, just search for contents. $valueliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($value); $xpath = $this->get_student_and_grade_cell_selector($student, $itemname); $xpath .= "[contains(normalize-space(.)," . $valueliteral . ")]"; $node = $this->getSession()->getDriver()->find($xpath); if (empty($node)) { $locatorexceptionmsg = 'Cell for "' . $student . '" and "' . $itemname . '" with value "' . $value . '"'; throw new ElementNotFoundException($this->getSession(), $locatorexceptionmsg, null, $xpath); } } }
/** * Fills the advanced permissions form with the provided data. Expects a table with capability name and permission (Inherit/Allow/Prevent/Prohibit) columns. * @Given /^I fill the capabilities form with the following permissions:$/ * @param TableNode $table * @return void */ public function i_fill_the_capabilities_form_with_the_following_permissions($table) { // Ensure we are using the advanced view. // Wrapped in a try/catch to capture the exception and continue execution, we don't know if advanced mode was already enabled. try { $advancedtoggle = $this->find_button(get_string('showadvanced', 'form')); if ($advancedtoggle) { $advancedtoggle->click(); // Wait for the page to load. $this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS); } } catch (Exception $e) { // We already are in advanced mode. } // Using getRows() as we are not sure if tests writers will add the header. foreach ($table->getRows() as $key => $row) { if (count($row) !== 2) { throw new ExpectationException('You should specify a table with capability/permission columns', $this->getSession()); } list($capability, $permission) = $row; // Skip the headers row if it was provided if (strtolower($capability) == 'capability' || strtolower($capability) == 'capabilities') { continue; } // Checking the permission value. $permissionconstant = 'CAP_' . strtoupper($permission); if (!defined($permissionconstant)) { throw new ExpectationException('The provided permission value "' . $permission . '" is not valid. Use Inherit, Allow, Prevent or Prohibited', $this->getSession()); } // Converting from permission to constant value. $permissionvalue = constant($permissionconstant); // Here we wait for the element to appear and exception if it does not exist. $radio = $this->find('xpath', '//input[@name="' . $capability . '" and @value="' . $permissionvalue . '"]'); $field = behat_field_manager::get_field_instance('radio', $radio, $this->getSession()); $field->set_value(1); } }