Skip to content

Commit

Permalink
* Added test for db upgrade system
Browse files Browse the repository at this point in the history
* Added backwards compatibility with old log formatting
  • Loading branch information
JWardee committed Jun 12, 2022
1 parent 9e600cd commit 3153264
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Bootstrap
public function __construct()
{
GeneralHelper::setSettings();
LoggerFactory::Set();
LoggerFactory::set();
$this->registerCronTasks();
$this->screenOptions = ScreenOptions::getInstance();

Expand Down
92 changes: 28 additions & 64 deletions src/DatabaseUpgradeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,39 @@ class DatabaseUpgradeManager
{
static private $instance = false;

private $dbVersion;
private $upgradePaths = [
'2.0.0' => 'doV2Upgrade',
];

private function __construct()
{
$this->dbVersion = Settings::get('db_version');
}

public static function getInstance()
{
if (self::$instance == false) {
self::$instance = new DatabaseUpgradeManager();
}

return self::$instance;
}


public function isUpgradeRequired()
{
foreach ($this->upgradePaths as $version => $method) {
if ($this->dbVersion < $version) {
return true;
}
}

return false;
}

public function doUpgrade()
{
if (!$this->isUpgradeRequired()) {
return;
if (self::$instance == true) {
return self::$instance;
}

foreach ($this->upgradePaths as $version => $method) {
if ($this->dbVersion < $version) {
$this->{$method}();
self::$instance = new DatabaseUpgradeService([
'2.0.0' => function () {
global $wpdb;

// dbDelta creates a diff between the table schemas, and executes
// the necessary SQL so they match. In this case we add the column
// is_html and default it to false
$sql = "CREATE TABLE " . $wpdb->prefix . GeneralHelper::$tableName . " (
id int NOT NULL AUTO_INCREMENT,
time int NOT NULL,
email_to text DEFAULT NULL,
subject text DEFAULT NULL,
message text DEFAULT NULL,
backtrace_segment text NOT NULL,
status bool DEFAULT 1 NOT NULL,
error text DEFAULT NULL,
attachments text DEFAULT NULL,
additional_headers text DEFAULT NULL,
is_html bool DEFAULT 0,
PRIMARY KEY (id)
) " . $wpdb->get_charset_collate() . ";";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
], Settings::get('db_version'));

Settings::update(['db_version' => GeneralHelper::$pluginVersion]);
}

private function doV2Upgrade()
{
global $wpdb;

// dbDelta creates a diff between the table schemas, and executes
// the necessary SQL so they match. In this case we add the column
// is_html and default it to false
$sql = "CREATE TABLE " . $wpdb->prefix . GeneralHelper::$tableName . " (
id int NOT NULL AUTO_INCREMENT,
time int NOT NULL,
email_to text DEFAULT NULL,
subject text DEFAULT NULL,
message text DEFAULT NULL,
backtrace_segment text NOT NULL,
status bool DEFAULT 1 NOT NULL,
error text DEFAULT NULL,
attachments text DEFAULT NULL,
additional_headers text DEFAULT NULL,
is_html bool DEFAULT 0,
PRIMARY KEY (id)
) " . $wpdb->get_charset_collate() . ";";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
return self::$instance;
}
}
43 changes: 43 additions & 0 deletions src/DatabaseUpgradeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace WpMailCatcher;

use WpMailCatcher\Models\Settings;

class DatabaseUpgradeService
{
private $dbVersion;
private $upgradePaths = [];

public function __construct($upgradePaths, $currentDbVersion)
{
$this->upgradePaths = $upgradePaths;
$this->dbVersion = $currentDbVersion;
}

public function isUpgradeRequired()
{
foreach ($this->upgradePaths as $version => $function) {
if ($this->dbVersion < $version) {
return true;
}
}

return false;
}

public function doUpgrade()
{
if (!$this->isUpgradeRequired()) {
return;
}

foreach ($this->upgradePaths as $version => $function) {
if ($this->dbVersion < $version) {
$function();
}
}

Settings::update(['db_version' => GeneralHelper::$pluginVersion]);
}
}
7 changes: 7 additions & 0 deletions src/GeneralHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ static public function getPrefixedSlug($slugOrLabel)
{
return self::$namespacePrefix . self::labelToSlug($slugOrLabel);
}

static public function dd($value)
{
echo '<pre>';
print_r($value);
exit;
}
}


2 changes: 1 addition & 1 deletion src/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class LoggerFactory
{
static public function Set()
static public function set()
{
/**
* When more loggers are added, the logic
Expand Down
14 changes: 11 additions & 3 deletions src/Models/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ static public function get($args = [])

$defaultColumns = [
'id', 'time', 'email_to', 'subject', 'message',
'status', 'error', 'backtrace_segment', 'attachments', 'is_html',
'status', 'error', 'backtrace_segment', 'attachments',
'additional_headers'
];

if (Settings::get('db_version') >= '2.0.0') {
$defaultColumns[] = 'is_html';
}

$columnsToSelect = array_diff($defaultColumns, $args['column_blacklist']);

/**
Expand Down Expand Up @@ -168,8 +172,12 @@ static private function dbResultTransform($results, $args = [])
$result['time'] = $args['date_time_format'] == 'human' ? GeneralHelper::getHumanReadableTimeFromNow($result['timestamp']) : date($args['date_time_format'], $result['timestamp']);
}

if (isset($result['is_html']) && isset($result['additional_headers'])) {
$result['is_html'] = $result['is_html'] ? (bool)$result['is_html'] : GeneralHelper::doesArrayContainSubString($result['additional_headers'], GeneralHelper::$htmlEmailHeader);
// This will exist if the db_version is >= 2.0.0
if (isset($result['is_html']) && $result['is_html'] == true) {
$result['is_html'] = (bool)$result['is_html'];
// Otherwise resort to the original method
} else if (isset($result['additional_headers'])) {
$result['is_html'] = GeneralHelper::doesArrayContainSubString($result['additional_headers'], GeneralHelper::$htmlEmailHeader);
}

if (isset($result['message'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Views/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<div class="notice notice-warning">
<p>
<?php
printf(__('Click <a href="%s">here</a> to upgrade your database',
printf(__('Your WP Mail Catcher database needs upgrading. <strong>Click <a href="%s">here</a> to perform the upgrade.</strong>',
'WpMailCatcher'),
'?page=' . GeneralHelper::$adminPageSlug . '&action=upgrade-database'
);
Expand Down
31 changes: 31 additions & 0 deletions testing/tests/TestDbUpgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use WpMailCatcher\DatabaseUpgradeService;

class TestDbUpgrade extends WP_UnitTestCase
{
public function testMultipleUpgradesAreHandledCorrectly()
{
$wasV05UpgradeCalled = false;
$wasV2UpgradeCalled = false;
$wasV25UpgradeCalled = false;

$dbUpgradeManager = new DatabaseUpgradeService([
'0.5.0' => function () use (&$wasV05UpgradeCalled) {
$wasV05UpgradeCalled = true;
},
'2.0.0' => function () use (&$wasV2UpgradeCalled) {
$wasV2UpgradeCalled = true;
},
'2.5.0' => function () use (&$wasV25UpgradeCalled) {
$wasV25UpgradeCalled = true;
},
], '1.0.0');

$dbUpgradeManager->doUpgrade();

$this->assertFalse($wasV05UpgradeCalled);
$this->assertTrue($wasV2UpgradeCalled);
$this->assertTrue($wasV25UpgradeCalled);
}
}

0 comments on commit 3153264

Please sign in to comment.