Skip to content

Commit

Permalink
Dev: Better JSON tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Dec 1, 2017
1 parent 8c9b275 commit 29ffe4a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
8 changes: 5 additions & 3 deletions tests/TestHelper.php
Expand Up @@ -217,10 +217,12 @@ public function connectToOriginalDatabase()
* @param int $version
* @return CDbConnection
*/
public function updateDbFromVersion($version)
public function updateDbFromVersion($version, $connection = null)
{
$connection = $this->connectToNewDatabase('__test_update_helper_' . $version);
$this->assertNotEmpty($connection, 'Could connect to new database');
if (is_null($connection)) {
$connection = $this->connectToNewDatabase('__test_update_helper_' . $version);
$this->assertNotEmpty($connection, 'Could connect to new database');
}

// Get InstallerController.
$inst = new \InstallerController('foobar');
Expand Down
2 changes: 1 addition & 1 deletion tests/data/sql/create-mysql.315.sql
Expand Up @@ -707,7 +707,7 @@ INSERT INTO `prefix_surveymenu_entries` VALUES
(25,2,NULL,10,'emailtemplates','Email templates','Email templates','Edit the templates for invitation, reminder and registration emails','envelope-square','fontawesome','','admin/emailtemplates/sa/index/','','','','','surveylocale','read','{\"render\": { \"link\": {\"data\": {\"surveyid\": [\"survey\",\"sid\"]}}}}','','en-GB',1, NOW(),0,NOW(),0),
(26,2,NULL,11,'surveyLogicFile','Survey logic file','Survey logic file','Survey logic file','sitemap','fontawesome','','admin/expressions/sa/survey_logic_file/','','','','','surveycontent','read','{\"render\": { \"link\": {\"data\": {\"surveyid\": [\"survey\",\"sid\"]}}}}','','en-GB',1, NOW(),0,NOW(),0),
(27,2,NULL,12,'tokens','Token handling','Participant tokens','Define how tokens should be treated or generated','user','fontawesome','','','updatesurveylocalesettings','editLocalSettings_main_view','/admin/survey/subview/accordion/_tokens_panel','','surveylocale','read','{\"render\": { \"link\": {\"data\": {\"surveyid\": [\"survey\",\"sid\"]}}}}','_tabTokens','en-GB',1, NOW(),0,NOW(),0),
(28,2,NULL,13,'cpdb','Central participant database','Central participant database','Central participant database','users','fontawesome','','admin/participants/sa/displayParticipants','','','','','tokens','read','{render\: {\"link\"\: {}}','','en-GB',1, NOW(),0,NOW(),0),
(28,2,NULL,13,'cpdb','Central participant database','Central participant database','Central participant database','users','fontawesome','','admin/participants/sa/displayParticipants','','','','','tokens','read','{"render": {"link": {}}}','','en-GB',1, NOW(),0,NOW(),0),
(29,2,NULL,14,'responses','Responses','Responses','Responses','icon-browse','iconclass','','admin/responses/sa/browse/','','','','','responses','read','{\"render\"\: {\"isActive\"\: true}}','','en-GB',1, NOW(),0,NOW(),0),
(30,2,NULL,15,'statistics','Statistics','Statistics','Statistics','bar-chart','fontawesome','','admin/statistics/sa/index/','','','','','statistics','read','{\"render\"\: {\"isActive\"\: true}}','','en-GB',1, NOW(),0,NOW(),0),
(31,2,NULL,16,'reorder','Reorder questions/question groups','Reorder questions/question groups','Reorder questions/question groups','icon-organize','iconclass','','admin/survey/sa/organize/','','','','','surveycontent','update','{\"render\": {\"isActive\": false, \"link\": {\"data\": {\"surveyid\": [\"survey\",\"sid\"]}}}}','','en-GB',1, NOW(),0,NOW(),0);
Expand Down
22 changes: 13 additions & 9 deletions tests/helpers/CheckDatabaseJsonValuesTest.php
Expand Up @@ -35,6 +35,7 @@ public function testCreate()
$db = \Yii::app()->getDb();

$config = require(\Yii::app()->getBasePath() . '/config/config.php');
$version = require(\Yii::app()->getBasePath() . '/config/version.php');
$connection = self::$testHelper->connectToNewDatabase('__test_check_database_json');
$this->assertNotEmpty($connection, 'Could connect to new database');

Expand All @@ -47,6 +48,9 @@ public function testCreate()
print_r($result);
}

// Run upgrade.
$result = \db_upgrade_all($version['dbversionnumber']);

// Check JSON.
$this->checkMenuEntriesJson($inst->connection);
$this->checkTemplateConfigurationJson($inst->connection);
Expand Down Expand Up @@ -77,9 +81,6 @@ public function testUpdateFrom258()
*/
public function testUpdateFrom315()
{
// TODO: Need to fix updatedb_helper to fix broken JSON.
$this->markTestSkipped();

$connection = self::$testHelper->updateDbFromVersion(315);

// Check JSON.
Expand All @@ -98,12 +99,12 @@ public function testUpdateFrom315()
*/
protected function checkMenuEntriesJson(\CDbConnection $connection)
{
$data = $connection->createCommand('SELECT data from {{surveymenu_entries}}')->query();
foreach ($data as $row) {
$data = $connection->createCommand('SELECT menu_title, data FROM {{surveymenu_entries}}')->query();
foreach ($data as $field => $row) {
$jsonString = $row['data'];
if (!empty($jsonString)) {
$json = json_decode($jsonString);
$this->assertNotNull($json, print_r($row, true));
$this->assertNotNull($json, $row['menu_title'] . ' ' . print_r($jsonString, true));
} else {
// Nothing to check.
}
Expand All @@ -128,11 +129,14 @@ protected function checkTemplateConfigurationJson(\CDbConnection $connection)
FROM
{{template_configuration}}'
)->query();
foreach ($data as $row) {
foreach ($row as $field => $jsonString) {
foreach ($data as $field => $row) {
foreach ($row as $field2 => $jsonString) {
if (!empty($jsonString)) {
$json = json_decode($jsonString);
$this->assertNotNull($json, $field . ': ' . print_r($row, true));
$this->assertNotNull(
$json,
'The following is not valid JSON: ' . $field2 . ': ' . print_r($jsonString, true)
);
} else {
// Nothing to check.
}
Expand Down

0 comments on commit 29ffe4a

Please sign in to comment.