Added default error response in case no errors were specified

- converted Setup endpoint
- unit tests for Setup endpoint
This commit is contained in:
Jonathan Labreuille
2016-08-03 12:41:21 +02:00
parent afa0d3af63
commit 28c39d301c
6 changed files with 29 additions and 19 deletions

View File

@ -60,7 +60,13 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery,
).then(function(data) {
return data;
}, function(xhr) {
if (!xhr.responseJSON) {
return {
errors: [{ error: 'undefined', message: "An unknown error occurred." }]
};
} else {
return xhr.responseJSON;
}
});
// clear options

View File

@ -21,7 +21,7 @@ class Settings extends APIEndpoint {
foreach($settings as $name => $value) {
Setting::setValue($name, $value);
}
return $this->successResponse(Setting::getAll());
return $this->successResponse();
}
}
}

View File

@ -1,10 +1,11 @@
<?php
namespace MailPoet\API\Endpoints;
use \MailPoet\API\Endpoint as APIEndpoint;
use \MailPoet\Config\Activator;
if(!defined('ABSPATH')) exit;
class Setup {
class Setup extends APIEndpoint {
function __construct() {
}
@ -13,12 +14,11 @@ class Setup {
$activator = new Activator();
$activator->deactivate();
$activator->activate();
$result = true;
return $this->successResponse();
} catch(\Exception $e) {
$result = false;
return $this->errorResponse(array(
$e->getMessage()
));
}
return array(
'result' => $result
);
}
}

View File

@ -1,4 +1,5 @@
<?php
use \MailPoet\API\Response;
use \MailPoet\API\Endpoints\Setup;
use \MailPoet\Models\Setting;
@ -8,12 +9,12 @@ class SetupTest extends MailPoetTest {
}
function testItCanReinstall() {
/*$router = new Setup();
$router = new Setup();
$response = $router->reset();
expect($response['result'])->true();
expect($response->status)->equals(Response::STATUS_OK);
$signup_confirmation = Setting::getValue('signup_confirmation.enabled');
expect($signup_confirmation)->true();*/
expect($signup_confirmation)->true();
}
function _after() {

View File

@ -77,20 +77,20 @@
endpoint: 'settings',
action: 'set',
data: settings_data
}).always(function() {
MailPoet.Modal.loading(false);
}).done(function(response) {
MailPoet.Notice.success(
"<%= __('Settings saved') %>",
{ scroll: true }
);
MailPoet.Modal.loading(false);
}).fail(function(response) {
if (response.errors !== undefined) {
if (response.errors.length > 0) {
MailPoet.Notice.error(
response.errors.map(function(error) { return error.message; }),
{ scroll: true }
);
}
MailPoet.Modal.loading(false);
});
}

View File

@ -175,13 +175,16 @@
MailPoet.Ajax.post({
'endpoint': 'setup',
'action': 'reset'
}).always(function() {
MailPoet.Modal.loading(false);
}).done(function(response) {
if(response.result === true) {
window.location = "<%= admin_url('admin.php?page=mailpoet-newsletters') %>";
} else {
}).fail(function(response) {
if (response.errors.length > 0) {
MailPoet.Notice.error(
"<%= __('MailPoet could not be reinstalled. You may need to remove it manually first.') %>",
{ scroll: true });
response.errors.map(function(error) { return error.message; }),
{ scroll: true }
);
}
});
}