[LON-CAPA-cvs] cvs: modules /jerf/tests lonnavmapsTest.pm

bowersj2 lon-capa-cvs@mail.lon-capa.org
Thu, 17 Jul 2003 20:34:19 -0000


bowersj2		Thu Jul 17 16:34:19 2003 EDT

  Added files:                 
    /modules/jerf/tests	lonnavmapsTest.pm 
  Log:
  lonnavmaps testing; this might have detected the problems we had with 
  instructors in sections having allowed problems.
  
  

Index: modules/jerf/tests/lonnavmapsTest.pm
+++ modules/jerf/tests/lonnavmapsTest.pm
# The LearningOnline Network with CAPA
# navmaps testing code
#
# $Id: lonnavmapsTest.pm,v 1.1 2003/07/17 20:34:19 bowersj2 Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
# (Testing: navmaps testing code.

# This file tests the navmaps code to make sure it is working
# correctly.

package lonnavmapsTest;

use lib '.';
use lib '/home/httpd/lib/perl/';
use ApacheRequest;

use base qw(Test::Unit::TestCase);
use strict;
use Apache::lonnavmaps;
use Data::Dumper;
use Utils;
use Data;

sub new {
    my $self = shift()->SUPER::new(@_);
    return $self;
}

sub set_up {
    my $self = shift;
}

sub tear_down {
    my $self = shift;
}

sub test_navmaps_basic_works {
    my $self = shift;

    # This tests the navmaps at the most basic level: in a course
    # with one map, do the resources come out in the right order?
    # And do they work for all roles?

    print "Doing basic navmaps test...\n";

    # First, publish the resources and the map
    my $author = Utils::User->new($Data::testDomain,
                                  $Data::authorName);
    my $request = ApacheRequest->new({user=>$author});
    $author->login($request);
    $author->selectRole($request, 'au');

    print "Publishing:";
    $author->initializeResourceSpace($request);
    for (@Data::problems) {
        print " $_";
        $author->copyResource($_);
        $author->publishResource($request, $_);
    }
    print " all.problems.sequence\n";
    $author->copyResource('all.problems.sequence');
    $author->publishResource($request, 'all.problems.sequence');

    # Now we've got the resources, create the course
    my $dc = Utils::User->new($Data::testDomain, $Data::dcName);
    $self->assert($dc->{exists}, "Can't test navmaps because the " .
                  "domain coordinator needs to be set up.");
    my $dcR = ApacheRequest->new({user=>$author});
    $dc->login($dcR);
    $dc->selectRole($dcR, 'dc');

    # Create a course, using old-style top-level map and no
    # syllabus to start with
    my $course = Utils::Course->new($dcR, 'Navmap test course, delete on sight',
                                    "/res/$Data::testDomain/" .
                                    "$Data::authorName/test/all.problems.sequence",
                                    $Data::dcName,
                                    { firstres => 'blank' });

    # Make sure the course was created correctly before we get too far
    $self->assert($course->{courseId});

    # Create our guinea pig
    my $tester = Utils::User->new($Data::testDomain, $Data::testName, $Data::testPassword);
    $tester->delete();
    $tester->create($dcR);
    my $testerR = ApacheRequest->new({user => $tester});
    
    # We've now created a course, given it the map we want, and
    # created a user for it. Now we need to walk through the roles,
    # log the test user in, and confirm that the navmaps walks through
    # the course map correctly in this simple case.
    print "\nTesting basic navmap functionality...\n";
    for my $roleData (@Data::allCourseRoles) {
	(my $role, my $section) = @$roleData;
	if ($section) {
	    print "Testing navmaps for role '$role', section '$section'... ";
	} else {
	    print "Testing navmaps for role '$role'... ";
	}
        $self->assert($course->addRole($Data::testName, $role, $section),
                      "Couldn't assign $role for $Data::testName");
        $tester->login($testerR);
        $tester->selectRole($testerR, $role, $course->{courseId});
        
        my $navmap = Apache::lonnavmaps::navmap->new(
                           $testerR->{env}->{'request.course.fn'}.'.db',
                           $testerR->{env}->{'request.course.fn'}.'_parms.db',
                                                     0, 0);
        $self->assert(defined($navmap), "Navmap object creation failed.");
	$navmap->init();
        my @resources = $testerR->execInEnv(sub { return
					    $navmap->retrieveResources(); });
	$self->assert(scalar(@resources), "Couldn't get any resources, "
		      ."that's wrong.");
        for (my $i = 0; $i < scalar(@resources); $i ++) {
            my $targetFileName = $Data::allProblemsSequence[$i];
            my $actualFileName = $resources[$i]->src();
            $self->assert($actualFileName =~ m|$targetFileName$|,
                          "Looking for $targetFileName, found " .
                          "$actualFileName in all.problems.sequence "
                          . "for $role.");
        }
	print "success.\n";
	$navmap->untieHashes();
        
        $course->revokeRole($Data::testName, $role, $section, 1);
    }
    $course->delete();
}

1;