From 6a0189eb3f78fb760e21c110b36bba8128fb6700 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 16 Sep 2013 17:10:48 +0100 Subject: [PATCH] MDL-41817 Backup/restore without blocks gives error Also applies to various other situations where backup includes 0 of something. This was caused by a problem in the progress-tracking code. --- .../progress/core_backup_progress.class.php | 4 +-- backup/util/progress/tests/progress_test.php | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/backup/util/progress/core_backup_progress.class.php b/backup/util/progress/core_backup_progress.class.php index 2c092c90fa2..513b18f1ea8 100644 --- a/backup/util/progress/core_backup_progress.class.php +++ b/backup/util/progress/core_backup_progress.class.php @@ -87,9 +87,9 @@ abstract class core_backup_progress { */ public function start_progress($description, $max = self::INDETERMINATE, $parentcount = 1) { - if ($max != self::INDETERMINATE && $max <= 0) { + if ($max != self::INDETERMINATE && $max < 0) { throw new coding_exception( - 'start_progress() max value cannot be zero or negative'); + 'start_progress() max value cannot be negative'); } if ($parentcount < 1) { throw new coding_exception( diff --git a/backup/util/progress/tests/progress_test.php b/backup/util/progress/tests/progress_test.php index 3c673f7274f..28f9f035ab3 100644 --- a/backup/util/progress/tests/progress_test.php +++ b/backup/util/progress/tests/progress_test.php @@ -213,6 +213,28 @@ class backup_progress_testcase extends basic_testcase { set_time_limit(0); } + /** + * To avoid causing problems, progress needs to work for sections that have + * zero entries. + */ + public function test_zero() { + $progress = new core_backup_mock_progress(); + $progress->start_progress('parent', 100); + $progress->progress(1); + $this->assert_min_max(0.01, 0.01, $progress); + $progress->start_progress('child', 0); + + // For 'zero' progress, the progress section as immediately complete + // within the parent count, so it moves up to 2%. + $this->assert_min_max(0.02, 0.02, $progress); + $progress->progress(0); + $this->assert_min_max(0.02, 0.02, $progress); + $progress->end_progress(); + $this->assert_min_max(0.02, 0.02, $progress); + + set_time_limit(0); + } + /** * Tests for any exceptions due to invalid calls. */ @@ -245,12 +267,12 @@ class backup_progress_testcase extends basic_testcase { $this->assertEquals(1, preg_match('~must be 1~', $e->getMessage())); } - // Check invalid start (0). + // Check invalid start (-2). try { - $progress->start_progress('hello', 0); + $progress->start_progress('hello', -2); $this->fail(); } catch (coding_exception $e) { - $this->assertEquals(1, preg_match('~cannot be zero or negative~', $e->getMessage())); + $this->assertEquals(1, preg_match('~cannot be negative~', $e->getMessage())); } // Indeterminate when value expected. -- 2.43.0