See Release Notes
Long Term Support Release
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_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 related with admin presets. 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 */ 43 class behat_admin_presets extends behat_base { 44 45 /** 46 * Downloads the file from a specific link on the page and checks the size is in a given range. 47 * 48 * Only works if the link has an href attribute. Javascript downloads are 49 * not supported. Currently, the href must be an absolute URL. 50 * 51 * The range includes the endpoints. That is, a 10 byte file in considered to 52 * be between "5" and "10" bytes, and between "10" and "20" bytes. 53 * 54 * @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$/ 55 * @param string $link the text of the link. 56 * @param string $selectortype The type of what we look for 57 * @param string $nodeelement Element we look in 58 * @param string $nodeselectortype The type of selector where we look in 59 * @param int $minexpectedsize the minimum expected file size in bytes. 60 * @param int $maxexpectedsize the maximum expected file size in bytes. 61 * @return void 62 * @throws ExpectationException 63 */ 64 final public function following_in_the_should_download_between_and_bytes(string $link, string $selectortype, 65 string $nodeelement, string $nodeselectortype, int $minexpectedsize, int $maxexpectedsize): void { 66 // If the minimum is greater than the maximum then swap the values. 67 if ((int) $minexpectedsize > (int) $maxexpectedsize) { 68 list($minexpectedsize, $maxexpectedsize) = [$maxexpectedsize, $minexpectedsize]; 69 } 70 71 $exception = new ExpectationException('Error while downloading data from ' . $link, $this->getSession()); 72 73 // It will stop spinning once file is downloaded or time out. 74 $result = $this->spin( 75 function($context, $args) { 76 return $this->download_file_from_link_within_node($args['selectortype'], $args['link'], 77 $args['nodeselectortype'], $args['nodeelement']); 78 }, 79 [ 80 'selectortype' => $selectortype, 81 'link' => $link, 82 'nodeselectortype' => $nodeselectortype, 83 'nodeelement' => $nodeelement 84 ], 85 behat_base::get_extended_timeout(), 86 $exception 87 ); 88 89 // Check download size. 90 $actualsize = (int) strlen($result); 91 if ($actualsize < $minexpectedsize || $actualsize > $maxexpectedsize) { 92 throw new ExpectationException('Downloaded data was ' . $actualsize . 93 ' bytes, expecting between ' . $minexpectedsize . ' and ' . 94 $maxexpectedsize, $this->getSession()); 95 } 96 } 97 98 /** 99 * Given the text of a link, download the linked file and return the contents. 100 * 101 * This is a helper method used by {@see following_in_the_should_download_between_and_bytes()} 102 * 103 * @param string $selectortype The type of what we look for 104 * @param string $link the text of the link. 105 * @param string $nodeselectortype The type of selector where we look in 106 * @param string $nodeelement Element we look in 107 * @return string the content of the downloaded file. 108 */ 109 final public function download_file_from_link_within_node(string $selectortype, string $link, 110 string $nodeselectortype, string $nodeelement): string { 111 // Find the link from ur specific node. 112 $linknode = $this->get_node_in_container($selectortype, $link, $nodeselectortype, $nodeelement); 113 $this->ensure_node_is_visible($linknode); 114 115 // Get the href and check it. 116 $url = $linknode->getAttribute('href'); 117 if (!$url) { 118 throw new ExpectationException('Download link does not have href attribute', 119 $this->getSession()); 120 } 121 if (!preg_match('~^https?://~', $url)) { 122 throw new ExpectationException('Download link not an absolute URL: ' . $url, 123 $this->getSession()); 124 } 125 126 // Download the URL and check the size. 127 $session = $this->getSession()->getCookie('MoodleSession'); 128 return download_file_content($url, ['Cookie' => 'MoodleSession=' . $session]); 129 } 130 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body