Perforce Chronicle 2012.2/486814
API Documentation

Analytics_Test_ConfigureControllerTest Class Reference

Test the Analytics configure controller. More...

List of all members.

Public Member Functions

 setUp ()
 Perform setup.
 testBadAddPost ()
 Test bad post data.
 testConfigure ()
 Test that the configuration form works properly.
 testGoodAddPost ()
 Test good post to save valid data.

Protected Attributes

 $_analyticsModule

Detailed Description

Test the Analytics configure controller.

Because the analytics service is outside of our control, all we can test is that the analytics code was injected accurately - we cannot test that it is working.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

Analytics_Test_ConfigureControllerTest::setUp ( )

Perform setup.

    {
        parent::setUp();
        
        if (!defined('TEST_ACCOUNT_NUMBER')) {
            define('TEST_ACCOUNT_NUMBER', 'UA-XXXXX-1');
        }
        $this->_analyticsModule = P4Cms_Module::fetch('Analytics');
        $this->_analyticsModule->enable();
        $this->_analyticsModule->load();
    }
Analytics_Test_ConfigureControllerTest::testBadAddPost ( )

Test bad post data.

    {
        $this->utility->impersonate('administrator');

        // form request without required fields.
        $this->request->setMethod('POST');
        $this->request->setPost('accountNumber', '');
        $this->request->setPost('save',          'save');

        $this->dispatch('/analytics/configure/index');
        $responseBody = $this->response->getBody();

        $this->assertModule('analytics',        'Expected module.');
        $this->assertController('configure',    'Expected controller');
        $this->assertAction('index',            'Expected action');

        $this->assertQueryContentContains(
            'span.message',
            'Invalid value entered for Analytics module configuration',
            $responseBody
        );
        
        $this->assertQueryContentContains(
            'ul.errors',
            "Value is required and can't be empty",
            $responseBody
        );
        $this->assertQuery("div.severity-error", 'Expected content', $responseBody);
    }
Analytics_Test_ConfigureControllerTest::testConfigure ( )

Test that the configuration form works properly.

    {
        $this->utility->impersonate('administrator');

        // test that this module has not broken the list
        $this->dispatch('/site/module');

        $this->assertModule('site', 'expected "site" module');
        $this->assertController('module', 'expected "module" controller');
        $this->assertAction('index', 'expected "index" action');

        $this->assertQuery("div.module-grid");
        $this->assertQuery("div.module-grid table");
        $this->assertQuery("div.module-grid thead");

        // dispatch again to get the module inventory
        $this->resetRequest()->resetResponse();
        $this->dispatch('/site/module/format/json');
        $this->assertModule('site', 'expected "site" module for JSON');
        $this->assertController('module', 'expected "module" controller for JSON');
        $this->assertAction('index', 'expected "index" action for JSON');

        // ensure that the module can be configured.
        $body = $this->response->getBody();
        $values = Zend_Json::decode($body);
        $uri    = $this->_analyticsModule->getConfigUri();
        $this->assertTrue(array_key_exists('items', $values), 'Expect an items entry in JSON output.');

        foreach ($values['items'] as $item) {
            if ($item['name'] !== $this->_analyticsModule->name) continue;

            $this->assertEquals($uri, $item['configUri'], 'Expected Analytics module configure uri.');
        }

        // test that module controller correctly forwards to module's configure action.
        $this->resetRequest()->resetResponse();
        $this->dispatch($uri);

        $this->assertModule('analytics', 'expected "analytics" module');
        $this->assertController('configure', 'expected "configure" controller');
        $this->assertAction('index', 'expected "index" action for analytics');
        $this->assertQueryContentContains("h1",     "Configure Analytics");
        $this->assertQueryContentContains("label",  "Site Profile Identifier");

        //verify form content
        $this->assertQuery("form",                          "Expected configuration form.");
        $this->assertQuery("input[name='accountNumber']",   "Expected accountNumber input.");
        $this->assertQuery("input[name='customVars[]']",    "Expected customVars input.");
        $this->assertQuery("input[type='submit']",          "Expected submit button.");

        // verify labels are present
        $labels = array(
            'accountNumber' => 'Site Profile Identifier',
            'customVars'    => 'Tracking Variables'
        );
        foreach ($labels as $field => $label) {
            $this->assertQueryContentContains("label[for='$field']", $label, "Expected $field label.");
        }
    }
Analytics_Test_ConfigureControllerTest::testGoodAddPost ( )

Test good post to save valid data.

    {
        $this->utility->impersonate('administrator');

        $this->request->setMethod('POST');
        $this->request->setPost('accountNumber', TEST_ACCOUNT_NUMBER);
        $this->request->setPost('save',          'save');

        $this->dispatch('/analytics/configure/index');
        
        $this->assertModule('analytics',        'Expected module.');
        $this->assertController('configure',    'Expected controller');
        $this->assertAction('index',            'Expected action');

        // check for saved tracking code
        $module =        P4Cms_Module::fetch('Analytics');
        $config =        $module->getConfig();
        $values =        $config->toArray();
        $accountNumber = $values['accountNumber'];

        $this->assertSame(
            'UA-XXXXX-1',
            $accountNumber,
            "Expected the same account number as was posted."
        );
    }

Member Data Documentation

Analytics_Test_ConfigureControllerTest::$_analyticsModule [protected]

The documentation for this class was generated from the following file: