From 88dc7f4199476adb0de748f1c7a58d3916493dfe Mon Sep 17 00:00:00 2001
From: Jonathan Labreuille
Date: Tue, 21 Jun 2016 16:45:08 +0200
Subject: [PATCH 1/3] removing DKIM and useless classes
---
assets/js/src/settings/tabs.js | 5 +-
composer.lock | 83 ++++++++++---------
lib/Config/Menu.php | 16 ----
lib/Util/DKIM.php | 28 -------
lib/Util/Security.php | 8 +-
lib/Util/Url.php | 3 -
lib/WP/Option.php | 17 ----
.../Util/{UtilCSSTest.php => CSSTest.php} | 2 +-
tests/unit/Util/SecurityTest.php | 32 +++++++
tests/unit/Util/UrlTest.php | 9 ++
tests/unit/Util/UtilDKIMTest.php | 17 ----
tests/unit/WP/DateTimeTest.php | 2 +-
views/settings/mta.html | 81 ------------------
13 files changed, 89 insertions(+), 214 deletions(-)
delete mode 100644 lib/Util/DKIM.php
delete mode 100644 lib/WP/Option.php
rename tests/unit/Util/{UtilCSSTest.php => CSSTest.php} (79%)
create mode 100644 tests/unit/Util/SecurityTest.php
create mode 100644 tests/unit/Util/UrlTest.php
delete mode 100644 tests/unit/Util/UtilDKIMTest.php
diff --git a/assets/js/src/settings/tabs.js b/assets/js/src/settings/tabs.js
index a851fb215d..79fade136a 100644
--- a/assets/js/src/settings/tabs.js
+++ b/assets/js/src/settings/tabs.js
@@ -34,16 +34,13 @@ define(
// show sending methods
jQuery('.mailpoet_sending_methods').fadeIn();
} else {
- // toggle SPF/DKIM (hidden if the sending method is MailPoet)
+ // toggle SPF (hidden if the sending method is MailPoet)
jQuery('#mailpoet_mta_spf')[
(group === 'mailpoet')
? 'hide'
: 'show'
]();
- // (HIDDEN FOR BETA)
- jQuery('#mailpoet_mta_dkim').hide();
-
// hide sending methods
jQuery('.mailpoet_sending_methods').hide();
diff --git a/composer.lock b/composer.lock
index 2c4b1bc658..3461e0d07e 100644
--- a/composer.lock
+++ b/composer.lock
@@ -1847,16 +1847,16 @@
},
{
"name": "phpunit/phpunit",
- "version": "5.4.4",
+ "version": "5.4.6",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
- "reference": "02d5b64aa0837a038a5a4faeeefa5ef44bdcb928"
+ "reference": "2f1fc94b77ea6418bd6a06c64a1dac0645fbce59"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/02d5b64aa0837a038a5a4faeeefa5ef44bdcb928",
- "reference": "02d5b64aa0837a038a5a4faeeefa5ef44bdcb928",
+ "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2f1fc94b77ea6418bd6a06c64a1dac0645fbce59",
+ "reference": "2f1fc94b77ea6418bd6a06c64a1dac0645fbce59",
"shasum": ""
},
"require": {
@@ -1875,7 +1875,7 @@
"phpunit/phpunit-mock-objects": "^3.2",
"sebastian/comparator": "~1.1",
"sebastian/diff": "~1.2",
- "sebastian/environment": "~1.3",
+ "sebastian/environment": "^1.3 || ^2.0",
"sebastian/exporter": "~1.2",
"sebastian/global-state": "~1.0",
"sebastian/object-enumerator": "~1.0",
@@ -1921,7 +1921,7 @@
"testing",
"xunit"
],
- "time": "2016-06-09 09:09:27"
+ "time": "2016-06-16 06:01:15"
},
{
"name": "phpunit/phpunit-mock-objects",
@@ -2295,16 +2295,16 @@
},
{
"name": "sebastian/exporter",
- "version": "1.2.1",
+ "version": "1.2.2",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/exporter.git",
- "reference": "7ae5513327cb536431847bcc0c10edba2701064e"
+ "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/7ae5513327cb536431847bcc0c10edba2701064e",
- "reference": "7ae5513327cb536431847bcc0c10edba2701064e",
+ "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/42c4c2eec485ee3e159ec9884f95b431287edde4",
+ "reference": "42c4c2eec485ee3e159ec9884f95b431287edde4",
"shasum": ""
},
"require": {
@@ -2312,12 +2312,13 @@
"sebastian/recursion-context": "~1.0"
},
"require-dev": {
+ "ext-mbstring": "*",
"phpunit/phpunit": "~4.4"
},
"type": "library",
"extra": {
"branch-alias": {
- "dev-master": "1.2.x-dev"
+ "dev-master": "1.3.x-dev"
}
},
"autoload": {
@@ -2357,7 +2358,7 @@
"export",
"exporter"
],
- "time": "2015-06-21 07:55:53"
+ "time": "2016-06-17 09:04:28"
},
{
"name": "sebastian/global-state",
@@ -2596,7 +2597,7 @@
},
{
"name": "symfony/browser-kit",
- "version": "v3.1.0",
+ "version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/browser-kit.git",
@@ -2706,16 +2707,16 @@
},
{
"name": "symfony/console",
- "version": "v3.1.0",
+ "version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/console.git",
- "reference": "f62db5b8afec27073a4609b8c84b1f9936652259"
+ "reference": "64a4d43b045f07055bb197650159769604cb2a92"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/symfony/console/zipball/f62db5b8afec27073a4609b8c84b1f9936652259",
- "reference": "f62db5b8afec27073a4609b8c84b1f9936652259",
+ "url": "https://api.github.com/repos/symfony/console/zipball/64a4d43b045f07055bb197650159769604cb2a92",
+ "reference": "64a4d43b045f07055bb197650159769604cb2a92",
"shasum": ""
},
"require": {
@@ -2762,20 +2763,20 @@
],
"description": "Symfony Console Component",
"homepage": "https://symfony.com",
- "time": "2016-05-30 06:58:39"
+ "time": "2016-06-14 11:18:07"
},
{
"name": "symfony/css-selector",
- "version": "v3.1.0",
+ "version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/css-selector.git",
- "reference": "e17f386efef7258ac671c24e727673abd086b0cf"
+ "reference": "c526d7b3cb4fe1673c6a34e13be2ff63f519df99"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/symfony/css-selector/zipball/e17f386efef7258ac671c24e727673abd086b0cf",
- "reference": "e17f386efef7258ac671c24e727673abd086b0cf",
+ "url": "https://api.github.com/repos/symfony/css-selector/zipball/c526d7b3cb4fe1673c6a34e13be2ff63f519df99",
+ "reference": "c526d7b3cb4fe1673c6a34e13be2ff63f519df99",
"shasum": ""
},
"require": {
@@ -2815,11 +2816,11 @@
],
"description": "Symfony CssSelector Component",
"homepage": "https://symfony.com",
- "time": "2016-03-04 07:56:56"
+ "time": "2016-06-06 11:42:41"
},
{
"name": "symfony/dom-crawler",
- "version": "v3.1.0",
+ "version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/dom-crawler.git",
@@ -2984,7 +2985,7 @@
},
{
"name": "symfony/finder",
- "version": "v3.1.0",
+ "version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/finder.git",
@@ -3294,16 +3295,16 @@
},
{
"name": "symfony/process",
- "version": "v3.1.0",
+ "version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/process.git",
- "reference": "1574f3451b40fa9bbae518ef71d19a56f409cac0"
+ "reference": "6350e63ed9c232da50e00f00a7e0330f066387a2"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/symfony/process/zipball/1574f3451b40fa9bbae518ef71d19a56f409cac0",
- "reference": "1574f3451b40fa9bbae518ef71d19a56f409cac0",
+ "url": "https://api.github.com/repos/symfony/process/zipball/6350e63ed9c232da50e00f00a7e0330f066387a2",
+ "reference": "6350e63ed9c232da50e00f00a7e0330f066387a2",
"shasum": ""
},
"require": {
@@ -3339,7 +3340,7 @@
],
"description": "Symfony Process Component",
"homepage": "https://symfony.com",
- "time": "2016-04-12 19:11:33"
+ "time": "2016-06-06 11:42:41"
},
{
"name": "symfony/property-access",
@@ -3559,16 +3560,16 @@
},
{
"name": "symfony/yaml",
- "version": "v3.1.0",
+ "version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
- "reference": "eca51b7b65eb9be6af88ad7cc91685f1556f5c9a"
+ "reference": "c5a7e7fc273c758b92b85dcb9c46149ccda89623"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/symfony/yaml/zipball/eca51b7b65eb9be6af88ad7cc91685f1556f5c9a",
- "reference": "eca51b7b65eb9be6af88ad7cc91685f1556f5c9a",
+ "url": "https://api.github.com/repos/symfony/yaml/zipball/c5a7e7fc273c758b92b85dcb9c46149ccda89623",
+ "reference": "c5a7e7fc273c758b92b85dcb9c46149ccda89623",
"shasum": ""
},
"require": {
@@ -3604,7 +3605,7 @@
],
"description": "Symfony Yaml Component",
"homepage": "https://symfony.com",
- "time": "2016-05-26 21:46:24"
+ "time": "2016-06-14 11:18:07"
},
{
"name": "twig/extensions",
@@ -3709,16 +3710,16 @@
},
{
"name": "vlucas/phpdotenv",
- "version": "v2.2.1",
+ "version": "v2.3.0",
"source": {
"type": "git",
"url": "https://github.com/vlucas/phpdotenv.git",
- "reference": "63f37b9395e8041cd4313129c08ece896d06ca8e"
+ "reference": "9ca5644c536654e9509b9d257f53c58630eb2a6a"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/63f37b9395e8041cd4313129c08ece896d06ca8e",
- "reference": "63f37b9395e8041cd4313129c08ece896d06ca8e",
+ "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/9ca5644c536654e9509b9d257f53c58630eb2a6a",
+ "reference": "9ca5644c536654e9509b9d257f53c58630eb2a6a",
"shasum": ""
},
"require": {
@@ -3730,7 +3731,7 @@
"type": "library",
"extra": {
"branch-alias": {
- "dev-master": "2.2-dev"
+ "dev-master": "2.3-dev"
}
},
"autoload": {
@@ -3755,7 +3756,7 @@
"env",
"environment"
],
- "time": "2016-04-15 10:48:49"
+ "time": "2016-06-14 14:14:52"
},
{
"name": "webmozart/assert",
diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php
index 06b545704d..9f437970d8 100644
--- a/lib/Config/Menu.php
+++ b/lib/Config/Menu.php
@@ -12,7 +12,6 @@ use MailPoet\Settings\Charsets;
use MailPoet\Settings\Hosts;
use MailPoet\Settings\Pages;
use MailPoet\Subscribers\ImportExport\ImportExportFactory;
-use MailPoet\Util\DKIM;
use MailPoet\Util\Permissions;
use MailPoet\Listing;
use MailPoet\WP\DateTime;
@@ -294,21 +293,6 @@ class Menu {
$settings = Setting::getAll();
$flags = $this->_getFlags();
- // dkim: check if public/private keys have been generated
- if(
- empty($settings['dkim'])
- or empty($settings['dkim']['public_key'])
- or empty($settings['dkim']['private_key'])
- ) {
- // generate public/private keys
- $keys = DKIM::generateKeys();
- $settings['dkim'] = array(
- 'public_key' => $keys['public'],
- 'private_key' => $keys['private'],
- 'domain' => preg_replace('/^www\./', '', $_SERVER['SERVER_NAME'])
- );
- }
-
$data = array(
'settings' => $settings,
'segments' => Segment::getPublic()->findArray(),
diff --git a/lib/Util/DKIM.php b/lib/Util/DKIM.php
deleted file mode 100644
index 75c5e63ca0..0000000000
--- a/lib/Util/DKIM.php
+++ /dev/null
@@ -1,28 +0,0 @@
-createKey();
-
- return array(
- 'public' => self::trimKey($rsa_keys['publickey']),
- 'private' => self::trimKey($rsa_keys['privatekey'])
- );
- } catch(\Exception $e) {
- return false;
- }
- }
-
- private static function trimKey($key) {
- $lines = explode("\n", trim($key));
- // remove first line
- array_shift($lines);
- // remove last line
- array_pop($lines);
- return join('', $lines);
- }
-}
\ No newline at end of file
diff --git a/lib/Util/Security.php b/lib/Util/Security.php
index 72ea638fd7..177eb04729 100644
--- a/lib/Util/Security.php
+++ b/lib/Util/Security.php
@@ -11,11 +11,9 @@ class Security {
static function generateRandomString($length = 5) {
// non-cryptographically strong random generator
return substr(
- md5(
- uniqid(
- mt_rand(), true)
- ),
+ md5(uniqid(mt_rand(), true)),
0,
- (!is_int($length) || $length <= 5 || $length >= 32) ? 5 : $length);
+ min(max(5, (int)$length), 32)
+ );
}
}
\ No newline at end of file
diff --git a/lib/Util/Url.php b/lib/Util/Url.php
index 5d7d34bbd7..4036383889 100644
--- a/lib/Util/Url.php
+++ b/lib/Util/Url.php
@@ -2,9 +2,6 @@
namespace MailPoet\Util;
class Url {
- function __construct() {
- }
-
static function getCurrentUrl() {
return home_url(add_query_arg(null, null));
}
diff --git a/lib/WP/Option.php b/lib/WP/Option.php
deleted file mode 100644
index a77d9c8227..0000000000
--- a/lib/WP/Option.php
+++ /dev/null
@@ -1,17 +0,0 @@
-prefix = 'mailpoet_';
- }
-
- function get($name) {
- return get_option($this->prefix . $name);
- }
-
- function set($name, $value) {
- return update_option($this->prefix .$name, $value);
- }
-}
diff --git a/tests/unit/Util/UtilCSSTest.php b/tests/unit/Util/CSSTest.php
similarity index 79%
rename from tests/unit/Util/UtilCSSTest.php
rename to tests/unit/Util/CSSTest.php
index c2d0d662a3..8c3be85172 100644
--- a/tests/unit/Util/UtilCSSTest.php
+++ b/tests/unit/Util/CSSTest.php
@@ -1,6 +1,6 @@
css = new \MailPoet\Util\CSS();
}
diff --git a/tests/unit/Util/SecurityTest.php b/tests/unit/Util/SecurityTest.php
new file mode 100644
index 0000000000..da7ba6fd97
--- /dev/null
+++ b/tests/unit/Util/SecurityTest.php
@@ -0,0 +1,32 @@
+equals(10);
+ // expect only alphanumerical characters
+ expect(ctype_alnum($wp_nonce))->true();
+ }
+
+ function testItCanGenerateARandomString() {
+ // it has a default length of 5
+ $hash = Security::generateRandomString();
+ expect(strlen($hash))->equals(5);
+
+ // it has a min length of 5
+ $short_hash = Security::generateRandomString(1);
+ expect(strlen($short_hash))->equals(5);
+
+ // it has a max length of 32
+ $long_hash = Security::generateRandomString(64);
+ expect(strlen($long_hash))->equals(32);
+
+ // expect only alphanumerical characters
+ expect(ctype_alnum($hash))->true();
+ expect(ctype_alnum($short_hash))->true();
+ expect(ctype_alnum($long_hash))->true();
+ }
+}
\ No newline at end of file
diff --git a/tests/unit/Util/UrlTest.php b/tests/unit/Util/UrlTest.php
new file mode 100644
index 0000000000..8333707215
--- /dev/null
+++ b/tests/unit/Util/UrlTest.php
@@ -0,0 +1,9 @@
+startsWith('http');
+ }
+}
\ No newline at end of file
diff --git a/tests/unit/Util/UtilDKIMTest.php b/tests/unit/Util/UtilDKIMTest.php
deleted file mode 100644
index 2e4dd7fb84..0000000000
--- a/tests/unit/Util/UtilDKIMTest.php
+++ /dev/null
@@ -1,17 +0,0 @@
-notEmpty();
- expect($keys['private'])->notEmpty();
-
- expect($keys['public'])->notContains($public_header);
- expect($keys['private'])->notContains($private_header);
- }
-}
diff --git a/tests/unit/WP/DateTimeTest.php b/tests/unit/WP/DateTimeTest.php
index 2620de3735..41878811cd 100644
--- a/tests/unit/WP/DateTimeTest.php
+++ b/tests/unit/WP/DateTimeTest.php
@@ -77,6 +77,6 @@ class DateTimeTest extends MailPoetTest {
}
function _afterStep() {
- WordPress::releaseAllFunctions();
+ WordPressHelper::releaseAllFunctions();
}
}
diff --git a/views/settings/mta.html b/views/settings/mta.html
index 365ad74daa..8a8daf1b4c 100644
--- a/views/settings/mta.html
+++ b/views/settings/mta.html
@@ -38,23 +38,6 @@
name="mta[frequency][interval]"
value="<%= settings.mta.frequency.interval %>"
/>
-
-
-
-
-
-
-
-
-
- |
-
-
- checked="checked"<% endif %>
- />
-
-
- |
-
From 264b7e180b14b11cfcaaf4daa1f855212095e12f Mon Sep 17 00:00:00 2001
From: Jonathan Labreuille
Date: Tue, 21 Jun 2016 20:39:11 +0200
Subject: [PATCH 2/3] listing handler and bulk actions tests completed
---
lib/Listing/BulkAction.php | 2 +-
lib/Listing/Handler.php | 10 --
tests/unit/Models/SubscriberTest.php | 4 +-
tests/unit/Router/NewslettersTest.php | 118 ++++++++++++++-
tests/unit/Router/SubscribersTest.php | 209 ++++++++++++++++++++++++--
tests/unit/Util/UrlTest.php | 2 +-
6 files changed, 314 insertions(+), 31 deletions(-)
diff --git a/lib/Listing/BulkAction.php b/lib/Listing/BulkAction.php
index 09e67ec832..5def17d77b 100644
--- a/lib/Listing/BulkAction.php
+++ b/lib/Listing/BulkAction.php
@@ -25,7 +25,7 @@ class BulkAction {
$bulk_action_method = 'bulk'.ucfirst($this->action);
if(!method_exists($this->model_class, $bulk_action_method)) {
- throw new Exception(
+ throw new \Exception(
$this->model_class. ' has not method "'.$bulk_action_method.'"'
);
return false;
diff --git a/lib/Listing/Handler.php b/lib/Listing/Handler.php
index 61e19dc65a..5fc996ee54 100644
--- a/lib/Listing/Handler.php
+++ b/lib/Listing/Handler.php
@@ -87,16 +87,6 @@ class Handler {
}
}
- function getSelectionIds() {
- $models = $this->getSelection()
- ->select('id')
- ->findArray();
-
- return array_map(function($model) {
- return (int)$model['id'];
- }, $models);
- }
-
function get() {
// get groups
$groups = array();
diff --git a/tests/unit/Models/SubscriberTest.php b/tests/unit/Models/SubscriberTest.php
index 6af1205039..c9434e51e7 100644
--- a/tests/unit/Models/SubscriberTest.php
+++ b/tests/unit/Models/SubscriberTest.php
@@ -313,7 +313,7 @@ class SubscriberTest extends MailPoetTest {
expect($subscribed_subscribers_in_segment)->count(2);
}
- function testItCannotTrashAWPUser() {
+ function testItCannotTrashWpUser() {
$wp_subscriber = Subscriber::createOrUpdate(array(
'email' => 'some.wp.user@mailpoet.com',
'wp_user_id' => 1
@@ -325,7 +325,7 @@ class SubscriberTest extends MailPoetTest {
expect($subscriber->deleted_at)->equals(null);
}
- function testItCannotDeleteAWPUser() {
+ function testItCannotDeleteWpUser() {
$wp_subscriber = Subscriber::createOrUpdate(array(
'email' => 'some.wp.user@mailpoet.com',
'wp_user_id' => 1
diff --git a/tests/unit/Router/NewslettersTest.php b/tests/unit/Router/NewslettersTest.php
index 3d0e803bd2..d94d478125 100644
--- a/tests/unit/Router/NewslettersTest.php
+++ b/tests/unit/Router/NewslettersTest.php
@@ -9,12 +9,12 @@ class NewslettersTest extends MailPoetTest {
function _before() {
$this->newsletter = Newsletter::createOrUpdate(array(
'subject' => 'My Standard Newsletter',
- 'type' => 'standard'
+ 'type' => Newsletter::TYPE_STANDARD
));
$this->post_notification = Newsletter::createOrUpdate(array(
'subject' => 'My Post Notification',
- 'type' => 'notification'
+ 'type' => Newsletter::TYPE_NOTIFICATION
));
}
@@ -34,7 +34,7 @@ class NewslettersTest extends MailPoetTest {
function testItCanSaveANewNewsletter() {
$valid_data = array(
'subject' => 'My First Newsletter',
- 'type' => 'standard'
+ 'type' => Newsletter::TYPE_STANDARD
);
$router = new Newsletters();
@@ -98,26 +98,26 @@ class NewslettersTest extends MailPoetTest {
$router = new Newsletters();
$response = $router->duplicate($this->newsletter->id());
expect($response['subject'])->equals('Copy of My Standard Newsletter');
- expect($response['type'])->equals('standard');
+ expect($response['type'])->equals(Newsletter::TYPE_STANDARD);
expect($response['body'])->equals($this->newsletter->body);
$response = $router->duplicate($this->post_notification->id());
expect($response['subject'])->equals('Copy of My Post Notification');
- expect($response['type'])->equals('notification');
+ expect($response['type'])->equals(Newsletter::TYPE_NOTIFICATION);
expect($response['body'])->equals($this->post_notification->body);
}
function testItCanCreateANewsletter() {
$data = array(
'subject' => 'My New Newsletter',
- 'type' => 'standard'
+ 'type' => Newsletter::TYPE_STANDARD
);
$router = new Newsletters();
$response = $router->create($data);
expect($response['result'])->true();
expect($response['newsletter']['id'] > 0)->true();
expect($response['newsletter']['subject'])->equals('My New Newsletter');
- expect($response['newsletter']['type'])->equals('standard');
+ expect($response['newsletter']['type'])->equals(Newsletter::TYPE_STANDARD);
expect($response['newsletter']['body'])->equals(array());
expect($response)->hasntKey('errors');
@@ -176,6 +176,110 @@ class NewslettersTest extends MailPoetTest {
->equals($segment_2->id);
}
+ function testItCanFilterListing() {
+ // create 2 segments
+ $segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1'));
+ $segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2'));
+
+ // link standard newsletter to the 2 segments
+ $newsletter_segment = NewsletterSegment::create();
+ $newsletter_segment->hydrate(array(
+ 'newsletter_id' => $this->newsletter->id(),
+ 'segment_id' => $segment_1->id()
+ ));
+ $newsletter_segment->save();
+
+ $newsletter_segment = NewsletterSegment::create();
+ $newsletter_segment->hydrate(array(
+ 'newsletter_id' => $this->newsletter->id(),
+ 'segment_id' => $segment_2->id()
+ ));
+ $newsletter_segment->save();
+
+ // link post notification to the 2nd segment
+ $newsletter_segment = NewsletterSegment::create();
+ $newsletter_segment->hydrate(array(
+ 'newsletter_id' => $this->post_notification->id(),
+ 'segment_id' => $segment_2->id()
+ ));
+ $newsletter_segment->save();
+
+ $router = new Newsletters();
+
+ // filter by 1st segment
+ $response = $router->listing(array(
+ 'filter' => array(
+ 'segment' => $segment_1->id
+ )
+ ));
+
+ // we should only get the standard newsletter
+ expect($response['count'])->equals(1);
+ expect($response['items'][0]['subject'])->equals($this->newsletter->subject);
+
+ // filter by 2nd segment
+ $response = $router->listing(array(
+ 'filter' => array(
+ 'segment' => $segment_2->id
+ )
+ ));
+
+ // we should have the 2 newsletters
+ expect($response['count'])->equals(2);
+ }
+
+ function testItCanLimitListing() {
+ $router = new Newsletters();
+ // get 1st page (limit items per page to 1)
+ $response = $router->listing(array(
+ 'limit' => 1,
+ 'sort_by' => 'subject',
+ 'sort_order' => 'asc'
+ ));
+
+ expect($response['count'])->equals(2);
+ expect($response['items'])->count(1);
+ expect($response['items'][0]['subject'])->equals(
+ $this->post_notification->subject
+ );
+
+ // get 1st page (limit items per page to 1)
+ $response = $router->listing(array(
+ 'limit' => 1,
+ 'offset' => 1,
+ 'sort_by' => 'subject',
+ 'sort_order' => 'asc'
+ ));
+
+ expect($response['count'])->equals(2);
+ expect($response['items'])->count(1);
+ expect($response['items'][0]['subject'])->equals(
+ $this->newsletter->subject
+ );
+ }
+
+ function testItCanBulkDeleteSelectionOfNewsletters() {
+ $newsletter = Newsletter::createOrUpdate(array(
+ 'subject' => 'To be deleted',
+ 'type' => Newsletter::TYPE_STANDARD
+ ));
+
+ $selection_ids = array(
+ $newsletter->id,
+ $this->newsletter->id
+ );
+
+ $router = new Newsletters();
+ $response = $router->bulkAction(array(
+ 'listing' => array(
+ 'selection' => $selection_ids
+ ),
+ 'action' => 'delete'
+ ));
+
+ expect($response)->equals(count($selection_ids));
+ }
+
function testItCanBulkDeleteNewsletters() {
expect(Newsletter::count())->equals(2);
diff --git a/tests/unit/Router/SubscribersTest.php b/tests/unit/Router/SubscribersTest.php
index 30c69d5fb7..6c861fd3fd 100644
--- a/tests/unit/Router/SubscribersTest.php
+++ b/tests/unit/Router/SubscribersTest.php
@@ -10,14 +10,16 @@ class SubscribersTest extends MailPoetTest {
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2'));
$this->subscriber_1 = Subscriber::createOrUpdate(array(
- 'email' => 'john.doe@mailpoet.com',
+ 'email' => 'john@mailpoet.com',
'first_name' => 'John',
- 'last_name' => 'Doe'
+ 'last_name' => 'Doe',
+ 'status' => Subscriber::STATUS_UNCONFIRMED
));
$this->subscriber_2 = Subscriber::createOrUpdate(array(
- 'email' => 'jane.doe@mailpoet.com',
+ 'email' => 'jane@mailpoet.com',
'first_name' => 'Jane',
'last_name' => 'Doe',
+ 'status' => Subscriber::STATUS_SUBSCRIBED,
'segments' => array(
$this->segment_1->id(),
$this->segment_2->id()
@@ -43,8 +45,8 @@ class SubscribersTest extends MailPoetTest {
function testItCanSaveANewSubscriber() {
$valid_data = array(
- 'email' => 'john.doe@mailpoet.com',
- 'first_name' => 'John',
+ 'email' => 'raul.doe@mailpoet.com',
+ 'first_name' => 'Raul',
'last_name' => 'Doe',
'segments' => array(
$this->segment_1->id(),
@@ -57,7 +59,7 @@ class SubscribersTest extends MailPoetTest {
expect($response['result'])->true();
expect($response)->hasntKey('errors');
- $subscriber = Subscriber::where('email', 'john.doe@mailpoet.com')->findOne();
+ $subscriber = Subscriber::where('email', 'raul.doe@mailpoet.com')->findOne();
$subscriber_segments = $subscriber->segments()->findMany();
expect($subscriber_segments)->count(2);
expect($subscriber_segments[0]->name)->equals($this->segment_1->name);
@@ -68,9 +70,7 @@ class SubscribersTest extends MailPoetTest {
expect($response['errors'][0])->equals('Please enter your email address.');
$invalid_data = array(
- 'email' => 'john.doe@invalid',
- 'first_name' => 'John',
- 'last_name' => 'Doe'
+ 'email' => 'john.doe@invalid'
);
$response = $router->save($invalid_data);
@@ -89,7 +89,7 @@ class SubscribersTest extends MailPoetTest {
expect($response['result'])->true();
$updated_subscriber = Subscriber::findOne($this->subscriber_2->id());
- expect($updated_subscriber->email)->equals('jane.doe@mailpoet.com');
+ expect($updated_subscriber->email)->equals($this->subscriber_2->email);
expect($updated_subscriber->first_name)->equals('Super Jane');
}
@@ -122,6 +122,180 @@ class SubscribersTest extends MailPoetTest {
expect(Subscriber::findOne($this->subscriber_1->id()))->false();
}
+ function testItCanFilterListing() {
+ $router = new Subscribers();
+
+ // filter by non existing segment
+ $response = $router->listing(array(
+ 'filter' => array(
+ 'segment' => '### invalid_segment_id ###'
+ )
+ ));
+
+ // it should return all subscribers
+ expect($response['count'])->equals(2);
+
+ // filter by 1st segment
+ $response = $router->listing(array(
+ 'filter' => array(
+ 'segment' => $this->segment_1->id
+ )
+ ));
+
+ expect($response['count'])->equals(1);
+ expect($response['items'][0]['email'])->equals($this->subscriber_2->email);
+
+ // filter by 2nd segment
+ $response = $router->listing(array(
+ 'filter' => array(
+ 'segment' => $this->segment_2->id
+ )
+ ));
+
+ expect($response['count'])->equals(1);
+ expect($response['items'][0]['email'])->equals($this->subscriber_2->email);
+ }
+
+ function testItCanSearchListing() {
+ $new_subscriber = Subscriber::createOrUpdate(array(
+ 'email' => 'search.me@find.me',
+ 'first_name' => 'Billy Bob',
+ 'last_name' => 'Thornton'
+ ));
+
+ $router = new Subscribers();
+
+ // empty search returns everything
+ $response = $router->listing(array(
+ 'search' => ''
+ ));
+ expect($response['count'])->equals(3);
+
+ // search by email
+ $response = $router->listing(array(
+ 'search' => '.me'
+ ));
+ expect($response['count'])->equals(1);
+ expect($response['items'][0]['email'])->equals($new_subscriber->email);
+
+ // search by last name
+ $response = $router->listing(array(
+ 'search' => 'doe'
+ ));
+ expect($response['count'])->equals(2);
+ expect($response['items'][0]['email'])->equals($this->subscriber_1->email);
+ expect($response['items'][1]['email'])->equals($this->subscriber_2->email);
+
+ // search by first name
+ $response = $router->listing(array(
+ 'search' => 'billy'
+ ));
+ expect($response['count'])->equals(1);
+ expect($response['items'][0]['email'])->equals($new_subscriber->email);
+ }
+
+ function testItCanGroupListing() {
+ $router = new Subscribers();
+
+ $subscribed_group = $router->listing(array(
+ 'group' => Subscriber::STATUS_SUBSCRIBED
+ ));
+ expect($subscribed_group['count'])->equals(1);
+ expect($subscribed_group['items'][0]['email'])->equals(
+ $this->subscriber_2->email
+ );
+
+ $unsubscribed_group = $router->listing(array(
+ 'group' => Subscriber::STATUS_UNSUBSCRIBED
+ ));
+ expect($unsubscribed_group['count'])->equals(0);
+
+ $unconfirmed_group = $router->listing(array(
+ 'group' => Subscriber::STATUS_UNCONFIRMED
+ ));
+ expect($unconfirmed_group['count'])->equals(1);
+ expect($unconfirmed_group['items'][0]['email'])->equals(
+ $this->subscriber_1->email
+ );
+
+ $trashed_group = $router->listing(array(
+ 'group' => 'trash'
+ ));
+ expect($trashed_group['count'])->equals(0);
+
+ // trash 1st subscriber
+ $this->subscriber_1->trash();
+
+ $trashed_group = $router->listing(array(
+ 'group' => 'trash'
+ ));
+ expect($trashed_group['count'])->equals(1);
+ expect($trashed_group['items'][0]['email'])->equals(
+ $this->subscriber_1->email
+ );
+ }
+
+ function testItCanSortAndLimitListing() {
+ $router = new Subscribers();
+ // get 1st page (limit items per page to 1)
+ $response = $router->listing(array(
+ 'limit' => 1,
+ 'sort_by' => 'first_name',
+ 'sort_order' => 'asc'
+ ));
+
+ expect($response['count'])->equals(2);
+ expect($response['items'])->count(1);
+ expect($response['items'][0]['email'])->equals(
+ $this->subscriber_2->email
+ );
+
+ // get 1st page (limit items per page to 1)
+ $response = $router->listing(array(
+ 'limit' => 1,
+ 'offset' => 1,
+ 'sort_by' => 'first_name',
+ 'sort_order' => 'asc'
+ ));
+
+ expect($response['count'])->equals(2);
+ expect($response['items'])->count(1);
+ expect($response['items'][0]['email'])->equals(
+ $this->subscriber_1->email
+ );
+ }
+
+ function testItCanBulkDeleteSelectionOfSubscribers() {
+ $deletable_subscriber = Subscriber::createOrUpdate(array(
+ 'email' => 'to.be.removed@mailpoet.com'
+ ));
+
+ $selection_ids = array(
+ $this->subscriber_1->id,
+ $deletable_subscriber->id
+ );
+
+ $router = new Subscribers();
+ $response = $router->bulkAction(array(
+ 'listing' => array(
+ 'selection' => $selection_ids
+ ),
+ 'action' => 'delete'
+ ));
+
+ expect($response)->equals(count($selection_ids));
+
+ $is_subscriber_1_deleted = (
+ Subscriber::findOne($this->subscriber_1->id) === false
+ );
+ $is_deletable_subscriber_deleted = (
+ Subscriber::findOne($deletable_subscriber->id) === false
+ );
+
+ expect($is_subscriber_1_deleted)->true();
+ expect($is_deletable_subscriber_deleted)->true();
+ }
+
function testItCanBulkDeleteSubscribers() {
expect(Subscriber::count())->equals(2);
@@ -144,6 +318,21 @@ class SubscribersTest extends MailPoetTest {
expect($response)->equals(0);
}
+ function testItCannotRunAnInvalidBulkAction() {
+ try {
+ $router = new Subscribers();
+ $response = $router->bulkAction(array(
+ 'action' => 'invalidAction',
+ 'listing' => array()
+ ));
+ $this->fail('Bulk Action class did not throw an exception');
+ } catch(Exception $e) {
+ expect($e->getMessage())->equals(
+ '\MailPoet\Models\Subscriber has not method "bulkInvalidAction"'
+ );
+ }
+ }
+
function _after() {
Segment::deleteMany();
Subscriber::deleteMany();
diff --git a/tests/unit/Util/UrlTest.php b/tests/unit/Util/UrlTest.php
index 8333707215..746919dc34 100644
--- a/tests/unit/Util/UrlTest.php
+++ b/tests/unit/Util/UrlTest.php
@@ -1,7 +1,7 @@
startsWith('http');
From 306cdeb68f4ab82b213a88f4ce6b78b2fffbfbb0 Mon Sep 17 00:00:00 2001
From: Jonathan Labreuille
Date: Mon, 27 Jun 2016 13:53:56 +0200
Subject: [PATCH 3/3] Models unit tests update
---
lib/Models/Model.php | 2 -
tests/unit/Models/CustomFieldTest.php | 13 ++++
tests/unit/Models/SettingTest.php | 9 +++
tests/unit/Models/StatisticsFormsTest.php | 8 +++
tests/unit/Models/SubscriberTest.php | 80 ++++++++++++++++++++++-
5 files changed, 109 insertions(+), 3 deletions(-)
diff --git a/lib/Models/Model.php b/lib/Models/Model.php
index 94402a494e..83792a8ebd 100644
--- a/lib/Models/Model.php
+++ b/lib/Models/Model.php
@@ -59,8 +59,6 @@ class Model extends \Sudzy\ValidModel {
default:
$this->setError($e->getMessage());
}
- } catch(\Exception $e) {
- $this->setError($e->getMessage());
}
return $this;
}
diff --git a/tests/unit/Models/CustomFieldTest.php b/tests/unit/Models/CustomFieldTest.php
index 78b3e646b7..d9eb649520 100644
--- a/tests/unit/Models/CustomFieldTest.php
+++ b/tests/unit/Models/CustomFieldTest.php
@@ -34,6 +34,19 @@ class CustomFieldTest extends MailPoetTest {
expect($this->custom_field->getErrors())->false();
}
+ function testItCanBeUpdated() {
+ expect($this->custom_field->name)->equals($this->data['name']);
+
+ $updated_custom_field = CustomField::createOrUpdate(array(
+ 'id' => $this->custom_field->id,
+ 'name' => 'Country'
+ ));
+
+ expect($updated_custom_field->getErrors())->false();
+ expect($updated_custom_field->name)->equals('Country');
+ expect($updated_custom_field->id)->equals($this->custom_field->id);
+ }
+
function testItHasAName() {
expect($this->custom_field->name)->equals($this->data['name']);
}
diff --git a/tests/unit/Models/SettingTest.php b/tests/unit/Models/SettingTest.php
index 2a59c9dd45..ac23fe85fd 100644
--- a/tests/unit/Models/SettingTest.php
+++ b/tests/unit/Models/SettingTest.php
@@ -25,6 +25,15 @@ class SettingTest extends MailPoetTest {
expect($default_settings['signup_confirmation']['enabled'])->true();
}
+ function testItCanLoadDefaults() {
+ Setting::$defaults = null;
+ expect(Setting::$defaults)->null();
+
+ $default_settings = Setting::getDefaults();
+ expect(Setting::$defaults)->notEmpty();
+ expect($default_settings['signup_confirmation']['enabled'])->true();
+ }
+
function testItCanGetAllSettingsIncludingDefaults() {
Setting::setValue('key_1', 'value_1');
Setting::setValue('key_2', 'value_2');
diff --git a/tests/unit/Models/StatisticsFormsTest.php b/tests/unit/Models/StatisticsFormsTest.php
index 3f9ee413b6..cff5579a45 100644
--- a/tests/unit/Models/StatisticsFormsTest.php
+++ b/tests/unit/Models/StatisticsFormsTest.php
@@ -28,6 +28,14 @@ class StatisticsFormsTest extends MailPoetTest {
expect(StatisticsForms::count())->equals(3);
}
+ function testItCannotRecordStatsWithoutFormOrSubscriber() {
+ $record = StatisticsForms::record($form_id = null, $subscriber_id = 1);
+ expect($record)->false();
+
+ $record = StatisticsForms::record($form_id = 1, $subscriber_id = null);
+ expect($record)->false();
+ }
+
function _after() {
StatisticsForms::deleteMany();
}
diff --git a/tests/unit/Models/SubscriberTest.php b/tests/unit/Models/SubscriberTest.php
index c9434e51e7..e16dcfce6b 100644
--- a/tests/unit/Models/SubscriberTest.php
+++ b/tests/unit/Models/SubscriberTest.php
@@ -48,6 +48,27 @@ class SubscriberTest extends MailPoetTest {
->equals($this->data['email']);
}
+ function testItShouldSetErrors() {
+ // model validation
+ $subscriber = Subscriber::create();
+ $subscriber->hydrate(array(
+ 'email' => 'invalid_email'
+ ));
+ $subscriber->save();
+ $errors = $subscriber->getErrors();
+ expect($errors)->contains("Your email address is invalid.");
+
+ // pdo error
+ $subscriber = Subscriber::create();
+ $subscriber->hydrate(array(
+ 'email' => 'test@test.com',
+ 'invalid_column' => true
+ ));
+ $subscriber->save();
+ $errors = $subscriber->getErrors();
+ expect($errors[0])->contains("Unknown column 'invalid_column' in 'field list'");
+ }
+
function emailMustBeUnique() {
$conflict_subscriber = Subscriber::create();
$conflict_subscriber->hydrate($this->data);
@@ -124,7 +145,7 @@ class SubscriberTest extends MailPoetTest {
function testItCanHaveCustomFields() {
$custom_field = CustomField::createOrUpdate(array(
'name' => 'DOB',
- 'type' => 'date',
+ 'type' => 'date'
));
$association = SubscriberCustomField::create();
@@ -138,6 +159,63 @@ class SubscriberTest extends MailPoetTest {
expect($subscriber->DOB)->equals($association->value);
}
+ function testItCanCreateSubscriberWithCustomFields() {
+ $custom_field = CustomField::createOrUpdate(array(
+ 'name' => 'City',
+ 'type' => 'text'
+ ));
+
+ $custom_field_2 = CustomField::createOrUpdate(array(
+ 'name' => 'Age',
+ 'type' => 'text'
+ ));
+
+ $subscriber_with_custom_field = Subscriber::createOrUpdate(array(
+ 'email' => 'user.with.cf@mailpoet.com',
+ 'cf_'.$custom_field->id => 'Paris',
+ 'cf_'.$custom_field_2->id => array(12, 23, 34)
+ ));
+
+ $subscriber = Subscriber::findOne($subscriber_with_custom_field->id)
+ ->withCustomFields();
+
+ expect($subscriber->id)->equals($subscriber_with_custom_field->id);
+ expect($subscriber->email)->equals('user.with.cf@mailpoet.com');
+ expect($subscriber->{'cf_'.$custom_field->id})->equals('Paris');
+ // array values are not supported so it should have assigned the first value
+ expect($subscriber->{'cf_'.$custom_field_2->id})->equals(12);
+ }
+
+ function testItShouldUnsubscribeFromAllSegments() {
+ $segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1'));
+ $segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2'));
+
+ $subscriber = Subscriber::createOrUpdate(array(
+ 'email' => 'jean.louis@mailpoet.com',
+ 'status' => Subscriber::STATUS_SUBSCRIBED,
+ 'segments' => array(
+ $segment_1->id,
+ $segment_2->id
+ )
+ ));
+
+ $subscriber = Subscriber::findOne($subscriber->id);
+
+ $subscribed_segments = $subscriber->segments()->findArray();
+ expect($subscribed_segments)->count(2);
+ expect($subscribed_segments[0]['name'] = 'Segment 1');
+ expect($subscribed_segments[1]['name'] = 'Segment 2');
+
+ // update subscriber status
+ $unsubscribed_subscriber = Subscriber::createOrUpdate(array(
+ 'email' => 'jean.louis@mailpoet.com',
+ 'status' => Subscriber::STATUS_UNSUBSCRIBED
+ ));
+
+ $subscribed_segments = $subscriber->segments()->findArray();
+ expect($subscribed_segments)->count(0);
+ }
+
function testItCanCreateOrUpdate() {
$data = array(
'email' => 'john.doe@mailpoet.com',
|