diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-31 02:10:11 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-10-31 02:11:20 +0300 |
commit | 1cd924d0371a2271798279b78e30585f4c7a80d9 (patch) | |
tree | d459921c818bda0a3f23fc53b9fcb60f92be3ced | |
parent | f5b6b4ccb021a3b082e578fc9a42c715985d1fae (diff) | |
parent | bca9fdab124fb27aa0a7c04da768011fba26589f (diff) |
Merge branch 'master' into php-di
Conflicts:
composer.json
composer.lock
61 files changed, 447 insertions, 899 deletions
diff --git a/.travis.yml b/.travis.yml index 536ada9496..e5e670be84 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ # do not edit this file manually, instead run the generate:travis-yml console command -# if you are a Piwik core developer, edit this template file to auto generate the .travis.yml: https://github.com/piwik/piwik/blob/master/plugins/CoreConsole/templates/travis.yml.twig +# if you are a Piwik core developer, edit this template file to auto generate the .travis.yml: https://github.com/piwik/piwik/blob/master/plugins/TestRunner/templates/travis.yml.twig # when modifying this file, please consider whether your changes should apply to plugin .travis.yml files. if not, check whether the pluginName twig var is empty, # otherwise your changes will be synced with every plugin that uses an auto generated .travis.yml @@ -88,7 +88,7 @@ before_install: install: # make sure travis test scripts are always latest (so in older releases/branches, the latest scripts will still be used) - git fetch - - git checkout master -- ./tests/travis ./plugins/CoreConsole || true + - git checkout master -- ./tests/travis ./plugins/TestRunner || true before_script: - if ([ -n "$TEST_SUITE" ]); diff --git a/CHANGELOG.md b/CHANGELOG.md index 700c488886..c2d56776b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ This is a changelog for Piwik platform developers. All changes for our HTTP API' ### Breaking Changes * Development related [console commands](http://developer.piwik.org/guides/piwik-on-the-command-line) are only available if the development mode is enabled. To enable the development mode execute `./console development:enable`. +### Deprecations +* Most methods of `Piwik\IP` have been deprecated in favor of the new [piwik/network](https://github.com/piwik/component-network) component. + +### Library updates +* Code for manipulating IP addresses has been moved to a separate standalone component: [piwik/network](https://github.com/piwik/component-network). Backward compatibility is kept in Piwik core. + ## Piwik 2.8.2 ### Library updates diff --git a/composer.json b/composer.json index 7120659b8d..91fffba957 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,7 @@ "mustangostang/spyc": "~0.5.0", "piwik/device-detector": "~2.0", "piwik/decompress": "~0.1.1", + "piwik/network": "~0.1.0", "mnapoli/php-di": "5.0.x-dev" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 75ee69696e..8790535c19 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "ad793a02e67244825709eb91b2280e59", + "hash": "92499fc432a629fefcd5c1b912e44d9c", "packages": [ { "name": "container-interop/container-interop", @@ -517,18 +517,50 @@ "time": "2014-10-06 20:13:03" }, { + "name": "piwik/network", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/piwik/component-network.git", + "reference": "9037fa29509f86767e02ba58a57d4deb1d01a844" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/piwik/component-network/zipball/9037fa29509f86767e02ba58a57d4deb1d01a844", + "reference": "9037fa29509f86767e02ba58a57d4deb1d01a844", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Piwik\\Network\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0" + ], + "time": "2014-10-23 03:30:23" + }, + { "name": "symfony/console", - "version": "v2.5.6", + "version": "v2.5.5", "target-dir": "Symfony/Component/Console", "source": { "type": "git", "url": "https://github.com/symfony/Console.git", - "reference": "6f177fca24200a5b97aef5ce7a5c98124a0f0db0" + "reference": "ca053eaa031c93afb68a71e4eb1f4168dfd4a661" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/6f177fca24200a5b97aef5ce7a5c98124a0f0db0", - "reference": "6f177fca24200a5b97aef5ce7a5c98124a0f0db0", + "url": "https://api.github.com/repos/symfony/Console/zipball/ca053eaa031c93afb68a71e4eb1f4168dfd4a661", + "reference": "ca053eaa031c93afb68a71e4eb1f4168dfd4a661", "shasum": "" }, "require": { @@ -569,7 +601,7 @@ ], "description": "Symfony Console Component", "homepage": "http://symfony.com", - "time": "2014-10-05 13:57:04" + "time": "2014-09-25 09:53:56" }, { "name": "tedivm/jshrink", @@ -614,16 +646,16 @@ }, { "name": "twig/twig", - "version": "v1.16.2", + "version": "v1.16.1", "source": { "type": "git", "url": "https://github.com/fabpot/Twig.git", - "reference": "42f758d9fe2146d1f0470604fc05ee43580873fc" + "reference": "7c4c01dcf578523cfcddf383641a4f12790270ec" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fabpot/Twig/zipball/42f758d9fe2146d1f0470604fc05ee43580873fc", - "reference": "42f758d9fe2146d1f0470604fc05ee43580873fc", + "url": "https://api.github.com/repos/fabpot/Twig/zipball/7c4c01dcf578523cfcddf383641a4f12790270ec", + "reference": "7c4c01dcf578523cfcddf383641a4f12790270ec", "shasum": "" }, "require": { @@ -667,7 +699,7 @@ "keywords": [ "templating" ], - "time": "2014-10-17 12:53:44" + "time": "2014-10-10 14:09:53" } ], "packages-dev": [ @@ -1260,16 +1292,16 @@ }, { "name": "phpunit/phpunit", - "version": "4.3.4", + "version": "4.3.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "23e4e0310f037aae873cc81b8658dbbb82878f71" + "reference": "5a0bc4dcbb7340c0a9a9bc3507854a67c564edc4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/23e4e0310f037aae873cc81b8658dbbb82878f71", - "reference": "23e4e0310f037aae873cc81b8658dbbb82878f71", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/5a0bc4dcbb7340c0a9a9bc3507854a67c564edc4", + "reference": "5a0bc4dcbb7340c0a9a9bc3507854a67c564edc4", "shasum": "" }, "require": { @@ -1280,7 +1312,7 @@ "ext-spl": "*", "php": ">=5.3.3", "phpunit/php-code-coverage": "~2.0", - "phpunit/php-file-iterator": "~1.3.2", + "phpunit/php-file-iterator": "~1.3.1", "phpunit/php-text-template": "~1.2", "phpunit/php-timer": "~1.0.2", "phpunit/phpunit-mock-objects": "~2.3", @@ -1330,7 +1362,7 @@ "testing", "xunit" ], - "time": "2014-10-22 11:43:12" + "time": "2014-10-16 16:53:00" }, { "name": "phpunit/phpunit-mock-objects", @@ -1505,16 +1537,16 @@ }, { "name": "sebastian/environment", - "version": "1.2.0", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d" + "reference": "6288ebbf6fa3ed2b2ff2d69c356fbaaf4f0971a7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/0d9bf79554d2a999da194a60416c15cf461eb67d", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6288ebbf6fa3ed2b2ff2d69c356fbaaf4f0971a7", + "reference": "6288ebbf6fa3ed2b2ff2d69c356fbaaf4f0971a7", "shasum": "" }, "require": { @@ -1526,7 +1558,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.2.x-dev" + "dev-master": "1.1.x-dev" } }, "autoload": { @@ -1551,7 +1583,7 @@ "environment", "hhvm" ], - "time": "2014-10-22 06:38:05" + "time": "2014-10-07 09:23:16" }, { "name": "sebastian/exporter", @@ -1655,17 +1687,17 @@ }, { "name": "symfony/event-dispatcher", - "version": "v2.5.6", + "version": "v2.5.5", "target-dir": "Symfony/Component/EventDispatcher", "source": { "type": "git", "url": "https://github.com/symfony/EventDispatcher.git", - "reference": "804eb28dbbfba9ffdab21fe2066744906cea2212" + "reference": "f6281337bf5f985f585d1db6a83adb05ce531f46" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/804eb28dbbfba9ffdab21fe2066744906cea2212", - "reference": "804eb28dbbfba9ffdab21fe2066744906cea2212", + "url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/f6281337bf5f985f585d1db6a83adb05ce531f46", + "reference": "f6281337bf5f985f585d1db6a83adb05ce531f46", "shasum": "" }, "require": { @@ -1708,21 +1740,21 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "http://symfony.com", - "time": "2014-10-01 15:43:05" + "time": "2014-09-28 15:56:11" }, { "name": "symfony/yaml", - "version": "v2.5.6", + "version": "v2.5.5", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "2d9f527449cabfa8543dd7fa3a466d6ae83d6726" + "reference": "b1dbc53593b98c2d694ebf383660ac9134d30b96" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/2d9f527449cabfa8543dd7fa3a466d6ae83d6726", - "reference": "2d9f527449cabfa8543dd7fa3a466d6ae83d6726", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/b1dbc53593b98c2d694ebf383660ac9134d30b96", + "reference": "b1dbc53593b98c2d694ebf383660ac9134d30b96", "shasum": "" }, "require": { @@ -1755,7 +1787,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-10-01 05:50:18" + "time": "2014-09-22 09:14:18" } ], "aliases": [], diff --git a/config/global.ini.php b/config/global.ini.php index 8413d9dc93..bc14fce1e4 100644 --- a/config/global.ini.php +++ b/config/global.ini.php @@ -48,7 +48,7 @@ aws_keyname = "" aws_pem_file = "<path to pem file>" aws_securitygroups[] = "default" aws_region = "us-east-1" -aws_ami = "ami-ce088ba6" +aws_ami = "ami-609c1e08" aws_instance_type = "c3.large" [log] diff --git a/core/Columns/Dimension.php b/core/Columns/Dimension.php index d591cea9db..e760a09083 100644 --- a/core/Columns/Dimension.php +++ b/core/Columns/Dimension.php @@ -56,11 +56,11 @@ abstract class Dimension * dimension. Example: * * ``` - $segment = new Segment(); - $segment->setSegment('exitPageUrl'); - $segment->setName('Actions_ColumnExitPageURL'); - $segment->setCategory('General_Visit'); - $this->addSegment($segment); + * $segment = new Segment(); + * $segment->setSegment('exitPageUrl'); + * $segment->setName('Actions_ColumnExitPageURL'); + * $segment->setCategory('General_Visit'); + * $this->addSegment($segment); * ``` */ protected function configureSegments() diff --git a/core/Exceptions/HtmlMessageException.php b/core/Exceptions/HtmlMessageException.php index 243a0f1726..69f809e1e0 100644 --- a/core/Exceptions/HtmlMessageException.php +++ b/core/Exceptions/HtmlMessageException.php @@ -16,15 +16,7 @@ use Exception; * * @api */ -class HtmlMessageException extends Exception +class HtmlMessageException extends Exception implements HtmlMessageExceptionInterface { - /** - * Returns the exception message. - * - * @return string - */ - public function getHtmlMessage() - { - return $this->getMessage(); - } + // empty }
\ No newline at end of file diff --git a/core/Exceptions/HtmlMessageExceptionInterface.php b/core/Exceptions/HtmlMessageExceptionInterface.php new file mode 100644 index 0000000000..b96c2e0a84 --- /dev/null +++ b/core/Exceptions/HtmlMessageExceptionInterface.php @@ -0,0 +1,20 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Exceptions; + +/** + * Exceptions that implement this interface are assumed to have HTML content + * in their messages. + * + * @api + */ +interface HtmlMessageExceptionInterface +{ + // empty +}
\ No newline at end of file diff --git a/core/FrontController.php b/core/FrontController.php index a45a21da7c..85fbbc8994 100644 --- a/core/FrontController.php +++ b/core/FrontController.php @@ -13,6 +13,7 @@ use Exception; use Piwik\API\Request; use Piwik\API\ResponseBuilder; use Piwik\Exceptions\HtmlMessageException; +use Piwik\Exceptions\HtmlMessageExceptionInterface; use Piwik\Http\Router; use Piwik\Plugin\Controller; use Piwik\Plugin\Report; @@ -614,14 +615,24 @@ class FrontController extends Singleton { $debugTrace = $ex->getTraceAsString(); - if (method_exists($ex, 'getHtmlMessage')) { - $message = $ex->getHtmlMessage(); + if ($ex instanceof HtmlMessageExceptionInterface) { + $message = $ex->getMessage(); } else { $message = Common::sanitizeInputValue($ex->getMessage()); } $logo = new CustomLogo(); - $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true, $logo->getHeaderLogoUrl(), $logo->getPathUserFavicon()); + + $logoHeaderUrl = false; + $logoFaviconUrl = false; + try { + $logoHeaderUrl = $logo->getHeaderLogoUrl(); + $logoFaviconUrl = $logo->getPathUserFavicon(); + } catch (Exception $ex) { + Log::debug($ex); + } + + $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true, $logoHeaderUrl, $logoFaviconUrl); /** * Triggered before a Piwik error page is displayed to the user. diff --git a/core/IP.php b/core/IP.php index 691bc7442c..e952ddbe27 100644 --- a/core/IP.php +++ b/core/IP.php @@ -9,9 +9,20 @@ namespace Piwik; +use Piwik\Network\IPUtils; +use Piwik\Network\IPv4; +use Piwik\Network\IPv6; + /** * Contains IP address helper functions (for both IPv4 and IPv6). * + * As of Piwik 2.9, most methods in this class are deprecated. You are + * encouraged to use classes from the Piwik "Network" component: + * + * @see \Piwik\Network\IP + * @see \Piwik\Network\IPUtils + * @link https://github.com/piwik/component-network + * * As of Piwik 1.3, IP addresses are stored in the DB has VARBINARY(16), * and passed around in network address format which has the advantage of * being in big-endian byte order. This allows for binary-safe string @@ -28,47 +39,18 @@ namespace Piwik; */ class IP { - const MAPPED_IPv4_START = '::ffff:'; - /** * Removes the port and the last portion of a CIDR IP address. * * @param string $ipString The IP address to sanitize. * @return string + * + * @deprecated Use IPUtils::sanitizeIp() instead + * @see \Piwik\Network\IPUtils */ public static function sanitizeIp($ipString) { - $ipString = trim($ipString); - - // CIDR notation, A.B.C.D/E - $posSlash = strrpos($ipString, '/'); - if ($posSlash !== false) { - $ipString = substr($ipString, 0, $posSlash); - } - - $posColon = strrpos($ipString, ':'); - $posDot = strrpos($ipString, '.'); - if ($posColon !== false) { - // IPv6 address with port, [A:B:C:D:E:F:G:H]:EEEE - $posRBrac = strrpos($ipString, ']'); - if ($posRBrac !== false && $ipString[0] == '[') { - $ipString = substr($ipString, 1, $posRBrac - 1); - } - - if ($posDot !== false) { - // IPv4 address with port, A.B.C.D:EEEE - if ($posColon > $posDot) { - $ipString = substr($ipString, 0, $posColon); - } - // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J - } else if (strpos($ipString, ':') === $posColon) { - $ipString = substr($ipString, 0, $posColon); - } - // else: IPv6 address, A:B:C:D:E:F:G:H - } - // else: IPv4 address, A.B.C.D - - return $ipString; + return IPUtils::sanitizeIp($ipString); } /** @@ -83,43 +65,15 @@ class IP * * @param string $ipRangeString IP address range * @return string|bool IP address range in CIDR notation OR false + * + * @deprecated Use IPUtils::sanitizeIpRange() instead + * @see \Piwik\Network\IPUtils */ public static function sanitizeIpRange($ipRangeString) { - $ipRangeString = trim($ipRangeString); - if (empty($ipRangeString)) { - return false; - } + $result = IPUtils::sanitizeIpRange($ipRangeString); - // IPv4 address with wildcards '*' - if (strpos($ipRangeString, '*') !== false) { - if (preg_match('~(^|\.)\*\.\d+(\.|$)~D', $ipRangeString)) { - return false; - } - - $bits = 32 - 8 * substr_count($ipRangeString, '*'); - $ipRangeString = str_replace('*', '0', $ipRangeString); - } - - // CIDR - if (($pos = strpos($ipRangeString, '/')) !== false) { - $bits = substr($ipRangeString, $pos + 1); - $ipRangeString = substr($ipRangeString, 0, $pos); - } - - // single IP - if (($ip = @inet_pton($ipRangeString)) === false) - return false; - - $maxbits = strlen($ip) * 8; - if (!isset($bits)) - $bits = $maxbits; - - if ($bits < 0 || $bits > $maxbits) { - return false; - } - - return "$ipRangeString/$bits"; + return $result === null ? false : $result; } /** @@ -127,12 +81,13 @@ class IP * * @param string $ipString IP address, either IPv4 or IPv6, e.g., `"127.0.0.1"`. * @return string Binary-safe string, e.g., `"\x7F\x00\x00\x01"`. + * + * @deprecated Use IPUtils::stringToBinaryIP() instead + * @see \Piwik\Network\IPUtils */ public static function P2N($ipString) { - // use @inet_pton() because it throws an exception and E_WARNING on invalid input - $ip = @inet_pton($ipString); - return $ip === false ? "\x00\x00\x00\x00" : $ip; + return IPUtils::stringToBinaryIP($ipString); } /** @@ -142,12 +97,12 @@ class IP * * @param string $ip IP address in network address format. * @return string IP address in presentation format. + * + * @deprecated Use IPUtils::binaryToStringIP() instead */ public static function N2P($ip) { - // use @inet_ntop() because it throws an exception and E_WARNING on invalid input - $ipStr = @inet_ntop($ip); - return $ipStr === false ? '0.0.0.0' : $ipStr; + return IPUtils::binaryToStringIP($ip); } /** @@ -155,10 +110,12 @@ class IP * * @param string $ip IP address in network address format. * @return string IP address in presentation format. + * + * @deprecated Will be removed */ public static function prettyPrint($ip) { - return self::N2P($ip); + return IPUtils::binaryToStringIP($ip); } /** @@ -167,27 +124,15 @@ class IP * * @param string $ip IP address in network address format. * @return bool True if IPv4, else false. + * + * @deprecated Will be removed + * @see \Piwik\Network\IP */ public static function isIPv4($ip) { - // in case mbstring overloads strlen function - $strlen = function_exists('mb_orig_strlen') ? 'mb_orig_strlen' : 'strlen'; + $ip = Network\IP::fromBinaryIP($ip); - // IPv4 - if ($strlen($ip) == 4) { - return true; - } - - // IPv6 - transitional address? - if ($strlen($ip) == 16) { - if (substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 0, 12) === 0 - || substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0 - ) { - return true; - } - } - - return false; + return $ip instanceof IPv4; } /** @@ -200,12 +145,14 @@ class IP * * @param string $ip IPv4 address in network address format. * @return string IP address in presentation format. + * + * @deprecated This method was kept for backward compatibility and doesn't seem used */ public static function long2ip($ip) { // IPv4 if (strlen($ip) == 4) { - return self::N2P($ip); + return IPUtils::binaryToStringIP($ip); } // IPv6 - transitional address? @@ -214,7 +161,7 @@ class IP || substr_compare($ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0 ) { // remap 128-bit IPv4-mapped and IPv4-compat addresses - return self::N2P(substr($ip, 12)); + return IPUtils::binaryToStringIP(substr($ip, 12)); } } @@ -227,10 +174,15 @@ class IP * * @param string $ip * @return bool + * + * @deprecated Will be removed + * @see \Piwik\Network\IP */ public static function isIPv6($ip) { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); + $ip = Network\IP::fromBinaryIP($ip); + + return $ip instanceof IPv6; } /** @@ -238,10 +190,19 @@ class IP * * @param string $ip * @return bool + * + * @deprecated Will be removed + * @see \Piwik\Network\IP */ public static function isMappedIPv4($ip) { - return substr($ip, 0, strlen(self::MAPPED_IPv4_START)) === self::MAPPED_IPv4_START; + $ip = Network\IP::fromStringIP($ip); + + if (! $ip instanceof IPv6) { + return false; + } + + return $ip->isMappedIPv4(); } /** @@ -249,10 +210,15 @@ class IP * * @param string $ip eg, `'::ffff:192.0.2.128'` * @return string eg, `'192.0.2.128'` + * + * @deprecated Use Piwik\Network\IP::toIPv4String() instead + * @see \Piwik\Network\IP */ public static function getIPv4FromMappedIPv6($ip) { - return substr($ip, strlen(self::MAPPED_IPv4_START)); + $ip = Network\IP::fromStringIP($ip); + + return $ip->toIPv4String(); } /** @@ -260,37 +226,15 @@ class IP * * @param array $ipRange An IP address range in presentation format. * @return array|bool Array `array($lowIp, $highIp)` in network address format, or false on failure. + * + * @deprecated Use Piwik\Network\IPUtils::getIPRangeBounds() instead + * @see \Piwik\Network\IPUtils */ public static function getIpsForRange($ipRange) { - if (strpos($ipRange, '/') === false) { - $ipRange = self::sanitizeIpRange($ipRange); - } - $pos = strpos($ipRange, '/'); - - $bits = substr($ipRange, $pos + 1); - $range = substr($ipRange, 0, $pos); - $high = $low = @inet_pton($range); - if ($low === false) { - return false; - } + $result = IPUtils::getIPRangeBounds($ipRange); - $lowLen = strlen($low); - $i = $lowLen - 1; - $bits = $lowLen * 8 - $bits; - - for ($n = (int)($bits / 8); $n > 0; $n--, $i--) { - $low[$i] = chr(0); - $high[$i] = chr(255); - } - - $n = $bits % 8; - if ($n) { - $low[$i] = chr(ord($low[$i]) & ~((1 << $n) - 1)); - $high[$i] = chr(ord($high[$i]) | ((1 << $n) - 1)); - } - - return array($low, $high); + return $result === null ? false : $result; } /** @@ -301,40 +245,15 @@ class IP * @param string $ip IP address in network address format * @param array $ipRanges List of IP address ranges * @return bool True if in any of the specified IP address ranges; else false. + * + * @deprecated Use Piwik\Network\IP::isInRanges() instead + * @see \Piwik\Network\IP */ public static function isIpInRange($ip, $ipRanges) { - $ipLen = strlen($ip); - if (empty($ip) || empty($ipRanges) || ($ipLen != 4 && $ipLen != 16)) { - return false; - } + $ip = Network\IP::fromBinaryIP($ip); - foreach ($ipRanges as $range) { - if (is_array($range)) { - // already split into low/high IP addresses - $range[0] = self::P2N($range[0]); - $range[1] = self::P2N($range[1]); - } else { - // expect CIDR format but handle some variations - $range = self::getIpsForRange($range); - } - if ($range === false) { - continue; - } - - $low = $range[0]; - $high = $range[1]; - if (strlen($low) != $ipLen) { - continue; - } - - // binary-safe string comparison - if ($ip >= $low && $ip <= $high) { - return true; - } - } - - return false; + return $ip->isInRanges($ipRanges); } /** @@ -356,7 +275,7 @@ class IP } $ipString = self::getNonProxyIpFromHeader($default, $clientHeaders); - return self::sanitizeIp($ipString); + return IPUtils::sanitizeIp($ipString); } /** @@ -406,7 +325,8 @@ class IP $elements = explode(',', $csv); for ($i = count($elements); $i--;) { $element = trim(Common::sanitizeInputValue($elements[$i])); - if (empty($excludedIps) || (!in_array($element, $excludedIps) && !self::isIpInRange(self::P2N(self::sanitizeIp($element)), $excludedIps))) { + $ip = \Piwik\Network\IP::fromStringIP(IPUtils::sanitizeIp($element)); + if (empty($excludedIps) || (!in_array($element, $excludedIps) && !$ip->isInRanges($excludedIps))) { return $element; } } @@ -415,16 +335,20 @@ class IP } /** - * Retirms the hostname for a given IP address. + * Returns the hostname for a given IP address. * * @param string $ipStr Human-readable IP address. * @return string The hostname or unmodified $ipStr on failure. + * + * @deprecated Use Piwik\Network\IP::getHostname() instead + * @see \Piwik\Network\IP */ public static function getHostByAddr($ipStr) { - // PHP's reverse lookup supports ipv4 and ipv6 - // except on Windows before PHP 5.3 - $host = strtolower(@gethostbyaddr($ipStr)); - return $host === '' ? $ipStr : $host; + $ip = Network\IP::fromStringIP($ipStr); + + $host = $ip->getHostname(); + + return $host === null ? $ipStr : $host; } } diff --git a/core/Tracker/Request.php b/core/Tracker/Request.php index 36b1e3a5b9..b733ae30ea 100644 --- a/core/Tracker/Request.php +++ b/core/Tracker/Request.php @@ -13,6 +13,7 @@ use Piwik\Common; use Piwik\Config; use Piwik\Cookie; use Piwik\IP; +use Piwik\Network\IPUtils; use Piwik\Piwik; use Piwik\Plugins\CustomVariables\CustomVariables; use Piwik\Registry; @@ -563,9 +564,7 @@ class Request public function getIp() { - $ipString = $this->getIpString(); - $ip = IP::P2N($ipString); - return $ip; + return IPUtils::stringToBinaryIP($this->getIpString()); } public function getForcedUserId() diff --git a/core/Tracker/Visit.php b/core/Tracker/Visit.php index c8fb12d389..8bdf641ece 100644 --- a/core/Tracker/Visit.php +++ b/core/Tracker/Visit.php @@ -11,7 +11,7 @@ namespace Piwik\Tracker; use Piwik\Common; use Piwik\Config; -use Piwik\IP; +use Piwik\Network\IPUtils; use Piwik\Piwik; use Piwik\Plugin\Dimension\VisitDimension; use Piwik\Tracker; @@ -241,7 +241,7 @@ class Visit implements VisitInterface */ protected function handleExistingVisit($visitor, $action, $visitIsConverted) { - Common::printDebug("Visit is known (IP = " . IP::N2P($this->getVisitorIp()) . ")"); + Common::printDebug("Visit is known (IP = " . IPUtils::binaryToStringIP($this->getVisitorIp()) . ")"); $valuesToUpdate = $this->getExistingVisitFieldsToUpdate($visitor, $action, $visitIsConverted); @@ -301,7 +301,7 @@ class Visit implements VisitInterface */ protected function handleNewVisit($visitor, $action, $visitIsConverted) { - Common::printDebug("New Visit (IP = " . IP::N2P($this->getVisitorIp()) . ")"); + Common::printDebug("New Visit (IP = " . IPUtils::binaryToStringIP($this->getVisitorIp()) . ")"); $this->setNewVisitorInformation($visitor); @@ -467,7 +467,7 @@ class Visit implements VisitInterface $debugVisitInfo = $this->visitorInfo; $debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']); $debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']); - $debugVisitInfo['location_ip'] = IP::N2P($debugVisitInfo['location_ip']); + $debugVisitInfo['location_ip'] = IPUtils::binaryToStringIP($debugVisitInfo['location_ip']); Common::printDebug($debugVisitInfo); } diff --git a/core/Tracker/VisitExcluded.php b/core/Tracker/VisitExcluded.php index 48eb1e3db7..ca481218cb 100644 --- a/core/Tracker/VisitExcluded.php +++ b/core/Tracker/VisitExcluded.php @@ -213,8 +213,9 @@ class VisitExcluded $websiteAttributes = Cache::getCacheWebsiteAttributes($this->idSite); if (!empty($websiteAttributes['excluded_ips'])) { - if (IP::isIpInRange($this->ip, $websiteAttributes['excluded_ips'])) { - Common::printDebug('Visitor IP ' . IP::N2P($this->ip) . ' is excluded from being tracked'); + $ip = \Piwik\Network\IP::fromBinaryIP($this->ip); + if ($ip->isInRanges($websiteAttributes['excluded_ips'])) { + Common::printDebug('Visitor IP ' . $ip->toString() . ' is excluded from being tracked'); return true; } } diff --git a/core/Url.php b/core/Url.php index 1d34c17ca3..dd9503ca66 100644 --- a/core/Url.php +++ b/core/Url.php @@ -10,6 +10,7 @@ namespace Piwik; use Exception; +use Piwik\Network\IPUtils; use Piwik\Session; /** @@ -547,7 +548,7 @@ class Url $disableHostCheck = Config::getInstance()->General['enable_trusted_host_check'] == 0; // compare scheme and host $parsedUrl = @parse_url($url); - $host = IP::sanitizeIp(@$parsedUrl['host']); + $host = IPUtils::sanitizeIp(@$parsedUrl['host']); return !empty($host) && ($disableHostCheck || in_array($host, $hosts)) && !empty($parsedUrl['scheme']) @@ -585,7 +586,7 @@ class Url */ public static function getHostSanitized($host) { - return IP::sanitizeIp($host); + return IPUtils::sanitizeIp($host); } protected static function getHostsFromConfig($domain, $key) diff --git a/misc/others/geoipUpdateRows.php b/misc/others/geoipUpdateRows.php index a7564d25ba..5f36ed5e7d 100755 --- a/misc/others/geoipUpdateRows.php +++ b/misc/others/geoipUpdateRows.php @@ -1,10 +1,8 @@ <?php use Piwik\Common; -use Piwik\Config; use Piwik\Db; -use Piwik\FrontController; -use Piwik\IP; use Piwik\Log; +use Piwik\Network\IPUtils; use Piwik\Piwik; use Piwik\Plugins\UserCountry\LocationProvider\GeoIp\Pecl; use Piwik\Plugins\UserCountry\LocationProvider; @@ -183,7 +181,7 @@ for (; $start < $end; $start += $limit) { continue; } - $ip = IP::N2P($row['location_ip']); + $ip = IPUtils::binaryToStringIP($row['location_ip']); $location = $provider->getLocation(array('ip' => $ip)); if (!empty($location[LocationProvider::COUNTRY_CODE_KEY])) { diff --git a/plugins/LanguagesManager/Commands/CompareKeys.php b/plugins/LanguagesManager/Commands/CompareKeys.php index 821342a90c..26c9a737f3 100644 --- a/plugins/LanguagesManager/Commands/CompareKeys.php +++ b/plugins/LanguagesManager/Commands/CompareKeys.php @@ -9,7 +9,6 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Piwik\Translate; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; @@ -18,7 +17,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** */ -class CompareKeys extends ConsoleCommand +class CompareKeys extends TranslationBase { protected function configure() { diff --git a/plugins/LanguagesManager/Commands/CreatePull.php b/plugins/LanguagesManager/Commands/CreatePull.php index 036547ec67..356f4e213f 100644 --- a/plugins/LanguagesManager/Commands/CreatePull.php +++ b/plugins/LanguagesManager/Commands/CreatePull.php @@ -9,7 +9,6 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Piwik\Plugins\LanguagesManager\API; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; @@ -18,7 +17,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** */ -class CreatePull extends ConsoleCommand +class CreatePull extends TranslationBase { protected function configure() { diff --git a/plugins/LanguagesManager/Commands/FetchFromOTrance.php b/plugins/LanguagesManager/Commands/FetchFromOTrance.php index c55b33a1a7..b1149a9afe 100644 --- a/plugins/LanguagesManager/Commands/FetchFromOTrance.php +++ b/plugins/LanguagesManager/Commands/FetchFromOTrance.php @@ -9,7 +9,6 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Piwik\Unzip; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -17,7 +16,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** */ -class FetchFromOTrance extends ConsoleCommand +class FetchFromOTrance extends TranslationBase { const DOWNLOADPATH = 'tmp/oTrance'; diff --git a/plugins/LanguagesManager/Commands/LanguageCodes.php b/plugins/LanguagesManager/Commands/LanguageCodes.php index 1a4fad066b..3491295782 100644 --- a/plugins/LanguagesManager/Commands/LanguageCodes.php +++ b/plugins/LanguagesManager/Commands/LanguageCodes.php @@ -9,14 +9,13 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Piwik\Plugins\LanguagesManager\API; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; /** */ -class LanguageCodes extends ConsoleCommand +class LanguageCodes extends TranslationBase { protected function configure() { diff --git a/plugins/LanguagesManager/Commands/LanguageNames.php b/plugins/LanguagesManager/Commands/LanguageNames.php index 2bb590882e..7dcd86bf35 100644 --- a/plugins/LanguagesManager/Commands/LanguageNames.php +++ b/plugins/LanguagesManager/Commands/LanguageNames.php @@ -9,14 +9,13 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Piwik\Plugins\LanguagesManager\API; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; /** */ -class LanguageNames extends ConsoleCommand +class LanguageNames extends TranslationBase { protected function configure() { diff --git a/plugins/LanguagesManager/Commands/PluginsWithTranslations.php b/plugins/LanguagesManager/Commands/PluginsWithTranslations.php index 1d8afc2964..69132ab2e9 100644 --- a/plugins/LanguagesManager/Commands/PluginsWithTranslations.php +++ b/plugins/LanguagesManager/Commands/PluginsWithTranslations.php @@ -9,13 +9,12 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; /** */ -class PluginsWithTranslations extends ConsoleCommand +class PluginsWithTranslations extends TranslationBase { protected function configure() { diff --git a/plugins/LanguagesManager/Commands/SetTranslations.php b/plugins/LanguagesManager/Commands/SetTranslations.php index 356481905e..89a4994670 100644 --- a/plugins/LanguagesManager/Commands/SetTranslations.php +++ b/plugins/LanguagesManager/Commands/SetTranslations.php @@ -9,7 +9,6 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Piwik\Plugins\LanguagesManager\API; use Piwik\Translate\Filter\ByBaseTranslations; use Piwik\Translate\Filter\ByParameterCount; @@ -25,7 +24,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** */ -class SetTranslations extends ConsoleCommand +class SetTranslations extends TranslationBase { protected function configure() { diff --git a/plugins/LanguagesManager/Commands/TranslationBase.php b/plugins/LanguagesManager/Commands/TranslationBase.php new file mode 100644 index 0000000000..0f1b72bef4 --- /dev/null +++ b/plugins/LanguagesManager/Commands/TranslationBase.php @@ -0,0 +1,28 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\LanguagesManager\Commands; + +use Piwik\Development; +use Piwik\Plugin\ConsoleCommand; +use Piwik\Translate; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + */ +abstract class TranslationBase extends ConsoleCommand +{ + public function isEnabled() + { + return Development::isEnabled(); + } +} diff --git a/plugins/LanguagesManager/Commands/Update.php b/plugins/LanguagesManager/Commands/Update.php index 2692768262..7014ab7241 100644 --- a/plugins/LanguagesManager/Commands/Update.php +++ b/plugins/LanguagesManager/Commands/Update.php @@ -9,7 +9,6 @@ namespace Piwik\Plugins\LanguagesManager\Commands; -use Piwik\Plugin\ConsoleCommand; use Piwik\Plugins\LanguagesManager\API; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; @@ -19,7 +18,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** */ -class Update extends ConsoleCommand +class Update extends TranslationBase { protected function configure() { diff --git a/plugins/Live/Visitor.php b/plugins/Live/Visitor.php index 965a61ff8f..27873ec2a1 100644 --- a/plugins/Live/Visitor.php +++ b/plugins/Live/Visitor.php @@ -13,7 +13,7 @@ use Piwik\DataAccess\LogAggregator; use Piwik\DataTable\Filter\ColumnDelete; use Piwik\Date; use Piwik\Db; -use Piwik\IP; +use Piwik\Network\IPUtils; use Piwik\Piwik; use Piwik\Plugins\CustomVariables\CustomVariables; use Piwik\Plugins\UserCountry\LocationProvider\GeoIp; @@ -97,7 +97,7 @@ class Visitor implements VisitorInterface function getIp() { if (isset($this->details['location_ip'])) { - return IP::N2P($this->details['location_ip']); + return IPUtils::binaryToStringIP($this->details['location_ip']); } return null; } diff --git a/plugins/PrivacyManager/IPAnonymizer.php b/plugins/PrivacyManager/IPAnonymizer.php index de588c59fd..a70312916f 100644 --- a/plugins/PrivacyManager/IPAnonymizer.php +++ b/plugins/PrivacyManager/IPAnonymizer.php @@ -9,7 +9,7 @@ namespace Piwik\Plugins\PrivacyManager; use Piwik\Common; -use Piwik\IP; +use Piwik\Network\IP; /** * Anonymize visitor IP addresses to comply with the privacy laws/guidelines in countries, such as Germany. @@ -19,51 +19,36 @@ class IPAnonymizer /** * Internal function to mask portions of the visitor IP address * - * @param string $ip IP address in network address format + * @param IP $ip * @param int $maskLength Number of octets to reset - * @return string + * @return IP */ - public static function applyIPMask($ip, $maskLength) + public static function applyIPMask(IP $ip, $maskLength) { - // IPv4 or mapped IPv4 in IPv6 - if (IP::isIPv4($ip)) { - $i = strlen($ip); - if ($maskLength > $i) { - $maskLength = $i; - } + $newIpObject = $ip->anonymize($maskLength); - while ($maskLength-- > 0) { - $ip[--$i] = chr(0); - } - } else { - $masks = array( - 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', - 'ffff:ffff:ffff:ffff::', - 'ffff:ffff:ffff:0000::', - 'ffff:ff00:0000:0000::' - ); - $ip = $ip & pack('a16', inet_pton($masks[$maskLength])); - } - return $ip; + return $newIpObject; } /** * Hook on Tracker.Visit.setVisitorIp to anomymize visitor IP addresses + * @param string $ip IP address in binary format (network format) */ public function setVisitorIpAddress(&$ip) { + $ipObject = IP::fromBinaryIP($ip); + if (!$this->isActive()) { - Common::printDebug("Visitor IP was _not_ anonymized: ". IP::N2P($ip)); + Common::printDebug("Visitor IP was _not_ anonymized: ". $ipObject->toString()); return; } - $originalIp = $ip; - $privacyConfig = new Config(); - $ip = self::applyIPMask($ip, $privacyConfig->ipAddressMaskLength); + $newIpObject = self::applyIPMask($ipObject, $privacyConfig->ipAddressMaskLength); + $ip = $newIpObject->toBinary(); - Common::printDebug("Visitor IP (was: ". IP::N2P($originalIp) .") has been anonymized: ". IP::N2P($ip)); + Common::printDebug("Visitor IP (was: ". $ipObject->toString() .") has been anonymized: ". $newIpObject->toString()); } /** diff --git a/plugins/PrivacyManager/tests/Unit/AnonymizeIPTest.php b/plugins/PrivacyManager/tests/Unit/AnonymizeIPTest.php index 0ed4e6e4ca..d5e50e5209 100644 --- a/plugins/PrivacyManager/tests/Unit/AnonymizeIPTest.php +++ b/plugins/PrivacyManager/tests/Unit/AnonymizeIPTest.php @@ -8,7 +8,7 @@ namespace Piwik\Plugins\PrivacyManager\tests; -use Piwik\IP; +use Piwik\Network\IP; use Piwik\Plugins\PrivacyManager\IPAnonymizer; require_once PIWIK_INCLUDE_PATH . '/plugins/PrivacyManager/IPAnonymizer.php'; @@ -58,23 +58,26 @@ class AnonymizeIPTest extends \PHPUnit_Framework_TestCase * @dataProvider getipv4Addresses * @group Plugins */ - public function testApplyIPMask($ip, $expected) + public function testApplyIPMask($ipString, $expected) { + $ip = IP::fromStringIP($ipString); + // each IP is tested with 0 to 4 octets masked for ($maskLength = 0; $maskLength <= 4; $maskLength++) { - $res = IPAnonymizer::applyIPMask(IP::P2N($ip), $maskLength); - $this->assertEquals($expected[$maskLength], $res, "Got " . bin2hex($res) . ", Expected " . bin2hex($expected[$maskLength])); + $res = IPAnonymizer::applyIPMask($ip, $maskLength); + $this->assertEquals($expected[$maskLength], $res->toBinary(), "Got " . $res . ", Expected " . bin2hex($expected[$maskLength])); } // edge case (bounds check) - $this->assertEquals("\x00\x00\x00\x00", IPAnonymizer::applyIPMask(IP::P2N($ip), 5)); + $this->assertEquals("\x00\x00\x00\x00", IPAnonymizer::applyIPMask($ip, 5)->toBinary()); // mask IPv4 mapped addresses + $mappedIp = IP::fromStringIP('::ffff:' . $ipString); for ($maskLength = 0; $maskLength <= 4; $maskLength++) { - $res = IPAnonymizer::applyIPMask(IP::P2N('::ffff:' . $ip), $maskLength); - $this->assertEquals($res, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff" . $expected[$maskLength], "Got " . bin2hex($res) . ", Expected " . bin2hex($expected[$maskLength])); + $res = IPAnonymizer::applyIPMask($mappedIp, $maskLength); + $this->assertEquals("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff" . $expected[$maskLength], $res->toBinary(), "Got " . $res . ", Expected " . bin2hex($expected[$maskLength])); } - $this->assertEquals("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00", IPAnonymizer::applyIPMask(IP::P2N('::ffff:' . $ip), 5)); + $this->assertEquals("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00", IPAnonymizer::applyIPMask($mappedIp, 5)->toBinary()); } /** @@ -85,8 +88,8 @@ class AnonymizeIPTest extends \PHPUnit_Framework_TestCase { // each IP is tested with 0 to 4 octets masked for ($maskLength = 0; $maskLength < 4; $maskLength++) { - $res = IPAnonymizer::applyIPMask(IP::P2N($ip), $maskLength); - $this->assertEquals($expected[$maskLength], $res, "Got " . bin2hex($res) . ", Expected " . bin2hex($expected[$maskLength]) . ", Mask Level " . $maskLength); + $res = IPAnonymizer::applyIPMask(IP::fromStringIP($ip), $maskLength); + $this->assertEquals($expected[$maskLength], $res->toBinary(), "Got " . $res . ", Expected " . bin2hex($expected[$maskLength]) . ", Mask Level " . $maskLength); } } }
\ No newline at end of file diff --git a/plugins/Provider/Columns/Provider.php b/plugins/Provider/Columns/Provider.php index c20b75ac9c..cba1b8b9f5 100644 --- a/plugins/Provider/Columns/Provider.php +++ b/plugins/Provider/Columns/Provider.php @@ -10,6 +10,7 @@ namespace Piwik\Plugins\Provider\Columns; use Piwik\Common; use Piwik\IP; +use Piwik\Network\IPUtils; use Piwik\Piwik; use Piwik\Plugin\Dimension\VisitDimension; use Piwik\Plugin\Segment; @@ -54,7 +55,7 @@ class Provider extends VisitDimension $ip = $request->getIp(); } - $ip = IP::N2P($ip); + $ip = IPUtils::binaryToStringIP($ip); // In case the IP was anonymized, we should not continue since the DNS reverse lookup will fail and this will slow down tracking if (substr($ip, -2, 2) == '.0') { diff --git a/plugins/SitesManager/API.php b/plugins/SitesManager/API.php index 08beed366f..02f50a1ece 100644 --- a/plugins/SitesManager/API.php +++ b/plugins/SitesManager/API.php @@ -15,6 +15,7 @@ use Piwik\Date; use Piwik\Db; use Piwik\IP; use Piwik\MetricsFormatter; +use Piwik\Network\IPUtils; use Piwik\Option; use Piwik\Piwik; use Piwik\ProxyHttp; @@ -745,12 +746,12 @@ class API extends \Piwik\Plugin\API */ public function getIpsForRange($ipRange) { - $range = IP::getIpsForRange($ipRange); - if ($range === false) { + $range = IPUtils::getIPRangeBounds($ipRange); + if ($range === null) { return false; } - return array(IP::N2P($range[0]), IP::N2P($range[1])); + return array(IPUtils::binaryToStringIP($range[0]), IPUtils::binaryToStringIP($range[1])); } /** @@ -1322,7 +1323,7 @@ class API extends \Piwik\Plugin\API */ private function isValidIp($ip) { - return IP::getIpsForRange($ip) !== false; + return IPUtils::getIPRangeBounds($ip) !== null; } /** diff --git a/plugins/TestRunner/Aws/Instance.php b/plugins/TestRunner/Aws/Instance.php index e7bcb7a6ad..8afa292b66 100644 --- a/plugins/TestRunner/Aws/Instance.php +++ b/plugins/TestRunner/Aws/Instance.php @@ -80,6 +80,9 @@ class Instance public function launch() { + // user data is executed when instance launches, it is important that this file starts with "#!" + $userData = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/TestRunner/scripts/on_instance_launch.sh'); + $result = $this->client->runInstances(array( 'ImageId' => $this->config->getAmi(), 'MinCount' => 1, @@ -87,7 +90,8 @@ class Instance 'InstanceType' => $this->config->getInstanceType(), 'KeyName' => $this->config->getKeyName(), 'SecurityGroups' => $this->config->getSecurityGroups(), - 'InstanceInitiatedShutdownBehavior' => 'terminate' + 'InstanceInitiatedShutdownBehavior' => 'terminate', + 'UserData' => base64_encode($userData) )); $instanceIds = $result->getPath('Instances/*/InstanceId'); diff --git a/plugins/TestRunner/Aws/Ssh.php b/plugins/TestRunner/Aws/Ssh.php index b0aa300139..392c0136a2 100644 --- a/plugins/TestRunner/Aws/Ssh.php +++ b/plugins/TestRunner/Aws/Ssh.php @@ -27,7 +27,8 @@ class Ssh extends Net_SSH2 $ssh = new Ssh($host); if (!$ssh->login('ubuntu', $key)) { - throw new \RuntimeException("Login to $host using $pemFile failed"); + $error = error_get_last(); + throw new \RuntimeException("Login to $host using $pemFile failed: " . $error['message']); } return $ssh; diff --git a/plugins/CoreConsole/Commands/GenerateTravisYmlFile.php b/plugins/TestRunner/Commands/GenerateTravisYmlFile.php index 13bcc5ad2f..43f75ccb76 100644 --- a/plugins/CoreConsole/Commands/GenerateTravisYmlFile.php +++ b/plugins/TestRunner/Commands/GenerateTravisYmlFile.php @@ -7,11 +7,11 @@ * */ -namespace Piwik\Plugins\CoreConsole\Commands; +namespace Piwik\Plugins\TestRunner\Commands; use Piwik\View; use Piwik\Plugin\ConsoleCommand; -use Piwik\Plugins\CoreConsole\TravisYmlView; +use Piwik\Plugins\TestRunner\TravisYmlView; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; diff --git a/plugins/TestRunner/Commands/TestsRun.php b/plugins/TestRunner/Commands/TestsRun.php index 2b8c861e70..2f262280cb 100644 --- a/plugins/TestRunner/Commands/TestsRun.php +++ b/plugins/TestRunner/Commands/TestsRun.php @@ -31,7 +31,7 @@ class TestsRun extends ConsoleCommand $this->addOption('options', 'o', InputOption::VALUE_OPTIONAL, 'All options will be forwarded to phpunit', ''); $this->addOption('xhprof', null, InputOption::VALUE_NONE, 'Profile using xhprof.'); $this->addOption('file', null, InputOption::VALUE_REQUIRED, 'Execute tests within this file. Should be a path relative to the tests/PHPUnit directory.'); - $this->addOption('testsuite', null, InputOption::VALUE_REQUIRED, 'Execute tests of a specific test suite, for instance UnitTests, IntegrationTests or SystemTests.'); + $this->addOption('testsuite', null, InputOption::VALUE_REQUIRED, 'Execute tests of a specific test suite, for instance unit, integration or system.'); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/plugins/TestRunner/Commands/TestsSetupFixture.php b/plugins/TestRunner/Commands/TestsSetupFixture.php index 7e95fc1d37..8c053b1711 100644 --- a/plugins/TestRunner/Commands/TestsSetupFixture.php +++ b/plugins/TestRunner/Commands/TestsSetupFixture.php @@ -111,7 +111,7 @@ class TestsSetupFixture extends ConsoleCommand } } - $fixture = $this->createFixture($input); + $fixture = $this->createFixture($input, $allowSave = !empty($configDomainToSave)); $this->setupDatabaseOverrides($input, $fixture); @@ -188,7 +188,7 @@ class TestsSetupFixture extends ConsoleCommand } } - private function createFixture(InputInterface $input) + private function createFixture(InputInterface $input, $allowSave) { $fixtureClass = $input->getArgument('fixture'); if (class_exists("Piwik\\Tests\\Fixtures\\" . $fixtureClass)) { @@ -221,7 +221,7 @@ class TestsSetupFixture extends ConsoleCommand } if ($fixture->createConfig) { - Config::getInstance()->setTestEnvironment($pathLocal = null, $pathGlobal = null, $pathCommon = null, $allowSaving = true); + Config::getInstance()->setTestEnvironment($pathLocal = null, $pathGlobal = null, $pathCommon = null, $allowSave); } $fixture->createConfig = false; diff --git a/plugins/TestRunner/README.md b/plugins/TestRunner/README.md index 4005bb93b7..822b05dbba 100644 --- a/plugins/TestRunner/README.md +++ b/plugins/TestRunner/README.md @@ -1,18 +1,51 @@ # Piwik TestRunner Plugin -## Description - -Add your plugin description here. - ## FAQ -__My question?__ -My answer - -## Changelog - -Here goes the changelog text. - -## Support - -Please direct any feedback to ...
\ No newline at end of file +__Can I easily change the config for tests that run on AWS?__ + +Yes, just edit `Aws/config.ini.php + +__I want to run the tests with different parameters on AWS, is it possible?__ + +Yes, at the time of writing this you have to edit the file `Runner/Remote.php` + +__How can I change the base image (AMI) that is used for AWS tests?__ + +* Log in to AWS +* Select `EC2 => AMI` +* Launch a new instance of the current AMI by selecting it and pressing `Launch` +* Select a `c3.large` instance type +* Press `Review and Launch` and on next page `Launch` (in theory you have to select your keypair somewhere otherwise you will not be able to log in but I couldn't find where) +* Log in to the newly created instance. To get login information + * Go to `EC2 => Instances` + * Select the created instance + * Press `Connect` + * SSH connect example is listed there + * Make sure to use user `ubuntu` and not `root` +* Make changes on the instance +* When you are done + * Go into the home directory `cd` + * Clear the history: `cat /dev/null > ~/.bash_history && history -c` + * Execute `cd www/piwik`, then `exit`. Why? Whenever a new instance is created, those two commands will be in the history + and provides a better usability for the developer who accesses it as those two commands are most likely needed. + * Reflect the changes you did in Puppet https://github.com/piwik/piwik-dev-environment/tree/master/puppet/modules/piwik/manifests + or if you don't know Puppet at least add it in this shell script https://github.com/piwik/piwik-dev-environment/blob/master/puppet/files/setup.sh + For instance if you installed a new package you can simply add a new entry here https://github.com/piwik/piwik-dev-environment/blob/master/puppet/modules/piwik/manifests/base.pp +* In `EC2 => Instances` menu select the instance you are currently using. +* Select `Actions => Create Image` +* Define the name `Piwik Testing vX.X` and a description like `Used to run Piwik tests via Piwik console`. Make sure to increase the box version in X.X (have a look in `EC2 => AMI` for current version) +* Press `Create Image` +* Go to `EC2 => AMIs` menu and while waiting for the image creation to complete add the following tags + * `Name` => `PiwikTesting` + * `Ubuntu` => Ubuntu version eg `14.04` + * `BoxVersion` => Version of the box eg `3.3` + * `PHP` => PHP Version eg `5.5` + * `MySQL` => MySQL Version eg `5.5` +* Copy the assigned AMI ID and replace the config value `[tests]aws_ami = ...` in `global.ini.php` +* Once the AMI is available trigger an `integration`, `system`, and `ui` test run using the `tests:run-aws` command to make sure everything still works +* Commit / push the new AMI-ID +* Once everything works remove the outdated AMI by selecting it and clicking `Actions => Deregister`. + +In the future once everything is completely automated we would simple create a new instance out of ((Vagrant || Docker) && Puppet) whenever we need a change but it takes a lot of time to do this and is not worth it right now. +
\ No newline at end of file diff --git a/plugins/TestRunner/Runner/Remote.php b/plugins/TestRunner/Runner/Remote.php index d9eb580a4f..04753d6cea 100644 --- a/plugins/TestRunner/Runner/Remote.php +++ b/plugins/TestRunner/Runner/Remote.php @@ -32,7 +32,6 @@ class Remote $this->ssh->exec('git checkout ' . trim($gitHash)); $this->ssh->exec('git submodule update --recursive --force'); $this->ssh->exec('git submodule foreach git clean -f'); - $this->ssh->exec('sudo composer.phar self-update'); $this->ssh->exec('composer.phar install'); } diff --git a/plugins/CoreConsole/TravisYmlView.php b/plugins/TestRunner/TravisYmlView.php index 76af956572..809de91144 100644 --- a/plugins/CoreConsole/TravisYmlView.php +++ b/plugins/TestRunner/TravisYmlView.php @@ -7,7 +7,7 @@ * */ -namespace Piwik\Plugins\CoreConsole; +namespace Piwik\Plugins\TestRunner; use Piwik\Filesystem; use Piwik\View; @@ -55,7 +55,7 @@ class TravisYmlView extends View */ public function __construct() { - parent::__construct("@CoreConsole/travis.yml"); + parent::__construct("@TestRunner/travis.yml"); } /** diff --git a/plugins/TestRunner/scripts/on_instance_launch.sh b/plugins/TestRunner/scripts/on_instance_launch.sh new file mode 100644 index 0000000000..b00dc184a3 --- /dev/null +++ b/plugins/TestRunner/scripts/on_instance_launch.sh @@ -0,0 +1,9 @@ +#!/bin/bash +mkdir /tmp/ramdisk +mount -t tmpfs -o size=1536M tmpfs /tmp/ramdisk/ +mv /var/lib/mysql /tmp/ramdisk/mysql +ln -s /tmp/ramdisk/mysql/ /var/lib/mysql +chmod -R 770 /var/lib/mysql +chown -R ubuntu:ubuntu /var/lib/mysql +service mysql restart +composer-phar self-update
\ No newline at end of file diff --git a/plugins/CoreConsole/templates/travis.yml.twig b/plugins/TestRunner/templates/travis.yml.twig index 36b741af45..5f4ae51682 100644 --- a/plugins/CoreConsole/templates/travis.yml.twig +++ b/plugins/TestRunner/templates/travis.yml.twig @@ -1,6 +1,6 @@ # do not edit this file manually, instead run the generate:travis-yml console command {% if pluginName is empty %} -# if you are a Piwik core developer, edit this template file to auto generate the .travis.yml: https://github.com/piwik/piwik/blob/master/plugins/CoreConsole/templates/travis.yml.twig +# if you are a Piwik core developer, edit this template file to auto generate the .travis.yml: https://github.com/piwik/piwik/blob/master/plugins/TestRunner/templates/travis.yml.twig # when modifying this file, please consider whether your changes should apply to plugin .travis.yml files. if not, check whether the pluginName twig var is empty, # otherwise your changes will be synced with every plugin that uses an auto generated .travis.yml @@ -110,14 +110,14 @@ install: fi # make sure travis test scripts are always latest (so in older releases/branches, the latest scripts will still be used) - - git checkout master -- ./tests/travis ./plugins/CoreConsole + - git checkout master -- ./tests/travis ./plugins/TestRunner # clone dependent repos - ./tests/travis/checkout_dependent_plugins.sh {% else %} # make sure travis test scripts are always latest (so in older releases/branches, the latest scripts will still be used) - git fetch - - git checkout master -- ./tests/travis ./plugins/CoreConsole || true + - git checkout master -- ./tests/travis ./plugins/TestRunner || true {% endif %} {% if customTravisBuildSteps.install.after|default is not empty %} diff --git a/plugins/CoreConsole/tests/Integration/TravisYmlViewTest.php b/plugins/TestRunner/tests/Integration/TravisYmlViewTest.php index 7dd32b2b67..eea866f303 100644 --- a/plugins/CoreConsole/tests/Integration/TravisYmlViewTest.php +++ b/plugins/TestRunner/tests/Integration/TravisYmlViewTest.php @@ -6,24 +6,24 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * */ -namespace Piwik\Plugins\CoreConsole\tests\Integration; +namespace Piwik\Plugins\TestRunner\tests\Integration; use Symfony\Component\Console\Output\ConsoleOutput; -use Piwik\Plugins\CoreConsole\TravisYmlView; +use Piwik\Plugins\TestRunner\TravisYmlView; use Piwik\Plugin\Manager as PluginManager; use PHPUnit_Framework_TestCase; use Spyc; // DeviceDectector requires Spyc /** - * @group CoreConsole - * @group CoreConsole_TravisYmlViewTest + * @group TestRunner + * @group TestRunner_TravisYmlViewTest */ class TravisYmlViewTest extends PHPUnit_Framework_TestCase { public function setUp() { PluginManager::getInstance()->loadPlugin('Morpheus'); - PluginManager::getInstance()->loadPlugin('CoreConsole'); + PluginManager::getInstance()->loadPlugin('TestRunner'); } public function testViewGeneratesCorrectLookingYAML() @@ -61,7 +61,7 @@ class TravisYmlViewTest extends PHPUnit_Framework_TestCase $view->setPlugin('ExamplePlugin'); $view->setExtraGlobalEnvVars(array('secure: artifactspass', 'secure: githubtoken')); $view->setGenerateYmlCommand('./console generate:travis-yml arg1 arg2'); - $view->processExistingTravisYml(PIWIK_INCLUDE_PATH . '/plugins/CoreConsole/tests/resources/test.travis.yml'); + $view->processExistingTravisYml(PIWIK_INCLUDE_PATH . '/plugins/TestRunner/tests/resources/test.travis.yml'); $output = $view->render(); $yaml = Spyc::YAMLLoadString($output); diff --git a/plugins/CoreConsole/tests/resources/test.travis.yml b/plugins/TestRunner/tests/resources/test.travis.yml index e92dabeba1..e92dabeba1 100644 --- a/plugins/CoreConsole/tests/resources/test.travis.yml +++ b/plugins/TestRunner/tests/resources/test.travis.yml diff --git a/plugins/UserCountry/Columns/Base.php b/plugins/UserCountry/Columns/Base.php index c3ee461183..3e4678a0e9 100644 --- a/plugins/UserCountry/Columns/Base.php +++ b/plugins/UserCountry/Columns/Base.php @@ -9,12 +9,12 @@ namespace Piwik\Plugins\UserCountry\Columns; use Piwik\Common; +use Piwik\Network\IPUtils; use Piwik\Plugin\Dimension\VisitDimension; use Piwik\Plugins\UserCountry\LocationProvider\GeoIp; use Piwik\Plugins\UserCountry\LocationProvider; use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig; use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider; -use Piwik\IP; use Piwik\Tracker\Visitor; use Piwik\Tracker\Visit; use Piwik\Tracker\Request; @@ -111,7 +111,7 @@ abstract class Base extends VisitDimension $ip = $anonymizedIp; } - $ipAddress = IP::N2P($ip); + $ipAddress = IPUtils::binaryToStringIP($ip); return $ipAddress; } diff --git a/plugins/UserCountry/LocationProvider.php b/plugins/UserCountry/LocationProvider.php index d0ffd93f51..a5a656c42f 100755 --- a/plugins/UserCountry/LocationProvider.php +++ b/plugins/UserCountry/LocationProvider.php @@ -444,23 +444,17 @@ abstract class LocationProvider /** * Returns an IP address from an array that was passed into getLocation. This - * will return an IPv4 address or false if the address is IPv6 (IPv6 is not + * will return an IPv4 address or null if the address is IPv6 (IPv6 is not * supported yet). * * @param array $info Must have 'ip' key. - * @return string|bool + * @return string|null */ protected function getIpFromInfo($info) { - $ip = $info['ip']; - if (IP::isMappedIPv4($ip)) { - return IP::getIPv4FromMappedIPv6($ip); - } else if (IP::isIPv6($ip)) // IPv6 is not supported (yet) - { - return false; - } else { - return $ip; - } + $ip = \Piwik\Network\IP::fromStringIP($info['ip']); + + return $ip->toIPv4String(); } } diff --git a/plugins/VisitorGenerator b/plugins/VisitorGenerator -Subproject c12768d036739865c79e7108f60cc5ec0aa607e +Subproject 26f7a686abecca3fb04c91bf006b644eddb0527 diff --git a/tests/PHPUnit/Framework/Fixture.php b/tests/PHPUnit/Framework/Fixture.php index 51fa08ee8a..f61beffbb1 100644 --- a/tests/PHPUnit/Framework/Fixture.php +++ b/tests/PHPUnit/Framework/Fixture.php @@ -18,6 +18,7 @@ use Piwik\DbHelper; use Piwik\Log; use Piwik\Option; use Piwik\Piwik; +use Piwik\Plugin; use Piwik\Plugins\LanguagesManager\API as APILanguageManager; use Piwik\Plugins\MobileMessaging\MobileMessaging; use Piwik\Plugins\ScheduledReports\API as APIScheduledReports; @@ -40,6 +41,7 @@ use Piwik_LocalTracker; use Piwik\Updater; use Piwik\Plugins\CoreUpdater\CoreUpdater; use Exception; +use ReflectionClass; /** * Base type for all system test fixtures. System test fixtures @@ -119,7 +121,9 @@ class Fixture extends \PHPUnit_Framework_Assert } if ($this->persistFixtureData) { - return str_replace("\\", "_", get_class($this)); + $klass = new ReflectionClass($this); + $id = Plugin::getPluginNameFromNamespace($klass->getNamespaceName()) . "_" . $klass->getShortName(); + return $id; } return Config::getInstance()->database_tests['dbname']; diff --git a/tests/PHPUnit/Integration/JsProxyTest.php b/tests/PHPUnit/Integration/JsProxyTest.php index d95224a452..92a6461acc 100644 --- a/tests/PHPUnit/Integration/JsProxyTest.php +++ b/tests/PHPUnit/Integration/JsProxyTest.php @@ -60,7 +60,7 @@ class Test_Piwik_JsProxy extends PHPUnit_Framework_TestCase $responseInfo = curl_getinfo($curlHandle); curl_close($curlHandle); - $this->assertEquals($responseInfo["http_code"], 200, 'Ok response'); + $this->assertEquals(200, $responseInfo["http_code"], var_export($responseInfo, true)); $expected = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="; $processed = base64_encode($fullResponse); if ($expected != $processed) { diff --git a/tests/PHPUnit/Integration/PiwikTest.php b/tests/PHPUnit/Integration/PiwikTest.php index 9e1bfdb95a..46cc79f0d2 100644 --- a/tests/PHPUnit/Integration/PiwikTest.php +++ b/tests/PHPUnit/Integration/PiwikTest.php @@ -239,9 +239,4 @@ class Core_PiwikTest extends IntegrationTestCase { $this->assertEquals($expected, Piwik::isAssociativeArray($array)); } - - public function testCheckIfFileSystemIsNFSOnNonNFS() - { - $this->assertFalse(Filesystem::checkIfFileSystemIsNFS()); - } } diff --git a/tests/PHPUnit/Integration/ReleaseCheckListTest.php b/tests/PHPUnit/Integration/ReleaseCheckListTest.php index 9efa43917e..eb542c08da 100644 --- a/tests/PHPUnit/Integration/ReleaseCheckListTest.php +++ b/tests/PHPUnit/Integration/ReleaseCheckListTest.php @@ -114,7 +114,7 @@ class Test_Piwik_ReleaseCheckListTest extends PHPUnit_Framework_TestCase $patternFailIfFound = 'dump('; $files = Filesystem::globr(PIWIK_INCLUDE_PATH . '/plugins', '*.twig'); foreach ($files as $file) { - if ($file == PIWIK_INCLUDE_PATH . '/plugins/CoreConsole/templates/travis.yml.twig') { + if ($file == PIWIK_INCLUDE_PATH . '/plugins/TestRunner/templates/travis.yml.twig') { continue; } diff --git a/tests/PHPUnit/Integration/Tracker/VisitTest.php b/tests/PHPUnit/Integration/Tracker/VisitTest.php index 0d1837e2c1..292b98737d 100644 --- a/tests/PHPUnit/Integration/Tracker/VisitTest.php +++ b/tests/PHPUnit/Integration/Tracker/VisitTest.php @@ -5,8 +5,9 @@ * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ + use Piwik\Access; -use Piwik\IP; +use Piwik\Network\IPUtils; use Piwik\Plugins\SitesManager\API; use Piwik\Tracker\Request; use Piwik\Tracker\VisitExcluded; @@ -86,7 +87,7 @@ class Core_Tracker_VisitTest extends IntegrationTestCase // test that IPs within the range, or the given IP, are excluded foreach ($tests as $ip => $expected) { - $testIpIsExcluded = IP::P2N($ip); + $testIpIsExcluded = IPUtils::stringToBinaryIP($ip); $excluded = new VisitExcluded_public($request, $testIpIsExcluded); $this->assertSame($expected, $excluded->public_isVisitorIpExcluded($testIpIsExcluded)); @@ -195,7 +196,7 @@ class Core_Tracker_VisitTest extends IntegrationTestCase $request = new Request(array('idsite' => $idsite, 'bots' => 0)); foreach ($isIpBot as $ip => $isBot) { - $excluded = new VisitExcluded_public($request, IP::P2N($ip)); + $excluded = new VisitExcluded_public($request, IPUtils::stringToBinaryIP($ip)); $this->assertSame($isBot, $excluded->public_isNonHumanBot(), $ip); } diff --git a/tests/PHPUnit/IntegrationTestCase.php b/tests/PHPUnit/IntegrationTestCase.php index c9e2d818a4..47c2f16bff 100644 --- a/tests/PHPUnit/IntegrationTestCase.php +++ b/tests/PHPUnit/IntegrationTestCase.php @@ -7,7 +7,7 @@ */ /** - * @deprecated since 2.8.0 extend \Piwik\Tests\Framework\TestCase\IntegrationTestCase instead + * @deprecated since 2.8.0 extend \Piwik\Tests\Framework\TestCase\SystemTestCase instead */ class IntegrationTestCase extends \Piwik\Tests\Framework\TestCase\SystemTestCase { diff --git a/tests/PHPUnit/Integration/CliMultiTest.php b/tests/PHPUnit/System/CliMultiTest.php index 8cf407967b..0802a7cc67 100644 --- a/tests/PHPUnit/Integration/CliMultiTest.php +++ b/tests/PHPUnit/System/CliMultiTest.php @@ -145,7 +145,7 @@ class Core_CliMultiTest extends SystemTestCase { $this->skipWhenPhp53(); $response = $this->cliMulti->request(array($this->completeUrl(''))); - $this->assertStringStartsWith('Error: no website was found', $response[0]); + $this->assertStringStartsWith('Error in Piwik: Error: no website was found', $response[0]); } public function test_request_shouldBeAbleToRenderARegularPageInPiwik() diff --git a/tests/PHPUnit/UI b/tests/PHPUnit/UI -Subproject 978394246973b7ad97ae41df6b83cb3ab08a7b9 +Subproject 990155a77da0d587cc3750ba41cf663bac8c998 diff --git a/tests/PHPUnit/Unit/AssetManagerTest.php b/tests/PHPUnit/Unit/AssetManagerTest.php index 5d3e709004..ca1ca94433 100644 --- a/tests/PHPUnit/Unit/AssetManagerTest.php +++ b/tests/PHPUnit/Unit/AssetManagerTest.php @@ -239,23 +239,9 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase ); } - private function clearDateCache() + private function getAssetContent() { - clearstatcache(); - } - - /** - * @return int - */ - private function waitAndGetModificationDate() - { - $this->clearDateCache(); - - sleep(1.5); - - $modificationDate = $this->mergedAsset->getModificationDate(); - - return $modificationDate; + return $this->mergedAsset->getContent(); } /** @@ -393,23 +379,19 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase } /** - * @param int $previousDate + * @param string $previousContent */ - private function validateDateDidNotChange($previousDate) + private function assertAssetContentIsSameAs($previousContent) { - $this->clearDateCache(); - - $this->assertEquals($previousDate, $this->mergedAsset->getModificationDate()); + $this->assertEquals($previousContent, $this->getAssetContent()); } /** - * @param int $previousDate + * @param string $previousContent */ - private function validateDateIsMoreRecent($previousDate) + private function assertAssetContentChanged($previousContent) { - $this->clearDateCache(); - - $this->assertTrue($previousDate < $this->mergedAsset->getModificationDate()); + $this->assertNotEquals($previousContent, $this->getAssetContent()); } /** @@ -496,11 +478,11 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase $this->triggerGetMergedCoreJavaScript(); - $modDateBeforeSecondRequest = $this->waitAndGetModificationDate(); + $content = $this->getAssetContent(); $this->triggerGetMergedCoreJavaScript(); - $this->validateDateDidNotChange($modDateBeforeSecondRequest); + $this->assertAssetContentIsSameAs($content); } /** @@ -514,13 +496,13 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase $this->triggerGetMergedCoreJavaScript(); - $modDateBeforeSecondRequest = $this->waitAndGetModificationDate(); + $content = $this->getAssetContent(); $this->setJSCacheBuster(self::SECOND_CACHE_BUSTER_JS); $this->triggerGetMergedCoreJavaScript(); - $this->validateDateIsMoreRecent($modDateBeforeSecondRequest); + $this->assertAssetContentChanged($content); $this->validateMergedCoreJs(); } @@ -549,13 +531,13 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase $this->triggerGetMergedStylesheet(); - $modDateBeforeSecondRequest = $this->waitAndGetModificationDate(); + $content = $this->getAssetContent(); $this->setStylesheetCacheBuster(self::SECOND_CACHE_BUSTER_SS); $this->triggerGetMergedStylesheet(); - $this->validateDateIsMoreRecent($modDateBeforeSecondRequest); + $this->assertAssetContentChanged($content); $this->validateMergedStylesheet(); } @@ -572,13 +554,13 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase $this->triggerGetMergedStylesheet(); - $modDateBeforeSecondRequest = $this->waitAndGetModificationDate(); + $content = $this->getAssetContent(); $this->setStylesheetCacheBuster(self::SECOND_CACHE_BUSTER_SS); $this->triggerGetMergedStylesheet(); - $this->validateDateIsMoreRecent($modDateBeforeSecondRequest); + $this->assertAssetContentChanged($content); $this->validateMergedStylesheet(); } @@ -594,11 +576,11 @@ class AssetManagerTest extends PHPUnit_Framework_TestCase $this->triggerGetMergedStylesheet(); - $modDateBeforeSecondRequest = $this->waitAndGetModificationDate(); + $content = $this->getAssetContent(); $this->triggerGetMergedStylesheet(); - $this->validateDateDidNotChange($modDateBeforeSecondRequest); + $this->assertAssetContentIsSameAs($content); } /** diff --git a/tests/PHPUnit/Unit/CliMulti/ProcessTest.php b/tests/PHPUnit/Unit/CliMulti/ProcessTest.php index 0fee8afd55..bc00463e3a 100644 --- a/tests/PHPUnit/Unit/CliMulti/ProcessTest.php +++ b/tests/PHPUnit/Unit/CliMulti/ProcessTest.php @@ -102,11 +102,13 @@ class ProcessTest extends PHPUnit_Framework_TestCase public function test_getSecondsSinceCreation() { - sleep(2); + // This is not proper, but it avoids using sleep and stopping the tests for several seconds + $r = new ReflectionProperty($this->process, 'timeCreation'); + $r->setAccessible(true); + $r->setValue($this->process, time() - 2); + $seconds = $this->process->getSecondsSinceCreation(); - $this->assertGreaterThanOrEqual(2, $seconds); - $this->assertLessThanOrEqual(3, $seconds); + $this->assertEquals(2, $seconds); } - -}
\ No newline at end of file +} diff --git a/tests/PHPUnit/Unit/CommonTest.php b/tests/PHPUnit/Unit/CommonTest.php index 719333ccd9..185fa064d3 100644 --- a/tests/PHPUnit/Unit/CommonTest.php +++ b/tests/PHPUnit/Unit/CommonTest.php @@ -5,6 +5,8 @@ use Piwik\Filesystem; /** * Piwik - free/libre analytics platform * + * @backupGlobals enabled + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ diff --git a/tests/PHPUnit/Unit/DataTable/Filter/PivotByDimensionTest.php b/tests/PHPUnit/Unit/DataTable/Filter/PivotByDimensionTest.php index 9bfa6847ae..f1faa99b88 100644 --- a/tests/PHPUnit/Unit/DataTable/Filter/PivotByDimensionTest.php +++ b/tests/PHPUnit/Unit/DataTable/Filter/PivotByDimensionTest.php @@ -181,6 +181,9 @@ class PivotByDimensionTest extends PHPUnit_Framework_TestCase $this->assertTableRowsEquals($expectedRows, $table); } + /** + * @backupGlobals enabled + */ public function test_filter_UsesCorrectSegment_WhenPivotingSegmentedReport() { $this->loadPlugins('Referrers', 'UserCountry', 'CustomVariables'); diff --git a/tests/PHPUnit/Unit/DeprecatedMethodsTest.php b/tests/PHPUnit/Unit/DeprecatedMethodsTest.php index 633d5b6ea3..82e564cb95 100644 --- a/tests/PHPUnit/Unit/DeprecatedMethodsTest.php +++ b/tests/PHPUnit/Unit/DeprecatedMethodsTest.php @@ -39,6 +39,21 @@ class DeprecatedMethodsTest extends PHPUnit_Framework_TestCase $this->assertDeprecatedClassIsRemoved('\Piwik\Tests\Fixture', $validTill); $this->assertDeprecatedClassIsRemoved('\Piwik\Tests\OverrideLogin', $validTill); + $validTill = '2015-03-01'; + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'sanitizeIp', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'sanitizeIpRange', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'P2N', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'N2P', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'prettyPrint', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'isIPv4', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'long2ip', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'isIPv6', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'isMappedIPv4', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'getIPv4FromMappedIPv6', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'getIpsForRange', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'isIpInRange', $validTill); + $this->assertDeprecatedMethodIsRemoved('Piwik\IP', 'getHostByAddr', $validTill); + $this->assertDeprecatedMethodIsRemovedInPiwik3('\Piwik\Menu\MenuAbstract', 'add'); } diff --git a/tests/PHPUnit/Unit/IPTest.php b/tests/PHPUnit/Unit/IPTest.php index f12d37dfed..b542a74ab2 100644 --- a/tests/PHPUnit/Unit/IPTest.php +++ b/tests/PHPUnit/Unit/IPTest.php @@ -1,301 +1,19 @@ <?php -use Piwik\Common; -use Piwik\Config; -use Piwik\IP; -use Piwik\SettingsServer; - /** * Piwik - free/libre analytics platform * + * @backupGlobals enabled + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ -class IPTest extends PHPUnit_Framework_TestCase -{ - /** - * Dataprovider for testSanitizeIp - */ - public function getIPData() - { - return array( // input, output - // single IPv4 address - array('127.0.0.1', '127.0.0.1'), - - // single IPv6 address (ambiguous) - array('::1', '::1'), - array('::ffff:127.0.0.1', '::ffff:127.0.0.1'), - array('2001:5c0:1000:b::90f8', '2001:5c0:1000:b::90f8'), - - // single IPv6 address - array('[::1]', '::1'), - array('[2001:5c0:1000:b::90f8]', '2001:5c0:1000:b::90f8'), - array('[::ffff:127.0.0.1]', '::ffff:127.0.0.1'), - - // single IPv4 address (CIDR notation) - array('192.168.1.1/32', '192.168.1.1'), - - // single IPv6 address (CIDR notation) - array('::1/128', '::1'), - array('::ffff:127.0.0.1/128', '::ffff:127.0.0.1'), - array('2001:5c0:1000:b::90f8/128', '2001:5c0:1000:b::90f8'), - - // IPv4 address with port - array('192.168.1.2:80', '192.168.1.2'), - - // IPv6 address with port - array('[::1]:80', '::1'), - array('[::ffff:127.0.0.1]:80', '::ffff:127.0.0.1'), - array('[2001:5c0:1000:b::90f8]:80', '2001:5c0:1000:b::90f8'), - - // hostnames with port? - array('localhost', 'localhost'), - array('localhost:80', 'localhost'), - array('www.example.com', 'www.example.com'), - array('example.com:80', 'example.com'), - array('example.com:8080', 'example.com'), - array('sub.example.com:8080', 'sub.example.com'), - ); - } - - /** - * @dataProvider getIPData - * @group Core - */ - public function testSanitizeIp($ip, $expected) - { - $this->assertEquals($expected, IP::sanitizeIp($ip)); - } - - /** - * Dataprovider for testSanitizeIpRange - */ - public function getIPRangeData() - { - return array( - array('', false), - array(' 127.0.0.1 ', '127.0.0.1/32'), - array('192.168.1.0', '192.168.1.0/32'), - array('192.168.1.1/24', '192.168.1.1/24'), - array('192.168.1.2/16', '192.168.1.2/16'), - array('192.168.1.3/8', '192.168.1.3/8'), - array('192.168.2.*', '192.168.2.0/24'), - array('192.169.*.*', '192.169.0.0/16'), - array('193.*.*.*', '193.0.0.0/8'), - array('*.*.*.*', '0.0.0.0/0'), - array('*.*.*.1', false), - array('*.*.1.1', false), - array('*.1.1.1', false), - array('1.*.1.1', false), - array('1.1.*.1', false), - array('1.*.*.1', false), - array('::1', '::1/128'), - array('::ffff:127.0.0.1', '::ffff:127.0.0.1/128'), - array('2001:5c0:1000:b::90f8', '2001:5c0:1000:b::90f8/128'), - array('::1/64', '::1/64'), - array('::ffff:127.0.0.1/64', '::ffff:127.0.0.1/64'), - array('2001:5c0:1000:b::90f8/64', '2001:5c0:1000:b::90f8/64'), - ); - } - - /** - * @dataProvider getIPRangeData - * @group Core - */ - public function testSanitizeIpRange($ip, $expected) - { - $this->assertEquals($expected, IP::sanitizeIpRange($ip)); - } - - /** - * Dataprovider for testP2N - */ - public function getP2NTestData() - { - return array( - // IPv4 - array('0.0.0.0', "\x00\x00\x00\x00"), - array('127.0.0.1', "\x7F\x00\x00\x01"), - array('192.168.1.12', "\xc0\xa8\x01\x0c"), - array('255.255.255.255', "\xff\xff\xff\xff"), - - // IPv6 - array('::', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - array('::1', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"), - array('::fffe:7f00:1', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x7f\x00\x00\x01"), - array('::ffff:127.0.0.1', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\x01"), - array('2001:5c0:1000:b::90f8', "\x20\x01\x05\xc0\x10\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x90\xf8"), - ); - } - - /** - * @dataProvider getP2NTestData - * @group Core - */ - public function testP2N($P, $N) - { - $this->assertEquals($N, IP::P2N($P)); - } - - /** - * Dataprovider for testP2NInvalidInput - */ - public function getP2NInvalidInputData() - { - return array( - // not a series of dotted numbers - array(null), - array(''), - array('alpha'), - array('...'), - - // missing an octet - array('.0.0.0'), - array('0..0.0'), - array('0.0..0'), - array('0.0.0.'), - - // octets must be 0-255 - array('-1.0.0.0'), - array('1.1.1.256'), - - // leading zeros not supported (i.e., can be ambiguous, e.g., octal) - array('07.07.07.07'), - ); - } - - /** - * @group Core - * - * @dataProvider getP2NInvalidInputData - */ - public function testP2NInvalidInput($P) - { - $this->assertEquals("\x00\x00\x00\x00", IP::P2N($P)); - } - - /** - * @group Core - */ - public function getN2PTestData() - { - // a valid network address is either 4 or 16 bytes; those lines are intentionally left blank ;) - return array( - array(null), - array(''), - array("\x01"), - array("\x01\x00"), - array("\x01\x00\x00"), - - array("\x01\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), - ); - } - - /** - * @dataProvider getP2NTestData - * @group Core - */ - public function testN2P($P, $N) - { - $this->assertEquals($P, IP::N2P($N), "$P vs" . IP::N2P($N)); - } - - /** - * @dataProvider getN2PTestData - * @group Core - */ - public function testN2PinvalidInput($N) - { - $this->assertEquals("0.0.0.0", IP::N2P($N), bin2hex($N)); - } - - /** - * @dataProvider getP2NTestData - * @group Core - */ - public function testPrettyPrint($P, $N) - { - $this->assertEquals($P, IP::prettyPrint($N), "$P vs" . IP::N2P($N)); - } - /** - * @dataProvider getN2PTestData - * @group Core - */ - public function testPrettyPrintInvalidInput($N) - { - $this->assertEquals("0.0.0.0", IP::prettyPrint($N), bin2hex($N)); - } - - /** - * Dataprovider for IP4 test data - */ - public function getIPv4Data() - { - // a valid network address is either 4 or 16 bytes; those lines are intentionally left blank ;) - return array( - // invalid - array(null, false), - array("", false), - - // IPv4 - array("\x00\x00\x00\x00", true), - array("\x7f\x00\x00\x01", true), - - // IPv4-compatible (this transitional format is deprecated in RFC 4291, section 2.5.5.1) - array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01\x01", true), - - // IPv4-mapped (RFC 4291, 2.5.5.2) - array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\xa8\x01\x02", true), - - // other IPv6 address - array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\xc0\xa8\x01\x03", false), - array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xc0\xa8\x01\x04", false), - array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01\x05", false), - - /* - * We assume all stored IP addresses (pre-Piwik 1.4) were converted from UNSIGNED INT to VARBINARY. - * The following is just for informational purposes. - */ - - // 192.168.1.0 - array('-1062731520', false), - array('3232235776', false), - - // 10.10.10.10 - array('168430090', false), - - // 0.0.39.15 - this is the ambiguous case (i.e., 4 char string) - array('9999', true), - array("\x39\x39\x39\x39", true), - - // 0.0.3.231 - array('999', false), - array("\x39\x39\x39", false), - ); - - } - - /** - * @dataProvider getIPv4Data - * @group Core - */ - public function testIsIPv4($ip, $bool) - { - $this->assertEquals($bool, IP::isIPv4($ip), bin2hex($ip)); - } +use Piwik\Common; +use Piwik\Config; +use Piwik\IP; +class IPTest extends PHPUnit_Framework_TestCase +{ /** * Dataprovider for long2ip test */ @@ -355,204 +73,6 @@ class IPTest extends PHPUnit_Framework_TestCase } /** - * Dataprovider for ip range test - */ - public function getIPsForRangeTest() - { - return array( - - // invalid ranges - array(null, false), - array('', false), - array('0', false), - - // single IPv4 - array('127.0.0.1', array("\x7f\x00\x00\x01", "\x7f\x00\x00\x01")), - - // IPv4 with wildcards - array('192.168.1.*', array("\xc0\xa8\x01\x00", "\xc0\xa8\x01\xff")), - array('192.168.*.*', array("\xc0\xa8\x00\x00", "\xc0\xa8\xff\xff")), - array('192.*.*.*', array("\xc0\x00\x00\x00", "\xc0\xff\xff\xff")), - array('*.*.*.*', array("\x00\x00\x00\x00", "\xff\xff\xff\xff")), - - // single IPv4 in expected CIDR notation - array('192.168.1.1/24', array("\xc0\xa8\x01\x00", "\xc0\xa8\x01\xff")), - - array('192.168.1.127/32', array("\xc0\xa8\x01\x7f", "\xc0\xa8\x01\x7f")), - array('192.168.1.127/31', array("\xc0\xa8\x01\x7e", "\xc0\xa8\x01\x7f")), - array('192.168.1.127/30', array("\xc0\xa8\x01\x7c", "\xc0\xa8\x01\x7f")), - array('192.168.1.127/29', array("\xc0\xa8\x01\x78", "\xc0\xa8\x01\x7f")), - array('192.168.1.127/28', array("\xc0\xa8\x01\x70", "\xc0\xa8\x01\x7f")), - array('192.168.1.127/27', array("\xc0\xa8\x01\x60", "\xc0\xa8\x01\x7f")), - array('192.168.1.127/26', array("\xc0\xa8\x01\x40", "\xc0\xa8\x01\x7f")), - array('192.168.1.127/25', array("\xc0\xa8\x01\x00", "\xc0\xa8\x01\x7f")), - - array('192.168.1.255/32', array("\xc0\xa8\x01\xff", "\xc0\xa8\x01\xff")), - array('192.168.1.255/31', array("\xc0\xa8\x01\xfe", "\xc0\xa8\x01\xff")), - array('192.168.1.255/30', array("\xc0\xa8\x01\xfc", "\xc0\xa8\x01\xff")), - array('192.168.1.255/29', array("\xc0\xa8\x01\xf8", "\xc0\xa8\x01\xff")), - array('192.168.1.255/28', array("\xc0\xa8\x01\xf0", "\xc0\xa8\x01\xff")), - array('192.168.1.255/27', array("\xc0\xa8\x01\xe0", "\xc0\xa8\x01\xff")), - array('192.168.1.255/26', array("\xc0\xa8\x01\xc0", "\xc0\xa8\x01\xff")), - array('192.168.1.255/25', array("\xc0\xa8\x01\x80", "\xc0\xa8\x01\xff")), - - array('192.168.255.255/24', array("\xc0\xa8\xff\x00", "\xc0\xa8\xff\xff")), - array('192.168.255.255/23', array("\xc0\xa8\xfe\x00", "\xc0\xa8\xff\xff")), - array('192.168.255.255/22', array("\xc0\xa8\xfc\x00", "\xc0\xa8\xff\xff")), - array('192.168.255.255/21', array("\xc0\xa8\xf8\x00", "\xc0\xa8\xff\xff")), - array('192.168.255.255/20', array("\xc0\xa8\xf0\x00", "\xc0\xa8\xff\xff")), - array('192.168.255.255/19', array("\xc0\xa8\xe0\x00", "\xc0\xa8\xff\xff")), - array('192.168.255.255/18', array("\xc0\xa8\xc0\x00", "\xc0\xa8\xff\xff")), - array('192.168.255.255/17', array("\xc0\xa8\x80\x00", "\xc0\xa8\xff\xff")), - - // single IPv6 - array('::1', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")), - - // single IPv6 in expected CIDR notation - array('::1/128', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")), - array('::1/127', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")), - array('::fffe:7f00:1/120', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x7f\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x7f\x00\x00\xff")), - array('::ffff:127.0.0.1/120', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\xff")), - - array('2001:ca11:911::b0b:15:dead/128', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xad", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xad")), - array('2001:ca11:911::b0b:15:dead/127', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xac", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xad")), - array('2001:ca11:911::b0b:15:dead/126', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xac", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xaf")), - array('2001:ca11:911::b0b:15:dead/125', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xa8", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xaf")), - array('2001:ca11:911::b0b:15:dead/124', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xa0", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xaf")), - array('2001:ca11:911::b0b:15:dead/123', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xa0", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xbf")), - array('2001:ca11:911::b0b:15:dead/122', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x80", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xbf")), - array('2001:ca11:911::b0b:15:dead/121', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x80", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xff")), - array('2001:ca11:911::b0b:15:dead/120', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xff")), - array('2001:ca11:911::b0b:15:dead/119', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), - array('2001:ca11:911::b0b:15:dead/118', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdc\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), - array('2001:ca11:911::b0b:15:dead/117', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xd8\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), - array('2001:ca11:911::b0b:15:dead/116', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xd0\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), - array('2001:ca11:911::b0b:15:dead/115', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xc0\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), - array('2001:ca11:911::b0b:15:dead/114', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xc0\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xff\xff")), - array('2001:ca11:911::b0b:15:dead/113', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\x80\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xff\xff")), - array('2001:ca11:911::b0b:15:dead/112', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\x00\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xff\xff")), - ); - } - - /** - * @dataProvider getIPsForRangeTest - * @group Core - */ - public function testGetIpsForRange($range, $expected) - { - $this->assertEquals($expected, IP::getIpsForRange($range)); - } - - /** - * Dataprovider for testIsIpInRange - */ - public function getIpsInRangeData() - { - return array( - array('192.168.1.10', array( - '192.168.1.9' => false, - '192.168.1.10' => true, - '192.168.1.11' => false, - - // IPv6 addresses (including IPv4 mapped) have to be compared against IPv6 address ranges - '::ffff:192.168.1.10' => false, - )), - - array('::ffff:192.168.1.10', array( - '::ffff:192.168.1.9' => false, - '::ffff:192.168.1.10' => true, - '::ffff:c0a8:010a' => true, - '0000:0000:0000:0000:0000:ffff:c0a8:010a' => true, - '::ffff:192.168.1.11' => false, - - // conversely, IPv4 addresses have to be compared against IPv4 address ranges - '192.168.1.10' => false, - )), - - array('192.168.1.10/32', array( - '192.168.1.9' => false, - '192.168.1.10' => true, - '192.168.1.11' => false, - )), - - array('192.168.1.10/31', array( - '192.168.1.9' => false, - '192.168.1.10' => true, - '192.168.1.11' => true, - '192.168.1.12' => false, - )), - - array('192.168.1.128/25', array( - '192.168.1.127' => false, - '192.168.1.128' => true, - '192.168.1.255' => true, - '192.168.2.0' => false, - )), - - array('192.168.1.10/24', array( - '192.168.0.255' => false, - '192.168.1.0' => true, - '192.168.1.1' => true, - '192.168.1.2' => true, - '192.168.1.3' => true, - '192.168.1.4' => true, - '192.168.1.7' => true, - '192.168.1.8' => true, - '192.168.1.15' => true, - '192.168.1.16' => true, - '192.168.1.31' => true, - '192.168.1.32' => true, - '192.168.1.63' => true, - '192.168.1.64' => true, - '192.168.1.127' => true, - '192.168.1.128' => true, - '192.168.1.255' => true, - '192.168.2.0' => false, - )), - - array('192.168.1.*', array( - '192.168.0.255' => false, - '192.168.1.0' => true, - '192.168.1.1' => true, - '192.168.1.2' => true, - '192.168.1.3' => true, - '192.168.1.4' => true, - '192.168.1.7' => true, - '192.168.1.8' => true, - '192.168.1.15' => true, - '192.168.1.16' => true, - '192.168.1.31' => true, - '192.168.1.32' => true, - '192.168.1.63' => true, - '192.168.1.64' => true, - '192.168.1.127' => true, - '192.168.1.128' => true, - '192.168.1.255' => true, - '192.168.2.0' => false, - )), - ); - } - - /** - * @group Core - * - * @dataProvider getIpsInRangeData - */ - public function testIsIpInRange($range, $test) - { - foreach ($test as $ip => $expected) { - // range as a string - $this->assertEquals($expected, IP::isIpInRange(IP::P2N($ip), array($range)), "$ip in $range"); - - // range as an array(low, high) - $aRange = IP::getIpsForRange($range); - $aRange[0] = IP::N2P($aRange[0]); - $aRange[1] = IP::N2P($aRange[1]); - $this->assertEquals($expected, IP::isIpInRange(IP::P2N($ip), array($aRange)), "$ip in $range"); - } - } - - /** * Dataprovider for ip from header tests */ public function getIpFromHeaderTestData() @@ -672,19 +192,4 @@ class IPTest extends PHPUnit_Framework_TestCase // with excluded Ips $this->assertEquals($expected, IP::getLastIpFromList($csv . ', 10.10.10.10', array('10.10.10.10'))); } - - /** - * @group Core - */ - public function testGetHostByAddr() - { - $hosts = array('localhost', 'localhost.localdomain', strtolower(@php_uname('n')), '127.0.0.1'); - $host = IP::getHostByAddr('127.0.0.1'); - $this->assertTrue(in_array(strtolower($host), $hosts), $host . ' -> localhost'); - - if (!SettingsServer::isWindows() || PHP_VERSION >= '5.3') { - $hosts = array('ip6-localhost', 'localhost', 'localhost.localdomain', strtolower(@php_uname('n')), '::1'); - $this->assertTrue(in_array(strtolower(IP::getHostByAddr('::1')), $hosts), '::1 -> ip6-localhost'); - } - } } diff --git a/tests/PHPUnit/Unit/NonceTest.php b/tests/PHPUnit/Unit/NonceTest.php index 9d0d8ef37d..6a9a90994c 100644 --- a/tests/PHPUnit/Unit/NonceTest.php +++ b/tests/PHPUnit/Unit/NonceTest.php @@ -5,6 +5,8 @@ use Piwik\Nonce; /** * Piwik - free/libre analytics platform * + * @backupGlobals enabled + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ diff --git a/tests/PHPUnit/Unit/UrlTest.php b/tests/PHPUnit/Unit/UrlTest.php index bbde992785..3c2bafe73e 100644 --- a/tests/PHPUnit/Unit/UrlTest.php +++ b/tests/PHPUnit/Unit/UrlTest.php @@ -5,6 +5,8 @@ use Piwik\Url; /** * Piwik - free/libre analytics platform * + * @backupGlobals enabled + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ diff --git a/tests/lib/screenshot-testing/support/page-renderer.js b/tests/lib/screenshot-testing/support/page-renderer.js index de3819a4d3..7e8edcb466 100644 --- a/tests/lib/screenshot-testing/support/page-renderer.js +++ b/tests/lib/screenshot-testing/support/page-renderer.js @@ -233,15 +233,15 @@ PageRenderer.prototype.capture = function (outputPath, callback, selector) { } var result = page.evaluate(function(selector) { - function isInvalidBoundingRect (rect) { - var docWidth = $(document).width(), - docHeight = $(document).height(); + var docWidth = $(document).width(), + docHeight = $(document).height(); + function isInvalidBoundingRect (rect) { return !rect.width || !rect.height - || rect.left < 0 || rect.left > docWidth - || rect.top < 0 || rect.top > docHeight - || rect.right < 0 || rect.right > docWidth - || rect.bottom < 0 || rect.bottom > docHeight; + || (rect.left < 0 && rect.right < 0) + || (rect.left > docWidth && rect.right > docWidth) + || (rect.top < 0 && rect.bottom < 0) + || (rect.top > docHeight && rect.bottom > docHeight); } var element = window.jQuery(selector); |