diff --git a/lib/datalib.php b/lib/datalib.php index b3f0ddcb072..9e7cd61be94 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -2036,7 +2036,7 @@ function decompose_update_into_safe_changes(array $newvalues, $unusedvalue) { $next = $nontrivialmap[$current]; unset($nontrivialmap[$current]); $current = $next; - } while ($current !== $cyclestart); + } while ($current != $cyclestart); // Now convert it to a sequence of safe renames by using a temp. $safechanges[] = array($cyclestart, $unusedvalue); diff --git a/lib/tests/datalib_update_with_unique_index_test.php b/lib/tests/datalib_update_with_unique_index_test.php index e19071f5861..039783585c9 100644 --- a/lib/tests/datalib_update_with_unique_index_test.php +++ b/lib/tests/datalib_update_with_unique_index_test.php @@ -77,6 +77,14 @@ class datalib_update_with_unique_index_testcase extends advanced_testcase { } } + public function test_decompose_update_into_safe_changes_string_values() { + // Sometimes this happens when data has been loaded from the database. + $this->assertEquals(array(array(1, -1), array(2, 1), + array(3, 2), array(4, 3), array(-1, 4)), + decompose_update_into_safe_changes( + array(1 => '4', 2 => '1', 3 => '2', 4 => '3'), -1)); + } + public function test_reorder_rows() { global $DB; $dbman = $DB->get_manager();