[Rt-commit] r2608 - in rt/branches/3.4-RELEASE/lib: RT t/regression
tla at bestpractical.com
tla at bestpractical.com
Tue Apr 12 15:29:25 EDT 2005
Author: tla
Date: Tue Apr 12 15:29:25 2005
New Revision: 2608
Added:
rt/branches/3.4-RELEASE/lib/t/regression/20savedsearch.t
Modified:
rt/branches/3.4-RELEASE/lib/RT/SavedSearch.pm
Log:
SavedSearch now shinily tested. API also changed somewhat.
Modified: rt/branches/3.4-RELEASE/lib/RT/SavedSearch.pm
==============================================================================
--- rt/branches/3.4-RELEASE/lib/RT/SavedSearch.pm (original)
+++ rt/branches/3.4-RELEASE/lib/RT/SavedSearch.pm Tue Apr 12 15:29:25 2005
@@ -88,7 +88,6 @@
=begin testing
-use_ok(RT::SavedSearch);
=end testing
@@ -101,11 +100,14 @@
if ($object) {
$self->{'Attribute'} = $object->Attributes->WithId($id);
- $self->{'Id'} = $self->{'Attribute'}->Id;
- $self->{'Privacy'} = $privacy;
- $self->{'Type'} = $self->{'Attribute'}->SubValue('SearchType');
- use Data::Dumper;
- print Dumper($self);
+ if ($self->{'Attribute'}->Id) {
+ $self->{'Id'} = $self->{'Attribute'}->Id;
+ $self->{'Privacy'} = $privacy;
+ $self->{'Type'} = $self->{'Attribute'}->SubValue('SearchType');
+ } else {
+ $RT::Logger->error("Could not load attribute " . $id
+ . " for object " . $privacy);
+ }
} else {
$RT::Logger->error("Could not find object $privacy when loading search");
}
@@ -114,47 +116,64 @@
=head2 Save
-Takes a privacy, an optional type, a name, and a hash containing the
+Takes a privacy, an optional type, a name, and a hashref containing the
search parameters. Saves the given parameters to the appropriate user/
-group object, and loads the resulting search.
+group object, and loads the resulting search. Defaults are:
+ Privacy: undef
+ Type: Ticket
+ Name: "new search"
+ SearchParams: (empty hash)
=cut
sub Save {
my $self = shift;
- my ($privacy, $type, $name, %params) = @_;
+ my %args = ('Privacy' => 'RT::User-' . $self->CurrentUser->Id,
+ 'Type' => 'Ticket',
+ 'Name' => 'new search',
+ 'SearchParams' => {},
+ @_);
+ my $privacy = $args{'Privacy'};
+ my $type = $args{'Type'};
+ my $name = $args{'Name'};
+ my %params = %{$args{'SearchParams'}};
- unless($privacy || $name) {
- # Save to the current object.
- unless($self->{'Attribute'}) {
- $RT::Logger->error("Tried to save to a nonexistent search without specifying a privacy!");
- return;
- }
- my ($ret, $msg) = $self->_UpdateContent(%params);
- unless($ret) {
- $RT::Logger->error("Could not update content of search "
- . $self->Id . ": $msg");
- }
- } else {
- $params{'SearchType'} = $type;
- my $object = $self->_GetObject($privacy);
- if ($object->Id) {
- my ($att_id, $att_msg) = $object->AddAttribute(
- 'Name' => 'SavedSearch',
- 'Description' => $name,
- 'Content' => \%params);
- if ($att_id) {
- $self->{'Attribute'} = $object->Attributes->WithId($att_id);
- $self->{'Id'} = $att_id;
- $self->{'Privacy'} = $privacy;
- $self->{'Type'} = $type;
- } else {
- $RT::Logger->error("SavedSearch save failure: $att_msg");
- }
+ $params{'SearchType'} = $type;
+ my $object = $self->_GetObject($privacy);
+ if ($object) {
+ my ($att_id, $att_msg) = $object->AddAttribute(
+ 'Name' => 'SavedSearch',
+ 'Description' => $name,
+ 'Content' => \%params);
+ if ($att_id) {
+ $self->{'Attribute'} = $object->Attributes->WithId($att_id);
+ $self->{'Id'} = $att_id;
+ $self->{'Privacy'} = $privacy;
+ $self->{'Type'} = $type;
+ } else {
+ $RT::Logger->error("SavedSearch save failure: $att_msg");
}
}
}
+=head2 Update
+
+Updates the parameters of an existing search. Takes a hashref with
+the new parameters.
+
+=cut
+
+sub Update {
+ my $self = shift;
+ my $params = shift;
+
+ return(0, "No search loaded!") unless $self->Id;
+ return(0, "Could not load search attribute")
+ unless $self->{'Attribute'}->Id;
+ my ($status, $msg) = $self->{'Attribute'}->SetSubValues(%{$params});
+ return ($status, $msg);
+}
+
### Accessor methods
=head2 Name
@@ -258,16 +277,6 @@
return $object;
}
-# _UpdateContent: Change the parameters of an existing saved search.
-
-sub _UpdateContent {
- my $self = shift;
- my %params = shift;
-
- my ($status, $msg) = $self->{'Attribute'}->SetSubValues(%params);
- return ($status, $msg);
-}
-
eval "require RT::SavedSearch_Vendor";
die $@ if ($@ && $@ !~ qr{^Can't locate RT/SavedSearch_Vendor.pm});
eval "require RT::SavedSearch_Local";
Added: rt/branches/3.4-RELEASE/lib/t/regression/20savedsearch.t
==============================================================================
--- (empty file)
+++ rt/branches/3.4-RELEASE/lib/t/regression/20savedsearch.t Tue Apr 12 15:29:25 2005
@@ -0,0 +1,158 @@
+use RT;
+use Test::More qw/no_plan/;
+use RT::User;
+use RT::Group;
+use RT::Ticket;
+use RT::Queue;
+
+use_ok(RT::SavedSearch);
+
+RT::LoadConfig();
+RT::Init();
+
+# Set up some infrastructure. These calls are tested elsewhere.
+
+my $searchuser = RT::User->new($RT::SystemUser);
+my ($ret, $msg) = $searchuser->Create(Name => 'searchuser'.$$,
+ Privileged => 1,
+ EmailAddress => "searchuser\@p$$.example.com",
+ RealName => 'Search user');
+ok($ret, "created searchuser: $msg");
+$searchuser->PrincipalObj->GrantRight(Right => 'LoadSavedSearch');
+$searchuser->PrincipalObj->GrantRight(Right => 'CreateSavedSearch');
+$searchuser->PrincipalObj->GrantRight(Right => 'ModifySelf');
+
+# This is the group whose searches searchuser should be able to see.
+my $ingroup = RT::Group->new($RT::SystemUser);
+$ingroup->CreateUserDefinedGroup(Name => 'searchgroup1'.$$);
+$ingroup->AddMember($searchuser->Id);
+$searchuser->PrincipalObj->GrantRight(Right => 'EditSavedSearches',
+ Object => $ingroup);
+$searchuser->PrincipalObj->GrantRight(Right => 'ShowSavedSearches',
+ Object => $ingroup);
+
+# This is the group whose searches searchuser should not be able to see.
+my $outgroup = RT::Group->new($RT::SystemUser);
+$outgroup->CreateUserDefinedGroup(Name => 'searchgroup2'.$$);
+$outgroup->AddMember($RT::SystemUser->Id);
+
+my $queue = RT::Queue->new($RT::SystemUser);
+$queue->Create(Name => 'SearchQueue'.$$);
+$searchuser->PrincipalObj->GrantRight(Right => 'SeeQueue', Object => $queue);
+$searchuser->PrincipalObj->GrantRight(Right => 'ShowTicket', Object => $queue);
+$searchuser->PrincipalObj->GrantRight(Right => 'OwnTicket', Object => $queue);
+
+
+my $ticket = RT::Ticket->new($RT::SystemUser);
+$ticket->Create(Queue => $queue->Id,
+ Requestor => [ $searchuser->Name ],
+ Owner => $searchuser,
+ Subject => 'saved search test');
+
+
+# Now start the search madness.
+my $curruser = RT::CurrentUser->new($searchuser);
+my $format = '\' <b><a href="/Ticket/Display.html?id=__id__">__id__</a></b>/TITLE:#\',
+\'<b><a href="/Ticket/Display.html?id=__id__">__Subject__</a></b>/TITLE:Subject\',
+\'__Status__\',
+\'__QueueName__\',
+\'__OwnerName__\',
+\'__Priority__\',
+\'__NEWLINE__\',
+\'\',
+\'<small>__Requestors__</small>\',
+\'<small>__CreatedRelative__</small>\',
+\'<small>__ToldRelative__</small>\',
+\'<small>__LastUpdatedRelative__</small>\',
+\'<small>__TimeLeft__</small>\'';
+
+my $mysearch = RT::SavedSearch->new($curruser);
+$mysearch->Save(Privacy => 'RT::User-' . $searchuser->Id,
+ Type => 'Ticket',
+ Name => 'owned by me',
+ SearchParams => {'Format' => $format,
+ 'Query' => "Owner = '"
+ . $searchuser->Name . "'"});
+is($mysearch->Type, 'Ticket', "mysearch was created");
+
+
+my $groupsearch = RT::SavedSearch->new($curruser);
+$groupsearch->Save(Privacy => 'RT::Group-' . $ingroup->Id,
+ Type => 'Ticket',
+ Name => 'search queue',
+ SearchParams => {'Format' => $format,
+ 'Query' => "Queue = '"
+ . $queue->Name . "'"});
+is($groupsearch->Type, 'Ticket', "groupsearch was created");
+like($groupsearch->GetParameter('Query'), qr/Queue/,
+ "Retrieved query of groupsearch");
+
+my $othersearch = RT::SavedSearch->new($curruser);
+$othersearch->Save(Privacy => 'RT::Group-' . $outgroup->Id,
+ Type => 'Ticket',
+ Name => 'searchuser requested',
+ SearchParams => {'Format' => $format,
+ 'Query' =>
+ "Requestor.Name LIKE 'search'"});
+is($othersearch->Id, 0, "othersearch NOT created");
+
+$othersearch = RT::SavedSearch->new($RT::SystemUser);
+$othersearch->Save(Privacy => 'RT::Group-' . $outgroup->Id,
+ Type => 'Ticket',
+ Name => 'searchuser requested',
+ SearchParams => {'Format' => $format,
+ 'Query' =>
+ "Requestor.Name LIKE 'search'"});
+is($othersearch->Type, 'Ticket', "othersearch created by systemuser");
+like($othersearch->GetParameter('Query'), qr/Requestor/,
+ "Retrieved query of othersearch");
+
+# Now try to load some searches.
+
+# This should work.
+my $loadedsearch1 = RT::SavedSearch->new($curruser);
+$loadedsearch1->Load('RT::User-'.$curruser->Id, $mysearch->Id);
+is($loadedsearch1->Id, $mysearch->Id, "Loaded mysearch");
+like($loadedsearch1->GetParameter('Query'), qr/Owner/,
+ "Retrieved query of mysearch");
+# Check through the other accessor methods.
+is($loadedsearch1->Privacy, 'RT::User-' . $curruser->Id,
+ "Privacy of mysearch correct");
+is($loadedsearch1->Name, 'owned by me', "Name of mysearch correct");
+is($loadedsearch1->Type, 'Ticket', "Type of mysearch correct");
+
+# See if it can be used to search for tickets.
+my $tickets = RT::Tickets->new($curruser);
+$tickets->FromSQL($loadedsearch1->GetParameter('Query'));
+is($tickets->Count, 1, "Found a ticket");
+
+# This should fail -- wrong object.
+# my $loadedsearch2 = RT::SavedSearch->new($curruser);
+# $loadedsearch2->Load('RT::User-'.$curruser->Id, $groupsearch->Id);
+# isnt($loadedsearch2->Id, $othersearch->Id, "Didn't load groupsearch as mine");
+# ...but this should succeed.
+my $loadedsearch3 = RT::SavedSearch->new($curruser);
+$loadedsearch3->Load('RT::Group-'.$ingroup->Id, $groupsearch->Id);
+is($loadedsearch3->Id, $groupsearch->Id, "Loaded groupsearch");
+like($loadedsearch3->GetParameter('Query'), qr/Queue/,
+ "Retrieved query of groupsearch");
+# Can it get tickets?
+$tickets = RT::Tickets->new($curruser);
+$tickets->FromSQL($loadedsearch3->GetParameter('Query'));
+is($tickets->Count, 1, "Found a ticket");
+
+# This should fail -- no permission.
+my $loadedsearch4 = RT::SavedSearch->new($curruser);
+$loadedsearch4->Load($othersearch->Privacy, $othersearch->Id);
+isnt($loadedsearch4->Id, $othersearch->Id, "Did not load othersearch");
+
+# Try to update an existing search.
+$loadedsearch1->Update({'Format' => $format,
+ 'Query' => "Queue = '" . $queue->Name . "'" });
+like($loadedsearch1->GetParameter('Query'), qr/Queue/,
+ "Updated mysearch parameter");
+is($loadedsearch1->Type, 'Ticket', "mysearch is still for tickets");
+is($loadedsearch1->Privacy, 'RT::User-'.$curruser->Id,
+ "mysearch still belongs to searchuser");
+is($mysearch->GetParameter('Query'), qr/Queue/,
+ "other mysearch object updated");
More information about the Rt-commit
mailing list