From d45a73777935c86fc5131955831833d7465b5e9d Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Jan 2013 01:22:01 -0500 Subject: Update copyright to 2013. Fixes #1953. --- modules/gallery/tests/Item_Helper_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/gallery/tests/Item_Helper_Test.php') diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index 818c9a73..0c08d1af 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -1,7 +1,7 @@ Date: Wed, 23 Jan 2013 21:33:19 -0500 Subject: Extract reweighting logic out of Organize_Controller into item::reweight_all_children as an API and write a test for it. Work in progress on #1914. --- modules/gallery/helpers/item.php | 12 ++++++++++++ modules/gallery/tests/Item_Helper_Test.php | 15 +++++++++++++++ modules/organize/controllers/organize.php | 8 +------- 3 files changed, 28 insertions(+), 7 deletions(-) (limited to 'modules/gallery/tests/Item_Helper_Test.php') diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 20a865d1..b2c4cadf 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -437,4 +437,16 @@ class item_Core { } return call_user_func_array($callback, $args); } + + /** + * Reset all child weights of a given album to a monotonically increasing sequence based on the + * current sort order of the album. + */ + static function resequence_child_weights($album) { + $weight = 0; + foreach ($album->children() as $child) { + $child->weight = ++$weight; + $child->save(); + } + } } \ No newline at end of file diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index 0c08d1af..63081884 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -235,4 +235,19 @@ class Item_Helper_Test extends Gallery_Unit_Test_Case { $level3b->id, item::find_by_relative_url("{$level1->slug}/{$level2b->slug}/{$level3b->slug}")->id); } + + public function resequence_child_weights_test() { + $album = test::random_album(); + $photo1 = test::random_photo($album); + $photo2 = test::random_photo($album); + $this->assert_true($photo2->weight > $photo1->weight); + + $album->reload(); + $album->sort_order = "DESC"; + $album->save(); + item::resequence_child_weights($album); + + $this->assert_equal(2, $photo1->reload()->weight); + $this->assert_equal(1, $photo2->reload()->weight); + } } diff --git a/modules/organize/controllers/organize.php b/modules/organize/controllers/organize.php index 97280489..37920020 100644 --- a/modules/organize/controllers/organize.php +++ b/modules/organize/controllers/organize.php @@ -128,13 +128,7 @@ class Organize_Controller extends Controller { access::required("edit", $album); if ($album->sort_column != "weight") { - // Force all the weights into the current order before changing the order to manual - $weight = 0; - foreach ($album->children() as $child) { - $child->weight = ++$weight; - $child->save(); - } - + item::resequence_child_weights($album); $album->sort_column = "weight"; $album->sort_order = "ASC"; $album->save(); -- cgit v1.2.3 From 1313a02bf4e28c169c672a342d7ad16f9b4b17dc Mon Sep 17 00:00:00 2001 From: shadlaws Date: Mon, 28 Jan 2013 14:47:26 +0100 Subject: #1971 - Make resequence_child_weights_test unit test more reliable. - Set the sort_column of the parent album to id, which has no possibility of being identical between the two photos. - Now, the reweighting will reverse the order even if they were created during the same second. --- modules/gallery/tests/Item_Helper_Test.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'modules/gallery/tests/Item_Helper_Test.php') diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index 63081884..f5b99bec 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -237,7 +237,10 @@ class Item_Helper_Test extends Gallery_Unit_Test_Case { } public function resequence_child_weights_test() { - $album = test::random_album(); + $album = test::random_album_unsaved(); + $album->sort_column = "id"; + $album->save(); + $photo1 = test::random_photo($album); $photo2 = test::random_photo($album); $this->assert_true($photo2->weight > $photo1->weight); -- cgit v1.2.3