I got the idea that I should create an ACL front controller plugin that
loads only if there is an authenticated user. My reasoning is, if they ain't
logged in, why load my ACL class? Send them to the login page, end of story.

Hence, in my Bootstrap,

    function _initFrontControllerPlugins() {
        $log = $this->getResource('log');
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Plugin_Authentication());
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $front->registerPlugin(new Plugin_Acl());
            $log->info("yes auth identity, Acl plugin loaded at Bootstrap
".__LINE__ ."\n");
        } else {
            $log->info("no auth identity, ergo acl not loaded at Bootstrap
".__LINE__ ."\n");
        }
    }

In a browser, I can log in, then load another page and see my Firebug logger
telling me the ACL plugin class has loaded as it should.

But in my unit test, this assertion fails:

function testAclPluginLoadsIfUserIsAuthenticated() {

        $this->loginDavid(); // tested independently and known to work
        $this->resetRequest()->resetResponse();
        $this->dispatch('/admin/index');
        $auth = Zend_Auth::getInstance();
        if (! $auth->hasIdentity()) {
            throw new Exception("test login for admin failed");
        } else {
            echo " OK there is an identity (Acl Test: ".__LINE__.  ")\n";
        }
        if ($auth->getStorage()->read()->username != 'david') {
            throw new Exception("unexpected username is logged in, can't run
test");
            return;
        } else {
            echo " OK David is authenticated\n" ;
        }
        $front = $this->bootstrap->getResource('FrontController');

$this->assertType('Plugin_Acl',$front->getPlugin('Plugin_Acl'),"Plugin_Acl
not loaded when it should be");
    }
}

Any ideas what I am doing wrong? (I am, I confess, still fairly new to
automated testing but I am an enthusiastic believer.)

btw, I tried simply loading both plugins unconditionally, and when I rewrite
the tests to check that both are loaded, it works. But it seems like the
tail is wagging the dog when you refactor (de-optimize?) your app just to
force tests to pass.

Gratefully,

           David


-- 
Demand health care for everyone:
http://mobilizeforhealthcare.org/

--
David Mintz
http://davidmintz.org/

Reply via email to