testSoap.php 38.8 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
<?php
require_once (KT_DIR . '/tests/test.php');

/**
* These are the unit tests for the SOAP webservice
*
*/
class SOAPTestCase extends KTUnitTestCase {

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

    /**
     * @var string $rootUrl The root server url for the rest web service
     */
    var $rootUrl;
    var $uploads_dir;

    public $client;
    private $user;
    private $pass;
    private $content = 'New document for testing the soap webservice api';
    private $content_update = 'Updated document for testing the soap webservice api';
    private $reason = 'Running unit tests';

    /**
    * This method sets up the server url
    *
    */
    public function setUp()
    {
        $url = KTUtil::kt_url();
        $this->rootUrl = $url.'/ktwebservice/webservice.php?';
        $this->user = isset($_GET['user']) ? $_GET['user'] : 'admin';
		$this->pass = isset($_GET['pass']) ? $_GET['pass'] : 'admin';

    	// Login and authenticate
    	$this->connect();
		$this->login('127.0.0.1');
    }

    /**
    * This method is a placeholder
    */
    public function tearDown()
    {
        // Logout
        $this->logout();
    }

    private function connect()
    {
        $wsdl = $this->rootUrl . "wsdl";
        $this->client = new SoapClient($wsdl);
    }

    private function login($ip = null)
    {
        $res = $this->client->__soapCall("login", array($this->user, $this->pass, $ip));
        if($res->status_code != 0){
            return false;
        }
        $this->session = $res->message;
    }

    private function logout()
    {
        $result = $this->client->__soapCall("logout", array($this->session));
        if($result->status_code != 0){
            return true;
        }
    }

    private function getDocTypes()
    {
        $result = $this->client->__soapCall("get_document_types", array($this->session));
        return $result->document_types;
    }

    private function search($expr)
    {
        $result = $this->client->__soapCall("search", array($this->session, $expr, ''));
        return $result->hits;
    }

    private function getFolderDetail($folder, $parentId = 1)
    {
        $result = $this->client->__soapCall("get_folder_detail_by_name", array($this->session, $folder, $parentId));
        return $result;
    }

    private function createFolder($parent_id, $folder)
    {
        $result = $this->client->__soapCall("create_folder", array($this->session, $parent_id, $folder));
        return $result;
    }

    private function createFolderShortcut($target_folder_id, $source_folder_id)
    {
        $result = $this->client->__soapCall("create_folder_shortcut", array($this->session, $target_folder_id, $source_folder_id));
        return $result;
    }

    private function deleteFolder($folder_id)
    {
        $result = $this->client->__soapCall("delete_folder", array($this->session, $folder_id, 'Testing'));
        return $result;
    }

    private function getFolderShortcuts($folder_id)
    {
        $result = $this->client->__soapCall("get_folder_shortcuts", array($this->session, $folder_id));
        return $result;
    }

    private function getDocumentVersionHistory($document_id)
    {
        $result = $this->client->__soapCall("get_document_version_history", array($this->session, $document_id));
        return $result;
    }

    private function createDocument($folder_id,  $title, $filename, $documenttype = 'Default')
    {
        global $default;
        $uploads_dir = $default->uploadDirectory;

        // create document in uploads folder
        $tempfilename = tempnam($uploads_dir, 'myfile');
        $fp = fopen($tempfilename, 'wt');
        fwrite($fp, $this->content);
        fclose($fp);

        // call add document to upload into repo
        $result = $this->client->__soapCall("add_document", array($this->session, $folder_id,  $title, $filename, $documenttype, $tempfilename));
        return $result;
    }

    private function createSmallDocument($folder_id,  $title, $filename, $documenttype = 'Default')
    {
        global $default;
        $uploads_dir = $default->uploadDirectory;

        // create document in uploads folder
        $base64 = base64_encode($this->content);

        // call add document to upload into repo
        $result = $this->client->__soapCall("add_small_document", array($this->session, $folder_id,  $title, $filename, $documenttype, $base64));
        return $result;
    }

    private function checkinDocument($document_id,  $filename, $major_update = false)
    {
        global $default;
        $uploads_dir = $default->uploadDirectory;

        // create document in uploads folder
        $tempfilename = tempnam($uploads_dir, 'myfile');
        $fp = fopen($tempfilename, 'wt');
        fwrite($fp, $this->content_update);
        fclose($fp);

        // call add document to upload into repo
        $result = $this->client->__soapCall("checkin_document", array($this->session, $document_id,  $filename, $this->reason, $tempfilename, $major_update));
        return $result;
    }

    private function checkinSmallDocument($document_id,  $filename, $major_update = false)
    {
        global $default;
        $uploads_dir = $default->uploadDirectory;

        // encode as base64
        $base64 = base64_encode($this->content_update);

        // call add document to upload into repo
        $result = $this->client->__soapCall("checkin_small_document", array($this->session, $document_id,  $filename, $this->reason, $base64, $major_update));
        return $result;
    }

    private function checkoutDocument($document_id, $download = true)
    {
        $result = $this->client->__soapCall("checkout_document", array($this->session, $document_id, $this->reason, $download));
        return $result;
    }

    private function checkoutSmallDocument($document_id, $download = true)
    {
        $result = $this->client->__soapCall("checkout_small_document", array($this->session, $document_id, $this->reason, $download));
        return $result;
    }

    private function createDocumentShortcut($folder_id, $target_document_id)
    {
        $result = $this->client->__soapCall("create_document_shortcut", array($this->session, $folder_id, $target_document_id));
        return $result;
    }

    private function getDocumentShortcuts($document_id)
    {
        $result = $this->client->__soapCall("get_document_shortcuts", array($this->session, $document_id));
        return $result;
    }

    private function getFolderContents($folder_id, $depth=1, $what='DFS')
    {
        $result = $this->client->__soapCall("get_folder_contents", array($this->session, $folder_id, $depth, $what));
        return $result;
    }

    private function getDocumentMetadata($document_id, $version = null)
    {
        $result = $this->client->__soapCall("get_document_metadata", array($this->session, $document_id, $version));
        return $result;
    }

    private function updateDocumentMetadata($document_id, $metadata, $sysdata=null)
    {
        $result = $this->client->__soapCall("update_document_metadata", array($this->session, $document_id, $metadata, $sysdata));
        return $result;
    }

    private function downloadDocument($document_id, $version = null)
    {
        $result = $this->client->__soapCall("download_document", array($this->session, $document_id, $version));
        return $result;
    }

    private function downloadSmallDocument($document_id, $version = null)
    {
        $result = $this->client->__soapCall("download_small_document", array($this->session, $document_id, $version));
        return $result;
    }

    private function doDownload($url)
    {
        $ch = curl_init($url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

		$download = curl_exec($ch);
		$status = curl_getinfo($ch,CURLINFO_HTTP_CODE);

		$error = curl_errno($ch);

		curl_close($ch);

		return array('status' => $status, 'error' => $error, 'download' => $download);
    }

    /* *** now the test functions *** */

    /**
     * Tests finding of a folder or folder detail by name
     * Runs the following sub-tests:
     * . Folder Detail by Name in root folder (no duplicate names)
     * . Folder Detail by Name in subfolder of root folder (no duplicate names)
     * . Folder Detail by Name in subfolder of root folder (duplicate names)
     *
     * NOTE there are less tests here because we cannot test the get_folder_by_name function since it does not exist within the web service code
     */
    public function testGetFolderByName()
    {
		// set up
    	$root_folder_id = array();
    	$sub_folder_id = array();
    	$folders[0][1] = 'Root Folder';

    	// Create a sub folder in the root folder
    	$parentId = 1;
    	$folderName = 'Test api sub-folder ONE';
    	$response = $this->createFolder($parentId, $folderName);
    	$root_folder_id[] = $response->id;
        $folders[$parentId][$response->id] = $folderName;
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));

    	// Create a second sub folder in the root folder
    	$parentId = 1;
    	$folderName = 'Test api sub-folder TWO';
    	$response = $this->createFolder($parentId, $folderName);
    	$root_folder_id[] = $response->id;
        $folders[$parentId][$response->id] = $folderName;
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));

    	// Create a sub folder in the first sub folder
    	$parentId = $root_folder_id[0];
    	$folderName = 'Test api sub-folder THREE';
    	$response = $this->createFolder($parentId, $folderName);
    	$sub_folder_id[0][] = $response->id;
        $folders[$parentId][$response->id] = $folderName;
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));

        // Create a sub folder within the first sub folder which shares a name with one of the root sub folders
    	$parentId = $sub_folder_id[0][0];
    	$folderName = 'Test api sub-folder TWO';
    	$response = $this->createFolder($parentId, $folderName);
    	$sub_folder_id[0][] = $response->id;
        $folders[$parentId][$response->id] = $folderName;
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));

        // Create a second sub folder in the first sub folder
    	$parentId = $root_folder_id[0];
    	$folderName = 'Test api sub-folder FOUR';
    	$response = $this->createFolder($parentId, $folderName);
    	$sub_folder_id[0][] = $response->id;
        $folders[$parentId][$response->id] = $folderName;
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));

    	// Create a sub folder within the second sub folder
    	$parentId = $root_folder_id[1];
    	$folderName = 'Test api sub-folder FIVE';
    	$response = $this->createFolder($parentId, $folderName);
    	$sub_folder_id[1][] = $response->id;
        $folders[$parentId][$response->id] = $folderName;
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));

    	// Create a sub folder within the second sub folder which shares a name with a sub folder in the first sub folder
    	$parentId = $sub_folder_id[1][0];
    	$folderName = 'Test api sub-folder THREE';
    	$response = $this->createFolder($parentId, $folderName);
    	$sub_folder_id[1][] = $response->id;
        $folders[$parentId][$response->id] = $folderName;
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));

		// NOTE default parent is 1, so does not need to be declared when searching the root folder, but we use it elsewhere

        // Fetching of root folder
		$parentId = 0;
        $folderName = 'Root Folder';
        $response = $this->getFolderDetail($folderName);
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));
    	if (($response->status_code == 0)) {
    		$this->assertEqual($folders[$parentId][$response->id], $folderName);
    	}

        // Folder Detail by Name in root folder (no duplicate names)
        $parentId = 1;
        $folderName = 'Test api sub-folder ONE';
        // no parent required
    	$response = $this->getFolderDetail($folderName);
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));
    	if (($response->status_code == 0)) {
    		$this->assertEqual($folders[$parentId][$response->id], $folderName);
    	}

        // Folder Detail by Name in sub folder of root folder (no duplicate names)
        $parentId = $root_folder_id[0];
        $folderName = 'Test api sub-folder FOUR';
        $response = $this->getFolderDetail($folderName, $parentId);
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));
    	if (($response->status_code == 0)) {
    		$this->assertEqual($folders[$parentId][$response->id], $folderName);
    	}

        // Folder Detail by Name in root folder (duplicate names)
        $parentId = 1;
        $folderName = 'Test api sub-folder TWO';
        $response = $this->getFolderDetail($folderName, $parentId);
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));
    	if (($response->status_code == 0)) {
    		$this->assertEqual($folders[$parentId][$response->id], $folderName);
    	}

        // Folder Detail by Name in sub folder of root folder (duplicate names)
        $parentId = $root_folder_id[0];
        $folderName = 'Test api sub-folder THREE';
        $response = $this->getFolderDetail($folderName, $parentId);
    	$this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));
    	if (($response->status_code == 0)) {
    		$this->assertEqual($folders[$parentId][$response->id], $folderName);
    	}

    	// Negative test with non duplicated folder - look for folder in location it does not exist
        $parentId = $root_folder_id[0];
        $folderName = 'Test api sub-folder ONE';
        $response = $this->getFolderDetail($folderName, $parentId);
        $this->assertNotEqual($response->status_code, 0);
    	$this->assertTrue(!empty($response->message));
    	$this->assertNotEqual($folders[$parentId][$response->id], $folderName);

    	// Negative test with duplicated folder - look for folder with incorrect parent id, result should not match expected folder
    	$parentId = 1;
    	$actualParentId = $sub_folder_id[0][0];
        $folderName = 'Test api sub-folder TWO';
        $response = $this->getFolderDetail($folderName, $parentId);
        // we should get a result
        $this->assertEqual($response->status_code, 0);
    	$this->assertFalse(!empty($response->message));
    	// but not the one we wanted
    	$expectedResponse = $this->getFolderDetail($folderName, $actualParentId);
    	$this->assertNotEqual($response->id, $expectedResponse->id);

        // Clean up - delete all of the folders
        foreach ($root_folder_id as $folder_id) {
        	$this->deleteFolder($folder_id);
        }
    }

    /**
     * Tests the creation of folders and folder shortcuts, getting the folder shortcut(s)
     * WebService V3 and higher
     */
    public function testCreateFolderShortcut()
    {
        // if V3
        if(LATEST_WEBSERVICE_VERSION < 3){
            return ;
        }

        // Create folder 1
        $result = $this->createFolder(1, 'Test Shortcut Container');
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $parentFolderId = $result->id;

        // Create folder 2
        $result = $this->createFolder(1, 'Test Shortcut Target');
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $targetFolderId = $result->id;

        // Create folder 3
        $result = $this->createFolder($targetFolderId, 'Test Shortcut Target 2');
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $target2FolderId = $result->id;

        // Create shortcut in folder 1 targeting folder 2
        $result = $this->createFolderShortcut($parentFolderId, $targetFolderId);
        $this->assertEqual($result->status_code, 0, 'Create folder Shortcut - '.$result->message);

        $shortcutId = $result->id;
        $linkedFolderId = $result->linked_folder_id;
        $this->assertTrue(is_numeric($shortcutId), 'Shortcut id should be numeric');
        $this->assertTrue(is_numeric($linkedFolderId), 'Linked folder id should be numeric');
        $this->assertEqual($linkedFolderId, $targetFolderId, 'Shortcut should contain link the target folder');

        // Create shortcut in folder 1 targeting folder 3
        $result = $this->createFolderShortcut($parentFolderId, $target2FolderId);
        $this->assertEqual($result->status_code, 0, 'Create folder shortcut - '.$result->message);

        $shortcut2Id = $result->id;
        $linkedFolder2Id = $result->linked_folder_id;
        $this->assertTrue(is_numeric($shortcut2Id), 'Shortcut id should be numeric');
        $this->assertTrue(is_numeric($linkedFolder2Id), 'Linked folder id should be numeric');
        $this->assertEqual($linkedFolder2Id, $target2FolderId, 'Shortcut should contain link the target folder');

        // Get the folder shortcut details
        $result = $this->getFolderShortcuts($targetFolderId);
        $this->assertEqual($result->status_code, 0, 'Should return list of folder shortcuts - '.$result->message);

        $shortcuts = $result[1];
        foreach ($shortcuts as $item) {
            $this->assertEqual($item->id, $shortcutId);
            $this->assertEqual($item->linked_folder_id, $targetFolderId);
        }

        $result = $this->getFolderShortcuts($target2FolderId);
        $this->assertEqual($result->status_code, 0, 'Should return list of folder shortcuts - '.$result->message);

        $shortcuts2 = $result[1];
        foreach ($shortcuts2 as $item) {
            $this->assertEqual($item->id, $shortcut2Id);
            $this->assertEqual($item->linked_folder_id, $target2FolderId);
        }

        // delete and cleanup
        $this->deleteFolder($parentFolderId);
        $this->deleteFolder($targetFolderId);
    }

    /**
     * Tests the document upload and the creation and retrieval of document shortcuts
     * WS V3
     */
    public function testCreateDocumentShortcut()
    {
        // if V3
        if(LATEST_WEBSERVICE_VERSION < 3){
            //$this->assertEqual(LATEST_WEBSERVICE_VERSION, 3, 'Webservice version is less than 3. Exiting test, functionality is V3 only');
            return ;
        }

        // Create folder 1 containing the shortcut
        $result = $this->createFolder(1, 'Test Shortcut Container');
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $parentFolderId = $result->id;

        // Create folder 2 containing the document
        $result = $this->createFolder(1, 'Test Shortcut Target');
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $targetFolderId = $result->id;

        // Upload document to folder 2
        $result = $this->createDocument($targetFolderId, 'Target Doc', 'target.txt');
        $this->assertEqual($result->status_code, 0, 'Create document - '.$result->message);
        $documentId = $result->document_id;
        $this->assertTrue(is_numeric($documentId), 'Document id should be numeric');

        // Create shortcut in folder 1, pointing to the document in folder 2
        $result = $this->createDocumentShortcut($parentFolderId, $documentId);
        $this->assertEqual($result->status_code, 0, 'Create document shortcut - '.$result->message);
        $shortcutId = $result->document_id;
        $linkedDocumentId = $result->linked_document_id;
        $this->assertTrue(is_numeric($shortcutId), 'Shortcut id should be numeric');
        $this->assertTrue(is_numeric($linkedDocumentId), 'Linked document id should be numeric');
        $this->assertEqual($linkedDocumentId, $documentId, 'Shortcut should contain link to the target document');

        // delete and cleanup
        $this->deleteFolder($parentFolderId);
        $this->deleteFolder($targetFolderId);
    }

    /**
     * Tests document uploads, checkin / checkout, downloads and document version downloads (V3)
     * Normal documents and small / base64 documents
     */
    public function testUploadDownloadDocument()
    {
        // Create folder containing the documents
        $result = $this->createFolder(1, 'Test Container');
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $folderId = $result->id;

        // Upload document to folder
        $filename = 'test_doc.txt';
        $result = $this->createDocument($folderId, 'Test Doc', $filename);
        $this->assertEqual($result->status_code, 0, 'Create document - '.$result->message);
        $documentId = $result->document_id;
        $this->assertTrue(is_numeric($documentId), 'Document id should be numeric');

        // Get the download url for the document
        $result = $this->downloadDocument($documentId);
        $this->assertEqual($result->status_code, 0, 'Get document download url - '.$result->message);
        $url = $result->message;
        $this->assertTrue(is_string($url), 'URL should be a string');
        $this->assertTrue(strpos($url, 'http') !== false, 'URL should contain http');

        // Download the document
        $result = $this->doDownload($url);
        $this->assertEqual($result['status'], 200, 'Status should be code 200 for success - '.$result['status']);
        $this->assertEqual($result['error'], 0, 'Error code should be 0 for success - '.$result['error']);
        $this->assertEqual($result['download'], $this->content, 'Document content should be the same as that uploaded');

        // ----------

        // Upload base64 document to folder
        $filename2 = 'test_base64_doc.txt';
        $result = $this->createSmallDocument($folderId, 'Test Base64 Doc', $filename2);
        $this->assertEqual($result->status_code, 0, 'Create base64 document - '.$result->message);
        $documentId2 = $result->document_id;
        $this->assertTrue(is_numeric($documentId2), 'Document id should be numeric');

        // Download base64 document
        $result = $this->downloadSmallDocument($documentId2);
        $this->assertEqual($result->status_code, 0, 'Download small document - '.$result->message);
        $content = $result->message;
        $this->assertTrue(is_string($content), 'Content should be a string');
        $content = base64_decode($content);
        $this->assertEqual($content, $this->content, 'Document content should be the same as that uploaded');

        if(LATEST_WEBSERVICE_VERSION < 3){
            // delete and cleanup
            $this->deleteFolder($folderId);
            return ;
        }

        /* *** V3 functions *** */

        // Checkout first document
        $result = $this->checkoutDocument($documentId);
        $this->assertEqual($result->status_code, 0, 'Checkout document - '.$result->message);
        $checkoutBy = $result->checked_out_by;
        $checkoutDate = $result->checked_out_date;
        $url = $result->message;
        $this->assertFalse(is_null($checkoutDate), 'Checked out date should not be null / empty - '.$checkoutDate);
        $this->assertEqual($checkoutBy, 'Administrator', 'Using the Administrative user, checked out user must match - '.$checkoutBy);
        $this->assertTrue(is_string($url), 'Download URL should be a string');
        $this->assertTrue(strpos($url, 'http') !== false, 'Download URL should contain http');

        // Download the document
        $result = $this->doDownload($url);
        $content = $result['download'];
        $this->assertEqual($result['status'], 200, 'Status should be code 200 for success - '.$result['status']);
        $this->assertEqual($result['error'], 0, 'Error code should be 0 for success - '.$result['error']);
        $this->assertEqual($result['download'], $this->content, 'Document content should be the same as that uploaded');

        // ----------

        // Checkin a new version
        $result = $this->checkinDocument($documentId, $filename);
        $this->assertEqual($result->status_code, 0, 'Checkin document - '.$result->message);
        $checkoutBy = $result->checked_out_by;
        $this->assertTrue(empty($checkoutBy) || $checkoutBy == 'n/a', 'Document should no longer be checked out by anyone - '.$checkoutBy);

        // Download new version
        $result = $this->downloadDocument($documentId);
        $this->assertEqual($result->status_code, 0, 'Get checkedin document download url - '.$result->message);
        $url = $result->message;

        // Download the document
        $result = $this->doDownload($url);
        $this->assertEqual($result['download'], $this->content_update, 'Document content should be the same as the updated content');

        // ----------

        // Get previous versions of the document
        $result = $this->getDocumentVersionHistory($documentId);
        $this->assertEqual($result->status_code, 0, 'Get document version history - '.$result->message);
        $history = $result->history;
        $this->assertTrue(is_array($history), 'Version history should be an array');
        $this->assertEqual(count($history), 2, 'Version history should contain 2 items / versions');

        // Get the previous version number
        $version = isset($history[1]) ? $history[1]->content_version : null;

        // Download previous version
        $result = $this->downloadDocument($documentId, $version);
        $this->assertEqual($result->status_code, 0, "Get document download url for  previous version ($version) - ".$result->message);
        $url = $result->message;

        // Download the document
        $result = $this->doDownload($url);
        $this->assertEqual($result['status'], 200, 'Status should be code 200 for success - '.$result['status']);
        $this->assertEqual($result['error'], 0, 'Error code should be 0 for success - '.$result['error']);
        $this->assertEqual($result['download'], $this->content, 'Document content should be the same as the original content of the initial upload');

        // ----------

        // Checkout base64 document
        $result = $this->checkoutSmallDocument($documentId2);
        $this->assertEqual($result->status_code, 0, 'Checkout base64 document - '.$result->message);
        $checkoutBy = $result->checked_out_by;
        $checkoutDate = $result->checked_out_date;
        $content = $result->message;
        $this->assertFalse(is_null($checkoutDate), 'Checked out date should not be null / empty - '.$checkoutDate);
        $this->assertEqual($checkoutBy, 'Administrator', 'Using the Administrative user, checked out user must match - '.$checkoutBy);
        $this->assertTrue(is_string($content), 'Base64 content should be a string');
        $content = base64_decode($content);
        $this->assertEqual($content, $this->content, 'Document content should be the same as that uploaded');

        // ----------

        // Checkin a new base64 version
        $result = $this->checkinSmallDocument($documentId2, $filename2);
        $this->assertEqual($result->status_code, 0, 'Checkin base64 document - '.$result->message);
        $checkoutBy = $result->checked_out_by;
        $this->assertTrue(empty($checkoutBy) || $checkoutBy == 'n/a', 'Document should no longer be checked out by anyone - '.$checkoutBy);

        // Download new version
        $result = $this->downloadSmallDocument($documentId2);
        $this->assertEqual($result->status_code, 0, 'Download checkedin base64 document - '.$result->message);
        $content = $result->message;
        $this->assertTrue(is_string($content), 'Content should be a string');
        $content = base64_decode($content);
        $this->assertEqual($content, $this->content_update, 'Document content should be the same as the updated content');

        // ----------

        // Get previous versions of the base64 document
        $result = $this->getDocumentVersionHistory($documentId2);
        $this->assertEqual($result->status_code, 0, 'Get document version history - '.$result->message);
        $history = $result->history;
        $this->assertTrue(is_array($history), 'Version history should be an array');
        $this->assertEqual(count($history), 2, 'Version history should contain 2 items / versions');

        // Get the previous version number
        $version = isset($history[1]) ? $history[1]->content_version : null;

        // Download previous version
        $result = $this->downloadSmallDocument($documentId2, $version);
        $this->assertEqual($result->status_code, 0, "Download previous version ($version) - ".$result->message);
        $content = $result->message;
        $this->assertTrue(is_string($content), 'Content should be a string');
        $content = base64_decode($content);
        $this->assertEqual($content, $this->content, 'Document content should be the same as the original content of the initial upload');

        // delete and cleanup
        $this->deleteFolder($folderId);
    }

    /**
     * Tests getting and updating document metadata
     */
    public function testDocumentMetadata()
    {
        // Create folder containing the documents
        $result = $this->createFolder(1, 'Test Metadata Container');
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $folderId = $result->id;

        // Upload document to folder
        $filename = 'test_doc.txt';
        $result = $this->createDocument($folderId, 'Test Doc', $filename);
        $this->assertEqual($result->status_code, 0, 'Create document - '.$result->message);
        $documentId = $result->document_id;
        $this->assertTrue(is_numeric($documentId), 'Document id should be numeric');

        // get document metadata
        $result = $this->getDocumentMetadata($documentId);
        $this->assertEqual($result->status_code, 0, 'Get document metadata - '.$result->message);
        $metadata = $result->metadata;
        $this->assertTrue(is_array($metadata), 'Returned document metadata should be an array of fieldsets');

        // Add a new tag and a document author
        foreach ($metadata as $fieldset){
            $fields = $fieldset->fields;
            switch($fieldset->fieldset){
                case 'Tag Cloud':
                    $field_name = 'Tag';
                    $curr_val = 'n/a';
                    $new_val = 'unit test';
                    break;
                case 'General information':
                    $field_name = 'Document Author';
                    $curr_val = 'n/a';
                    $new_val = 'Test Framework';
                    break;
            }

            foreach ($fields as $field){
                if($field->name == $field_name){
                    $this->assertEqual($field->value, $curr_val, "The current value of the given field, $field_name, should be \"$curr_val\"");
                    // update the value
                    $field->value = $new_val;
                }
                if($field->value == 'n/a'){
                    $field->value = '';
                }
            }
        }

        // update metadata
        $result = $this->updateDocumentMetadata($documentId, $metadata);

        // get metadata - ensure it matches the updated metadata
        $result = $this->getDocumentMetadata($documentId);
        $this->assertEqual($result->status_code, 0, 'Get document metadata - '.$result->message);
        $metadata = $result->metadata;
        $this->assertTrue(is_array($metadata), 'Returned document metadata should be an array of fieldsets');

        // Add a new tag and a document author
        foreach ($metadata as $fieldset){
            $fields = $fieldset->fields;
            switch($fieldset->fieldset){
                case 'Tag Cloud':
                    $field_name = 'Tag';
                    $curr_val = 'unit test';
                    break;
                case 'General information':
                    $field_name = 'Document Author';
                    $curr_val = 'Test Framework';
                    break;
            }

            foreach ($fields as $field){
                if($field->name == $field_name){
                    $this->assertEqual($field->value, $curr_val, "The current value of the given field, $field_name, should be the same as the updated value: \"$curr_val\"");
                }
            }
        }

        // Get previous versions of the document
        $result = $this->getDocumentVersionHistory($documentId);
        $this->assertEqual($result->status_code, 0, 'Get document version history - '.$result->message);
        $history = $result->history;
        $this->assertTrue(is_array($history), 'Version history should be an array');
        $this->assertEqual(count($history), 2, 'Version history should contain 2 items / versions');

        // Get the previous version number
        $version = isset($history[1]) ? $history[1]->metadata_version : null;

        // get document metadata for previous version
        $result = $this->getDocumentMetadata($documentId, $version);
        $this->assertEqual($result->status_code, 0, 'Get document metadata - '.$result->message);
        $metadata = $result->metadata;
        $this->assertTrue(is_array($metadata), 'Returned document metadata should be an array of fieldsets');

        // Add a new tag and a document author
        foreach ($metadata as $fieldset){
            $fields = $fieldset->fields;
            switch($fieldset->fieldset){
                case 'Tag Cloud':
                    $field_name = 'Tag';
                    $curr_val = 'n/a';
                    break;
                case 'General information':
                    $field_name = 'Document Author';
                    $curr_val = 'n/a';
                    break;
            }

            foreach ($fields as $field){
                if($field->name == $field_name){
                    $this->assertEqual($field->value, $curr_val, "The current value of the given field, $field_name, should be the same as the previous version's value: \"$curr_val\"");
                }
            }
        }

        // delete and cleanup
        $this->deleteFolder($folderId);
    }

    /**
     * Test getting the contents of a folder
     *
     */
    public function testGetFolderListing()
    {
        // create folder
        $main_folder = 'Test Listing Container';
        $result = $this->createFolder(1, $main_folder);
        $this->assertEqual($result->status_code, 0, 'Create folder - '.$result->message);
        $folderId = $result->id;

        // create subfolder 1
        $sub_folder_name = 'Subfolder One';
        $result = $this->createFolder($folderId, $sub_folder_name);
        $this->assertEqual($result->status_code, 0, 'Create subfolder 1 - '.$result->message);
        $subFolderId1 = $result->id;

        // create subfolder 2
        $result = $this->createFolder($folderId, 'Subfolder Two');
        $this->assertEqual($result->status_code, 0, 'Create subfolder 2 - '.$result->message);
        $subFolderId2 = $result->id;

        // create subfolder 3 under subfolder 1
        $result = $this->createFolder($subFolderId1, 'Subfolder Three');
        $this->assertEqual($result->status_code, 0, 'Create subfolder 3 under subfolder 1 - '.$result->message);
        $subFolderId3 = $result->id;

        // upload document into main folder
        $filename = 'test_doc.txt';
        $result = $this->createDocument($folderId, 'Test Doc', $filename);
        $this->assertEqual($result->status_code, 0, 'Create document 1 in folder - '.$result->message);
        $documentId = $result->document_id;
        $this->assertTrue(is_numeric($documentId), 'Document id should be numeric');

        // upload document 2 into main folder
        $filename2 = 'test_doc2.txt';
        $result = $this->createDocument($folderId, 'Test Doc 2', $filename2);
        $this->assertEqual($result->status_code, 0, 'Create document 1 in folder - '.$result->message);
        $documentId2 = $result->document_id;
        $this->assertTrue(is_numeric($documentId2), 'Document id should be numeric');

        // upload document 3 into subfolder 1
        $filename3 = 'test_doc3.txt';
        $result = $this->createDocument($subFolderId1, 'Test Doc 3', $filename3);
        $this->assertEqual($result->status_code, 0, 'Create document 1 in sub folder 1 - '.$result->message);
        $documentId3 = $result->document_id;
        $this->assertTrue(is_numeric($documentId3), 'Document id should be numeric');

        // Get folder listing for folder 1 - folders only, depth of 1
        $result = $this->getFolderContents($folderId, 1, 'F');
        $this->assertEqual($result->status_code, 0, 'Get subfolders in folder - '.$result->message);
        $folder_name = $result->folder_name;
        $sub_folders = $result->items;
        $this->assertEqual($folder_name, $main_folder, 'Folder name is - '.$folder_name);
        $this->assertTrue(is_array($sub_folders), 'There should be an array of subfolders');
        $this->assertEqual(count($sub_folders), 2, 'There should be 2 subfolders');

        // Get folder listing for folder 1 - folders and documents, infinite depth
        $result = $this->getFolderContents($folderId, 5, 'FD');
        $this->assertEqual($result->status_code, 0, 'Get all subfolders and documents under the folder - '.$result->message);
        $items = $result->items;
        $this->assertTrue(is_array($items), 'There should be an array of subfolders and documents');
        $this->assertEqual(count($items), 4, 'There should be 2 subfolders and 2 documents in the immediate folder');

        // Loop through items, find sub folder 1
        $docs = 0;
        $folders = 0;
        foreach ($items as $item){
            // increment count of item type
            $folders = ($item->item_type == 'F') ? $folders + 1 : $folders;
            $docs = ($item->item_type == 'D') ? $docs + 1 : $docs;

            if($item->id == $subFolderId1){
                $sub_items = $item->items;

                $this->assertTrue(is_array($sub_items), 'Subfolder 1 should contain an array of contents');
                $this->assertEqual(count($sub_items), 2, 'Subfolder 1 should contain a folder and document');

            }
        }

        $this->assertEqual($folders, 2, 'There should be 2 folders');
        $this->assertEqual($docs, 2, 'There should be 2 documents');

        // Get folder listing for subfolder 1 - documents only, depth of 1
        $result = $this->getFolderContents($subFolderId1, 1, 'D');
        $this->assertEqual($result->status_code, 0, 'Get documents under subfolder 1 - '.$result->message);
        $folder_name = $result->folder_name;
        $items = $result->items;
        $this->assertEqual($folder_name, $sub_folder_name, 'Subfolder name is - '.$folder_name);
        $this->assertTrue(is_array($items), 'There should be an array of documents');
        $this->assertEqual(count($items), 1, 'There should be 1 document');

        // delete and cleanup
        $this->deleteFolder($folderId);
    }
}
?>