Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 $additem = $this->escape(get_string('add_item', 'feedback')); 54 55 $this->execute('behat_forms::i_select_from_the_singleselect', array($questiontype, $additem)); 56 57 // Wait again, for page to reloaded. 58 $this->execute('behat_general::i_wait_to_be_redirected'); 59 60 $rows = $questiondata->getRows(); 61 $modifiedrows = array(); 62 foreach ($rows as $row) { 63 foreach ($row as $key => $value) { 64 $row[$key] = preg_replace('|\\\\n|', "\n", $value); 65 } 66 $modifiedrows[] = $row; 67 } 68 $newdata = new TableNode($modifiedrows); 69 70 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $newdata); 71 72 $saveitem = $this->escape(get_string('save_item', 'feedback')); 73 $this->execute("behat_forms::press_button", $saveitem); 74 } 75 76 /** 77 * Adds a question to the existing feedback with filling the form. 78 * 79 * The form for creating a question should be on one page. 80 * 81 * @When /^I add a page break to the feedback$/ 82 */ 83 public function i_add_a_page_break_to_the_feedback() { 84 85 $questiontype = $this->escape(get_string('add_pagebreak', 'feedback')); 86 $additem = $this->escape(get_string('add_item', 'feedback')); 87 88 $this->execute('behat_forms::i_select_from_the_singleselect', array($questiontype, $additem)); 89 90 // Wait again, for page to reloaded. 91 $this->execute('behat_general::i_wait_to_be_redirected'); 92 } 93 94 /** 95 * Quick way to generate answers to a one-page feedback. 96 * 97 * @When /^I log in as "(?P<user_name_string>(?:[^"]|\\")*)" and complete feedback "(?P<feedback_name_string>(?:[^"]|\\")*)" in course "(?P<course_name_string>(?:[^"]|\\")*)" with:$/ 98 * @param string $questiontype 99 * @param TableNode $questiondata with data for filling the add question form 100 */ 101 public function i_log_in_as_and_complete_feedback_in_course($username, $feedbackname, $coursename, TableNode $answers) { 102 $username = $this->escape($username); 103 $coursename = $this->escape($coursename); 104 $feedbackname = $this->escape($feedbackname); 105 $completeform = $this->escape(get_string('complete_the_form', 'feedback')); 106 107 // Log in as user. 108 $this->execute('behat_auth::i_log_in_as', $username); 109 110 // Navigate to feedback complete form. 111 $this->execute('behat_navigation::i_am_on_page_instance', [$feedbackname, 'feedback activity']); 112 $this->execute('behat_general::click_link', $completeform); 113 114 // Fill form and submit. 115 $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $answers); 116 $this->execute("behat_forms::press_button", 'Submit your answers'); 117 118 // Log out. 119 $this->execute('behat_auth::i_log_out'); 120 } 121 122 /** 123 * Exports feedback and makes sure the export file is the same as in the fixture 124 * 125 * @Then /^following "(?P<link_string>(?:[^"]|\\")*)" should export feedback identical to "(?P<filename_string>(?:[^"]|\\")*)"$/ 126 * @param string $link 127 * @param string $filename 128 */ 129 public function following_should_export_feedback_identical_to($link, $filename) { 130 global $CFG; 131 $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession()); 132 133 // It will stop spinning once file is downloaded or time out. 134 $behatgeneralcontext = behat_context_helper::get('behat_general'); 135 $result = $this->spin( 136 function($context, $args) use ($behatgeneralcontext) { 137 $link = $args['link']; 138 return $behatgeneralcontext->download_file_from_link($link); 139 }, 140 array('link' => $link), 141 behat_base::get_extended_timeout(), 142 $exception 143 ); 144 145 $this->compare_exports(file_get_contents($CFG->dirroot . '/' . $filename), $result); 146 } 147 148 /** 149 * Clicks on Show chart data to display chart data if not visible. 150 * 151 * @Then /^I show chart data for the "(?P<feedback_name_string>(?:[^"]|\\")*)" feedback$/ 152 * @param string $feedbackname name of the feedback for which chart data needs to be shown. 153 */ 154 public function i_show_chart_data_for_the_feedback($feedbackname) { 155 156 $feedbackxpath = "//th[contains(normalize-space(string(.)), \"" . $feedbackname . "\")]/ancestor::table//" . 157 "div[contains(concat(' ', normalize-space(@class), ' '), ' chart-table ')]" . 158 "//p[contains(concat(' ', normalize-space(@class), ' '), ' chart-table-expand ') and ". 159 "//a[contains(normalize-space(string(.)), '".get_string('showchartdata')."')]]"; 160 161 $charttabledataxpath = $feedbackxpath . 162 "/following-sibling::div[contains(concat(' ', normalize-space(@class), ' '), ' chart-table-data ')][1]"; 163 164 // If chart data is not visible then expand. 165 $node = $this->get_selected_node("xpath_element", $charttabledataxpath); 166 if ($node) { 167 if ($node->getAttribute('aria-expanded') === 'false') { 168 $this->execute('behat_general::i_click_on_in_the', array( 169 get_string('showchartdata'), 170 'link', 171 $feedbackxpath, 172 'xpath_element' 173 )); 174 } 175 } 176 } 177 178 /** 179 * Ensures two feedback export files are identical 180 * 181 * Maps the itemids and converts DEPENDITEM if necessary 182 * 183 * Throws ExpectationException if exports are different 184 * 185 * @param string $expected 186 * @param string $actual 187 * @throws ExpectationException 188 */ 189 protected function compare_exports($expected, $actual) { 190 $dataexpected = xmlize($expected, 1, 'UTF-8'); 191 $dataexpected = $dataexpected['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM']; 192 $dataactual = xmlize($actual, 1, 'UTF-8'); 193 $dataactual = $dataactual['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM']; 194 195 if (count($dataexpected) != count($dataactual)) { 196 throw new ExpectationException('Expected ' . count($dataexpected) . 197 ' items in the export file, found ' . count($dataactual), $this->getSession()); 198 } 199 200 $itemmapping = array(); 201 $itemactual = reset($dataactual); 202 foreach ($dataexpected as $idx => $itemexpected) { 203 // Map ITEMID and DEPENDITEM. 204 $itemmapping[intval($itemactual['#']['ITEMID'][0]['#'])] = intval($itemexpected['#']['ITEMID'][0]['#']); 205 $itemactual['#']['ITEMID'][0]['#'] = $itemexpected['#']['ITEMID'][0]['#']; 206 $expecteddependitem = $actualdependitem = 0; 207 if (isset($itemexpected['#']['DEPENDITEM'][0]['#'])) { 208 $expecteddependitem = intval($itemexpected['#']['DEPENDITEM'][0]['#']); 209 } 210 if (isset($itemactual['#']['DEPENDITEM'][0]['#'])) { 211 $actualdependitem = intval($itemactual['#']['DEPENDITEM'][0]['#']); 212 } 213 if ($expecteddependitem && !$actualdependitem) { 214 throw new ExpectationException('Expected DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession()); 215 } 216 if (!$expecteddependitem && $actualdependitem) { 217 throw new ExpectationException('Unexpected DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession()); 218 } 219 if ($expecteddependitem && $actualdependitem) { 220 if (!isset($itemmapping[$actualdependitem]) || $itemmapping[$actualdependitem] != $expecteddependitem) { 221 throw new ExpectationException('Unknown DEPENDITEM in ' . ($idx + 1) . 'th item', $this->getSession()); 222 } 223 $itemactual['#']['DEPENDITEM'][0]['#'] = $itemexpected['#']['DEPENDITEM'][0]['#']; 224 } 225 // Now, after mapping, $itemexpected should be exactly the same as $itemactual. 226 if (json_encode($itemexpected) !== json_encode($itemactual)) { 227 throw new ExpectationException('Actual ' . ($idx + 1) . 'th item does not match expected', $this->getSession()); 228 } 229 // Get the next itemactual. 230 $itemactual = next($dataactual); 231 } 232 } 233 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body