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

Visit.test.php « Tracker « core « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4aff75d73f56beed86e6e45f65a98a10f814394f (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
<?php
if(!defined("PIWIK_PATH_TEST_TO_ROOT")) {
	define('PIWIK_PATH_TEST_TO_ROOT', getcwd().'/../../..');
}
if(!defined('PIWIK_CONFIG_TEST_INCLUDED'))
{
	require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php";
}

require_once 'Tracker/Visit.php';


require_once "Database.test.php";
class Test_Piwik_TrackerVisit extends Test_Database
{
	public function setUp()
	{
		$GLOBALS['PIWIK_TRACKER_MODE'] = true;
		parent::setUp();

		// setup the access layer
		$pseudoMockAccess = new FakeAccess;
		FakeAccess::$superUser = true;
		Zend_Registry::set('access', $pseudoMockAccess);
	}

	public function tearDown()
	{
		$GLOBALS['PIWIK_TRACKER_MODE'] = false;
	}

	function test_isVisitorIpExcluded()
	{
		$excludedIps = array(
			'12.12.12.12',
			'13.13.13.*',
			'14.14.*.*',
			'15.*.*.*',
			'255.1.1.1',
			'255.150.150.150',
			'155.*.*.*',
			'255.255.100.*'
		);
		$visit = new Test_Piwik_TrackerVisit_public();
		foreach($excludedIps as $idExcludedId => $excludedIp) 
		{
    		$idsite = Piwik_SitesManager_API::getInstance()->addSite("name","http://piwik.net/", $excludedIp);
    		$visit->setRequest(array('idsite' => $idsite));
    		
    		// test that IPs within the range, or the given IP, are excluded
    		$ips = $this->getIpsFromWildcardIp($excludedIp);
    		foreach($ips as $testIpIsExcluded)
    		{
        		$testIpIsExcluded = ip2long($testIpIsExcluded);
        		$this->assertTrue($testIpIsExcluded !== false);
        		$testIpIsExcluded = sprintf("%u", $testIpIsExcluded);
    			$this->assertTrue($visit->public_isVisitorIpExcluded($testIpIsExcluded), long2ip($testIpIsExcluded) . " is not excluded");
    		}
    		
    		// test that all other IPs (set as being exclusively out of any other IP ranges)
    		// are included in the tracking
    		foreach($excludedIps as $idIncludedIp => $includedIp)
    		{
    			if($idIncludedIp == $idExcludedId)
    			{
    				continue;
    			}
        		$ips = $this->getIpsFromWildcardIp($includedIp);
        		foreach($ips as $testIpIsIncluded)
        		{
            		$testIpIsIncluded = ip2long($testIpIsIncluded);
            		$this->assertTrue($testIpIsIncluded !== false);
            		$testIpIsIncluded = sprintf("%u", $testIpIsIncluded);
        			$this->assertFalse($visit->public_isVisitorIpExcluded($testIpIsIncluded), long2ip($testIpIsIncluded) . " is excluded by the rule ". $excludedIp);
        		}
    		}
		}
	}
	
	/**
	 * Given an IP (containing wildcards or not), returns IP within the range (replacing wildcards with proper values)
	 * @param $wildcardIp 145.65.*.*
	 * @return array (145.65.1.1, 145.65.255.255, etc.)
	 */
	private function getIpsFromWildcardIp($wildcardIp)
	{
		if(substr_count($wildcardIp, '*') === 0 )
		{
			return array($wildcardIp);
		}
		
		$ips = array();
		foreach(array(1,50,100,255) as $byte)
		{
			$ips[] = str_replace('*', $byte, $wildcardIp); 
		}
		return $ips;
	}
}



class Test_Piwik_TrackerVisit_public extends Piwik_Tracker_Visit {
	public function public_isVisitorIpExcluded( $ip )
	{
		return $this->isVisitorIpExcluded($ip);
	}
}