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

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthieu Napoli <matthieu@mnapoli.fr>2014-12-30 02:32:32 +0300
committerMatthieu Napoli <matthieu@mnapoli.fr>2015-01-05 05:25:11 +0300
commitea9c95410a436cea3051ab813476c24b4ddaca0d (patch)
tree2ba9b6e9904d7046b4dc08c34dea5205cf5c5627
parentde06fd66ee7d01832ff08f13d58e21a9d7a510c7 (diff)
Fix unit tests by loading no translations by default in tests
-rw-r--r--config/environment/test.php6
-rw-r--r--core/Intl/Locale.php19
-rw-r--r--core/Translate.php5
-rw-r--r--core/Translation/Translator.php10
-rw-r--r--plugins/LanguagesManager/LanguagesManager.php5
-rw-r--r--tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php5
-rw-r--r--tests/PHPUnit/Unit/Metrics/FormatterTest.php6
-rw-r--r--tests/PHPUnit/Unit/Period/BasePeriodTest.php33
-rw-r--r--tests/PHPUnit/Unit/Period/DayTest.php14
-rw-r--r--tests/PHPUnit/Unit/Period/MonthTest.php13
-rw-r--r--tests/PHPUnit/Unit/Period/RangeTest.php11
-rw-r--r--tests/PHPUnit/Unit/Period/WeekTest.php11
-rw-r--r--tests/PHPUnit/Unit/Period/YearTest.php6
-rw-r--r--tests/PHPUnit/bootstrap.php3
14 files changed, 85 insertions, 62 deletions
diff --git a/config/environment/test.php b/config/environment/test.php
index 95857fd134..0dfd6d9770 100644
--- a/config/environment/test.php
+++ b/config/environment/test.php
@@ -5,4 +5,10 @@ return array(
// Disable logging
'Psr\Log\LoggerInterface' => DI\object('Psr\Log\NullLogger'),
+ // Disable translation cache
+ 'Piwik\Translation\Loader\LoaderInterface' => DI\object('Piwik\Translation\Loader\JsonFileLoader'),
+ // Disable loading core translations
+ 'Piwik\Translation\Translator' => DI\object()
+ ->constructorParameter('directories', array()),
+
);
diff --git a/core/Intl/Locale.php b/core/Intl/Locale.php
new file mode 100644
index 0000000000..7c18b727c5
--- /dev/null
+++ b/core/Intl/Locale.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Piwik\Intl;
+
+class Locale
+{
+ public static function setLocale($locale)
+ {
+ $localeVariant = str_replace('UTF-8', 'UTF8', $locale);
+
+ setlocale(LC_ALL, $locale, $localeVariant);
+ setlocale(LC_CTYPE, '');
+ }
+
+ public static function setDefaultLocale()
+ {
+ self::setLocale('en_US.UTF-8');
+ }
+}
diff --git a/core/Translate.php b/core/Translate.php
index 70f795245b..c828ed3d1d 100644
--- a/core/Translate.php
+++ b/core/Translate.php
@@ -31,10 +31,12 @@ class Translate
public static function loadEnglishTranslation()
{
+ self::loadCoreTranslation();
}
public static function unloadEnglishTranslation()
{
+ self::getTranslator()->reset();
}
public static function reloadLanguage($language = false)
@@ -49,6 +51,7 @@ class Translate
*/
public static function loadCoreTranslation($language = false)
{
+ self::getTranslator()->addDirectory(PIWIK_INCLUDE_PATH . '/lang');
}
public static function mergeTranslationArray($translation)
@@ -67,7 +70,7 @@ class Translate
/** Reset the cached language to load. Used in tests. */
public static function reset()
{
- self::getTranslator()->setCurrentLanguage(null);
+ self::getTranslator()->reset();
}
/**
diff --git a/core/Translation/Translator.php b/core/Translation/Translator.php
index 7233882cd6..9d256b6940 100644
--- a/core/Translation/Translator.php
+++ b/core/Translation/Translator.php
@@ -178,6 +178,16 @@ class Translator
}
/**
+ * Should be used by tests only, and this method should eventually be removed.
+ */
+ public function reset()
+ {
+ $this->currentLanguage = $this->getDefaultLanguage();
+ $this->directories = array();
+ $this->translations = array();
+ }
+
+ /**
* @param string $translation
* @return null|string
*/
diff --git a/plugins/LanguagesManager/LanguagesManager.php b/plugins/LanguagesManager/LanguagesManager.php
index 711de26648..afd60dc53d 100644
--- a/plugins/LanguagesManager/LanguagesManager.php
+++ b/plugins/LanguagesManager/LanguagesManager.php
@@ -15,6 +15,7 @@ use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Cookie;
use Piwik\Db;
+use Piwik\Intl\Locale;
use Piwik\Piwik;
use Piwik\Translate;
use Piwik\Translation\Translator;
@@ -108,9 +109,7 @@ class LanguagesManager extends \Piwik\Plugin
}
$locale = $translator->translate('General_Locale');
- $localeVariant = str_replace('UTF-8', 'UTF8', $locale);
- setlocale(LC_ALL, $locale, $localeVariant);
- setlocale(LC_CTYPE, '');
+ Locale::setLocale($locale);
}
public function deleteUserLanguage($userLogin)
diff --git a/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php b/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php
index fd9b996f50..6d7d18def3 100644
--- a/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php
+++ b/tests/PHPUnit/Unit/Metrics/Formatter/HtmlTest.php
@@ -7,6 +7,7 @@
*/
namespace Piwik\Tests\Unit\Metrics\Formatter;
+use Piwik\Intl\Locale;
use Piwik\Metrics\Formatter\Html;
use Piwik\Translate;
use Piwik\Plugins\SitesManager\API as SitesManagerAPI;
@@ -34,8 +35,6 @@ class HtmlTest extends \PHPUnit_Framework_TestCase
$this->formatter = new Html();
- setlocale(LC_ALL, null);
-
Translate::loadEnglishTranslation();
$this->setSiteManagerApiMock();
}
@@ -44,8 +43,6 @@ class HtmlTest extends \PHPUnit_Framework_TestCase
{
Translate::unloadEnglishTranslation();
$this->unsetSiteManagerApiMock();
-
- setlocale(LC_ALL, null);
}
public function test_getPrettyTimeFromSeconds_DefaultsToShowingSentences_AndUsesNonBreakingSpaces()
diff --git a/tests/PHPUnit/Unit/Metrics/FormatterTest.php b/tests/PHPUnit/Unit/Metrics/FormatterTest.php
index ce8c268b08..6cf3a8e502 100644
--- a/tests/PHPUnit/Unit/Metrics/FormatterTest.php
+++ b/tests/PHPUnit/Unit/Metrics/FormatterTest.php
@@ -7,6 +7,7 @@
*/
namespace Piwik\Tests\Unit\Metrics;
+use Piwik\Intl\Locale;
use Piwik\Metrics\Formatter;
use Piwik\Translate;
use Piwik\Plugins\SitesManager\API as SitesManagerAPI;
@@ -50,8 +51,6 @@ class FormatterTest extends \PHPUnit_Framework_TestCase
$this->formatter = new Formatter();
- setlocale(LC_ALL, null);
-
Translate::loadEnglishTranslation();
$this->setSiteManagerApiMock();
}
@@ -60,8 +59,6 @@ class FormatterTest extends \PHPUnit_Framework_TestCase
{
Translate::unloadEnglishTranslation();
$this->unsetSiteManagerApiMock();
-
- setlocale(LC_ALL, null);
}
/**
@@ -83,6 +80,7 @@ class FormatterTest extends \PHPUnit_Framework_TestCase
}
$this->assertEquals($expected, $this->formatter->getPrettyNumber($number, 2));
+ Locale::setDefaultLocale();
}
/**
diff --git a/tests/PHPUnit/Unit/Period/BasePeriodTest.php b/tests/PHPUnit/Unit/Period/BasePeriodTest.php
new file mode 100644
index 0000000000..e6713b5f3d
--- /dev/null
+++ b/tests/PHPUnit/Unit/Period/BasePeriodTest.php
@@ -0,0 +1,33 @@
+<?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\Tests\Unit\Period;
+
+use Piwik\Container\StaticContainer;
+use Piwik\Translate;
+use Piwik\Translation\Translator;
+
+abstract class BasePeriodTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ parent::setUp();
+
+ Translate::loadCoreTranslation();
+ /** @var Translator $translator */
+ $translator = StaticContainer::getContainer()->get('Piwik\Translation\Translator');
+ $translator->addDirectory(PIWIK_INCLUDE_PATH . '/plugins/CoreHome/lang');
+ }
+
+ public function tearDown()
+ {
+ parent::tearDown();
+
+ Translate::reset();
+ }
+} \ No newline at end of file
diff --git a/tests/PHPUnit/Unit/Period/DayTest.php b/tests/PHPUnit/Unit/Period/DayTest.php
index ae1ba01844..c2b4a05a5b 100644
--- a/tests/PHPUnit/Unit/Period/DayTest.php
+++ b/tests/PHPUnit/Unit/Period/DayTest.php
@@ -10,9 +10,8 @@ namespace Piwik\Tests\Unit\Period;
use Piwik\Date;
use Piwik\Period\Day;
-use Piwik\Translate;
-class Period_DayTest extends \PHPUnit_Framework_TestCase
+class DayTest extends BasePeriodTest
{
/**
* @group Core
@@ -215,8 +214,6 @@ class Period_DayTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedShortString()
{
- $this->loadEnglishTranslation();
-
$month = new Day(Date::factory('2024-10-09'));
$shouldBe = 'Wed 9 Oct';
$this->assertEquals($shouldBe, $month->getLocalizedShortString());
@@ -227,8 +224,6 @@ class Period_DayTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedLongString()
{
- $this->loadEnglishTranslation();
-
$month = new Day(Date::factory('2024-10-09'));
$shouldBe = 'Wednesday 9 October 2024';
$this->assertEquals($shouldBe, $month->getLocalizedLongString());
@@ -239,15 +234,8 @@ class Period_DayTest extends \PHPUnit_Framework_TestCase
*/
public function testGetPrettyString()
{
- $this->loadEnglishTranslation();
-
$month = new Day(Date::factory('2024-10-09'));
$shouldBe = '2024-10-09';
$this->assertEquals($shouldBe, $month->getPrettyString());
}
-
- private function loadEnglishTranslation()
- {
- Translate::reloadLanguage('en');
- }
} \ No newline at end of file
diff --git a/tests/PHPUnit/Unit/Period/MonthTest.php b/tests/PHPUnit/Unit/Period/MonthTest.php
index 916f925e6d..6ef1c824e3 100644
--- a/tests/PHPUnit/Unit/Period/MonthTest.php
+++ b/tests/PHPUnit/Unit/Period/MonthTest.php
@@ -10,9 +10,8 @@ namespace Piwik\Tests\Unit\Period;
use Piwik\Date;
use Piwik\Period\Month;
-use Piwik\Translate;
-class Period_MonthTest extends \PHPUnit_Framework_TestCase
+class MonthTest extends BasePeriodTest
{
/**
* testing december
@@ -271,8 +270,6 @@ class Period_MonthTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedShortString()
{
- $this->loadEnglishTranslation();
-
$month = new Month(Date::factory('2024-10-09'));
$shouldBe = 'Oct 2024';
$this->assertEquals($shouldBe, $month->getLocalizedShortString());
@@ -283,8 +280,6 @@ class Period_MonthTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedLongString()
{
- $this->loadEnglishTranslation();
-
$month = new Month(Date::factory('2024-10-09'));
$shouldBe = '2024, October';
$this->assertEquals($shouldBe, $month->getLocalizedLongString());
@@ -295,15 +290,9 @@ class Period_MonthTest extends \PHPUnit_Framework_TestCase
*/
public function testGetPrettyString()
{
- $this->loadEnglishTranslation();
-
$month = new Month(Date::factory('2024-10-09'));
$shouldBe = '2024-10';
$this->assertEquals($shouldBe, $month->getPrettyString());
}
- private function loadEnglishTranslation()
- {
- Translate::reloadLanguage('en');
- }
} \ No newline at end of file
diff --git a/tests/PHPUnit/Unit/Period/RangeTest.php b/tests/PHPUnit/Unit/Period/RangeTest.php
index 29c716d68b..c7896b638f 100644
--- a/tests/PHPUnit/Unit/Period/RangeTest.php
+++ b/tests/PHPUnit/Unit/Period/RangeTest.php
@@ -14,9 +14,8 @@ use Piwik\Period\Month;
use Piwik\Period\Range;
use Piwik\Period\Week;
use Piwik\Period\Year;
-use Piwik\Translate;
-class Period_RangeTest extends \PHPUnit_Framework_TestCase
+class RangeTest extends BasePeriodTest
{
// test range 1
/**
@@ -1272,7 +1271,6 @@ class Period_RangeTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedShortString()
{
- $this->loadEnglishTranslation();
$month = new Range('range', '2000-12-09,2001-02-01');
$shouldBe = '9 Dec 00 - 1 Feb 01';
$this->assertEquals($shouldBe, $month->getLocalizedShortString());
@@ -1283,7 +1281,6 @@ class Period_RangeTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedLongString()
{
- $this->loadEnglishTranslation();
$month = new Range('range', '2023-05-09,2023-05-21');
$shouldBe = '8 May 23 - 21 May 23';
$this->assertEquals($shouldBe, $month->getLocalizedLongString());
@@ -1294,7 +1291,6 @@ class Period_RangeTest extends \PHPUnit_Framework_TestCase
*/
public function testGetPrettyString()
{
- $this->loadEnglishTranslation();
$month = new Range('range', '2007-02-09,2007-03-15');
$shouldBe = 'From 2007-02-09 to 2007-03-15';
$this->assertEquals($shouldBe, $month->getPrettyString());
@@ -1322,9 +1318,4 @@ class Period_RangeTest extends \PHPUnit_Framework_TestCase
$range = new Range($period, 'last' . $lastN);
$this->assertEquals($expectedLastN, $range->getNumberOfSubperiods());
}
-
- private function loadEnglishTranslation()
- {
- Translate::reloadLanguage('en');
- }
}
diff --git a/tests/PHPUnit/Unit/Period/WeekTest.php b/tests/PHPUnit/Unit/Period/WeekTest.php
index c543a2db5d..05f54b9942 100644
--- a/tests/PHPUnit/Unit/Period/WeekTest.php
+++ b/tests/PHPUnit/Unit/Period/WeekTest.php
@@ -10,9 +10,8 @@ namespace Piwik\Tests\Unit\Period;
use Piwik\Date;
use Piwik\Period\Week;
-use Piwik\Translate;
-class Period_WeekTest extends \PHPUnit_Framework_TestCase
+class WeekTest extends BasePeriodTest
{
/**
* test week between 2 years
@@ -123,7 +122,6 @@ class Period_WeekTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedShortString()
{
- $this->loadEnglishTranslation();
$week = new Week(Date::factory('2024-10-09'));
$shouldBe = '7 Oct - 13 Oct 24';
$this->assertEquals($shouldBe, $week->getLocalizedShortString());
@@ -134,7 +132,6 @@ class Period_WeekTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedLongString()
{
- $this->loadEnglishTranslation();
$week = new Week(Date::factory('2024-10-09'));
$shouldBe = 'Week 7 October - 13 October 2024';
$this->assertEquals($shouldBe, $week->getLocalizedLongString());
@@ -145,14 +142,8 @@ class Period_WeekTest extends \PHPUnit_Framework_TestCase
*/
public function testGetPrettyString()
{
- $this->loadEnglishTranslation();
$week = new Week(Date::factory('2024-10-09'));
$shouldBe = 'From 2024-10-07 to 2024-10-13';
$this->assertEquals($shouldBe, $week->getPrettyString());
}
-
- private function loadEnglishTranslation()
- {
- Translate::reloadLanguage('en');
- }
}
diff --git a/tests/PHPUnit/Unit/Period/YearTest.php b/tests/PHPUnit/Unit/Period/YearTest.php
index fad41535de..13d688bda9 100644
--- a/tests/PHPUnit/Unit/Period/YearTest.php
+++ b/tests/PHPUnit/Unit/Period/YearTest.php
@@ -10,9 +10,8 @@ namespace Piwik\Tests\Unit\Period;
use Piwik\Date;
use Piwik\Period\Year;
-use Piwik\Translate;
-class Period_YearTest extends \PHPUnit_Framework_TestCase
+class YearTest extends BasePeriodTest
{
/**
* test normal case
@@ -70,7 +69,6 @@ class Period_YearTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedShortString()
{
- Translate::loadEnglishTranslation();
$year = new Year(Date::factory('2024-10-09'));
$shouldBe = '2024';
$this->assertEquals($shouldBe, $year->getLocalizedShortString());
@@ -81,7 +79,6 @@ class Period_YearTest extends \PHPUnit_Framework_TestCase
*/
public function testGetLocalizedLongString()
{
- Translate::loadEnglishTranslation();
$year = new Year(Date::factory('2024-10-09'));
$shouldBe = '2024';
$this->assertEquals($shouldBe, $year->getLocalizedLongString());
@@ -92,7 +89,6 @@ class Period_YearTest extends \PHPUnit_Framework_TestCase
*/
public function testGetPrettyString()
{
- Translate::loadEnglishTranslation();
$year = new Year(Date::factory('2024-10-09'));
$shouldBe = '2024';
$this->assertEquals($shouldBe, $year->getPrettyString());
diff --git a/tests/PHPUnit/bootstrap.php b/tests/PHPUnit/bootstrap.php
index 1507ced00d..a0f9d5b349 100644
--- a/tests/PHPUnit/bootstrap.php
+++ b/tests/PHPUnit/bootstrap.php
@@ -3,6 +3,7 @@
use Piwik\Container\StaticContainer;
use Piwik\Http;
use Piwik\Tests\Framework\Fixture;
+use Piwik\Intl\Locale;
define('PIWIK_TEST_MODE', true);
define('PIWIK_PRINT_ERROR_BACKTRACE', false);
@@ -58,6 +59,8 @@ foreach($fixturesToLoad as $fixturePath) {
}
}
+Locale::setDefaultLocale();
+
function prepareServerVariables()
{
\Piwik\Config::getInstance()->init();