Source for file AlphaController_Test.php
Documentation is available at AlphaController_Test.php
require_once $config->get('sysRoot'). 'alpha/controller/Search.php';
require_once $config->get('sysRoot'). 'alpha/model/PersonObject.inc';
require_once $config->get('sysRoot'). 'alpha/model/ArticleObject.inc';
require_once $config->get('sysRoot'). 'alpha/model/RightsObject.inc';
* Test cases for the AlphaController class.
* @author John Collins <dev@alphaframework.org>
* @version $Id: AlphaController_Test.php 1341 2011-03-17 15:02:02Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Sample controller for Testing with
* An ArticleObject for Testing
* A PersonObject for Testing (any business object will do)
* @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::setUp()
protected function setUp() {
$article->rebuildTable();
$this->controller = new Search();
$this->person = $this->createPersonObject('unitTestUser');
$this->person->rebuildTable();
$this->article = $this->createArticleObject('unitTestArticle');
$this->article->rebuildTable();
$this->group->rebuildTable();
$this->group->set('name', 'Admin');
$lookup = $this->group->getMembers()->getLookup();
$lookup->setValue(array($_SESSION['currentUser']->getOID(), $this->group->getOID()));
* @see alpha/lib/PEAR/PHPUnit-3.2.9/PHPUnit/Framework/PHPUnit_Framework_TestCase::tearDown()
$this->controller->abort();
$this->article->dropTable();
unset ($this->controller);
$this->person->dropTable();
$this->group->dropTable();
$this->group->dropTable('Person2Rights');
* Creates a person object for Testing
private function createPersonObject($name) {
$person->setDisplayname($name);
$person->set('email', $name. '@test.com');
$person->set('password', 'passwordTest');
$person->set('URL', 'http://unitTestUser/');
* Creates an article object for Testing
private function createArticleObject($name) {
$article->set('title', $name);
$article->set('description', 'unitTestArticleTagOne unitTestArticleTagTwo');
$article->set('author', 'unitTestArticleTagOne');
$article->set('content', 'unitTestArticleTagOne');
* Testing that objects are being added to the dirtyObjects array correctly
$this->controller->markDirty($this->person);
$dirtyObjects = $this->controller->getDirtyObjects();
$this->assertEquals('http://unitTestUser/', $dirtyObjects[0]->get('URL'), 'Testing that objects are being added to the dirtyObject array correctly');
* Testing that objects are being added to the dirtyObject array correctly
* and that this array is in the session being shared by controllers
$this->person->set('email', 'changed@test.com');
$this->controller->markDirty($this->person);
// calling the constructor of the other controller will check the session
$dirty = $controller2->getDirtyObjects();
$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');
* Testing that objects are being added to the newObjects array correctly
$this->controller->markNew($this->person);
$newObjects = $this->controller->getNewObjects();
$this->assertEquals('http://unitTestUser/', $newObjects[0]->get('URL'), 'Testing that objects are being added to the newObject array correctly');
* Testing that objects are being added to the newObjects array correctly
* and that this array is in the session being shared by controllers
$person = $this->createPersonObject('newuser');
$person->set('email', 'newuser@test.com');
$this->controller->markNew($person);
// calling the constructor of the other controller will check the session
$new = $controller2->getNewObjects();
$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');
* test cases to see if access rights on controllers are working as expected
$this->group->set('name', 'testgroup');
$lookup = $this->person->getPropObject('rights')->getLookup();
$lookup->setValue(array($this->person->getOID(), $this->group->getOID()));
$admin = $_SESSION['currentUser'];
$_SESSION['currentUser'] = $this->person;
$controller = new Search('testgroup');
$this->fail('failed to access a controller that I have access to by rights group membership');
$_SESSION['currentUser'] = $admin;
* test the getUnitDuration method for equality
$controller1->setUnitEndTime(2005, 10, 30, 21, 15, 15);
$controller2->setUnitEndTime(2005, 10, 30, 21, 15, 15);
$this->assertEquals($controller1->getUnitDuration(), $controller2->getUnitDuration(), 'test the getUnitDuration method for equality');
* Test the getUnitDuration method for greater than
$controller1->setUnitEndTime(2006, 10, 30, 21, 15, 15);
$controller2->setUnitEndTime(2005, 10, 30, 21, 15, 15);
$this->assertTrue($controller1->getUnitDuration() > $controller2->getUnitDuration(), 'Test the getUnitDuration method for greater than');
* Testing the setUnitOfWork method with a bad controller name
$this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','BadControllerName'));
$this->fail('Passed a bad controller name BadControllerName to setUnitOfWork() and did not get the expected exception!');
$this->assertEquals('', $this->controller->getFirstJob(), 'Testing the setUnitOfWork method with a bad controller name');
* Testing the setUnitOfWork method and getNextJob
$this->controller->setName('Search');
$this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
$this->assertEquals('Edit', $this->controller->getNextJob(), 'Testing the setUnitOfWork method and getNextJob');
* Testing the setUnitOfWork method and getFirstJob
$this->controller->setName('ListAll');
$this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
$this->assertEquals('Search', $this->controller->getFirstJob(), 'Testing the setUnitOfWork method and getFirstJob');
* Testing the setUnitOfWork method and getPreviousJob
$this->controller->setName('ListAll');
$this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
$this->assertEquals('Create', $this->controller->getPreviousJob(), 'Testing the setUnitOfWork method and getPreviousJob');
* Testing the setUnitOfWork method and getLastJob
$this->controller->setName('ListAll');
$this->controller->setUnitOfWork(array('Search','Edit','Create','ListAll','Detail'));
$this->assertEquals('Detail', $this->controller->getLastJob(), 'Testing the setUnitOfWork method and getLastJob');
* Testing the commit method for new and dirty objects
$this->person->set('email', 'changed@test.com');
$this->controller->markDirty($this->person);
$person = $this->createPersonObject('newuser');
$person->set('email', 'newuser@test.com');
$this->controller->markNew($person);
$this->controller->commit();
$this->fail('Failed to commit the unit of work transaction for new and dirty objects');
* Testing that we can load dirty and new objects post commit
$this->person->set('email', 'changed@test.com');
$this->controller->markDirty($this->person);
$person = $this->createPersonObject('newuser');
$person->set('email', 'newuser@test.com');
$this->controller->markNew($person);
$this->controller->commit();
$this->fail('Failed to commit the unit of work transaction for new and dirty objects');
$newPerson->loadByAttribute('email', 'newuser@test.com');
$this->fail('Failed to load the new person that we commited in the unit of work');
$dirtyPerson->loadByAttribute('email', 'changed@test.com');
$this->fail('Failed to load the dirty person that we commited in the unit of work');
* Testing that aborting a unit of work clears the list of new objects
$person = $this->createPersonObject('newuser');
$person->set('email', 'newuser@test.com');
$this->controller->markNew($person);
// calling the constructor of the other controller will check the session
$new = $controller2->getNewObjects();
$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');
// now abort the unit of work from the second controller, and confirm that the new object array is empty
$new = $controller2->getNewObjects();
$this->assertEquals(0, count($new), 'Testing that aborting a unit of work clears the list of new objects');
* Testing that the AlphaController constructor uses the controller name as the AlphaController->name (job) of the controller
$this->assertEquals('Search', $this->controller->getName(), 'Testing that the AlphaController constructor defaults to using the controller name as the AlphaController->name of the controller');
* Testing that providing a bad BO name returns null
* Testing the checkRights method with various account types
$controller = new Search('Admin');
$admin = $_SESSION['currentUser'];
$_SESSION['currentUser'] = null;
$this->assertFalse($controller->checkRights(), 'Testing that a user with no session cannot access an Admin controller');
$controller = new Search('Public');
$this->assertTrue($controller->checkRights(), 'Testing that a user with no session can access a Public controller');
$_SESSION['currentUser'] = $admin;
* Testing the checkSecurityFields method
$_REQUEST['var1'] = $securityFields[0];
$_REQUEST['var2'] = $securityFields[1];
$_REQUEST['var1'] = null;
$_REQUEST['var2'] = null;
* Testing that a bad controller name passed to loadControllerDef will cause an exception
$this->controller->loadControllerDef('DoesNotExist');
$this->fail('Testing that a bad controller name passed to loadControllerDef will cause an exception');
$this->assertEquals('The class [DoesNotExist] is not defined anywhere!', $e->getMessage(), 'Testing that a bad controller name passed to loadControllerDef will cause an exception');
* Testing that status messages can be shared between controllers via the session
$this->controller->setStatusMessage('test message');
$this->assertEquals('test message', $controller->getStatusMessage(), 'Testing that status messages can be shared between controllers via the session');
* Testing that a BO attached to a controller that contains tags will have those tags mapped to the controller's keywords
$tags = $this->article->getPropObject('tags')->getRelatedObjects();
if($tag->get('content') == 'unittestarticle') {
$this->assertTrue($found, 'Testing the TagObject::tokenize method returns a tag called "unittestarticle"');
$this->controller->setBO($this->article);
$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');
|