PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPUnit/Plugins/UserCountryTest.php

https://github.com/CodeYellowBV/piwik
PHP | 182 lines | 112 code | 33 blank | 37 comment | 5 complexity | 6b1c1841653b71c1c10e95bdebc7bd6c 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\Plugins\UserCountry\GeoIPAutoUpdater;
  9. use Piwik\Plugins\UserCountry\LocationProvider\GeoIp;
  10. use Piwik\Plugins\UserCountry;
  11. use Piwik\Plugins\UserCountry\LocationProvider;
  12. require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/UserCountry.php';
  13. require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/functions.php';
  14. require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Countries.php';
  15. class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase
  16. {
  17. /**
  18. * @group Plugins
  19. */
  20. public function testGetFlagFromCode()
  21. {
  22. $flag = \Piwik\Plugins\UserCountry\getFlagFromCode("us");
  23. $this->assertEquals(basename($flag), "us.png");
  24. }
  25. /**
  26. * @group Plugins
  27. */
  28. public function testGetFlagFromInvalidCode()
  29. {
  30. $flag = \Piwik\Plugins\UserCountry\getFlagFromCode("foo");
  31. $this->assertEquals(basename($flag), "xx.png");
  32. }
  33. /**
  34. * @group Plugins
  35. */
  36. public function testFlagsAndContinents()
  37. {
  38. require PIWIK_PATH_TEST_TO_ROOT . '/core/DataFiles/Countries.php';
  39. $continents = $GLOBALS['Piwik_ContinentList'];
  40. $countries = array_merge($GLOBALS['Piwik_CountryList'], $GLOBALS['Piwik_CountryList_Extras']);
  41. // Get list of existing flag icons
  42. $flags = scandir(PIWIK_PATH_TEST_TO_ROOT . '/plugins/UserCountry/images/flags/');
  43. // Get list of countries
  44. foreach ($countries as $country => $continent) {
  45. // test continent
  46. $this->assertContains($continent, $continents);
  47. // test flag
  48. $this->assertContains($country . '.png', $flags);
  49. }
  50. foreach ($flags as $filename) {
  51. if ($filename == '.' || $filename == '..') {
  52. continue;
  53. }
  54. $country = substr($filename, 0, strpos($filename, '.png'));
  55. // test country
  56. $this->assertArrayHasKey($country, $countries, $filename);
  57. }
  58. }
  59. /**
  60. * Test that redundant checks work.
  61. *
  62. * @group Plugins
  63. */
  64. public function testGeoIpUpdaterRedundantChecks()
  65. {
  66. GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
  67. LocationProvider::$providers = null;
  68. // create empty ISP & Org files
  69. $this->createEmptyISPOrgFiles();
  70. // run redundant checks
  71. $updater = new Piwik_UserCountry_GeoIPAutoUpdater_publictest();
  72. $updater->performRedundantDbChecks();
  73. // check that files are renamed correctly
  74. $this->checkBrokenGeoIPState();
  75. // create empty files again & run checks again
  76. $this->createEmptyISPOrgFiles();
  77. $updater->performRedundantDbChecks();
  78. // check that w/ broken files already there, redundant checks still work correctly
  79. $this->checkBrokenGeoIPState();
  80. }
  81. /**
  82. * @group Plugins
  83. *
  84. * @dataProvider getInvalidGeoIpUrlsToTest
  85. */
  86. public function testGeoIpDownloadInvalidUrl($url)
  87. {
  88. $updater = new Piwik_UserCountry_GeoIPAutoUpdater_publictest();
  89. try {
  90. $updater->downloadFile('loc', $url);
  91. $this->fail("Downloading invalid url succeeded!");
  92. } catch (Exception $ex) {
  93. $this->assertEquals("UserCountry_UnsupportedArchiveType", $ex->getMessage());
  94. }
  95. }
  96. public function getInvalidGeoIpUrlsToTest()
  97. {
  98. return array(array("http://localhost/tests/resources/geoip.tar"),
  99. array("http://localhost/tests/resources/geoip.tar.bz2"),
  100. array("http://localhost/tests/resources/geoip.dat"));
  101. }
  102. public function setUp()
  103. {
  104. // empty
  105. }
  106. public function tearDown()
  107. {
  108. $geoIpDirPath = PIWIK_INCLUDE_PATH . '/tests/lib/geoip-files';
  109. $filesToRemove = array('GeoIPISP.dat.broken', 'GeoIPOrg.dat.broken', 'GeoIPISP.dat', 'GeoIPOrg.dat');
  110. foreach ($filesToRemove as $name) {
  111. $path = $geoIpDirPath . '/' . $name;
  112. if (file_exists($path)) {
  113. unlink($path);
  114. }
  115. }
  116. }
  117. private function createEmptyISPOrgFiles()
  118. {
  119. $geoIpDir = PIWIK_INCLUDE_PATH . '/tests/lib/geoip-files';
  120. $fd = fopen($geoIpDir . '/GeoIPISP.dat', 'w');
  121. fclose($fd);
  122. $fd = fopen($geoIpDir . '/GeoIPOrg.dat', 'w');
  123. fclose($fd);
  124. }
  125. private function checkBrokenGeoIPState()
  126. {
  127. $geoIpDir = PIWIK_INCLUDE_PATH . '/tests/lib/geoip-files';
  128. $this->assertFalse(file_exists($geoIpDir . '/GeoIPCity.dat.broken'));
  129. $this->assertFalse(file_exists($geoIpDir . '/GeoIPISP.dat'));
  130. $this->assertTrue(file_exists($geoIpDir . '/GeoIPISP.dat.broken'));
  131. $this->assertFalse(file_exists($geoIpDir . '/GeoIPOrg.dat'));
  132. $this->assertTrue(file_exists($geoIpDir . '/GeoIPOrg.dat.broken'));
  133. }
  134. }
  135. class Piwik_UserCountry_GeoIPAutoUpdater_publictest extends GeoIPAutoUpdater
  136. {
  137. public function __construct()
  138. {
  139. // empty
  140. }
  141. public function performRedundantDbChecks()
  142. {
  143. parent::performRedundantDbChecks();
  144. }
  145. public function downloadFile($type, $url)
  146. {
  147. parent::downloadFile($type, $url);
  148. }
  149. }