From: <pau...@us...> - 2007-07-24 18:08:53
|
Revision: 977 http://svn.sourceforge.net/everydevel/?rev=977&view=rev Author: paul_the_nomad Date: 2007-07-24 11:08:52 -0700 (Tue, 24 Jul 2007) Log Message: ----------- Added tests, bug fixes and clean up of the code Modified Paths: -------------- trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm Property Changed: ---------------- trunk/ebase/ Property changes on: trunk/ebase ___________________________________________________________________ Name: svk:merge - 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:1030 1b7afbaf-3eae-422c-ad05-e2bef7c06a0f:/wip/ebase:980 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 + 16c2b9cb-492b-4d64-9535-64d4e875048d:/wip/ebase:1030 1b7afbaf-3eae-422c-ad05-e2bef7c06a0f:/wip/ebase:981 a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17930 Modified: trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm =================================================================== --- trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm 2007-07-24 18:07:57 UTC (rev 976) +++ trunk/ebase/lib/Everything/HTTP/Response/Htmlpage.pm 2007-07-24 18:08:52 UTC (rev 977) @@ -5,7 +5,7 @@ use base 'Class::Accessor::Fast'; __PACKAGE__->follow_best_practice; __PACKAGE__->mk_accessors( - qw/http_header http_body request htmlpage theme allowed/); + qw/http_header http_body request htmlpage theme allowed redirect/); use strict; ### because this is called from a Class::Factory object new is not @@ -35,8 +35,9 @@ $self->get_request->get_nodebase->getNode( $self->get_redirect ); $self->get_request->set_node($node); $self->getTheme( $self->get_request ); - die "Incorrect permissions!" unless $htmlpage->check_permissions; + die "Incorrect permissions!" unless $self->check_permissions; + } return $self->set_http_body( $self->get_htmlpage->make_html( $self->get_request ) ); @@ -52,7 +53,7 @@ } -=head2 C<getPageForType> +=head2 C<get_page_for_type> Given a nodetype, get the htmlpages needed to display nodes of this type. This runs up the nodetype inheritance hierarchy until it finds something. @@ -73,7 +74,7 @@ =cut -sub getPageForType { +sub get_page_for_type { my ( $self, $TYPE, $displaytype ) = @_; my %WHEREHASH; my $PAGE; @@ -133,7 +134,7 @@ my $NODE = $E->get_node; my $user = $E->get_user; my $query = $E->get_cgi; - my $permission_needed = $PAGE->get_permissionneeded; + my $permission_needed = $PAGE->get_permissionneeded(); # If the user does not have the needed permission to view this # node through the desired htmlpage, we send them to the permission @@ -141,7 +142,7 @@ #Also check to see if the particular displaytype can be executed by the user - unless ($NODE->hasAccess( $E->get_user, $permission_needed ) + unless ($NODE->hasAccess( $user, $permission_needed ) and $PAGE->hasAccess( $user, "x" ) ) { @@ -186,7 +187,7 @@ # my ($NODE, $displaytype) = @_; my $TYPE; - $TYPE = $DB->getType( $$NODE{type_nodetype} ); + $TYPE = $DB->getType( $NODE->get_type_nodetype ); $displaytype ||= $$VARS{ 'displaypref_' . $$TYPE{title} } if exists $$VARS{ 'displaypref_' . $$TYPE{title} }; $displaytype ||= $$THEME{ 'displaypref_' . $$TYPE{title} } @@ -197,12 +198,10 @@ # First, we try to find the htmlpage for the desired display type, # if one does not exist, we default to using the display page. - $PAGE ||= $this->getPageForType( $TYPE, $displaytype ); - $PAGE ||= $this->getPageForType( $TYPE, 'display' ); + $PAGE ||= $this->get_page_for_type( $TYPE, $displaytype ); - die "can't load a page $displaytype for $$TYPE{title} type" unless $PAGE; + die "Can't load a page $displaytype for $$TYPE{title} type" unless $PAGE; - die "NO PAGE!" unless $PAGE; $this->set_htmlpage($PAGE); } Modified: trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm =================================================================== --- trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm 2007-07-24 18:07:57 UTC (rev 976) +++ trunk/ebase/lib/Everything/HTTP/Response/Test/Htmlpage.pm 2007-07-24 18:08:52 UTC (rev 977) @@ -2,75 +2,189 @@ use Test::More; use Test::MockObject; +use Test::MockObject::Extends; use Scalar::Util 'blessed'; +use Everything::HTTP::Response::Htmlpage; +use Everything::HTTP::Request; use base 'Test::Class'; use strict; use warnings; -my $mock; +sub module_class { + my $self = shift; + my $name = blessed($self); + $name =~ s/Test:://; + return $name; +} -sub module_class -{ - my $self = shift; - my $name = blessed( $self ); - $name =~ s/Test:://; - return $name; +sub startup : Test(startup=>2) { + my $self = shift; + $self->{class} = $self->module_class(); + use_ok( $self->{class} ) || die $self->{class}; + my $mock = Test::MockObject->new; + + $mock->set_always( getNode => $mock ); + $mock->set_true( 'set_theme', 'param', 'get_type_nodetype' ); + $mock->set_always( 'get_user_vars' => {} ); + $mock->set_always( '-get_theme', $mock ); + $mock->set_always( '-get_node', $mock ); + $mock->set_always( '-get_nodebase', $mock ); + $mock->set_always( '-get_cgi', $mock ); + $mock->set_always( 'getType', $mock ); + $mock->{title} = 'a title'; + $self->{mock} = $mock; + isa_ok( $self->{instance} = $self->{class}->new($mock), $self->{class} ); + } -use Everything::HTTP::Response::Htmlpage; -use Everything::HTTP::Request; +sub test_http_response : Test(3) { + my $self = shift; + my $class = $self->{class}; + my $instance = $self->{instance}; + can_ok( $class, 'create_http_header' ); + can_ok( $class, 'create_http_body' ); + can_ok( $class, 'get_mime_type' ); +} -sub startup : Test(startup=>2) { - my $self = shift; - $self->{class} = $self->module_class(); - use_ok($self->{class}) || die $self->{class}; - my $mock = Test::MockObject->new; +sub test_check_permissions : Test(7) { + my $self = shift; + my $class = $self->{class}; + my $instance = $self->{instance}; - $mock->set_always( getNode => $mock ); - $mock->set_true( 'set_theme' ); - $mock->set_always( 'get_theme', $mock ); - $mock->set_always( 'get_node', $mock ); - $mock->set_always( 'get_nodebase', $mock ); - $mock->set_always( 'param', 'display' ); - $mock->set_always( 'get_cgi', $mock ); - $mock->set_always( 'getType', $mock ); - $mock->set_always( 'get_user_vars', { key => 'value' } ); - $mock->{title} = 'a title'; - $self->{mock} = $mock; - isa_ok ($self->{instance} = $self->{class}->new($mock), $self->{class}); + my $mock = $self->{mock}; + $mock->clear; + $instance->set_request($mock); + $mock->set_always( -get_user => $mock ); + $mock->set_always( -hasAccess => 0 ); + $mock->set_always( -get_permissionneeded => 'r' ); + $mock->set_always( -get_system_vars => + { permissionDenied_node => 999, nodeLocked_node => 1001 } ); + ok( !$instance->check_permissions, + '...if no access check permissions returns false.' ); + is( $instance->get_redirect, 999, + '...and redirects to the permission deniend page.' ); + $mock->set_always( -hasAccess => 1 ); + ok( $instance->check_permissions, + '...returns true if the user has the correct permissions.' ); + + ## check node locking + $mock->clear; + $mock->set_always( -get_permissionneeded => 'w' ); + $mock->set_true('lock'); + ok( $instance->check_permissions, + '...access to edit htmlpage if we can obtain node lock.' ); + + $mock->clear; + $mock->set_false('lock'); + ok( !$instance->check_permissions, + '...but no access to edit htmlpage without the node lock.' ); + my ( $method, $args ) = $mock->next_call(2); + is( + "$method@$args", + "param$mock displaytype display", + '...and sets param on cgi to "display".' + ); + is( $instance->get_redirect, 1001, '...and redirects to nodeLocked_node.' ); } +sub test_get_theme : Test(2) { -sub test_http_response : Test(3){ - my $self = shift; - my $class = $self->{class}; - my $instance = $self->{instance}; - can_ok($class, 'create_http_header'); - can_ok($class, 'create_http_body'); - can_ok($class, 'get_mime_type'); + my $self = shift; + my $class = $self->{class}; + my $instance = $self->{instance}; + my $e = $instance->get_request; + can_ok( $class, 'getTheme' ) || return; + my $mock = $self->{mock}; + $mock->set_always( 'get_user_vars', { key => 'value' } ); + $e->set_always( get_system_vars => { one => 'two' } ) + ->set_always( get_db => $mock ); + $mock->set_series( isOfType => 1, 0 ) + ->set_always( 'getVars', { var1 => 'one', var2 => 'two' } ); + ok( $instance->getTheme( $instance->get_request ) ); } +sub test_select_htmlpage : Test(8) { -sub test_get_theme : Test(2) { + my $self = shift; + my $class = $self->{class}; + my $instance = Test::MockObject::Extends->new( $self->{instance} ); - my $self = shift; - my $class = $self->{class}; - my $instance = $self->{instance}; - my $e = $instance->get_request; - can_ok($class, 'getTheme') || return; - my $mock = $self->{mock}; - $e->set_always(get_system_vars => { one => 'two' }) - ->set_always(get_db => $mock); - $mock->set_series(isOfType => 1, 0) - ->set_always('getVars', {var1 => 'one', var2 => 'two'}); - ok($instance->getTheme($instance->get_request)); + my $mock = $self->{mock}; + $mock->clear; + $instance->set_request($mock); + + $mock->set_always( -get_theme => $mock ); + $mock->set_always( -get_user_vars => {} ); + $mock->set_always( -get_cgi => $mock ); + $mock->set_always( param => 'adisplaytype' ); + $mock->set_always( '-get_user_vars', { key => 'value' } ); + + $mock->set_always( getType => $mock ); + $mock->set_always( -get_type_nodetype => 222 ); + $mock->set_always( -get_nodebase => $mock ); + + $instance->set_always( get_page_for_type => $mock ); + + is( $instance->select_htmlpage, $mock, '...should retrieve an htmlpage.' ); + + my ( $method, $args ) = $mock->next_call(); + is( $method . $$args[1], + 'paramdisplaytype', '...gets display type from cgi.' ); + ( $method, $args ) = $mock->next_call(); + is( $method . $$args[1], 'getType222', '...retrieves htmlpage nodetype.' ); + ( $method, $args ) = $instance->next_call(); + is( + "$method@$args", + "get_page_for_type$instance $mock adisplaytype", + '...gets display type calling with display name.' + ); + + $mock->set_always( param => '' ); + + $instance->set_always( get_page_for_type => 'htmlpagenode' ); + + is( $instance->select_htmlpage, 'htmlpagenode', + '...should retrieve a page if param is not set.' ); + ( $method, $args ) = $instance->next_call(); + is( + "$method@$args", + "get_page_for_type$instance $mock display", + '...and gets the "display" displaytype.' + ); + + $mock->set_always( + -get_user_vars => { 'displaypref_a title' => 'varsdisplaytype' } ); + + $instance->select_htmlpage; + ( $method, $args ) = $instance->next_call(); + is( + "$method@$args", + "get_page_for_type$instance $mock varsdisplaytype", + '...and get the display type specified by user vars.' + ); + + $mock->set_always( -get_user_vars => {} ); + $mock->{'displaypref_a title'} = 'themedisplaytype'; + $instance->select_htmlpage; + ( $method, $args ) = $instance->next_call(); + is( + "$method@$args", + "get_page_for_type$instance $mock themedisplaytype", + '...or gets the display type specified by the theme.' + ); + } +sub test_create_http_header : Test(1) { + local $TODO = "create http header should return a valid http header???." + +} + 1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |