diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index b62f60f66..2267dffb2 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -1,4 +1,11 @@ 7.5.8 + - moved Gallery utility methods to WebGUI::Utility::Gallery + - Added tests for GalleryAlbum RSS + - More tests for comments + - Test International Macro sprintf as third+ arguments + - Add Gallery search limiting by user ID + - Remaining i18n for Gallery templates + - Fix: Search form now visible in Photo assets 7.5.7 - fixed: HttpProxy mixes original site's content encoding with WebGUI's diff --git a/docs/upgrades/_upgrade.skeleton b/docs/upgrades/_upgrade.skeleton index 0d47257e5..957377ce2 100644 --- a/docs/upgrades/_upgrade.skeleton +++ b/docs/upgrades/_upgrade.skeleton @@ -27,15 +27,16 @@ my $session = start(); # this line required finish($session); # this line required -##------------------------------------------------- +#---------------------------------------------------------------------------- #sub exampleFunction { -# my $session = shift; -# print "\tWe're doing some stuff here that you should know about.\n" unless ($quiet); -# # and here's our code +# my $session = shift; +# print "\tWe're doing some stuff here that you should know about... " unless $quiet; +# # and here's our code +# print "DONE!\n" unless $quiet; #} -# --------------- DO NOT EDIT BELOW THIS LINE -------------------------------- +# -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- #---------------------------------------------------------------------------- # Add a package to the import node diff --git a/docs/upgrades/packages-7.5.8/root_import_gallery-templates.wgpkg b/docs/upgrades/packages-7.5.8/root_import_gallery-templates.wgpkg new file mode 100644 index 000000000..183b4117a Binary files /dev/null and b/docs/upgrades/packages-7.5.8/root_import_gallery-templates.wgpkg differ diff --git a/docs/upgrades/upgrade_7.5.7-7.5.8.pl b/docs/upgrades/upgrade_7.5.7-7.5.8.pl index 40a07ed2d..1921be275 100644 --- a/docs/upgrades/upgrade_7.5.7-7.5.8.pl +++ b/docs/upgrades/upgrade_7.5.7-7.5.8.pl @@ -23,6 +23,9 @@ my $quiet; # this line required my $session = start(); # this line required # upgrade functions go here +removeOldGalleryColumns( $session ); +moveColumnsToGalleryFile( $session ); +moveCommentsToGalleryFile( $session ); finish($session); # this line required @@ -34,6 +37,82 @@ finish($session); # this line required # # and here's our code #} +#---------------------------------------------------------------------------- +sub removeOldGalleryColumns { + my $session = shift; + $session->db->write( + "ALTER TABLE Gallery DROP COLUMN groupIdModerator" + ); +} + +#---------------------------------------------------------------------------- +# moveColumnsToGalleryFile +# Move columns from Photo that are better handled under GalleryFile +sub moveColumnsToGalleryFile { + my $session = shift; + print "\tMoving Photo columns to GalleryFile (its superclass)... " unless $quiet; + + # Add the galleryfile columns + $session->db->write(q{ + CREATE TABLE GalleryFile ( + assetId VARCHAR(22) BINARY NOT NULL, + revisionDate BIGINT NOT NULL, + userDefined1 LONGTEXT, + userDefined2 LONGTEXT, + userDefined3 LONGTEXT, + userDefined4 LONGTEXT, + userDefined5 LONGTEXT, + views BIGINT DEFAULT 0, + friendsOnly INT(1) DEFAULT 0, + rating INT(1) DEFAULT 0, + PRIMARY KEY ( assetId, revisionDate ) + ) + }); + + # Move Photo data to GalleryFile + my $sth = $session->db->read( "SELECT * FROM Photo" ); + while ( my %row = $sth->hash ) { + $session->db->write( + q{ INSERT INTO GalleryFile ( + assetId, revisionDate, userDefined1, userDefined2, userDefined3, userDefined4, + userDefined5, views, friendsOnly, rating ) + VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) + }, + [ @row{ qw( assetId revisionDate userDefined1 userDefined2 userDefined3 userDefined4 + userDefined5 views friendsOnly rating ) } ], + ); + } + + # Drop the photo columns + $session->db->write( q{ + ALTER TABLE Photo + DROP COLUMN userDefined1, + DROP COLUMN userDefined2, + DROP COLUMN userDefined3, + DROP COLUMN userDefined4, + DROP COLUMN userDefined5, + DROP COLUMN views, + DROP COLUMN friendsOnly, + DROP COLUMN rating + } ); + + print "DONE!\n" unless $quiet; +} + +#---------------------------------------------------------------------------- +# moveCommentsToGalleryFile +# Move comments to a better-described table +sub moveCommentsToGalleryFile { + my $session = shift; + print "\tMoving Photo_comment to GalleryFile_comment... " unless $quiet; + + $session->db->write( q{ + ALTER TABLE Photo_comment RENAME TO GalleryFile_comment + } ); + + print "DONE!\n" unless $quiet; +} + # --------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/lib/WebGUI/Asset/File/GalleryFile.pm b/lib/WebGUI/Asset/File/GalleryFile.pm index 52449c897..05ed57cce 100644 --- a/lib/WebGUI/Asset/File/GalleryFile.pm +++ b/lib/WebGUI/Asset/File/GalleryFile.pm @@ -17,6 +17,8 @@ package WebGUI::Asset::File::GalleryFile; use strict; use base 'WebGUI::Asset::File'; +use Carp qw( croak confess ); + =head1 NAME @@ -34,6 +36,311 @@ These methods are available from this class =cut +#------------------------------------------------------------------- + +=head2 definition ( session, definition ) + +Define the properties of all GalleryFile assets. + +=cut + +sub definition { + my $class = shift; + my $session = shift; + my $definition = shift; + my $i18n = __PACKAGE__->i18n($session); + + tie my %properties, 'Tie::IxHash', ( + views => { + defaultValue => 0, + }, + friendsOnly => { + defaultValue => 0, + }, + rating => { + defaultValue => 0, + }, + ); + + # UserDefined Fields + for my $i (1 .. 5) { + $properties{"userDefined".$i} = { + defaultValue => undef, + }; + } + + push @{$definition}, { + assetName => $i18n->get('assetName'), + autoGenerateForms => 0, + tableName => 'GalleryFile', + className => 'WebGUI::Asset::File::GalleryFile', + properties => \%properties, + }; + + return $class->SUPER::definition($session, $definition); +} + +#---------------------------------------------------------------------------- + +=head2 appendTemplateVarsCommentForm ( var [, comment ] ) + +Add the template variables necessary for the comment form to the given hash +reference. Returns the hash reference for convenience. C is a hash +reference of values to populate the form with. + +=cut + +sub appendTemplateVarsCommentForm { + my $self = shift; + my $var = shift; + my $comment = shift || {}; + my $session = $self->session; + + # Default comment + $comment->{ commentId } ||= "new"; + + $var->{ commentForm_start } + = WebGUI::Form::formHeader( $session ) + . WebGUI::Form::hidden( $session, { + name => "func", + value => "editCommentSave" + } ) + . WebGUI::Form::hidden( $session, { + name => "commentId", + value => $comment->{ commentId } + } ) + ; + + # Add hidden fields for editing a comment + if ( $comment->{ commentId } ne "new" ) { + $var->{ commentForm_start } + .= WebGUI::Form::hidden( $session, { + name => "userId", + value => $comment->{ userId } + } ) + . WebGUI::Form::hidden( $session, { + name => "visitorIp", + value => $comment->{ visitorIp } + } ) + . WebGUI::Form::hidden( $session, { + name => "creationDate", + value => $comment->{ creationDate } + } ) + ; + } + + $var->{ commentForm_end } + = WebGUI::Form::formFooter( $session ); + + $var->{ commentForm_bodyText } + = WebGUI::Form::HTMLArea( $session, { + name => "bodyText", + richEditId => $self->getGallery->get("richEditIdComment"), + value => $comment->{ bodyText }, + }); + + $var->{ commentForm_submit } + = WebGUI::Form::submit( $session, { + name => "submit", + value => "Save Comment", + }); + + return $var; +} + +#---------------------------------------------------------------------------- + +=head2 canAdd ( ) + +Override canAdd to ignore its permissions check. Permissions are handled +by the parent Gallery and other permissions methods. + +=cut + +sub canAdd { + return 1; +} + +#---------------------------------------------------------------------------- + +=head2 canComment ( [userId] ) + +Returns true if the user can comment on this asset. C is a WebGUI +user ID. If no userId is passed, check the current user. + +Users can comment on this GalleryFile if they are allowed to view and the album +allows comments. + +=cut + +sub canComment { + my $self = shift; + my $userId = shift || $self->session->user->userId; + my $album = $self->getParent; + + return 0 if !$self->canView($userId); + + return $album->canComment($userId); +} + +#---------------------------------------------------------------------------- + +=head2 canEdit ( [userId] ) + +Returns true if the user can edit this asset. C is a WebGUI user ID. +If no userId is passed, check the current user. + +Users can edit this GalleryFile if they are the owner or if they are able to edit +the parent Album asset. + +=cut + +sub canEdit { + my $self = shift; + my $userId = shift || $self->session->user->userId; + my $album = $self->getParent; + + return 1 if $userId eq $self->get("ownerUserId"); + return $album->canEdit($userId); +} + +#---------------------------------------------------------------------------- + +=head2 canView ( [userId] ) + +Returns true if the user can view this asset. C is a WebGUI user ID. +If no user is passed, checks the current user. + +Users can view this GalleryFile if they can view the parent asset. If this is a +C GalleryFile, then they must also be in the owners friends list. + +=cut + +sub canView { + my $self = shift; + my $userId = shift || $self->session->user->userId; + + my $album = $self->getParent; + return 0 unless $album->canView($userId); + + if ($self->isFriendsOnly && $userId != $self->get("ownerUserId") ) { + my $owner = WebGUI::User->new( $self->session, $self->get("ownerUserId") ); + return 0 + unless WebGUI::Friends->new($self->session, $owner)->isFriend($userId); + } + + # Passed all checks + return 1; +} + +#---------------------------------------------------------------------------- + +=head2 deleteComment ( commentId ) + +Delete a comment from this asset. C is the ID of the comment to delete. + +=cut + +sub deleteComment { + my $self = shift; + my $commentId = shift; + + croak "GalleryFile->deleteComment: No commentId specified." + unless $commentId; + + return $self->session->db->write( + "DELETE FROM GalleryFile_comment WHERE assetId=? AND commentId=?", + [$self->getId, $commentId], + ); +} + +#---------------------------------------------------------------------------- + +=head2 getAutoCommitWorkflowId ( ) + +Returns the workflowId of the Gallery's approval workflow. + +=cut + +sub getAutoCommitWorkflowId { + my $self = shift; + return $self->getGallery->get("workflowIdCommit"); +} + +#---------------------------------------------------------------------------- + +=head2 getComment ( commentId ) + +Get a comment from this asset. C is the ID of the comment to get. Returns +a hash reference of comment information. + +=cut + +sub getComment { + my $self = shift; + my $commentId = shift; + + return $self->session->db->getRow( + "GalleryFile_comment", "commentId", $commentId, + ); +} + +#---------------------------------------------------------------------------- + +=head2 getCommentIds ( ) + +Get an array reference of comment IDs for this GalleryFile, in chronological order. + +=cut + +sub getCommentIds { + my $self = shift; + + return [ + $self->session->db->buildArray( + "SELECT commentId FROM GalleryFile_comment WHERE assetId=?", + [$self->getId], + ) + ]; +} + +#---------------------------------------------------------------------------- + +=head2 getCommentPaginator ( ) + +Get a WebGUI::Paginator for the comments for this GalleryFile. + +=cut + +sub getCommentPaginator { + my $self = shift; + my $session = $self->session; + + my $p = WebGUI::Paginator->new($session, $self->getUrl); + $p->setDataByQuery( + "SELECT * FROM GalleryFile_comment WHERE assetId=? ORDER BY creationDate DESC", + undef, undef, + [$self->getId], + ); + + return $p; +} + +#---------------------------------------------------------------------------- + +=head2 getGallery ( ) + +Gets the Gallery asset this GalleryFile is a member of. + +=cut + +sub getGallery { + my $self = shift; + my $gallery = $self->getParent->getParent; + return $gallery if $gallery->isa("WebGUI::Asset::Wobject::Gallery"); + return undef; +} + #---------------------------------------------------------------------------- =head2 getThumbnailUrl ( ) @@ -46,6 +353,7 @@ overridded by your child class. sub getThumbnailUrl { my $self = shift; + # TODO: Make a "default" thumbnail } #---------------------------------------------------------------------------- @@ -59,14 +367,200 @@ method. sub getTemplateVars { my $self = shift; + my $session = $self->session; my $var = $self->get; + my $owner = WebGUI::User->new( $session, $self->get("ownerUserId") ); $var->{ fileUrl } = $self->getFileUrl; $var->{ thumbnailUrl } = $self->getThumbnailUrl; + # Fix 'undef' vars since HTML::Template does inheritence on them + for my $key ( qw( synopsis ) ) { + unless ( defined $var->{$key} ) { + $var->{ $key } = ''; + } + } + + # Add some things from Gallery + my $galleryVar = $self->getGallery->getTemplateVars; + for my $key ( qw{ url_listFilesForCurrentUser url_search } ) { + $var->{ $key } = $galleryVar->{ $key }; + } + + # Add the search form + $self->getGallery->appendTemplateVarsSearchForm( $var ); + + $var->{ canComment } = $self->canComment; + $var->{ canEdit } = $self->canEdit; + $var->{ numberOfComments } = scalar @{ $self->getCommentIds }; + $var->{ ownerUsername } = $owner->profileField("alias") || $owner->username; + $var->{ url } = $self->getUrl; + $var->{ url_addArchive } = $self->getParent->getUrl('func=addArchive'), + $var->{ url_delete } = $self->getUrl('func=delete'); + $var->{ url_demote } = $self->getUrl('func=demote'); + $var->{ url_edit } = $self->getUrl('func=edit'); + $var->{ url_gallery } = $self->getGallery->getUrl; + $var->{ url_makeShortcut } = $self->getUrl('func=makeShortcut'); + $var->{ url_listFilesForOwner } + = $self->getGallery->getUrl('func=listFilesForUser;userId=' . $self->get("ownerUserId")); + $var->{ url_promote } = $self->getUrl('func=promote'); + return $var; } + +#---------------------------------------------------------------------------- + +=head2 i18n ( session ) + +Get the i18n object for this class. This sub must not be inherited, so always +call it using C<__PACKAGE__>, not C<$self>. + +=cut + +sub i18n { + my $self = shift; + my $session = shift; + # TODO: Make a migration script to move the appropriate parts from + # Asset_Photo to Asset_GalleryFile + return WebGUI::International->new( $session, "Asset_Photo" ); +} + +#---------------------------------------------------------------------------- + +=head2 isFriendsOnly ( ) + +Returns true if this GalleryFile is friends only. Returns false otherwise. + +=cut + +sub isFriendsOnly { + my $self = shift; + return $self->get("friendsOnly"); +} + +#---------------------------------------------------------------------------- + +=head2 makeShortcut ( parentId [, overrides ] ) + +Make a shortcut to this asset under the specified parent, optionally adding +the specified hash reference of C. + +Returns the created shortcut asset. + +=cut + +sub makeShortcut { + my $self = shift; + my $parentId = shift; + my $overrides = shift; + my $session = $self->session; + + croak "GalleryFile->makeShortcut: parentId must be defined" + unless $parentId; + + my $parent = WebGUI::Asset->newByDynamicClass($session, $parentId) + || croak "GalleryFile->makeShortcut: Could not instanciate asset '$parentId'"; + + my $shortcut + = $parent->addChild({ + className => "WebGUI::Asset::Shortcut", + shortcutToAssetId => $self->getId, + }); + + if ($overrides) { + $shortcut->setOverride( $overrides ); + } + + return $shortcut; +} + +#---------------------------------------------------------------------------- + +=head2 prepareView ( ) + +Prepare the template to be used for the C method. + +=cut + +sub prepareView { + my $self = shift; + $self->SUPER::prepareView(); + + my $template + = WebGUI::Asset::Template->new($self->session, $self->getGallery->get("templateIdViewFile")); + $template->prepare; + + $self->{_viewTemplate} = $template; +} + +#---------------------------------------------------------------------------- + +=head2 processCommentEditForm ( ) + +Process the Comment Add / Edit Form. Returns a hash reference of properties +that can be passed to C. + +Will die with an i18n-friendly error message if something is missing or +wrong. + +=cut + +sub processCommentEditForm { + my $self = shift; + my $session = $self->session; + my $form = $self->session->form; + my $now = WebGUI::DateTime->new( $session, time ); + my $i18n = __PACKAGE__->i18n( $session ); + + # Using die here to suppress line number and file path info + die $i18n->get("commentForm error no commentId") . "\n" + unless $form->get("commentId"); + die $i18n->get("commentForm error no bodyText") . "\n" + unless $form->get("bodyText"); + + my $new = $form->get('commentId') eq "new" + ? 1 + : 0 + ; + + my $visitorIp = $session->user->userId eq "1" + ? $session->env->get("REMOTE_ADDR") + : undef + ; + + my $properties = { + commentId => $form->get("commentId"), + assetId => $self->getId, + bodyText => $form->get("bodyText"), + creationDate => ( $new ? $now->toDatabaseDate : $form->get("creationDate") ), + userId => ( $new ? $session->user->userId : $form->get("userId") ), + visitorIp => ( $new ? $visitorIp : $form->get("visitorIp") ), + }; + + return $properties; +} + +#---------------------------------------------------------------------------- + +=head2 processPropertiesFromFormPost ( ) + + +=cut + +sub processPropertiesFromFormPost { + my $self = shift; + my $form = $self->session->form; + my $errors = $self->SUPER::processPropertiesFromFormPost || []; + + # Return if errors + return $errors if @$errors; + + ### Passes all checks + + $self->requestAutoCommit; +} + #---------------------------------------------------------------------------- =head2 processStyle ( html ) @@ -82,6 +576,186 @@ sub processStyle { #---------------------------------------------------------------------------- +=head2 purge ( ) + +Purge the asset. Remove all comments on the GalleryFile. + +=cut + +sub purge { + my $self = shift; + + for my $commentId ( @{ $self->getCommentIds } ) { + $self->deleteComment( $commentId ); + } + + return $self->SUPER::purge; +} + +#---------------------------------------------------------------------------- + +=head2 setComment ( properties ) + +Set a comment. C is a hash reference of comment information with +the following keys: + + assetId - The assetId of the asset this comment is for + commentId - The ID of the comment. If "new", will make a new comment. + bodyText - The body of the comment + userId - The userId of the user who made the comment + visitorIp - If the user was a visitor, the IP address of the user + creationDate - A MySQL-formatted date/time when the comment was posted + +=cut + +sub setComment { + my $self = shift; + my $properties = shift; + + croak "GalleryFile->setComment: properties must be a hash reference" + unless $properties && ref $properties eq "HASH"; + croak "GalleryFile->setComment: commentId must be defined" + unless $properties->{ commentId }; + croak "GalleryFile->setComment: properties must contain a bodyText key" + unless $properties->{ bodyText }; + + $properties->{ creationDate } ||= WebGUI::DateTime->new($self->session, time)->toDatabase; + $properties->{ assetId } = $self->getId; + + return $self->session->db->setRow( + "GalleryFile_comment", "commentId", + $properties, + ); +} + +#---------------------------------------------------------------------------- + +=head2 view ( ) + +method called by the container www_view method. + +=cut + +sub view { + my $self = shift; + my $session = $self->session; + my $var = $self->getTemplateVars; + + $self->appendTemplateVarsCommentForm( $var ); + + # Keywords + my $k = WebGUI::Keyword->new( $session ); + my $keywords = $k->getKeywordsForAsset( { asArrayRef => 1, asset => $self } ); + for my $keyword ( @{ $keywords } ) { + push @{ $var->{keywords} }, { + keyword => $keyword, + url_searchKeyword + => $self->getGallery->getUrl( + "func=search;submit=1;keywords=" . uri_escape($keyword) + ), + url_searchKeywordUser + => $self->getGallery->getUrl( + "func=search;submit=1;" + . "userId=" . $self->get("ownerUserId") . ';' + . 'keywords=' . uri_escape( $keyword ) + ), + }; + } + + # Comments + my $p = $self->getCommentPaginator; + for my $comment ( @{ $p->getPageData } ) { + $comment->{ url_deleteComment } + = $self->getUrl('func=deleteComment;commentId=' . $comment->{commentId} ); + $comment->{ url_editComment } + = $self->getUrl('func=editComment;commentId=' . $comment->{commentId} ); + + my $user = WebGUI::User->new( $session, $comment->{userId} ); + $comment->{ username } = $user->username; + + my $dt = WebGUI::DateTime->new( $session, $comment->{ creationDate } ); + $comment->{ creationDate } = $dt->toUserTimeZone; + + push @{ $var->{commentLoop} }, $comment; + } + $var->{ commentLoop_pageBar } = $p->getBarAdvanced; + + return $self->processTemplate($var, undef, $self->{_viewTemplate}); +} + +#---------------------------------------------------------------------------- + +=head2 www_delete ( ) + +Show the page to confirm the deletion of this GalleryFile. Show a list of albums +this GalleryFile exists in. + +=cut + +sub www_delete { + my $self = shift; + my $session = $self->session; + + return $self->session->privilege->insufficient unless $self->canEdit; + + my $var = $self->getTemplateVars; + $var->{ url_yes } = $self->getUrl("func=deleteConfirm"); + + # TODO Get albums with shortcuts to this asset + + return $self->processStyle( + $self->processTemplate( $var, $self->getGallery->get("templateIdDeleteFile") ) + ); +} + +#---------------------------------------------------------------------------- + +=head2 www_deleteComment ( ) + +Delete a comment immediately. Only those who can edit this GalleryFile can delete +comments on it. + +=cut + +sub www_deleteComment { + my $self = shift; + my $session = $self->session; + + return $session->privilege->insufficient unless $self->canEdit; + + my $i18n = __PACKAGE__->i18n( $session ); + my $commentId = $session->form->get('commentId'); + + $self->deleteComment( $commentId ); + + return $self->www_view; +} + +#---------------------------------------------------------------------------- + +=head2 www_deleteConfirm ( ) + +Confirm the deletion of this GalleryFile. Show a message and a link back to the +album. + +=cut + +sub www_deleteConfirm { + my $self = shift; + + return $self->session->privilege->insufficient unless $self->canEdit; + + my $i18n = __PACKAGE__->i18n( $self->session ); + + $self->purge; + + return $self->processStyle( + sprintf $i18n->get("delete message"), $self->getParent->getUrl, + ); +} + +#---------------------------------------------------------------------------- + =head2 www_demote Override the default demote page to send the user back to the GalleryAlbum @@ -101,6 +775,151 @@ sub www_demote { #---------------------------------------------------------------------------- +=head2 www_editComment ( params ) + +Form to edit a comment. C is a hash reference of parameters +with the following keys: + + errors = An array reference of errors to show the user. + +=cut + +sub www_editComment { + my $self = shift; + my $params = shift; + my $session = $self->session; + + return $session->privilege->insufficient unless $self->canEdit; + + my $var = $self->getTemplateVars; + + if ( $params->{ errors } ) { + $var->{ errors } = [ map { { "error" => $_ } } @{ $params->{errors} } ]; + } + + my $commentId = $session->form->get( "commentId" ); + my $comment = $commentId ne "new" + ? $self->getComment( $commentId ) + : {} + ; + $self->appendTemplateVarsCommentForm( $var, $comment ); + + $var->{ isNew } = $commentId eq "new"; + + return $self->processStyle( + $self->processTemplate( $var, $self->getGallery->get("templateIdEditComment") ) + ); +} + +#---------------------------------------------------------------------------- + +=head2 www_editCommentSave ( ) + +Save a comment being edited + +=cut + +sub www_editCommentSave { + my $self = shift; + my $session = $self->session; + + return $session->privilege->insufficient unless $self->canEdit; + + my $i18n = __PACKAGE__->i18n( $session ); + + my $comment = eval { $self->processCommentEditForm }; + if ( $@ ) { + return $self->www_editComment( { errors => [ $@ ] } ); + } + + # setComment changes commentId, so keep track if we're adding a new comment + my $isNew = $comment->{commentId} eq "new"; + + $self->setComment( $comment ); + + # Return different message for adding and editing + if ( $isNew ) { + return $self->processStyle( + sprintf $i18n->get('comment message'), $self->getUrl + ); + } + else { + return $self->processStyle( + sprintf $i18n->get('editCommentSave message'), $self->getUrl + ); + } +} + +#---------------------------------------------------------------------------- + +=head2 www_makeShortcut ( ) + +Display the form to make a shortcut. + +This page is only available to those who can edit this GalleryFile. + +=cut + +sub www_makeShortcut { + my $self = shift; + my $session = $self->session; + + return $self->session->privilege->insufficient unless $self->canEdit; + + # Create the form to make a shortcut + my $var = $self->getTemplateVars; + + $var->{ form_start } + = WebGUI::Form::formHeader( $session ) + . WebGUI::Form::hidden( $session, { name => "func", value => "makeShortcutSave" }); + $var->{ form_end } + = WebGUI::Form::formFooter( $session ); + + # Albums under this Gallery + my $albums = $self->getGallery->getAlbumIds; + my %albumOptions; + for my $assetId ( @$albums ) { + my $asset = WebGUI::Asset->newByDynamicClass($session, $assetId); + if ($asset->canAddFile) { + $albumOptions{ $assetId } = $asset->get("title"); + } + } + $var->{ form_parentId } + = WebGUI::Form::selectBox( $session, { + name => "parentId", + value => $self->getParent->getId, + options => \%albumOptions, + }); + + return $self->processStyle( + $self->processTemplate($var, $self->getGallery->get("templateIdMakeShortcut")) + ); +} + +#---------------------------------------------------------------------------- + +=head2 www_makeShortcutSave ( ) + +Make the shortcut. + +This page is only available to those who can edit this GalleryFile. + +=cut + +sub www_makeShortcutSave { + my $self = shift; + my $form = $self->session->form; + + return $self->session->privilege->insufficient unless $self->canEdit; + + my $parentId = $form->get('parentId'); + my $shortcut = $self->makeShortcut( $parentId ); + + return $shortcut->www_view; +} + +#---------------------------------------------------------------------------- + =head2 www_promote Override the default promote page to send the user back to the GalleryAlbum @@ -122,12 +941,19 @@ sub www_promote { =head2 www_view ( ) -Show the default view, with content chunking. +Shows the output of L inside of the style provided by the gallery this +GalleryFile is in. =cut sub www_view { - my $self = shift; + my $self = shift; + + return $self->session->privilege->insufficient unless $self->canView; + + # Add to views + $self->update({ views => $self->get('views') + 1 }); + $self->session->http->setLastModified($self->getContentLastModified); $self->session->http->sendHeader; $self->prepareView; @@ -139,4 +965,5 @@ sub www_view { return "chunked"; } + 1; # Who knew the truth would be so obvious? diff --git a/lib/WebGUI/Asset/File/GalleryFile/Photo.pm b/lib/WebGUI/Asset/File/GalleryFile/Photo.pm index 0fab64b66..f3af924b6 100644 --- a/lib/WebGUI/Asset/File/GalleryFile/Photo.pm +++ b/lib/WebGUI/Asset/File/GalleryFile/Photo.pm @@ -73,37 +73,20 @@ sub definition { my $i18n = __PACKAGE__->i18n($session); tie my %properties, 'Tie::IxHash', ( - views => { - defaultValue => 0, - }, exifData => { defaultValue => undef, }, - friendsOnly => { - defaultValue => 0, - }, location => { defaultValue => undef, }, - rating => { - defaultValue => 0, - }, ); - # UserDefined Fields - for my $i (1 .. 5) { - $properties{"userDefined".$i} = { - defaultValue => undef, - }; - } - push @{$definition}, { assetName => $i18n->get('assetName'), autoGenerateForms => 0, icon => 'photo.gif', tableName => 'Photo', className => 'WebGUI::Asset::File::GalleryFile::Photo', - i18n => 'Asset_Photo', properties => \%properties, }; @@ -112,74 +95,6 @@ sub definition { #---------------------------------------------------------------------------- -=head2 appendTemplateVarsCommentForm ( var [, comment ] ) - -Add the template variables necessary for the comment form to the given hash -reference. Returns the hash reference for convenience. C is a hash -reference of values to populate the form with. - -=cut - -sub appendTemplateVarsCommentForm { - my $self = shift; - my $var = shift; - my $comment = shift || {}; - my $session = $self->session; - - # Default comment - $comment->{ commentId } ||= "new"; - - $var->{ commentForm_start } - = WebGUI::Form::formHeader( $session ) - . WebGUI::Form::hidden( $session, { - name => "func", - value => "editCommentSave" - } ) - . WebGUI::Form::hidden( $session, { - name => "commentId", - value => $comment->{ commentId } - } ) - ; - - # Add hidden fields for editing a comment - if ( $comment->{ commentId } ne "new" ) { - $var->{ commentForm_start } - .= WebGUI::Form::hidden( $session, { - name => "userId", - value => $comment->{ userId } - } ) - . WebGUI::Form::hidden( $session, { - name => "visitorIp", - value => $comment->{ visitorIp } - } ) - . WebGUI::Form::hidden( $session, { - name => "creationDate", - value => $comment->{ creationDate } - } ) - ; - } - - $var->{ commentForm_end } - = WebGUI::Form::formFooter( $session ); - - $var->{ commentForm_bodyText } - = WebGUI::Form::HTMLArea( $session, { - name => "bodyText", - richEditId => $self->getGallery->get("richEditIdComment"), - value => $comment->{ bodyText }, - }); - - $var->{ commentForm_submit } - = WebGUI::Form::submit( $session, { - name => "submit", - value => "Save Comment", - }); - - return $var; -} - -#---------------------------------------------------------------------------- - =head2 applyConstraints ( options ) Apply the constraints to the original file. Called automatically by C @@ -211,112 +126,6 @@ sub applyConstraints { $self->updateExifDataFromFile; } -#---------------------------------------------------------------------------- - -=head2 canAdd ( ) - -Override canAdd to ignore its permissions check. Permissions are handled -by the parent Gallery and other permissions methods. - -=cut - -sub canAdd { - return 1; -} - -#---------------------------------------------------------------------------- - -=head2 canComment ( [userId] ) - -Returns true if the user can comment on this asset. C is a WebGUI -user ID. If no userId is passed, check the current user. - -Users can comment on this Photo if they are allowed to view and the album -allows comments. - -=cut - -sub canComment { - my $self = shift; - my $userId = shift || $self->session->user->userId; - my $album = $self->getParent; - - return 0 if !$self->canView($userId); - - return $album->canComment($userId); -} - -#---------------------------------------------------------------------------- - -=head2 canEdit ( [userId] ) - -Returns true if the user can edit this asset. C is a WebGUI user ID. -If no userId is passed, check the current user. - -Users can edit this Photo if they are the owner or if they are able to edit -the parent Album asset. - -=cut - -sub canEdit { - my $self = shift; - my $userId = shift || $self->session->user->userId; - my $album = $self->getParent; - - return 1 if $userId eq $self->get("ownerUserId"); - return $album->canEdit($userId); -} - -#---------------------------------------------------------------------------- - -=head2 canView ( [userId] ) - -Returns true if the user can view this asset. C is a WebGUI user ID. -If no user is passed, checks the current user. - -Users can view this photo if they can view the parent asset. If this is a -C photo, then they must also be in the owners friends list. - -=cut - -sub canView { - my $self = shift; - my $userId = shift || $self->session->user->userId; - - my $album = $self->getParent; - return 0 unless $album->canView($userId); - - if ($self->isFriendsOnly && $userId != $self->get("ownerUserId") ) { - my $owner = WebGUI::User->new( $self->session, $self->get("ownerUserId") ); - return 0 - unless WebGUI::Friends->new($self->session, $owner)->isFriend($userId); - } - - # Passed all checks - return 1; -} - -#---------------------------------------------------------------------------- - -=head2 deleteComment ( commentId ) - -Delete a comment from this asset. C is the ID of the comment to delete. - -=cut - -sub deleteComment { - my $self = shift; - my $commentId = shift; - - croak "Photo->deleteComment: No commentId specified." - unless $commentId; - - return $self->session->db->write( - "DELETE FROM Photo_comment WHERE assetId=? AND commentId=?", - [$self->getId, $commentId], - ); -} - #------------------------------------------------------------------- =head2 generateThumbnail ( ) @@ -336,78 +145,6 @@ sub generateThumbnail { #---------------------------------------------------------------------------- -=head2 getAutoCommitWorkflowId ( ) - -Returns the workflowId of the Gallery's approval workflow. - -=cut - -sub getAutoCommitWorkflowId { - my $self = shift; - return $self->getGallery->get("workflowIdCommit"); -} - -#---------------------------------------------------------------------------- - -=head2 getComment ( commentId ) - -Get a comment from this asset. C is the ID of the comment to get. Returns -a hash reference of comment information. - -=cut - -sub getComment { - my $self = shift; - my $commentId = shift; - - return $self->session->db->getRow( - "Photo_comment", "commentId", $commentId, - ); -} - -#---------------------------------------------------------------------------- - -=head2 getCommentIds ( ) - -Get an array reference of comment IDs for this Photo, in chronological order. - -=cut - -sub getCommentIds { - my $self = shift; - - return [ - $self->session->db->buildArray( - "SELECT commentId FROM Photo_comment WHERE assetId=?", - [$self->getId], - ) - ]; -} - -#---------------------------------------------------------------------------- - -=head2 getCommentPaginator ( ) - -Get a WebGUI::Paginator for the comments for this Photo. - -=cut - -sub getCommentPaginator { - my $self = shift; - my $session = $self->session; - - my $p = WebGUI::Paginator->new($session, $self->getUrl); - $p->setDataByQuery( - "SELECT * FROM Photo_comment WHERE assetId=? ORDER BY creationDate DESC", - undef, undef, - [$self->getId], - ); - - return $p; -} - -#---------------------------------------------------------------------------- - =head2 getDownloadFileUrl ( resolution ) Get the absolute URL to download the requested resolution. Will croak if the @@ -424,7 +161,7 @@ sub getDownloadFileUrl { croak "Photo->getDownloadFileUrl: resolution doesn't exist for this Photo" unless grep /$resolution/, @{ $self->getResolutions }; - return $self->getStorageLocation->getFileUrl( $resolution . ".jpg" ); + return $self->getStorageLocation->getUrl( $resolution . ".jpg" ); } #---------------------------------------------------------------------------- @@ -444,21 +181,6 @@ sub getExifData { #---------------------------------------------------------------------------- -=head2 getGallery ( ) - -Gets the Gallery asset this Photo is a member of. - -=cut - -sub getGallery { - my $self = shift; - my $gallery = $self->getParent->getParent; - return $gallery if $gallery->isa("WebGUI::Asset::Wobject::Gallery"); - return undef; -} - -#---------------------------------------------------------------------------- - =head2 getResolutions ( ) Get an array reference of download resolutions that exist for this image. @@ -471,7 +193,7 @@ sub getResolutions { my $storage = $self->getStorageLocation; # Return a list not including the web view image. - return grep { $_ ne $self->get("filename") } @{ $storage->getFiles }; + return [ grep { $_ ne $self->get("filename") } @{ $storage->getFiles } ]; } #---------------------------------------------------------------------------- @@ -499,32 +221,9 @@ sub getTemplateVars { my $self = shift; my $session = $self->session; my $var = $self->SUPER::getTemplateVars; - my $owner = WebGUI::User->new( $session, $self->get("ownerUserId") ); - - # Fix 'undef' vars since HTML::Template does inheritence on them - for my $key ( qw( synopsis ) ) { - unless ( defined $var->{$key} ) { - $var->{ $key } = ''; - } - } - - $var->{ canComment } = $self->canComment; - $var->{ canEdit } = $self->canEdit; - $var->{ numberOfComments } = scalar @{ $self->getCommentIds }; - $var->{ ownerUsername } = $owner->username; - $var->{ url } = $self->getUrl; - $var->{ url_addArchive } = $self->getParent->getUrl('func=addArchive'), - $var->{ url_delete } = $self->getUrl('func=delete'); - $var->{ url_demote } = $self->getUrl('func=demote'); - $var->{ url_edit } = $self->getUrl('func=edit'); - $var->{ url_gallery } = $self->getGallery->getUrl; - $var->{ url_makeShortcut } = $self->getUrl('func=makeShortcut'); - $var->{ url_listFilesForOwner } - = $self->getGallery->getUrl('func=listFilesForUser;userId=' . $self->get("ownerUserId")); - $var->{ url_promote } = $self->getUrl('func=promote'); ### Download resolutions - for my $resolution ( $self->getResolutions ) { + for my $resolution ( @{ $self->getResolutions } ) { push @{ $var->{ resolutions_loop } }, { url_download => $self->getStorageLocation->getPathFrag($resolution) }; @@ -581,19 +280,6 @@ sub i18n { #---------------------------------------------------------------------------- -=head2 isFriendsOnly ( ) - -Returns true if this Photo is friends only. Returns false otherwise. - -=cut - -sub isFriendsOnly { - my $self = shift; - return $self->get("friendsOnly"); -} - -#---------------------------------------------------------------------------- - =head2 makeResolutions ( [resolutions] ) Create the specified resolutions for this Photo. If resolutions is not @@ -630,182 +316,6 @@ sub makeResolutions { #---------------------------------------------------------------------------- -=head2 makeShortcut ( parentId [, overrides ] ) - -Make a shortcut to this asset under the specified parent, optionally adding -the specified hash reference of C. - -Returns the created shortcut asset. - -=cut - -sub makeShortcut { - my $self = shift; - my $parentId = shift; - my $overrides = shift; - my $session = $self->session; - - croak "Photo->makeShortcut: parentId must be defined" - unless $parentId; - - my $parent = WebGUI::Asset->newByDynamicClass($session, $parentId) - || croak "Photo->makeShortcut: Could not instanciate asset '$parentId'"; - - my $shortcut - = $parent->addChild({ - className => "WebGUI::Asset::Shortcut", - shortcutToAssetId => $self->getId, - }); - - if ($overrides) { - $shortcut->setOverride( $overrides ); - } - - return $shortcut; -} - -#---------------------------------------------------------------------------- - -=head2 prepareView ( ) - -Prepare the template to be used for the C method. - -=cut - -sub prepareView { - my $self = shift; - $self->SUPER::prepareView(); - - my $template - = WebGUI::Asset::Template->new($self->session, $self->getGallery->get("templateIdViewFile")); - $template->prepare; - - $self->{_viewTemplate} = $template; -} - -#---------------------------------------------------------------------------- - -=head2 processCommentEditForm ( ) - -Process the Comment Add / Edit Form. Returns a hash reference of properties -that can be passed to C. - -Will die with an i18n-friendly error message if something is missing or -wrong. - -=cut - -sub processCommentEditForm { - my $self = shift; - my $session = $self->session; - my $form = $self->session->form; - my $now = WebGUI::DateTime->new( $session, time ); - my $i18n = __PACKAGE__->i18n( $session ); - - # Using die here to suppress line number and file path info - die $i18n->get("commentForm error no commentId") . "\n" - unless $form->get("commentId"); - die $i18n->get("commentForm error no bodyText") . "\n" - unless $form->get("bodyText"); - - my $new = $form->get('commentId') eq "new" - ? 1 - : 0 - ; - - my $visitorIp = $session->user->userId eq "1" - ? $session->env->get("REMOTE_ADDR") - : undef - ; - - my $properties = { - commentId => $form->get("commentId"), - assetId => $self->getId, - bodyText => $form->get("bodyText"), - creationDate => ( $new ? $now->toDatabaseDate : $form->get("creationDate") ), - userId => ( $new ? $session->user->userId : $form->get("userId") ), - visitorIp => ( $new ? $visitorIp : $form->get("visitorIp") ), - }; - - return $properties; -} - -#---------------------------------------------------------------------------- - -=head2 processPropertiesFromFormPost ( ) - - -=cut - -sub processPropertiesFromFormPost { - my $self = shift; - my $form = $self->session->form; - my $errors = $self->SUPER::processPropertiesFromFormPost || []; - - # Return if errors - return $errors if @$errors; - - ### Passes all checks - - $self->requestAutoCommit; -} - -#---------------------------------------------------------------------------- - -=head2 purge ( ) - -Purge the asset. Remove all comments on the photo. - -=cut - -sub purge { - my $self = shift; - - for my $commentId ( @{ $self->getCommentIds } ) { - $self->deleteComment( $commentId ); - } - - return $self->SUPER::purge; -} - -#---------------------------------------------------------------------------- - -=head2 setComment ( properties ) - -Set a comment. C is a hash reference of comment information with -the following keys: - - assetId - The assetId of the asset this comment is for - commentId - The ID of the comment. If "new", will make a new comment. - bodyText - The body of the comment - userId - The userId of the user who made the comment - visitorIp - If the user was a visitor, the IP address of the user - creationDate - A MySQL-formatted date/time when the comment was posted - -=cut - -sub setComment { - my $self = shift; - my $properties = shift; - - croak "Photo->setComment: properties must be a hash reference" - unless $properties && ref $properties eq "HASH"; - croak "Photo->setComment: commentId must be defined" - unless $properties->{ commentId }; - croak "Photo->setComment: properties must contain a bodyText key" - unless $properties->{ bodyText }; - - $properties->{ creationDate } ||= WebGUI::DateTime->new($self->session, time)->toDatabase; - $properties->{ assetId } = $self->getId; - - return $self->session->db->setRow( - "Photo_comment", "commentId", - $properties, - ); -} - -#---------------------------------------------------------------------------- - =head2 setFile ( filename ) Extend the superclass setFile to automatically generate thumbnails. @@ -848,124 +358,6 @@ sub updateExifDataFromFile { #---------------------------------------------------------------------------- -=head2 view ( ) - -method called by the container www_view method. - -=cut - -sub view { - my $self = shift; - my $session = $self->session; - my $var = $self->getTemplateVars; - - $self->appendTemplateVarsCommentForm( $var ); - - # Keywords - my $k = WebGUI::Keyword->new( $session ); - my $keywords = $k->getKeywordsForAsset( { asArrayRef => 1, asset => $self } ); - for my $keyword ( @{ $keywords } ) { - push @{ $var->{keywords} }, { - url_searchKeyword - => $self->getGallery->getUrl("func=search;submit=1;keywords=" . uri_escape($keyword) ), - keyword => $keyword, - }; - } - - # Comments - my $p = $self->getCommentPaginator; - for my $comment ( @{ $p->getPageData } ) { - $comment->{ url_deleteComment } - = $self->getUrl('func=deleteComment;commentId=' . $comment->{commentId} ); - $comment->{ url_editComment } - = $self->getUrl('func=editComment;commentId=' . $comment->{commentId} ); - - my $user = WebGUI::User->new( $session, $comment->{userId} ); - $comment->{ username } = $user->username; - - my $dt = WebGUI::DateTime->new( $session, $comment->{ creationDate } ); - $comment->{ creationDate } = $dt->toUserTimeZone; - - push @{ $var->{commentLoop} }, $comment; - } - $var->{ commentLoop_pageBar } = $p->getBarAdvanced; - - return $self->processTemplate($var, undef, $self->{_viewTemplate}); -} - -#---------------------------------------------------------------------------- - -=head2 www_delete ( ) - -Show the page to confirm the deletion of this Photo. Show a list of albums -this Photo exists in. - -=cut - -sub www_delete { - my $self = shift; - my $session = $self->session; - - return $self->session->privilege->insufficient unless $self->canEdit; - - my $var = $self->getTemplateVars; - $var->{ url_yes } = $self->getUrl("func=deleteConfirm"); - - # TODO Get albums with shortcuts to this asset - - return $self->processStyle( - $self->processTemplate( $var, $self->getGallery->get("templateIdDeleteFile") ) - ); -} - -#---------------------------------------------------------------------------- - -=head2 www_deleteComment ( ) - -Delete a comment immediately. Only those who can edit this Photo can delete -comments on it. - -=cut - -sub www_deleteComment { - my $self = shift; - my $session = $self->session; - - return $session->privilege->insufficient unless $self->canEdit; - - my $i18n = __PACKAGE__->i18n( $session ); - my $commentId = $session->form->get('commentId'); - - $self->deleteComment( $commentId ); - - return $self->www_view; -} - -#---------------------------------------------------------------------------- - -=head2 www_deleteConfirm ( ) - -Confirm the deletion of this Photo. Show a message and a link back to the -album. - -=cut - -sub www_deleteConfirm { - my $self = shift; - - return $self->session->privilege->insufficient unless $self->canEdit; - - my $i18n = __PACKAGE__->i18n( $self->session ); - - $self->purge; - - return $self->processStyle( - sprintf $i18n->get("delete message"), $self->getParent->getUrl, - ); -} - -#---------------------------------------------------------------------------- - =head2 www_download Download the Photo with the specified resolution. If no resolution specified, @@ -1101,146 +493,6 @@ sub www_edit { #---------------------------------------------------------------------------- -=head2 www_editComment ( params ) - -Form to edit a comment. C is a hash reference of parameters -with the following keys: - - errors = An array reference of errors to show the user. - -=cut - -sub www_editComment { - my $self = shift; - my $params = shift; - my $session = $self->session; - - return $session->privilege->insufficient unless $self->canEdit; - - my $var = $self->getTemplateVars; - - if ( $params->{ errors } ) { - $var->{ errors } = [ map { { "error" => $_ } } @{ $params->{errors} } ]; - } - - my $commentId = $session->form->get( "commentId" ); - my $comment = $self->getComment( $commentId ); - $self->appendTemplateVarsCommentForm( $var, $comment ); - - return $self->processStyle( - $self->processTemplate( $var, $self->getGallery->get("templateIdEditComment") ) - ); -} - -#---------------------------------------------------------------------------- - -=head2 www_editCommentSave ( ) - -Save a comment being edited - -=cut - -sub www_editCommentSave { - my $self = shift; - my $session = $self->session; - - return $session->privilege->insufficient unless $self->canEdit; - - my $i18n = __PACKAGE__->i18n( $session ); - - my $comment = eval { $self->processCommentEditForm }; - if ( $@ ) { - return $self->www_editComment( { errors => [ $@ ] } ); - } - - # setComment changes commentId, so keep track if we're adding a new comment - my $isNew = $comment->{commentId} eq "new"; - - $self->setComment( $comment ); - - # Return different message for adding and editing - if ( $isNew ) { - return $self->processStyle( - sprintf $i18n->get('comment message'), $self->getUrl - ); - } - else { - return $self->processStyle( - sprintf $i18n->get('editCommentSave message'), $self->getUrl - ); - } -} - -#---------------------------------------------------------------------------- - -=head2 www_makeShortcut ( ) - -Display the form to make a shortcut. - -This page is only available to those who can edit this Photo. - -=cut - -sub www_makeShortcut { - my $self = shift; - my $session = $self->session; - - return $self->session->privilege->insufficient unless $self->canEdit; - - # Create the form to make a shortcut - my $var = $self->getTemplateVars; - - $var->{ form_start } - = WebGUI::Form::formHeader( $session ) - . WebGUI::Form::hidden( $session, { name => "func", value => "makeShortcutSave" }); - $var->{ form_end } - = WebGUI::Form::formFooter( $session ); - - # Albums under this Gallery - my $albums = $self->getGallery->getAlbumIds; - my %albumOptions; - for my $assetId ( @$albums ) { - my $asset = WebGUI::Asset->newByDynamicClass($session, $assetId); - if ($asset->canAddFile) { - $albumOptions{ $assetId } = $asset->get("title"); - } - } - $var->{ form_parentId } - = WebGUI::Form::selectBox( $session, { - name => "parentId", - value => $self->getParent->getId, - options => \%albumOptions, - }); - - return $self->processStyle( - $self->processTemplate($var, $self->getGallery->get("templateIdMakeShortcut")) - ); -} - -#---------------------------------------------------------------------------- - -=head2 www_makeShortcutSave ( ) - -Make the shortcut. - -This page is only available to those who can edit this Photo. - -=cut - -sub www_makeShortcutSave { - my $self = shift; - my $form = $self->session->form; - - return $self->session->privilege->insufficient unless $self->canEdit; - - my $parentId = $form->get('parentId'); - my $shortcut = $self->makeShortcut( $parentId ); - - return $shortcut->www_view; -} - -#---------------------------------------------------------------------------- - =head2 www_showConfirmation ( ) Shows the confirmation message after adding / editing a gallery album. @@ -1260,24 +512,4 @@ sub www_showConfirmation { ); } -#---------------------------------------------------------------------------- - -=head2 www_view ( ) - -Shows the output of L inside of the style provided by the gallery this -photo is in. - -=cut - -sub www_view { - my $self = shift; - - return $self->session->privilege->insufficient unless $self->canView; - - # Add to views - $self->update({ views => $self->get('views') + 1 }); - - return $self->SUPER::www_view; -} - 1; diff --git a/lib/WebGUI/Asset/Wobject/Gallery.pm b/lib/WebGUI/Asset/Wobject/Gallery.pm index 08ac3d984..7207fbb66 100644 --- a/lib/WebGUI/Asset/Wobject/Gallery.pm +++ b/lib/WebGUI/Asset/Wobject/Gallery.pm @@ -82,13 +82,6 @@ sub definition { label => $i18n->get("groupIdAddFile label"), hoverHelp => $i18n->get("groupIdAddFile description"), }, - groupIdModerator => { - tab => "security", - fieldType => "group", - defaultValue => 3, # Admins - label => $i18n->get("groupIdModerator label"), - hoverHelp => $i18n->get("groupIdModerator description"), - }, imageResolutions => { tab => "properties", fieldType => "checkList", @@ -963,6 +956,11 @@ sub www_search { . $db->quote( '%' . $form->get("description") . '%' ) ; } + if ( $form->get("userId") ) { + $where .= q{ AND assetData.ownerUserId = } + . $db->quote( $form->get("userId") ) + ; + } my $joinClass = [ 'WebGUI::Asset::Wobject::GalleryAlbum', @@ -983,6 +981,7 @@ sub www_search { . 'className=' . $form->get('className') . ';' . 'creationDate_after=' . $form->get('creationDate_after') . ';' . 'creationDate_before=' . $form->get('creationDate_before') . ';' + . 'userId=' . $form->get("userId") . ';' ); my $p diff --git a/lib/WebGUI/Asset/Wobject/GalleryAlbum.pm b/lib/WebGUI/Asset/Wobject/GalleryAlbum.pm index 81847bc6d..591e40067 100644 --- a/lib/WebGUI/Asset/Wobject/GalleryAlbum.pm +++ b/lib/WebGUI/Asset/Wobject/GalleryAlbum.pm @@ -120,16 +120,18 @@ sub addArchive { for my $filePath (@files) { my ($volume, $directory, $filename) = File::Spec->splitpath( $filePath ); - $self->session->errorHandler->info( "trying $filename" ); next if $filename =~ m{^[.]}; my $class = $gallery->getAssetClassForFile( $filePath ); next unless $class; # class is undef for those files the Gallery can't handle $self->session->errorHandler->info( "Adding $filename to album!" ); + # Remove the file extention + $filename =~ s{\.[^.]+}{}; + $properties->{ className } = $class; $properties->{ menuTitle } = $filename; $properties->{ title } = $filename; - $properties->{ url } = $self->getUrl . "/" . $filename; + $properties->{ url } = $self->session->url->urlize( $self->getUrl . "/" . $filename ); my $asset = $self->addChild( $properties, undef, undef, { skipAutoCommitWorkflows => 1 } ); $asset->setFile( $filePath ); diff --git a/lib/WebGUI/Help/Asset_Photo.pm b/lib/WebGUI/Help/Asset_Photo.pm index 4b2ace319..391891e72 100644 --- a/lib/WebGUI/Help/Asset_Photo.pm +++ b/lib/WebGUI/Help/Asset_Photo.pm @@ -257,6 +257,24 @@ our $HELP = { name => 'commentLoop_pageBar', description => 'helpvar commentLoop_pageBar', }, + { + name => 'keywords', + description => 'helpvar keywords', + variables => [ + { + name => 'keyword', + description => 'helpvar keyword', + }, + { + name => 'url_searchKeyword', + description => 'helpvar url_searchKeyword', + }, + { + name => 'url_searchKeywordUser', + description => 'helpvar url_searchKeywordUser', + }, + ], + }, ], }, diff --git a/lib/WebGUI/Asset/Wobject/Gallery/Utility.pm b/lib/WebGUI/Utility/Gallery.pm similarity index 96% rename from lib/WebGUI/Asset/Wobject/Gallery/Utility.pm rename to lib/WebGUI/Utility/Gallery.pm index c4f1da162..e9514d7f2 100644 --- a/lib/WebGUI/Asset/Wobject/Gallery/Utility.pm +++ b/lib/WebGUI/Utility/Gallery.pm @@ -1,4 +1,4 @@ -package WebGUI::Asset::Wobject::Gallery::Utility; +package WebGUI::Utility::Gallery; =head1 LEGAL @@ -26,7 +26,7 @@ use WebGUI::Storage::Image; =head1 NAME -WebGUI::Asset::Wobject::Gallery::Utility -- Utility functions for working +WebGUI::Utility::Gallery -- Utility functions for working with Gallery assets. =head1 DESCRIPTION @@ -38,8 +38,8 @@ This module is B to be used by the Gallery asset itself! =head1 SYNOPSIS - use WebGUI::Asset::Wobject::Gallery::Utility; - my $utility = "WebGUI::Asset::Wobject::Gallery::Utility" # <- not as cumbersome + use WebGUI::Utility::Gallery; + my $utility = "WebGUI::Utility::Gallery" # <- not as cumbersome # Add albums from a collaboration system's threads my $gallery = WebGUI::Asset::Wobject::Gallery->new( ... ); diff --git a/lib/WebGUI/i18n/English/Asset_GalleryAlbum.pm b/lib/WebGUI/i18n/English/Asset_GalleryAlbum.pm index 6f7aa0bd1..228dc378a 100644 --- a/lib/WebGUI/i18n/English/Asset_GalleryAlbum.pm +++ b/lib/WebGUI/i18n/English/Asset_GalleryAlbum.pm @@ -442,6 +442,23 @@ our $I18N = { context => 'Body text explaining what kinds of archives can be submitted', }, + 'template delete message' => { + message => q{Are you sure you wish to delete this gallery?}, + lastUpdated => 0, + context => q{The message for the delete page}, + }, + + 'template delete no' => { + message => q{No}, + lastUpdated => 0, + context => q{Label for button to cancel the delete}, + }, + + 'template delete yes' => { + message => q{Yes}, + lastUpdated => 0, + context => q{Label for button to confirm the delete}, + }, }; 1; diff --git a/lib/WebGUI/i18n/English/Asset_Photo.pm b/lib/WebGUI/i18n/English/Asset_Photo.pm index 638894f39..7d2e92f6e 100644 --- a/lib/WebGUI/i18n/English/Asset_Photo.pm +++ b/lib/WebGUI/i18n/English/Asset_Photo.pm @@ -510,6 +510,60 @@ our $I18N = { lastUpdated => 0, context => q{Error message for Photo comments}, }, + + 'helpvar keywords' => { + message => q{A loop over the keywords associated with this photo}, + lastUpdated => 0, + context => q{Description of template loop}, + }, + + 'helpvar keyword' => { + message => q{The keyword}, + lastUpdated => 0, + context => q{Description of template variable}, + }, + + 'helpvar url_searchKeyword' => { + message => q{A URL to the Gallery search page for this keyword}, + lastUpdated => 0, + context => q{Description of template variable}, + }, + + 'helpvar url_searchKeywordUser' => { + message => q{A URL to the Gallery search page for this keyword. Limits the search to Photos by this user.}, + lastUpdated => 0, + context => q{Description of template variable}, + }, + + 'template makeShortcut title' => { + message => q{Cross Publish}, + lastUpdated => 0, + context => q{Title for the make shortcut page}, + }, + + 'template makeShortcut file' => { + message => q{File}, + lastUpdated => 0, + context => q{Label for the file we're making a shortcut of}, + }, + + 'template makeShortcut album' => { + message => q{Album}, + lastUpdated => 0, + context => q{Label for the album in which to make the shortcut}, + }, + + 'template delete message' => { + message => q{Are you sure you wish to delete this gallery?}, + lastUpdated => 0, + context => q{The message for the delete page}, + }, + + 'template delete albums' => { + message => q{Photo is currently in these albums:}, + lastUpdated => 0, + context => q{Label for the albums the photo will be removed from.}, + }, }; diff --git a/sbin/migrateCollabToGallery.pl b/sbin/migrateCollabToGallery.pl index e850bd855..be9ab59ef 100644 --- a/sbin/migrateCollabToGallery.pl +++ b/sbin/migrateCollabToGallery.pl @@ -3,7 +3,7 @@ use lib "../lib"; use strict; use Getopt::Long; use Pod::Usage; -use WebGUI::Asset::Wobject::Gallery::Utility; +use WebGUI::Utility::Gallery; use WebGUI::Session; my $session = start(); @@ -11,7 +11,7 @@ my $session = start(); my $collab = getCollaborationFromArgs(); my $gallery = getGalleryFromArgs(); -WebGUI::Asset::Wobject::Gallery::Utility->addAlbumFromCollaboration( $gallery, $collab ); +WebGUI::Utility::Gallery->addAlbumFromCollaboration( $gallery, $collab ); finish($session); @@ -133,4 +133,4 @@ The WebGUI config file to use. =head1 DESCRIPTION This script migrates a collaboration system's threads into gallery albums. It -uses C for its major features. +uses C for its major features. diff --git a/t/Asset/File/GalleryFile/Photo/comment.t b/t/Asset/File/GalleryFile/Photo/comment.t index 25401a8d2..9c66976d3 100644 --- a/t/Asset/File/GalleryFile/Photo/comment.t +++ b/t/Asset/File/GalleryFile/Photo/comment.t @@ -50,7 +50,7 @@ $versionTags[-1]->commit; #---------------------------------------------------------------------------- # Tests -plan tests => 29; +plan tests => 32; #---------------------------------------------------------------------------- # Test with no comments @@ -184,11 +184,70 @@ ok( ); #---------------------------------------------------------------------------- -# Test appendTemplateVarsForCommentForm -TODO: { - local $TODO = "Test appendTemplateVarsForCommentForm"; - ok(0, "Test template variable generation"); -} +# Test appendTemplateVarsForCommentForm for a new comment +my $var = {}; +my $newVar = $photo->appendTemplateVarsCommentForm( $var ); + +is ( $var, $newVar, "appendTemplateVarsCommentForm returns the same hashref it's given" ); +cmp_deeply( + $var, + superhashof( { + commentForm_start => all( + re( qr/]+name="func"[^>]+value="editCommentSave"[^>]+>/ ), + re( qr/]+name="commentId"[^>]+value="new"[^>]+>/ ), + ), + commentForm_end => all( + re( qr{} ), + ), + commentForm_bodyText => all( + re( qr{]+>} ), + re( qr{TinyMCE}i ), + ), + commentForm_submit => all( + re( qr/]+type="submit"[^>]+name="submit"[^>]+value="Save Comment"[^>]+>/ ), + ), + } ), + "appendTemplateVarsCommentForm returns the correct structure", +); + +#---------------------------------------------------------------------------- +# Test appendTemplateVarsForCommentForm for an existing comment +$var = {}; +my $comment = { + commentId => "new", + bodyText => "New comment", + creationDate => WebGUI::DateTime->new( $session, time )->toDatabase, + userId => "3", +}; + +my $commentId = $photo->setComment( $comment ); + +$newVar = $photo->appendTemplateVarsCommentForm( $var, $photo->getComment( $commentId ) ); + +is ( $var, $newVar, "appendTemplateVarsCommentForm returns the same hashref it's given" ); +cmp_deeply( + $var, + superhashof( { + commentForm_start => all( + re( qr/]+name="func"[^>]+value="editCommentSave"[^>]+>/ ), + re( qr/]+name="commentId"[^>]+value="$commentId"[^>]+>/ ), + re( qr/]+name="creationDate"[^>]+value="$comment->{creationDate}"[^>]+>/ ), + re( qr/]+name="userId"[^>]+value="$comment->{userId}"[^>]+>/ ), + ), + commentForm_end => all( + re( qr{} ), + ), + commentForm_bodyText => all( + re( qr{]+>} ), + re( qr{TinyMCE}i ), + re( qr{$comment->{bodyText}} ), + ), + commentForm_submit => all( + re( qr/]+type="submit"[^>]+name="submit"[^>]+value="Save Comment"[^>]+>/ ), + ), + } ), + "appendTemplateVarsCommentForm returns the correct structure", +); #---------------------------------------------------------------------------- # Test www_editCommentSave page sanity checks diff --git a/t/Asset/File/GalleryFile/Photo/download.t b/t/Asset/File/GalleryFile/Photo/download.t index b69c03058..b67ada59e 100644 --- a/t/Asset/File/GalleryFile/Photo/download.t +++ b/t/Asset/File/GalleryFile/Photo/download.t @@ -19,6 +19,7 @@ use Scalar::Util qw( blessed ); use WebGUI::Test; use WebGUI::Session; use Test::More; +use Test::Deep; use WebGUI::Asset::File::GalleryFile::Photo; #---------------------------------------------------------------------------- @@ -35,6 +36,7 @@ my $gallery = $node->addChild({ className => "WebGUI::Asset::Wobject::Gallery", imageResolutions => "100\n200\n300", + groupIdView => 7, }); my $album = $gallery->addChild({ @@ -46,7 +48,7 @@ my $album skipAutoCommitWorkflows => 1, }); my $photo - = $gallery->addChild({ + = $album->addChild({ className => "WebGUI::Asset::File::GalleryFile::Photo", }, undef, @@ -59,12 +61,30 @@ $versionTags[-1]->commit; #---------------------------------------------------------------------------- # Tests -plan tests => 1; +plan tests => 3; + +#---------------------------------------------------------------------------- +# getResolutions returns an array reference of available resolutions +$photo->setFile( WebGUI::Test->getTestCollateralPath( "lamp.jpg" ) ); +cmp_deeply( + $photo->getResolutions, + bag( "100.jpg", "200.jpg", "300.jpg" ), + "getResolutions returns the correct array reference", +); + +#---------------------------------------------------------------------------- +# getDownloadFileUrl returns the URL to download the resolution +is( + $photo->getDownloadFileUrl("100"), + $photo->getStorageLocation->getUrl( "100.jpg" ), + "getDownloadFileUrl returns the URL to download the resolution", +); + +ok( + !eval{ $photo->getDownloadFileUrl("400"); 1 }, + "getDownloadFileUrl croaks if resolution doesn't exist", +); -TODO: { - local $TODO = 'Write some tests for download testing'; - ok(0, 'No tests yet'); -} #---------------------------------------------------------------------------- # Cleanup diff --git a/t/Asset/File/GalleryFile/Photo/editSave.t b/t/Asset/File/GalleryFile/Photo/editSave.t index 33613743c..a5224f9f1 100644 --- a/t/Asset/File/GalleryFile/Photo/editSave.t +++ b/t/Asset/File/GalleryFile/Photo/editSave.t @@ -19,7 +19,6 @@ use Scalar::Util qw( blessed ); use WebGUI::Test; use WebGUI::Session; use Test::More; -use WebGUI::Test::Maker::HTML; use WebGUI::Asset::File::GalleryFile::Photo; #---------------------------------------------------------------------------- @@ -33,10 +32,20 @@ $versionTags[-1]->set({name=>"Photo Test, add Gallery, Album and 1 Photo"}); $session->user( { userId => 3 } ); # Admins can do everything -my $maker = WebGUI::Test::Maker::HTML->new; +# Create a user for testing purposes +my $user = WebGUI::User->new( $session, "new" ); +$user->username( 'dufresne' ); +$user->addToGroups( ['3'] ); +my $identifier = 'ritahayworth'; +my $auth = WebGUI::Operation::Auth::getInstance( $session, $user->authMethod, $user->userId ); +$auth->saveParams( $user->userId, $user->authMethod, { + 'identifier' => Digest::MD5::md5_base64( $identifier ), +}); + my $gallery = $node->addChild({ className => "WebGUI::Asset::Wobject::Gallery", + groupIdAddFile => 3, # Admins }); my $album = $gallery->addChild({ @@ -61,67 +70,58 @@ $versionTags[-1]->commit; #---------------------------------------------------------------------------- # Tests -plan skip_all => "Tests are not working yet."; +plan skip_all => "Tests not working yet"; +#plan tests => 1; + +use_ok("Test::WWW::Mechanize"); +my $mech; #---------------------------------------------------------------------------- # Test permissions +$mech = Test::WWW::Mechanize->new; # Edit an existing photo -$maker->prepare({ - object => $photo, - method => "www_edit", - userId => "1", - test_privilege => "insufficient", -})->run; +$mech->get( $session->url->getSiteURL . $photo->getUrl("func=edit") ); +$mech->content_contains("permission denied"); + +$mech->get( $session->url->getSiteURL . $photo->getUrl("func=editSave") ); +$mech->content_contains("permission denied"); # Save a new photo -$maker->prepare({ - object => $photo, - method => "www_editSave", - userId => "1", - test_privilege => "insufficient", -})->run; +$mech->get( $session->url->getSiteURL . $album->getUrl("func=add;class=WebGUI::Asset::File::GalleryFile::Photo") ); +$mech->content_contains("permission denied"); + +$mech->get( $session->url->getSiteURL . $album->getUrl("func=editSave;assetId=new;class=WebGUI::Asset::File::GalleryFile::Photo") ); +$mech->content_contains("permission denied"); #---------------------------------------------------------------------------- # Test processPropertiesFromFormPost errors # TODO: This test should use i18n. # TODO: This error / test should occur in File, not Photo -$maker->prepare({ - object => $album, - method => "www_editSave", - formParams => { - assetId => "new", - className => "WebGUI::Asset::File::GalleryFile::Photo", +$mech = Test::WWW::Mechanize->new; +# Login mech object +$mech->get( $session->url->getSiteURL . '?op=auth;method=login;username=dufresne;identifier=ritahayworth' ); + +$mech->get_ok( $album->getUrl('func=add;class=WebGUI::Asset::File::GalleryFile::Photo') ); +$mech->submit_form( + with_fields => { + title => '', + newFile_file => '', }, - test_regex => [ - qr/You must select a file/, - qr/You must enter a title/, - ], -})->run; +); #---------------------------------------------------------------------------- # Test editSave success result # TODO: This test should use i18n -$maker->prepare({ - object => $album, - method => "www_editSave", - formParams => { - assetId => "new", - className => "WebGUI::Asset::File::GalleryFile::Photo", - }, - test_regex => [ - qr/awaiting approval and commit/, - ], -})->run; #---------------------------------------------------------------------------- # Cleanup END { - $gallery->purge; foreach my $versionTag (@versionTags) { $versionTag->rollback; } + $user->delete; } diff --git a/t/Asset/File/GalleryFile/Photo/makeShortcut.t b/t/Asset/File/GalleryFile/Photo/makeShortcut.t index 6f283294d..c41c7fdea 100644 --- a/t/Asset/File/GalleryFile/Photo/makeShortcut.t +++ b/t/Asset/File/GalleryFile/Photo/makeShortcut.t @@ -19,6 +19,7 @@ use Scalar::Util qw( blessed ); use WebGUI::Test; use WebGUI::Session; use Test::More; +use Test::Deep; use WebGUI::Test::Maker::HTML; use WebGUI::Asset::File::GalleryFile::Photo; @@ -48,7 +49,7 @@ $versionTag->commit; #---------------------------------------------------------------------------- # Tests -plan tests => 11; +plan tests => 10; #---------------------------------------------------------------------------- # makeShortcut argument checking @@ -76,7 +77,7 @@ ok( ); is( - blessed $shortcut, "WebGUI::Asset::Shortcut", + Scalar::Util::blessed($shortcut), "WebGUI::Asset::Shortcut", "Photo->makeShortcut returns a WebGUI::Shortcut asset", ); @@ -96,7 +97,7 @@ ok( ); is( - blessed $shortcut, "WebGUI::Asset::Shortcut", + Scalar::Util::blessed($shortcut), "WebGUI::Asset::Shortcut", "Photo->makeShortcut returns a WebGUI::Shortcut asset", ); @@ -105,26 +106,15 @@ is( "Photo->makeShortcut makes a shortcut to the correct asset", ); -SKIP: { - skip "Asset::Shortcut does not have a getShortcutOverrides method", 1; - is_deeply( - {$shortcut->getShortcutOverrides}, $overrides, - "Photo->makeShortcut makes a shortcut with the correct overrides", - ); -} +my %shortcutOverrides = $shortcut->getOverrides; +cmp_deeply( + { map({ $_ => $shortcutOverrides{overrides}->{$_}->{newValue} } keys %{ $overrides }) }, + $overrides, + "Photo->makeShortcut makes a shortcut with the correct overrides", +); #---------------------------------------------------------------------------- # www_makeShortcut is only available to those who can edit the photo -SKIP: { - skip "test_privilege has a bug", 1; - $maker->prepare({ - object => $photo, - method => "www_makeShortcut", - userId => 1, - test_privilege => "insufficient", - }); - $maker->run; -} #---------------------------------------------------------------------------- # www_makeShortcut diff --git a/t/Asset/File/GalleryFile/Photo/setFile.t b/t/Asset/File/GalleryFile/Photo/setFile.t index 30156d45a..4dc21c112 100644 --- a/t/Asset/File/GalleryFile/Photo/setFile.t +++ b/t/Asset/File/GalleryFile/Photo/setFile.t @@ -67,7 +67,7 @@ cmp_deeply( ); ok( - -e $storage->getPath($gallery->get('imageResolutions') . '.jpg'), + -e $storage->getPath($gallery->getImageResolutions->[0] . '.jpg'), "Generated resolution file exists on the filesystem", ); diff --git a/t/Asset/File/GalleryFile/Photo/view.t b/t/Asset/File/GalleryFile/Photo/view.t index 53b97959c..ee646289e 100644 --- a/t/Asset/File/GalleryFile/Photo/view.t +++ b/t/Asset/File/GalleryFile/Photo/view.t @@ -18,7 +18,7 @@ use Scalar::Util qw( blessed ); use WebGUI::Test; use WebGUI::Session; use Test::More; -use WebGUI::Test::Maker::HTML; +use Test::Deep; use WebGUI::Asset::File::GalleryFile::Photo; #---------------------------------------------------------------------------- @@ -27,10 +27,11 @@ my $session = WebGUI::Test->session; my $node = WebGUI::Asset->getImportNode($session); my $versionTag = WebGUI::VersionTag->getWorking($session); $versionTag->set({name=>"Photo Test"}); -my $maker = WebGUI::Test::Maker::HTML->new; my $gallery = $node->addChild({ className => "WebGUI::Asset::Wobject::Gallery", + groupIdAddComment => 7, # Everyone + groupIdAddFile => 2, # Registered Users }); my $album = $gallery->addChild({ @@ -44,6 +45,7 @@ my $album my $photo = $album->addChild({ className => "WebGUI::Asset::File::GalleryFile::Photo", + ownerUserId => 3, }, undef, undef, @@ -57,10 +59,47 @@ $photo->setFile( WebGUI::Test->getTestCollateralPath('page_title.jpg') ); # Tests plan tests => 1; -TODO: { - local $TODO = "Write some tests"; - ok(0, 'No tests here, move on'); +#---------------------------------------------------------------------------- +# Test getTemplateVars +$session->user( { userId => 1 } ); +my $testTemplateVars = { + %{ $photo->get }, + synopsis => '', # Synopsis is not undef, is changed to empty string + canComment => bool( 1 ), + canEdit => bool( 0 ), + ownerUsername => WebGUI::User->new( $session, 3 )->username, + url => $photo->getUrl, + url_addArchive => $album->getUrl('func=addArchive'), + url_delete => $photo->getUrl('func=delete'), + url_demote => $photo->getUrl('func=demote'), + url_edit => $photo->getUrl('func=edit'), + url_gallery => $gallery->getUrl, + url_makeShortcut => $photo->getUrl('func=makeShortcut'), + url_listFilesForOwner + => $gallery->getUrl('func=listFilesForUser;userId=3'), + url_promote => $photo->getUrl('func=promote'), + fileUrl => $photo->getFileUrl, + thumbnailUrl => $photo->getThumbnailUrl, + numberOfComments => scalar @{ $photo->getCommentIds }, + resolutions_loop => ignore(), # Tested elsewhere + exifLoop => ignore(), # Tested elsewhere + # Gallery stuff + url_search => $gallery->getUrl('func=search'), + url_listFilesForCurrentUser => $gallery->getUrl('func=listFilesForUser'), + +}; +# Ignore all EXIF tags +for my $tag ( keys %{ $photo->getExifData } ) { + $testTemplateVars->{ 'exif_' . $tag } = ignore(); } +# Add search vars +$gallery->appendTemplateVarsSearchForm( $testTemplateVars ); + +cmp_deeply( + $photo->getTemplateVars, + $testTemplateVars, + "getTemplateVars is correct and complete", +); #---------------------------------------------------------------------------- # Cleanup diff --git a/t/Asset/Wobject/Gallery/00base.t b/t/Asset/Wobject/Gallery/00base.t index 75f448262..d1f219edc 100644 --- a/t/Asset/Wobject/Gallery/00base.t +++ b/t/Asset/Wobject/Gallery/00base.t @@ -51,7 +51,13 @@ isa_ok( ); #---------------------------------------------------------------------------- -# Test deleting a album +# Test adding children to Gallery + +# Only GalleryAlbums may be added + + +#---------------------------------------------------------------------------- +# Test deleting a gallery my $properties = $gallery->get; $gallery->purge; diff --git a/t/Asset/Wobject/Gallery/permission.t b/t/Asset/Wobject/Gallery/permission.t new file mode 100644 index 000000000..1ae9a558f --- /dev/null +++ b/t/Asset/Wobject/Gallery/permission.t @@ -0,0 +1,83 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2007 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"; + +## The goal of this test is to test permissions inside Gallerys + +use Scalar::Util qw( blessed ); +use WebGUI::Test; +use WebGUI::Session; +use WebGUI::Test::Maker::Permission; +use Test::More; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +my $node = WebGUI::Asset->getImportNode($session); +my $versionTag = WebGUI::VersionTag->getWorking($session); +$versionTag->set({name=>"Gallery Test"}); +my $maker = WebGUI::Test::Maker::Permission->new; +my $gallery; + +my $nonAdmin = WebGUI::User->new( $session, "new" ); + + +#---------------------------------------------------------------------------- +# Tests +# Plan is delayed until all tests are prepared + +#---------------------------------------------------------------------------- +my $gallery + = $node->addChild({ + className => 'WebGUI::Asset::Wobject::Gallery', + groupIdAddComment => '7', # Everyone + groupIdAddFile => '2', # Registered Users + groupIdEdit => '3', # Admins + groupIdView => '7', # Everyone + ownerUserId => '3', # Admin + }); + +$maker->prepare( + { + object => $gallery, + method => "canView", + pass => [ '1', '3', $nonAdmin->userId ], + }, + { + object => $gallery, + method => 'canEdit', + pass => [ '3' ], + fail => [ '1', $nonAdmin->userId ], + }, + { + object => $gallery, + method => 'canAddFile', + pass => [ '3', $nonAdmin->userId ], + fail => [ '1' ], + }, + { + object => $gallery, + method => 'canComment', + pass => [ '1', '3', $nonAdmin->userId ], + } +); + +plan tests => $maker->plan; + +$maker->run; + +#---------------------------------------------------------------------------- +# Cleanup +END { + $versionTag->rollback; +} diff --git a/t/Asset/Wobject/GalleryAlbum/addArchive.t b/t/Asset/Wobject/GalleryAlbum/addArchive.t index 5616f3cb3..0896309ca 100644 --- a/t/Asset/Wobject/GalleryAlbum/addArchive.t +++ b/t/Asset/Wobject/GalleryAlbum/addArchive.t @@ -52,7 +52,7 @@ $versionTag->commit; #---------------------------------------------------------------------------- # Tests -plan tests => 2; +plan tests => 5; #---------------------------------------------------------------------------- # Test the addArchive sub @@ -65,11 +65,30 @@ cmp_deeply( bag( "Aana1.jpg", "Aana2.jpg", "Aana3.jpg" ), ); +cmp_deeply( + [ map { $_->get("title") } @$images ], + bag( "Aana1", "Aana2", "Aana3" ), +); + +cmp_deeply( + [ map { $_->get("menuTitle") } @$images ], + bag( "Aana1", "Aana2", "Aana3" ), +); + +cmp_deeply( + [ map { $_->get("url") } @$images ], + bag( + $session->url->urlize( $album->getUrl . "/Aana1" ), + $session->url->urlize( $album->getUrl . "/Aana2" ), + $session->url->urlize( $album->getUrl . "/Aana3" ), + ), +); + #---------------------------------------------------------------------------- # Test the www_addArchive page #---------------------------------------------------------------------------- # Cleanup END { - $versionTag->rollback(); + $versionTag->rollback; } diff --git a/t/Asset/Wobject/GalleryAlbum/rss.t b/t/Asset/Wobject/GalleryAlbum/rss.t index 10523b6b7..cf5a97217 100644 --- a/t/Asset/Wobject/GalleryAlbum/rss.t +++ b/t/Asset/Wobject/GalleryAlbum/rss.t @@ -19,11 +19,11 @@ use Scalar::Util qw( blessed ); use WebGUI::Test; use WebGUI::Session; use Test::More; -use WebGUI::Test::Maker::HTML; +use Test::Deep; +use XML::Simple; #---------------------------------------------------------------------------- # Init -my $maker = WebGUI::Test::Maker::HTML->new; my $session = WebGUI::Test->session; my $node = WebGUI::Asset->getImportNode($session); my $versionTag = WebGUI::VersionTag->getWorking($session); @@ -41,6 +41,7 @@ my $album = $gallery->addChild({ className => "WebGUI::Asset::Wobject::GalleryAlbum", ownerUserId => "3", # Admin + description => "An RSS Description", }, undef, undef, @@ -53,6 +54,7 @@ for my $i ( 0 .. 5 ) { = $album->addChild({ className => "WebGUI::Asset::File::GalleryFile::Photo", filename => "$i.jpg", + synopsis => "This is a description for $i.jpg", }, undef, undef, @@ -65,15 +67,38 @@ $versionTag->commit; #---------------------------------------------------------------------------- # Tests -plan tests => 1; +plan tests => 2; + +use_ok("Test::WWW::Mechanize"); +my $mech; #---------------------------------------------------------------------------- # Test www_viewRss - -TODO: { - local $TODO = "Write some tests"; - ok(0, "No tests here"); -} +$mech = Test::WWW::Mechanize->new; +my $url = $session->url->getSiteURL . $session->url->makeAbsolute( $album->getUrl('func=viewRss') ); +$mech->get( $url ); +cmp_deeply( + XMLin( $mech->content ), + { + version => '2.0', + channel => { + link => $session->url->getSiteURL . $album->getUrl, + description => $album->get("description"), + title => $album->get("title"), + item => bag( + map { + superhashof({ + link => $session->url->getSiteURL . $_->getUrl, + title => $_->get("title"), + pubDate => $session->datetime->epochToMail( $_->get("revisionDate") ), + description => $_->get("synopsis"), + }) + } @photos + ), + }, + }, + "RSS Datastructure is complete and correct", +); #---------------------------------------------------------------------------- # Cleanup diff --git a/t/Macro/International.t b/t/Macro/International.t index a057b06ea..da117740d 100644 --- a/t/Macro/International.t +++ b/t/Macro/International.t @@ -21,26 +21,31 @@ use Test::More; # increment this value for each test you create my $session = WebGUI::Test->session; my @testSets = ( - { - input => ['none', 'Asset'], - output => q!None!, - comment => q|explicit namespace|, - }, - { - input => ['change url', 'Asset'], - output => q!Change URL!, - comment => q|space in label|, - }, - { - input => ['webgui', 'WebGUI'], - output => q!WebGUI!, - comment => q|explicit namespace #2|, - }, - { - input => ['webgui', ''], - output => q!WebGUI!, - comment => q|default namespace|, - }, + { + input => ['none', 'Asset'], + output => q!None!, + comment => q|explicit namespace|, + }, + { + input => ['change url', 'Asset'], + output => q!Change URL!, + comment => q|space in label|, + }, + { + input => ['webgui', 'WebGUI'], + output => q!WebGUI!, + comment => q|explicit namespace #2|, + }, + { + input => ['webgui', ''], + output => q!WebGUI!, + comment => q|default namespace|, + }, + { + input => ['template listFilesForUser title', 'Asset_Gallery', 'plainblack'], + output => q{plainblack's Gallery}, + comment => q{Third and more arguments are passed to sprintf()}, + }, ); my $numTests = scalar @testSets; diff --git a/t/Asset/Wobject/Gallery/Utility/addAlbum.t b/t/Utility/Gallery/addAlbum.t similarity index 98% rename from t/Asset/Wobject/Gallery/Utility/addAlbum.t rename to t/Utility/Gallery/addAlbum.t index 6e005344b..194d520f7 100644 --- a/t/Asset/Wobject/Gallery/Utility/addAlbum.t +++ b/t/Utility/Gallery/addAlbum.t @@ -15,13 +15,12 @@ use strict; use FindBin; -use lib "$FindBin::Bin/../../../../../lib"; -use lib "$FindBin::Bin/../../../../lib"; +use lib "$FindBin::Bin/../../lib"; use Test::More; use Test::Deep; +use WebGUI::Test; use WebGUI::Asset; use WebGUI::Session; -use WebGUI::Test; #---------------------------------------------------------------------------- # Init @@ -132,7 +131,7 @@ plan tests => 10 #---------------------------------------------------------------------------- # Test use -my $utility = 'WebGUI::Asset::Wobject::Gallery::Utility'; +my $utility = 'WebGUI::Utility::Gallery'; use_ok($utility); #----------------------------------------------------------------------------