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 related with admin presets.
19
 *
20
 * @package   tool_admin_presets
21
 * @category  test
22
 * @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
23
 * @author    Sylvain Revenu | Pimenko
24
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28
 
29
require_once(__DIR__ . '/../../../../../lib/behat/behat_deprecated_base.php');
30
require_once(__DIR__ . '/../../../../../lib/behat/behat_field_manager.php');
31
 
32
use Behat\Mink\Exception\ExpectationException as ExpectationException;
33
 
34
/**
35
 * Steps definitions that are now deprecated and will be removed in the next releases.
36
 *
37
 * @package   tool_admin_presets
38
 * @category  test
39
 * @copyright 2021 Pimenko <support@pimenko.com><pimenko.com>
40
 * @author    Sylvain Revenu | Pimenko
41
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 * @todo      MDL-78077 This will be deleted in Moodle 6.0.
43
 */
44
class behat_admin_presets_deprecated extends behat_deprecated_base {
45
 
46
    /**
47
     * Downloads the file from a specific link on the page and checks the size is in a given range.
48
     *
49
     * Only works if the link has an href attribute. Javascript downloads are
50
     * not supported. Currently, the href must be an absolute URL.
51
     *
52
     * The range includes the endpoints. That is, a 10 byte file in considered to
53
     * be between "5" and "10" bytes, and between "10" and "20" bytes.
54
     *
55
     * @deprecated since 5.0
56
     *
57
     * @Then /^following "(?P<link_string>[^"]*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should download between "(?P<min_bytes>\d+)" and "(?P<max_bytes>\d+)" bytes$/
58
     * @param string $link the text of the link.
59
     * @param string $selectortype The type of what we look for
60
     * @param string $nodeelement Element we look in
61
     * @param string $nodeselectortype The type of selector where we look in
62
     * @param int $minexpectedsize the minimum expected file size in bytes.
63
     * @param int $maxexpectedsize the maximum expected file size in bytes.
64
     * @return void
65
     * @throws ExpectationException
66
     */
67
    #[\core\attribute\deprecated('behat_admin_presets::following_in_the_should_download_between_and_bytes', since: '5.0')]
68
    final public function following_in_the_should_download_between_and_bytes(string $link, string $selectortype,
69
        string $nodeelement, string $nodeselectortype, int $minexpectedsize, int $maxexpectedsize): void {
70
 
71
        $this->deprecated_message("behat_admin_presets::following_in_the_should_download_between_and_bytes
72
            is deprecated. Use: the following element should download a file that:");
73
 
74
        // If the minimum is greater than the maximum then swap the values.
75
        if ((int) $minexpectedsize > (int) $maxexpectedsize) {
76
            list($minexpectedsize, $maxexpectedsize) = [$maxexpectedsize, $minexpectedsize];
77
        }
78
 
79
        $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession());
80
 
81
        // It will stop spinning once file is downloaded or time out.
82
        $result = $this->spin(
83
            function($context, $args) {
84
                return $this->download_file_from_link_within_node($args['selectortype'], $args['link'],
85
                    $args['nodeselectortype'], $args['nodeelement']);
86
            },
87
            [
88
                'selectortype' => $selectortype,
89
                'link' => $link,
90
                'nodeselectortype' => $nodeselectortype,
91
                'nodeelement' => $nodeelement
92
            ],
93
            behat_base::get_extended_timeout(),
94
            $exception
95
        );
96
 
97
        // Check download size.
98
        $actualsize = (int) strlen($result);
99
        if ($actualsize < $minexpectedsize || $actualsize > $maxexpectedsize) {
100
            throw new ExpectationException('Downloaded data was ' . $actualsize .
101
                ' bytes, expecting between ' . $minexpectedsize . ' and ' .
102
                $maxexpectedsize, $this->getSession());
103
        }
104
    }
105
 
106
    /**
107
     * Given the text of a link, download the linked file and return the contents.
108
     *
109
     * This is a helper method used by {@see following_in_the_should_download_between_and_bytes()}
110
     *
111
     * @param string $selectortype The type of what we look for
112
     * @param string $link the text of the link.
113
     * @param string $nodeselectortype The type of selector where we look in
114
     * @param string $nodeelement Element we look in
115
     * @return string the content of the downloaded file.
116
     */
117
    final public function download_file_from_link_within_node(string $selectortype, string $link,
118
        string $nodeselectortype, string $nodeelement): string {
119
        // Find the link from ur specific node.
120
        $linknode = $this->get_node_in_container($selectortype, $link, $nodeselectortype, $nodeelement);
121
        $this->ensure_node_is_visible($linknode);
122
 
123
        // Get the href and check it.
124
        $url = $linknode->getAttribute('href');
125
        if (!$url) {
126
            throw new ExpectationException('Download link does not have href attribute',
127
                $this->getSession());
128
        }
129
        if (!preg_match('~^https?://~', $url)) {
130
            throw new ExpectationException('Download link not an absolute URL: ' . $url,
131
                $this->getSession());
132
        }
133
 
134
        // Download the URL and check the size.
135
        $session = $this->getSession()->getCookie('MoodleSession');
136
        return download_file_content($url, ['Cookie' => 'MoodleSession=' . $session]);
137
    }
138
}