diff --git a/tests/behat/behat_mod_attendance.php b/tests/behat/behat_mod_attendance.php new file mode 100644 index 0000000..cbcd3b0 --- /dev/null +++ b/tests/behat/behat_mod_attendance.php @@ -0,0 +1,116 @@ +. + + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); + +use Behat\Mink\Exception\ExpectationException as ExpectationException, + Behat\Behat\Exception\PendingException as PendingException; + +/** + * Attendance steps definitions. + * + * @package mod + * @subpackage attendance + * @category test + * @copyright 2014 University of Nottingham + * @author Joseph Baxter (joseph.baxter@nottingham.ac.uk) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_attendance extends behat_base { + + protected $file_contents; + + /** + * @Then /^attendance export file is ok$/ + */ + public function attendance_export_file_is_ok() { + + global $CFG; + + $check = true; + + // Location selenium will download to. + $dir = $CFG->behat_download; + $files = scandir($dir, 1); + $filename = $files[0]; + $file = fopen($dir . $filename, "r"); + + $count = 0; + $header = null; + + // The file is tab seperated but not exactly a tsv. + while (($row = fgetcsv($file, 0, "\t")) !== FALSE) { + + // Ignore unwanted information at the start of the file. + if ($count < 3) { + $count++; + continue; + } + + if (!$header) { + $header = $row; + } else { + $this->file_contents = array_combine($header, $row); + } + + $count++; + } + + fclose($file); + unlink($dir . $filename); + + // Check if data rows exist. + if ($count < 2) { + $check = false; + } + + if ($check) { + + return true; + + } else { + + throw new ExpectationException('Attendance export file not ok', $this->getSession()); + } + + } + + /** + * @Given /^I should see "([^"]*)" as "([^"]*)" in the file$/ + */ + public function i_should_see_as_in_the_file($field, $value) { + + foreach ($this->file_contents as $array_field => $array_value) { + + if ($field == $array_field) { + + if ($value == $array_value) { + + return true; + + } else { + + throw new PendingException(); + + } + } + } + } + +}