forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-63399 behat: implement a new download step
- Loading branch information
Showing
24 changed files
with
263 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Steps definitions to verify a downloaded file. | ||
* | ||
* @package core | ||
* @category test | ||
* @copyright 2023 Simey Lameze <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
use Behat\Gherkin\Node\TableNode; | ||
use Behat\Mink\Exception\ExpectationException; | ||
|
||
require_once(__DIR__ . '/../../behat/behat_base.php'); | ||
|
||
/** | ||
* Steps definitions to verify a downloaded file. | ||
* | ||
* @package core | ||
* @category test | ||
* @copyright 2023 Simey Lameze <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class behat_download extends behat_base { | ||
|
||
/** | ||
* Downloads the file from a link on the page and checks the size is in a given range. | ||
* | ||
* @Then /^following "(?P<link_string>[^"]*)" should download a "(?P<format_string>[^"]*)" file that:$/ | ||
* | Contains | abc | | ||
* | Does not contain | xyz | | ||
*/ | ||
public function following_should_download_a_file_that(string $link, string $format, TableNode $table): void { | ||
|
||
// Find the link. | ||
$linknode = $this->find_link($link); | ||
$this->ensure_node_is_visible($linknode); | ||
|
||
// Get the href and check it. | ||
$url = $linknode->getAttribute('href'); | ||
if (!$url) { | ||
throw new ExpectationException( | ||
'Download link does not have href attribute', | ||
$this->getSession(), | ||
); | ||
} | ||
if (!preg_match('~^https?://~', $url)) { | ||
throw new ExpectationException( | ||
"Download link not an absolute URL: {$url}", | ||
$this->getSession(), | ||
); | ||
} | ||
|
||
$behatgeneralcontext = behat_context_helper::get('behat_general'); | ||
$exception = new ExpectationException( | ||
"Error while downloading data from {$link}", | ||
$this->getSession(), | ||
); | ||
|
||
// It will stop spinning once file is downloaded or time out. | ||
$filecontent = $this->spin( | ||
fn($context, $link) => $behatgeneralcontext->download_file_from_link($link), | ||
$link, | ||
behat_base::get_extended_timeout(), | ||
$exception | ||
); | ||
|
||
if (!$rows = $table->getRows()) { | ||
// Nothing further to verify. | ||
return; | ||
} | ||
|
||
// TODO: | ||
// 2) Assert multiple files in the zip archive. | ||
// 3) Validate txt, gift and png files. | ||
// 4) Validate "Does not contain." | ||
// 5) Validate "Contains" with multiple strings. | ||
// 6) Validate "Does not contain PHP errors" -> (which can be based on look_for_exceptions from lib/behat/classes/behat_session_trait.php | ||
$this->validate_mime_type($filecontent, $format); | ||
|
||
$assertmethod = 'assert_file_type_' . strtolower($format); | ||
foreach ($rows as $row) { | ||
$assertion = strtolower(trim($row[0])); | ||
match ($assertion) { | ||
'contains' => $this->$assertmethod($filecontent, $row[1]), | ||
default => throw new ExpectationException( | ||
"Invalid assertion: {$assertion}", | ||
$this->getSession(), | ||
) | ||
}; | ||
} | ||
} | ||
|
||
protected function validate_mime_type(string $filecontent, string $format): void { | ||
|
||
$finfo = new \finfo(FILEINFO_MIME_TYPE); | ||
$mimetype = $finfo->buffer($filecontent); | ||
|
||
$validmimetypes = match ($format) { | ||
'zip' => ['application/zip', 'application/x-zip-compressed', 'multipart/x-zip'], | ||
'xml' => ['application/xml', 'text/xml'], | ||
'txt' => ['text/plain'], | ||
'gift' => ['text/plain'], | ||
'aiken' => ['text/plain'], | ||
'png' => ['image/png'], | ||
default => [$format], // If the format is not one of the above, assume it is the only valid MIME type | ||
}; | ||
|
||
if (!in_array($mimetype, $validmimetypes)) { | ||
throw new ExpectationException( | ||
"The file downloaded should have been a {$format} file, but got {$mimetype} instead.", | ||
$this->getSession(), | ||
); | ||
} | ||
} | ||
|
||
protected function assert_file_type_xml(string $filecontent, string $contains): void { | ||
|
||
// Load the XML content into a SimpleXMLElement object | ||
$xml = new SimpleXMLElement($filecontent); | ||
|
||
// Use xpath to search for the string in the XML content | ||
$result = $xml->xpath("//*[contains(text(), '$contains')]"); | ||
|
||
// If the result is empty, the string was not found in the XML content | ||
if (empty($result)) { | ||
throw new ExpectationException( | ||
"The string '{$contains}' was not found in the XML content.", | ||
$this->getSession(), | ||
); | ||
} | ||
} | ||
|
||
protected function assert_file_type_zip(string $filecontent, string $expectedfile): void { | ||
|
||
// Save the file to disk. | ||
$tempdir = make_request_directory(); | ||
$filepath = $tempdir . '/downloaded.zip'; | ||
file_put_contents($filepath, $filecontent); | ||
|
||
$zip = new ZipArchive(); | ||
$res = $zip->open($filepath); | ||
|
||
if ($res !== true) { | ||
throw new ExpectationException( | ||
"Failed to open zip file.", | ||
$this->getSession(), | ||
); | ||
} | ||
|
||
// Check if the expected file exists in the zip archive. | ||
if ($zip->locateName($expectedfile) === false) { | ||
throw new ExpectationException( | ||
"The file '{$expectedfile}' was not found in the zip archive.", | ||
$this->getSession(), | ||
); | ||
} | ||
} | ||
|
||
protected function assert_file_type_text(string $filecontent, string $contains): void { | ||
|
||
// Check if the string is present in the file content. | ||
if (!str_contains($filecontent, $contains)) { | ||
throw new ExpectationException( | ||
"The string '{$contains}' was not found in the file content.", | ||
$this->getSession(), | ||
); | ||
} | ||
} | ||
|
||
protected function assert_file_type_gift(string $filecontent, string $contains): void { | ||
$this->assert_file_type_text($filecontent, $contains); | ||
} | ||
|
||
protected function assert_file_type_aiken(string $filecontent, string $contains): void { | ||
$this->assert_file_type_text($filecontent, $contains); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.