From c35cf42e557b37c64d324cd0a8824a7caf77bc87 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 18 Mar 2014 17:55:58 +0000 Subject: [PATCH] MDL-44682 fix bug in decompose_update_into_safe_changes --- lib/datalib.php | 2 +- lib/tests/datalib_update_with_unique_index_test.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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();