Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ManySitesImportedLogs.php « Fixtures « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de486be2b2c18b09ab88e0f2c2c7bbccd1b90b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link	http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

/**
 * Imports visits from several log files using the python log importer.
 */
class Test_Piwik_Fixture_ManySitesImportedLogs extends Test_Piwik_BaseFixture
{
	public $dateTime = '2010-03-06 11:22:33';
	public $idSite = 1;
	public $idSite2 = 2;
	public $idGoal = 1;
	
	public function setUp()
	{
        $this->setUpWebsitesAndGoals();
		self::downloadGeoIpDbs();
		
		Piwik_UserCountry_LocationProvider::$providers = null;
		Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
		Piwik_UserCountry_LocationProvider::setCurrentProvider('geoip_php');
		
        $this->trackVisits();
	}
	
	public function tearDown()
	{
		Piwik_UserCountry_LocationProvider::$providers = null;
		Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
		Piwik_UserCountry_LocationProvider::setCurrentProvider('default');
	}
	
    public function setUpWebsitesAndGoals()
    {
		// for conversion testing
	    self::createWebsite($this->dateTime);
		Piwik_Goals_API::getInstance()->addGoal($this->idSite, 'all', 'url', 'http', 'contains', false, 5);
	    self::createWebsite($this->dateTime,  $ecommerce = 0, $siteName = 'Piwik test two',
	    					$siteUrl = 'http://example-site-two.com');
    }
    
    private function trackVisits()
    {
    	$this->logVisitsWithStaticResolver();
    	$this->logVisitsWithAllEnabled();
    	$this->replayLogFile();
		$this->logCustomFormat();
    }

	/**
	 * Logs a couple visits for Aug 9, Aug 10, Aug 11 of 2012, for site we create.
	 */
	private function logVisitsWithStaticResolver()
    {
    	$logFile = PIWIK_INCLUDE_PATH.'/tests/resources/fake_logs.log'; # log file
    	
    	$opts = array('--idsite' => $this->idSite,
    				  '--token-auth' => self::getTokenAuth(),
    				  '--recorders' => '4',
    				  '--recorder-max-payload-size' => '2');
		
		self::executeLogImporter($logFile, $opts);
	}
	
	/**
	 * Logs a couple visits for the site we created and two new sites that do not
	 * exist yet. Visits are from Aug 12, 13 & 14 of 2012.
	 */
	public function logVisitsWithDynamicResolver()
	{
		$logFile = PIWIK_INCLUDE_PATH.'/tests/resources/fake_logs_dynamic.log'; # log file
		
		$opts = array('--add-sites-new-hosts' => false,
					  '--token-auth' => self::getTokenAuth(),
    				  '--recorders' => '4',
    				  '--recorder-max-payload-size' => '1');
		
		self::executeLogImporter($logFile, $opts);
	}
	
	/**
	 * Logs a couple visits for the site we created w/ all log importer options
	 * enabled. Visits are for Aug 11 of 2012.
	 */
	private function logVisitsWithAllEnabled()
	{
		$logFile = PIWIK_INCLUDE_PATH.'/tests/resources/fake_logs_enable_all.log';
		
		$opts = array('--idsite' => $this->idSite,
					  '--token-auth' => self::getTokenAuth(),
					  '--recorders' => '4',
					  '--recorder-max-payload-size' => '2',
					  '--enable-static' => false,
					  '--enable-bots' => false,
					  '--enable-http-errors' => false,
					  '--enable-http-redirects' => false,
					  '--enable-reverse-dns' => false);
		
		self::executeLogImporter($logFile, $opts);
	}
	
	/**
	 * Logs a couple visit using log entries that are tracking requests to a piwik.php file.
	 * Adds two visits to idSite=1 and two to non-existant sites.
	 */
	private function replayLogFile()
	{
		$logFile = PIWIK_INCLUDE_PATH.'/tests/resources/fake_logs_replay.log';
		
		$opts = array('--token-auth' => self::getTokenAuth(),
					  '--recorders' => '4',
					  '--recorder-max-payload-size' => '2',
					  '--replay-tracking' => false);
		
		self::executeLogImporter($logFile, $opts);
	}

	/**
	 * Imports a log file in custom format that contains generation time
	 */
	private function logCustomFormat()
	{
		$logFile = PIWIK_INCLUDE_PATH.'/tests/resources/fake_logs_custom.log';
				
		$opts = array('--idsite' => $this->idSite,
		              '--token-auth' => self::getTokenAuth(),
			          '--log-format-regex' => '(?P<ip>\S+) - - \[(?P<date>.*?) (?P<timezone>.*?)\] (?P<status>\S+) '
		                  . '\"\S+ (?P<path>.*?) \S+\" (?P<generation_time_micro>\S+)'); 
		
		self::executeLogImporter($logFile, $opts);
	}

	private static function executeLogImporter( $logFile, $options )
	{
		$python = Piwik_Common::isWindows() ? "C:\Python27\python.exe" : 'python';
		
		// create the command
		$cmd = $python
			 . ' "'.PIWIK_INCLUDE_PATH.'/misc/log-analytics/import_logs.py" ' # script loc
		     . '-ddd ' // debug
			 . '--url="'.self::getRootUrl().'tests/PHPUnit/proxy/" ' # proxy so that piwik uses test config files
			 ;
		
		foreach ($options as $name => $value)
		{
			$cmd .= $name;
			if ($value !== false)
			{
				$cmd .= '="'.$value.'"';
			}
			$cmd .= ' ';
		}
		
		$cmd .= '"'.$logFile.'" 2>&1';
		
		// run the command
		exec($cmd, $output, $result);
		if ($result !== 0)
		{
			throw new Exception("log importer failed: ".implode("\n", $output)."\n\ncommand used: $cmd");
		}
		
		return $output;
    }
}