From 5fc2a1f248097246bba2b4b56bde458468eabf77 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 7 Oct 2009 10:05:43 -0700 Subject: [PATCH] Fixed bug #11096. Deleting a thread causes a WebGUI error. Checked to make sure that anything is returned from getLineage before calculating the rating. When the thread is in the trash or clipboard, then getLineage will not return anything. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Asset/Post.pm | 13 +++---- lib/WebGUI/Asset/Post/Thread.pm | 13 ++++--- t/Asset/Post/Thread.t | 64 +++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 t/Asset/Post/Thread.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index f1ac06cab..9a8428f79 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -1,5 +1,6 @@ 7.8.2 - fixed #11098: Leaving a version tag makes everyone leave + - fixed #11096: Error on deleting FAQ(CS)-item 7.8.1 - mark $session->datetime->time as deprecated and remove its use from core code diff --git a/lib/WebGUI/Asset/Post.pm b/lib/WebGUI/Asset/Post.pm index 58729cf3c..635e3b526 100644 --- a/lib/WebGUI/Asset/Post.pm +++ b/lib/WebGUI/Asset/Post.pm @@ -1222,19 +1222,20 @@ An integer indicating either thumbss up (+1) or thumbs down (-1) =cut sub rate { - my $self = shift; + my $self = shift; my $rating = shift; return undef unless ($rating == -1 || $rating == 1); return undef if $self->hasRated; + my $session = $self->session; $self->insertUserPostRating($rating); $self->recalculatePostRating(); my $thread = $self->getThread; $thread->updateThreadRating(); - if ($self->session->setting->get("useKarma") - && $self->session->user->karma > $thread->getParent->get('karmaSpentToRate')) { - $self->session->user->karma(-$self->getThread->getParent->get("karmaSpentToRate"), "Rated Post ".$self->getId, "Rated a CS Post."); - my $u = WebGUI::User->new($self->session, $self->get("ownerUserId")); - $u->karma($self->getThread->getParent->get("karmaRatingMultiplier"), "Post ".$self->getId." Rated by ".$self->session->user->userId, "Had post rated."); + if ($session->setting->get("useKarma") + && $session->user->karma > $thread->getParent->get('karmaSpentToRate')) { + $session->user->karma(-$thread->getParent->get("karmaSpentToRate"), "Rated Post ".$self->getId, "Rated a CS Post."); + my $u = WebGUI::User->new($session, $self->get("ownerUserId")); + $u->karma($thread->getParent->get("karmaRatingMultiplier"), "Post ".$self->getId." Rated by ".$session->user->userId, "Had post rated."); } } diff --git a/lib/WebGUI/Asset/Post/Thread.pm b/lib/WebGUI/Asset/Post/Thread.pm index 970a7e6d8..f4c6e060b 100644 --- a/lib/WebGUI/Asset/Post/Thread.pm +++ b/lib/WebGUI/Asset/Post/Thread.pm @@ -974,15 +974,17 @@ sub updateThreadRating { my $self = shift; my $session = $self->session; - my $calcRating = 0; my $postIds = $self->getLineage(["descendants","self"], { includeOnlyClasses => ["WebGUI::Asset::Post","WebGUI::Asset::Post::Thread"], includeArchived => 1, }); - $calcRating += $session->db->quickScalar( - "SELECT SUM(rating) FROM Post_rating WHERE assetId IN (".$session->db->quoteAndJoin($postIds).")" - ); + my $calcRating = 0; + if (scalar @{ $postIds }) { + $calcRating += $session->db->quickScalar( + "SELECT SUM(rating) FROM Post_rating WHERE assetId IN (".$session->db->quoteAndJoin($postIds).")" + ); + } $self->update({ threadRating => $calcRating @@ -991,7 +993,8 @@ sub updateThreadRating { my $parent = $self->getParent; if (defined $parent) { $parent->recalculateRating; - } else { + } + else { $self->session->errorHandler->error("Couldn't get parent for thread ".$self->getId); } } diff --git a/t/Asset/Post/Thread.t b/t/Asset/Post/Thread.t new file mode 100644 index 000000000..eb476c533 --- /dev/null +++ b/t/Asset/Post/Thread.t @@ -0,0 +1,64 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use WebGUI::Test; +use WebGUI::Session; +use Test::More tests => 2; # increment this value for each test you create +use Test::MockObject::Extends; +use WebGUI::Asset::Wobject::Collaboration; +use WebGUI::Asset::Post::Thread; + +my $session = WebGUI::Test->session; + +# Do our work in the import node +my $node = WebGUI::Asset->getImportNode($session); + +# Grab a named version tag +my $versionTag = WebGUI::VersionTag->getWorking($session); +$versionTag->set({name=>"Collab setup"}); +addToCleanup($versionTag); + +# Need to create a Collaboration system in which the post lives. +my @addArgs = ( undef, undef, { skipAutoCommitWorkflows => 1, skipNotification => 1 } ); +my $collab = $node->addChild({className => 'WebGUI::Asset::Wobject::Collaboration', editTimeout => '1'}, @addArgs); + + +# finally, add the post to the collaboration system +my $props = { + className => 'WebGUI::Asset::Post::Thread', + content => 'hello, world!', + ownerUserId => 1, +}; + +my $thread = $collab->addChild($props, @addArgs); + +$versionTag->commit(); + +# Test for a sane object type +isa_ok($thread, 'WebGUI::Asset::Post::Thread'); + +my $env = $session->env; +$env = Test::MockObject::Extends->new($env); + +my %mockEnv = ( + REMOTE_ADDR => '192.168.0.2', +); + +$env->mock('get', sub { return $mockEnv{$_[1]}}); + +$session->user({userId => 3}); +$thread->rate(1); +$thread->trash; +is($thread->get('threadRating'), 0, 'trash does not die, and updates the threadRating to 0'); + +# vim: syntax=perl filetype=perl