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