Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 to verify a downloaded file.
19
 *
20
 * @package    core
21
 * @category   test
22
 * @copyright  2024 Simey Lameze <simey@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
use Behat\Gherkin\Node\TableNode;
27
use Behat\Mink\Exception\ExpectationException;
28
 
29
require_once(__DIR__ . '/../../behat/behat_base.php');
30
 
31
/**
32
 * Steps definitions to verify a downloaded file.
33
 *
34
 * @package    core
35
 * @category   test
36
 * @copyright  2024 Simey Lameze <simey@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class behat_download extends behat_base {
40
 
41
    /**
42
     * Downloads the file from a link on the page and verify the type and content.
43
     *
44
     * @Then following :link_text should download a file that:
45
     *
46
     * @param string $linktext the text of the link.
47
     * @param TableNode $table the table of assertions to use the check the file contents.
48
     * @throws ExpectationException if the file cannot be downloaded, or if the download does not pass all the checks.
49
     */
50
    public function following_should_download_a_file_that(string $linktext, TableNode $table): void {
51
        $this->following_in_element_should_download_a_file_that($linktext, '', '', $table);
52
    }
53
 
54
    /**
55
     * Downloads the file from a link on the page and verify the type and content.
56
     *
57
     * @Then following :link_text in the :element_container_string :text_selector_string should download a file that:
58
     *
59
     * @param string $linktext the text of the link.
60
     * @param string $containerlocator the container element.
61
     * @param string $containertype the container selector type.
62
     * @param TableNode $table the table of assertions to use the check the file contents.
63
     * @throws ExpectationException if the file cannot be downloaded, or if the download does not pass all the checks.
64
     */
65
    public function following_in_element_should_download_a_file_that(string $linktext, string $containerlocator,
66
            string $containertype, TableNode $table): void {
67
 
68
        $filecontent = $this->download_file($linktext, $containerlocator, $containertype);
69
        $this->verify_file_content($filecontent, $table);
70
    }
71
 
72
    /**
73
     * Download a file from the given link.
74
     *
75
     * @param string $linktext the text of the link.
76
     * @param string $containerlocator the container element.
77
     * @param string $containertype the container selector type.
78
     * @return string the file contents.
79
     * @throws ExpectationException if the download fails.
80
     */
81
    protected function download_file(string $linktext, string $containerlocator, string $containertype): string {
82
        return behat_context_helper::get('behat_general')->download_file_from_link($linktext, $containerlocator, $containertype);
83
    }
84
 
85
    /**
86
     * Checks the content of the downloaded file.
87
     *
88
     * @param string $filecontent the content of the file.
89
     * @param TableNode $table the table of assertions to check.
90
     * @throws ExpectationException if the file content does not pass all the checks.
91
     */
92
    private function verify_file_content(string $filecontent, TableNode $table): void {
93
        foreach ($table->getRows() as $row) {
94
            switch (strtolower(trim($row[0]))) {
95
                case 'contains text':
96
                    $this->verify_file_contains_text($filecontent, $row[1]);
97
                    break;
98
                case 'contains text in xml element':
99
                    $this->verify_xml_element_contains($filecontent, $row[1]);
100
                    break;
101
                case 'has mimetype':
102
                    $this->verify_file_mimetype($filecontent, $row[1]);
103
                    break;
104
                case 'contains file in zip':
105
                    $this->verify_zip_file_content($filecontent, $row[1]);
106
                    break;
107
                default:
108
                    throw new ExpectationException(
109
                        'Invalid type of file assertion: ' . $row[0], $this->getSession());
110
            }
111
        }
112
    }
113
 
114
    /**
115
     * Validates the downloaded file appears to be of the mimetype.
116
     *
117
     * @param string $filecontent the content of the file.
118
     * @param string $expectedmimetype the expected file mimetype e.g. 'application/xml'.
119
     * @throws ExpectationException if the file does not appear to be of the expected type.
120
     */
121
    protected function verify_file_mimetype(string $filecontent, string $expectedmimetype): void {
122
 
123
        $finfo = new finfo(FILEINFO_MIME_TYPE);
124
        $actualmimetype = $finfo->buffer($filecontent);
125
 
126
        if ($actualmimetype !== $expectedmimetype) {
127
            throw new ExpectationException(
128
                "The file downloaded should have been a $expectedmimetype file, " .
129
                "but got $actualmimetype instead.",
130
                $this->getSession(),
131
            );
132
        }
133
    }
134
 
135
    /**
136
     * Asserts that the given string is present in the file content.
137
     *
138
     * @param string $filecontent the content of the file.
139
     * @param string $expectedcontent the string to search for.
140
     * @throws ExpectationException if verification fails.
141
     */
142
    protected function verify_file_contains_text(string $filecontent, string $expectedcontent): void {
143
        if (!str_contains($filecontent, $expectedcontent)) {
144
            throw new ExpectationException(
145
                "The string '$expectedcontent' was not found in the file content.",
146
                $this->getSession(),
147
            );
148
        }
149
    }
150
 
151
    /**
152
     * Asserts that the given XML file is valid and contains the expected string.
153
     *
154
     * @param string $filecontent the content of the file.
155
     * @param string $expectedcontent the string to search for.
156
     * @throws ExpectationException
157
     */
158
    protected function verify_xml_element_contains(string $filecontent, string $expectedcontent): void {
159
        $xml = new SimpleXMLElement($filecontent);
160
        $result = $xml->xpath("//*[contains(text(), '$expectedcontent')]");
161
 
162
        if (empty($result)) {
163
            throw new ExpectationException(
164
                "The string '$expectedcontent' was not found in the content of any element in this XML file.",
165
                $this->getSession(),
166
            );
167
        }
168
    }
169
 
170
    /**
171
     * Save the downloaded file to tempdir and return the path.
172
     *
173
     * @param string $filecontent the content of the file.
174
     * @param string $fileextension the expected file type, given as a file extension, e.g. 'txt', 'xml'.
175
     * @return string path where the file was saved temporarily.
176
     */
177
    protected function save_to_temp_file(string $filecontent, string $fileextension): string {
178
        // Then perform additional image-specific validations.
179
        $tempdir = make_request_directory();
180
        $filepath = $tempdir . '/downloaded.' . $fileextension;
181
        file_put_contents($filepath, $filecontent);
182
        return $filepath;
183
    }
184
 
185
    /**
186
     * Asserts that the given zip archive contains the expected file(s).
187
     *
188
     * @param string $filecontent the content of the file.
189
     * @param string $expectedfile the name of the file to search for.
190
     * @throws ExpectationException if the zip file does not contain the expected files.
191
     */
192
    protected function verify_zip_file_content(string $filecontent, string $expectedfile): void {
193
 
194
        $zip = new ZipArchive();
195
        $res = $zip->open($this->save_to_temp_file($filecontent, 'zip'));
196
 
197
        if ($res !== true) {
198
            throw new ExpectationException(
199
                "Failed to open zip file.",
200
                $this->getSession(),
201
            );
202
        }
203
 
204
        if ($zip->locateName($expectedfile) === false) {
205
            throw new ExpectationException(
206
                "The file '$expectedfile' was not found in the downloaded zip archive.",
207
                $this->getSession(),
208
            );
209
        }
210
    }
211
}