From 98992b8920f00c974e5a798d42c6b935ffbf04d8 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Sat, 26 Apr 2008 02:23:10 +0000 Subject: [PATCH] - added: Inbox is now pruned after 1 year - Added about a hundred tests for Collaboration system, Post, and Thread permissions - Cleaned up code tested by the aforementioned tests - Fixed all Test::WWW::Mechanize tests and updated the skeleton. Should be usable now. --- docs/changelog/7.x.x.txt | 6 + docs/upgrades/upgrade_7.5.10-7.5.11.pl | 13 ++ lib/WebGUI/Asset/Post.pm | 40 ++++-- lib/WebGUI/Asset/Post/Thread.pm | 13 +- lib/WebGUI/Asset/Wobject/Collaboration.pm | 129 +++++++++++------- .../Activity/PurgeOldInboxMessages.pm | 124 +++++++++++++++++ ...Workflow_Activity_PurgeOldInboxMessages.pm | 25 ++++ t/Asset/Event/edit.t | 11 +- t/Asset/File/GalleryFile/Photo/edit.t | 101 +++++++------- t/Asset/Post/Thread/permission.t | 129 ++++++++++++++++++ t/Asset/Post/permission.t | 100 ++++++++++++++ t/Asset/Redirect/mech.t | 16 ++- t/Asset/Wobject/Collaboration/permission.t | 115 ++++++++++++++++ t/Asset/Wobject/GalleryAlbum/edit.t | 12 ++ t/Auth/mech.t | 14 +- t/_test.skeleton.mech | 14 +- 16 files changed, 742 insertions(+), 120 deletions(-) create mode 100644 lib/WebGUI/Workflow/Activity/PurgeOldInboxMessages.pm create mode 100644 lib/WebGUI/i18n/English/Workflow_Activity_PurgeOldInboxMessages.pm create mode 100644 t/Asset/Post/Thread/permission.t create mode 100644 t/Asset/Post/permission.t create mode 100644 t/Asset/Wobject/Collaboration/permission.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 58e42eaf2..59ea33c26 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -31,6 +31,12 @@ displayed - fixed: Thingy: thing view screen always displays all fields - fixed: Thingy: search result "sort by" not working + - added: Inbox is now pruned after 1 year + - Added about a hundred tests for Collaboration system, Post, and Thread + permissions + - Cleaned up code tested by the aforementioned tests + - Fixed all Test::WWW::Mechanize tests and updated the skeleton. Should be + usable now. 7.5.10 diff --git a/docs/upgrades/upgrade_7.5.10-7.5.11.pl b/docs/upgrades/upgrade_7.5.10-7.5.11.pl index ab4c6d6f1..8e5868f4f 100644 --- a/docs/upgrades/upgrade_7.5.10-7.5.11.pl +++ b/docs/upgrades/upgrade_7.5.10-7.5.11.pl @@ -25,6 +25,7 @@ my $session = start(); # this line required # upgrade functions go here addCalendarEventWorkflow( $session ); +addPurgeOldInboxActivity( $session ); addingInStoreCredit($session); insertCommerceTaxTable($session); migrateOldTaxTable($session); @@ -63,6 +64,12 @@ sub addCalendarEventWorkflow { print "DONE!\n" unless $quiet; } +#---------------------------------------------------------------------------- +# Add the new PurgeOldInboxMessages activity to the config file +sub addPurgeOldInboxActivity { + my $session = shift; + print "\tAdding Purge Old Inbox Messages workflow activity... " unless $quiet; + #------------------------------------------------- sub addingInStoreCredit { my $session = shift; @@ -77,6 +84,12 @@ sub addingInStoreCredit { )"); } + my $activity = $session->config->get( "workflowActivities" ); + push @{ $activity->{"None"} }, 'WebGUI::Workflow::Activity::PurgeOldInboxMessages'; + $session->config->set( "workflowActivities", $activity ); + + print "DONE!\n" unless $quiet; +} #------------------------------------------------- sub upgradeEMS { diff --git a/lib/WebGUI/Asset/Post.pm b/lib/WebGUI/Asset/Post.pm index 7bb51c74f..02713ae81 100644 --- a/lib/WebGUI/Asset/Post.pm +++ b/lib/WebGUI/Asset/Post.pm @@ -92,13 +92,36 @@ sub canAdd { #------------------------------------------------------------------- sub canEdit { - my $self = shift; - return (($self->session->form->process("func") eq "add" || ($self->session->form->process("assetId") eq "new" && $self->session->form->process("func") eq "editSave" && $self->session->form->process("class","className") eq "WebGUI::Asset::Post")) && $self->getThread->getParent->canPost) || # account for new posts + my $self = shift; + my $userId = shift || $self->session->user->userId; + my $session = $self->session; + my $form = $self->session->form; + my $user = WebGUI::User->new( $session, $userId ); - ($self->isPoster && $self->getThread->getParent->get("editTimeout") > ($self->session->datetime->time() - $self->get("revisionDate"))) || - $self->session->user->isInGroup($self->getThread->getParent->get('groupToEditPost')) || - $self->getThread->getParent->canEdit; + # Handle adding new posts + if ( + ( $form->get("func") eq "add" + || ( $form->get("func") eq "editSave" && $form->get("assetId") eq "new" ) + ) + && $form->get("class") eq "WebGUI::Asset::Post" + ) { + return $self->getThread->getParent->canPost; + } + # User who posted can edit their own post + if ( $self->isPoster( $userId ) ) { + my $editTimeout = $self->getThread->getParent->get( 'editTimeout' ); + if ( $editTimeout > time - $self->get( "revisionDate" ) ) { + return 1; + } + } + + # Users in groupToEditPost of the Collab can edit any post + if ( $user->isInGroup( $self->getThread->getParent->get('groupToEditPost') ) ) { + return 1; + } + + return $self->getThread->getParent->canEdit( $userId ); } #------------------------------------------------------------------- @@ -690,15 +713,16 @@ sub isNew { #------------------------------------------------------------------- -=head2 isPoster ( ) +=head2 isPoster ( userId ) Returns a boolean that is true if the current user created this post and is not a visitor. =cut sub isPoster { - my $self = shift; - return ($self->session->user->userId ne "1" && $self->session->user->userId eq $self->get("ownerUserId")); + my $self = shift; + my $userId = shift || $self->session->user->userId; + return ( $userId ne "1" && $userId eq $self->get("ownerUserId") ); } diff --git a/lib/WebGUI/Asset/Post/Thread.pm b/lib/WebGUI/Asset/Post/Thread.pm index 992dbb393..ec5b778e2 100644 --- a/lib/WebGUI/Asset/Post/Thread.pm +++ b/lib/WebGUI/Asset/Post/Thread.pm @@ -47,14 +47,19 @@ sub canAdd { #------------------------------------------------------------------- sub canReply { - my $self = shift; - return !$self->isThreadLocked && $self->getParent->get("allowReplies") && $self->getParent->canPost; + my $self = shift; + my $userId = shift || $self->session->user->userId; + return !$self->isThreadLocked + && $self->getParent->get("allowReplies") + && $self->getParent->canPost( $userId ) + ; } #------------------------------------------------------------------- sub canSubscribe { - my $self = shift; - return ($self->session->user->userId ne "1" && $self->canView); + my $self = shift; + my $userId = shift || $self->session->user->userId; + return ($userId ne "1" && $self->canView( $userId ) ); } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Asset/Wobject/Collaboration.pm b/lib/WebGUI/Asset/Wobject/Collaboration.pm index 91945d846..b06a1197c 100644 --- a/lib/WebGUI/Asset/Wobject/Collaboration.pm +++ b/lib/WebGUI/Asset/Wobject/Collaboration.pm @@ -248,7 +248,8 @@ sub appendTemplateLabels { #------------------------------------------------------------------- sub canEdit { - my $self = shift; + my $self = shift; + my $userId = shift || $self->session->user->userId; return ( ( ( @@ -259,85 +260,109 @@ sub canEdit { $self->session->form->process("class") eq "WebGUI::Asset::Post::Thread" ) ) && - $self->canPost - ) || # account for new posts - $self->SUPER::canEdit() + $self->canStartThread( $userId ) + ) || # account for new threads + $self->SUPER::canEdit( $userId ) ); } #------------------------------------------------------------------- sub canModerate { - my $self = shift; - return $self->SUPER::canEdit; + my $self = shift; + my $userId = shift || $self->session->user->userId; + return $self->SUPER::canEdit( $userId ); } #------------------------------------------------------------------- sub canPost { - my $self = shift; - return ( - ( - $self->get("status") eq "approved" || - $self->getTagCount > 1 # checks to make sure that the cs has been committed at least once - ) && ( - $self->session->user->isInGroup($self->get("postGroupId")) - || $self->SUPER::canEdit - ) - ); + my $self = shift; + my $userId = shift; + my $session = $self->session; + my $user = $userId + ? WebGUI::User->new( $session, $userId ) + : $self->session->user + ; + + # checks to make sure that the cs has been committed at least once + if ( $self->get("status") ne "approved" && $self->getTagCount <= 1 ) { + return 0; + } + # Users in the postGroupId can post + elsif ( $user->isInGroup( $self->get("postGroupId") ) ) { + return 1; + } + # Users who can edit the collab can post + else { + return $self->SUPER::canEdit( $userId ); + } } #------------------------------------------------------------------- sub canSubscribe { - my $self = shift; - return ($self->session->user->userId ne "1" && $self->canView); + my $self = shift; + my $userId = shift; + my $session = $self->session; + my $user = $userId + ? WebGUI::User->new( $session, $userId ) + : $self->session->user + ; + return ($user->userId ne "1" && $self->canView( $userId ) ); } #------------------------------------------------------------------- sub canStartThread { - my $self = shift; - return ( - ( - $self->get("status") eq "approved" || - $self->getTagCount > 1 # checks to make sure that the cs has been committed at least once - ) && ( - $self->session->user->isInGroup($self->get("canStartThreadGroupId")) - || $self->SUPER::canEdit - ) - ); + my $self = shift; + my $userId = shift; + my $session = $self->session; + my $user = $userId + ? WebGUI::User->new( $session, $userId ) + : $self->session->user + ; + return ( + ( + $self->get("status") eq "approved" || + $self->getTagCount > 1 # checks to make sure that the cs has been committed at least once + ) && ( + $user->isInGroup($self->get("canStartThreadGroupId")) + || $self->SUPER::canEdit( $userId ) + ) + ); } #------------------------------------------------------------------- sub canView { my $self = shift; - return $self->SUPER::canView || $self->canPost; + my $userId = shift || $self->session->user->userId; + return $self->SUPER::canView( $userId ) || $self->canPost( $userId ); } #------------------------------------------------------------------- sub commit { - my $self = shift; - $self->SUPER::commit; - my $cron = undef; - if ($self->get("getMailCronId")) { - $cron = WebGUI::Workflow::Cron->new($self->session, $self->get("getMailCronId")); - } - my $i18n = WebGUI::International->new($self->session, "Asset_Collaboration"); - unless (defined $cron) { - $cron = WebGUI::Workflow::Cron->create($self->session, { - title=>$self->getTitle." ".$i18n->get("mail"), - minuteOfHour=>"*/".($self->get("getMailInterval")/60), - className=>(ref $self), - methodName=>"new", - parameters=>$self->getId, - workflowId=>"csworkflow000000000001" - }); - $self->update({getMailCronId=>$cron->getId}); - } - if ($self->get("getMail")) { - $cron->set({enabled=>1,title=>$self->getTitle." ".$i18n->get("mail"), minuteOfHour=>"*/".($self->get("getMailInterval")/60)}); - } else { - $cron->set({enabled=>0,title=>$self->getTitle." ".$i18n->get("mail"), minuteOfHour=>"*/".($self->get("getMailInterval")/60)}); - } + my $self = shift; + $self->SUPER::commit; + my $cron = undef; + if ($self->get("getMailCronId")) { + $cron = WebGUI::Workflow::Cron->new($self->session, $self->get("getMailCronId")); + } + my $i18n = WebGUI::International->new($self->session, "Asset_Collaboration"); + unless (defined $cron) { + $cron = WebGUI::Workflow::Cron->create($self->session, { + title=>$self->getTitle." ".$i18n->get("mail"), + minuteOfHour=>"*/".($self->get("getMailInterval")/60), + className=>(ref $self), + methodName=>"new", + parameters=>$self->getId, + workflowId=>"csworkflow000000000001" + }); + $self->update({getMailCronId=>$cron->getId}); + } + if ($self->get("getMail")) { + $cron->set({enabled=>1,title=>$self->getTitle." ".$i18n->get("mail"), minuteOfHour=>"*/".($self->get("getMailInterval")/60)}); + } else { + $cron->set({enabled=>0,title=>$self->getTitle." ".$i18n->get("mail"), minuteOfHour=>"*/".($self->get("getMailInterval")/60)}); + } } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Workflow/Activity/PurgeOldInboxMessages.pm b/lib/WebGUI/Workflow/Activity/PurgeOldInboxMessages.pm new file mode 100644 index 000000000..82a69802f --- /dev/null +++ b/lib/WebGUI/Workflow/Activity/PurgeOldInboxMessages.pm @@ -0,0 +1,124 @@ +package WebGUI::Workflow::Activity::PurgeOldInboxMessages; + + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2008 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 + ------------------------------------------------------------------- + +=cut + +use strict; +use base 'WebGUI::Workflow::Activity'; +use WebGUI::Asset; + +=head1 NAME + +Package WebGUI::Workflow::Activity::PurgeOldInboxMessages + +=head1 DESCRIPTION + +Removes old, completed inbox messages from the database + +=head1 SYNOPSIS + +See WebGUI::Workflow::Activity for details on how to use any activity. + +=head1 METHODS + +These methods are available from this class: + +=cut + + +#------------------------------------------------------------------- + +=head2 definition ( session, definition ) + +See WebGUI::Workflow::Activity::defintion() for details. + +=cut + +sub definition { + my $class = shift; + my $session = shift; + my $definition = shift; + my $i18n = WebGUI::International->new($session, "Workflow_Activity_PurgeOldInboxMessages"); + + push @{$definition}, { + name => $i18n->get("activityName"), + properties => { + purgeAfter => { + fieldType => "interval", + defaultValue => 60 * 60 * 24 * 365, + label => $i18n->get("editForm purgeAfter label"), + hoverHelp => $i18n->get("editForm purgeAfter description"), + }, + }, + }; + + return $class->SUPER::definition($session,$definition); +} + + +#------------------------------------------------------------------- + +=head2 execute ( ) + +See WebGUI::Workflow::Activity::execute() for details. + +=cut + +sub execute { + my ($self, $nothing, $instance) = @_; + my $session = $self->session; + my $log = $session->errorHandler; + + # keep track of how much time it's taking + my $start = time; + + # Only purge this many per operation to prevent MySQL returning + # a half-million rows (which will take longer than the time we have) + my $limit = 2_500; + + my $sth + = $session->db->read( + "SELECT messageId FROM inbox WHERE completedOn IS NOT NULL AND dateStamp < ? ORDER BY dateStamp ASC LIMIT $limit", + [ $start - $self->get('purgeAfter') ], + ); + + while ( ( my $messageId ) = $sth->array ) { + $session->db->write( + "DELETE FROM inbox WHERE messageId = ?", + [ $messageId ], + ); + + # give up if we're taking too long + if (time - $start > 120) { + $sth->finish; + return $self->WAITING; + } + } + + # If there are more messages waiting to be purged, return WAITING + if ( $sth->rows >= $limit ) { + return $self->WAITING; + } + else { + return $self->COMPLETE; + } +} + + + + +1; + + diff --git a/lib/WebGUI/i18n/English/Workflow_Activity_PurgeOldInboxMessages.pm b/lib/WebGUI/i18n/English/Workflow_Activity_PurgeOldInboxMessages.pm new file mode 100644 index 000000000..b9b6da03c --- /dev/null +++ b/lib/WebGUI/i18n/English/Workflow_Activity_PurgeOldInboxMessages.pm @@ -0,0 +1,25 @@ +package WebGUI::i18n::English::Workflow_Activity_PurgeOldInboxMessages; + +use strict; + +our $I18N = { + 'activityName' => { + message => q{Purge Old Inbox Messages}, + lastUpdated => 0, + context => 'Title of workflow activity', + }, + + 'editForm purgeAfter label' => { + message => q{Purge After}, + lastUpdated => 0, + context => 'Label for workflow property', + }, + + 'editForm purgeAfter description' => { + message => q{The length of time a message is allowed to remain in the inbox after it has been set to "completed"}, + lastUpdated => 0, + context => 'Description of workflow property', + }, +}; + +1; diff --git a/t/Asset/Event/edit.t b/t/Asset/Event/edit.t index 3d46c5425..65adada54 100644 --- a/t/Asset/Event/edit.t +++ b/t/Asset/Event/edit.t @@ -65,9 +65,16 @@ $versionTags[-1]->commit; #---------------------------------------------------------------------------- # Tests -plan tests => 9; # Increment this number for each test you create +if ( !eval { require Test::WWW::Mechanize; 1; } ) { + plan skip_all => 'Cannot load Test::WWW::Mechanize. Will not test.'; +} +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl ); +if ( !$mech->success ) { + plan skip_all => "Cannot load URL '$baseUrl'. Will not test."; +} -use_ok( 'Test::WWW::Mechanize' ) or BAIL_OUT("Cannot continue without Test::WWW::Mechanize"); +plan tests => 8; # Increment this number for each test you create #---------------------------------------------------------------------------- # Add event: Users without permission are not shown form diff --git a/t/Asset/File/GalleryFile/Photo/edit.t b/t/Asset/File/GalleryFile/Photo/edit.t index 2e33eecdb..4d6110a50 100644 --- a/t/Asset/File/GalleryFile/Photo/edit.t +++ b/t/Asset/File/GalleryFile/Photo/edit.t @@ -31,6 +31,9 @@ my %oldSettings; # userFunctionStyleId $oldSettings{ userFunctionStyleId } = $session->setting->get( 'userFunctionStyleId' ); $session->setting->set( 'userFunctionStyleId', 'PBtmpl0000000000000132' ); +# specialState +$oldSettings{ specialState } = $session->setting->get( 'specialState' ); +$session->setting->set( 'specialState', '' ); # Create a user for testing purposes my $user = WebGUI::User->new( $session, "new" ); @@ -67,62 +70,66 @@ my $photo; #---------------------------------------------------------------------------- # Tests -plan tests => 6; # Increment this number for each test you create +if ( !eval { require Test::WWW::Mechanize; 1; } ) { + plan skip_all => 'Cannot load Test::WWW::Mechanize. Will not test.'; +} +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl ); +if ( !$mech->success ) { + plan skip_all => "Cannot load URL '$baseUrl'. Will not test."; +} -SKIP: { +plan tests => 5; # Increment this number for each test you create - use_ok( 'Test::WWW::Mechanize' ) or skip( "Cannot continue without Test::WWW::Mechanize", 5 ); +#---------------------------------------------------------------------------- +# Test permissions for new photos +$mech = Test::WWW::Mechanize->new; - #---------------------------------------------------------------------------- - # Test permissions for new photos - $mech = Test::WWW::Mechanize->new; +# Save a new photo +$mech->get( $baseUrl . $album->getUrl("func=add;class=WebGUI::Asset::File::GalleryFile::Photo") ); +$mech->content_lacks( 'value="editSave"' ); - # Save a new photo - $mech->get( $baseUrl . $album->getUrl("func=add;class=WebGUI::Asset::File::GalleryFile::Photo") ); - $mech->content_lacks( 'value="editSave"' ); +#---------------------------------------------------------------------------- +# Test creating a new Photo +SKIP: { + skip "File control needs to be fixed to be more 508-compliant before this can be used", 4; + $mech = getMechLogin( $baseUrl, $user, $identifier ); + $mech->get_ok( $baseUrl . $album->getUrl("func=add;class=WebGUI::Asset::File::GalleryFile::Photo") ); - #---------------------------------------------------------------------------- - # Test creating a new Photo - SKIP: { - skip "File control needs to be fixed to be more 508-compliant before this can be used", 4; - $mech = getMechLogin( $baseUrl, $user, $identifier ); - $mech->get_ok( $baseUrl . $album->getUrl("func=add;class=WebGUI::Asset::File::GalleryFile::Photo") ); + open my $file, '<', WebGUI::Test->getTestCollateralPath( 'lamp.jpg' ) + or die( "Couldn't open test collateral 'lamp.jpg' for reading: $!" ); + my $properties = { + title => 'Photo Title' . time, + synopsis => '

Photo Synopsis' . time . '

', + newFile_file => $file, + }; - open my $file, '<', WebGUI::Test->getTestCollateralPath( 'lamp.jpg' ) - or die( "Couldn't open test collateral 'lamp.jpg' for reading: $!" ); - my $properties = { - title => 'Photo Title' . time, - synopsis => '

Photo Synopsis' . time . '

', - newFile_file => $file, - }; + $mech->submit_form_ok( + { + form_number => 1, + fields => $properties, + }, + 'Submit new Photo' + ); - $mech->submit_form_ok( - { - form_number => 1, - fields => $properties, - }, - 'Submit new Photo' - ); + # Add properties that should be default and remove those that should be different + delete $properties->{ newFile_file }; + $properties = { + %{ $properties }, + ownerUserId => $user->userId, + filename => 'lamp.jpg', + }; - # Add properties that should be default and remove those that should be different - delete $properties->{ newFile_file }; - $properties = { - %{ $properties }, - ownerUserId => $user->userId, - filename => 'lamp.jpg', - }; + # Make sure properties were saved + my $photo = WebGUI::Asset->newByDynamicClass( $session, $album->getFileIds->[0] ); + cmp_deeply( $photo->get, superhashof( $properties ), "Photo properties saved correctly" ); - # Make sure properties were saved - my $photo = WebGUI::Asset->newByDynamicClass( $session, $album->getFileIds->[0] ); - cmp_deeply( $photo->get, superhashof( $properties ), "Photo properties saved correctly" ); - - # First File in an album should update assetIdThumbnail - my $album = WebGUI::Asset->newByDynamicClass( $session, $album->getId ); - is( - $album->get('assetIdThumbnail'), $photo->getId, - "Album assetIdThumbnail gets set by first File added", - ); - } + # First File in an album should update assetIdThumbnail + my $album = WebGUI::Asset->newByDynamicClass( $session, $album->getId ); + is( + $album->get('assetIdThumbnail'), $photo->getId, + "Album assetIdThumbnail gets set by first File added", + ); } #---------------------------------------------------------------------------- diff --git a/t/Asset/Post/Thread/permission.t b/t/Asset/Post/Thread/permission.t new file mode 100644 index 000000000..bc81e3ab7 --- /dev/null +++ b/t/Asset/Post/Thread/permission.t @@ -0,0 +1,129 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 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 +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../../lib"; +use Test::More; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Test::Maker::Permission; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +$session->user( { userId => 3 } ); +my $maker = WebGUI::Test::Maker::Permission->new; +my $node = WebGUI::Asset->getImportNode( $session ); + +my %user; +$user{"2"} = WebGUI::User->new( $session, "new" ); +$user{"2"}->addToGroups( ['2'] ); # Registered user + +my $versionTag = WebGUI::VersionTag->getWorking( $session ); +$versionTag->set( { name => "Collaboration Test" } ); + +my @addArgs = ( undef, undef, { skipAutoCommitWorkflows => 1 } ); + +my $collab + = $node->addChild({ + className => "WebGUI::Asset::Wobject::Collaboration", + groupIdView => 7, # Everyone + groupIdEdit => 3, # Admins + groupToEditPost => 3, # Admins + ownerUserId => 3, # Admin + postGroupId => 2, # Registered Users + canStartThreadGroupId => 3, # Admin + allowReplies => 1, + editTimeout => 60 * 60 * 24, # 24 hours + }, @addArgs ); + +my $thread + = $collab->addChild({ + className => 'WebGUI::Asset::Post::Thread', + ownerUserId => $user{"2"}->userId, + groupIdView => 7, + }, @addArgs ); + +$versionTag->commit( { timeout => 1_000_000 } ); + +# Re-load the collab to get the newly committed properties +$collab = WebGUI::Asset->newByDynamicClass( $session, $collab->getId ); +$thread = WebGUI::Asset->newByDynamicClass( $session, $thread->getId ); + +#---------------------------------------------------------------------------- +# Tests +plan tests => 36; + +#---------------------------------------------------------------------------- +# Permissions for threads +# View +$maker->prepare( { + object => $thread, + method => 'canView', + pass => [ '1', $user{"2"}, '3', ], +} )->run; + +# Subscribe +$maker->prepare( { + object => $thread, + method => 'canSubscribe', + pass => [ $user{"2"}, '3', ], + fail => [ '1', ], +} )->run; + +# Edit +$maker->prepare( { + object => $thread, + method => 'canEdit', + pass => [ $user{"2"}, '3', ], + fail => [ '1', ], +} )->run; + +# Reply +$maker->prepare( { + object => $thread, + method => 'canReply', + pass => [ $user{"2"}, '3', ], + fail => [ '1', ], +} )->run; + +# Reply with allowReplies = 0 +$collab->update({ allowReplies => 0 }); +$thread = WebGUI::Asset->newByDynamicClass( $session, $thread->getId ); +$maker->prepare( { + object => $thread, + method => 'canReply', + fail => [ '1', $user{"2"}, '3', ], +} )->run; +$collab->update({ allowReplies => 1 }); + +# Reply with thread isLocked +$thread->lock; +$maker->prepare( { + object => $thread, + method => 'canReply', + fail => [ '1', $user{"2"}, '3', ], +} )->run; +$thread->unlock; + +#---------------------------------------------------------------------------- +# Cleanup +END { + for my $user ( values %user ) { + $user->delete; + } + $versionTag->rollback; +} diff --git a/t/Asset/Post/permission.t b/t/Asset/Post/permission.t new file mode 100644 index 000000000..eb6891524 --- /dev/null +++ b/t/Asset/Post/permission.t @@ -0,0 +1,100 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 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 +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use Test::More; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Test::Maker::Permission; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +$session->user( { userId => 3 } ); +my $maker = WebGUI::Test::Maker::Permission->new; +my $node = WebGUI::Asset->getImportNode( $session ); + +my %user; +$user{"2"} = WebGUI::User->new( $session, "new" ); +$user{"2"}->addToGroups( ['2'] ); # Registered user + +my $versionTag = WebGUI::VersionTag->getWorking( $session ); +$versionTag->set( { name => "Collaboration Test" } ); + +my @addArgs = ( undef, undef, { skipAutoCommitWorkflows => 1 } ); + +my $collab + = $node->addChild({ + className => "WebGUI::Asset::Wobject::Collaboration", + groupIdView => 7, # Everyone + groupIdEdit => 3, # Admins + groupToEditPost => 3, # Admins + ownerUserId => 3, # Admin + postGroupId => 2, # Registered Users + canStartThreadGroupId => 3, # Admin + allowReplies => 1, + editTimeout => 60 * 60 * 24, # 24 hours + }, @addArgs ); + +my $thread + = $collab->addChild({ + className => 'WebGUI::Asset::Post::Thread', + ownerUserId => $user{"2"}->userId, + }, @addArgs ); + +my $post + = $thread->addChild({ + className => 'WebGUI::Asset::Post', + ownerUserId => $user{"2"}->userId, + }, @addArgs ); + +$versionTag->commit( { timeout => 1_000_000 } ); + +# Re-load the collab to get the newly committed properties +$collab = WebGUI::Asset->newByDynamicClass( $session, $collab->getId ); +$thread = WebGUI::Asset->newByDynamicClass( $session, $thread->getId ); +$post = WebGUI::Asset->newByDynamicClass( $session, $post->getId ); + +#---------------------------------------------------------------------------- +# Tests +plan tests => 12; + +#---------------------------------------------------------------------------- +# Permissions for posts +# View +$maker->prepare( { + object => $post, + method => 'canView', + pass => [ '1', $user{"2"}, '3', ], +} )->run; + +# Edit +$maker->prepare( { + object => $post, + method => 'canEdit', + pass => [ $user{"2"}, '3', ], + fail => [ '1', ], +} )->run; + +#---------------------------------------------------------------------------- +# Cleanup +END { + for my $user ( values %user ) { + $user->delete; + } + $versionTag->rollback; +} diff --git a/t/Asset/Redirect/mech.t b/t/Asset/Redirect/mech.t index eeb8c3652..7106d8003 100644 --- a/t/Asset/Redirect/mech.t +++ b/t/Asset/Redirect/mech.t @@ -33,10 +33,13 @@ my %oldSettings; # userFunctionStyleId $oldSettings{ userFunctionStyleId } = $session->setting->get( 'userFunctionStyleId' ); $session->setting->set( 'userFunctionStyleId', 'PBtmpl0000000000000132' ); +# specialState +$oldSettings{ specialState } = $session->setting->get( 'specialState' ); +$session->setting->set( 'specialState', '' ); # Create a user for testing purposes my $user = WebGUI::User->new( $session, "new" ); -$user->username( 'dufresne' . time ); +$user->username( 'dufresne' ); my $identifier = 'ritahayworth'; my $auth = WebGUI::Operation::Auth::getInstance( $session, $user->authMethod, $user->userId ); $auth->saveParams( $user->userId, $user->authMethod, { @@ -65,9 +68,16 @@ $versionTags[-1]->commit; #---------------------------------------------------------------------------- # Tests -plan tests => 13; # Increment this number for each test you create +if ( !eval { require Test::WWW::Mechanize; 1; } ) { + plan skip_all => 'Cannot load Test::WWW::Mechanize. Will not test.'; +} +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl ); +if ( !$mech->success ) { + plan skip_all => "Cannot load URL '$baseUrl'. Will not test."; +} -use_ok( 'Test::WWW::Mechanize' ); +plan tests => 12; # Increment this number for each test you create #---------------------------------------------------------------------------- # Test operation with a public Redirect diff --git a/t/Asset/Wobject/Collaboration/permission.t b/t/Asset/Wobject/Collaboration/permission.t new file mode 100644 index 000000000..9ba5be474 --- /dev/null +++ b/t/Asset/Wobject/Collaboration/permission.t @@ -0,0 +1,115 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 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 +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../../lib"; +use Test::More; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Test::Maker::Permission; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +$session->user( { userId => 3 } ); +my $maker = WebGUI::Test::Maker::Permission->new; +my $node = WebGUI::Asset->getImportNode( $session ); + +my %user; +$user{"2"} = WebGUI::User->new( $session, "new" ); +$user{"2"}->addToGroups( ['2'] ); # Registered user + +my $versionTag = WebGUI::VersionTag->getWorking( $session ); +$versionTag->set( { name => "Collaboration Test" } ); + +my @addArgs = ( undef, undef, { skipAutoCommitWorkflows => 1 } ); + +my $collab + = $node->addChild({ + className => "WebGUI::Asset::Wobject::Collaboration", + groupIdView => 7, # Everyone + groupIdEdit => 3, # Admins + ownerUserId => 3, # Admin + postGroupId => 2, # Registered Users + canStartThreadGroupId => 3, # Admin + }, @addArgs ); + +$versionTag->commit( { timeout => 1_000_000 } ); + +# Re-load the collab to get the newly committed properties +$collab = WebGUI::Asset->newByDynamicClass( $session, $collab->getId ); + +#---------------------------------------------------------------------------- +# Tests +plan tests => 36; + +#---------------------------------------------------------------------------- +# Permissions for collaboration systems +# View +$maker->prepare( { + object => $collab, + method => 'canView', + pass => [ '1', $user{"2"}, '3', ], +} )->run; + +# Edit +$maker->prepare( { + object => $collab, + method => 'canEdit', + pass => [ '3', ], + fail => [ '1', $user{"2"}, ], +} )->run; + +# Post +$maker->prepare( { + object => $collab, + method => 'canPost', + pass => [ $user{"2"}, '3', ], + fail => [ '1', ], +} )->run; + +# Post Thread +$maker->prepare( { + object => $collab, + method => 'canStartThread', + pass => [ '3', ], + fail => [ '1', $user{"2"}, ], +} )->run; + +# Subscribe +$maker->prepare( { + object => $collab, + method => 'canSubscribe', + pass => [ $user{"2"}, '3', ], + fail => [ '1', ], +} )->run; + +# Moderate +$maker->prepare( { + object => $collab, + method => 'canModerate', + pass => [ '3', ], + fail => [ '1', $user{"2"}, ], +} )->run; + +#---------------------------------------------------------------------------- +# Cleanup +END { + for my $user ( values %user ) { + $user->delete; + } + $versionTag->rollback; +} diff --git a/t/Asset/Wobject/GalleryAlbum/edit.t b/t/Asset/Wobject/GalleryAlbum/edit.t index 40ede1d0d..d01f454bb 100644 --- a/t/Asset/Wobject/GalleryAlbum/edit.t +++ b/t/Asset/Wobject/GalleryAlbum/edit.t @@ -33,6 +33,9 @@ my %oldSettings; # userFunctionStyleId $oldSettings{ userFunctionStyleId } = $session->setting->get( 'userFunctionStyleId' ); $session->setting->set( 'userFunctionStyleId', 'PBtmpl0000000000000132' ); +# specialState +$oldSettings{ specialState } = $session->setting->get( 'specialState' ); +$session->setting->set( 'specialState', '' ); # Create a user for testing purposes my $user = WebGUI::User->new( $session, "new" ); @@ -62,6 +65,15 @@ $versionTags[-1]->commit; #---------------------------------------------------------------------------- # Tests +if ( !eval { require Test::WWW::Mechanize; 1; } ) { + plan skip_all => 'Cannot load Test::WWW::Mechanize. Will not test.'; +} +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl ); +if ( !$mech->success ) { + plan skip_all => "Cannot load URL '$baseUrl'. Will not test."; +} + plan tests => 6; # Increment this number for each test you create #---------------------------------------------------------------------------- diff --git a/t/Auth/mech.t b/t/Auth/mech.t index fde65d1c9..a10a4d0fd 100644 --- a/t/Auth/mech.t +++ b/t/Auth/mech.t @@ -38,6 +38,9 @@ my %oldSettings; # userFunctionStyleId $oldSettings{ userFunctionStyleId } = $session->setting->get( 'userFunctionStyleId' ); $session->setting->set( 'userFunctionStyleId', 'PBtmpl0000000000000132' ); +# specialState +$oldSettings{ specialState } = $session->setting->get( 'specialState' ); +$session->setting->set( 'specialState', '' ); # Create a user for testing purposes my $USERNAME = 'dufresne'; @@ -72,9 +75,16 @@ my $assetUrl = $baseUrl . $asset->get('url'); #---------------------------------------------------------------------------- # Tests -plan tests => 41; # Increment this number for each test you create +if ( !eval { require Test::WWW::Mechanize; 1; } ) { + plan skip_all => 'Cannot load Test::WWW::Mechanize. Will not test.'; +} +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl ); +if ( !$mech->success ) { + plan skip_all => "Cannot load URL '$baseUrl'. Will not test."; +} -use_ok( 'Test::WWW::Mechanize' ); +plan tests => 40; # Increment this number for each test you create #---------------------------------------------------------------------------- # no form: Test logging in on a normal page sends the user back to the same page diff --git a/t/_test.skeleton.mech b/t/_test.skeleton.mech index 5513c1f9e..1efeac294 100644 --- a/t/_test.skeleton.mech +++ b/t/_test.skeleton.mech @@ -43,6 +43,9 @@ my %oldSettings; # userFunctionStyleId $oldSettings{ userFunctionStyleId } = $session->setting->get( 'userFunctionStyleId' ); $session->setting->set( 'userFunctionStyleId', 'PBtmpl0000000000000132' ); +# specialState +$oldSettings{ specialState } = $session->setting->get( 'specialState' ); +$session->setting->set( 'specialState', '' ); # Create a user for testing purposes my $user = WebGUI::User->new( $session, "new" ); @@ -62,9 +65,16 @@ my $baseUrl = 'http://' . $session->config->get('sitename')->[0]; #---------------------------------------------------------------------------- # Tests -plan tests => 1; # Increment this number for each test you create +if ( !eval { require Test::WWW::Mechanize; 1; } ) { + plan skip_all => 'Cannot load Test::WWW::Mechanize. Will not test.'; +} +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl ); +if ( !$mech->success ) { + plan skip_all => "Cannot load URL '$baseUrl'. Will not test."; +} -use_ok( 'Test::WWW::Mechanize' ) or BAIL_OUT( "Cannot continue without Test::WWW::Mechanize" ); +plan tests => 1; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here