diff --git a/tests/PHPUnit/Fixture.php b/tests/PHPUnit/Fixture.php index d0a1aa0fc702a7dd2c0c06de863859f9a6666346..ab61984c6852df70597f2f0396a6ab7d42aad9b6 100644 --- a/tests/PHPUnit/Fixture.php +++ b/tests/PHPUnit/Fixture.php @@ -62,6 +62,7 @@ class Fixture extends PHPUnit_Framework_Assert public $createSuperUser = true; public $overwriteExisting = true; public $configureComponents = true; + public $persistFixtureData = false; public $testOptions = array(); @@ -77,27 +78,7 @@ class Fixture extends PHPUnit_Framework_Assert // empty } - private function handleConfiguration($testCase) - { - $testsConfig = Config::getInstance()->Tests; - if (!empty($testsConfig['persist_fixture_data']) - && is_subclass_of($testCase, "Piwik\\Tests\\UI\\UITest") - ) { - $this->dbName = get_class($this); - $this->dropDatabaseInSetUp = false; - $this->dropDatabaseInTearDown = false; - $this->overwriteExisting = false; - - Config::getInstance()->database_tests['dbname'] = Config::getInstance()->database['dbname'] = $this->dbName; - Config::getInstance()->saveConfigOverride(); - } - - if ($this->dbName === false) { // must be after test config is created - $this->dbName = Config::getInstance()->database['dbname']; - } - } - - public function performSetUp($testCase) + public function performSetUp($testCase, $setupEnvironmentOnly = false) { try { \Piwik\SettingsPiwik::$piwikUrlCache = ''; @@ -108,7 +89,19 @@ class Fixture extends PHPUnit_Framework_Assert Config::getInstance()->setTestEnvironment(); } - $this->handleConfiguration($testCase); + if ($this->persistFixtureData) { + $this->dbName = str_replace("\\", "_", get_class($this)); + $this->dropDatabaseInSetUp = false; + $this->dropDatabaseInTearDown = false; + $this->overwriteExisting = false; + + Config::getInstance()->database_tests['dbname'] = Config::getInstance()->database['dbname'] = $this->dbName; + Config::getInstance()->saveConfigOverride(); + } + + if ($this->dbName === false) { // must be after test config is created + $this->dbName = Config::getInstance()->database['dbname']; + } static::connectWithoutDatabase(); @@ -170,6 +163,10 @@ class Fixture extends PHPUnit_Framework_Assert self::createSuperUser($removeExisting = true); } + if ($setupEnvironmentOnly) { + return; + } + if ($this->overwriteExisting || !$this->isFixtureSetUp() ) { @@ -177,7 +174,7 @@ class Fixture extends PHPUnit_Framework_Assert $this->markFixtureSetUp(); } else { - echo "\n---Using existing database {$this->dbName}---\n"; + echo "Using existing database {$this->dbName}."; } foreach ($this->testOptions as $key => $value) { diff --git a/tests/PHPUnit/Fixtures/OmniFixture.php b/tests/PHPUnit/Fixtures/OmniFixture.php index 36860e226e57cf23fccfbe6a32d3f77de5ffb31c..ae9fed1cbc436e09d9486152a6c794255ce4aae8 100644 --- a/tests/PHPUnit/Fixtures/OmniFixture.php +++ b/tests/PHPUnit/Fixtures/OmniFixture.php @@ -5,6 +5,7 @@ * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ +namespace Piwik\Tests\Fixtures; use Piwik\Date; use Piwik\Tracker\Visit; @@ -13,7 +14,7 @@ use Piwik\Tracker\Visit; * This fixture is the combination of every other fixture defined by Piwik. Should be used * with year periods. */ -class Test_Piwik_Fixture_OmniFixture extends Fixture +class OmniFixture extends \Fixture { public $month = '2012-01'; public $idSite = 'all'; @@ -36,6 +37,7 @@ class Test_Piwik_Fixture_OmniFixture extends Fixture $classes = get_declared_classes(); foreach ($classes as $className) { if (is_subclass_of($className, 'Fixture') + && !is_subclass_of($className, __CLASS__) && $className != __CLASS__ ) { $fixture = new $className(); diff --git a/tests/PHPUnit/Fixtures/UITestFixture.php b/tests/PHPUnit/Fixtures/UITestFixture.php new file mode 100644 index 0000000000000000000000000000000000000000..d138a103006fd63bce790bf7780932309b3fcd21 --- /dev/null +++ b/tests/PHPUnit/Fixtures/UITestFixture.php @@ -0,0 +1,45 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ +namespace Piwik\Tests\Fixtures; + +use Piwik\DbHelper; +use Piwik\Plugins\UsersManager\API as UsersManagerAPI; +use Piwik\Plugins\VisitsSummary\API as VisitsSummaryAPI; +use Piwik\AssetManager; +use Piwik\Date; + +/** + * TODO + */ +class UITestFixture extends OmniFixture +{ + private static $recursiveProxyLinkNames = array('libs', 'plugins', 'tests'); + + public function setUp() + { + parent::setUp(); + + DbHelper::createAnonymousUser(); + UsersManagerAPI::getInstance()->setSuperUserAccess('superUserLogin', true); + + AssetManager::getInstance()->removeMergedAssets(); + + // 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)); + + // make sure symbolic links exist (phantomjs doesn't support symlink-ing yet) + foreach (self::$recursiveProxyLinkNames as $linkName) { + $linkPath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName; + if (!file_exists($linkPath)) { + symlink(PIWIK_INCLUDE_PATH . '/' . $linkName, $linkPath); + } + } + } +} \ No newline at end of file diff --git a/tests/PHPUnit/UITest.php b/tests/PHPUnit/UITest.php deleted file mode 100644 index f4be4f53f9f9b79ad957c3aec5b7c68cab7cd3f3..0000000000000000000000000000000000000000 --- a/tests/PHPUnit/UITest.php +++ /dev/null @@ -1,418 +0,0 @@ -<?php -/** - * Piwik - Open source web analytics - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ -namespace Piwik\Tests\UI; - -use Piwik\Access; -use Piwik\AssetManager; -use Piwik\Date; -use Piwik\Db; -use Piwik\DbHelper; -use Piwik\Plugins\VisitsSummary\API; -use Piwik\Plugins\UsersManager\API as UsersManagerApi; -use Piwik\ArchiveProcessor\Rules; -use \Fixture; - -abstract class UITest extends \IntegrationTestCase -{ - const IMAGE_TYPE = 'png'; - const CAPTURE_PROGRAM = 'phantomjs'; - const SCREENSHOT_GROUP_SIZE = 12; - const DEBUG_IMAGE_MAGICK_COMPARE = true; - const GENERATE_ZEITGEIST = false; - - private static $recursiveProxyLinkNames = array('libs', 'plugins', 'tests'); - private static $imageMagickAvailable = false; - - public static $failureScreenshotNames = array(); - - public static function createAccessInstance() - { - Access::setSingletonInstance($access = new Test_Access_OverrideLogin()); - \Piwik\Piwik::postEvent('Request.initAuthenticationObject'); - } - - public static function setUpBeforeClass() - { - if (self::CAPTURE_PROGRAM == 'slimerjs' - && !self::isSlimerJsAvailable() - ) { - self::markTestSkipped("slimerjs is not available, skipping UI integration tests. " - . "(install by downloading http://slimerjs.org/download.html)"); - } else if (self::CAPTURE_PROGRAM == 'phantomjs' - && !self::isPhantomJsAvailable() - ) { - self::markTestSkipped("phantomjs is not available, skipping UI integration tests. " - . "(install by downloading http://phantomjs.org/download.html)"); - } - - parent::setUpBeforeClass(); - - DbHelper::createAnonymousUser(); - UsersManagerApi::getInstance()->setSuperUserAccess('superUserLogin', true); - - AssetManager::getInstance()->removeMergedAssets(); - - // launch archiving so tests don't run out of time - $date = Date::factory(static::$fixture->dateTime)->toString(); - API::getInstance()->get(static::$fixture->idSite, 'year', $date); - API::getInstance()->get(static::$fixture->idSite, 'year', $date, urlencode(static::$fixture->segment)); - - // make sure processed & expected dirs exist - self::makeDirsAndLinks(); - - // run slimerjs/phantomjs w/ all urls so we only invoke it once per 25 entries (travis needs - // there to be output) - static::generateScreenshots(); - - // check if image magick available - self::$imageMagickAvailable = self::checkImageMagickAvailable(); - - // remove existing diffs - self::removeExistingDiffs(); - } - - public static function generateScreenshots() - { - $urlsToTest = static::getUrlsForTesting(); - - reset($urlsToTest); - for ($i = 0; $i < count($urlsToTest); $i += self::SCREENSHOT_GROUP_SIZE) { - $urls = array(); - echo "Generating screenshots..."; - for ($j = $i; $j != $i + self::SCREENSHOT_GROUP_SIZE && $j < count($urlsToTest); ++$j) { - $currentTest = current($urlsToTest); - - if (count($currentTest) == 2) { - list($name, $urlQuery) = $currentTest; - $jsToTest = false; - } else { - list($name, $urlQuery, $jsToTest) = $currentTest; - } - - $testUrl = self::getProxyUrl() . $urlQuery; - - // Screenshot morpheus - list($processedScreenshotPath, $expectedScreenshotPath) = self::getProcessedAndExpectedScreenshotPaths($name); - $urls[] = array($processedScreenshotPath, $testUrl, $jsToTest); - - // Screenshot Zeitgeist - if (self::GENERATE_ZEITGEIST) { - list($processedScreenshotPath, $expectedScreenshotPath) = self::getProcessedAndExpectedScreenshotPaths($name, "Zeitgeist/"); - $enableZeitgeist = "&zeitgeist=1"; - // Add the parameter to the query string, not the hash - if(($hash = strpos($testUrl, '#')) !== false) { - $testUrl = substr($testUrl, 0, $hash) . $enableZeitgeist . substr($testUrl, $hash); - } else { - $testUrl .= $enableZeitgeist; - } - - $urls[] = array($processedScreenshotPath, $testUrl, $jsToTest); - } - - next($urlsToTest); - echo "."; - } - echo "\n"; - - self::runCaptureProgram($urls); - } - } - - public static function removeExistingDiffs() - { - $files = glob(dirname(__FILE__) . '/UI/screenshot-diffs/' . static::getOutputPrefix() . '*.png'); - foreach ($files as $file) { - unlink($file); - } - } - - public static function tearDownAfterClass() - { - if (file_exists("C:\\nppdf32Log\\debuglog.txt")) { // remove slimerjs oddity - unlink("C:\\nppdf32Log\\debuglog.txt"); - } - - self::removeRecursiveLinks(); - - Db::createDatabaseObject(); - - self::outputDiffViewerHtmlFile(); - - parent::tearDownAfterClass(); - } - - public function setUp() - { - parent::setUp(); - - if (!Db::get()) { - DbHelper::createDatabaseObject(); - } - } - - public function tearDown() - { - parent::tearDown(); - - Db::get()->closeConnection(); - } - - protected static function runCaptureProgram($urlInfo) - { - file_put_contents(PIWIK_INCLUDE_PATH . '/tmp/urls.txt', json_encode($urlInfo)); - $cmd = self::CAPTURE_PROGRAM . " \"" . PIWIK_INCLUDE_PATH . "/tests/resources/screenshot-capture/capture.js\" 2>&1"; - \Piwik\Log::info("running capture: $cmd"); - - exec($cmd, $output, $result); - $output = implode("\n", $output); - if ($result !== 0 - || strpos($output, "ERROR") !== false - ) { - echo self::CAPTURE_PROGRAM . " failed: " . $output . "\n\ncommand used: $cmd\n"; - } - - \Piwik\Log::info("phantomjs output: %s", $output); - - return $output; - } - - protected function compareScreenshots($testsInfo) - { - $failures = array(); - foreach ($testsInfo as $info) { - list($name, $urlQuery) = $info; - - // compare processed w/ expected - try { - $this->compareScreenshot($name, $urlQuery); - } catch (Exception $ex) { - $failures[] = $ex; - } - } - } - - protected function compareScreenshot($name, $urlQuery) - { - list($processedPath, $expectedPath) = self::getProcessedAndExpectedScreenshotPaths($name); - - $this->compareScreenshotAgainstExpected($name, $urlQuery, $processedPath, $expectedPath); - } - - private function saveImageDiff($expectedPath, $processedPath, $diffPath) - { - $cmd = "compare \"$expectedPath\" \"$processedPath\" \"$diffPath\" 2>&1"; - \Piwik\Log::info("running compare: $cmd"); - - exec($cmd, $output, $result); - - if (self::DEBUG_IMAGE_MAGICK_COMPARE - && $result !== 0 - ) { - echo "Could not save image diff: " . implode("\n", $output) . "\n"; - } - } - - private static function checkImageMagickAvailable() - { - return self::isProgramAvailable('compare'); - } - - private static function isSlimerJsAvailable() - { - return self::isProgramAvailable('slimerjs'); - } - - public static function isPhantomJsAvailable() - { - return self::isProgramAvailable('phantomjs'); - } - - private static function isProgramAvailable($name) - { - exec($name . ' --help 2>&1', $output, $result); - return $result === 0 || $result === 1; - } - - protected static function getProcessedAndExpectedScreenshotPaths($name, $pathSuffix = '') - { - list($processedDir, $expectedDir) = self::getProcessedAndExpectedDirs(); - - $outputPrefix = static::getOutputPrefix(); - - $processedScreenshotPath = $processedDir . $pathSuffix . $outputPrefix . '_' . "$name." . self::IMAGE_TYPE; - $expectedScreenshotPath = $expectedDir . $pathSuffix . $outputPrefix . '_' . "$name." . self::IMAGE_TYPE; - - return array($processedScreenshotPath, $expectedScreenshotPath); - } - - protected static function getProcessedAndExpectedDirs() - { - $path = self::getPathToTestDirectory() . '/../UI'; - return array($path . '/processed-ui-screenshots/', $path . '/expected-ui-screenshots/'); - } - - public static function getProxyUrl() - { - return Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php'; - } - - private static function makeDirsAndLinks() - { - $dirs = array_merge(self::getProcessedAndExpectedDirs(), array( - PIWIK_INCLUDE_PATH . '/tmp/sessions', self::getScreenshotDiffDir() - )); - foreach ($dirs as $dir) { - if (!is_dir($dir)) { - mkdir($dir); - } - } - - self::createProxySymlinks(); - } - - private static function createProxySymlinks() - { - foreach (self::$recursiveProxyLinkNames as $linkName) { - $linkPath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName; - if (!file_exists($linkPath)) { - symlink(PIWIK_INCLUDE_PATH . '/' . $linkName, $linkPath); - } - } - } - - private static function removeRecursiveLinks() - { - foreach (self::$recursiveProxyLinkNames as $linkName) { - $wholePath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName; - if (file_exists($wholePath)) { - unlink($wholePath); - } - } - } - - private static function getScreenshotDiffPath($name) - { - $diffDir = self::getScreenshotDiffDir(); - return $diffDir . "/" . $name . '.' . self::IMAGE_TYPE; - } - - private static function getScreenshotDiffDir() - { - return dirname(__FILE__) . "/UI/screenshot-diffs"; - } - - private static function outputDiffViewerHtmlFile() - { - $diffViewerPath = self::getScreenshotDiffDir() . '/diffviewer.html'; - if (!empty(self::$failureScreenshotNames)) { - echo "\nFailures encountered. View all diffs at: -$diffViewerPath - -If processed screenshots are correct, you can copy the generated screenshots to the expected screenshot folder. - -*** IMPORTANT *** In your commit message, explain the cause of the difference in rendering so other Piwik developers will be aware of it."; - } - - list($processedDir, $expectedDir) = self::getProcessedAndExpectedDirs(); - $diffDir = self::getScreenshotDiffDir(); - - $diffViewerEntries = array(); - foreach (self::$failureScreenshotNames as $name) { - $file = $name . '.png'; - - $diffFileOrError = $file; - if (!is_file($diffDir . '/' . $file)) { - $diffFileOrError = false; - } - - $diffViewerEntries[] = array( - 'name' => $name, - 'expectedUrl' => 'https://raw.github.com/piwik/piwik-ui-tests/master/expected-ui-screenshots/' . $file, - 'processedUrl' => '../processed-ui-screenshots/' . $file, - 'diffUrl' => $diffFileOrError - ); - } - - $diffViewerHtml = '<html> -<head></head> -<body> -<h1>Screenshot Test Failures</h1> -<table> - <tr> - <th>Name</th> - <th>Expected</th> - <th>Processed</th> - <th>Difference</th> - </tr>'; - foreach ($diffViewerEntries as $fileInfo) { - $diffViewerHtml .= ' - <tr> - <td>' . $fileInfo['name'] . '</td> - <td><a href="' . $fileInfo['expectedUrl'] . '">Expected</a></td> - <td><a href="' . $fileInfo['processedUrl'] . '">Processed</a></td> - <td>'; - if (!empty($fileInfo['diffUrl'])) { - $diffViewerHtml .= '<a href="' . $fileInfo['diffUrl'] . '">Difference</a>'; - } else { - $diffViewerHtml .= "<em>Could not create diff.</em>"; - } - $diffViewerHtml .= ' - </td> - </tr>'; - } - $diffViewerHtml .= ' -</table> -</body> -</html>'; - - file_put_contents($diffViewerPath, $diffViewerHtml); - } - - /** - * @param $name - * @param $urlQuery - * @param $processedPath - * @param $expectedPath - */ - protected function compareScreenshotAgainstExpected($name, $urlQuery, $processedPath, $expectedPath) - { - if (!file_exists($processedPath)) { - $this->fail("failed to generate screenshot for '$name'."); - return; - } - - $fullName = static::getOutputPrefix() . '_' . $name; - - $processed = file_get_contents($processedPath); - - if (!file_exists($expectedPath)) { - self::$failureScreenshotNames[] = $fullName; - - $this->fail("expected screenshot for '$name' test is missing. -Generated screenshot: $processedPath"); - return; - } - - $expected = file_get_contents($expectedPath); - if ($expected != $processed) { - self::$failureScreenshotNames[] = $fullName; - - $diffPath = self::getScreenshotDiffPath($fullName); - - echo "\nFail: generated screenshot does not match expected for '$name'. -Url to reproduce: $urlQuery -Generated screenshot: $processedPath -Expected screenshot: $expectedPath -Screenshot diff: $diffPath\n"; - - $this->saveImageDiff($expectedPath, $processedPath, $diffPath); - } - - $this->assertTrue($expected == $processed, "screenshot compare failed for '$processedPath'"); - } -} diff --git a/tests/PHPUnit/UIUnitTest.php b/tests/PHPUnit/UIUnitTest.php deleted file mode 100644 index f49102ad1e3f57d68e46a227395020a896b9a14a..0000000000000000000000000000000000000000 --- a/tests/PHPUnit/UIUnitTest.php +++ /dev/null @@ -1,40 +0,0 @@ -<?php -/** - * Piwik - Open source web analytics - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ -namespace Piwik\Tests\UI; - -abstract class UIUnitTest extends UITest -{ - public static function generateScreenshots() - { - $url = static::getUrlToTest(); - $screens = static::getScreensToCapture(); - - $screensData = array(); - foreach ($screens as $info) { - list($name, $js) = $info; - - list($processedScreenshotPath, $expectedScreenshotPath) = self::getProcessedAndExpectedScreenshotPaths($name); - $screensData[] = array($processedScreenshotPath, $js); - } - - $captureProgramData = array( - 'url' => self::getProxyUrl() . $url, - 'screens' => $screensData - ); - - echo "Generating screenshots...\n"; - self::runCaptureProgram($captureProgramData); - } - - protected function compareScreenshot($name, $ignore = false) - { - list($processedScreenshotPath, $expectedScreenshotPath) = self::getProcessedAndExpectedScreenshotPaths($name); - - $this->compareScreenshotAgainstExpected($name, false, $processedScreenshotPath, $expectedScreenshotPath); - } -} \ No newline at end of file diff --git a/tests/PHPUnit/bootstrap.php b/tests/PHPUnit/bootstrap.php index e133221e7fb4261dd483903332b18c482d0d821f..d4e3a724509c472a09e56467c83f4fb099bf628b 100644 --- a/tests/PHPUnit/bootstrap.php +++ b/tests/PHPUnit/bootstrap.php @@ -37,8 +37,6 @@ require_once PIWIK_INCLUDE_PATH . '/core/FrontController.php'; require_once PIWIK_INCLUDE_PATH . '/libs/spyc.php'; require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/DatabaseTestCase.php'; require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/IntegrationTestCase.php'; -require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/UITest.php'; -require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/UIUnitTest.php'; require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/FakeAccess.php'; require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/MockPiwikOption.php'; require_once file_exists(PIWIK_INCLUDE_PATH . '/vendor/autoload.php')