<html>
<head>
<title>DiGIR request test suite</title>
</head>
<body>
<h1>Testing DiGIR Request Parser</h1>
<pre>$Id: test_request.php,v 1.1 2003/04/23 03:53:52 vieglais Exp $</pre>
<p>This script is testing the functionality of the DiGIR request parser.
All tests listed here should pass.</p>
<hr />
<?php
//barf if the script is not called properly
if ((isset($_SERVER['SCRIPT_NAME']) && preg_match("/.\/test_request.php/i",$_SERVER['SCRIPT_NAME'])) ||
    (isset($_SERVER['argv'][0]) && strcasecmp($_SERVER['argv'][0],'test_request.php') === 0))
{
?>
<p>You should not call this script directly, but rather by using
<a href="../DiGIR.php?operation=test">DiGIR.php?operation=test</a>.</p>
<?php
    die();
}

$tmp = @include_once('PHPUnit.php');
if (!$tmp)
{
        //error - unable to load phpUnit.php!
        $msg = "Fatal Error- unable to load phpUnit.php.\n".
            "There is a problem with the installation that prevents loading the necessary libraries";
        if (headers_sent())
        {
                echo "<error>".$msg."</error>";
        }
        else
        {
                echo "<pre>".$msg;
        }
        flush();
        phpinfo();
        die();
}


    class requestTest extends PHPUnit_TestCase 
    {
        var $loglevels;
        
        function requestTest($name) 
        {
            $this->PHPUnit_TestCase($name);
        }

        function setUp() 
        {
            $this->loglevels = array(PEAR_LOG_EMERG,
                    PEAR_LOG_ALERT,PEAR_LOG_CRIT,PEAR_LOG_ERR,
                    PEAR_LOG_WARNING,PEAR_LOG_NOTICE,PEAR_LOG_INFO,
                    PEAR_LOG_DEBUG);
        }
        
/*
    $params['operation'] = 'metadata';
    $params['filter'] = '';
    $params['resource'] = '';
    $params['startrec'] = 0;
    //$params['maxrecs'] = DIGIR_DEFAULT_PAGESIZE;
    //this value is set within get content to the default for the resource
    //if it is less than zero (ie, unchanged by the request)
    $params['maxrecs'] = -1;
    $params['clientkey'] = '';
    $params['recordstruct'] = '';;
    $params['sortstruct'] = '';
    $params['countrecs'] = false;
    $params['getrecords'] = true;	
    $params['sort'] = array(NULL,'ASC');
    $params['clientid'] = $_SERVER['REMOTE_ADDR'];
    $params['requestnamespaces'] = array();
    $params['cache'] = DIGIR_USE_CACHE;
    $params['resourceconfigfile'] = null;*/
        function test_loadParamsDiGIRRequest_inventory_1()
        {
            $xmlreq = '<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by Stanley D Blum (California Academy of Sciences) -->
<!-- $Id: test_request.php,v 1.1 2003/04/23 03:53:52 vieglais Exp $ -->
<request xmlns="http://digir.net/schema/protocol/2003/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
	xmlns:darwin="http://digir.net/schema/conceptual/darwin/2003/1.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://digir.net/schema/protocol/2003/1.0 http://digir.sourceforge.net/schema/protocol/2003/1.0/digir.xsd http://digir.net/schema/conceptual/darwin/2003/1.0 http://digir.sourceforge.net/schema/conceptual/darwin/2003/1.0/darwin2.xsd">
	<header>
		<version>1.0</version>
		<sendTime>2003-03-09T19:14:58-05:00</sendTime>
		<source>216.91.87.102</source>
		<destination resource="test">http://velo.dyndns.info:80/phpdev/DiGIR/DiGIR.php</destination>
		<type>inventory</type>
	</header>
	<inventory>
		<filter><equals><darwin:Genus>manis</darwin:Genus></equals></filter>
		<darwin:Species/>
	</inventory>
</request>';
            $params = array();
            intializeParams($params);
            loadParamsDiGIRRequest($xmlreq,$params);
            $this->assertEquals('http://www.w3.org/2001/XMLSchema-instance',$params['requestnamespaces']['xsi'],'xsi namespace');
            $this->assertEquals('http://digir.net/schema/conceptual/darwin/2003/1.0',$params['requestnamespaces']['darwin'],'conceptual schema namespace');
            $this->assertEquals('http://digir.net/schema/protocol/2003/1.0',$params['requestnamespaces'][0],'default namespace');
            $this->assertEquals('inventory',$params['operation'],'operation');
            $this->assertEquals('test',$params['resource'],'resource name');
            $this->assertEquals('',$params['clientkey'],'clientkey');
            $this->assertEquals(false,$params['countrecs'],'countrecs');
            $this->assertEquals(true,$params['getrecords'],'getrecords');
            $this->assertEquals(0,$params['startrec'],'startrec');
            $this->assertEquals(-1,$params['maxrecs'],'maxrecs');
            $test = preg_replace("/(\r|\n|\s|\(\?\#.*\))/", "", $params['filter']);            
            $this->assertEquals('<filter><equals><darwin:Genus>manis</darwin:Genus></equals></filter>',$test,'filter');
            $this->assertEquals('darwin:Species',$params['recordstruct'],'inventory concept');
        }
        
        function test_loadParamsDiGIRRequest_inventory_2()
        {
            $xmlreq = '<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by Stanley D Blum (California Academy of Sciences) -->
<!-- $Id: test_request.php,v 1.1 2003/04/23 03:53:52 vieglais Exp $ -->
<request xmlns="http://digir.net/schema/protocol/2003/1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
	xmlns:darwin="http://digir.net/schema/conceptual/darwin/2003/1.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://digir.net/schema/protocol/2003/1.0 http://digir.sourceforge.net/schema/protocol/2003/1.0/digir.xsd http://digir.net/schema/conceptual/darwin/2003/1.0 http://digir.sourceforge.net/schema/conceptual/darwin/2003/1.0/darwin2.xsd">
	<header clientKey="testkey">
		<version>1.0</version>
		<sendTime>2003-03-09T19:14:58-05:00</sendTime>
		<source>216.91.87.102</source>
		<destination resource="test">http://velo.dyndns.info:80/phpdev/DiGIR/DiGIR.php</destination>
		<type>inventory</type>
	</header>
	<inventory>
		<darwin:Species/>
		<count>TRUE</count>
	</inventory>
</request>';
            $params = array();
            intializeParams($params);
            loadParamsDiGIRRequest($xmlreq,$params);
            $this->assertEquals('http://www.w3.org/2001/XMLSchema-instance',$params['requestnamespaces']['xsi'],'xsi schema namespace');
            $this->assertEquals('http://digir.net/schema/conceptual/darwin/2003/1.0',$params['requestnamespaces']['darwin'],'conceptual schema namespace');
            $this->assertEquals('http://digir.net/schema/protocol/2003/1.0',$params['requestnamespaces'][0],'default namespace');
            $this->assertEquals('inventory',$params['operation'],'operation');
            $this->assertEquals('test',$params['resource'],'resource name');
            $this->assertEquals('testkey',$params['clientkey'],'client key');
            $this->assertEquals(true,$params['countrecs'],'countrecs');
            $this->assertEquals(true,$params['getrecords'],'getrecords');
            $this->assertEquals(0,$params['startrec'],'startrec');
            $this->assertEquals(-1,$params['maxrecs'],'maxrecs');
            $test = preg_replace("/(\r|\n|\s|\(\?\#.*\))/", "", $params['filter']);            
            $this->assertEquals('',$test,'filter');
            $this->assertEquals('darwin:Species',$params['recordstruct'],'inventory concept');
        }
        
        
        function test_loadParamsDiGIRRequest_metadata_1()
        {
            $xmlreq = '<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by Stanley D Blum (California Academy of Sciences) -->
<!-- $Id: test_request.php,v 1.1 2003/04/23 03:53:52 vieglais Exp $ -->
<request xmlns="http://digir.net/schema/protocol/2003/1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://digir.net/schema/protocol/2003/1.0 http://digir.sourceforge.net/schema/protocol/2003/1.0/digir.xsd">
	<header>
		<version>1.0</version>
		<sendTime>2003-03-09T19:30:04-05:00</sendTime>
		<source>216.91.87.102</source>
		<destination>http://velo.dyndns.info:80/phpdev/DiGIR/DiGIR.php</destination>
		<type>metadata</type>
	</header>
</request>';
            $params = array();
            intializeParams($params);
            loadParamsDiGIRRequest($xmlreq,$params);
            $this->assertEquals('http://www.w3.org/2001/XMLSchema-instance',$params['requestnamespaces']['xsi'],'xsi namespace');
            $this->assertEquals('http://digir.net/schema/protocol/2003/1.0',$params['requestnamespaces'][0],'protocol namespace');
            $this->assertEquals('metadata',$params['operation'],'operation name');
            $this->assertEquals('',$params['resource'],'resource name');
            $this->assertEquals('',$params['clientkey'],'client key');
            $this->assertEquals(false,$params['countrecs'],'countrecs');
            $this->assertEquals(true,$params['getrecords'],'getrecords');
            $this->assertEquals(0,$params['startrec'],'startrec');
            $this->assertEquals(-1,$params['maxrecs'],'maxrecs');
        }
        
        function test_loadParamsDiGIRRequest_metadata_2()
        {
            $xmlreq = '<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.1 U (http://www.xmlspy.com) by Stanley D Blum (California Academy of Sciences) -->
<!-- $Id: test_request.php,v 1.1 2003/04/23 03:53:52 vieglais Exp $ -->
<request xmlns="http://digir.net/schema/protocol/2003/1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://digir.net/schema/protocol/2003/1.0 http://digir.sourceforge.net/schema/protocol/2003/1.0/digir.xsd">
	<header clientKey="testkey">
		<version>1.0</version>
		<sendTime>2003-03-09T19:30:04-05:00</sendTime>
		<source>216.91.87.102</source>
		<destination>http://velo.dyndns.info:80/phpdev/DiGIR/DiGIR.php</destination>
		<type>metadata</type>
	</header>
</request>';
            $params = array();
            intializeParams($params);
            loadParamsDiGIRRequest($xmlreq,$params);
            $this->assertEquals('http://www.w3.org/2001/XMLSchema-instance',$params['requestnamespaces']['xsi'],'xsi namespace');
            $this->assertEquals('http://digir.net/schema/protocol/2003/1.0',$params['requestnamespaces'][0],'protocol namespace');
            $this->assertEquals('metadata',$params['operation'],'operation name');
            $this->assertEquals('',$params['resource'],'resource name');
            $this->assertEquals('testkey',$params['clientkey'],'client key');
            $this->assertEquals(false,$params['countrecs'],'countrecs');
            $this->assertEquals(true,$params['getrecords'],'getrecords');
            $this->assertEquals(0,$params['startrec'],'startrec');
            $this->assertEquals(-1,$params['maxrecs'],'maxrecs');
        }
    }

    $suite = new PHPUnit_TestSuite('requestTest');
    $result = PHPUnit::run($suite);
    echo $result->toHTML();
?>
<hr />
</body>
</html>