From c5824ab0e3b049371f9e8fa6ed0b1d5873217610 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 15 Sep 2009 21:14:24 -0700 Subject: [PATCH] Return something more appropriate than unprivileged for getScheduleAsJSON. --- .../Asset/Wobject/EventManagementSystem.pm | 27 +++++++-------- t/Asset/Wobject/EventManagementSystem.t | 33 ++++++++++++------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/lib/WebGUI/Asset/Wobject/EventManagementSystem.pm b/lib/WebGUI/Asset/Wobject/EventManagementSystem.pm index 486bc1178..8f1d9f56a 100644 --- a/lib/WebGUI/Asset/Wobject/EventManagementSystem.pm +++ b/lib/WebGUI/Asset/Wobject/EventManagementSystem.pm @@ -1156,29 +1156,30 @@ returns the JSON data for a page of the schedule table sub www_getScheduleDataJSON { my $self = shift; my $session = $self->session; - return $session->privilege->insufficient() unless $self->canView; + my $emptyRecord = JSON->new->encode( { + records => [ ], totalRecords => 0, recordsReturned => 0, + startIndex => 0, currentLocationPage => 0, totalLocationPages => 0, + currentDatePage => 0, totalDatePages => 0, dateRecords => [ ], + sort => undef, dir => 'asc', pageSize => 0, + }); + return $emptyRecord unless $self->canView; # the following two are expected to be configurable... - my $locationsPerPage = $self->get('scheduleColumnsPerPage'); + my $locationsPerPage = $self->get('scheduleColumnsPerPage'); - my ($db, $form) = $session->quick(qw(db form)); + my ($db, $form) = $session->quick(qw(db form)); my $locationPageNumber = $form->get('locationPage') || 1; - my $datePageNumber = $form->get('datePage') || 1; + my $datePageNumber = $form->get('datePage') || 1; my @dateRecords; my @ticketLocations = $self->getLocations( \@dateRecords ); # the total number of pages is the number of locations divided by the number of locations per page my $numberOfLocationPages = int( .9 + scalar(@ticketLocations) / $locationsPerPage ); # skip everything else if there are no locations/pages - return JSON->new->encode( { - records => [ ], totalRecords => 0, recordsReturned => 0, startIndex => 0, - currentLocationPage => 0, totalLocationPages => 0, - currentDatePage => 0, totalDatePages => 0, dateRecords => [ ], - sort => undef, dir => 'asc', pageSize => 0, - }) if $numberOfLocationPages == 0; + return $emptyRecord if $numberOfLocationPages == 0; # now we pick out the locations to be displayed on this page my $indexFirstLocation = ($locationPageNumber-1)*$locationsPerPage; - my $indexLastLocation = $locationPageNumber*$locationsPerPage - 1; - my $currentDate = $dateRecords[$datePageNumber-1]; - @ticketLocations = @ticketLocations[$indexFirstLocation..$indexLastLocation]; + my $indexLastLocation = $locationPageNumber*$locationsPerPage - 1; + my $currentDate = $dateRecords[$datePageNumber-1]; + @ticketLocations = @ticketLocations[$indexFirstLocation..$indexLastLocation]; my $tickets = $db->read( q{ select assetData.assetId, sku.description, assetData.title, EMSTicket.startDate, EMSTicket.location from EMSTicket diff --git a/t/Asset/Wobject/EventManagementSystem.t b/t/Asset/Wobject/EventManagementSystem.t index 6d7c2b9eb..9385fee85 100644 --- a/t/Asset/Wobject/EventManagementSystem.t +++ b/t/Asset/Wobject/EventManagementSystem.t @@ -22,8 +22,8 @@ use WebGUI::Session; use WebGUI::User; use WebGUI::Group; use JSON; +use Data::Dumper; use Test::Deep; -#use Data::Dumper; #---------------------------------------------------------------------------- # Init @@ -128,12 +128,16 @@ ok(scalar(@$badges) == 2, 'Two Badges exist'); # Add tickets my @tickets; push(@tickets, $ems->addChild({ - className=>'WebGUI::Asset::Sku::EMSTicket', - startDate => '2009-01-01 14:00:00', + className => 'WebGUI::Asset::Sku::EMSTicket', + startDate => '2009-01-01 14:00:00', + eventNumber => 1, + location => 'qq', })); push(@tickets, $ems->addChild({ - className=>'WebGUI::Asset::Sku::EMSTicket', - startDate => '2009-01-01 14:00:00', + className => 'WebGUI::Asset::Sku::EMSTicket', + startDate => '2009-01-01 14:00:00', + eventNumber => 2, + location => 'qq', })); foreach my $ticket(@tickets) { @@ -173,14 +177,19 @@ is($session->http->getStatus, 201, '... attender user can see the schedule'); $session->http->setStatus(201); $session->user({userId => $crasher->getId}); -$data = $ems->www_getScheduleDataJSON(); -is($session->http->getStatus, 401, 'www_getScheduleDataJSON: non-attender may now see the schedule JSON'); +my ($json, $records); +$json = $ems->www_getScheduleDataJSON(); +$records = eval { JSON::from_json($json)->{records} }; +cmp_deeply($records, [], 'www_getScheduleDataJSON: visitor may not see the schedule JSON'); -$session->http->setStatus(201); $session->user({userId => $attender->getId}); -$data = $ems->www_getScheduleDataJSON(); -is($session->http->getStatus, 201, '... attender can see the schedule JSON'); -$session->http->setStatus(201); +$json = $ems->www_getScheduleDataJSON(); +$records = eval { JSON::from_json($json)->{records} }; +cmp_deeply($records, [ignore(), ignore()], '... attender can see the schedule JSON'); + +foreach my $ticket (@tickets) { + $ticket->purge; +} my $html = $ems->www_viewSchedule(); ok( $html !~ /REPLACE/, 'tags were successfully replaced'); @@ -291,7 +300,7 @@ my @tickets= ( ); is( scalar(@tickets), 12, 'created tickets for ems'); my $tickets = $ems->getTickets; -is(scalar(@$tickets), 14, 'Fourteen tickets exist'); +is(scalar(@{ $tickets }), 12, 'Fourteen tickets exist'); my $locations = [ $ems->getLocations ]; cmp_deeply($locations, [ 'a','b','c','d','e','f' ], 'get locations returns all expected locations'); # print 'locations=[', join( ',', @$locations ),"]\n";