Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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