testDocument.php 15.9 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
<?php
require_once (KT_DIR . '/tests/test.php');
require_once (KT_DIR . '/ktapi/ktapi.inc.php');

/**
 * Helper class for the KTAPI_Document unit tests
 *
 * @author KnowledgeTree Team
 * @package KTAPI
 * @version 0.9
 */
class APIDocumentHelper {
    function createRandomFile($content = 'this is some text') {
        $temp = tempnam(dirname(__FILE__), 'myfile');
        $fp = fopen($temp, 'wt');
        fwrite($fp, $content);
        fclose($fp);
        return $temp;
    }
}

/**
 * Unit tests for the KTAPI_Document class
 *
 * @author KnowledgeTree Team
 * @package KTAPI
 * @version 0.9
 */
class APIDocumentTestCase extends KTUnitTestCase {

    /**
     * @var KTAPI
     */
    var $ktapi;

    /**
     * @var KTAPI_Folder
     */
    var $root;

    /**
     * @var KTAPI_Session
     */
    var $session;

    /**
     * Create a ktapi session
     */
    function setUp() {
        $this->ktapi = new KTAPI();
        $this->session = $this->ktapi->start_system_session();
        $this->root = $this->ktapi->get_root_folder();
        $this->assertTrue($this->root instanceof KTAPI_Folder);
    }

    /**
     * End the ktapi session
     */
    function tearDown() {
        $this->session->logout();
    }

    /* *** KTAPI functions *** */

    function testGetRoleAllocation()
    {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle', 'testname.txt', 'Default', $randomFile);
        @unlink($randomFile);
        $this->assertNotError($document);
        if(PEAR::isError($document)) return;

        $response = $this->ktapi->get_role_allocation_for_document($document->get_documentid());
        $this->assertEqual($response['status_code'], 0);

        $document->delete('Testing');
        $document->expunge();
    }
    
    // causing a failure due to:
    // Fatal error: Call to undefined function sendGroupEmails() in C:\ktdms\knowledgeTree\ktapi\KTAPIDocument.inc.php
    // This causes all following tests to fail as well
    /*
    function testEmailDocument()
    {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle', 'testname.txt', 'Default', $randomFile);
        @unlink($randomFile);
        $this->assertNotError($document);
        if(PEAR::isError($document)) return;

        $members = array('users' => array(1));
        $response = $this->ktapi->email_document($document->get_documentid(), $members, 'Test Email', false);
        $this->assertEqual($response['status_code'], 0);

        $document->delete('Testing');
        $document->expunge();
    }
    */

    /* *** Class functions *** */

    /**
     * Tests the add and delete document functionality
     */
    function testAddDocument() {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle.txt', 'testname.txt', 'Default', $randomFile);
        $this->assertNotError($document);

        if(PEAR::isError($document)) return;
        $this->assertTrue($document instanceof KTAPI_Document);
        @unlink($randomFile);
        $documentid = $document->get_documentid();

        // get document
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertTrue($document instanceof KTAPI_Document);
        $this->assertEqual($document->get_title(), 'testtitle.txt');
        $this->assertFalse($document->is_deleted());
        $document->delete('Testing document add and delete');

        // check if document still exists
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertTrue($document instanceof KTAPI_Document);
        $this->assertTrue($document->is_deleted());
        $document->expunge();

        // check if document still exists
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertFalse($document instanceof KTAPI_Document);
    }

    /**
     * Tests the document download functionality
     */
    function testDownload() {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));
        $document = $this->root->add_document('testtitle789', 'testname789.txt', 'Default', $randomFile);
        $this->assertNotError($document);

        if(PEAR::isError($document)) return;
        $this->assertTrue($document instanceof KTAPI_Document);
        @unlink($randomFile);

        $download_url = $document->get_download_url();
        $this->assertTrue(is_string($download_url));

        $doc_id = $document->get_documentid();
        $this->assertFalse(strpos($download_url, 'd='.$doc_id) === false);

        // Delete and expunge document
        $document->delete('Testing document download');
        $document->expunge();
    }

    /**
     * Tests the get metadata, update metadata and get packed metadata functionality
     */
    function testGetMetadata() {
        // Create a new document
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle123', 'testname123.txt', 'Default', $randomFile);
        $this->assertNotError($document);

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

        // Get the document metadata
        $metadata = $document->get_metadata();
        $this->assertTrue(count($metadata) > 0);

        $this->assertTrue($metadata[0]['fieldset'] == 'Tag Cloud');
        $this->assertTrue($metadata[0]['fields'][0]['description'] == 'Tag Words');

        // Update the metadata - add a tag
        $metadata[0]['fields'][0]['value'] = 'test';

        $res = $document->update_metadata($metadata);
        $this->assertFalse($res instanceof PEAR_Error);

        $new_metadata = $document->get_packed_metadata();

        $this->assertTrue($new_metadata[0][0]->getDescription() == 'Tag Words');
        $this->assertTrue($new_metadata[0][1] == 'test');

        // Delete and expunge document
        $document->delete('Testing document get metadata');
        $document->expunge();
    }

    /**
     * Tests the copy functionality. Includes the get_title() functionality.
     */
    function testCopy() {
        // Create a new document
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle123', 'testname123.txt', 'Default', $randomFile);
        $this->assertNotError($document);

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

        // Add a folder to copy into
        $newFolder = $this->root->add_folder("New folder");
        $this->assertNotError($newFolder);

        if(PEAR::isError($newFolder)) return;

        // Copy document into the new folder
        $copyDoc = $document->copy($newFolder, 'Testing document copy');
        $this->assertTrue($copyDoc instanceof KTAPI_Document);
        $this->assertTrue($copyDoc->get_title() == 'testtitle123');

        // Delete and expunge documents
        $document->delete('Testing document copy');
        $document->expunge();
        $copyDoc->delete('Testing document copy');
        $copyDoc->expunge();

        // Delete test folder
        $newFolder->delete('Testing document copy');
    }

    /**
     * Tests the move functionality. Includes the get_detail() functionality.
     */
    function testMove() {
        // Create a new document
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle246', 'testname246.txt', 'Default', $randomFile);
        $this->assertNotError($document);

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

        // Add a folder to copy into
        $newFolder = $this->root->add_folder("New folder");
        $this->assertNotError($newFolder);

        if(PEAR::isError($newFolder)) return;

        // Copy document into the new folder
        $document->move($newFolder, 'Testing document move');
        $detail = $document->get_detail();

        $this->assertFalse($detail['folder_id'] == $this->root->get_folderid());
        $this->assertTrue($detail['folder_id'] == $newFolder->get_folderid());

        // Delete and expunge documents
        $document->delete('Testing document move');
        $document->expunge();

        // Delete test folder
        $newFolder->delete('Testing document move');
    }

    /**
     * Tests the checkout, checkin, cancel checkout and is_checked_out document functionality
     */
    function testCheckinCheckout() {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));
        $document = $this->root->add_document('testtitle369', 'testname369.txt', 'Default', $randomFile);
        $this->assertNotError($document);

        if(PEAR::isError($document)) return;
        $this->assertTrue($document instanceof KTAPI_Document);
        @unlink($randomFile);
        $documentid = $document->get_documentid();

        // document should be checked in
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertFalse($document->is_checked_out());
        $document->checkout('Testing document checkout');

        // document should now be checked out
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertTrue($document->is_checked_out());
        $document->undo_checkout('Testing document cancel checkout');

        // document should be checked in
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertFalse($document->is_checked_out());
        $document->checkout('Testing document checkout');

        // document should now be checked out
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertTrue($document->is_checked_out());

        // create another random file
        $randomFile = APIDocumentHelper::createRandomFile('updating the previous content');
        $this->assertTrue(is_file($randomFile));
        $document->checkin('testname369.txt', 'Updating test checkin document', $randomFile);
        @unlink($randomFile);

        // document should be checked in
        $document = $this->ktapi->get_document_by_id($documentid);
        $this->assertFalse($document->is_checked_out());
        $document->delete('Testing document checkin');
        $document->expunge();
    }

    /**
     * Test role allocation and permission allocation
     */
    function testPermissions()
    {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle', 'testname', 'Default', $randomFile);
        @unlink($randomFile);
        $this->assertNotError($document);
        if(PEAR::isError($document)) return;

        $permAllocation = $document->getPermissionAllocation();
        $this->assertNotNull($permAllocation);
        $this->assertEntity($permAllocation, KTAPI_PermissionAllocation);

        $roleAllocation = $document->getRoleAllocation();
        $this->assertNotNull($roleAllocation);
        $this->assertEntity($roleAllocation, KTAPI_RoleAllocation);

        $document->delete('Testing');
        $document->expunge();
    }

    /**
     * Tests the adding of a duplicate document title
     *
    function testAddingDuplicateTitle()
    {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));
        $document = $this->root->add_document('testtitle.txt', 'testname.txt', 'Default', $randomFile);
        $this->assertNotError($document);

        if(PEAR::isError($document)) return;
        $this->assertEntity($document, 'KTAPI_Document');
        $this->assertFalse(is_file($randomFile));
        $documentid = $document->get_documentid();

        // file would have been cleaned up because of the add_document
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        // filenames must be the same as above
        $document2 = $this->root->add_document('testtitle.txt', 'testname.txt', 'Default', $randomFile);
        $this->assertFalse($document2 instanceof KTAPI_Document);
        @unlink($randomFile);
        $document->delete('because we can');
        $document->expunge();
        if ($document2 instanceof KTAPI_Document) {
            $document2->delete('because we can');
            $document2->expunge();
        }
    }

    /**
     * Tests the adding of duplicate document file names
     *
    function testAddingDuplicateFile()
    {
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));
        $document = $this->root->add_document('testtitle.txt', 'testname.txt', 'Default', $randomFile);
        $this->assertNotError($document);

        if(PEAR::isError($document)) return;
        $this->assertTrue($document instanceof KTAPI_Document);
        $this->assertFalse(is_file($randomFile));
        $documentid = $document->get_documentid();
        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        // filenames must be the same as above
        $document2 = $this->root->add_document('testtitle2.txt', 'testname.txt', 'Default', $randomFile);
        $this->assertFalse($document2 instanceof KTAPI_Document);

        @unlink($randomFile);
        $document->delete('because we can');
        $document->expunge();
        if ($document2 instanceof KTAPI_Document) {
            $document2->delete('because we can');
            $document2->expunge();
        }
    }
    */

    /**
    * Method to test the document subscriptions for webservices
    *
    */
    public function testSubscriptions_KTAPI()
    {
        $this->ktapi->session_logout();
        $this->session = $this->ktapi->start_session('admin', 'admin');

        $randomFile = APIDocumentHelper::createRandomFile();
        $this->assertTrue(is_file($randomFile));

        $document = $this->root->add_document('testtitle.txt', 'testname.txt', 'Default', $randomFile);
        $this->assertIsA($document, 'KTAPI_Document');
        $this->assertNoErrors();

        @unlink($randomFile);
        $documentid = $document->get_documentid();

        // case no subscription
        $response = $this->ktapi->is_document_subscribed($documentid);
        $this->assertIsA($response, 'array');
        $this->assertEqual($response['results']['subscribed'], 'FALSE');
        $this->assertNoErrors();

        //case add subscription
        $response = $this->ktapi->subscribe_to_document($documentid);
        $this->assertIsA($response, 'array');
        $this->assertEqual($response['results']['action_result'], 'TRUE');
        $this->assertNoErrors();

        //case add DUPLICATE subscription
        $response = $this->ktapi->subscribe_to_document($documentid);
        $this->assertIsA($response, 'array');
        $this->assertEqual($response['results']['action_result'], 'TRUE');
        $this->assertNoErrors();

        // case subscription exists
        $response = $this->ktapi->is_document_subscribed($documentid);
        $this->assertIsA($response, 'array');
        $this->assertEqual($response['results']['subscribed'], 'TRUE');
        $this->assertNoErrors();

        //case delete subscription
        $response = $this->ktapi->unsubscribe_from_document($documentid);
        $this->assertIsA($response, 'array');
        $this->assertEqual($response['results']['action_result'], 'TRUE');
        $this->assertNoErrors();

        //case delete NOT EXISTANT subscription
        $response = $this->ktapi->unsubscribe_from_document($documentid);
        $this->assertIsA($response, 'array');
        $this->assertEqual($response['results']['action_result'], 'TRUE');
        $this->assertNoErrors();

        $document->delete('Test');
        $document->expunge();
    }
}

?>