From 4a65691dcc3a1e006f3a3508f8902474448cf20c Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Thu, 28 Feb 2008 18:40:06 +0000 Subject: [PATCH] add: Calendar List view and associated Help add: Ical Page length add: WWW::Mech tests for Auth and Redirect --- docs/changelog/7.x.x.txt | 2 + .../home_calendar-templates.wgpkg | Bin 0 -> 9728 bytes docs/upgrades/upgrade_7.5.3-7.5.4.pl | 39 ++ lib/WebGUI/Asset/Wobject/Calendar.pm | 486 ++++++++++-------- lib/WebGUI/Help/Asset_Calendar.pm | 92 +++- lib/WebGUI/i18n/English/Asset_Calendar.pm | 272 ++++++++-- t/Asset/Redirect/mech.t | 174 +++++++ t/Auth/mech.t | 277 ++++++++++ 8 files changed, 1083 insertions(+), 259 deletions(-) create mode 100644 docs/upgrades/packages-7.5.4/home_calendar-templates.wgpkg create mode 100644 t/Asset/Redirect/mech.t create mode 100644 t/Auth/mech.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 4b70dac74..a02266148 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -9,6 +9,8 @@ removed. - added script to remove unneeded .wgaccess files instead of having it run during upgrade - fixed: wiki pages link to deleted pages (Arjan Widlak, United Knowledge) + - add: Calendar List View and associated Help + - add: Test::WWW::Mechanize tests for Redirect and Auth 7.5.3 - prevent HTML and Macro injection in usernames diff --git a/docs/upgrades/packages-7.5.4/home_calendar-templates.wgpkg b/docs/upgrades/packages-7.5.4/home_calendar-templates.wgpkg new file mode 100644 index 0000000000000000000000000000000000000000..e5b0a11dc0b0c83cfd432b5c453124ca59ddf44e GIT binary patch literal 9728 zcmeHLZFAZ<5YFfR3XebZQz-`97&wlZ1llGkd1*pQ)5}dp*a9mU+pz?c-1NWSO11%k zLuf8^ax-`a43_qNwfn5!?`{9mG|e*2y*+-nwE10`UD{qjvq}ZiGK*$uPbm}%rQ)6f zB-_76l%WVzo8WGLL>`y-)H)akmoQG;ALsO{R)p3C@m=KKg~Izg8u%}&Bd{$Q#ysxu zU*tg&{-RYhmg28lB}fDC=Ju~`#sB^GReuQy|Ga@HsFVc}>SI-@Du3|@aPiyU@5E{t z1tE?Y!IW~_I)sKEnzjk^1hHCB1xd2(A<91d2!n`WXI~KeBhOphVMiFj;=z=2 zR&|LQ4knAahNG@WsEZwG-N%y-@tt5IXfRHBvWQE@o{uTadhNlNX0)`gbp!s z+NhsNK8id+<_1HYw~>c^2SquChu{eYE?P7XQHoh2rdP_{+m(l=-)UP1;ePp~b${8} zm$nmn9tA95(=fJMwu#U~;yBpPFn3HSfe<#3?cz2W;$XyL(@L7mqaOvM@W}b#kYs}- zpR%ck+leKK+LYlj77M&QB$pg02U9yDAtQm%Z6(H#2J{pSW1Bj-`|0YaTHWWasa88d z_YT|e<741CIFi)yh>w%3RE1!vPDxuut~6Za_a8~7#PF{K$LV58IONW zbU?z>fRP@tQEIO$yLgEG(fmL>#Se0PB+^VpA)K@EQjUuRx+D*=?{uvqsZD4rum`Ed z%YI+f@ynCCb;T?=-z6=U0d#?5BmGRHmON&3><9 ze>ybFxzq3VmErXzlfsDjjO%lmaUwIk3xG*Y>h{k!KYyh!te&g*$xEUD;JB{;8(Ps? z>i>pOG@c?b8<$YVKxBrG?EhB9mZ(ivXt{ewHb zoBO7d(!<4P-)(h`@1)UMu3{Vae!2FpZ|P}e7R9mV7VBw4s^#I&O?2b)PmR~>TQ3Mg z1-iY@XuQH|VfsUCAUy>@*X$NO<8YS9tSI2}i?}Kw8^KL`lZ-No!oiSydq5tYuwf@#)fAEiT zp72mO6K;rzu@sru=0v6sCuReA^KEMV6i5Vn~!D(F6G=`7LyirL!LZfU|PVWGOVL+Fgfx+ z2GKCa*RofAm`7-c>=;<5MKRAKE!ny$PNfZu&Fdd`Uk4vg>`%p9+3j@4GN@aFQ?~#? z%axL3hy+0&nMI!mA)gC9c66`61CQW;{kP(Oa~1#V@cwuA{%`vUvcvx??;SVA|L{H` ze)lJ)_4zm;mEFZDV06){+_Qmo(bcXmj*UWVczAZan*t`vZVK2<0SoVhGePGDQ-F*O zUwjHkK_ZM-@OExK6iDZNgBQfVFbh;PQ|A7xSzsf@|DO|quA8!lUVI`D!G8W+aCdb->write( + "ALTER TABLE Calendar ADD COLUMN listViewPageInterval BIGINT" + ); + $session->db->write( + "ALTER TABLE Calendar ADD COLUMN templateIdList VARCHAR(22)" + ); + $session->db->write( + "ALTER TABLE Calendar ADD COLUMN templateIdPrintList VARCHAR(22)" + ); + $session->db->write( + "ALTER TABLE Calendar MODIFY COLUMN defaultView ENUM('month','week','day','list') DEFAULT 'month'" + ); + + print "DONE!\n" unless $quiet; + return; +} + +#---------------------------------------------------------------------------- +# Add the ability to set the iCalendar interval +sub addIcalPageSize { + my $session = shift; + print "\tAdding iCalendar interval to Calendar... " unless $quiet; + + $session->db->write( + "ALTER TABLE Calendar ADD COLUMN icalInterval BIGINT" + ); + + print "DONE!\n" unless $quiet; + return; +} + # --------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/lib/WebGUI/Asset/Wobject/Calendar.pm b/lib/WebGUI/Asset/Wobject/Calendar.pm index 3a1341378..cb973da00 100644 --- a/lib/WebGUI/Asset/Wobject/Calendar.pm +++ b/lib/WebGUI/Asset/Wobject/Calendar.pm @@ -2,15 +2,15 @@ package WebGUI::Asset::Wobject::Calendar; use strict; -#################################################################### +#---------------------------------------------------------------------------- # WebGUI is Copyright 2001-2008 Plain Black Corporation. -#################################################################### +#---------------------------------------------------------------------------- # Please read the legal notices (docs/legal.txt) and the license # (docs/license.txt) that came with this distribution before using # this software. -#################################################################### +#---------------------------------------------------------------------------- # http://www.plainblack.com info@plainblack.com -#################################################################### +#---------------------------------------------------------------------------- use Tie::IxHash; @@ -26,21 +26,20 @@ use base 'WebGUI::Asset::Wobject'; use DateTime; use JSON qw/to_json/; -=head1 Name +=head1 NAME -=head1 Description +=head1 DESCRIPTION -=head1 Synopsis +=head1 SYNOPSIS -=head1 Methods - +=head1 METHODS =cut -#################################################################### +#---------------------------------------------------------------------------- sub definition { my $class = shift; @@ -50,25 +49,27 @@ sub definition { my $i18n = WebGUI::International->new($session, 'Asset_Calendar'); ### Set up list options ### - tie (my %optionsDefaultView, 'Tie::IxHash', + tie my %optionsDefaultView, 'Tie::IxHash', ( month => $i18n->get("defaultView value month"), week => $i18n->get("defaultView value week"), day => $i18n->get("defaultView value day"), + list => $i18n->get('defaultView value list'), ); - tie (my %optionsDefaultDate, 'Tie::IxHash', + + tie my %optionsDefaultDate, 'Tie::IxHash', ( current => $i18n->get("defaultDate value current"), first => $i18n->get("defaultDate value first"), last => $i18n->get("defaultDate value last"), ); - tie (my %optionsEventSort, 'Tie::IxHash', + + tie my %optionsEventSort, 'Tie::IxHash', ( time => $i18n->get("sortEventsBy value time"), sequencenumber => $i18n->get("sortEventsBy value sequencenumber"), ); + ### Build properties hash ### - tie my %properties, 'Tie::IxHash'; - %properties = ( - + tie my %properties, 'Tie::IxHash', ( ##### DEFAULTS ##### defaultView => { fieldType => "SelectBox", @@ -97,6 +98,7 @@ sub definition { label => $i18n->get("groupIdEventEdit label"), hoverHelp => $i18n->get("groupIdEventEdit description"), }, + groupIdSubscribed => { fieldType => 'hidden', }, @@ -133,6 +135,16 @@ sub definition { label => $i18n->get('templateIdDay label'), }, + # List + templateIdList => { + fieldType => "template", + defaultValue => '', + tab => "display", + namespace => "Calendar/List", + hoverHelp => $i18n->get('editForm templateIdList description'), + label => $i18n->get('editForm templateIdList label'), + }, + # Event Details templateIdEvent => { fieldType => "template", @@ -195,6 +207,16 @@ sub definition { label => $i18n->get('templateIdPrintDay label'), }, + # List + templateIdPrintList => { + fieldType => "template", + defaultValue => '', + tab => "display", + namespace => "Calendar/Print/List", + hoverHelp => $i18n->get('editForm templateIdPrintList description'), + label => $i18n->get('editForm templateIdPrintList label'), + }, + # Event Details templateIdPrintEvent => { fieldType => "template", @@ -222,6 +244,24 @@ sub definition { label => $i18n->get("sortEventsBy label"), hoverHelp => $i18n->get("sortEventsBy description"), }, + + listViewPageInterval => { + fieldType => "interval", + defaultValue => $session->datetime->intervalToSeconds( 3, 'months' ), + tab => "display", + label => $i18n->get('editForm listViewPageInterval label'), + hoverHelp => $i18n->get('editForm listViewPageInterval description'), + unitsAvailable => [ qw( days weeks months years ) ], + }, + + icalInterval => { + fieldType => "interval", + defaultValue => $session->datetime->intervalToSeconds( 3, 'months' ), + tab => "display", + label => $i18n->get('editForm icalInterval label'), + hoverHelp => $i18n->get('editForm icalInterval description'), + unitsAvailable => [ qw( days weeks months years ) ], + }, # This doesn't function currently #subscriberNotifyOffset => { @@ -233,24 +273,19 @@ sub definition { #}, ); - push(@{$definition}, { + push @{$definition}, { assetName => $i18n->get('assetName'), icon => 'calendar.gif', tableName => 'Calendar', className => 'WebGUI::Asset::Wobject::Calendar', properties => \%properties, autoGenerateForms => 1, - }); + }; return $class->SUPER::definition($session, $definition); } - - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 addChild ( properties [, more ] ) @@ -271,11 +306,69 @@ sub addChild { return $self->SUPER::addChild($properties, @other); } +#---------------------------------------------------------------------------- +=head2 appendTemplateVarsDateTime( var, datetime [, prefix ] ) +Append template vars from the given datetime. C is a hash reference. +C is a WebGUI::DateTime object. C is an optional prefix for +the template variables. +=cut -#################################################################### +sub appendTemplateVarsDateTime { + my $self = shift; + my $var = shift; + my $dt = shift; + my $prefix = shift || ''; + + # Simple fields + my %fields = ( + # label => method + hour24 => 'hour', + hour => 'hour_12', + dayName => 'day_name', + dayAbbr => 'day_abbr', + dayOfMonth => 'day_of_month', + dayOfWeek => 'day_of_week', + monthName => 'month_name', + monthAbbr => 'month_abbr', + month => 'month', + year => 'year', + ymd => 'ymd', + mdy => 'mdy', + dmy => 'dmy', + hms => 'hms', + epoch => 'epoch', + ); + + for my $name ( keys %fields ) { + if ( $prefix ) { + $var->{ $prefix . ucfirst $name } + = $dt->can( $fields{ $name } )->( $dt ); + } + else { + $var->{ $name } = $dt->can( $fields{ $name } )->( $dt ); + } + } + + # Special fields + if ( $prefix ) { + $var->{ $prefix . "Second" } = sprintf "%02d", $dt->second; + $var->{ $prefix . "Minute" } = sprintf "%02d", $dt->minute; + $var->{ $prefix . "M" } = ( $dt->hour < 12 ? "AM" : "PM" ); + $var->{ $prefix . "Meridiem" } = $var->{ $prefix . "M" }; + } + else { + $var->{ "second" } = sprintf "%02d", $dt->second; + $var->{ "minute" } = sprintf "%02d", $dt->minute; + $var->{ "meridiem" } = ( $dt->hour < 12 ? "AM" : "PM" ); + } + + return $var; +} + +#---------------------------------------------------------------------------- =head2 canEdit @@ -303,10 +396,7 @@ sub canEdit { return $self->SUPER::canEdit() } - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 canAddEvent @@ -332,10 +422,7 @@ sub canAddEvent { ); } - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 createSubscriptionGroup ( ) @@ -361,11 +448,7 @@ sub createSubscriptionGroup { return undef; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 duplicate ( ) @@ -393,11 +476,7 @@ sub duplicate { return $newCalendar; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 getEditForm @@ -557,11 +636,7 @@ ENDHTML return $form; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 getEvent ( assetId ) @@ -582,17 +657,18 @@ sub getEvent { my $event = WebGUI::Asset->newByDynamicClass($self->session, $assetId); + unless ( $event ) { + $self->session->errorHandler->warn("Event '$assetId' doesn't exist!"); + return undef; + } + $self->session->errorHandler->warn("WebGUI::Asset::Wobject::Calendar->getEvent :: Event '$assetId' not a child of calendar '".$self->getId."'"), return unless $event->get("parentId") eq $self->getId; return $event; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 getEventsIn ( startDate, endDate, options ) @@ -638,6 +714,8 @@ sub getEventsIn { my ($startDate,$startTime) = split / /, $start; my ($endDate,$endTime) = split / /, $end; + #use Data::Dumper; + #$self->session->errorHandler->warn( Dumper [caller(1), caller(2), caller(3)] ); my $startTz = WebGUI::DateTime->new($self->session, mysql => $start, time_zone => $tz) ->set_time_zone("UTC")->toMysql; my $endTz = WebGUI::DateTime->new($self->session, mysql => $end, time_zone => $tz) @@ -689,7 +767,7 @@ sub getEventsIn { return @{$events}; } -#################################################################### +#---------------------------------------------------------------------------- =head2 getEventVars ( event ) @@ -710,11 +788,7 @@ sub getEventVars { return %eventVar, %eventDates; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 getFeeds ( ) @@ -734,11 +808,7 @@ sub getFeeds { ); } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 getFirstEvent ( ) @@ -760,14 +830,11 @@ sub getFirstEvent { LIMIT 1 ENDSQL + return unless $assetId; return $self->getEvent($assetId); } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 getLastEvent ( ) @@ -789,13 +856,27 @@ sub getLastEvent { LIMIT 1 ENDSQL + return unless $assetId; return $self->getEvent($assetId); } +#---------------------------------------------------------------------------- +=head2 getTemplateVars ( ) +Get template variables common to all views of the Calendar. -#################################################################### +=cut + +sub getTemplateVars { + my $self = shift; + my $var = $self->get; + + + return $var; +} + +#---------------------------------------------------------------------------- =head2 getSearchUrl ( ) @@ -808,10 +889,7 @@ sub getSearchUrl { return $self->getUrl('func=search'); } - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 prepareView ( ) @@ -835,7 +913,7 @@ sub prepareView { $self->session->style->makePrintable(1); } - #$self->session->errorHandler->warn("Prepare view ".$view." with template ".$self->get("templateId".$view)); + $self->session->errorHandler->warn("Prepare view ".$view." with template ".$self->get("templateId".$view)); my $template = WebGUI::Asset::Template->new($self->session, $self->get("templateId".$view)); $template->prepare; @@ -843,12 +921,7 @@ sub prepareView { $self->{_viewTemplate} = $template; } - - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 processPropertiesFromFormPost ( ) @@ -869,7 +942,8 @@ sub processPropertiesFromFormPost { unless ($self->get("groupIdSubscribed")) { $self->createSubscriptionGroup(); } - + + $self->session->errorHandler->info( "DEFAULT VIEW:" . $self->get('defaultView') ); ### Get feeds from the form # Workaround WebGUI::Session::Form->param bug that returns duplicate @@ -906,10 +980,12 @@ sub processPropertiesFromFormPost { feedType => "ical", }); } + + return; } -#################################################################### +#---------------------------------------------------------------------------- =head2 purge ( ) @@ -925,7 +1001,7 @@ sub purge { $self->SUPER::purge; } -#################################################################### +#---------------------------------------------------------------------------- =head2 view ( ) @@ -943,8 +1019,6 @@ sub view { my $self = shift; my $session = $self->session; my $form = $session->form; - my $var; - ## INTERRUPT: If user is only a Visitor and we have a cached version # and is not expired, use it. @@ -955,7 +1029,7 @@ sub view { $params->{start} = $form->param("start"); # Validate type passed, or recover from session scratchpad - if ($params->{type} =~ /^(?:month|week|day)$/) { + if ($params->{type} =~ /^(?:month|week|day|list)$/i) { $session->scratch->set('cal_view_type', $params->{'type'}); } else { @@ -985,13 +1059,16 @@ sub view { } # Get the template from the appropriate view* method - $var = lc $params->{type} eq "month" ? $self->viewMonth($params) - : lc $params->{type} eq "week" ? $self->viewWeek($params) - : lc $params->{type} eq "day" ? $self->viewDay($params) + # TODO: This should be abstracted + my $var = lc $params->{type} eq "month" ? $self->viewMonth( $params ) + : lc $params->{type} eq "week" ? $self->viewWeek( $params ) + : lc $params->{type} eq "day" ? $self->viewDay( $params ) + : lc $params->{type} eq "list" ? $self->viewList( $params ) : return $self->errorHandler->error("Calendar invalid 'type=' url parameter") ; - ##### Add any global variables + ##### Process the template + # Add any global variables # Admin if ($self->session->var->isAdminOn) { $var->{'admin'} = 1; @@ -1005,6 +1082,7 @@ sub view { } # URLs + $var->{ url } = $self->getUrl; $var->{"urlDay"} = $self->getUrl("type=day;start=".$params->{start}); $var->{"urlWeek"} = $self->getUrl("type=week;start=".$params->{start}); $var->{"urlMonth"} = $self->getUrl("type=month;start=".$params->{start}); @@ -1016,25 +1094,18 @@ sub view { $params->{start}, ); - # Parameters - $var->{"paramStart"} = $params->{start}; - $var->{"paramType"} = $params->{type}; - $var->{"extrasUrl"} = $self->session->url->extras(); - - ##### Process the template + $var->{ paramStart } = $params->{ start }; + $var->{ paramType } = $params->{ type }; + # TODO: If user is only a Visitor and we've gotten this far, update the cache # Return the processed template to be displayed for the user return $self->processTemplate($var, undef, $self->{_viewTemplate}); } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 viewDay ( \%params ) @@ -1057,7 +1128,7 @@ sub viewDay { my $session = $self->session; my $params = shift; my $i18n = WebGUI::International->new($session,"Asset_Calendar"); - my $var = {}; + my $var = $self->getTemplateVars; ### Get all the events in this time period # Get the range of the epoch of this day @@ -1120,12 +1191,85 @@ sub viewDay { return $var; } +#---------------------------------------------------------------------------- +=head2 viewList ( \%params ) +Shows the list view. Returns the template parameters as a hash reference. +%params keys: +=over 4 -#################################################################### +=item start + +The start of the list page + +=back + +=cut + +sub viewList { + my $self = shift; + my $params = shift; + my $session = $self->session; + my $i18n = WebGUI::International->new($session,"Asset_Calendar"); + my $var = $self->getTemplateVars; + + ### Get the events + my $dtStart = WebGUI::DateTime->new( $session, $params->{start} ); + my $dtEnd = $dtStart->clone->add( seconds => $self->get('listViewPageInterval') ); + + my @events + = $self->getEventsIn( + $dtStart->toDatabase, + $dtEnd->toDatabase, + ); + + ### Build the event vars + my $dtLast = $dtStart; # The DateTime of the last event + for my $event (@events) { + my ( %eventVar, %eventDate ) + = $self->getEventVars( $event ); + + # Add the change flags + my $dt = $event->getDateTimeStart; + if ( $dt->year > $dtLast->year ) { + $eventVar{ new_year } = 1; + } + if ( $dt->month > $dtLast->month ) { + $eventVar{ new_month } = 1; + } + if ( $dt->day > $dtLast->day ) { + $eventVar{ new_day } = 1; + } + + push @{ $var->{events} }, { %eventVar, %eventDate }; + $dtLast = $dt; + } + + ### Additional variables + # Date span + $self->appendTemplateVarsDateTime( $var, $dtStart, "start" ); + $self->appendTemplateVarsDateTime( $var, $dtEnd, "end" ); + + # Previous and next pages + if ( $self->getFirstEvent && $self->getFirstEvent->getDateTimeStart < $dtStart ) { + my $dtPrevious + = $dtStart->clone->add( seconds => 0 - $self->get('listViewPageInterval') ); + $var->{ url_previousPage } + = $self->getUrl( 'type=list;start=' . $dtPrevious->toDatabase ); + } + + if ( $self->getLastEvent && $self->getLastEvent->getDateTimeStart > $dtEnd ) { + $var->{ url_nextPage } + = $self->getUrl( 'type=list;start=' . $dtEnd->toDatabase ); + } + + return $var; +} + +#---------------------------------------------------------------------------- =head2 viewMonth ( \%params ) @@ -1148,7 +1292,7 @@ sub viewMonth { my $session = $self->session; my $params = shift; my $i18n = WebGUI::International->new($session,"Asset_Calendar"); - my $var = {}; + my $var = $self->getTemplateVars; my $tz = $session->user->profileField("timeZone"); my $today = WebGUI::DateTime->new($self->session, time) ->set_time_zone($tz)->toMysqlDate; @@ -1265,12 +1409,7 @@ sub viewMonth { return $var; } - - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 viewWeek ( \%params ) @@ -1293,7 +1432,7 @@ sub viewWeek { my $session = $self->session; my $params = shift; my $i18n = WebGUI::International->new($session,"Asset_Calendar"); - my $var = {}; + my $var = $self->getTemplateVars; my $tz = $session->user->profileField("timeZone"); my $today = WebGUI::DateTime->new($self->session, time)->set_time_zone($tz) ->toMysqlDate; @@ -1571,11 +1710,7 @@ sub viewWeek { return $var; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 unwrapIcal ( text ) @@ -1594,11 +1729,7 @@ sub unwrapIcal } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 wrapIcal ( text ) @@ -1621,11 +1752,7 @@ sub wrapIcal { return join "\r\n ",@text; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 www_edit ( ) @@ -1647,12 +1774,7 @@ sub www_edit { ); } - - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 www_ical @@ -1663,17 +1785,9 @@ Export an iCalendar feed of this Events Calendar's events. sub www_ical { my $self = shift; my $session = $self->session; + my $user = $self->session->user; my $form = $self->session->form; - #!!! Events from what time period should we show? Default perpage? - # By default show the events for a month - my $type = $form->param("type") || lc($self->get("defaultView")) || "month"; - my $start = $form->param("start"); - my $end = $form->param("end"); - - - - #!!! KLUDGE: # An "adminId" may be passed as a parameter in order to facilitate # calls between calendars on the same server getting administrator @@ -1696,53 +1810,31 @@ sub www_ical { } #/KLUDGE - my $dt_start; - unless ($start) { - #if ($self->get("defaultDate") eq "first") - #{ - #!! TODO: Get the first event's date - # select startDate from Events - # join assetLineage - # order by startDate ASC, revisionDate DESC - # limit 1 - #} - #elsif ($self->get("defaultDate") eq "last") - #{ - #!! TODO: Get the last event's date - # select startDate from Events - # join assetLineage - # order by startDate DESC, revisionDate DESC - # limit 1 - #} - #else - #{ - $dt_start = WebGUI::DateTime->new($self->session, time)->set_time_zone($session->user->profileField("timeZone")); - #} + my $start = $form->param("start"); + if ($start) { + $dt_start + = WebGUI::DateTime->new($session, + mysql => $start, + time_zone => $user->profileField("timeZone") + ); } else { - $dt_start = WebGUI::DateTime->new($self->session, mysql => $start, time_zone => $session->user->profileField("timeZone")); + $dt_start = WebGUI::DateTime->new($self->session, time); + $dt_start->set_time_zone( $user->profileField("timeZone") ); } - - my $dt_end; - unless ($end) { - #if ($type eq "month") - #{ - $dt_end = $dt_start->clone->add(months => 1); - #} - #elsif ($type eq "week") - #{ - # $dt_end = $dt_start->clone->add(weeks => 1); - #} - #elsif ($type eq "day") - #{ - # $dt_end = $dt_start->clone->add(days => 1); - #} + my $end = $form->param("end"); + if ($end) { + $dt_end + = WebGUI::DateTime->new($self->session, + mysql => $end, + time_zone => $user->profileField("timeZone") + ); } else { - $dt_end = WebGUI::DateTime->new($self->session, mysql => $end, time_zone => $session->user->profileField("timeZone")); + $dt_end = $dt_start->clone->add( seconds => $self->get('icalInterval') ); } @@ -1820,11 +1912,7 @@ sub www_ical { return $ical; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 www_importIcal @@ -1840,11 +1928,7 @@ sub www_importIcal { return $_[0]->session->privilege->noAccess; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 www_search ( ) @@ -1864,7 +1948,7 @@ sub www_search { my $endDate = $form->process("enddate"); my $perpage = $form->param("perpage"); - my $var = $self->get; + my $var = $self->getTemplateVars; $var->{url} = $self->getUrl; # If there is a search to perform @@ -1988,11 +2072,7 @@ sub www_search { return "chunked"; } - - - - -#################################################################### +#---------------------------------------------------------------------------- =head2 www_view ( ) @@ -2021,18 +2101,6 @@ page. =back - - - -=head1 Templates - -The templates provided by this Wobject and the parameters they contain - -!!! TODO !!! - - - - =head1 BUGS / RFE In the calendar edit form on the Default View field on the display tab, put the diff --git a/lib/WebGUI/Help/Asset_Calendar.pm b/lib/WebGUI/Help/Asset_Calendar.pm index 06bd968ff..8ff6842b1 100644 --- a/lib/WebGUI/Help/Asset_Calendar.pm +++ b/lib/WebGUI/Help/Asset_Calendar.pm @@ -6,8 +6,6 @@ use warnings; our $HELP = { -#### View Calendar Page - 'view calendar template' => { title => 'view calendar title', body => 'view calendar body', @@ -38,13 +36,38 @@ our $HELP = { related => [] }, -#### Search Calendar Page + 'calendar dateTime' => { + title => 'help calendar dateTime title', + body => 'help calendar dateTime body', + variables => [ + { 'name' => 'second', description => 'helpvar dateTime second', }, + { 'name' => 'minute', description => 'helpvar dateTime minute', }, + { 'name' => 'meridiem', description => 'helpvar dateTime meridiem', }, + { 'name' => 'month', description => 'helpvar dateTime month', }, + { 'name' => 'monthName', description => 'helpvar dateTime monthName', }, + { 'name' => 'monthAbbr', description => 'helpvar dateTime monthAbbr', }, + { 'name' => 'dayOfMonth', description => 'helpvar dateTime dayOfMonth', }, + { 'name' => 'dayName', description => 'helpvar dateTime dayName', }, + { 'name' => 'dayAbbr', description => 'helpvar dateTime dayAbbr', }, + { 'name' => 'year', description => 'helpvar dateTime year', }, + { 'name' => 'dayOfWeek', description => 'helpvar dateTime dayOfWeek', }, + { 'name' => 'ymd', description => 'helpvar dateTime ymd', }, + { 'name' => 'mdy', description => 'helpvar dateTime mdy', }, + { 'name' => 'dmy', description => 'helpvar dateTime dmy', }, + { 'name' => 'epoch', description => 'helpvar dateTime epoch', }, + ], + }, -#### ICal Calendar Page - -#### View Month Template - -#### View Week Template + 'event variables' => { + title => 'help event variables title', + body => 'help event variables body', + related => [ + { + namespace => 'Asset_Event', + tag => 'event common variables', + }, + ], + }, 'view week template' => { title => 'view calendar week title', @@ -143,9 +166,60 @@ our $HELP = { related => [] }, +#### List view template + 'view list template' => { + title => 'help view list title', + body => 'help view list body', + isa => [ + { + namespace => "Asset_Calendar", + tag => "view calendar template", + }, + { + namespace => "Asset_Calendar", + tag => "event variables", + }, + ], + related => [ + { + namespace => "Asset_Calendar", + tag => 'calendar dateTime', + }, + ], + variables => [ + { + name => 'newYear', + description => 'helpvar newYear', + }, + { + name => 'newMonth', + description => 'helpvar newMonth', + }, + { + name => 'newDay', + description => 'helpvar newDay', + }, + { + name => 'url_previousPage', + description => 'helpvar url_previousPage', + }, + { + name => 'url_nextPage', + description => 'helpvar url_nextPage', + }, + { + name => 'start', + description => 'helpvar dateTime start', + }, + { + name => 'end', + description => 'helpvar dateTime end', + }, + + ], + }, }; -#### Search Template 1; diff --git a/lib/WebGUI/i18n/English/Asset_Calendar.pm b/lib/WebGUI/i18n/English/Asset_Calendar.pm index 07ba78d22..cfee17640 100755 --- a/lib/WebGUI/i18n/English/Asset_Calendar.pm +++ b/lib/WebGUI/i18n/English/Asset_Calendar.pm @@ -8,18 +8,17 @@ our $I18N = { # context => q{}, #}, + 'assetName' => { + message => q{Calendar}, + lastUpdated => 1131394072, + }, -#################### DATETIME LOCALE #################### 'locale' => { message => q{en_US}, lastUpdated => 0, context => q{The ISO locale name for month and day labels.}, }, - -#################### CALENDAR PROPERTIES FIELDS #################### - - ##### Subscriber Notify Offset ##### 'subscriberNotifyOffset label' => { message => q{Subscriber Notify Offset}, lastUpdated => 0, @@ -31,11 +30,6 @@ our $I18N = { context => q{The Hover Help for the Subscriber Notify Offset field}, }, - - -#################### CALENDAR DISPLAY FIELDS #################### - - ##### Default View ##### 'defaultView label' => { message => q{Default View}, lastUpdated => 0, @@ -61,9 +55,13 @@ our $I18N = { lastUpdated => 0, context => q{A value for the Default View field.}, }, + 'defaultView value list' => { + message => q{List}, + lastUpdated => 0, + context => q{A value for the Default View field.}, + }, - ##### Default Date ##### 'defaultDate label' => { message => q{Default Date}, lastUpdated => 0, @@ -91,7 +89,6 @@ our $I18N = { }, - ##### Visitor Cache Timeout ##### 'visitorCacheTimeout label' => { message => q{Visitor Cache Timeout}, lastUpdated => 0, @@ -104,7 +101,6 @@ our $I18N = { }, - ##### Default Daily Event Sort Order ##### 'sortEventsBy label' => { message => q{Daily Events Sort Order}, lastUpdated => 0, @@ -126,14 +122,32 @@ our $I18N = { context => q{A value for the Daily Events Sort Order field.}, }, + + 'editForm listViewPageInterval label' => { + message => "List View Page Interval", + lastUpdated => 0, + context => 'Label for the asset property', + }, + 'editForm listViewPageInterval description' => { + message => "Period of time displayed in a single page of the list view", + lastUpdated => 0, + context => 'Description of the asset property', + }, + + 'editForm icalInterval label' => { + message => "ICalendar Feed Interval", + lastUpdated => 0, + context => 'Label for the asset property', + }, + 'editForm icalInterval description' => { + message => "Period of time displayed in the iCalendar feed", + lastUpdated => 0, + context => 'Description of the asset property', + }, - -#################### CALENDAR SECURITY FIELDS #################### - - ##### Group to add/edit events ##### 'groupIdEventEdit label' => { message => q{Who can add Events?}, lastUpdated => 0, @@ -148,9 +162,7 @@ our $I18N = { -#################### CALENDAR FEEDS FIELDS #################### - ##### Feeds tab ##### 'feeds' => { message => q{Feeds}, lastUpdated => 0, @@ -159,11 +171,9 @@ our $I18N = { -#################### TEMPLATES #################### - ##### Template - Month ##### 'templateIdMonth label' => { message => q{Month View Template}, lastUpdated => 0, @@ -175,7 +185,6 @@ our $I18N = { context => q{Hover Help for the default month template.}, }, - ##### Template - Week ##### 'templateIdWeek label' => { message => q{Week View Template}, lastUpdated => 0, @@ -187,7 +196,6 @@ our $I18N = { context => q{Hover Help for the default Week template.}, }, - ##### Template - Day ##### 'templateIdDay label' => { message => q{Day View Template}, lastUpdated => 0, @@ -198,8 +206,18 @@ our $I18N = { lastUpdated => 0, context => q{Hover Help for the default Day template.}, }, + + 'editForm templateIdList label' => { + message => q{List View Template}, + lastUpdated => 0, + context => q{The label for the default List template.}, + }, + 'editForm templateIdList description' => { + message => q{This template shows the calendar in List form.}, + lastUpdated => 0, + context => q{Hover Help for the default List template.}, + }, - ##### Template - Event ##### 'templateIdEvent label' => { message => q{Event Details Template}, lastUpdated => 0, @@ -211,7 +229,6 @@ our $I18N = { context => q{Hover Help for the default Event template.}, }, - ##### Template - EventEdit ##### 'templateIdEventEdit label' => { message => q{Event Edit Template}, lastUpdated => 0, @@ -223,7 +240,6 @@ our $I18N = { context => q{Hover Help for the default Event Edit template.}, }, - ##### Template - Search ##### 'templateIdSearch label' => { message => q{Search View Template}, lastUpdated => 0, @@ -237,7 +253,6 @@ our $I18N = { - ##### Template - Print Month ##### 'templateIdPrintMonth label' => { message => q{Print Month Template}, lastUpdated => 0, @@ -249,7 +264,6 @@ our $I18N = { context => q{Hover Help for the default month template.}, }, - ##### Template - Print Week ##### 'templateIdPrintWeek label' => { message => q{Print Week Template}, lastUpdated => 0, @@ -261,7 +275,6 @@ our $I18N = { context => q{Hover Help for the default Week template.}, }, - ##### Template - Print Day ##### 'templateIdPrintDay label' => { message => q{Print Day Template}, lastUpdated => 0, @@ -273,7 +286,17 @@ our $I18N = { context => q{Hover Help for the default Day template.}, }, - ##### Template - Print Event ##### + 'editForm templateIdPrintList label' => { + message => q{Print List View Template}, + lastUpdated => 0, + context => q{The label for the default Print List template.}, + }, + 'editForm templateIdPrintList description' => { + message => q{This template print the calendar in List form.}, + lastUpdated => 0, + context => q{Hover Help for the default Print List template.}, + }, + 'templateIdPrintEvent label' => { message => q{Print Event Details Template}, lastUpdated => 0, @@ -285,9 +308,6 @@ our $I18N = { context => q{Hover Help for the default Event template.}, }, - - -#################### WORKFLOW ACTIVITIES #################### 'workflow updateFeeds' => { message => q{Update Calendar Feeds}, lastUpdated => 0, @@ -313,14 +333,11 @@ our $I18N = { context => q{Description of what the CalendarGenerateRecurringEvents workflow activity does}, }, -#################### HELP PAGES #################### 'searchButtonLabel' => { message => q|Search|, lastUpdated => 1170803504, }, -#################### HELP PAGES View Calendar #################### - 'add event' => { message => q|Add Event|, lastUpdated => 1171043337, @@ -624,11 +641,184 @@ our $I18N = { }, -#################### ASSET NAME #################### - 'assetName' => { - message => q{Calendar}, - lastUpdated => 1131394072, - }, + 'help view list title' => { + message => 'Calendar List View', + lastUpdated => 0, + context => 'Title for help page', + }, + + 'help view list body' => { + message => 'These variables are available to the Calendar List View', + lastUpdated => 0, + context => 'Body of help page', + }, + + 'helpvar newYear' => { + message => 'This variable is true when the current event is in a different + year than the previous event.', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar newMonth' => { + message => 'This variable is true when the current event is in a different + month than the previous event.', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar newDay' => { + message => 'This variable is true when the current event is in a different + day than the previous event.', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar url_previousPage' => { + message => 'The URL to the previous page. If there is no previous page, this + variable will not exist.', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar url_nextPage' => { + message => 'The URL to the next page. If there is no previous page, this + variable will not exist.', + lastUpdated => 0, + context => 'Description of template variable', + }, + + + 'help calendar dateTime title' => { + message => 'Calendar Date/Time variables', + lastUpdated => 0, + context => 'Title for help page', + }, + + 'help calendar dateTime body' => { + message => 'These variables are available for most date/time in the Calendar.

+ NOTE: Sometimes these variables have a prefix, like "start" or "end". + In that case, the first letter of the variables below is capitalized, so + "monthName" with a prefix of "start" becomes "startMonthName".', + lastUpdated => 0, + context => 'Body for help page', + }, + + 'helpvar dateTime second' => { + message => 'The seconds', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime minute' => { + message => 'The minutes', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime meridiem' => { + message => 'The meridiem (A.M. or P.M.)', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime month' => { + message => 'The month number (01)', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime monthName' => { + message => 'The month name ("January")', + lastUpdated => 0, + context => 'Description of template variable', + },, + + 'helpvar dateTime monthAbbr' => { + message => 'The abbreviated month name ("Jan")', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime dayOfMonth' => { + message => 'The number of the day of the month', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime dayName' => { + message => 'The day name (Sunday)', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime dayAbbr' => { + message => 'The abbreviated day name (Sun)', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime year' => { + message => 'The year', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime dayOfWeek' => { + message => 'The number of the day of the week (1 is Monday, 7 is Sunday)', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime ymd' => { + message => 'The year, month, and day in ISO format: YYYY-MM-DD', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime mdy' => { + message => 'The month, day, and year in US format: MM/DD/YYYY', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime dmy' => { + message => 'The day, month, and year in UK format: DD/MM/YYYY', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime epoch' => { + message => 'The epoch date (number of seconds since 1970-01-01 00:00:00). Suitable to be used in the Date macro.', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'helpvar dateTime start' => { + message => 'A set of date/time variables with the prefix "start". See "Calendar Date/Time Variables" for more information.', + lastUpdated => 0, + context => 'Description of template variable.', + }, + + 'helpvar dateTime end' => { + message => 'A set of date/time variables with the prefix "end". See "Calendar Date/Time Variables for more information.', + lastUpdated => 0, + context => 'Description of template variable', + }, + + 'help event variables title' => { + message => "Calendar Event Variables", + lastUpdated => 0, + context => 'Title for help page', + }, + + 'help event variables body' => { + message => q{When the Calendar is displaying an Event, it gets the entire set of Event template variables and changes the name slightly.

+ The Event template variable "title" becomes "eventTitle". The Event template variable "startDate" becomes "eventStartDate" and so on. + }, + lastUpdated => 0, + context => 'Body for help page', + }, }; 1; diff --git a/t/Asset/Redirect/mech.t b/t/Asset/Redirect/mech.t new file mode 100644 index 000000000..84e04a4cf --- /dev/null +++ b/t/Asset/Redirect/mech.t @@ -0,0 +1,174 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# This script uses Test::WWW::Mechanize to test the operation of the Redirect +# asset. +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use Test::More; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Asset; +use WebGUI::VersionTag; +use WebGUI::Session; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +my $node = WebGUI::Asset->getImportNode( $session ); +my @versionTags = ( WebGUI::VersionTag->getWorking( $session ) ); + +# Override some settings to make things easier to test +my %oldSettings; +# userFunctionStyleId +$oldSettings{ userFunctionStyleId } = $session->setting->get( 'userFunctionStyleId' ); +$session->setting->set( 'userFunctionStyleId', 'PBtmpl0000000000000132' ); + +# Create a user for testing purposes +my $user = WebGUI::User->new( $session, "new" ); +$user->username( 'dufresne' ); +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 ($mech, $redirect, $response); + +# Get the site's base URL +my $baseUrl = 'http://' . $session->config->get('sitename')->[0]; +$baseUrl .= $session->config->get('gateway'); + +# Set some constants +my $redirectUrl = time . "shawshank"; +my $testContent = "Perhaps if you've gone this far, you'd be willing to go further."; +my $snippetUrl = time . "zejuatenejo"; +my $redirectToUrl = $snippetUrl . "?name=value"; +my $redirectToAsset + = $node->addChild({ + className => 'WebGUI::Asset::Snippet', + url => $snippetUrl, + snippet => $testContent, + }); +$versionTags[-1]->commit; + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 13; # Increment this number for each test you create + +use_ok( 'Test::WWW::Mechanize' ); + +#---------------------------------------------------------------------------- +# Test operation with a public Redirect +push @versionTags, WebGUI::VersionTag->getWorking( $session ); +$redirect + = $node->addChild({ + className => 'WebGUI::Asset::Redirect', + redirectUrl => $redirectToUrl, + url => $redirectUrl . scalar(@versionTags), + }); +$versionTags[-1]->commit; + +$mech = Test::WWW::Mechanize->new; +$mech->get_ok( $baseUrl . $redirectUrl . scalar(@versionTags), "We get the redirect" ); +$mech->content_contains( $testContent, "We made it to the snippet" ); + +$response = $mech->res->previous; +ok( $response, 'There were at least two requests' ); +is( + $response->headers->header('location'), + $redirectToUrl, + 'We were redirected to the right URL' +); + +#---------------------------------------------------------------------------- +# Test operation with a private Redirect through a login +push @versionTags, WebGUI::VersionTag->getWorking( $session ); +$redirect + = $node->addChild({ + className => 'WebGUI::Asset::Redirect', + redirectUrl => $redirectToUrl, + url => $redirectUrl . scalar(@versionTags), + groupIdView => 2, + groupIdEdit => 3, + }); +$versionTags[-1]->commit; + +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl . $redirectUrl . scalar(@versionTags) ); +$mech->submit_form_ok( { + with_fields => { + username => $user->username, + identifier => $identifier, + }, +}, 'Sent login form' ); +$mech->content_contains( $testContent, "We made it to the snippet through the login" ); + +$response = $mech->res->previous; +ok( $response, 'There were at least two requests' ); +is( + $response->headers->header('location'), + $redirectToUrl, + "We were redirected to the right URL", +); + + +#---------------------------------------------------------------------------- +# Test operation with a private Redirect through a login with translate +# query params +push @versionTags, WebGUI::VersionTag->getWorking( $session ); +$redirect + = $node->addChild({ + className => 'WebGUI::Asset::Redirect', + redirectUrl => $redirectToUrl, + url => $redirectUrl . scalar(@versionTags), + groupIdView => 2, + groupIdEdit => 3, + forwardQueryParams => 1, + }); +$versionTags[-1]->commit; + +my $extraParams = 'extra=hi'; +$mech = Test::WWW::Mechanize->new; +$mech->get( $baseUrl . $redirectUrl . scalar(@versionTags) . '?' . $extraParams ); +$mech->submit_form_ok( { + with_fields => { + username => $user->username, + identifier => $identifier, + }, +}, 'Sent login form' ); +$mech->content_contains( $testContent, "We made it to the snippet through the login" ); + +$response = $mech->res->previous; +ok( $response, 'There were at least two requests' ); +is( + $response->headers->header('location'), + $redirectToUrl . ';' . $extraParams, + "We were redirected to the right URL with forwarded query params", +); + + +#---------------------------------------------------------------------------- +# Cleanup +END { + for my $tag ( @versionTags ) { + $tag->rollback; + } + + $user->delete; + + for my $key ( keys %oldSettings ) { + $session->setting->set( $key, $oldSettings{ $key } ); + } +} diff --git a/t/Auth/mech.t b/t/Auth/mech.t new file mode 100644 index 000000000..fde65d1c9 --- /dev/null +++ b/t/Auth/mech.t @@ -0,0 +1,277 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# This script uses Test::WWW::Mechanize to test the operation of Auth +# NOTE: This mostly tests Auth's common methods, even though it uses +# WebGUI::Auth::WebGUI. + +# no form: tests assume that the form exists on the page +# displayLogin: tests go to ?op=auth;method=displayLogin after going to +# unauthorized page +# returnUrl: tests use returnUrl= to try to return to the right place + +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; +use Test::More; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Asset; +use WebGUI::VersionTag; +use WebGUI::Session; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +my $node = WebGUI::Asset->getImportNode( $session ); +my @versionTags = ( WebGUI::VersionTag->getWorking( $session ) ); + +# Override some settings to make things easier to test +my %oldSettings; +# userFunctionStyleId +$oldSettings{ userFunctionStyleId } = $session->setting->get( 'userFunctionStyleId' ); +$session->setting->set( 'userFunctionStyleId', 'PBtmpl0000000000000132' ); + +# Create a user for testing purposes +my $USERNAME = 'dufresne'; +my $IDENTIFIER = 'ritahayworth'; +my $user = WebGUI::User->new( $session, "new", "something new" ); +$user->username( $USERNAME ); +$user->addToGroups( ['3'] ); +my $auth = WebGUI::Operation::Auth::getInstance( $session, $user->authMethod, $user->userId ); +$auth->saveParams( $user->userId, $user->authMethod, { + 'identifier' => Digest::MD5::md5_base64( $IDENTIFIER ), +}); + +my ($mech, $redirect, $response, $url); + +# Get the site's base URL +my $baseUrl = 'http://' . $session->config->get('sitename')->[0]; +$baseUrl .= $session->config->get('gateway'); + +# Make an asset we can login on +my $asset + = $node->addChild({ + className => 'WebGUI::Asset::Wobject::Article', + description => "ARTICLE", + url => time . 'loginAsset', + groupIdView => 2, # Registered Users + groupIdEdit => 3, # Admins + styleTemplateId => 'PBtmpl0000000000000132', + }); +$versionTags[-1]->commit; +my $assetUrl = $baseUrl . $asset->get('url'); + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 41; # Increment this number for each test you create + +use_ok( 'Test::WWW::Mechanize' ); + +#---------------------------------------------------------------------------- +# no form: Test logging in on a normal page sends the user back to the same page +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl ); +$mech->base_is( $assetUrl, "We got the page we were expecting" ); +$url = $assetUrl . '?op=auth;method=login;username=' . $USERNAME . ';identifier=' . $IDENTIFIER; +$mech->get_ok( $url ); +$mech->base_is( $assetUrl, "We weren't redirected anywhere" ); +$mech->content_contains( "ARTICLE", "We are shown the article" ); + + +#---------------------------------------------------------------------------- +# no form: Test logging in on a normal page sends user back to same page AFTER at least one +# failed attempt +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl ); +$mech->base_is( $assetUrl, "We got the page we were expecting" ); +$url = $assetUrl . '?op=auth;method=login;username=' . $USERNAME . ';identifier=nowai'; +$mech->get( $url ); +$mech->submit_form_ok( + { + with_fields => { + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); +$mech->base_is( $assetUrl, "We weren't redirected anywhere" ); +$mech->content_contains( "ARTICLE", "We are shown the article" ); + + +#---------------------------------------------------------------------------- +# displayLogin: Test logging in on a normal page sends the user back to the same page +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl ); +$mech->base_is( $assetUrl, "We got the page we were expecting" ); +$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" ); +$mech->submit_form_ok( + { + with_fields => { + op => 'auth', + method => 'login', + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); + +$mech->base_is( $assetUrl, "We were redirected to the same page after login" ); + +#---------------------------------------------------------------------------- +# displayLogin: Test logging in on a normal page sends user back to same page AFTER at least one +# failed attempt +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl ); +$mech->base_is( $assetUrl, "We got the page we were expecting" ); +$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" ); +$mech->submit_form( + with_fields => { + username => $USERNAME, + identifier => 'innocence', + }, +); +$mech->submit_form_ok( + { + with_fields => { + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); + +$mech->base_is( $assetUrl, "We were redirected to the same page after login and failing once"); + +#---------------------------------------------------------------------------- +# displayLogin: Test logging in on an operation other than ?op=auth +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl . '?op=listUsers' ); +$mech->base_is( $assetUrl . '?op=listUsers', "We got the page we were expecting" ); +$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" ); +$mech->submit_form_ok( + { + with_fields => { + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); + +$mech->base_is( $assetUrl, "We weren't redirected"); + +#---------------------------------------------------------------------------- +# displayLogin: Test logging in on an operation other than ?op=auth after at least one +# failed attempt +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl . '?op=listUsers' ); +$mech->base_is( $assetUrl . '?op=listUsers', "We got the page we were expecting" ); +$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" ); +$mech->submit_form( + with_fields => { + username => $USERNAME, + identifier => 'innocence', + }, +); +$mech->submit_form_ok( + { + with_fields => { + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); + +$mech->base_is( $assetUrl, "We weren't redirected" ); + + +#---------------------------------------------------------------------------- +# displayLogin: Test logging in after directly going to ?op=auth;method=init +$mech = Test::WWW::Mechanize->new; +$mech->get_ok( $assetUrl . '?op=auth;method=init' ); +$mech->base_is( $assetUrl . '?op=auth;method=init', "We got the page we were expecting" ); +$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" ); +$mech->submit_form_ok( + { + with_fields => { + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); + +$mech->base_is( $assetUrl, "We were redirected to the right page" ); + + +#---------------------------------------------------------------------------- +# displayLogin: Test logging in after directly going to ?op=auth;method=init and failing +# at least once. +$mech = Test::WWW::Mechanize->new; +$mech->get_ok( $assetUrl . '?op=auth;method=init' ); +$mech->base_is( $assetUrl . '?op=auth;method=init', "We got the page we were expecting" ); +$mech->get_ok( $assetUrl . "?op=auth;method=displayLogin" ); +$mech->submit_form( + with_fields => { + username => $USERNAME, + identifier => 'innocence', + }, +); +$mech->submit_form_ok( + { + with_fields => { + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); + +$mech->base_is( $assetUrl, "We were redirected to the right place" ); + +#---------------------------------------------------------------------------- +# returnUrl: Test logging in on a normal page sends the user back to the same page +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl ); +$mech->base_is( $assetUrl, "We got the page we were expecting" ); +$url = $assetUrl + . '?op=auth;returnUrl=%2Froot%2Fimport;method=login;username=' + . $USERNAME . ';identifier=' . $IDENTIFIER; +$mech->get_ok( $url ); +$mech->base_is( $baseUrl . 'root/import', "We were redirected properly" ); + + +#---------------------------------------------------------------------------- +# returnUrl: Test logging in on a normal page sends user back to same page AFTER at least one +# failed attempt +$mech = Test::WWW::Mechanize->new; +$mech->get( $assetUrl ); +$mech->base_is( $assetUrl, "We got the page we were expecting" ); +$url = $assetUrl + . '?op=auth;returnUrl=%2Froot%2Fimport;method=login;username=' + . $USERNAME . ';identifier=nowai'; +$mech->get( $url ); +$mech->submit_form_ok( + { + with_fields => { + username => $USERNAME, + identifier => $IDENTIFIER, + }, + }, +); +$mech->base_is( $assetUrl, "We don't get redirected" ); + +#---------------------------------------------------------------------------- +# Cleanup +END { + $user->delete; + + for my $key ( keys %oldSettings ) { + $session->setting->set( $key, $oldSettings{ $key } ); + } +}