/** * Validate that log files are archived for a variety of import and * export plugins, for a variety of configured log paths * * @param string $plugintype One of 'import' or 'export' * @param string $plugin The import plugin to associate log files to * @param string $logfilelocation The logfilelocation setting value to use * @dataProvider importpluginprovider */ public function test_logfilesarchived($plugintype, $plugin, $logfilelocation) { global $CFG, $DB, $USER; require_once $CFG->dirroot . '/local/datahub/fileplugins/log/log.class.php'; require_once $CFG->libdir . '/filestorage/zip_archive.php'; // Clean-up any existing log & zip files. self::cleanup_log_files(); self::cleanup_zip_files(); // Set up the log path. set_config('logfilelocation', $logfilelocation, $plugin); $format = get_string('logfile_timestamp', 'local_datahub'); $USER->timezone = 99; // Create some log files to be zipped by the cron job. // Way earlier then any real existing files! $starttime = make_timestamp(1971, 1, 3); $filenames = array(); for ($i = 0; $i < 10; ++$i) { $filenames[$i] = rlip_log_file_name($plugintype, $plugin, $logfilelocation, 'user', false, $starttime + $i * 3600); // Write out a line to the logfile. $logfile = new rlip_fileplugin_log($filenames[$i]); $logfile->open(RLIP_FILE_WRITE); $logfile->write(array('test entry')); $logfile->close(); } // Call cron job that zips the specified day's log files. $zipfiles = rlip_compress_logs_cron('bogus', 0, $starttime); $this->assertTrue(!empty($zipfiles)); // Was a zip file created?. // Verify that the compressed file exists. $exists = file_exists($zipfiles[0]); $this->assertTrue($exists); // Open zip_archive and verify all logs included. $zip = new zip_archive(); $result = $zip->open($zipfiles[0]); $this->assertTrue($result); $this->assertEquals(10, $zip->count()); $zip->close(); // Verify that the log files created are gone.... for ($i = 0; $i < 10; ++$i) { $exists = file_exists($filenames[$i]); $this->assertFalse($exists); } // Validate that the zip file name corresponds to the plugin. // E.g. pugin is 'dhimport_version1' and file name starts with 'import_version1_'. $parts = explode('/', $zipfiles[0]); $this->assertStringStartsWith($plugin . '_', 'dh' . $parts[count($parts) - 1]); // Delete the test zip. @unlink($zipfiles[0]); }
} // If we haven't found a valid archive file by now, make sure that we display an appropriate error if ($logfilename == '') { // ELIS-5224: delete the temp log files & directory if (!empty($path)) { foreach (glob("{$path}/*") as $logfile) { @unlink($logfile); } @rmdir($path); } print_error('filenotfound', 'error', $CFG->wwwroot . '/local/datahub/viewlogs.php'); } $filein = new rlip_fileplugin_log($logfilename); $filein->open(RLIP_FILE_READ); // ELIS-5199 only use the filename part of the log file path when creating the filename for download $fileout = new rlip_fileplugin_log(basename($logfilename)); $fileout->sendtobrowser = true; $fileout->open(RLIP_FILE_WRITE); while ($entry = $filein->read()) { // remove new lines, they will be added back in write() $entry = preg_replace("/[\n\r]/", "", $entry); // write expects an array $fileout->write(array($entry)); } $filein->close(); $fileout->close(); // ELIS-5224: delete the temp log files & directory if (!empty($path)) { foreach (glob("{$path}/*") as $logfile) { @unlink($logfile); }