From 39a2c9c7237f2cd62c0949ac77c5e8673e9d3963 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@gmail.com>
Date: Thu, 19 Feb 2015 23:13:37 +0000
Subject: [PATCH] fixes #6635 automatically configure database_tests config for
 developers when running tests.

I also replaced the check that does a request against Piwik to check
whether it is installed with `SettingsPiwik::isInstalled()` as discussed
recently
---
 config/global.ini.php       |  2 +-
 core/Updates/2.11.1-b3.php  | 39 ++++++++++++++++++++++
 tests/PHPUnit/bootstrap.php | 65 ++++++++++++++++++++++---------------
 3 files changed, 78 insertions(+), 28 deletions(-)
 create mode 100644 core/Updates/2.11.1-b3.php

diff --git a/config/global.ini.php b/config/global.ini.php
index 4972d40096..580abafd46 100644
--- a/config/global.ini.php
+++ b/config/global.ini.php
@@ -29,7 +29,7 @@ schema = Mysql
 
 [database_tests]
 host = localhost
-username = root
+username = "@USERNAME@"
 password =
 dbname = piwik_tests
 tables_prefix = piwiktests_
diff --git a/core/Updates/2.11.1-b3.php b/core/Updates/2.11.1-b3.php
new file mode 100644
index 0000000000..ebb82cabcf
--- /dev/null
+++ b/core/Updates/2.11.1-b3.php
@@ -0,0 +1,39 @@
+<?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\Updates;
+
+use Piwik\Config;
+use Piwik\Development;
+use Piwik\Updates;
+
+class Updates_2_11_1_b3 extends Updates
+{
+    /**
+     * Here you can define any action that should be performed during the update. For instance executing SQL statements,
+     * renaming config entries, updating files, etc.
+     */
+    static function update()
+    {
+        if (!Development::isEnabled()) {
+            return;
+        }
+
+        $config  = Config::getInstance();
+        $dbTests = $config->database_tests;
+
+        if ($dbTests['username'] === '@USERNAME@') {
+            $dbTests['username'] = 'root';
+        }
+
+        $config->database_tests = $dbTests;
+
+        $config->forceSave();
+    }
+}
diff --git a/tests/PHPUnit/bootstrap.php b/tests/PHPUnit/bootstrap.php
index 3a9cf799db..5a7eb7bce2 100644
--- a/tests/PHPUnit/bootstrap.php
+++ b/tests/PHPUnit/bootstrap.php
@@ -2,8 +2,9 @@
 
 use Piwik\Container\StaticContainer;
 use Piwik\Http;
-use Piwik\Tests\Framework\Fixture;
 use Piwik\Intl\Locale;
+use Piwik\Config;
+use Piwik\SettingsPiwik;
 
 define('PIWIK_TEST_MODE', true);
 define('PIWIK_PRINT_ERROR_BACKTRACE', false);
@@ -60,9 +61,9 @@ foreach($fixturesToLoad as $fixturePath) {
 
 Locale::setDefaultLocale();
 
-function prepareServerVariables()
+function prepareServerVariables(Config $config)
 {
-    $testConfig = \Piwik\Config::getInstance()->tests;
+    $testConfig = $config->tests;
 
     if ('@REQUEST_URI@' === $testConfig['request_uri']) {
         // config not done yet, if Piwik is installed we can automatically configure request_uri and http_host
@@ -72,8 +73,8 @@ function prepareServerVariables()
             $parsedUrl = parse_url($url);
             $testConfig['request_uri'] = $parsedUrl['path'];
             $testConfig['http_host']   = $parsedUrl['host'];
-            \Piwik\Config::getInstance()->tests = $testConfig;
-            \Piwik\Config::getInstance()->forceSave();
+            $config->tests = $testConfig;
+            $config->forceSave();
         }
     }
 
@@ -82,13 +83,41 @@ function prepareServerVariables()
     $_SERVER['REMOTE_ADDR'] = $testConfig['remote_addr'];
 }
 
-prepareServerVariables();
+function prepareTestDatabaseConfig(Config $config)
+{
+    $testDb = $config->database_tests;
+
+    if ('@USERNAME@' !== $testDb['username']) {
+        return; // testDb is already configured, we do not want to overwrite any existing settings.
+    }
+
+    $db = $config->database;
+    $testDb['username'] = $db['username'];
+
+    if (empty($testDb['password'])) {
+        $testDb['password'] = $db['password'];
+    }
+
+    if (empty($testDb['host'])) {
+        $testDb['host'] = $db['host'];
+    }
 
-// General requirement checks & help: a webserver must be running for tests to work if not running UnitTests!
-if (empty($_SERVER['argv']) || !in_array('UnitTests', $_SERVER['argv'])) {
-    checkPiwikSetupForTests();
+    $testDb['tables_prefix'] = ''; // tables_prefix has to be empty for UI tests
+
+    $config->database_tests = $testDb;
+    $config->forceSave();
+}
+
+if (!SettingsPiwik::isPiwikInstalled()) {
+    throw new Exception('Piwik needs to be installed in order to run the tests');
 }
 
+$config = Config::getInstance();
+$config->init();
+prepareServerVariables($config);
+prepareTestDatabaseConfig($config);
+checkPiwikSetupForTests();
+
 function checkPiwikSetupForTests()
 {
     if (empty($_SERVER['REQUEST_URI'])
@@ -108,22 +137,4 @@ Try again.";
         exit(1);
     }
 
-    $url = Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php?module=TestRunner&action=check';
-    $response = Http::sendHttpRequestBy('curl', $url, 5);
-
-    if ($response === 'OK'
-        // The SQL error is for Travis...
-        || strpos($response, 'Table &#039;piwik_tests.option&#039; doesn&#039;t exist') !== false
-        || strpos($response, 'Table &#039;piwik_tests.piwiktests_option&#039; doesn&#039;t exist') !== false
-        || strpos($response, 'Unknown database &#039;piwik_tests&#039;') !== false
-        || strpos($response, '<title>Piwik &rsaquo; Update</title>') !== false
-    ) {
-        return;
-    }
-
-    throw new Exception(sprintf(
-        "Piwik should be running at %s but this URL returned an unexpected response: '%s'",
-        $url,
-        $response
-    ));
 }
-- 
GitLab