Skip to content

Commit

Permalink
Pick a new album cover when the photo that's the current cover is
Browse files Browse the repository at this point in the history
deleted.  Fixes ticket #1083.
  • Loading branch information
bharat committed May 11, 2010
1 parent b2f7f80 commit 9affa8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
11 changes: 11 additions & 0 deletions modules/gallery/helpers/gallery_event.php
Expand Up @@ -97,6 +97,17 @@ static function item_created($item) {

static function item_deleted($item) {
access::delete_item($item);

$parent = $item->parent();
if (!$parent->album_cover_item_id) {
// Assume we deleted the album cover and pick a new one. Choosing the first photo in the
// album is logical, but it's not the most efficient in the case where we're deleting all
// the photos in the album one at a time since we'll probably delete them in order which
// means that we'll be resetting the album cover each time.
if ($child = $parent->children(1)->current()) {
item::make_album_cover($child);
}
}
}

static function item_moved($item, $old_parent) {
Expand Down
13 changes: 12 additions & 1 deletion modules/gallery/tests/Item_Helper_Test.php
Expand Up @@ -54,7 +54,6 @@ public function move_test() {
$this->assert_same($dst_album->id, $photo->parent_id);
}


public function move_updates_album_covers_test() {
// 2 photos in the source album
$src_album = test::random_album();
Expand Down Expand Up @@ -106,4 +105,16 @@ public function move_conflicts_result_in_a_rename_test() {
$this->assert_not_same("{$rand}.jpg", $photo2->name);
$this->assert_not_same($rand, $photo2->slug);
}

public function delete_cover_photo_picks_new_album_cover() {
$album = test::random_album();
$photo1 = test::random_photo($album);
// At this point, $photo1 is the album cover. We verify this in
// Item_Model_Test::first_photo_becomes_album_cover
$photo2 = test::random_photo($album);
$photo1->delete();
$album->reload();

$this->assert_same($photo2->id, $album->album_cover_item_id);
}
}
8 changes: 8 additions & 0 deletions modules/gallery/tests/Item_Model_Test.php
Expand Up @@ -363,4 +363,12 @@ public function as_restful_array_test() {
$this->assert_true(!array_key_exists("parent_id", $result));
$this->assert_true(!array_key_exists("album_cover_item_id", $result));
}

public function first_photo_becomes_album_cover() {
$album = test::random_album();
$photo = test::random_photo($album);
$album->reload();

$this->assert_same($photo->id, $album->album_cover_item_id);
}
}

0 comments on commit 9affa8e

Please sign in to comment.