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) { ).then(function(data) {
return data; return data;
}, function(xhr) { }, function(xhr) {
return xhr.responseJSON; if (!xhr.responseJSON) {
return {
errors: [{ error: 'undefined', message: "An unknown error occurred." }]
};
} else {
return xhr.responseJSON;
}
}); });
// clear options // clear options

View File

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

View File

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

View File

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

View File

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

View File

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