From 8a525ad008a5235744de7f6c922516a36d45c314 Mon Sep 17 00:00:00 2001 From: diosmosis <benakamoorthi@fastmail.fm> Date: Sat, 1 Mar 2014 15:05:36 +0000 Subject: [PATCH] Refs #4189, more work getting UI tests to pass and adding admin screenshots, including: - make updatetoken.php use TestingEnvironment in testmode, - allow persisted fixture to be dropped using new --drop command line option (for ui test runner), - fix debugging mistake in realtime map, fix merge error in empty.twig, - make sure Fixture prints to screen only for UI test setup, re-enable CustomAlerts fixture in OmniFixture, - make sure forced now value for realtime map is constant, - use *.getDatabaseConfig events to override database name in TestingEnvironment - use TestingEnvironment in proxy/piwik.php - add wait() method to screenshot testing framework's PageRenderer - hide current UTC time in manage websites admin screenshot --- misc/cron/updatetoken.php | 4 +- plugins/CoreConsole/Commands/RunUITests.php | 6 +++ .../javascripts/realtime-map.js | 2 +- plugins/Zeitgeist/templates/empty.twig | 4 -- tests/PHPUnit/Fixture.php | 21 +++++++-- .../ManySitesImportedLogsWithXssAttempts.php | 19 -------- tests/PHPUnit/Fixtures/OmniFixture.php | 1 - tests/PHPUnit/Fixtures/UITestFixture.php | 46 +++++++++++++++++-- tests/PHPUnit/TestingEnvironment.php | 19 ++++++-- tests/PHPUnit/UI | 2 +- tests/PHPUnit/proxy/piwik.php | 2 + tests/lib/screenshot-testing/support/app.js | 4 ++ .../support/page-renderer.js | 8 ++++ .../support/setupDatabase.php | 4 ++ .../support/teardownDatabase.php | 1 + .../resources/screenshot-override/override.js | 3 ++ 16 files changed, 107 insertions(+), 39 deletions(-) diff --git a/misc/cron/updatetoken.php b/misc/cron/updatetoken.php index f7cba2ab20..9e6713235b 100644 --- a/misc/cron/updatetoken.php +++ b/misc/cron/updatetoken.php @@ -31,7 +31,9 @@ if (!Common::isPhpCliMode()) { $testmode = in_array('--testmode', $_SERVER['argv']); if ($testmode) { - Config::getInstance()->setTestEnvironment(); + require_once PIWIK_INCLUDE_PATH . "/tests/PHPUnit/TestingEnvironment.php"; + + \Piwik_TestingEnvironment::addHooks(); } $token = Db::get()->fetchOne("SELECT token_auth diff --git a/plugins/CoreConsole/Commands/RunUITests.php b/plugins/CoreConsole/Commands/RunUITests.php index 1eab394a0e..005a1bf440 100644 --- a/plugins/CoreConsole/Commands/RunUITests.php +++ b/plugins/CoreConsole/Commands/RunUITests.php @@ -23,6 +23,7 @@ class RunUITests extends ConsoleCommand $this->addOption("persist-fixture-data", null, InputOption::VALUE_NONE, "Persist test data in a database and do not execute tear down."); $this->addOption('keep-symlinks', null, InputOption::VALUE_NONE, "Keep recursive directory symlinks so test pages can be viewed in a browser."); $this->addOption('print-logs', null, InputOption::VALUE_NONE, "Print webpage logs even if tests succeed."); + $this->addOption('drop', null, InputOption::VALUE_NONE, "Drop the existing database and re-setup a persisted fixture."); } protected function execute(InputInterface $input, OutputInterface $output) @@ -31,6 +32,7 @@ class RunUITests extends ConsoleCommand $persistFixtureData = $input->getOption("persist-fixture-data"); $keepSymlinks = $input->getOption('keep-symlinks'); $printLogs = $input->getOption('print-logs'); + $drop = $input->getOption('drop'); $options = array(); if ($persistFixtureData) { @@ -44,6 +46,10 @@ class RunUITests extends ConsoleCommand if ($printLogs) { $options[] = "--print-logs"; } + + if ($drop) { + $options[] = "--drop"; + } $options = implode(" ", $options); $specs = implode(" ", $specs); diff --git a/plugins/UserCountryMap/javascripts/realtime-map.js b/plugins/UserCountryMap/javascripts/realtime-map.js index 45e3076125..9b6dd763bf 100644 --- a/plugins/UserCountryMap/javascripts/realtime-map.js +++ b/plugins/UserCountryMap/javascripts/realtime-map.js @@ -449,7 +449,7 @@ visitSymbols.layout().render(); - if (!enableAnimation) { + if (enableAnimation) { $.each(newSymbols, function (i, s) { if (i > 10) return false; diff --git a/plugins/Zeitgeist/templates/empty.twig b/plugins/Zeitgeist/templates/empty.twig index d892ab6016..4275f80503 100644 --- a/plugins/Zeitgeist/templates/empty.twig +++ b/plugins/Zeitgeist/templates/empty.twig @@ -1,6 +1,2 @@ {% block content %} -<<<<<<< HEAD {% endblock %} -======= -{% endblock %} ->>>>>>> 4d5e89b... refs #2174 loads of bugfixes and improvements diff --git a/tests/PHPUnit/Fixture.php b/tests/PHPUnit/Fixture.php index 67fba647f1..7b9fb329ac 100644 --- a/tests/PHPUnit/Fixture.php +++ b/tests/PHPUnit/Fixture.php @@ -64,6 +64,8 @@ class Fixture extends PHPUnit_Framework_Assert public $overwriteExisting = true; public $configureComponents = true; public $persistFixtureData = false; + public $resetPersistedFixture = false; + public $printToScreen = false; public $testEnvironment = null; @@ -108,7 +110,11 @@ class Fixture extends PHPUnit_Framework_Assert static::connectWithoutDatabase(); - if ($this->dropDatabaseInSetUp) { + if ($this->dropDatabaseInSetUp + || $this->resetPersistedFixture + ) { + $this->log("Dropping database..."); + $this->dropDatabase(); } @@ -180,12 +186,10 @@ class Fixture extends PHPUnit_Framework_Assert $this->setUp(); $this->markFixtureSetUp(); - echo "Database {$this->dbName} marked as successfully set up."; + $this->log("Database {$this->dbName} marked as successfully set up."); } else { - echo "Using existing database {$this->dbName}."; + $this->log("Using existing database {$this->dbName}."); } - - $this->testEnvironment->save(); } public function isFixtureSetUp() @@ -685,6 +689,13 @@ class Fixture extends PHPUnit_Framework_Assert DbHelper::dropDatabase(); } + + public function log($message) + { + if ($this->printToScreen) { + echo $message . "\n"; + } + } } // TODO: remove when other plugins don't use BaseFixture diff --git a/tests/PHPUnit/Fixtures/ManySitesImportedLogsWithXssAttempts.php b/tests/PHPUnit/Fixtures/ManySitesImportedLogsWithXssAttempts.php index 3af47af61a..da4bbdf827 100644 --- a/tests/PHPUnit/Fixtures/ManySitesImportedLogsWithXssAttempts.php +++ b/tests/PHPUnit/Fixtures/ManySitesImportedLogsWithXssAttempts.php @@ -143,25 +143,6 @@ class Test_Piwik_Fixture_ManySitesImportedLogsWithXssAttempts extends Test_Piwik return strcmp($lhs['uniqueId'], $rhs['uniqueId']); }); - // create empty dashboard - $widget = reset($allWidgets); - $dashboard = array( - array( - array( - 'uniqueId' => $widget['uniqueId'], - 'parameters' => $widget['parameters'] - ) - ), - array(), - array() - ); - - $_GET['name'] = 'D4'; - $_GET['layout'] = Common::json_encode($dashboard); - $_GET['idDashboard'] = 5; - $_GET['idSite'] = 2; - FrontController::getInstance()->fetchDispatch('Dashboard', 'saveLayout'); - $_GET = $oldGet; } diff --git a/tests/PHPUnit/Fixtures/OmniFixture.php b/tests/PHPUnit/Fixtures/OmniFixture.php index e2b351a176..ac0c610b79 100644 --- a/tests/PHPUnit/Fixtures/OmniFixture.php +++ b/tests/PHPUnit/Fixtures/OmniFixture.php @@ -39,7 +39,6 @@ class OmniFixture extends \Fixture if (is_subclass_of($className, 'Fixture') && !is_subclass_of($className, __CLASS__) && $className != __CLASS__ - && $className != "Test_Piwik_Fixture_CustomAlerts" // HACK! should check by namespace ) { $fixture = new $className(); if (!property_exists($fixture, 'dateTime')) { diff --git a/tests/PHPUnit/Fixtures/UITestFixture.php b/tests/PHPUnit/Fixtures/UITestFixture.php index 9c89de3619..22f48283b0 100644 --- a/tests/PHPUnit/Fixtures/UITestFixture.php +++ b/tests/PHPUnit/Fixtures/UITestFixture.php @@ -14,6 +14,8 @@ use Piwik\AssetManager; use Piwik\Date; use Piwik\Common; use Piwik\Db; +use Piwik\FrontController; +use Piwik\Option; /** * Fixture for UI tests. @@ -25,11 +27,14 @@ class UITestFixture extends OmniFixture parent::setUp(); $this->addNewSitesForSiteSelector(); + $this->createEmptyDashboard(); DbHelper::createAnonymousUser(); UsersManagerAPI::getInstance()->setSuperUserAccess('superUserLogin', true); - // launch archiving so tests don't run out of time + Option::set("Tests.forcedNowTimestamp", $this->now->getTimestamp()); + + // launch archiving so tests don't run out of time $date = Date::factory($this->dateTime)->toString(); VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date); VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date, urlencode($this->segment)); @@ -41,12 +46,18 @@ class UITestFixture extends OmniFixture AssetManager::getInstance()->removeMergedAssets(); - $this->testEnvironment->forcedNowTimestamp = $this->now->getTimestamp(); - $visitorIdDeterministic = bin2hex(Db::fetchOne( "SELECT idvisitor FROM " . Common::prefixTable('log_visit') . " WHERE idsite = 2 AND location_latitude IS NOT NULL LIMIT 1")); $this->testEnvironment->forcedIdVisitor = $visitorIdDeterministic; + + $forcedNowTimestamp = Option::get("Tests.forcedNowTimestamp"); + if ($forcedNowTimestamp == false) { + throw Exception("Incorrect fixture setup, Tests.forcedNowTimestamp option does not exist! Run the setup again."); + } + + $this->testEnvironment->forcedNowTimestamp = $forcedNowTimestamp; + $this->testEnvironment->save(); } public function addNewSitesForSiteSelector() @@ -55,4 +66,33 @@ class UITestFixture extends OmniFixture self::createWebsite("2011-01-01 00:00:00", $ecommerce = 1, $siteName = "Site #$i", $siteUrl = "http://site$i.com"); } } + + public function createEmptyDashboard() + { + $oldGet = $_GET; + + // create empty dashboard + $dashboard = array( + array( + array( + 'uniqueId' => "widgetVisitsSummarygetEvolutionGraphcolumnsArray", + 'parameters' => array( + 'module' => 'VisitsSummary', + 'action' => 'getEvolutionGraph', + 'columns' => 'nb_visits' + ) + ) + ), + array(), + array() + ); + + $_GET['name'] = 'D4'; + $_GET['layout'] = Common::json_encode($dashboard); + $_GET['idDashboard'] = 5; + $_GET['idSite'] = 2; + FrontController::getInstance()->fetchDispatch('Dashboard', 'saveLayout'); + + $_GET = $oldGet; + } } \ No newline at end of file diff --git a/tests/PHPUnit/TestingEnvironment.php b/tests/PHPUnit/TestingEnvironment.php index 7c1ba8f1ac..65b1095880 100644 --- a/tests/PHPUnit/TestingEnvironment.php +++ b/tests/PHPUnit/TestingEnvironment.php @@ -5,6 +5,8 @@ use Piwik\Config; use Piwik\Common; use Piwik\Session\SessionNamespace; +require_once PIWIK_INCLUDE_PATH . "/core/Config.php"; + if (!defined('PIWIK_TEST_MODE')) { define('PIWIK_TEST_MODE', true); } @@ -93,10 +95,6 @@ class Piwik_TestingEnvironment $config->setTestEnvironment(); - if ($testingEnvironment->dbName) { - $config->database_tests['dbname'] = $config->database['dbname'] = $testingEnvironment->dbName; - } - $pluginsToLoad = \Piwik\Plugin\Manager::getInstance()->getPluginsToLoadDuringTests(); $config->Plugins = array('Plugins' => $pluginsToLoad); @@ -106,6 +104,16 @@ class Piwik_TestingEnvironment $config->Plugins_Tracker = array('Plugins_Tracker' => $trackerPluginsToLoad); $config->log['log_writers'] = array('file'); }); + Piwik::addAction('Db.getDatabaseConfig', function (&$dbConfig) use ($testingEnvironment) { + if ($testingEnvironment->dbName) { + $dbConfig['dbname'] = $testingEnvironment->dbName; + } + }); + Piwik::addAction('Tracker.getDatabaseConfig', function (&$dbConfig) use ($testingEnvironment) { + if ($testingEnvironment->dbName) { + $dbConfig['dbname'] = $testingEnvironment->dbName; + } + }); Piwik::addAction('Request.dispatch', function() { \Piwik\Plugins\CoreVisualizations\Visualizations\Cloud::$debugDisableShuffle = true; \Piwik\Visualization\Sparkline::$enableSparklineImages = false; @@ -114,6 +122,9 @@ class Piwik_TestingEnvironment Piwik::addAction('AssetManager.getStylesheetFiles', function(&$stylesheets) { $stylesheets[] = 'tests/resources/screenshot-override/override.css'; }); + Piwik::addAction('AssetManager.getJavaScriptFiles', function(&$jsFiles) { + $jsFiles[] = 'tests/resources/screenshot-override/override.js'; + }); Piwik::addAction('Test.Mail.send', function($mail) { $outputFile = PIWIK_INCLUDE_PATH . 'tmp/' . Common::getRequestVar('module') . '.' . Common::getRequestVar('action') . '.mail.json'; diff --git a/tests/PHPUnit/UI b/tests/PHPUnit/UI index 1e8287b5c9..b3749d8fa0 160000 --- a/tests/PHPUnit/UI +++ b/tests/PHPUnit/UI @@ -1 +1 @@ -Subproject commit 1e8287b5c9b7afa3d67fee2b5932afc87d30fa26 +Subproject commit b3749d8fa0ad972ce4c0fe2b3767d425f45bdb48 diff --git a/tests/PHPUnit/proxy/piwik.php b/tests/PHPUnit/proxy/piwik.php index bae1f90a72..5776660840 100755 --- a/tests/PHPUnit/proxy/piwik.php +++ b/tests/PHPUnit/proxy/piwik.php @@ -21,6 +21,8 @@ require realpath(dirname(__FILE__)) . "/includes.php"; // calling us waits for the full request to process before unblocking ob_start(); +Piwik_TestingEnvironment::addHooks(); + Config::getInstance()->setTestEnvironment(); try { diff --git a/tests/lib/screenshot-testing/support/app.js b/tests/lib/screenshot-testing/support/app.js index 2a812ada0b..73eade6892 100644 --- a/tests/lib/screenshot-testing/support/app.js +++ b/tests/lib/screenshot-testing/support/app.js @@ -139,6 +139,10 @@ Application.prototype.setupDatabase = function () { processArgs.push('--persist-fixture-data'); } + if (options['drop']) { + processArgs.push('--drop'); + } + var child = require('child_process').spawn(config.php, processArgs); child.stdout.on("data", function (data) { diff --git a/tests/lib/screenshot-testing/support/page-renderer.js b/tests/lib/screenshot-testing/support/page-renderer.js index 65a492164e..3c99751faf 100644 --- a/tests/lib/screenshot-testing/support/page-renderer.js +++ b/tests/lib/screenshot-testing/support/page-renderer.js @@ -40,6 +40,10 @@ PageRenderer.prototype.getCurrentUrl = function () { }; // event queueing functions +PageRenderer.prototype.wait = function (waitTime) { + this.queuedEvents.push([this._wait, waitTime]); +}; + PageRenderer.prototype.click = function () { var selector = arguments[0], waitTime = null, @@ -86,6 +90,10 @@ PageRenderer.prototype.evaluate = function (impl, waitTime) { }; // event impl functions +PageRenderer.prototype._wait = function (callback) { + callback(); +}; + PageRenderer.prototype._click = function (selector, modifiers, callback) { var position = this._getPosition(selector); diff --git a/tests/lib/screenshot-testing/support/setupDatabase.php b/tests/lib/screenshot-testing/support/setupDatabase.php index c70eeb45af..bbd7c979f2 100644 --- a/tests/lib/screenshot-testing/support/setupDatabase.php +++ b/tests/lib/screenshot-testing/support/setupDatabase.php @@ -21,6 +21,10 @@ $fixture = new \Piwik\Tests\Fixtures\UITestFixture(); if (in_array("--persist-fixture-data", $argv)) { $fixture->persistFixtureData = true; } +if (in_array("--drop", $argv)) { + $fixture->resetPersistedFixture = true; +} +$fixture->printToScreen = true; $fixture->performSetUp(""); // make sure symbolic links exist (phantomjs doesn't support symlink-ing yet) diff --git a/tests/lib/screenshot-testing/support/teardownDatabase.php b/tests/lib/screenshot-testing/support/teardownDatabase.php index 0771170692..45a07c91aa 100644 --- a/tests/lib/screenshot-testing/support/teardownDatabase.php +++ b/tests/lib/screenshot-testing/support/teardownDatabase.php @@ -19,5 +19,6 @@ require_once dirname(__FILE__) . "/../../../PHPUnit/bootstrap.php"; $fixture = new \Piwik\Tests\Fixtures\UITestFixture(); $fixture->dropDatabaseInSetUp = false; +$fixture->printToScreen = true; $fixture->performSetUp("", $environmentOnly = true); $fixture->performTearDown(""); \ No newline at end of file diff --git a/tests/resources/screenshot-override/override.js b/tests/resources/screenshot-override/override.js index e69de29bb2..7113d0d134 100644 --- a/tests/resources/screenshot-override/override.js +++ b/tests/resources/screenshot-override/override.js @@ -0,0 +1,3 @@ +$(document).ready(function () { + $('.ui-inline-help:contains(UTC time is)').hide(); +}); \ No newline at end of file -- GitLab