Commit 52555411ac456356e7fe55e1b7335787e77ecee0
1 parent
a21056f1
Unit tests for ktapi
Committed By: Kevin Cyster Reviewed By: Megan Watson git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@9725 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
505 additions
and
0 deletions
tests/api/testApi.php
0 → 100644
| 1 | +<?php | |
| 2 | +require_once (dirname(__FILE__) . '/../test.php'); | |
| 3 | +require_once (KT_DIR . '/ktapi/ktapi.inc.php'); | |
| 4 | + | |
| 5 | +/** | |
| 6 | +* This class creates a random file to test the object/permissions access | |
| 7 | +* | |
| 8 | +*/ | |
| 9 | +class APIDocumentHelper { | |
| 10 | + function createRandomFile($content = 'this is some text') { | |
| 11 | + $temp = tempnam(dirname(__FILE__), 'myfile'); | |
| 12 | + $fp = fopen($temp, 'wt'); | |
| 13 | + fwrite($fp, $content); | |
| 14 | + fclose($fp); | |
| 15 | + return $temp; | |
| 16 | + } | |
| 17 | +} | |
| 18 | + | |
| 19 | +/** | |
| 20 | +* These are the unit tests for the main KnowledgeTree API class | |
| 21 | +* | |
| 22 | +*/ | |
| 23 | +class APITestCase extends KTUnitTestCase { | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * @var object $ktapi The main ktapi object | |
| 27 | + */ | |
| 28 | + var $ktapi; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @var object $session The KT session object | |
| 32 | + */ | |
| 33 | + var $session; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * @var object $root The KT folder object | |
| 37 | + */ | |
| 38 | + var $root; | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * This method sets up the KT session | |
| 42 | + * | |
| 43 | + */ | |
| 44 | + public function setUp() { | |
| 45 | + $this->ktapi = new KTAPI(); | |
| 46 | + $this->session = $this->ktapi->start_session('admin', 'admin'); | |
| 47 | + $this->root = $this->ktapi->get_root_folder(); | |
| 48 | + $this->assertTrue($this->root instanceof KTAPI_Folder); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * This method emds the KT session | |
| 53 | + * | |
| 54 | + */ | |
| 55 | + public function tearDown() { | |
| 56 | + $this->session->logout(); | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * This method tests for the session object | |
| 61 | + * | |
| 62 | + */ | |
| 63 | + public function testGetSession() | |
| 64 | + { | |
| 65 | + $session = $this->ktapi->get_session(); | |
| 66 | + | |
| 67 | + $this->assertNotNull($session); | |
| 68 | + $this->assertIsA($session, 'KTAPI_Session'); | |
| 69 | + $this->assertNoErrors(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * This method tests for the user object | |
| 74 | + * | |
| 75 | + */ | |
| 76 | + public function testGetUser() | |
| 77 | + { | |
| 78 | + $user = $this->ktapi->get_user(); | |
| 79 | + | |
| 80 | + $this->assertNotNull($user); | |
| 81 | + $this->assertIsA($user, 'User'); | |
| 82 | + $this->assertNoErrors(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * This method tests for the permission object | |
| 87 | + * | |
| 88 | + */ | |
| 89 | + public function testGetPermission() | |
| 90 | + { | |
| 91 | + // test case 1 | |
| 92 | + // the permissions string | |
| 93 | + $permission = 'ktcore.permissions.read'; | |
| 94 | + | |
| 95 | + $permissions = $this->ktapi->get_permission($permission); | |
| 96 | + | |
| 97 | + $this->assertNotNull($permissions); | |
| 98 | + $this->assertIsA($permissions, 'KTPermission'); | |
| 99 | + $this->assertNoErrors(); | |
| 100 | + | |
| 101 | + // test case 2 | |
| 102 | + // the permissions string | |
| 103 | + $permission = 'ktcore.permissions.write'; | |
| 104 | + | |
| 105 | + $permissions = $this->ktapi->get_permission($permission); | |
| 106 | + | |
| 107 | + $this->assertNotNull($permissions); | |
| 108 | + $this->assertIsA($permissions, 'KTPermission'); | |
| 109 | + $this->assertNoErrors(); | |
| 110 | + | |
| 111 | + // test case 3 | |
| 112 | + // the permissions string | |
| 113 | + $permission = 'ktcore.permissions.security'; | |
| 114 | + | |
| 115 | + $permissions = $this->ktapi->get_permission($permission); | |
| 116 | + | |
| 117 | + $this->assertNotNull($permissions); | |
| 118 | + $this->assertIsA($permissions, 'KTPermission'); | |
| 119 | + $this->assertNoErrors(); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * This method tests if a user can access an object with certain permssions | |
| 124 | + * | |
| 125 | + */ | |
| 126 | + public function testCheckAccess() | |
| 127 | + { | |
| 128 | + // test case 1 - normal test | |
| 129 | + // the permission string | |
| 130 | + $permission = 'ktcore.permissions.read'; | |
| 131 | + | |
| 132 | + // create the document object | |
| 133 | + $randomFile = APIDocumentHelper::createRandomFile(); | |
| 134 | + $document = $this->root->add_document('title_1.txt', 'name_1.txt', 'Default', $randomFile); | |
| 135 | + $internalDocObject = $document->document; | |
| 136 | + | |
| 137 | + $user = $this->ktapi->can_user_access_object_requiring_permission($internalDocObject, $permission); | |
| 138 | + | |
| 139 | + $this->assertNotNull($user); | |
| 140 | + $this->assertIsA($user, 'User'); | |
| 141 | + $this->assertNoErrors(); | |
| 142 | + @unlink($randomFile); | |
| 143 | + | |
| 144 | + // test case 2 - test for bad permissions string | |
| 145 | + $permission = 'ktcore.permissions.badstring'; | |
| 146 | + | |
| 147 | + // create the document object | |
| 148 | + $randomFile = APIDocumentHelper::createRandomFile(); | |
| 149 | + $document = $this->root->add_document('title_2.txt', 'name_2.txt', 'Default', $randomFile); | |
| 150 | + | |
| 151 | + $user = $this->ktapi->can_user_access_object_requiring_permission($document, $permission); | |
| 152 | + | |
| 153 | + $this->assertNotNull($user); | |
| 154 | + $this->assertEqual($user, PEAR::isError($user)); | |
| 155 | + $this->assertNoErrors(); | |
| 156 | + @unlink($randomFile); | |
| 157 | + | |
| 158 | + | |
| 159 | + /* | |
| 160 | + // test case 3 - test for incorect permissions | |
| 161 | + $permission = 'ktcore.permissions.read'; | |
| 162 | + | |
| 163 | + // create the document object | |
| 164 | + $randomFile = APIDocumentHelper::createRandomFile(); | |
| 165 | + $document = $this->root->add_document('title_3.txt', 'name_3.txt', 'Default', $randomFile); | |
| 166 | + | |
| 167 | + $user = $this->ktapi->can_user_access_object_requiring_permission($document, $permission); | |
| 168 | + | |
| 169 | + $this->assertNotNull($user); | |
| 170 | + $this->assertEqual($user, PEAR::isError($user)); | |
| 171 | + $this->assertNoErrors(); | |
| 172 | + @unlink($randomFile); | |
| 173 | + */ | |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * This method tests the retrieval of a document by its oem number | |
| 178 | + * | |
| 179 | + * | |
| 180 | + public function testGetDocByOem() | |
| 181 | + { | |
| 182 | + // test case 1 - no matching oem numbers | |
| 183 | + // create the document object | |
| 184 | + $randomFile = APIDocumentHelper::createRandomFile(); | |
| 185 | + $document = $this->root->add_document('title_4.txt', 'name_4.txt', 'Default', $randomFile); | |
| 186 | + | |
| 187 | + $list = $this->ktapi->get_documents_by_oem_no('1'); | |
| 188 | + | |
| 189 | + $this->assertTrue(empty($list)); | |
| 190 | + $this->assertNoErrors(); | |
| 191 | + @unlink($randomFile); | |
| 192 | + | |
| 193 | + // test case 2 - matching oem numbers | |
| 194 | + // create the document object | |
| 195 | + $randomFile = APIDocumentHelper::createRandomFile(); | |
| 196 | + $document = $this->root->add_document('title_5.txt', 'name_5.txt', 'Default', $randomFile); | |
| 197 | + | |
| 198 | + $list = $this->ktapi->get_documents_by_oem_no('2'); | |
| 199 | + | |
| 200 | + $this->assertFalse(empty($list)); | |
| 201 | + $this->assertNoErrors(); | |
| 202 | + @unlink($randomFile); | |
| 203 | + } | |
| 204 | + | |
| 205 | + /** | |
| 206 | + * This method tests for the current session | |
| 207 | + * | |
| 208 | + * | |
| 209 | + public function testGetActiveSession() | |
| 210 | + { | |
| 211 | + // get session id of active session | |
| 212 | + $sessionID = $this->session->get_sessionid(); | |
| 213 | + | |
| 214 | + $session = KTAPI::get_active_session($sessionID); | |
| 215 | + $this->assertNotNull($session); | |
| 216 | + $this->assertIsA($session, 'KTAPI_Session'); | |
| 217 | + $this->assertNoErrors(); | |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * This method tests the creation of a session | |
| 222 | + * | |
| 223 | + * | |
| 224 | + public function testStartSession() | |
| 225 | + { | |
| 226 | + $this->session->logout(); | |
| 227 | + $this->session = NULL; | |
| 228 | + | |
| 229 | + $this->session = $this->ktapi->start_session('admin', 'admin'); | |
| 230 | + | |
| 231 | + $this->assertNotNull($this->session); | |
| 232 | + $this->assertIsA($this->session, 'KTAPI_Session'); | |
| 233 | + $this->assertNoErrors(); | |
| 234 | + die(); | |
| 235 | + } | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * This method tests the creation of a root session | |
| 239 | + * | |
| 240 | + * | |
| 241 | + public function testStartSystemSession() | |
| 242 | + { | |
| 243 | + $session = $this->ktapi->start_system_session(); | |
| 244 | + | |
| 245 | + $this->assertNotNull($session); | |
| 246 | + $this->assertIsA($session, 'KTAPI_Session'); | |
| 247 | + $this->assertNoErrors(); | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * This method tests the creation of an anonymous session | |
| 252 | + * | |
| 253 | + * | |
| 254 | + public function testStartAnonymousSession() | |
| 255 | + { | |
| 256 | + $session = $this->ktapi->start_anonymous_session(); | |
| 257 | + | |
| 258 | + $this->assertNotNull($session); | |
| 259 | + $this->assertIsA($session, 'KTAPI_Session'); | |
| 260 | + $this->assertNoErrors(); | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * This method tests the retrieval of the root folder | |
| 265 | + * | |
| 266 | + */ | |
| 267 | + public function testGetRootFolder() | |
| 268 | + { | |
| 269 | + $folder = $this->ktapi->get_root_folder(); | |
| 270 | + | |
| 271 | + $this->assertNotNull($folder); | |
| 272 | + $this->assertIsA($folder, 'KTAPI_Folder'); | |
| 273 | + $this->assertNoErrors(); | |
| 274 | + } | |
| 275 | + | |
| 276 | + /** | |
| 277 | + * This method tests the retrieval of a folder by id | |
| 278 | + * | |
| 279 | + */ | |
| 280 | + public function testGetFolderById() | |
| 281 | + { | |
| 282 | + $folder = $this->ktapi->get_folder_by_id(1); | |
| 283 | + | |
| 284 | + $this->assertNotNull($folder); | |
| 285 | + $this->assertIsA($folder, 'KTAPI_Folder'); | |
| 286 | + $this->assertNoErrors(); | |
| 287 | + } | |
| 288 | + | |
| 289 | + /** | |
| 290 | + * This method tests the retrieval of a folder by name | |
| 291 | + * | |
| 292 | + */ | |
| 293 | + public function testGetFolderByName() | |
| 294 | + { | |
| 295 | + $folder = $this->ktapi->get_folder_by_name('Root Folder'); | |
| 296 | + | |
| 297 | + $this->assertNotNull($folder); | |
| 298 | + $this->assertIsA($folder, 'KTAPI_Folder'); | |
| 299 | + $this->assertNoErrors(); | |
| 300 | + } | |
| 301 | + | |
| 302 | + /** | |
| 303 | + * This method tests the retrieval of a document by it's id | |
| 304 | + * | |
| 305 | + */ | |
| 306 | + public function testGetDocumentById() | |
| 307 | + { | |
| 308 | + // create the document object | |
| 309 | + $randomFile = APIDocumentHelper::createRandomFile(); | |
| 310 | + $document = $this->root->add_document('title_5.txt', 'name_5.txt', 'Default', $randomFile); | |
| 311 | + | |
| 312 | + $documentID = $document->get_documentid(); | |
| 313 | + | |
| 314 | + $docObject = $this->ktapi->get_document_by_id($documentID); | |
| 315 | + | |
| 316 | + $this->assertNotNull($docObject); | |
| 317 | + $this->assertIsA($docObject, 'KTAPI_Document'); | |
| 318 | + $this->assertNoErrors(); | |
| 319 | + @unlink($randomFile); | |
| 320 | + } | |
| 321 | + | |
| 322 | + /** | |
| 323 | + * This method tests the retrieval of a document type id based on the type name | |
| 324 | + * | |
| 325 | + */ | |
| 326 | + public function testGetDocumentTypeid() | |
| 327 | + { | |
| 328 | + $typeID = $this->ktapi->get_documenttypeid('Default'); | |
| 329 | + | |
| 330 | + $this->assertNotNull($typeID); | |
| 331 | + $this->assertNoErrors(); | |
| 332 | + } | |
| 333 | + | |
| 334 | + /** | |
| 335 | + * This method tests the retrieval of a link type id based on the link type name | |
| 336 | + * | |
| 337 | + */ | |
| 338 | + public function testGetLinkTypeid() | |
| 339 | + { | |
| 340 | + $typeID = $this->ktapi->get_link_type_id('Default'); | |
| 341 | + | |
| 342 | + $this->assertNotNull($typeID); | |
| 343 | + $this->assertNoErrors(); | |
| 344 | + } | |
| 345 | + | |
| 346 | + /** | |
| 347 | + * This method tests the retrieval of document types | |
| 348 | + * | |
| 349 | + */ | |
| 350 | + public function testGetDocTypes() | |
| 351 | + { | |
| 352 | + $types = $this->ktapi->get_documenttypes(); | |
| 353 | + | |
| 354 | + $this->assertNotNull($types); | |
| 355 | + $this->assertNoErrors(); | |
| 356 | + } | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * This method tests the retrieval of Link types | |
| 360 | + * | |
| 361 | + */ | |
| 362 | + public function testGetLinkTypes() | |
| 363 | + { | |
| 364 | + $types = $this->ktapi->get_document_link_types(); | |
| 365 | + | |
| 366 | + $this->assertNotNull($types); | |
| 367 | + $this->assertNoErrors(); | |
| 368 | + } | |
| 369 | + | |
| 370 | + /** | |
| 371 | + * This method tests the retrieval of metadata fieldsets | |
| 372 | + * | |
| 373 | + */ | |
| 374 | + public function testGetTypeMetadata() | |
| 375 | + { | |
| 376 | + $fieldsets = $this->ktapi->get_document_type_metadata(); | |
| 377 | + | |
| 378 | + $this->assertNotNull($fieldsets); | |
| 379 | + $this->assertNoErrors(); | |
| 380 | + } | |
| 381 | + | |
| 382 | + /** | |
| 383 | + * This method tests the retrieval of users | |
| 384 | + * | |
| 385 | + */ | |
| 386 | + public function testGetUsers() | |
| 387 | + { | |
| 388 | + $users = $this->ktapi->get_users(); | |
| 389 | + | |
| 390 | + $this->assertNotNull($users); | |
| 391 | + $this->assertNoErrors(); | |
| 392 | + } | |
| 393 | + | |
| 394 | + /** | |
| 395 | + * This method tests the retrieval of metadata based on the document field id | |
| 396 | + * | |
| 397 | + */ | |
| 398 | + public function testGetMetadataLookup() | |
| 399 | + { | |
| 400 | + $name = $this->ktapi->get_metadata_lookup(4); | |
| 401 | + | |
| 402 | + $this->assertNotNull($name); | |
| 403 | + $this->assertNoErrors(); | |
| 404 | + } | |
| 405 | + | |
| 406 | + /** | |
| 407 | + * This method tests the loading of a metadata tree on the document field id | |
| 408 | + * | |
| 409 | + */ | |
| 410 | + public function testGetMetadataTree() | |
| 411 | + { | |
| 412 | + $tree = $this->ktapi->get_metadata_tree(4); | |
| 413 | + | |
| 414 | + $this->assertNotNull($tree); | |
| 415 | + $this->assertNoErrors(); | |
| 416 | + } | |
| 417 | + | |
| 418 | + | |
| 419 | + /** | |
| 420 | + * This method tests the retrieval of active workflows | |
| 421 | + * | |
| 422 | + */ | |
| 423 | + public function testGetWorkflows() | |
| 424 | + { | |
| 425 | + $workflows = $this->ktapi->get_workflows(); | |
| 426 | + | |
| 427 | + $this->assertNotNull($workflows); | |
| 428 | + $this->assertNoErrors(); | |
| 429 | + } | |
| 430 | + | |
| 431 | + /** | |
| 432 | + * This method tests the creation of the saved search | |
| 433 | + * | |
| 434 | + */ | |
| 435 | + public function testCreate() | |
| 436 | + { | |
| 437 | + $searchID = $this->ktapi->create(rand(1,1000), '(GeneralText contains "title")'); | |
| 438 | + | |
| 439 | + $this->assertNotNull($searchID); | |
| 440 | + $this->assertNoErrors(); | |
| 441 | + } | |
| 442 | + | |
| 443 | + /** | |
| 444 | + * This method tests the retrieval for the saved search by it's id | |
| 445 | + * | |
| 446 | + */ | |
| 447 | + public function testGetSavedSearch() | |
| 448 | + { | |
| 449 | + $list = $this->ktapi->getList(); | |
| 450 | + | |
| 451 | + $searchID = $list[0]['id']; | |
| 452 | + $search = $this->ktapi->getSavedSearch($searchID); | |
| 453 | + | |
| 454 | + $this->assertNotNull($search); | |
| 455 | + $this->assertNoErrors(); | |
| 456 | + } | |
| 457 | + | |
| 458 | + /** | |
| 459 | + * This method tests the list of the saved search | |
| 460 | + * | |
| 461 | + */ | |
| 462 | + public function testList() | |
| 463 | + { | |
| 464 | + $list = $this->ktapi->getList(); | |
| 465 | + | |
| 466 | + $this->assertNotNull($list); | |
| 467 | + $this->assertNoErrors(); | |
| 468 | + } | |
| 469 | + | |
| 470 | + /** | |
| 471 | + * This method tests the deleting of the saved search | |
| 472 | + * | |
| 473 | + */ | |
| 474 | + public function testDelete() | |
| 475 | + { | |
| 476 | + $searchID = $this->ktapi->create(rand(1,1000), '(GeneralText contains "title")'); | |
| 477 | + $this->ktapi->delete($searchID); | |
| 478 | + $result = $this->ktapi->getSavedSearch($searchID); | |
| 479 | + | |
| 480 | + $this->assertTrue(empty($result)); | |
| 481 | + $this->assertEqual($result, PEAR::isError($result)); | |
| 482 | + $this->assertNoErrors(); | |
| 483 | + } | |
| 484 | + | |
| 485 | + /** | |
| 486 | + * This method tests the processing of the saved search | |
| 487 | + * | |
| 488 | + */ | |
| 489 | + public function testRunSavedSearch() | |
| 490 | + { | |
| 491 | + // create the document object | |
| 492 | + $randomFile = APIDocumentHelper::createRandomFile(); | |
| 493 | + $document = $this->root->add_document('title_1.txt', 'name_1.txt', 'Default', $randomFile); | |
| 494 | + | |
| 495 | + $searchID = $this->ktapi->create(rand(1,1000), '(GeneralText contains "title")'); | |
| 496 | + | |
| 497 | + $result = $this->ktapi->runSavedSearch($searchID); | |
| 498 | + | |
| 499 | + $this->assertNotNull($result); | |
| 500 | + $this->assertNotEqual($result, PEAR::isError($result)); | |
| 501 | + $this->assertNoErrors(); | |
| 502 | + @unlink($randomFile); | |
| 503 | + } | |
| 504 | +} | |
| 505 | +?> | |
| 0 | 506 | \ No newline at end of file | ... | ... |