Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Tests for antivirus manager.
  19   *
  20   * @package    core_antivirus
  21   * @category   phpunit
  22   * @copyright  2016 Ruslan Kabalin, Lancaster University.
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  require_once (__DIR__ . '/fixtures/testable_antivirus.php');
  28  
  29  class core_antivirus_testcase extends advanced_testcase {
  30      protected $tempfile;
  31  
  32      protected function setUp() {
  33          global $CFG;
  34          // Use our special testable fixture plugin.
  35          $CFG->antiviruses = 'testable';
  36  
  37          $this->resetAfterTest();
  38  
  39          // Create tempfile.
  40          $tempfolder = make_request_directory(false);
  41          $this->tempfile = $tempfolder . '/' . rand();
  42          touch($this->tempfile);
  43      }
  44  
  45      protected function tearDown() {
  46          @unlink($this->tempfile);
  47      }
  48  
  49      public function test_manager_get_antivirus() {
  50          // We are using clamav plugin in the test,
  51          // as the only plugin we know exists for sure.
  52          $antivirusviaget = \core\antivirus\manager::get_antivirus('clamav');
  53          $antivirusdirect = new \antivirus_clamav\scanner();
  54          $this->assertEquals($antivirusdirect, $antivirusviaget);
  55      }
  56  
  57      public function test_manager_scan_file_no_virus() {
  58          // Run mock scanning.
  59          $this->assertFileExists($this->tempfile);
  60          $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true));
  61          // File expected to remain in place.
  62          $this->assertFileExists($this->tempfile);
  63      }
  64  
  65      public function test_manager_scan_file_error() {
  66          // Run mock scanning.
  67          $this->assertFileExists($this->tempfile);
  68          $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true));
  69          // File expected to remain in place.
  70          $this->assertFileExists($this->tempfile);
  71      }
  72  
  73      public function test_manager_scan_file_virus() {
  74          // Run mock scanning without deleting infected file.
  75          $this->assertFileExists($this->tempfile);
  76          $this->expectException(\core\antivirus\scanner_exception::class);
  77          $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', false));
  78          // File expected to remain in place.
  79          $this->assertFileExists($this->tempfile);
  80  
  81          // Run mock scanning with deleting infected file.
  82          $this->expectException(\core\antivirus\scanner_exception::class);
  83          $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true));
  84          // File expected to be deleted.
  85          $this->assertFileNotExists($this->tempfile);
  86      }
  87  
  88      public function test_manager_scan_data_no_virus() {
  89          // Run mock scanning.
  90          $this->assertEmpty(\core\antivirus\manager::scan_data('OK'));
  91      }
  92  
  93      public function test_manager_scan_data_error() {
  94          // Run mock scanning.
  95          $this->assertEmpty(\core\antivirus\manager::scan_data('ERROR'));
  96      }
  97  
  98      public function test_manager_scan_data_virus() {
  99          // Run mock scanning.
 100          $this->expectException(\core\antivirus\scanner_exception::class);
 101          $this->assertEmpty(\core\antivirus\manager::scan_data('FOUND'));
 102      }
 103  }