1: <?php
  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: class AlphaController_Test extends PHPUnit_Framework_TestCase {
 49:      50:  51:  52:  53:  54: 
 55:     private $controller;
 56: 
 57:      58:  59:  60:  61:  62: 
 63:     private $article;
 64: 
 65:      66:  67:  68:  69:  70: 
 71:     private $person;
 72: 
 73:      74:  75:  76:  77:  78: 
 79:     private $group;
 80: 
 81:      82:  83:  84:  85:  86: 
 87:     protected function setUp() {
 88:         $tag = new TagObject();
 89:         $tag->rebuildTable();
 90: 
 91:         $denum = new DEnum();
 92:         $denum->rebuildTable();
 93: 
 94:         $item = new DEnumItem();
 95:         $item->rebuildTable();
 96: 
 97:         $article = new ArticleObject();
 98:         $article->rebuildTable();
 99: 
100:         $this->controller = new Search();
101: 
102:         $this->person = $this->createPersonObject('unitTestUser');
103:         $this->person->rebuildTable();
104: 
105:         $this->article = $this->createArticleObject('unitTestArticle');
106:         $this->article->rebuildTable();
107: 
108:         $this->group = new RightsObject();
109:         $this->group->rebuildTable();
110:         $this->group->set('name', 'Admin');
111:         $this->group->save();
112: 
113:         $lookup = $this->group->getMembers()->getLookup();
114:         $lookup->setValue(array($_SESSION['currentUser']->getOID(), $this->group->getOID()));
115:         $lookup->save();
116:     }
117: 
118:     119: 120: 121: 122: 123: 
124:     protected function tearDown() {
125:         $this->controller->abort();
126: 
127:         $this->article->dropTable();
128:         unset($this->article);
129: 
130:         unset($this->controller);
131: 
132:         $this->person->dropTable();
133:         unset($this->person);
134: 
135:         $this->group->dropTable();
136:         $this->group->dropTable('Person2Rights');
137:         unset($this->group);
138: 
139:         $article = new ArticleObject();
140:         $article->dropTable();
141: 
142:         $tag = new TagObject();
143:         $tag->dropTable();
144: 
145:         $denum = new DEnum();
146:         $denum->dropTable();
147: 
148:         $item = new DEnumItem();
149:         $item->dropTable();
150:     }
151: 
152:     153: 154: 155: 156: 157: 
158:     private function createPersonObject($name) {
159:         $person = new PersonObject();
160:         $person->setDisplayname($name);
161:         $person->set('email', $name.'@test.com');
162:         $person->set('password', 'passwordTest');
163:         $person->set('URL', 'http://unitTestUser/');
164: 
165:         return $person;
166:     }
167: 
168:     169: 170: 171: 172: 173: 
174:     private function createArticleObject($name) {
175:         $article = new ArticleObject();
176:         $article->set('title', $name);
177:         $article->set('description', 'unitTestArticleTagOne unitTestArticleTagTwo');
178:         $article->set('author', 'unitTestArticleTagOne');
179:         $article->set('content', 'unitTestArticleTagOne');
180: 
181:         return $article;
182:     }
183: 
184:     185: 186: 187: 188: 
189:     public function testMarkDirtyAdd() {
190:         $this->controller->markDirty($this->person);
191: 
192:         $dirtyObjects = $this->controller->getDirtyObjects();
193: 
194:         $this->assertEquals('http://unitTestUser/', $dirtyObjects[0]->get('URL'), 'Testing that objects are being added to the dirtyObject array correctly');   
195:     }
196: 
197:     198: 199: 200: 201: 202: 
203:     public function testMarkDirtySession() {
204:         $this->person->set('email', 'changed@test.com');
205:         $this->controller->markDirty($this->person);
206: 
207:         
208:         $controller2 = new Search();
209: 
210:         $dirty = $controller2->getDirtyObjects();
211: 
212:         $this->assertEquals('changed@test.com', $dirty[0]->get('email'), 'Testing that objects are being added to the dirtyObject array correctly and that this array is in the session being shared by controllers');  
213:     }
214: 
215:     216: 217: 218: 219: 
220:     public function testMarkNewAdd() {
221:         $this->controller->markNew($this->person);
222: 
223:         $newObjects = $this->controller->getNewObjects();
224: 
225:         $this->assertEquals('http://unitTestUser/', $newObjects[0]->get('URL'), 'Testing that objects are being added to the newObject array correctly');   
226:     }
227: 
228:     229: 230: 231: 232: 233: 
234:     public function testMarkNewSession() {
235:         $person = $this->createPersonObject('newuser');
236:         $person->set('email', 'newuser@test.com');
237:         $this->controller->markNew($person);
238: 
239:         
240:         $controller2 = new Search();
241: 
242:         $new = $controller2->getNewObjects();
243: 
244:         $this->assertEquals('newuser@test.com', $new[0]->get('email'), 'Testing that objects are being added to the newObjects array correctly and that this array is in the session being shared by controllers'); 
245:     }
246: 
247:     248: 249: 250: 251: 
252:     public function testRightsAccess() {
253:         $this->group->set('name', 'testgroup');
254:         $this->group->save();
255: 
256:         $this->person->save();
257: 
258:         $lookup = $this->person->getPropObject('rights')->getLookup();
259:         $lookup->setValue(array($this->person->getOID(), $this->group->getOID()));
260:         $lookup->save();
261: 
262:         $admin = $_SESSION['currentUser'];
263:         $_SESSION['currentUser'] = $this->person;
264: 
265:         try {
266:             $controller = new Search('testgroup');
267:         }catch (PHPException $e) {
268:             $this->fail('failed to access a controller that I have access to by rights group membership');
269:         }
270: 
271:         $_SESSION['currentUser'] = $admin;
272:     }
273: 
274:     275: 276: 277: 278: 
279:     public function testGetUnitDurationEqual() {
280:         $controller1 = new Search();
281:         $controller2 = new Search();
282:         $controller1->setUnitEndTime(2005, 10, 30, 21, 15, 15);
283:         $controller2->setUnitEndTime(2005, 10, 30, 21, 15, 15);
284: 
285:         $this->assertEquals($controller1->getUnitDuration(), $controller2->getUnitDuration(), 'test the getUnitDuration method for equality');
286:     }
287: 
288:     289: 290: 291: 292: 
293:     public function testGetUnitDurationGreater() {
294:         $controller1 = new Search();
295:         $controller2 = new Search();
296:         $controller1->setUnitEndTime(2006, 10, 30, 21, 15, 15);
297:         $controller2->setUnitEndTime(2005, 10, 30, 21, 15, 15);
298: 
299:         $this->assertTrue($controller1->getUnitDuration() > $controller2->getUnitDuration(), 'Test the getUnitDuration method for greater than');
300:     }
301: 
302:     303: 304: 305: 306: 
307:     public function testSetUnitOfWorkBadControllerName() {
308:         try {
309:             $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','BadControllerName'));
310:             $this->fail('Passed a bad controller name BadControllerName to setUnitOfWork() and did not get the expected exception!');
311:         }catch (IllegalArguementException $e) {
312:             $this->assertEquals('', $this->controller->getFirstJob(), 'Testing the setUnitOfWork method with a bad controller name');
313:         }
314:     }
315: 
316:     317: 318: 319: 320: 
321:     public function testSetUnitOfWorkNext() {
322:         $this->controller->setName('Search');
323:         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
324: 
325:         $this->assertEquals('Edit', $this->controller->getNextJob(), 'Testing the setUnitOfWork method and getNextJob');
326:     }
327: 
328:     329: 330: 331: 332: 
333:     public function testSetUnitOfWorkFirst() {
334:         $this->controller->setName('ListAll');
335:         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
336: 
337:         $this->assertEquals('Search', $this->controller->getFirstJob(), 'Testing the setUnitOfWork method and getFirstJob');
338:     }
339: 
340:     341: 342: 343: 344: 
345:     public function testSetUnitOfWorkPrevious() {
346:         $this->controller->setName('ListAll');
347:         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
348: 
349:         $this->assertEquals('Create', $this->controller->getPreviousJob(), 'Testing the setUnitOfWork method and getPreviousJob');
350:     }
351: 
352:     353: 354: 355: 356: 
357:     public function testSetUnitOfWorkLast() {
358:         $this->controller->setName('ListAll');
359:         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
360: 
361:         $this->assertEquals('Detail', $this->controller->getLastJob(), 'Testing the setUnitOfWork method and getLastJob');
362:     }
363: 
364:     365: 366: 367: 368: 
369:     public function testCommit() {
370:         $this->person->set('email', 'changed@test.com');
371:         $this->controller->markDirty($this->person);
372: 
373:         $person = $this->createPersonObject('newuser');
374:         $person->set('email', 'newuser@test.com');
375:         $this->controller->markNew($person);
376: 
377:         try {
378:             $this->controller->commit();
379:         }catch (FailedUnitCommitException $e) {
380:             $this->fail('Failed to commit the unit of work transaction for new and dirty objects');
381:         }
382:     }
383: 
384:     385: 386: 387: 388: 
389:     public function testPostCommitLoad() {
390:         $this->person->set('email', 'changed@test.com');
391:         $this->controller->markDirty($this->person);
392: 
393:         $person = $this->createPersonObject('newuser');
394:         $person->set('email', 'newuser@test.com');
395:         $this->controller->markNew($person);
396: 
397:         try {
398:             $this->controller->commit();
399:         }catch (FailedUnitCommitException $e) {
400:             $this->fail('Failed to commit the unit of work transaction for new and dirty objects');
401:         }
402: 
403:         $newPerson = new PersonObject();
404:         try {
405:             $newPerson->loadByAttribute('email', 'newuser@test.com');
406:         }catch (BONotFoundException $e) {
407:             $this->fail('Failed to load the new person that we commited in the unit of work');
408:         }
409: 
410:         $dirtyPerson = new PersonObject();
411:         try {
412:             $dirtyPerson->loadByAttribute('email', 'changed@test.com');
413:         }catch (BONotFoundException $e) {
414:             $this->fail('Failed to load the dirty person that we commited in the unit of work');
415:         }
416:     }
417: 
418:     419: 420: 421: 422: 
423:     public function testAbort() {
424:         $person = $this->createPersonObject('newuser');
425:         $person->set('email', 'newuser@test.com');
426:         $this->controller->markNew($person);
427: 
428:         
429:         $controller2 = new Search();
430: 
431:         $new = $controller2->getNewObjects();
432: 
433:         $this->assertEquals('newuser@test.com', $new[0]->get('email'), 'Testing that objects are being added to the newObjects array correctly and that this array is in the session being shared by controllers');
434: 
435:         
436:         $controller2->abort();
437: 
438:         $new = $controller2->getNewObjects();
439: 
440:         $this->assertEquals(0, count($new), 'Testing that aborting a unit of work clears the list of new objects');
441:     }
442: 
443:     444: 445: 446: 447: 
448:     public function testConstructorJobControllerName() {
449:         $this->assertEquals('Search', $this->controller->getName(), 'Testing that the AlphaController constructor defaults to using the controller name as the AlphaController->name of the controller');
450:     }
451: 
452:     453: 454: 455: 456: 
457:     public function testGetCustomControllerName() {
458:         $this->assertNull(AlphaController::getCustomControllerName('DoesNotExistObject', 'view'), 'Testing that providing a bad BO name returns null');
459:     }
460: 
461:     462: 463: 464: 465: 
466:     public function testCheckRights() {
467:         $controller = new Search('Admin');
468:         $admin = $_SESSION['currentUser'];
469:         $_SESSION['currentUser'] = null;
470: 
471:         $this->assertFalse($controller->checkRights(), 'Testing that a user with no session cannot access an Admin controller');
472:         $controller = new Search('Public');
473:         $this->assertTrue($controller->checkRights(), 'Testing that a user with no session can access a Public controller');
474: 
475:         $_SESSION['currentUser'] = $admin;
476:     }
477: 
478:     479: 480: 481: 482: 
483:     public function testCheckSecurityFields() {
484:         $securityFields = AlphaController::generateSecurityFields();
485: 
486:         $_REQUEST['var1'] = $securityFields[0];
487:         $_REQUEST['var2'] = $securityFields[1];
488: 
489:         $this->assertTrue(AlphaController::checkSecurityFields(), 'Testing the checkSecurityFields method with valid security params');
490: 
491:         $_REQUEST['var1'] = null;
492:         $_REQUEST['var2'] = null;
493: 
494:         $this->assertFalse(AlphaController::checkSecurityFields(), 'Testing the checkSecurityFields method with invalid security params');
495:     }
496: 
497:     498: 499: 500: 501: 
502:     public function testLoadControllerDef() {
503:         try {
504:             $this->controller->loadControllerDef('DoesNotExist');
505:             $this->fail('Testing that a bad controller name passed to loadControllerDef will cause an exception');
506:         }catch (IllegalArguementException $e) {
507:             $this->assertEquals('The class [DoesNotExist] is not defined anywhere!', $e->getMessage(), 'Testing that a bad controller name passed to loadControllerDef will cause an exception');
508:         }
509:     }
510: 
511:     512: 513: 514: 515: 
516:     public function testStatusMessages() {
517:         $this->controller->setStatusMessage('test message');
518: 
519:         $controller = new Search();
520: 
521:         $this->assertEquals('test message', $controller->getStatusMessage(), 'Testing that status messages can be shared between controllers via the session');
522:     }
523: 
524:     525: 526: 527: 528: 
529:     public function testTagsMapToMetaKeywords() {
530:         AlphaDAO::begin();
531:         $this->article->save();
532:         AlphaDAO::commit();
533:         $tags = $this->article->getPropObject('tags')->getRelatedObjects();
534: 
535:         $found = false;
536:         foreach($tags as $tag) {
537:             if($tag->get('content') == 'unittestarticle') {
538:                 $found = true;
539:                 break;
540:             }
541:         }
542:         $this->assertTrue($found, 'Testing the TagObject::tokenize method returns a tag called "unittestarticle"');
543: 
544:         $this->controller->setBO($this->article);
545: 
546:         $this->assertEquals('unittestarticle,unittestarticletagone,unittestarticletagtwo', $this->controller->getKeywords(), 'Testing that a BO attached to a controller that contains tags will have those tags mapped to the controller\'s keywords');
547: 
548:     }
549: 
550:     551: 552: 553: 554: 
555:     public function testCheckControllerDefExists() {
556: 
557:         $this->assertTrue(AlphaController::checkControllerDefExists('/'), 'Testing that the / controller always exists');
558:         $this->assertTrue(AlphaController::checkControllerDefExists('Search'), 'Testing that a good controller classname returns true');
559:         $this->assertFalse(AlphaController::checkControllerDefExists('DoesNotExist'), 'Testing that a bad controller classname returns false');
560:     }
561: 
562:     563: 564: 565: 566: 
567:     public function testCheckIfAccessingFromSecureURL() {
568: 
569:         global $config;
570: 
571:         $oldToken = $_GET['tk'];
572:         $oldRequestURI = $_SERVER['REQUEST_URI'];
573: 
574:         $_GET['tk'] = null;
575:         $_SERVER['REQUEST_URI'] = '/search';
576: 
577:         $this->assertFalse(AlphaController::checkIfAccessingFromSecureURL(), 'Testing that the false is returned when tk is unavailable');
578: 
579:         $_GET['tk'] = '8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==';
580: 
581:         $this->assertTrue(AlphaController::checkIfAccessingFromSecureURL(), 'Testing that the true is returned when tk is set in global _GET array');
582: 
583:         $_GET['tk'] = null;
584:         $_SERVER['REQUEST_URI'] = $config->get('app.url').'tk/8kqoeebEej0V-FN5-DOdA1HBDDieFcNWTib2yLSUNjq0B0FWzAupIA==';
585: 
586:         $this->assertTrue(AlphaController::checkIfAccessingFromSecureURL(), 'Testing that the true is returned when tk is part of the mod_rewrite style URL');
587: 
588:         $_GET['tk'] = $oldToken;
589:         $_SERVER['REQUEST_URI'] = $oldRequestURI;
590:     }
591: }
592: 
593: ?>