Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Steps definitions related to mod_feedback. 19 * 20 * @package mod_feedback 21 * @category test 22 * @copyright 2016 Marina Glancy 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. 27 28 require_once (__DIR__ . '/../../../../lib/behat/behat_base.php'); 29 30 use Behat\Gherkin\Node\TableNode as TableNode, 31 Behat\Mink\Exception\ExpectationException as ExpectationException; 32 33 /** 34 * Steps definitions related to mod_feedback. 35 * 36 * @copyright 2016 Marina Glancy 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class behat_mod_feedback extends behat_base { 40 41 /** 42 * Adds a question to the existing feedback with filling the form. 43 * 44 * The form for creating a question should be on one page. 45 * 46 * @When /^I add a "(?P<question_type_string>(?:[^"]|\\")*)" question to the feedback with:$/ 47 * @param string $questiontype 48 * @param TableNode $questiondata with data for filling the add question form 49 */ 50 public function i_add_question_to_the_feedback_with($questiontype, TableNode $questiondata) { 51 52 $questiontype = $this->escape($questiontype); 53 $this->execute('behat_forms::i_select_from_the_singleselect', array($questiontype, 'typ')); 54 55 // Wait again, for page to reloaded. 56 $this->execute('behat_general::i_wait_to_be_redirected'); 57 58 $rows = $questiondata->getRows(); 59 $modifiedrows = array(); 60 foreach ($rows as $row) { 61 foreach ($row as $key => $value) { 62 $row[$key] = preg_replace('|\\\\n|', "\n", $value); 63 } 64 $modifiedrows[] = $row; 65 } 66 $newdata = new TableNode($modifiedrows); 67 68 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $newdata); 69 70 $saveitem = $this->escape(get_string('save')); 71 $this->execute("behat_forms::press_button", $saveitem); 72 } 73 74 /** 75 * Adds a question to the existing feedback with filling the form. 76 * 77 * The form for creating a question should be on one page. 78 * 79 * @When /^I add a page break to the feedback$/ 80 */ 81 public function i_add_a_page_break_to_the_feedback() { 82 83 $questiontype = $this->escape(get_string('add_pagebreak', 'feedback')); 84 $this->execute('behat_forms::i_select_from_the_singleselect', array($questiontype, 'typ')); 85 86 // Wait again, for page to reloaded. 87 $this->execute('behat_general::i_wait_to_be_redirected'); 88 } 89 90 /** 91 * Quick way to generate answers to a one-page feedback. 92 * 93 * @When /^I log in as "(?P<user_name_string>(?:[^"]|\\")*)" and complete feedback "(?P<feedback_name_string>(?:[^"]|\\")*)" in course "(?P<course_name_string>(?:[^"]|\\")*)" with:$/ 94 * @param string $questiontype 95 * @param TableNode $questiondata with data for filling the add question form 96 */ 97 public function i_log_in_as_and_complete_feedback_in_course($username, $feedbackname, $coursename, TableNode $answers) { 98 $username = $this->escape($username); 99 $coursename = $this->escape($coursename); 100 $feedbackname = $this->escape($feedbackname); 101 $completeform = $this->escape(get_string('complete_the_form', 'feedback')); 102 103 // Log in as user. 104 $this->execute('behat_auth::i_log_in_as', $username); 105 106 // Navigate to feedback complete form. 107 $this->execute('behat_navigation::i_am_on_page_instance', [$feedbackname, 'feedback activity']); 108 $this->execute('behat_general::click_link', $completeform); 109 110 // Fill form and submit. 111 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $answers); 112 $this->execute("behat_forms::press_button", 'Submit your answers'); 113 114 // Log out. 115 $this->execute('behat_auth::i_log_out'); 116 } 117 118 /** 119 * Exports feedback and makes sure the export file is the same as in the fixture 120 * 121 * @Then /^following "(?P<link_string>(?:[^"]|\\")*)" should export feedback identical to "(?P<filename_string>(?:[^"]|\\")*)"$/ 122 * @param string $link 123 * @param string $filename 124 */ 125 public function following_should_export_feedback_identical_to($link, $filename) { 126 global $CFG; 127 $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession()); 128 129 // It will stop spinning once file is downloaded or time out. 130 $behatgeneralcontext = behat_context_helper::get('behat_general'); 131 $result = $this->spin( 132 function($context, $args) use ($behatgeneralcontext) { 133 $link = $args['link']; 134 return $behatgeneralcontext->download_file_from_link($link); 135 }, 136 array('link' => $link), 137 behat_base::get_extended_timeout(), 138 $exception 139 ); 140 141 $this->compare_exports(file_get_contents($CFG->dirroot . '/' . $filename), $result); 142 } 143 144 /** 145 * Clicks on Show chart data to display chart data if not visible. 146 * 147 * @Then /^I show chart data for the "(?P<feedback_name_string>(?:[^"]|\\")*)" feedback$/ 148 * @param string $feedbackname name of the feedback for which chart data needs to be shown. 149 */ 150 public function i_show_chart_data_for_the_feedback($feedbackname) { 151 152 $feedbackxpath = "//th[contains(normalize-space(string(.)), \"" . $feedbackname . "\")]/ancestor::table//" . 153 "div[contains(concat(' ', normalize-space(@class), ' '), ' chart-table ')]" . 154 "//p[contains(concat(' ', normalize-space(@class), ' '), ' chart-table-expand ') and ". 155 "//a[contains(normalize-space(string(.)), '".get_string('showchartdata')."')]]"; 156 157 $charttabledataxpath = $feedbackxpath . 158 "/following-sibling::div[contains(concat(' ', normalize-space(@class), ' '), ' chart-table-data ')][1]"; 159 160 // If chart data is not visible then expand. 161 $node = $this->get_selected_node("xpath_element", $charttabledataxpath); 162 if ($node) { 163 if ($node->getAttribute('aria-expanded') === 'false') { 164 $this->execute('behat_general::i_click_on_in_the', array( 165 get_string('showchartdata'), 166 'link', 167 $feedbackxpath, 168 'xpath_element' 169 )); 170 } 171 } 172 } 173 174 /** 175 * Ensures two feedback export files are identical 176 * 177 * Maps the itemids and converts DEPENDITEM if necessary 178 * 179 * Throws ExpectationException if exports are different 180 * 181 * @param string $expected 182 * @param string $actual 183 * @throws ExpectationException 184 */ 185 protected function compare_exports($expected, $actual) { 186 $dataexpected = xmlize($expected, 1, 'UTF-8'); 187 $dataexpected = $dataexpected['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM']; 188 $dataactual = xmlize($actual, 1, 'UTF-8'); 189 $dataactual = $dataactual['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM']; 190 191 if (count($dataexpected) != count($dataactual)) { 192 throw new ExpectationException('Expected ' . count($dataexpected) . 193 ' items in the export file, found ' . count($dataactual), $this->getSession()); 194 } 195 196 $itemmapping = array(); 197 $itemactual = reset($dataactual); 198 foreach ($dataexpected as $idx => $itemexpected) { 199 // Map ITEMID and DEPENDITEM. 200 $itemmapping[intval($itemactual['#']['ITEMID'][0]['#'])] = intval($itemexpected['#']['ITEMID'][0]['#']); 201 $itemactual['#']['ITEMID'][0]['#'] = $itemexpected['#']['ITEMID'][0]['#']; 202 $expecteddependitem = $actualdependitem = 0; 203 if (isset($itemexpected['#']['DEPENDITEM'][0]['#'])) { 204 $expecteddependitem = intval($itemexpected['#']['DEPENDITEM'][0]['#']); 205 } 206 if (isset($itemactual['#']['DEPENDITEM'][0]['#'])) { 207 $actualdependitem = intval($itemactual['#']['DEPENDITEM'][0]['#']); 208 } 209 if ($expecteddependitem && !$actualdependitem) { 210 throw new ExpectationException('Expected DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession()); 211 } 212 if (!$expecteddependitem && $actualdependitem) { 213 throw new ExpectationException('Unexpected DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession()); 214 } 215 if ($expecteddependitem && $actualdependitem) { 216 if (!isset($itemmapping[$actualdependitem]) || $itemmapping[$actualdependitem] != $expecteddependitem) { 217 throw new ExpectationException('Unknown DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession()); 218 } 219 $itemactual['#']['DEPENDITEM'][0]['#'] = $itemexpected['#']['DEPENDITEM'][0]['#']; 220 } 221 // Now, after mapping, $itemexpected should be exactly the same as $itemactual. 222 if (json_encode($itemexpected) !== json_encode($itemactual)) { 223 throw new ExpectationException('Actual ' . ($idx + 1) . 'th item does not match expected', $this->getSession()); 224 } 225 // Get the next itemactual. 226 $itemactual = next($dataactual); 227 } 228 } 229 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body