Skip to content
Extraits de code Groupes Projets
IntegrationTestCase.php 43,2 ko
Newer Older
  • Learn to ignore specific revisions
  •      *     array('SomeAPI.method', array('testOption1' => 'value1', 'testOption2' => 'value2'),
         *     array(array('SomeAPI.method', 'SomeOtherAPI.method'), array(...)),
         *     .
         *     .
         *     .
         * )
         * </code>
         *
         * Valid test options:
         * <ul>
         *   <li><b>testSuffix</b> The suffix added to the test name. Helps determine
         *   the filename of the expected output.</li>
         *   <li><b>format</b> The desired format of the output. Defaults to 'xml'.</li>
         *   <li><b>idSite</b> The id of the website to get data for.</li>
         *   <li><b>date</b> The date to get data for.</li>
         *   <li><b>periods</b> The period or periods to get data for. Can be an array.</li>
         *   <li><b>setDateLastN</b> Flag describing whether to query for a set of
         *   dates or not.</li>
         *   <li><b>language</b> The language to use.</li>
         *   <li><b>segment</b> The segment to use.</li>
         *   <li><b>visitorId</b> The visitor ID to use.</li>
         *   <li><b>abandonedCarts</b> Whether to look for abandoned carts or not.</li>
         *   <li><b>idGoal</b> The goal ID to use.</li>
         *   <li><b>apiModule</b> The value to use in the apiModule request parameter.</li>
         *   <li><b>apiAction</b> The value to use in the apiAction request parameter.</li>
         *   <li><b>otherRequestParameters</b> An array of extra request parameters to use.</li>
         *   <li><b>disableArchiving</b> Disable archiving before running tests.</li>
         * </ul>
         *
         * All test options are optional, except 'idSite' & 'date'.
         */
        public function getApiForTesting() {
            return array();
        }
    
        /**
         * Returns an array describing the Controller actions to call & compare
         * with expected output.
         *
         * The returned array must be of the following format:
         * <code>
         * array(
         *     array('Controller.action', array('testOption1' => 'value1', 'testOption2' => 'value2'),
         *     array(array('Controller.action', 'OtherController.action'), array(...)),
         *     .
         *     .
         *     .
         * )
         * </code>
         *
         * Valid test options:
         * <ul>
         *   <li><b>UNIMPLEMENTED</b></li>
         * </ul>
         */
        public function getControllerActionsForTesting() {
            return array();
        }
    
        /**
         * Gets the string prefix used in the name of the expected/processed output files.
         */
        public function getOutputPrefix()
        {
            return str_replace('Test_Piwik_Integration_', '', get_class($this));
        }
    
        /**
         * Runs API tests.
         */
        protected function runApiTests($api, $params)
        {
            $testName = 'test_' . $this->getOutputPrefix();
    
            if ($api == 'all')
            {
                $this->setApiToCall(array());
                $this->setApiNotToCall(self::$defaultApiNotToCall);
            }
            else
            {
                if (!is_array($api))
                {
                    $api = array($api);
                }
    
                $this->setApiToCall($api);
                $this->setApiNotToCall(array('API.getPiwikVersion'));
            }
    
            if (isset($params['disableArchiving']) && $params['disableArchiving'] === true)
            {
                Piwik_ArchiveProcessing::$forceDisableArchiving = true;
            }
            else
            {
                Piwik_ArchiveProcessing::$forceDisableArchiving = false;
            }
    
            if (isset($params['language']))
            {
                $this->changeLanguage($params['language']);
            }
    
            $testSuffix = isset($params['testSuffix']) ? $params['testSuffix'] : '';
    
            $this->_callGetApiCompareOutput(
                $testName . $testSuffix,
                isset($params['format']) ? $params['format'] : 'xml',
                isset($params['idSite']) ? $params['idSite'] : false,
                isset($params['date']) ? $params['date'] : false,
                isset($params['periods']) ? $params['periods'] : false,
                isset($params['setDateLastN']) ? $params['setDateLastN'] : false,
                isset($params['language']) ? $params['language'] : false,
                isset($params['segment']) ? $params['segment'] : false,
                isset($params['visitorId']) ? $params['visitorId'] : false,
                isset($params['abandonedCarts']) ? $params['abandonedCarts'] : false,
                isset($params['idGoal']) ? $params['idGoal'] : false,
                isset($params['apiModule']) ? $params['apiModule'] : false,
                isset($params['apiAction']) ? $params['apiAction'] : false,
                isset($params['otherRequestParameters']) ? $params['otherRequestParameters'] : array());
    
            // change the language back to en
            if ($this->lastLanguage != 'en')
            {
                $this->changeLanguage('en');
            }
        }
    
        /**
         * Runs controller tests.
         */
    
        protected function runControllerTests($actions, $params)
    
        {
            static $nonRequestParameters = array('testingLevelOverride' => null, 'userTypes' => null);
    
            $testName = 'test_' . $this->getOutputPrefix();
    
    
            // deal w/ any language changing hacks
            if (isset($params['language'])) {
                $this->changeLanguage($params['language']);
            }
    
            // separate request parameters from function parameters
            $requestParams = array();
            foreach ($params as $key => $value) {
                if (!isset($nonRequestParameters[$key])) {
                    $requestParams[$key] = $value;
    
            $testSuffix = isset($params['testSuffix']) ? $params['testSuffix'] : '';
    
            $this->callWidgetsCompareOutput(
                $testName . $testSuffix,
                $actions,
                $requestParams,
                isset($params['userTypes']) ? $params['userTypes'] : false,
                isset($params['testingLevelOverride']) ? $params['testingLevelOverride'] : false);
    
            // change the language back to en
            if ($this->lastLanguage != 'en') {
                $this->changeLanguage('en');
    
            }
        }
    
        /**
         * changing the language within one request is a bit fancy
         * in order to keep the core clean, we need a little hack here
         *
         * @param string $langId
         */
        protected function changeLanguage( $langId )
        {
            if (isset($this->lastLanguage) && $this->lastLanguage != $langId)
            {
                $_GET['language'] = $langId;
                Piwik_Translate::reset();
                Piwik_Translate::getInstance()->reloadLanguage($langId);
            }
    
            $this->lastLanguage = $langId;
        }
    
        /**
         * Path where expected/processed output files are stored. Can be overridden.
         */
        public function getPathToTestDirectory()
        {
            /**
             * Use old path as long as files were not moved
             * @todo move files
             */
            //return dirname(__FILE__).DIRECTORY_SEPARATOR.'Integration';
            return dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'integration';
        }
    
    }