      1: <?php
      2:
      3: /**
      4:  *
      5:  * Test cases for the AlphaController class.
      6:  *
      7:  * @package alpha::tests
      8:  * @since 1.0
      9:  * @author John Collins <dev@alphaframework.org>
     10:  * @version $Id: AlphaController_Test.php 1596 2012-11-27 16:16:55Z alphadevx $
     11:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     12:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     13:  * All rights reserved.
     14:  *
     15:  * <pre>
     16:  * Redistribution and use in source and binary forms, with or
     17:  * without modification, are permitted provided that the
     18:  * following conditions are met:
     19:  *
     20:  * * Redistributions of source code must retain the above
     21:  *   copyright notice, this list of conditions and the
     22:  *   following disclaimer.
     23:  * * Redistributions in binary form must reproduce the above
     24:  *   copyright notice, this list of conditions and the
     25:  *   following disclaimer in the documentation and/or other
     26:  *   materials provided with the distribution.
     27:  * * Neither the name of the Alpha Framework nor the names
     28:  *   of its contributors may be used to endorse or promote
     29:  *   products derived from this software without specific
     30:  *   prior written permission.
     31:  *
     32:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     33:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     34:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     35:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     36:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     37:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     38:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     39:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     40:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     41:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     43:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     44:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     45:  * </pre>
     46:  *
     47:  */
     48: class AlphaController_Test extends PHPUnit_Framework_TestCase {
     49:     /**
     50:      * Sample controller for Testing with
     51:      *
     52:      * @var Search
     53:      * @since 1.0
     54:      */
     55:     private $controller;
     56:
     57:     /**
     58:      * An ArticleObject for Testing
     59:      *
     60:      * @var ArticleObject
     61:      * @since 1.0
     62:      */
     63:     private $article;
     64:
     65:     /**
     66:      * A PersonObject for Testing (any business object will do)
     67:      *
     68:      * @var PersonObject
     69:      * @since 1.0
     70:      */
     71:     private $person;
     72:
     73:     /**
     74:      * Test rights group
     75:      *
     76:      * @var RightsObject
     77:      * @since 1.0
     78:      */
     79:     private $group;
     80:
     81:     /**
     82:      * (non-PHPdoc)
     83:      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::setUp()
     84:      *
     85:      * @since 1.0
     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:      * (non-PHPdoc)
    120:      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::tearDown()
    121:      *
    122:      * @since 1.0
    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:      * Creates a person object for Testing
    154:      *
    155:      * @return PersonObject
    156:      * @since 1.0
    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:      * Creates an article object for Testing
    170:      *
    171:      * @return ArticleObject
    172:      * @since 1.0
    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:      * Testing that objects are being added to the dirtyObjects array correctly
    186:      *
    187:      * @since 1.0
    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:      * Testing that objects are being added to the dirtyObject array correctly
    199:      * and that this array is in the session being shared by controllers
    200:      *
    201:      * @since 1.0
    202:      */
    203:     public function testMarkDirtySession() {
    204:         $this->person->set('email', 'changed@test.com');
    205:         $this->controller->markDirty($this->person);
    206:
    207:         // calling the constructor of the other controller will check the session
    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:      * Testing that objects are being added to the newObjects array correctly
    217:      *
    218:      * @since 1.0
    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:      * Testing that objects are being added to the newObjects array correctly
    230:      * and that this array is in the session being shared by controllers
    231:      *
    232:      * @since 1.0
    233:      */
    234:     public function testMarkNewSession() {
    235:         $person = $this->createPersonObject('newuser');
    236:         $person->set('email', 'newuser@test.com');
    237:         $this->controller->markNew($person);
    238:
    239:         // calling the constructor of the other controller will check the session
    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:      * test cases to see if access rights on controllers are working as expected
    249:      *
    250:      * @since 1.0
    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:      * test the getUnitDuration method for equality
    276:      *
    277:      * @since 1.0
    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:      * Test the getUnitDuration method for greater than
    290:      *
    291:      * @since 1.0
    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:      * Testing the setUnitOfWork method with a bad controller name
    304:      *
    305:      * @since 1.0
    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:      * Testing the setUnitOfWork method and getNextJob
    318:      *
    319:      * @since 1.0
    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:      * Testing the setUnitOfWork method and getFirstJob
    330:      *
    331:      * @since 1.0
    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:      * Testing the setUnitOfWork method and getPreviousJob
    342:      *
    343:      * @since 1.0
    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:      * Testing the setUnitOfWork method and getLastJob
    354:      *
    355:      * @since 1.0
    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:      * Testing the commit method for new and dirty objects
    366:      *
    367:      * @since 1.0
    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:      * Testing that we can load dirty and new objects post commit
    386:      *
    387:      * @since 1.0
    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:      * Testing that aborting a unit of work clears the list of new objects
    420:      *
    421:      * @since 1.0
    422:      */
    423:     public function testAbort() {
    424:         $person = $this->createPersonObject('newuser');
    425:         $person->set('email', 'newuser@test.com');
    426:         $this->controller->markNew($person);
    427:
    428:         // calling the constructor of the other controller will check the session
    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:         // now abort the unit of work from the second controller, and confirm that the new object array is empty
    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:      * Testing that the AlphaController constructor uses the controller name as the AlphaController->name (job) of the controller
    445:      *
    446:      * @since 1.0
    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:      * Testing that providing a bad BO name returns null
    454:      *
    455:      * @since 1.0
    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:      * Testing the checkRights method with various account types
    463:      *
    464:      * @since 1.0
    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:      * Testing the checkSecurityFields method
    480:      *
    481:      * @since 1.0
    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:      * Testing that a bad controller name passed to loadControllerDef will cause an exception
    499:      *
    500:      * @since 1.0
    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:      * Testing that status messages can be shared between controllers via the session
    513:      *
    514:      * @since 1.0
    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:      * Testing that a BO attached to a controller that contains tags will have those tags mapped to the controller's keywords
    526:      *
    527:      * @since 1.0
    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:      * Testing the checkControllerDefExists method with good and bad input
    552:      *
    553:      * @since 1.2.1
    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:      * Testing the checkIfAccessingFromSecureURL method with good and bad input
    564:      *
    565:      * @since 1.2.1
    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: ?>