MDL-41817 Backup/restore without blocks gives error
authorsam marshall <s.marshall@open.ac.uk>
Mon, 16 Sep 2013 16:10:48 +0000 (17:10 +0100)
committersam marshall <s.marshall@open.ac.uk>
Mon, 16 Sep 2013 16:11:40 +0000 (17:11 +0100)
Also applies to various other situations where backup includes 0 of
something. This was caused by a problem in the progress-tracking code.

backup/util/progress/core_backup_progress.class.php
backup/util/progress/tests/progress_test.php

index 2c092c9..513b18f 100644 (file)
@@ -87,9 +87,9 @@ abstract class core_backup_progress {
      */
     public function start_progress($description, $max = self::INDETERMINATE,
             $parentcount = 1) {
      */
     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(
             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(
         }
         if ($parentcount < 1) {
             throw new coding_exception(
index 3c673f7..28f9f03 100644 (file)
@@ -213,6 +213,28 @@ class backup_progress_testcase extends basic_testcase {
         set_time_limit(0);
     }
 
         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.
      */
     /**
      * 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()));
         }
 
             $this->assertEquals(1, preg_match('~must be 1~', $e->getMessage()));
         }
 
-        // Check invalid start (0).
+        // Check invalid start (-2).
         try {
         try {
-            $progress->start_progress('hello', 0);
+            $progress->start_progress('hello', -2);
             $this->fail();
         } catch (coding_exception $e) {
             $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.
         }
 
         // Indeterminate when value expected.