From c668226651958d540b09fc2766421b5e1f537fba Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 20 Jul 2009 16:01:30 +0000 Subject: [PATCH] Fix Keywords so that by default, only Keywords from published assets are returned. Reported as bug against the StoryManager, but also affects Wiki, Shelf. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Keyword.pm | 59 +++++++++++++++++++++++++++++++--------- t/Keyword.t | 52 +++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 16 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 702396a0a..8e83e438c 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -9,6 +9,7 @@ - fixed #10652: Story Archive: hover help confusing - fixed #10653: Story Archive: Description text doesn't display - fixed #10656: Story: Photo caption doesn't appear + - fixed #10658: Delete in Story Archive doesn't delete in Story Topic 7.7.15 - fixed #10629: WebGUI::ProfileField create new field bug diff --git a/lib/WebGUI/Keyword.pm b/lib/WebGUI/Keyword.pm index 6a128c7b6..c0a2e4936 100644 --- a/lib/WebGUI/Keyword.pm +++ b/lib/WebGUI/Keyword.pm @@ -223,16 +223,25 @@ sub generateCloud { #------------------------------------------------------------------- -=head2 getKeywordsForAsset ( { asset => $asset } ) +=head2 getKeywordsForAsset ( $options ) -Returns a string of keywords separated by spaces. If the keyword has spaces in it, it -will be quoted. +Looks up the keywords for an asset, identified by either an asset object +or an asset GUID in the options. Returns a string of keywords separated +by spaces. If the keyword has spaces in it, it will be quoted. -=head3 asset +=head3 $options + +A hash reference of options. + +=head4 asset An asset that you want to get the keywords for. -=head3 asArrayRef +=head4 assetId + +The GUID of an asset you want to get the keywords for. + +=head4 asArrayRef A boolean, that if set to 1 will return the keywords as an array reference rather than a string. @@ -254,40 +263,50 @@ sub getKeywordsForAsset { #------------------------------------------------------------------- -=head2 getMatchingAssets ( { startAsset => $asset, keyword => $keyword } ) +=head2 getMatchingAssets ( $options ) Returns an array reference of asset ids matching the params. Assets are returned in order of creationDate. +Only published assets are returned, unless the C options is used. -=head3 startAsset +=head3 $options + +A hash reference of options. + +=head4 startAsset An asset object where you'd like to start searching for matching keywords. Doesn't search any particular branch if one isn't specified. -=head3 keyword +=head4 keyword The keyword to match. -=head3 keywords +=head4 keywords An array reference of keywords to match. -=head3 matchAssetKeywords +=head4 matchAssetKeywords A reference to an asset that has a list of keywords to match. This can help locate assets that are similar to another asset. If the referenced asset does not have any keywords, then an empty array reference is returned. -=head3 isa +=head4 isa A classname pattern to match. For example, if you provide 'WebGUI::Asset::Sku' then everything that has a class name that starts with that including 'WebGUI::Asset::Sku::Product' will be included. -=head3 usePaginator +=head4 usePaginator Instead of returning an array reference of assetId's, return a paginator object. -=head3 rowsPerPage +=head4 rowsPerPage If usePaginator is passed, then this variable will set the number of rows per page that the paginator uses. If usePaginator is not passed, then this variable will limit the number of assetIds that are returned. +=head4 states + +An array reference of asset states. The ids of assets in those states will be returned. If this option +is missing, only assets in the C state will be returned. + =cut sub getMatchingAssets { @@ -324,6 +343,20 @@ sub getMatchingAssets { push @params, $options->{keyword}; } + # looking for a single keyword + my @states; + if (exists $options->{states} && scalar(@{ $options->{states} } )) { + @states = @{ $options->{states} }; + } + else { + @states = 'published'; + } + { + my @placeholders = ('?') x scalar @states; + push @params, @states; + push @clauses, 'state in ('.join(',', @placeholders).')'; + } + # looking for a list of keywords if (exists $options->{keywords} && scalar(@{$options->{keywords}})) { my @placeholders = (); diff --git a/t/Keyword.t b/t/Keyword.t index 3f721a54d..03c7e2a01 100644 --- a/t/Keyword.t +++ b/t/Keyword.t @@ -17,7 +17,9 @@ use WebGUI::Keyword; use WebGUI::Asset; # load your modules here -use Test::More tests => 9; # increment this value for each test you create +use Test::More tests => 14; # increment this value for each test you create +use Test::Deep; +use Data::Dumper; my $session = WebGUI::Test->session; @@ -31,6 +33,19 @@ isa_ok($keyword, "WebGUI::Keyword"); $keyword->setKeywordsForAsset({ asset=>$home, keywords=>"test key, word, foo bar"}); my ($count) = $session->db->quickArray("select count(*) from assetKeyword where assetId=?", [$home->getId]); is($count, 3, "setKeywordsForAsset() create"); +cmp_bag( + $keyword->getKeywordsForAsset({ asset => $home, asArrayRef => 1}), + ['test key', 'word', 'foo bar'], + '... check correct keywords set, returns array ref' +); + +my $keywords = $keyword->getKeywordsForAsset({ asset => $home, }); +my @keywords = split ',\s*', $keywords; +cmp_bag( + \@keywords, + ['test key', 'word', 'foo bar'], + '... check correct keywords set, returns string' +); $keyword->setKeywordsForAsset({ asset=>$home, keywords=>"webgui, rules"}); my ($count) = $session->db->quickArray("select count(*) from assetKeyword where assetId=?", [$home->getId]); @@ -46,8 +61,39 @@ like($keyword->getKeywordsForAsset({asset=>$home }), qr/owns/, "getLatestVersion $keyword->deleteKeyword({keyword => "owns"}); unlike($keyword->getKeywordsForAsset({asset=>$home }), qr/owns/, "getLatestVersionNumber()"); +my $snippet = $home->addChild({ + className => 'WebGUI::Asset::Snippet', + title => 'keyword snippet', + snippet => 'keyword snippet', + keywords => 'webgui', +}); + +my $tag = WebGUI::VersionTag->getWorking($session); +WebGUI::Test->tagsToRollback($tag); +$tag->commit; + +my $assetIds = $keyword->getMatchingAssets({ keyword => 'webgui', }); + +cmp_deeply( + $assetIds, + [$snippet->getId, $home->getId, ], + 'getMatchingAssets, by keyword, assetIds in order by creationDate, descending' +); + +$snippet->trash(); + +cmp_deeply( + $keyword->getMatchingAssets({ keyword => 'webgui', }), + [$home->getId, ], + '... only published assets' +); + +cmp_deeply( + $keyword->getMatchingAssets({ keyword => 'webgui', states => [ qw/published trash/, ]}), + [$snippet->getId, $home->getId, ], + '... retrieving assets in more than one state' +); + $keyword->deleteKeywordsForAsset($home); is(scalar(@{$keyword->getKeywordsForAsset({ asset=>$home, asArrayRef=>1})}), 0, "getKeywordsForAsset()"); -undef $keyword; -undef $home;