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