Location: PHPKode > projects > Xyster Framework > xyster-02build01/xyster-02build01/tests/Xyster/Controller/Plugin/AclTest.php
<?php
/**
 * Xyster Framework
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category  Xyster
 * @package   Xyster_Controller
 * @subpackage   UnitTests
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   $Id: AclTest.php 418 2010-10-18 21:40:08Z jonathanhawk $
 */
namespace XysterTest\Controller\Plugin;
use Xyster\Controller\Plugin\Acl;
/**
 * Test class for Xyster_Controller_Plugin_Acl.
 * Generated by PHPUnit on 2007-09-11 at 19:39:03.
 */
class AclTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Request object
     * @var \Zend_Controller_Request_Http
     */
    public $request;

    /**
     * Response object
     * @var \Zend_Controller_Response_Http
     */
    public $response;

    /**
     * Acl plugin
     * @var Acl
     */
    public $plugin;
    
    /**
     * ACL
     * @var \Xyster\Acl\Acl
     */
    public $acl;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     */
    protected function setUp()
    {
        $this->acl = new \Xyster\Acl\Acl();
        $auth = \Zend_Auth::getInstance();
        $auth->setStorage(new \Zend_Auth_Storage_NonPersistent());
        $auth->clearIdentity();
        $auth->authenticate(new AclTest_Success_Adapter());
        $this->acl->addRole(new \Zend_Acl_Role('doublecompile'));
        
        \Zend_Controller_Front::getInstance()->resetInstance();
        $this->request  = new \Zend_Controller_Request_Http();
        $this->response = new \Zend_Controller_Response_Cli();
        $this->plugin   = new Acl($this->acl);

        $this->plugin->setRequest($this->request);
        $this->plugin->setResponse($this->response);
    }
    
    /**
     * Tears down the test
     *
     */
    protected function tearDown()
    {
        \Zend_Auth::getInstance()->clearIdentity();
    }

    /**
     * Tests the plugin does nothing if access is granted
     *
     */
    public function testPostDispatchWithoutException()
    {
        $this->plugin->allow('doublecompile'); // allow all
        
        $this->request->setModuleName('foo')
                      ->setControllerName('bar')
                      ->setActionName('baz');
        $this->plugin->preDispatch($this->request);
        
        $this->assertEquals('baz', $this->request->getActionName());
        $this->assertEquals('bar', $this->request->getControllerName());
        $this->assertEquals('foo', $this->request->getModuleName());
    }

    /**
     * Tests the 'deny' method
     *
     */
    public function testDeny()
    {
        $this->plugin->deny('doublecompile'); // deny all
        
        $this->request->setModuleName('foo')
                      ->setControllerName('bar')
                      ->setActionName('baz');
        $this->plugin->preDispatch($this->request);
        
        $this->assertFalse($this->request->isDispatched());
    }
    
    /**
     * Tests the plugin forwards to the login action if no identity is available
     *
     */
    public function testPostDispatchNotAuthenticated()
    {
        \Zend_Auth::getInstance()->clearIdentity();
        
        $this->request->setModuleName('foo')
                      ->setControllerName('bar')
                      ->setActionName('baz');
        $this->plugin->preDispatch($this->request);
        
        $this->assertEquals('index', $this->request->getActionName());
        $this->assertEquals('login', $this->request->getControllerName());
        $this->assertEquals('default', $this->request->getModuleName());
    }
    
    /**
     * Tests the error handler information is correct
     *
     */
    public function testPostDispatchErrorRequestIsClone()
    {
        $this->request->setModuleName('foo')
                      ->setControllerName('bar')
                      ->setActionName('baz');
        $this->plugin->preDispatch($this->request);

        $errorHandler = $this->request->getParam('error_handler');
        $this->assertType('\ArrayObject', $errorHandler);
        $this->assertType('\Zend_Controller_Request_Http', $errorHandler->request);
        $this->assertNotSame($this->request, $errorHandler->request);
    }
    
    /**
     * Tests the 'setAccessDenied' method
     *
     */
    public function testSetAccessDenied() 
    {
        $return = $this->plugin->setAccessDenied('myfoo', 'bar', 'boobaz');

        $this->assertEquals('myfoo', $this->plugin->getAccessDeniedModule());
        $this->assertEquals('bar', $this->plugin->getAccessDeniedController());
        $this->assertEquals('boobaz', $this->plugin->getAccessDeniedAction());
        $this->assertSame($this->plugin, $return);
    }
    
    /**
     * Tests the 'setLogin' method
     *
     */
    public function testSetLogin() 
    {
        $return = $this->plugin->setLogin('myfoo', 'bar', 'boobaz');

        $this->assertEquals('myfoo', $this->plugin->getLoginModule());
        $this->assertEquals('bar', $this->plugin->getLoginController());
        $this->assertEquals('boobaz', $this->plugin->getLoginAction());
        $this->assertSame($this->plugin, $return);
    }
    
    /**
     * Tests the 'setRules' method
     *
     */
    public function testSetRules()
    {
        $rules = array(
                array(
                    'role' => 'doublecompile',
                    'module' => 'foo'
                ),
                array(
                    'type' => \Zend_Acl::TYPE_DENY,
                    'module' => 'foo',
                    'controller' => 'bar',
                    'action' => 'baz'
                )
            );
        $return = $this->plugin->setRules($rules);
        
        $this->assertSame($this->plugin, $return);
        
        $this->plugin->dispatchLoopStartup($this->request);

        $this->request->setModuleName('foo')
                      ->setControllerName('bar')
                      ->setActionName('index');
        $this->plugin->preDispatch($this->request);
        
        $this->assertEquals('index', $this->request->getActionName());
        $this->assertEquals('bar', $this->request->getControllerName());
        $this->assertEquals('foo', $this->request->getModuleName());
        
        $this->request->setModuleName('foo')
                      ->setControllerName('bar')
                      ->setActionName('baz');
        $this->plugin->preDispatch($this->request);
        
        $errorHandler = $this->request->getParam('error_handler');
        $this->assertType('\ArrayObject', $errorHandler);
    }
}

/**
 * Just a simple stub object
 *
 */
class AclTest_Success_Adapter implements \Zend_Auth_Adapter_Interface
{
    public function authenticate()
    {
        return new \Zend_Auth_Result(1, 'doublecompile');
    }
}
Return current item: Xyster Framework