testElectronicSignatures.php 37.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
<?php
require_once (KT_DIR . '/tests/test.php');
require_once (KT_DIR . '/ktapi/ktapi.inc.php');

// username and password for authentication
// must be set correctly for all of the tests to pass in all circumstances
define (KT_TEST_USER, 'admin');
define (KT_TEST_PASS, 'admin');

// NOTE these tests may fail if the system isn't clean - i.e. if there are folders and documents
// TODO change the assert checks to look for the esignature specific messages?

/**
 * Unit tests specifically for testing the KTAPI functionality with API Electronic Signatures enabled
 * Tests are run for both failure and success, unlike the regular KTAPI tests which only look for a
 * success response on the functions requiring signatures
 *
 * IF API Electronic Signatures are NOT enabled, functions should not try to test
 * add these two lines to the beginning of any new test functions to ensure this:
 * 
 * // if not enabled, do not run remaining tests
 * if (!$this->esig_enabled) return null;
 */
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(KT_TEST_USER, KT_TEST_PASS);
        $this->root = $this->ktapi->get_root_folder();
        $this->assertTrue($this->root instanceof KTAPI_Folder);
        $this->esig_enabled = $this->ktapi->electronic_sig_enabled();

        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;
        
        $this->assertTrue($this->esig_enabled);

        // force reset of lockout status just in case :)
        unset($_SESSION['esignature_attempts']);
        unset($_SESSION['esignature_lock']);
    }

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

    /**
     * Test lockout on multiple failed authentications
     */
    public function testLockout()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;

        // doesn't matter what we call here, just need 3 failed attempts
        // NOTE the number of failed attempts must be changed if there is
        //      a change in the electronic signature definition of the
        //      maximum number of attempts before lockout

        $result = $this->ktapi->create_folder(1, 'New test api folder');
        $this->assertEqual($result['status_code'], 1);
        $result = $this->ktapi->create_folder(1, 'New test api folder');
        $this->assertEqual($result['status_code'], 1);
        $result = $this->ktapi->create_folder(1, 'New test api folder');
        $this->assertEqual($result['status_code'], 1);

        // fourth attempt to check lockout message returned
        $result = $this->ktapi->create_folder(1, 'New test api folder');
        $this->assertEqual($result['status_code'], 1);   
        $eSignature = new ESignature('api');
        $this->assertTrue($result['message'] == $eSignature->getLockMsg());

        // force reset of the lockout so that remaining tests can run :)
        unset($_SESSION['esignature_attempts']);
        unset($_SESSION['esignature_lock']);
    }

    /**
     * Testing folder creation and deletion, add document, get folder contents, folder detail
     * Folder shortcuts and actions
     */
    public function testFolderApiFunctions()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;
        
        // 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', KT_TEST_USER, KT_TEST_PASS, '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', KT_TEST_USER, KT_TEST_PASS, '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,
                                          KT_TEST_USER, KT_TEST_PASS, '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', KT_TEST_USER, KT_TEST_PASS, '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, KT_TEST_USER, KT_TEST_PASS);
//        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', KT_TEST_USER, KT_TEST_PASS, 'Testing API');

//        // test with authentication
//        $moved = $this->ktapi->move_folder($source_id, $target_id, $reason, KT_TEST_USER, KT_TEST_PASS);
//        $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', KT_TEST_USER, KT_TEST_PASS, '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', KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($deleted['status_code'], 0);
    }

    /**
     * Testing document get, update, actions, delete, shortcuts and detail
     */
    public function testDocumentApiFunctions()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;
        
        // 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', KT_TEST_USER, KT_TEST_PASS, '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', KT_TEST_USER, KT_TEST_PASS, '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,
                                          KT_TEST_USER, KT_TEST_PASS, '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, KT_TEST_USER, KT_TEST_PASS);
        $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, KT_TEST_USER, KT_TEST_PASS);
        $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', KT_TEST_USER, KT_TEST_PASS, 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', KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($result8['status_code'], 0);
    }

    /**
     * Test role allocation on folders
     */
    function testAllocatingMembersToRoles()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;

        $folder = $this->ktapi->get_folder_by_name('test123');
        if(!$folder instanceof KTAPI_Folder){
            $folder = $this->root->add_folder('test123');
        }
        $folder_id = $folder->get_folderid();

        $allocation = $this->ktapi->get_role_allocation_for_folder($folder_id);
        $this->assertEqual($allocation['status_code'], 0);
        $this->assertTrue(empty($allocation['results']));

        // add a user to a role
        $role_id = 2; // Publisher
        $user_id = 1; // Admin
        // test without authentication - should fail
        $result = $this->ktapi->add_user_to_role_on_folder($folder_id, $role_id, $user_id);
        $this->assertEqual($result['status_code'], 1);
        // test with authentication
        $result = $this->ktapi->add_user_to_role_on_folder($folder_id, $role_id, $user_id, KT_TEST_USER, KT_TEST_PASS, 'Testing API');
        $this->assertEqual($result['status_code'], 0);

        $allocation = $this->ktapi->get_role_allocation_for_folder($folder_id);
        $this->assertEqual($allocation['status_code'], 0);
        $this->assertTrue(isset($allocation['results']['Publisher']));
        $this->assertEqual($allocation['results']['Publisher']['user'][1], 'Administrator');

        // test check on members in the role
        $check = $this->ktapi->is_member_in_role_on_folder($folder_id, $role_id, $user_id, 'user');
        $this->assertEqual($check['status_code'], 0);
        $this->assertEqual($check['results'], 'YES');

        // remove user from a role
        // test without authentication - should fail
        $result = $this->ktapi->remove_user_from_role_on_folder($folder_id, $role_id, $user_id);
        $this->assertEqual($result['status_code'], 1);
        // test with authentication
        $result = $this->ktapi->remove_user_from_role_on_folder($folder_id, $role_id, $user_id, KT_TEST_USER, KT_TEST_PASS, 'Testing API');
        $this->assertEqual($result['status_code'], 0);

        $allocation = $this->ktapi->get_role_allocation_for_folder($folder_id);
        $this->assertEqual($allocation['status_code'], 0);
        $this->assertFalse(isset($allocation['results']['Publisher']));

        // clean up
        $folder->delete('Testing API');
    }

    /**
     * Test inherit and override role allocation and remove all allocations
     */
    function testRoleAllocationInheritance()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;

        $folder = $this->ktapi->get_folder_by_name('test123');
        if(!$folder instanceof KTAPI_Folder){
            $folder = $this->root->add_folder('test123');
        }
        $folder_id = $folder->get_folderid();

        $allocation = $this->ktapi->get_role_allocation_for_folder($folder_id);
        $this->assertEqual($allocation['status_code'], 0);

        // Override
        // test without authentication - should fail
        $result = $this->ktapi->override_role_allocation_on_folder($folder_id);
        $this->assertEqual($result['status_code'], 1);
        // test with authentication
        $result = $this->ktapi->override_role_allocation_on_folder($folder_id, KT_TEST_USER, KT_TEST_PASS, 'Testing API');
        $this->assertEqual($result['status_code'], 0);

        $role_id = 2; // Publisher
        $user_id = 1; // Admin
        $group_id = 1; // System Administrators
        $members = array('users' => array($user_id), 'groups' => array($group_id));

        // test without authentication - should fail
        $result = $this->ktapi->add_members_to_role_on_folder($folder_id, $role_id, $members);
        $this->assertEqual($result['status_code'], 1);
        // test with authentication
        $result = $this->ktapi->add_members_to_role_on_folder($folder_id, $role_id, $members, KT_TEST_USER, KT_TEST_PASS, 'Testing API');
        $this->assertEqual($result['status_code'], 0);

        $check = $this->ktapi->is_member_in_role_on_folder($folder_id, $role_id, $user_id, 'user');
        $this->assertEqual($check['status_code'], 0);
        $this->assertEqual($check['results'], 'YES');

        // Remove all
        // test without authentication - should fail
        $result = $this->ktapi->remove_all_role_allocation_from_folder($folder_id, $role_id);
        $this->assertEqual($result['status_code'], 1);
        // test with authentication
        $result = $this->ktapi->remove_all_role_allocation_from_folder($folder_id, $role_id, KT_TEST_USER, KT_TEST_PASS, 'Testing API');
        $this->assertEqual($result['status_code'], 0);

        $check = $this->ktapi->is_member_in_role_on_folder($folder_id, $role_id, $group_id, 'group');
        $this->assertEqual($check['status_code'], 0);
        $this->assertEqual($check['results'], 'NO');

        // Inherit
        // test without authentication - should fail
        $result = $this->ktapi->inherit_role_allocation_on_folder($folder_id);
        $this->assertEqual($result['status_code'], 1);
        // test with authentication
        $result = $this->ktapi->inherit_role_allocation_on_folder($folder_id, KT_TEST_USER, KT_TEST_PASS, 'Testing API');
        $this->assertEqual($result['status_code'], 0);

        // clean up
        $folder->delete('Testing API');
    }

    /**
     * Testing the bulk actions - copy, move, delete
     */
    public function testApiBulkCopyMoveDelete()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;

        // Create folder and documents
        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
        $folder1 = $this->root->add_folder("New test folder");
        $this->assertNotError($newFolder);
        if(PEAR::isError($newFolder)) return;

        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);

        $target_folder = $this->root->add_folder("New target folder");
        $this->assertNotError($target_folder);
        if(PEAR::isError($target_folder)) return;
        $target_folder_id = $target_folder->get_folderid();

        $aItems = array();
        $aItems['documents'][] = $doc1->get_documentid();
        $aItems['documents'][] = $doc2->get_documentid();
        $aItems['folders'][] = $folder1->get_folderid();

        // Call bulk action - copy
        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('copy', $aItems, 'Testing API', $target_folder_id);
        $this->assertEqual($response['status_code'], 1);
        // test with authentication
        $response = $this->ktapi->performBulkAction('copy', $aItems, 'Testing API', $target_folder_id, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
        $this->assertTrue(empty($response['results']));

        // Test move action - delete and recreate target folder
        $target_folder->delete('Testing API');

        $target_folder = $this->root->add_folder("New target folder");
        $this->assertNotError($target_folder);
        if(PEAR::isError($target_folder)) return;
        $target_folder_id = $target_folder->get_folderid();

        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('move', $aItems, 'Testing API', $target_folder_id);
        $this->assertEqual($response['status_code'], 1);
        // test with authentication
        $response = $this->ktapi->performBulkAction('move', $aItems, 'Testing API', $target_folder_id, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
        $this->assertTrue(empty($response['results']));

        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('delete', $aItems, 'Testing API');
        $this->assertEqual($response['status_code'], 1);
        // test with authentication
        $response = $this->ktapi->performBulkAction('delete', $aItems, 'Testing API', null, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
        $this->assertTrue(empty($response['results']));

        // Delete and expunge documents and folder
        $target_folder->delete('Testing API');
    }

    /**
     * Testing the bulk actions - checkout and cancel check out
     */
    public function testApiBulkCheckout()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;

        // Create folder and documents
        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
        $folder1 = $this->root->add_folder("New test folder");
        $this->assertNotError($newFolder);
        if(PEAR::isError($newFolder)) return;

        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);

        $doc1_id = $doc1->get_documentid();
        $doc2_id = $doc2->get_documentid();

        $aItems = array();
        $aItems['documents'][] = $doc1_id;
        $aItems['documents'][] = $doc2_id;
        $aItems['folders'][] = $folder1->get_folderid();

        // Call bulk action - checkout
        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('checkout', $aItems, 'Testing API', null);
        $this->assertEqual($response['status_code'], 1);
        // test with authentication
        $response = $this->ktapi->performBulkAction('checkout', $aItems, 'Testing API', null, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
        $this->assertTrue(empty($response['results']));

        // update document object
        $doc1 = $this->ktapi->get_document_by_id($doc1_id);
        $this->assertTrue($doc1->is_checked_out());

        // cancel the checkout
        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('undo_checkout', $aItems, 'Testing API', null);
        $this->assertEqual($response['status_code'], 1);
        // test with authentication
        $response = $this->ktapi->performBulkAction('undo_checkout', $aItems, 'Testing API', null, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
        $this->assertTrue(empty($response['results']));

        // delete items
        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('delete', $aItems, 'Testing API');
        $this->assertEqual($response['status_code'], 1);
        // test with authentication
        $response = $this->ktapi->performBulkAction('delete', $aItems, 'Testing API', null, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
    }

    /**
     * Testing the bulk actions - checkout and cancel check out
     */
    public function testApiBulkImmute()
    {
        // if not enabled, do not run remaining tests
        if (!$this->esig_enabled) return null;

        // Create folder and documents
        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
        $folder1 = $this->root->add_folder("New test folder");
        $this->assertNotError($newFolder);
        if(PEAR::isError($newFolder)) return;

        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);

        $doc1_id = $doc1->get_documentid();
        $doc2_id = $doc2->get_documentid();

        $aItems = array();
        $aItems['documents'][] = $doc1_id;
        $aItems['documents'][] = $doc2_id;
        $aItems['folders'][] = $folder1->get_folderid();

        // Call bulk action - checkout
        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('immute', $aItems, 'Testing API');
        $this->assertEqual($response['status_code'], 1);
        $doc1 = $this->ktapi->get_document_by_id($doc1_id);
        $this->assertFalse($doc1->isImmutable());
        // test with authentication
        $response = $this->ktapi->performBulkAction('immute', $aItems, 'Testing API', null, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
        $this->assertTrue(empty($response['results']));

        // update document object
        $doc1 = $this->ktapi->get_document_by_id($doc1_id);
        $this->assertTrue($doc1->isImmutable());

        // remove immutability for deletion
        $doc1->unimmute();
        $doc2->unimmute();
        $doc4->unimmute();

        // delete items
        // test without authentication - should fail
        $response = $this->ktapi->performBulkAction('delete', $aItems, 'Testing API');
        $this->assertEqual($response['status_code'], 1);
        // test with authentication
        $response = $this->ktapi->performBulkAction('delete', $aItems, 'Testing API', null, KT_TEST_USER, KT_TEST_PASS);
        $this->assertEqual($response['status_code'], 0);
    }

//    /* *** Test Bulk actions class *** */
//
//    /**
//     * Test the bulk copy functionality
//     */
//    function testCopy()
//    {
//        // Create documents
//        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
//        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
//        $doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
//        $folder1 = $this->root->add_folder("New copy folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
//
//        // Add a folder
//        $targetFolder = $this->root->add_folder("New target folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $aItems = array($doc1, $doc2, $doc3, $folder1);
//
//        // Copy documents and folder into target folder
//        $res = $this->bulk->copy($aItems, $targetFolder, 'Testing bulk copy');
//
//        $this->assertTrue(empty($res));
//
//        // Check the documents copied
//        $listDocs = $targetFolder->get_listing(1, 'D');
//        $this->assertTrue(count($listDocs) == 3);
//
//        // Check the folder copied
//        $listFolders = $targetFolder->get_listing(1, 'F');
//        $this->assertTrue(count($listFolders) == 1);
//
//        // Check the document contained in the folder copied
//        $newFolderId = $listFolders[0]['id'];
//        $newFolder = $this->ktapi->get_folder_by_id($newFolderId);
//        $listSubDocs = $newFolder->get_listing(1, 'D');
//        $this->assertTrue(count($listSubDocs) == 1);
//
//        // Delete and expunge documents and folder
//        $this->deleteDocument($doc1);
//        $this->deleteDocument($doc2);
//        $this->deleteDocument($doc3);
//        $this->deleteDocument($doc4);
//        $targetFolder->delete('Testing bulk copy');
//        $folder1->delete('Testing bulk copy');
//    }
//
//    /**
//     * Test the bulk move functionality
//     */
//    function testMove()
//    {
//        // Create documents
//        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
//        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
//        $doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
//        $folder1 = $this->root->add_folder("New move folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
//
//        // Add a folder
//        $targetFolder = $this->root->add_folder("New target folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $aItems = array($doc1, $doc2, $doc3, $folder1);
//
//        // Copy documents and folder into target folder
//        $res = $this->bulk->move($aItems, $targetFolder, 'Testing bulk move');
//
//        $this->assertTrue(empty($res));
//
//        // Check document has been moved not copied
//        $detail = $doc1->get_detail();
//        $this->assertFalse($detail['folder_id'] == $this->root->get_folderid());
//        $this->assertTrue($detail['folder_id'] == $targetFolder->get_folderid());
//
//        // Check folder has been moved not copied
//        $this->assertFalse($folder1->get_parent_folder_id() == $this->root->get_folderid());
//        $this->assertTrue($folder1->get_parent_folder_id() == $targetFolder->get_folderid());
//
//        // Check the documents copied
//        $listDocs = $targetFolder->get_listing(1, 'D');
//        $this->assertTrue(count($listDocs) == 3);
//
//        // Check the folder copied
//        $listFolders = $targetFolder->get_listing(1, 'F');
//        $this->assertTrue(count($listFolders) == 1);
//
//        // Check the document contained in the folder copied
//        $newFolderId = $listFolders[0]['id'];
//        $newFolder = $this->ktapi->get_folder_by_id($newFolderId);
//        $listSubDocs = $newFolder->get_listing(1, 'D');
//        $this->assertTrue(count($listSubDocs) == 1);
//
//        // Delete and expunge documents and folder
//        $this->deleteDocument($doc1);
//        $this->deleteDocument($doc2);
//        $this->deleteDocument($doc3);
//        $this->deleteDocument($doc4);
//        $targetFolder->delete('Testing bulk copy');
//        $folder1->delete('Testing bulk copy');
//    }
//
//    /**
//     * Test the bulk checkout and cancel checkout functionality
//     */
//    function testCheckout()
//    {
//        // Create documents
//        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
//        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
//        $doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
//        $folder1 = $this->root->add_folder("New test folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
//
//        $aItems = array($doc1, $doc2, $doc3, $folder1);
//
//        // Checkout documents and folder
//        $res = $this->bulk->checkout($aItems, 'Testing bulk checkout');
//
//        $this->assertTrue(empty($res));
//
//        $this->assertTrue($doc1->is_checked_out());
//        $this->assertTrue($doc2->is_checked_out());
//        $this->assertTrue($doc3->is_checked_out());
//
//        // refresh the doc4 document object to reflect changes
//        $doc4 = KTAPI_Document::get($this->ktapi, $doc4->get_documentid());
//        $this->assertTrue($doc4->is_checked_out());
//
//        $res = $this->bulk->undo_checkout($aItems, 'Testing bulk undo / cancel checkout');
//
//        $this->assertTrue(empty($res));
//
//        $this->assertFalse($doc1->is_checked_out());
//        $this->assertFalse($doc2->is_checked_out());
//        $this->assertFalse($doc3->is_checked_out());
//
//        // refresh the doc4 document object to reflect changes
//        $doc4 = KTAPI_Document::get($this->ktapi, $doc4->get_documentid());
//        $this->assertFalse($doc4->is_checked_out());
//
//        // Delete and expunge documents and folder
//        $this->deleteDocument($doc1);
//        $this->deleteDocument($doc2);
//        $this->deleteDocument($doc3);
//        $this->deleteDocument($doc4);
//        $folder1->delete('Testing bulk checkout');
//    }
//
//    /**
//     * Test the bulk immute functionality
//     */
//    function testImmute()
//    {
//        // Create documents
//        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
//        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
//        $doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
//        $folder1 = $this->root->add_folder("New test folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
//
//        $aItems = array($doc1, $doc2, $doc3, $folder1);
//
//        // Immute documents
//        $res = $this->bulk->immute($aItems);
//
//        $this->assertTrue(empty($res));
//
//        $this->assertTrue($doc1->isImmutable());
//        $this->assertTrue($doc2->isImmutable());
//        $this->assertTrue($doc3->isImmutable());
//
//        // refresh the doc4 document object to reflect changes
//        $doc4 = KTAPI_Document::get($this->ktapi, $doc4->get_documentid());
//        $this->assertTrue($doc4->isImmutable());
//
//        // remove immutability for deletion
//        $doc1->unimmute();
//        $doc2->unimmute();
//        $doc3->unimmute();
//        $doc4->unimmute();
//
//        // Delete and expunge documents and folder
//        $this->deleteDocument($doc1);
//        $this->deleteDocument($doc2);
//        $this->deleteDocument($doc3);
//        $this->deleteDocument($doc4);
//        $folder1->delete('Testing bulk checkout');
//    }
//
//    /**
//     * Test the bulk delete functionality
//     */
//    function testDelete()
//    {
//        // Create documents
//        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
//        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
//        $doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
//        $folder1 = $this->root->add_folder("New test folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
//
//        $aItems = array($doc1, $doc2, $doc3, $folder1);
//
//        // Delete documents and folder
//        $res = $this->bulk->delete($aItems, 'Testing bulk delete');
//
//        $this->assertTrue(empty($res));
//
//        // Check documents have been deleted
//        $this->assertTrue($doc1->is_deleted());
//        $this->assertTrue($doc2->is_deleted());
//        $this->assertTrue($doc3->is_deleted());
//
//        // refresh the doc4 document object to reflect changes
//        $doc4 = KTAPI_Document::get($this->ktapi, $doc4->get_documentid());
//        $this->assertTrue($doc4->is_deleted());
//
//        // Check folder has been deleted
//        $folder = $this->ktapi->get_folder_by_name('New test folder');
//        $this->assertError($folder);
//
//        // Expunge documents
//        $doc1->expunge();
//        $doc2->expunge();
//        $doc3->expunge();
//        $doc4->expunge();
//    }
//
//    /**
//     * Test the bulk archive functionality
//     */
//    function testArchive()
//    {
//        // Create documents
//        $doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
//        $doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
//        $doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
//        $folder1 = $this->root->add_folder("New test folder");
//        $this->assertNotError($newFolder);
//        if(PEAR::isError($newFolder)) return;
//
//        $doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
//
//        $aItems = array($doc1, $doc2, $doc3, $folder1);
//
//        // Archive documents and folder
//        $res = $this->bulk->archive($aItems, 'Testing bulk archive');
//
//        $this->assertTrue(empty($res));
//
//        $document1 = $doc1->getObject();
//        $this->assertTrue($document1->getStatusID() == 4);
//
//        // refresh the doc4 document object to reflect changes
//        $doc4 = KTAPI_Document::get($this->ktapi, $doc4->get_documentid());
//        $document4 = $doc4->getObject();
//        $this->assertTrue($document4->getStatusID() == 4);
//
//        // Restore for deletion
//        $doc1->restore();
//        $doc2->restore();
//        $doc3->restore();
//        $doc4->restore();
//
//        // Delete and expunge documents and folder
//        $this->deleteDocument($doc1);
//        $this->deleteDocument($doc2);
//        $this->deleteDocument($doc3);
//        $this->deleteDocument($doc4);
//        $folder1->delete('Testing bulk archive');
//    }
    
    /**
     * 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, KT_TEST_USER, KT_TEST_PASS, '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;
    }
}
?>