Skip to content
Extraits de code Groupes Projets
IntegrationTestCase.php 43,6 ko
Newer Older
  • Learn to ignore specific revisions
  •             self::setApiToCall($api);
                self::setApiNotToCall(array('API.getPiwikVersion'));
    
        }
    
        /**
         * Runs API tests.
         */
        protected function runApiTests($api, $params)
        {
            $testName = 'test_' . $this->getOutputPrefix();
    
            $this->missingExpectedFiles = array();
            $this->comparisonFailures = array();
    
    
            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'] : '';
    
    
            $requestUrls = $this->_generateApiUrls(
                        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(),
    
    JulienMoumne's avatar
    JulienMoumne a validé
                        isset($params['supertableApi']) ? $params['supertableApi'] : false,
                        isset($params['fileExtension']) ? $params['fileExtension'] : false);
    
            foreach($requestUrls as $apiId => $requestUrl)
    
                $this->_testApiUrl( $testName . $testSuffix, $apiId, $requestUrl);
    
            // change the language back to en
            if ($this->lastLanguage != 'en')
            {
    
            
            if (!empty($this->missingExpectedFiles))
            {
                $expectedDir = dirname(reset($this->missingExpectedFiles));
                $this->markTestIncomplete(" ERROR: Could not find expected API output '"
                	. implode("', '", $this->missingExpectedFiles)
                	. "'. For new tests, to pass the test, you can copy files from the processed/ directory into"
                	. " $expectedDir  after checking that the output is valid. %s ");
            }
            
            if (!empty($this->comparisonFailures))
            {
            	throw reset($this->comparisonFailures);
            }
    
        }
    
        /**
         * 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 ($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()
        {
    
            return dirname(__FILE__).DIRECTORY_SEPARATOR.'Integration';
    
        
        /**
         * Returns an array associating table names w/ lists of row data.
         * 
         * @return array
         */
        protected static function getDbTablesWithData()
        {
        	$result = array();
        	foreach (Piwik::getTablesInstalled() as $tableName)
        	{
        		$result[$tableName] = Piwik_FetchAll("SELECT * FROM $tableName");
        	}
        	return $result;
        }
        
        /**
         * Truncates all tables then inserts the data in $tables into each
         * mapped table.
         * 
         * @param array $tables Array mapping table names with arrays of row data.
         */
        protected static function restoreDbTables( $tables )
        {
        	// truncate existing tables
        	Piwik::truncateAllTables();
        	
        	// insert data
        	$existingTables = Piwik::getTablesInstalled();
        	foreach ($tables as $table => $rows)
        	{
        		// create table if it's an archive table
        		if (strpos($table, 'archive_') !== false && !in_array($table, $existingTables))
        		{
        			$tableType = strpos($table, 'archive_numeric') !== false ? 'archive_numeric' : 'archive_blob';
        			
        			$createSql = Piwik::getTableCreateSql($tableType);
        			$createSql = str_replace(Piwik_Common::prefixTable($tableType), $table, $createSql);
        			Piwik_Query($createSql);
        		}
        		
        		if (empty($rows))
        		{
        			continue;
        		}
        		
        		$rowsSql = array();
        		foreach ($rows as $row)
        		{
        			$values = array();
        			foreach ($row as $name => $value)
        			{
        				if (is_null($value))
        				{
        					$values[] = 'NULL';
        				}
        				else if (is_numeric($value))
        				{
        					$values[] = $value;
        				}
        				else if (!ctype_print($value))
        				{
        					$values[] = "x'".bin2hex(substr($value, 1))."'";
        				}
        				else
        				{
        					$values[] = "'$value'";
        				}
        			}
        			
        			$rowsSql[] = "(".implode(',', $values).")";
        		}
        		
        		$sql = "INSERT INTO $table VALUES ".implode(',', $rowsSql);
        		Piwik_Query($sql);
        	}
        }
    
    	
    	/**
    	 * Drops all archive tables.
    	 */
    	public static function deleteArchiveTables()
    	{
    		foreach (Piwik::getTablesArchivesInstalled() as $table)
    		{
    			Piwik_Query("DROP TABLE IF EXISTS $table");
    		}
    		
    		Piwik_TablePartitioning::$tablesAlreadyInstalled = Piwik::getTablesInstalled($forceReload = true);
    	}
    
    	
    	public static $geoIpDbUrl = 'http://piwik-team.s3.amazonaws.com/GeoIP.dat.gz';
    	public static $geoLiteCityDbUrl = 'http://piwik-team.s3.amazonaws.com/GeoLiteCity.dat.gz';
    	
    	public static function downloadGeoIpDbs()
    	{
    		$geoIpOutputDir = PIWIK_INCLUDE_PATH.'/tests/lib/geoip-files';
    		self::downloadAndUnzip(self::$geoIpDbUrl, $geoIpOutputDir, 'GeoIP.dat');
    		self::downloadAndUnzip(self::$geoLiteCityDbUrl, $geoIpOutputDir, 'GeoIPCity.dat');
    	}
    	
    	public static function downloadAndUnzip( $url, $outputDir, $filename )
    	{
    		$bufferSize = 1024 * 1024;
    		
    		try
    		{
    			if (!is_dir($outputDir)) 
    			{
    				mkdir($outputDir);
    			}
    			
    			$deflatedOut = $outputDir.'/'.$filename;
    			$outfileName = $deflatedOut.'.gz';
    			
    			if (file_exists($deflatedOut))
    			{
    				return;
    			}
    			
    			$dump = fopen($url, 'rb');
    			$outfile = fopen($outfileName, 'wb');
    			$bytesRead = 0;
    			while (!feof($dump))
    			{
    				fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
    				$bytesRead += $bufferSize;
    			}
    			fclose($dump);
    			fclose($outfile);
    			
    			// unzip the dump
    			exec("gunzip -c \"".$outfileName."\" > \"$deflatedOut\"", $output, $return);
    			if ($return !== 0)
    			{
    				throw new Exception("gunzip failed($return): ".implode("\n", $output));
    			}
    		}
    		catch (Exception $ex)
    		{
    			self::markTestSkipped(
    				"Cannot download GeoIp DBs, skipping: ".$ex->getMessage()."\n".$ex->getTraceAsString());
    		}
    	}