Skip to content

Commit

Permalink
Dev: Namespace fix in test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Nov 11, 2021
1 parent 1e8015d commit 66d7f24
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions tests/TestHelper.php
Expand Up @@ -2,6 +2,8 @@

namespace ls\tests;

use Yii;
use Exception;
use Facebook\WebDriver\Exception\WebDriverException;
use PHPUnit\Framework\TestCase;
use Facebook\WebDriver\Remote\DesiredCapabilities;
Expand All @@ -23,17 +25,17 @@ class TestHelper extends TestCase
*/
public function importAll()
{
\Yii::import('application.helpers.common_helper', true);
\Yii::import('application.helpers.replacements_helper', true);
\Yii::import('application.helpers.surveytranslator_helper', true);
\Yii::import('application.helpers.admin.import_helper', true);
\Yii::import('application.helpers.expressions.em_manager_helper', true);
\Yii::import('application.helpers.expressions.em_manager_helper', true);
\Yii::import('application.helpers.qanda_helper', true);
\Yii::import('application.helpers.update.updatedb_helper', true);
\Yii::import('application.helpers.update.update_helper', true);
\Yii::import('application.helpers.SurveyRuntimeHelper', true);
\Yii::app()->loadHelper('admin/activate');
Yii::import('application.helpers.common_helper', true);
Yii::import('application.helpers.replacements_helper', true);
Yii::import('application.helpers.surveytranslator_helper', true);
Yii::import('application.helpers.admin.import_helper', true);
Yii::import('application.helpers.expressions.em_manager_helper', true);
Yii::import('application.helpers.expressions.em_manager_helper', true);
Yii::import('application.helpers.qanda_helper', true);
Yii::import('application.helpers.update.updatedb_helper', true);
Yii::import('application.helpers.update.update_helper', true);
Yii::import('application.helpers.SurveyRuntimeHelper', true);
Yii::app()->loadHelper('admin/activate');
}

/**
Expand Down Expand Up @@ -92,7 +94,7 @@ public function getSurveyOptions($surveyId)
'anonymized' => ($thissurvey['anonymized'] != 'N'),
'assessments' => ($thissurvey['assessments'] == 'Y'),
'datestamp' => ($thissurvey['datestamp'] == 'Y'),
'deletenonvalues'=>\Yii::app()->getConfig('deletenonvalues'),
'deletenonvalues'=>Yii::app()->getConfig('deletenonvalues'),
'hyperlinkSyntaxHighlighting' => (($LEMdebugLevel & LEM_DEBUG_VALIDATION_SUMMARY) == LEM_DEBUG_VALIDATION_SUMMARY),
'ipaddr' => ($thissurvey['ipaddr'] == 'Y'),
'radix'=>$radix,
Expand All @@ -101,8 +103,8 @@ public function getSurveyOptions($surveyId)
'savetimings' => ($thissurvey['savetimings'] == "Y"),
'surveyls_dateformat' => (isset($thissurvey['surveyls_dateformat']) ? $thissurvey['surveyls_dateformat'] : 1),
'startlanguage'=>(isset(App()->language) ? App()->language : $thissurvey['language']),
'target' => \Yii::app()->getConfig('uploaddir').DIRECTORY_SEPARATOR.'surveys'.DIRECTORY_SEPARATOR.$thissurvey['sid'].DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR,
'tempdir' => \Yii::app()->getConfig('tempdir').DIRECTORY_SEPARATOR,
'target' => Yii::app()->getConfig('uploaddir').DIRECTORY_SEPARATOR.'surveys'.DIRECTORY_SEPARATOR.$thissurvey['sid'].DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR,
'tempdir' => Yii::app()->getConfig('tempdir').DIRECTORY_SEPARATOR,
'timeadjust' => (isset($timeadjust) ? $timeadjust : 0),
'token' => (isset($clienttoken) ? $clienttoken : null),
);
Expand All @@ -117,7 +119,7 @@ public function activateSurvey($surveyId)
{
$survey = \Survey::model()->findByPk($surveyId);
if (empty($survey)) {
throw new \Exception('Found no survey with id ' . $surveyId);
throw new Exception('Found no survey with id ' . $surveyId);
}
$survey->anonymized = '';
$survey->datestamp = '';
Expand All @@ -136,7 +138,7 @@ public function activateSurvey($surveyId)
\SurveyDynamic::sid($surveyId);
\SurveyDynamic::model()->refreshMetaData();

$db = \Yii::app()->getDb();
$db = Yii::app()->getDb();
$db->schema->getTables();
$db->schema->refresh();
$db->active = false;
Expand All @@ -152,9 +154,9 @@ public function activateSurvey($surveyId)
public function deactivateSurvey($surveyId)
{
$date = date('YmdHis');
$oldSurveyTableName = \Yii::app()->db->tablePrefix."survey_{$surveyId}";
$newSurveyTableName = \Yii::app()->db->tablePrefix."old_survey_{$surveyId}_{$date}";
\Yii::app()->db->createCommand()->renameTable($oldSurveyTableName, $newSurveyTableName);
$oldSurveyTableName = Yii::app()->db->tablePrefix."survey_{$surveyId}";
$newSurveyTableName = Yii::app()->db->tablePrefix."old_survey_{$surveyId}_{$date}";
Yii::app()->db->createCommand()->renameTable($oldSurveyTableName, $newSurveyTableName);
$survey = \Survey::model()->findByPk($surveyId);
$survey->active = 'N';
$result = $survey->save();
Expand All @@ -175,12 +177,12 @@ public function deactivateSurvey($surveyId)
*/
public function connectToNewDatabase($databaseName)
{
$db = \Yii::app()->getDb();
$db = Yii::app()->getDb();

$config = require(\Yii::app()->getBasePath() . '/config/config.php');
$config = require(Yii::app()->getBasePath() . '/config/config.php');

// Check that we're using MySQL.
$conStr = \Yii::app()->db->connectionString;
$conStr = Yii::app()->db->connectionString;
$isMysql = substr($conStr, 0, 5) === 'mysql';
if (!$isMysql) {
$this->markTestSkipped('Only works on MySQL');
Expand Down Expand Up @@ -223,24 +225,24 @@ public function connectToNewDatabase($databaseName)
'dbname=' . $databaseName,
$config['components']['db']['connectionString']
);
\Yii::app()->setComponent('db', $newConfig['components']['db'], false);
Yii::app()->setComponent('db', $newConfig['components']['db'], false);
$db->setActive(true);
\Yii::app()->db->schema->getTables();
\Yii::app()->db->schema->refresh();
return \Yii::app()->getDb();
Yii::app()->db->schema->getTables();
Yii::app()->db->schema->refresh();
return Yii::app()->getDb();
}

/**
* @return void
*/
public function connectToOriginalDatabase()
{
\Yii::app()->db->setActive(false);
$config = require(\Yii::app()->getBasePath() . '/config/config.php');
\Yii::app()->setComponent('db', $config['components']['db'], false);
\Yii::app()->db->setActive(true);
\Yii::app()->db->schema->getTables();
\Yii::app()->db->schema->refresh();
Yii::app()->db->setActive(false);
$config = require(Yii::app()->getBasePath() . '/config/config.php');
Yii::app()->setComponent('db', $config['components']['db'], false);
Yii::app()->db->setActive(true);
Yii::app()->db->schema->getTables();
Yii::app()->db->schema->refresh();
}

/**
Expand All @@ -267,12 +269,12 @@ public function updateDbFromVersion($version, $connection = null)
$result = \db_upgrade_all($version);

// Check error messages.
$flashes = \Yii::app()->user->getFlashes();
$flashes = Yii::app()->user->getFlashes();
$this->assertEmpty($flashes, 'No flash error messages: ' . json_encode($flashes));
$this->assertTrue($result, 'Upgrade successful');

$oDB = Yii::app()->db;
$currentDbVersion = $oDB->createCommand()->select('stg_value')->from('{{settings_global}}')->where("stg_name=:stg_name", array('stg_name' => 'DBVersion'))->queryRow();
$db = Yii::app()->db;
$currentDbVersion = $db->createCommand()->select('stg_value')->from('{{settings_global}}')->where("stg_name=:stg_name", array('stg_name' => 'DBVersion'))->queryRow();
$this->assertEquals($version, $currentDbVersion, 'Version in db is same as updated to');

return $connection;
Expand Down Expand Up @@ -310,7 +312,7 @@ public function enablePreview()
public function teardownDatabase($databaseName, $connection = null)
{
if (is_null($connection)) {
$connection = \Yii::app()->getDb();
$connection = Yii::app()->getDb();
}
try {
$connection->createCommand('DROP DATABASE ' . $databaseName)->execute();
Expand Down Expand Up @@ -340,7 +342,7 @@ public function takeScreenshot($webDriver, $name)
$nameParts = explode('\\', $name);
$name = $nameParts[count($nameParts) - 1];

$tempFolder = \Yii::app()->getBasePath() .'/../tests/tmp';
$tempFolder = Yii::app()->getBasePath() .'/../tests/tmp';
$folder = $tempFolder.'/screenshots/';
$screenshot = $webDriver->takeScreenshot();
$filename = $folder . $name . '.png';
Expand Down Expand Up @@ -476,12 +478,12 @@ private static function executeSQLFile($sFileName, $connection)
if ($iLineLength && $sLine[0] != '#' && substr($sLine, 0, 2) != '--') {
if (substr($sLine, $iLineLength - 1, 1) == ';') {
$sCommand .= $sLine;
$sDatabasePrefix = \Yii::app()->db->tablePrefix;
$sDatabasePrefix = Yii::app()->db->tablePrefix;
$sCommand = str_replace('prefix_', $sDatabasePrefix, $sCommand); // Table prefixes

try {
$connection->createCommand($sCommand)->execute();
} catch (\Exception $e) {
} catch (Exception $e) {
$aMessages[] = "Executing: ".$sCommand." failed! Reason: ".$e;
}

Expand Down

0 comments on commit 66d7f24

Please sign in to comment.