Alpha Framework alpha--tests
[ class tree: alpha--tests ] [ index: alpha--tests ] [ all elements ]

Source for file AlphaController_Test.php

Documentation is available at AlphaController_Test.php

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/controller/Search.php';
  4. require_once $config->get('sysRoot').'alpha/model/PersonObject.inc';
  5. require_once $config->get('sysRoot').'alpha/model/ArticleObject.inc';
  6. require_once $config->get('sysRoot').'alpha/model/RightsObject.inc';
  7.  
  8. /**
  9.  *
  10.  * Test cases for the AlphaController class.
  11.  * 
  12.  * @package alpha::tests
  13.  * @since 1.0
  14.  * @author John Collins <dev@alphaframework.org>
  15.  * @version $Id: AlphaController_Test.php 1341 2011-03-17 15:02:02Z johnc $
  16.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  17.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  18.  *  All rights reserved.
  19.  * 
  20.  *  <pre>
  21.  *  Redistribution and use in source and binary forms, with or
  22.  *  without modification, are permitted provided that the
  23.  *  following conditions are met:
  24.  * 
  25.  *  * Redistributions of source code must retain the above
  26.  *    copyright notice, this list of conditions and the
  27.  *    following disclaimer.
  28.  *  * Redistributions in binary form must reproduce the above
  29.  *    copyright notice, this list of conditions and the
  30.  *    following disclaimer in the documentation and/or other
  31.  *    materials provided with the distribution.
  32.  *  * Neither the name of the Alpha Framework nor the names
  33.  *    of its contributors may be used to endorse or promote
  34.  *    products derived from this software without specific
  35.  *    prior written permission.
  36.  *   
  37.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  38.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  39.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  40.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  42.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  48.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  49.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50.  *  </pre>
  51.  *  
  52.  */
  53. class AlphaController_Test extends PHPUnit_Framework_TestCase {
  54.     /**
  55.      * Sample controller for Testing with
  56.      * 
  57.      * @var Search 
  58.      * @since 1.0
  59.      */
  60.     private $controller;
  61.     
  62.     /**
  63.      * An ArticleObject for Testing
  64.      * 
  65.      * @var ArticleObject 
  66.      * @since 1.0
  67.      */
  68.     private $article;
  69.     
  70.     /**
  71.      * A PersonObject for Testing (any business object will do)
  72.      * 
  73.      * @var PersonObject 
  74.      * @since 1.0
  75.      */
  76.     private $person;
  77.     
  78.     /**
  79.      * Test rights group
  80.      * 
  81.      * @var RightsObject 
  82.      * @since 1.0
  83.      */
  84.     private $group;
  85.     
  86.     /**
  87.      * (non-PHPdoc)
  88.      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::setUp()
  89.      * 
  90.      * @since 1.0
  91.      */
  92.     protected function setUp({
  93.         $tag new TagObject();
  94.         $tag->rebuildTable();
  95.         
  96.         $denum new DEnum();
  97.         $denum->rebuildTable();
  98.         
  99.         $item new DEnumItem();
  100.         $item->rebuildTable();
  101.         
  102.         $article new ArticleObject();
  103.         $article->rebuildTable();
  104.         
  105.         $this->controller new Search();
  106.         
  107.         $this->person $this->createPersonObject('unitTestUser');
  108.         $this->person->rebuildTable();
  109.         
  110.         $this->article $this->createArticleObject('unitTestArticle');
  111.         $this->article->rebuildTable();
  112.         
  113.         $this->group new RightsObject();
  114.         $this->group->rebuildTable();
  115.         $this->group->set('name''Admin');
  116.         $this->group->save();
  117.         
  118.         
  119.         $lookup $this->group->getMembers()->getLookup();
  120.         $lookup->setValue(array($_SESSION['currentUser']->getOID()$this->group->getOID()));
  121.         $lookup->save();
  122.     }
  123.     
  124.     /**
  125.      * (non-PHPdoc)
  126.      * @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::tearDown()
  127.      * 
  128.      * @since 1.0
  129.      */
  130.     protected function tearDown({
  131.         $this->controller->abort();
  132.         
  133.         $this->article->dropTable();
  134.         unset($this->article);
  135.         
  136.         unset($this->controller);
  137.         
  138.         $this->person->dropTable();
  139.         unset($this->person);
  140.         
  141.         $this->group->dropTable();
  142.         $this->group->dropTable('Person2Rights');
  143.         unset($this->group);
  144.         
  145.         $article new ArticleObject();
  146.         $article->dropTable();
  147.         
  148.         $tag new TagObject();
  149.         $tag->dropTable();
  150.         
  151.         $denum new DEnum();
  152.         $denum->dropTable();
  153.         
  154.         $item new DEnumItem();
  155.         $item->dropTable();
  156.     }
  157.     
  158.     /**
  159.      * Creates a person object for Testing
  160.      * 
  161.      * @return PersonObject 
  162.      * @since 1.0
  163.      */
  164.     private function createPersonObject($name{
  165.         $person new PersonObject();
  166.         $person->setDisplayname($name);        
  167.         $person->set('email'$name.'@test.com');
  168.         $person->set('password''passwordTest');
  169.         $person->set('URL''http://unitTestUser/');
  170.         
  171.         return $person;
  172.     }
  173.     
  174.     /**
  175.      * Creates an article object for Testing
  176.      * 
  177.      * @return ArticleObject 
  178.      * @since 1.0
  179.      */
  180.     private function createArticleObject($name{
  181.         $article new ArticleObject();
  182.         $article->set('title'$name);
  183.         $article->set('description''unitTestArticleTagOne unitTestArticleTagTwo');
  184.         $article->set('author''unitTestArticleTagOne');
  185.         $article->set('content''unitTestArticleTagOne');        
  186.         
  187.         return $article;
  188.     }
  189.     
  190.     /**
  191.      * Testing that objects are being added to the dirtyObjects array correctly
  192.      * 
  193.      * @since 1.0
  194.      */
  195.     public function testMarkDirtyAdd({
  196.         $this->controller->markDirty($this->person);
  197.         
  198.         $dirtyObjects $this->controller->getDirtyObjects();
  199.         
  200.         $this->assertEquals('http://unitTestUser/'$dirtyObjects[0]->get('URL')'Testing that objects are being added to the dirtyObject array correctly');    
  201.     }
  202.     
  203.     /**
  204.      * Testing that objects are being added to the dirtyObject array correctly
  205.      * and that this array is in the session being shared by controllers
  206.      * 
  207.      * @since 1.0
  208.      */
  209.     public function testMarkDirtySession({
  210.         $this->person->set('email''changed@test.com');
  211.         $this->controller->markDirty($this->person);
  212.         
  213.         // calling the constructor of the other controller will check the session
  214.         $controller2 new Search();
  215.         
  216.         $dirty $controller2->getDirtyObjects();        
  217.         
  218.         $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');    
  219.     }
  220.     
  221.     /**
  222.      * Testing that objects are being added to the newObjects array correctly
  223.      * 
  224.      * @since 1.0
  225.      */
  226.     public function testMarkNewAdd({
  227.         $this->controller->markNew($this->person);
  228.         
  229.         $newObjects $this->controller->getNewObjects();
  230.         
  231.         $this->assertEquals('http://unitTestUser/'$newObjects[0]->get('URL')'Testing that objects are being added to the newObject array correctly');    
  232.     }
  233.     
  234.     /**
  235.      * Testing that objects are being added to the newObjects array correctly
  236.      * and that this array is in the session being shared by controllers
  237.      * 
  238.      * @since 1.0
  239.      */
  240.     public function testMarkNewSession({        
  241.         $person $this->createPersonObject('newuser');
  242.         $person->set('email''newuser@test.com')
  243.         $this->controller->markNew($person);
  244.         
  245.         // calling the constructor of the other controller will check the session
  246.         $controller2 new Search();
  247.         
  248.         $new $controller2->getNewObjects();        
  249.         
  250.         $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');    
  251.     }
  252.     
  253.     /**
  254.      * test cases to see if access rights on controllers are working as expected
  255.      * 
  256.      * @since 1.0
  257.      */
  258.     public function testRightsAccess({
  259.         $this->group->set('name''testgroup');
  260.         $this->group->save();
  261.         
  262.         $this->person->save();
  263.         
  264.         $lookup $this->person->getPropObject('rights')->getLookup();
  265.         $lookup->setValue(array($this->person->getOID()$this->group->getOID()));
  266.         $lookup->save();
  267.         
  268.         $admin $_SESSION['currentUser'];
  269.         $_SESSION['currentUser'$this->person;
  270.         
  271.         try {
  272.             $controller new Search('testgroup');
  273.         }catch (PHPException $e{
  274.             $this->fail('failed to access a controller that I have access to by rights group membership');
  275.         }
  276.         
  277.         $_SESSION['currentUser'$admin;
  278.     }
  279.     
  280.     /** 
  281.      * test the getUnitDuration method for equality
  282.      * 
  283.      * @since 1.0
  284.      */
  285.     public function testGetUnitDurationEqual({
  286.         $controller1 new Search();
  287.         $controller2 new Search();
  288.         $controller1->setUnitEndTime(20051030211515);
  289.         $controller2->setUnitEndTime(20051030211515);
  290.     
  291.         $this->assertEquals($controller1->getUnitDuration()$controller2->getUnitDuration()'test the getUnitDuration method for equality');
  292.     }
  293.     
  294.     /** 
  295.      * Test the getUnitDuration method for greater than
  296.      * 
  297.      * @since 1.0
  298.      */
  299.     public function testGetUnitDurationGreater({
  300.         $controller1 new Search();
  301.         $controller2 new Search();
  302.         $controller1->setUnitEndTime(20061030211515);
  303.         $controller2->setUnitEndTime(20051030211515);
  304.     
  305.         $this->assertTrue($controller1->getUnitDuration($controller2->getUnitDuration()'Test the getUnitDuration method for greater than');
  306.     }
  307.     
  308.     /**
  309.      * Testing the setUnitOfWork method with a bad controller name
  310.      * 
  311.      * @since 1.0
  312.      */
  313.     public function testSetUnitOfWorkBadControllerName({
  314.         try {
  315.             $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','BadControllerName'));
  316.             $this->fail('Passed a bad controller name BadControllerName to setUnitOfWork() and did not get the expected exception!');
  317.         }catch (IllegalArguementException $e{
  318.             $this->assertEquals(''$this->controller->getFirstJob()'Testing the setUnitOfWork method with a bad controller name');
  319.         }
  320.     }
  321.     
  322.     /**
  323.      * Testing the setUnitOfWork method and getNextJob
  324.      * 
  325.      * @since 1.0
  326.      */
  327.     public function testSetUnitOfWorkNext({
  328.         $this->controller->setName('Search');
  329.         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
  330.         
  331.         $this->assertEquals('Edit'$this->controller->getNextJob()'Testing the setUnitOfWork method and getNextJob');
  332.     }
  333.     
  334.     /**
  335.      * Testing the setUnitOfWork method and getFirstJob
  336.      * 
  337.      * @since 1.0
  338.      */
  339.     public function testSetUnitOfWorkFirst({
  340.         $this->controller->setName('ListAll');
  341.         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
  342.         
  343.         $this->assertEquals('Search'$this->controller->getFirstJob()'Testing the setUnitOfWork method and getFirstJob');
  344.     }
  345.     
  346.     /**
  347.      * Testing the setUnitOfWork method and getPreviousJob
  348.      * 
  349.      * @since 1.0
  350.      */
  351.     public function testSetUnitOfWorkPrevious({
  352.         $this->controller->setName('ListAll');
  353.         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
  354.         
  355.         $this->assertEquals('Create'$this->controller->getPreviousJob()'Testing the setUnitOfWork method and getPreviousJob');
  356.     }
  357.     
  358.     /**
  359.      * Testing the setUnitOfWork method and getLastJob
  360.      * 
  361.      * @since 1.0
  362.      */
  363.     public function testSetUnitOfWorkLast({
  364.         $this->controller->setName('ListAll');
  365.         $this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
  366.         
  367.         $this->assertEquals('Detail'$this->controller->getLastJob()'Testing the setUnitOfWork method and getLastJob');
  368.     }
  369.     
  370.     /**
  371.      * Testing the commit method for new and dirty objects
  372.      * 
  373.      * @since 1.0
  374.      */
  375.     public function testCommit({
  376.         $this->person->set('email''changed@test.com');
  377.         $this->controller->markDirty($this->person);
  378.         
  379.         $person $this->createPersonObject('newuser');
  380.         $person->set('email''newuser@test.com')
  381.         $this->controller->markNew($person);
  382.         
  383.         try {
  384.             $this->controller->commit();
  385.         }catch (FailedUnitCommitException $e{
  386.             $this->fail('Failed to commit the unit of work transaction for new and dirty objects');
  387.         }
  388.     }
  389.     
  390.     /**
  391.      * Testing that we can load dirty and new objects post commit
  392.      * 
  393.      * @since 1.0
  394.      */
  395.     public function testPostCommitLoad({
  396.         $this->person->set('email''changed@test.com');
  397.         $this->controller->markDirty($this->person);
  398.         
  399.         $person $this->createPersonObject('newuser');
  400.         $person->set('email''newuser@test.com')
  401.         $this->controller->markNew($person);
  402.         
  403.         try {
  404.             $this->controller->commit();
  405.         }catch (FailedUnitCommitException $e{
  406.             $this->fail('Failed to commit the unit of work transaction for new and dirty objects');
  407.         }
  408.         
  409.         $newPerson new PersonObject();
  410.         try {
  411.             $newPerson->loadByAttribute('email''newuser@test.com');
  412.         }catch (BONotFoundException $e{
  413.             $this->fail('Failed to load the new person that we commited in the unit of work');
  414.         }
  415.         
  416.         $dirtyPerson new PersonObject();
  417.         try {
  418.             $dirtyPerson->loadByAttribute('email''changed@test.com');
  419.         }catch (BONotFoundException $e{
  420.             $this->fail('Failed to load the dirty person that we commited in the unit of work');
  421.         }
  422.     }
  423.     
  424.     /**
  425.      * Testing that aborting a unit of work clears the list of new objects
  426.      * 
  427.      * @since 1.0
  428.      */
  429.     public function testAbort({
  430.         $person $this->createPersonObject('newuser');
  431.         $person->set('email''newuser@test.com')
  432.         $this->controller->markNew($person);
  433.         
  434.         // calling the constructor of the other controller will check the session
  435.         $controller2 new Search();
  436.         
  437.         $new $controller2->getNewObjects();
  438.         
  439.         $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');
  440.  
  441.         // now abort the unit of work from the second controller, and confirm that the new object array is empty
  442.         $controller2->abort();
  443.         
  444.         $new $controller2->getNewObjects();
  445.         
  446.         $this->assertEquals(0count($new)'Testing that aborting a unit of work clears the list of new objects');
  447.     }
  448.     
  449.     /**
  450.      * Testing that the AlphaController constructor uses the controller name as the AlphaController->name (job) of the controller
  451.      * 
  452.      * @since 1.0
  453.      */
  454.     public function testConstructorJobControllerName({
  455.         $this->assertEquals('Search'$this->controller->getName()'Testing that the AlphaController constructor defaults to using the controller name as the AlphaController->name of the controller');
  456.     }
  457.     
  458.     /**
  459.      * Testing that providing a bad BO name returns null
  460.      * 
  461.      * @since 1.0
  462.      */
  463.     public function testGetCustomControllerName({
  464.         $this->assertNull(AlphaController::getCustomControllerName('DoesNotExistObject''view')'Testing that providing a bad BO name returns null');
  465.     }
  466.     
  467.     /**
  468.      * Testing the checkRights method with various account types
  469.      * 
  470.      * @since 1.0
  471.      */
  472.     public function testCheckRights({
  473.         $controller new Search('Admin');
  474.         $admin $_SESSION['currentUser'];
  475.         $_SESSION['currentUser'null;
  476.         
  477.         $this->assertFalse($controller->checkRights()'Testing that a user with no session cannot access an Admin controller');
  478.         $controller new Search('Public');
  479.         $this->assertTrue($controller->checkRights()'Testing that a user with no session can access a Public controller');
  480.         
  481.         $_SESSION['currentUser'$admin;
  482.     }
  483.     
  484.     /**
  485.      * Testing the checkSecurityFields method
  486.      * 
  487.      * @since 1.0
  488.      */
  489.     public function testCheckSecurityFields({
  490.         $securityFields AlphaController::generateSecurityFields();
  491.         
  492.         $_REQUEST['var1'$securityFields[0];
  493.         $_REQUEST['var2'$securityFields[1];
  494.         
  495.         $this->assertTrue(AlphaController::checkSecurityFields()'Testing the checkSecurityFields method with valid security params');
  496.         
  497.         $_REQUEST['var1'null;
  498.         $_REQUEST['var2'null;
  499.         
  500.         $this->assertFalse(AlphaController::checkSecurityFields()'Testing the checkSecurityFields method with invalid security params');
  501.     }
  502.     
  503.     /**
  504.      * Testing that a bad controller name passed to loadControllerDef will cause an exception
  505.      * 
  506.      * @since 1.0
  507.      */
  508.     public function testLoadControllerDef({
  509.         try {
  510.             $this->controller->loadControllerDef('DoesNotExist');
  511.             $this->fail('Testing that a bad controller name passed to loadControllerDef will cause an exception');
  512.         }catch (IllegalArguementException $e{
  513.             $this->assertEquals('The class [DoesNotExist] is not defined anywhere!'$e->getMessage()'Testing that a bad controller name passed to loadControllerDef will cause an exception');
  514.         }
  515.     }
  516.     
  517.     /**
  518.      * Testing that status messages can be shared between controllers via the session
  519.      * 
  520.      * @since 1.0
  521.      */
  522.     public function testStatusMessages({
  523.         $this->controller->setStatusMessage('test message');
  524.         
  525.         $controller new Search();
  526.         
  527.         $this->assertEquals('test message'$controller->getStatusMessage()'Testing that status messages can be shared between controllers via the session');
  528.     }
  529.     
  530.     /**
  531.      * Testing that a BO attached to a controller that contains tags will have those tags mapped to the controller's keywords
  532.      * 
  533.      * @since 1.0
  534.      */
  535.     public function testTagsMapToMetaKeywords({
  536.         AlphaDAO::begin();
  537.         $this->article->save();
  538.         AlphaDAO::commit();
  539.         $tags $this->article->getPropObject('tags')->getRelatedObjects();
  540.         
  541.         $found false;
  542.         foreach($tags as $tag{
  543.             if($tag->get('content'== 'unittestarticle'{
  544.                 $found true;
  545.                 break;
  546.             }
  547.         }
  548.         $this->assertTrue($found'Testing the TagObject::tokenize method returns a tag called "unittestarticle"');
  549.         
  550.         $this->controller->setBO($this->article);
  551.         
  552.         $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');
  553.         
  554.     }
  555. }
  556.  
  557. ?>

Documentation generated on Thu, 17 Mar 2011 16:43:13 +0000 by phpDocumentor 1.4.3