PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPUnit/Fixtures/InvalidVisits.php

https://github.com/CodeYellowBV/piwik
PHP | 110 lines | 70 code | 21 blank | 19 comment | 2 complexity | a06f5f85693494399c263182baf7bf3c MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. use Piwik\Http;
  9. use Piwik\Plugins\SitesManager\API;
  10. /**
  11. * Adds one site and sends several invalid tracking requests. The result should be
  12. * one website with no visits.
  13. */
  14. class Test_Piwik_Fixture_InvalidVisits extends Fixture
  15. {
  16. public $idSite = 1;
  17. public $dateTime = '2009-01-04 00:11:42';
  18. public $trackInvalidRequests = true;
  19. public function setUp()
  20. {
  21. $this->setUpWebsitesAndGoals();
  22. $this->trackVisits();
  23. }
  24. public function tearDown()
  25. {
  26. // empty
  27. }
  28. private function setUpWebsitesAndGoals()
  29. {
  30. if (!self::siteCreated($idSite = 1)) {
  31. self::createWebsite($this->dateTime);
  32. }
  33. }
  34. private function trackVisits()
  35. {
  36. if (!$this->trackInvalidRequests) {
  37. return;
  38. }
  39. $dateTime = $this->dateTime;
  40. $idSite = $this->idSite;
  41. API::getInstance()->setSiteSpecificUserAgentExcludeEnabled(true);
  42. API::getInstance()->setGlobalExcludedUserAgents('globalexcludeduseragent');
  43. // Trigger empty request
  44. $trackerUrl = self::getTrackerUrl();
  45. $response = Http::fetchRemoteFile($trackerUrl);
  46. self::assertTrue(strpos($response, 'is a free/libre web') !== false, 'Piwik empty request response not correct: ' . $response);
  47. $t = self::getTracker($idSite, $dateTime, $defaultInit = true);
  48. // test GoogleBot UA visitor
  49. $t->setUserAgent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
  50. self::checkResponse($t->doTrackPageView('bot visit, please do not record'));
  51. // Test IP Exclusion works with or without IP exclusion
  52. foreach (array(false, true) as $enable) {
  53. $excludedIp = '154.1.12.34';
  54. API::getInstance()->updateSite($idSite, 'new site name', $url = array('http://site.com'), $ecommerce = 0, $ss = 1, $ss_kwd = '', $ss_cat = '', $excludedIp . ',1.2.3.4', $excludedQueryParameters = null, $timezone = null, $currency = null, $group = null, $startDate = null, $excludedUserAgents = 'excludeduseragentstring');
  55. // Enable IP Anonymization
  56. $t->DEBUG_APPEND_URL = '&forceIpAnonymization=' . (int)$enable;
  57. // test with excluded User Agent
  58. $t->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729) (excludeduseragentstring)');
  59. $t->setIp('211.1.2.3');
  60. self::checkResponse($t->doTrackPageView('visit from excluded User Agent'));
  61. $t->setUserAgent('Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0 Google (+https://developers.google.com/+/web/snippet/)');
  62. self::checkResponse($t->doTrackPageView('visit from excluded User Agent'));
  63. // test w/ global excluded User Agent
  64. $t->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729) (globalexcludeduseragent)');
  65. $t->setIp('211.1.2.3');
  66. self::checkResponse($t->doTrackPageView('visit from global excluded User Agent'));
  67. // test with excluded IP
  68. $t->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)'); // restore normal user agent
  69. $t->setIp($excludedIp);
  70. self::checkResponse($t->doTrackPageView('visit from IP excluded'));
  71. // test with global list of excluded IPs
  72. $excludedIpBis = '145.5.3.4';
  73. API::getInstance()->setGlobalExcludedIps($excludedIpBis);
  74. $t->setIp($excludedIpBis);
  75. self::checkResponse($t->doTrackPageView('visit from IP globally excluded'));
  76. }
  77. try {
  78. @$t->setAttributionInfo(array());
  79. self::fail();
  80. } catch (Exception $e) {
  81. }
  82. try {
  83. $t->setAttributionInfo(json_encode('test'));
  84. self::fail();
  85. } catch (Exception $e) {
  86. }
  87. $t->setAttributionInfo(json_encode(array()));
  88. }
  89. }