From 694cb8eb21569866d3d41d84e061f00fc95f58a3 Mon Sep 17 00:00:00 2001 From: Ruslan Kabalin Date: Thu, 20 Jul 2017 12:12:38 +0100 Subject: [PATCH] MDL-50907 antivirus: Add test coverage for scan_data. --- lib/tests/antivirus_test.php | 42 ++++++++++++----------- lib/tests/fixtures/testable_antivirus.php | 22 ++++++++++++ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/lib/tests/antivirus_test.php b/lib/tests/antivirus_test.php index 895df2c1a7b..91920290038 100644 --- a/lib/tests/antivirus_test.php +++ b/lib/tests/antivirus_test.php @@ -57,11 +57,7 @@ class core_antivirus_testcase extends advanced_testcase { public function test_manager_scan_file_no_virus() { // Run mock scanning. $this->assertFileExists($this->tempfile); - try { - \core\antivirus\manager::scan_file($this->tempfile, 'OK', true); - } catch (\moodle_exception $e) { - $this->fail('Exception scanner_exception is not expected in clean file scanning.'); - } + $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'OK', true)); // File expected to remain in place. $this->assertFileExists($this->tempfile); } @@ -69,11 +65,7 @@ class core_antivirus_testcase extends advanced_testcase { public function test_manager_scan_file_error() { // Run mock scanning. $this->assertFileExists($this->tempfile); - try { - \core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true); - } catch (\moodle_exception $e) { - $this->fail('Exception scanner_exception is not expected in error file scanning.'); - } + $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'ERROR', true)); // File expected to remain in place. $this->assertFileExists($this->tempfile); } @@ -81,21 +73,31 @@ class core_antivirus_testcase extends advanced_testcase { public function test_manager_scan_file_virus() { // Run mock scanning without deleting infected file. $this->assertFileExists($this->tempfile); - try { - \core\antivirus\manager::scan_file($this->tempfile, 'FOUND', false); - } catch (\moodle_exception $e) { - $this->assertInstanceOf('\core\antivirus\scanner_exception', $e); - } + $this->expectException(\core\antivirus\scanner_exception::class); + $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', false)); // File expected to remain in place. $this->assertFileExists($this->tempfile); // Run mock scanning with deleting infected file. - try { - \core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true); - } catch (\moodle_exception $e) { - $this->assertInstanceOf('\core\antivirus\scanner_exception', $e); - } + $this->expectException(\core\antivirus\scanner_exception::class); + $this->assertEmpty(\core\antivirus\manager::scan_file($this->tempfile, 'FOUND', true)); // File expected to be deleted. $this->assertFileNotExists($this->tempfile); } + + public function test_manager_scan_data_no_virus() { + // Run mock scanning. + $this->assertEmpty(\core\antivirus\manager::scan_data('OK')); + } + + public function test_manager_scan_data_error() { + // Run mock scanning. + $this->assertEmpty(\core\antivirus\manager::scan_data('ERROR')); + } + + public function test_manager_scan_data_virus() { + // Run mock scanning. + $this->expectException(\core\antivirus\scanner_exception::class); + $this->assertEmpty(\core\antivirus\manager::scan_data('FOUND')); + } } diff --git a/lib/tests/fixtures/testable_antivirus.php b/lib/tests/fixtures/testable_antivirus.php index c25f8229da3..1e8ccbf6274 100644 --- a/lib/tests/fixtures/testable_antivirus.php +++ b/lib/tests/fixtures/testable_antivirus.php @@ -66,4 +66,26 @@ class scanner extends \core\antivirus\scanner { break; } } + + /** + * Scan data. + * + * Provides fake responses for testing \core\antivirus\manager. + * + * @param string $data The variable containing the data to scan. + * @return int Scanning result constant. + */ + public function scan_data($data) { + switch ($data) { + case 'OK': + return self::SCAN_RESULT_OK; + case 'FOUND': + return self::SCAN_RESULT_FOUND; + case 'ERROR': + return self::SCAN_RESULT_ERROR; + default: + debugging('$data should be either OK, FOUND or ERROR.'); + break; + } + } }