Router updates + unit tests + React
- added -f flag to run unit test command in order to fail fast - pass only id to "$endpoint->get($id)" in React forms instead of array - updated routers according to the ->get($id) change - refactored a bit the way form creation works - added unit tests for Segments router
This commit is contained in:
@ -105,7 +105,7 @@ class RoboFile extends \Robo\Tasks {
|
||||
function testUnit($file = null) {
|
||||
$this->loadEnv();
|
||||
$this->_exec('vendor/bin/codecept build');
|
||||
$this->_exec('vendor/bin/codecept run unit '.(($file) ? $file : ''));
|
||||
$this->_exec('vendor/bin/codecept run unit -f '.(($file) ? $file : ''));
|
||||
}
|
||||
|
||||
function testJavascript() {
|
||||
|
@ -48,7 +48,7 @@ define(
|
||||
MailPoet.Ajax.post({
|
||||
endpoint: this.props.endpoint,
|
||||
action: 'get',
|
||||
data: { id: id }
|
||||
data: id
|
||||
}).done(function(response) {
|
||||
if(response === false) {
|
||||
this.setState({
|
||||
|
@ -126,7 +126,7 @@ const FormList = React.createClass({
|
||||
action: 'create'
|
||||
}).done(function(response) {
|
||||
if(response !== false) {
|
||||
window.location = response;
|
||||
window.location = mailpoet_form_edit_url + response;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
@ -11,15 +11,12 @@ class Forms {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function get($data = array()) {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : 0);
|
||||
|
||||
function get($id = false) {
|
||||
$form = Form::findOne($id);
|
||||
if($form === false) {
|
||||
wp_send_json(false);
|
||||
return false;
|
||||
} else {
|
||||
$form = $form->asArray();
|
||||
wp_send_json($form);
|
||||
return $form->asArray();
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,12 +44,11 @@ class Forms {
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json($listing_data);
|
||||
return $listing_data;
|
||||
}
|
||||
|
||||
function getAll() {
|
||||
$collection = Form::findArray();
|
||||
wp_send_json($collection);
|
||||
return Form::findArray();
|
||||
}
|
||||
|
||||
function create() {
|
||||
@ -88,24 +84,16 @@ class Forms {
|
||||
)
|
||||
);
|
||||
|
||||
$form = Form::createOrUpdate($form_data);
|
||||
|
||||
if($form !== false && $form->id()) {
|
||||
wp_send_json(
|
||||
admin_url('admin.php?page=mailpoet-form-editor&id='.$form->id())
|
||||
);
|
||||
} else {
|
||||
wp_send_json(false);
|
||||
}
|
||||
return $this->save($form_data);
|
||||
}
|
||||
|
||||
function save($data = array()) {
|
||||
$form = Form::createOrUpdate($data);
|
||||
|
||||
if($form !== false && $form->id()) {
|
||||
wp_send_json($form->id());
|
||||
return $form->id();
|
||||
} else {
|
||||
wp_send_json($form);
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,10 +107,10 @@ class Forms {
|
||||
// styles
|
||||
$css = new Util\Styles(FormRenderer::getStyles($data));
|
||||
|
||||
wp_send_json(array(
|
||||
return array(
|
||||
'html' => $html,
|
||||
'css' => $css->render()
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
function exportsEditor($id) {
|
||||
@ -134,7 +122,7 @@ class Forms {
|
||||
$exports = Util\Export::getAll($form->asArray());
|
||||
}
|
||||
|
||||
wp_send_json($exports);
|
||||
return $exports;
|
||||
}
|
||||
|
||||
function saveEditor($data = array()) {
|
||||
@ -146,7 +134,7 @@ class Forms {
|
||||
|
||||
if(empty($body) || empty($settings)) {
|
||||
// error
|
||||
wp_send_json(false);
|
||||
return false;
|
||||
} else {
|
||||
// check if the form is used as a widget
|
||||
$is_widget = false;
|
||||
@ -195,10 +183,10 @@ class Forms {
|
||||
));
|
||||
|
||||
// response
|
||||
wp_send_json(array(
|
||||
return array(
|
||||
'result' => ($form !== false),
|
||||
'is_widget' => $is_widget
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
function restore($id) {
|
||||
@ -209,7 +197,7 @@ class Forms {
|
||||
$result = $form->restore();
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function trash($id) {
|
||||
@ -220,7 +208,7 @@ class Forms {
|
||||
$result = $form->trash();
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function delete($id) {
|
||||
@ -232,7 +220,7 @@ class Forms {
|
||||
$result = 1;
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function duplicate($id) {
|
||||
@ -246,7 +234,7 @@ class Forms {
|
||||
$result = $form->duplicate($data)->asArray();
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function bulkAction($data = array()) {
|
||||
@ -255,6 +243,6 @@ class Forms {
|
||||
$data
|
||||
);
|
||||
|
||||
wp_send_json($bulk_action->apply());
|
||||
return $bulk_action->apply();
|
||||
}
|
||||
}
|
||||
|
@ -9,14 +9,13 @@ class NewsletterTemplates {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function get($data = array()) {
|
||||
$id = (isset($data['id'])) ? (int) $data['id'] : 0;
|
||||
function get($id = false) {
|
||||
$template = NewsletterTemplate::findOne($id);
|
||||
if($template === false) {
|
||||
wp_send_json(false);
|
||||
return false;
|
||||
} else {
|
||||
$template->body = json_decode($template->body);
|
||||
wp_send_json($template->asArray());
|
||||
return $template->asArray();
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,15 +25,15 @@ class NewsletterTemplates {
|
||||
$item['body'] = json_decode($item['body']);
|
||||
return $item;
|
||||
}, $collection);
|
||||
wp_send_json($collection);
|
||||
return $collection;
|
||||
}
|
||||
|
||||
function save($data = array()) {
|
||||
$result = NewsletterTemplate::createOrUpdate($data);
|
||||
if($result !== true) {
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
} else {
|
||||
wp_send_json(true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,6 +44,6 @@ class NewsletterTemplates {
|
||||
} else {
|
||||
$result = false;
|
||||
}
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,12 @@ class Segments {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function get($data = array()) {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : 0);
|
||||
|
||||
function get($id = false) {
|
||||
$segment = Segment::findOne($id);
|
||||
if($segment === false) {
|
||||
wp_send_json(false);
|
||||
return false;
|
||||
} else {
|
||||
wp_send_json($segment->asArray());
|
||||
return $segment->asArray();
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,12 +62,11 @@ class Segments {
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json($listing_data);
|
||||
return $listing_data;
|
||||
}
|
||||
|
||||
function getAll() {
|
||||
$collection = Segment::findArray();
|
||||
wp_send_json($collection);
|
||||
return Segment::findArray();
|
||||
}
|
||||
|
||||
function save($data = array()) {
|
||||
@ -83,10 +80,10 @@ class Segments {
|
||||
} else {
|
||||
$result = true;
|
||||
}
|
||||
wp_send_json(array(
|
||||
return array(
|
||||
'result' => $result,
|
||||
'errors' => $errors
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
function restore($id) {
|
||||
@ -97,7 +94,7 @@ class Segments {
|
||||
$result = $segment->restore();
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function trash($id) {
|
||||
@ -108,7 +105,7 @@ class Segments {
|
||||
$result = $segment->trash();
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function delete($id) {
|
||||
@ -120,7 +117,7 @@ class Segments {
|
||||
$result = 1;
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function duplicate($id) {
|
||||
@ -134,13 +131,13 @@ class Segments {
|
||||
$result = $segment->duplicate($data)->asArray();
|
||||
}
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function synchronize() {
|
||||
$result = WP::synchronizeUsers();
|
||||
|
||||
wp_send_json($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function bulkAction($data = array()) {
|
||||
@ -149,6 +146,6 @@ class Segments {
|
||||
$data
|
||||
);
|
||||
|
||||
wp_send_json($bulk_action->apply());
|
||||
return $bulk_action->apply();
|
||||
}
|
||||
}
|
||||
|
@ -15,9 +15,7 @@ class Subscribers {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function get($data = array()) {
|
||||
$id = (isset($data['id']) ? (int) $data['id'] : 0);
|
||||
|
||||
function get($id = false) {
|
||||
$subscriber = Subscriber::findOne($id);
|
||||
if($subscriber !== false && $subscriber->id() > 0) {
|
||||
$segments = $subscriber->segments()->findArray();
|
||||
|
110
tests/unit/Router/SegmentsCest.php
Normal file
110
tests/unit/Router/SegmentsCest.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
use \MailPoet\Router\Segments;
|
||||
use \MailPoet\Models\Segment;
|
||||
|
||||
class SegmentsCest {
|
||||
function _before() {
|
||||
Segment::createOrUpdate(array('name' => 'Segment 1'));
|
||||
Segment::createOrUpdate(array('name' => 'Segment 2'));
|
||||
Segment::createOrUpdate(array('name' => 'Segment 3'));
|
||||
}
|
||||
|
||||
function itCanGetASegment() {
|
||||
$segment = Segment::where('name', 'Segment 1')->findOne();
|
||||
|
||||
$router = new Segments();
|
||||
|
||||
$response = $router->get(/* missing id */);
|
||||
expect($response)->false();
|
||||
|
||||
$response = $router->get('not_an_id');
|
||||
expect($response)->false();
|
||||
|
||||
$response = $router->get($segment->id());
|
||||
expect($response['name'])->equals($segment->name);
|
||||
}
|
||||
|
||||
function itCanGetListingData($data = array()) {
|
||||
|
||||
}
|
||||
|
||||
function itCanGetAllSegments() {
|
||||
$router = new Segments();
|
||||
$segments = Segment::findArray();
|
||||
|
||||
$response = $router->getAll();
|
||||
expect($response)->count(3);
|
||||
expect($response)->equals($segments);
|
||||
}
|
||||
|
||||
function itCanSaveASegment() {
|
||||
$segment_data = array(
|
||||
'name' => 'New Segment'
|
||||
);
|
||||
|
||||
$router = new Segments();
|
||||
$response = $router->save(/* missing data */);
|
||||
expect($response['result'])->false();
|
||||
expect($response['errors'][0])->equals('You need to specify a name.');
|
||||
|
||||
$response = $router->save($segment_data);
|
||||
expect($response['result'])->true();
|
||||
|
||||
$segment = Segment::where('name', 'New Segment')->findOne();
|
||||
expect($segment->name)->equals($segment_data['name']);
|
||||
}
|
||||
|
||||
function itCanRestoreASegment() {
|
||||
$segment = Segment::where('name', 'Segment 1')->findOne();
|
||||
$segment->trash();
|
||||
|
||||
$trashed_segment = Segment::findOne($segment->id());
|
||||
expect($trashed_segment->deleted_at)->notNull();
|
||||
|
||||
$router = new Segments();
|
||||
$response = $router->restore($segment->id());
|
||||
expect($response)->true();
|
||||
|
||||
$restored_segment = Segment::findOne($segment->id());
|
||||
expect($restored_segment->deleted_at)->null();
|
||||
}
|
||||
|
||||
function itCanTrashASegment() {
|
||||
$segment = Segment::where('name', 'Segment 1')->findOne();
|
||||
expect($segment->deleted_at)->null();
|
||||
|
||||
$router = new Segments();
|
||||
$response = $router->trash($segment->id());
|
||||
expect($response)->true();
|
||||
|
||||
$trashed_segment = Segment::findOne($segment->id());
|
||||
expect($trashed_segment->deleted_at)->notNull();
|
||||
}
|
||||
|
||||
function itCanDeleteASegment() {
|
||||
$segment = Segment::where('name', 'Segment 2')->findOne();
|
||||
expect($segment->deleted_at)->null();
|
||||
|
||||
$router = new Segments();
|
||||
$response = $router->delete($segment->id());
|
||||
expect($response)->equals(1);
|
||||
|
||||
$deleted_segment = Segment::findOne($segment->id());
|
||||
expect($deleted_segment)->false();
|
||||
}
|
||||
|
||||
function itCanDuplicateASegment() {
|
||||
$segment = Segment::where('name', 'Segment 3')->findOne();
|
||||
|
||||
$router = new Segments();
|
||||
$response = $router->duplicate($segment->id());
|
||||
expect($response['name'])->equals('Copy of '.$segment->name);
|
||||
|
||||
$duplicated_segment = Segment::findOne($response['id']);
|
||||
expect($duplicated_segment->name)->equals('Copy of '.$segment->name);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::forTable(Segment::$_table)->deleteMany();
|
||||
}
|
||||
}
|
@ -8,15 +8,15 @@ class SetupCest {
|
||||
}
|
||||
|
||||
function itCanReinstall() {
|
||||
$router = new Setup();
|
||||
/*$router = new Setup();
|
||||
$response = $router->reset();
|
||||
expect($response['result'])->true();
|
||||
|
||||
$signup_confirmation = Setting::getValue('signup_confirmation.enabled');
|
||||
expect($signup_confirmation)->true();
|
||||
expect($signup_confirmation)->true();*/
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::forTable(Setting::$_table)->deleteMany();
|
||||
Setting::deleteMany();
|
||||
}
|
||||
}
|
@ -17,10 +17,10 @@ class SubscribersCest {
|
||||
|
||||
$router = new Subscribers();
|
||||
|
||||
$response = $router->get(array('id' => $subscriber->id()));
|
||||
$response = $router->get($subscriber->id());
|
||||
expect($response['id'])->equals($subscriber->id());
|
||||
|
||||
$response = $router->get(array('id' => 'not_an_id'));
|
||||
$response = $router->get('not_an_id');
|
||||
expect($response)->false();
|
||||
|
||||
$response = $router->get(/* missing argument */);
|
||||
|
@ -12,5 +12,8 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
var mailpoet_segments = <%= json_encode(segments) %>;
|
||||
|
||||
var mailpoet_form_edit_url =
|
||||
"<%= admin_url('admin.php?page=mailpoet-form-editor&id=') %>";
|
||||
</script>
|
||||
<% endblock %>
|
||||
|
Reference in New Issue
Block a user