testElectronicSignatures.php 10.5 KB
<?php
require_once (KT_DIR . '/tests/test.php');
require_once (KT_DIR . '/ktapi/ktapi.inc.php');

/**
* These are the unit tests for the main KTAPI class
*
*/
class APIElectronicSignaturesTestCase extends KTUnitTestCase {

    /**
    * @var object $ktapi The main ktapi object
    */
    var $ktapi;

    /**
    * @var object $session The KT session object
    */
    var $session;

    /**
     * @var object $root The KT folder object
     */
    var $root;

    /**
     * @var bool $esig_enabled True if electronic signatures for api are enabled | False if not enabled
     */
    var $esig_enabled;

    /**
    * This method sets up the KT session
    *
    */
    public function setUp() {
        $this->ktapi = new KTAPI();
        $this->session = $this->ktapi->start_session('admin', 'admin');
        $this->root = $this->ktapi->get_root_folder();
        $this->assertTrue($this->root instanceof KTAPI_Folder);
        $this->esig_enabled = $this->ktapi->electronic_sig_enabled();
        $this->assertTrue($this->esig_enabled);
    }

    /**
    * This method emds the KT session
    *
    */
    public function tearDown() {
        $this->session->logout();
    }

    /* *** Test webservice functions *** */

    /**
     * Testing folder creation and deletion, add document, get folder contents, folder detail
     * Folder shortcuts and actions
     */
    public function testFolderApiFunctions()
    {
        // Create a folder
        // test without authentication - should fail
        $result1 = $this->ktapi->create_folder(1, 'New test api folder');
        $this->assertEqual($result1['status_code'], 1);

        // test with authentication
        $result2 = $this->ktapi->create_folder(1, 'New test api folder', 'admin', 'admin', 'Testing API');
        $folder_id = $result2['results']['id'];
        $this->assertEqual($result2['status_code'], 0);
        $this->assertTrue($result2['results']['parent_id'] == 1);

        // Create a sub folder
        // test without authentication - should fail
        $result3 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder');
        $this->assertEqual($result3['status_code'], 1);

        // test with authentication
        $result4 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder', 'admin', 'admin', 'Testing API');
        $folder_id2 = $result4['results']['id'];
        $this->assertEqual($result4['status_code'], 0);

        // Add a document
        global $default;
        $dir = $default->uploadDirectory;
        $tempfilename = $this->createRandomFile('some text', $dir);

        // test without authentication - should fail
        $doc = $this->ktapi->add_document($folder_id,  'New API test doc', 'testdoc1.txt', 'Default', $tempfilename);
        $this->assertEqual($doc['status_code'], 1);

        // test with authentication
        $doc = $this->ktapi->add_document($folder_id,  'New API test doc', 'testdoc1.txt', 'Default', $tempfilename,
                                          'admin', 'admin', 'Testing API');
        $this->assertEqual($doc['status_code'], 0);
        $doc_id = $doc['results']['document_id'];
        $this->assertEqual($doc['results']['title'], 'New API test doc');

        // Rename the folder
        // test without authentication - should fail
        $renamed = $this->ktapi->rename_folder($folder_id, 'Renamed test folder');
        $this->assertEqual($renamed['status_code'], 1);

        // test with authentication
        $renamed = $this->ktapi->rename_folder($folder_id, 'Renamed test folder', 'admin', 'admin', 'Testing API');
        $this->assertEqual($renamed['status_code'], 0);

        /**
         * Copy and move appear to fail in other parts of the code, so only going to test failure here
         * 
         * Must be VERY careful here with skipping the valid submissions, 3 failed auth attempts in a row locks out the user!
         */
        // Copy folder
        // test without authentication - should fail
        $copied = $this->ktapi->copy_folder($source_id, $target_id, $reason);
        $this->assertEqual($copied['status_code'], 1);

//        // test with authentication
//        $copied = $this->ktapi->copy_folder($source_id, $target_id, $reason, 'admin', 'admin');
//        echo $copied['status_code']."sd<BR>";
//        $this->assertEqual($copied['status_code'], 0);

        // Move folder
        // test without authentication - should fail
        $moved = $this->ktapi->move_folder($source_id, $target_id, $reason);
        $this->assertEqual($moved['status_code'], 1);

        // before we end up with 3 fails in a row (see note above the first copy attempt,) force a successful auth
        $renamed = $this->ktapi->rename_folder($folder_id, 'A New Name', 'admin', 'admin', 'Testing API');

//        // test with authentication
//        $moved = $this->ktapi->move_folder($source_id, $target_id, $reason, 'admin', 'admin');
//        $this->assertEqual($moved['status_code'], 0);

        // before we end up with 3 fails in a row (see note above the first copy attempt,) force a successful auth
        $renamed = $this->ktapi->rename_folder($folder_id, 'A New Name', 'admin', 'admin', 'Testing API');

        // Clean up - delete the folder
        // test without authentication - should fail
        $deleted = $this->ktapi->delete_folder($folder_id, 'Testing API');
        $this->assertEqual($deleted['status_code'], 1);

        // test with authentication
        $deleted = $this->ktapi->delete_folder($folder_id, 'Testing API', 'admin', 'admin');
        $this->assertEqual($deleted['status_code'], 0);
    }

    /**
     * Testing document get, update, actions, delete, shortcuts and detail
     */
    public function testDocumentApiFunctions()
    {
        // Create a folder
        // test without authentication - should fail
        $result1 = $this->ktapi->create_folder(1, 'New test api folder');
        $this->assertEqual($result1['status_code'], 1);
        
        // test with authentication
        $result2 = $this->ktapi->create_folder(1, 'New test api folder', 'admin', 'admin', 'Testing API');
        $folder_id = $result2['results']['id'];
        $this->assertEqual($result2['status_code'], 0);
        
        // Create a sub folder
        // test without authentication - should fail
        $result3 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder');
        $this->assertEqual($result3['status_code'], 1);
        
        // test with authentication
        $result4 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder', 'admin', 'admin', 'Testing API');
        $folder_id2 = $result4['results']['id'];
        $this->assertEqual($result4['status_code'], 0);

        // Add a document
        global $default;
        $dir = $default->uploadDirectory;
        $tempfilename = $this->createRandomFile('some text', $dir);
        
        // test without authentication - should fail
        $doc = $this->ktapi->add_document($folder_id,  'New API test doc', 'testdoc1.txt', 'Default', $tempfilename);
        $this->assertEqual($doc['status_code'], 1);
        
        // test with authentication
        $doc = $this->ktapi->add_document($folder_id,  'New API test doc', 'testdoc1.txt', 'Default', $tempfilename,
                                          'admin', 'admin', 'Testing API');
        $this->assertEqual($doc['status_code'], 0);
        $doc_id = $doc['results']['document_id'];
        
        // Checkout the document
        // test without authentication - should fail
        $result1 = $this->ktapi->checkout_document($doc_id, 'Testing API', true);
        $this->assertEqual($result1['status_code'], 1);
        
        // test with authentication
        $result2 = $this->ktapi->checkout_document($doc_id, 'Testing API', true, 'admin', 'admin');
        $this->assertEqual($doc['status_code'], 0);
        $this->assertTrue(!empty($result2['results']));

        // Checkin the document
        $dir = $default->uploadDirectory;
        $tempfilename = $this->createRandomFile('some text', $dir);
        // test without authentication - should fail
        $result3 = $this->ktapi->checkin_document($doc_id,  'testdoc1.txt', 'Testing API', $tempfilename, false);
        $this->assertEqual($result3['status_code'], 1);
        
        // test with authentication
        $result4 = $this->ktapi->checkin_document($doc_id,  'testdoc1.txt', 'Testing API', $tempfilename, false, 'admin', 'admin');
        $this->assertEqual($result4['status_code'], 0);
        $this->assertEqual($result4['results']['document_id'], $doc_id);
        
        // Delete the document
        // test without authentication - should fail
        $result5 = $this->ktapi->delete_document($doc_id, 'Testing API');
        $this->assertEqual($result5['status_code'], 1);
        
        // test with authentication
        $result6 = $this->ktapi->delete_document($doc_id, 'Testing API', 'admin', 'admin', true);
        $this->assertEqual($result6['status_code'], 0);

        // Clean up - delete the folder
        // test without authentication - should fail
        $result7 = $this->ktapi->delete_folder($folder_id, 'Testing API');
        $this->assertEqual($result7['status_code'], 1);
        
        $result8 = $this->ktapi->delete_folder($folder_id, 'Testing API', 'admin', 'admin');
        $this->assertEqual($result8['status_code'], 0);
    }

    /**
     * Helper function to create a document
     */
    function createDocument($title, $filename, $folder = null)
    {
        if(is_null($folder)){
            $folder = $this->root;
        }

        // Create a new document
        $randomFile = $this->createRandomFile();
        $this->assertTrue(is_file($randomFile));

        if ($this->esig_enabled)
        {
            $document = $folder->add_document($title, $filename, 'Default', $randomFile, 'admin', 'admin', 'Testing API');
        }
        else
        {
            $document = $folder->add_document($title, $filename, 'Default', $randomFile);
        }
        $this->assertNotError($document);

        @unlink($randomFile);
        if(PEAR::isError($document)) return false;

        return $document;
    }

    /**
     * Helper function to delete docs
     */
    function deleteDocument($document)
    {
        $document->delete('Testing API');
        $document->expunge();
    }

    function createRandomFile($content = 'this is some text', $uploadDir = null) {
        if(is_null($uploadDir)){
           $uploadDir = dirname(__FILE__);
        }
        $temp = tempnam($uploadDir, 'myfile');
        $fp = fopen($temp, 'wt');
        fwrite($fp, $content);
        fclose($fp);
        return $temp;
    }
}
?>