Wrap calls in DateTime and Emoji classes

This commit is contained in:
Amine Ben hammou
2019-01-23 16:43:12 +01:00
parent 06f1dad120
commit d452cfcae7
7 changed files with 81 additions and 47 deletions

View File

@@ -41,11 +41,14 @@ class Newsletter extends Model {
// automatic newsletters status
const STATUS_ACTIVE = 'active';
private $emoji;
function __construct() {
parent::__construct();
$this->addValidations('type', array(
'required' => __('Please specify a type.', 'mailpoet')
));
$this->emoji = new Emoji();
}
function queue() {
@@ -105,7 +108,7 @@ class Newsletter extends Model {
}
$this->set(
'body',
Emoji::encodeForUTF8Column(self::$_table, 'body', $this->body)
$this->emoji->encodeForUTF8Column(self::$_table, 'body', $this->body)
);
}

View File

@@ -24,12 +24,15 @@ class SendingQueue extends Model {
const PRIORITY_MEDIUM = 5;
const PRIORITY_LOW = 10;
private $emoji;
function __construct() {
parent::__construct();
$this->addValidations('newsletter_rendered_body', array(
'validRenderedNewsletterBody' => __('Rendered newsletter body is invalid!', 'mailpoet')
));
$this->emoji = new Emoji();
}
function task() {
@@ -107,7 +110,7 @@ class SendingQueue extends Model {
function encodeEmojisInBody($newsletter_rendered_body) {
if(is_array($newsletter_rendered_body)) {
foreach($newsletter_rendered_body as $key => $value) {
$newsletter_rendered_body[$key] = Emoji::encodeForUTF8Column(
$newsletter_rendered_body[$key] = $this->emoji->encodeForUTF8Column(
self::$_table,
'newsletter_rendered_body',
$value
@@ -120,7 +123,7 @@ class SendingQueue extends Model {
function decodeEmojisInBody($newsletter_rendered_body) {
if(is_array($newsletter_rendered_body)) {
foreach($newsletter_rendered_body as $key => $value) {
$newsletter_rendered_body[$key] = Emoji::decodeEntities($value);
$newsletter_rendered_body[$key] = $this->emoji->decodeEntities($value);
}
}
return $newsletter_rendered_body;

View File

@@ -12,18 +12,21 @@ class DateTime {
private $wp;
function __construct() {
$this->wp = new WPFunctions();
function __construct(WPFunctions $wp = null) {
if($wp === null) {
$wp = new WPFunctions();
}
$this->wp = $wp;
}
function getTimeFormat() {
$time_format = get_option('time_format');
$time_format = $this->wp->getOption('time_format');
if (empty($time_format)) $time_format = self::DEFAULT_TIME_FORMAT;
return $time_format;
}
function getDateFormat() {
$date_format = get_option('date_format');
$date_format = $this->wp->getOption('date_format');
if (empty($date_format)) $date_format = self::DEFAULT_DATE_FORMAT;
return $date_format;
}

View File

@@ -1,17 +1,27 @@
<?php
namespace MailPoet\WP;
use MailPoet\WP\Functions as WPFunctions;
class Emoji {
static function encodeForUTF8Column($table, $field, $value) {
private $wp;
function __construct(WPFunctions $wp = null) {
if($wp === null) {
$wp = new WPFunctions();
}
$this->wp = $wp;
}
function encodeForUTF8Column($table, $field, $value) {
global $wpdb;
$charset = $wpdb->get_col_charset($table, $field);
if($charset === 'utf8') {
$value = wp_encode_emoji($value);
$value = $this->wp->wpEncodeEmoji($value);
}
return $value;
}
static function decodeEntities($content) {
function decodeEntities($content) {
// Based on wp_staticize_emoji()
// Loosely match the Emoji Unicode range.

View File

@@ -34,6 +34,13 @@ class Functions {
function isUserLoggedIn() {
return call_user_func_array('is_user_logged_in', func_get_args());
function getOption() {
return call_user_func_array('get_option', func_get_args());
}
function wpEncodeEmoji() {
return call_user_func_array('wp_encode_emoji', func_get_args());
}
function getImageInfo($id) {

View File

@@ -1,62 +1,73 @@
<?php
namespace MailPoet\Test\WP;
use Helper\WordPress as WordPressHelper;
use MailPoet\WP\DateTime;
use Codeception\Stub;
use MailPoet\WP\DateTime as WPDateTime;
use MailPoet\WP\Functions as WPFunctions;
class DateTimeTest extends \MailPoetTest {
function _before() {
$this->date_time = new DateTime();
}
function testGetTimeFormat() {
WordPressHelper::interceptFunction('get_option', function($key) {
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
'getOption' => function($key) {
return 'H:i';
});
expect($this->date_time->getTimeFormat())->equals('H:i');
}
]));
expect($date_time->getTimeFormat())->equals('H:i');
WordPressHelper::interceptFunction('get_option', function($key) {
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
'getOption' => function($key) {
return '';
});
expect($this->date_time->getTimeFormat())->equals('H:i:s');
}
]));
expect($date_time->getTimeFormat())->equals('H:i:s');
}
function testGetDateFormat() {
WordPressHelper::interceptFunction('get_option', function($key) {
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
'getOption' => function($key) {
return 'm-d';
});
expect($this->date_time->getDateFormat())->equals('m-d');
}
]));
expect($date_time->getDateFormat())->equals('m-d');
WordPressHelper::interceptFunction('get_option', function($key) {
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
'getOption' => function($key) {
return '';
});
expect($this->date_time->getDateFormat())->equals('Y-m-d');
}
]));
expect($date_time->getDateFormat())->equals('Y-m-d');
}
function testGetCurrentDate() {
expect($this->date_time->getCurrentDate("Y-m"))->equals(date("Y-m"));
$date_time = new WPDateTime();
expect($date_time->getCurrentDate("Y-m"))->equals(date("Y-m"));
}
function testGetCurrentTime() {
expect($this->date_time->getCurrentTime("i:s"))->regExp('/\d\d:\d\d/');
$date_time = new WPDateTime();
expect($date_time->getCurrentTime("i:s"))->regExp('/\d\d:\d\d/');
}
function testFormatTime() {
$date_time = new WPDateTime();
$timestamp = 1234567;
$format = "H:i:s";
expect($this->date_time->formatTime($timestamp))->equals(date($this->date_time->getTimeFormat(), $timestamp));
expect($this->date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp));
expect($date_time->formatTime($timestamp))->equals(date($date_time->getTimeFormat(), $timestamp));
expect($date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp));
}
function testFormatDate() {
$date_time = new WPDateTime();
$timestamp = 1234567;
$format = "Y-m-d";
expect($this->date_time->formatDate($timestamp))->equals(date($this->date_time->getDateFormat(), $timestamp));
expect($this->date_time->formatDate($timestamp, $format))->equals(date($format, $timestamp));
expect($date_time->formatDate($timestamp))->equals(date($date_time->getDateFormat(), $timestamp));
expect($date_time->formatDate($timestamp, $format))->equals(date($format, $timestamp));
}
function testTimeInterval() {
$one_hour_interval = array_keys($this->date_time->getTimeInterval(
$date_time = new WPDateTime();
$one_hour_interval = array_keys($date_time->getTimeInterval(
'00:00:00',
'+1 hour',
$total_steps = 5
@@ -65,7 +76,7 @@ class DateTimeTest extends \MailPoetTest {
'00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00');
expect($one_hour_interval)->equals($one_hour_expected);
$quarter_hour_interval = array_keys($this->date_time->getTimeInterval(
$quarter_hour_interval = array_keys($date_time->getTimeInterval(
'00:00:00',
'+15 minute',
$total_steps = 5
@@ -75,7 +86,7 @@ class DateTimeTest extends \MailPoetTest {
);
expect($quarter_hour_interval)->equals($quarter_hour_expected);
$offset_start_time_interval = array_keys($this->date_time->getTimeInterval(
$offset_start_time_interval = array_keys($date_time->getTimeInterval(
'03:00:00',
'+1 hour',
$total_steps = 5
@@ -85,8 +96,4 @@ class DateTimeTest extends \MailPoetTest {
);
expect($offset_start_time_interval)->equals($offset_start_time_expected);
}
function _afterStep() {
WordPressHelper::releaseAllFunctions();
}
}

View File

@@ -10,13 +10,14 @@ class EmojiTest extends \MailPoetTest {
$this->data_decoded = "Emojis: 😃😵💪, not emojis: &#046;&#0142;";
$this->column = 'dummycol';
$this->emoji = new Emoji();
}
function testItCanEncodeForUTF8Column() {
$table = Env::$db_prefix . 'dummytable_utf8';
$this->createTable($table, 'utf8');
$result = Emoji::encodeForUTF8Column($table, $this->column, $this->data_decoded);
$result = $this->emoji->encodeForUTF8Column($table, $this->column, $this->data_decoded);
expect($result)->equals($this->data_encoded);
$this->dropTable($table);
@@ -26,14 +27,14 @@ class EmojiTest extends \MailPoetTest {
$table = Env::$db_prefix . 'dummytable_utf8mb4';
$this->createTable($table, 'utf8mb4');
$result = Emoji::encodeForUTF8Column($table, $this->column, $this->data_decoded);
$result = $this->emoji->encodeForUTF8Column($table, $this->column, $this->data_decoded);
expect($result)->equals($this->data_decoded);
$this->dropTable($table);
}
function testItCanDecodeEntities() {
$result = Emoji::decodeEntities($this->data_encoded);
$result = $this->emoji->decodeEntities($this->data_encoded);
expect($result)->equals($this->data_decoded);
}