add Cronjobs API command ad unit-tests
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
@@ -77,31 +77,21 @@ if ($page == 'cronjobs' || $page == 'overview') {
|
|||||||
* @TODO later
|
* @TODO later
|
||||||
*/
|
*/
|
||||||
} elseif ($action == 'edit' && $id != 0) {
|
} elseif ($action == 'edit' && $id != 0) {
|
||||||
$result_stmt = Database::prepare("SELECT * FROM `" . TABLE_PANEL_CRONRUNS . "` WHERE `id`= :id");
|
try {
|
||||||
Database::pexecute($result_stmt, array('id' => $id));
|
$json_result = Cronjobs::getLocal($userinfo, array(
|
||||||
$result = $result_stmt->fetch(PDO::FETCH_ASSOC);
|
'id' => $id
|
||||||
|
))->get();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
dynamic_error($e->getMessage());
|
||||||
|
}
|
||||||
|
$result = json_decode($json_result, true)['data'];
|
||||||
if ($result['cronfile'] != '') {
|
if ($result['cronfile'] != '') {
|
||||||
if (isset($_POST['send']) && $_POST['send'] == 'send') {
|
if (isset($_POST['send']) && $_POST['send'] == 'send') {
|
||||||
$isactive = isset($_POST['isactive']) ? 1 : 0;
|
try {
|
||||||
$interval_value = validate($_POST['interval_value'], 'interval_value', '/^([0-9]+)$/Di', 'stringisempty');
|
Cronjobs::getLocal($userinfo, $_POST)->update();
|
||||||
$interval_interval = validate($_POST['interval_interval'], 'interval_interval');
|
} catch (Exception $e) {
|
||||||
|
dynamic_error($e->getMessage());
|
||||||
if ($isactive != 1) {
|
|
||||||
$isactive = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$interval = $interval_value . ' ' . strtoupper($interval_interval);
|
|
||||||
|
|
||||||
$upd = Database::prepare("
|
|
||||||
UPDATE `" . TABLE_PANEL_CRONRUNS . "`
|
|
||||||
SET `isactive` = :isactive, `interval` = :int
|
|
||||||
WHERE `id` = :id"
|
|
||||||
);
|
|
||||||
Database::pexecute($upd, array('isactive' => $isactive, 'int' => $interval, 'id' => $id));
|
|
||||||
|
|
||||||
// insert task to re-generate the cron.d-file
|
|
||||||
inserttask('99');
|
|
||||||
|
|
||||||
redirectTo($filename, array('page' => $page, 's' => $s));
|
redirectTo($filename, array('page' => $page, 's' => $s));
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
|||||||
@@ -17,20 +17,142 @@
|
|||||||
*/
|
*/
|
||||||
class Cronjobs extends ApiCommand implements ResourceEntity
|
class Cronjobs extends ApiCommand implements ResourceEntity
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* You cannot add new cronjobs yet.
|
||||||
|
*/
|
||||||
public function add()
|
public function add()
|
||||||
{}
|
{
|
||||||
|
throw new Exception('You cannot add new cronjobs yet.', 303);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return a cronjob entry by id
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* cronjob-id
|
||||||
|
*
|
||||||
|
* @access admin
|
||||||
|
* @throws Exception
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function get()
|
public function get()
|
||||||
{}
|
{
|
||||||
|
if ($this->isAdmin()) {
|
||||||
|
$id = $this->getParam('id');
|
||||||
|
|
||||||
|
$result_stmt = Database::prepare("
|
||||||
|
SELECT * FROM `" . TABLE_PANEL_CRONRUNS . "` WHERE `id` = :id
|
||||||
|
");
|
||||||
|
$result = Database::pexecute_first($result_stmt, array(
|
||||||
|
'id' => $id
|
||||||
|
), true, true);
|
||||||
|
if ($result) {
|
||||||
|
return $this->response(200, "successfull", $result);
|
||||||
|
}
|
||||||
|
throw new Exception("cronjob with id #" . $id . " could not be found", 404);
|
||||||
|
}
|
||||||
|
throw new Exception("Not allowed to execute given command.", 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* update a cronjob entry by given id
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param bool $isactive
|
||||||
|
* optional whether the cronjob is active or not
|
||||||
|
* @param int $interval_value
|
||||||
|
* optional number of seconds/minutes/hours/etc. for the interval
|
||||||
|
* @param string $interval_interval
|
||||||
|
* optional interval for the cronjob (MINUTE, HOUR, DAY, WEEK or MONTH)
|
||||||
|
*
|
||||||
|
* @access admin
|
||||||
|
* @throws Exception
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function update()
|
public function update()
|
||||||
{}
|
{
|
||||||
|
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
|
||||||
|
|
||||||
|
// required parameter
|
||||||
|
$id = $this->getParam('id');
|
||||||
|
|
||||||
|
$result = $this->apiCall('Cronjobs.get', array(
|
||||||
|
'id' => $id
|
||||||
|
));
|
||||||
|
|
||||||
|
// split interval
|
||||||
|
$cur_int = explode(" ", $result['interval']);
|
||||||
|
|
||||||
|
// parameter
|
||||||
|
$isactive = $this->getParam('isactive', true, $result['isactive']);
|
||||||
|
$interval_value = $this->getParam('interval_value', true, $cur_int[0]);
|
||||||
|
$interval_interval = $this->getParam('interval_interval', true, $cur_int[1]);
|
||||||
|
|
||||||
|
// validation
|
||||||
|
if ($isactive != 1) {
|
||||||
|
$isactive = 0;
|
||||||
|
}
|
||||||
|
$interval_value = validate($interval_value, 'interval_value', '/^([0-9]+)$/Di', 'stringisempty', array(), true);
|
||||||
|
$interval_interval = validate($interval_interval, 'interval_interval', '', '', array(), true);
|
||||||
|
|
||||||
|
// put together interval value
|
||||||
|
$interval = $interval_value . ' ' . strtoupper($interval_interval);
|
||||||
|
|
||||||
|
$upd_stmt = Database::prepare("
|
||||||
|
UPDATE `" . TABLE_PANEL_CRONRUNS . "`
|
||||||
|
SET `isactive` = :isactive, `interval` = :int
|
||||||
|
WHERE `id` = :id
|
||||||
|
");
|
||||||
|
Database::pexecute($upd_stmt, array(
|
||||||
|
'isactive' => $isactive,
|
||||||
|
'int' => $interval,
|
||||||
|
'id' => $id
|
||||||
|
), true, true);
|
||||||
|
|
||||||
|
// insert task to re-generate the cron.d-file
|
||||||
|
inserttask('99');
|
||||||
|
$this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] cronjob with description '" . $result['module'] . '/' . $result['cronfile'] . "' has been updated by '" . $this->getUserDetail('loginname') . "'");
|
||||||
|
$result = $this->apiCall('Cronjobs.get', array(
|
||||||
|
'id' => $id
|
||||||
|
));
|
||||||
|
return $this->response(200, "successfull", $result);
|
||||||
|
}
|
||||||
|
throw new Exception("Not allowed to execute given command.", 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lists all cronjob entries
|
||||||
|
*
|
||||||
|
* @access admin
|
||||||
|
* @throws Exception
|
||||||
|
* @return array count|list
|
||||||
|
*/
|
||||||
public function listing()
|
public function listing()
|
||||||
{}
|
{
|
||||||
|
if ($this->isAdmin()) {
|
||||||
|
$this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] list cronjobs");
|
||||||
|
$result_stmt = Database::prepare("
|
||||||
|
SELECT `c`.* FROM `" . TABLE_PANEL_CRONRUNS . "` `c` ORDER BY `module` ASC, `cronfile` ASC
|
||||||
|
");
|
||||||
|
Database::pexecute($result_stmt);
|
||||||
|
$result = array();
|
||||||
|
while ($row = $result_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||||
|
$result[] = $row;
|
||||||
|
}
|
||||||
|
return $this->response(200, "successfull", array(
|
||||||
|
'count' => count($result),
|
||||||
|
'list' => $result
|
||||||
|
));
|
||||||
|
}
|
||||||
|
throw new Exception("Not allowed to execute given command.", 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* You cannot delete system cronjobs.
|
||||||
|
*/
|
||||||
public function delete()
|
public function delete()
|
||||||
{}
|
{
|
||||||
|
throw new Exception('You cannot delete system cronjobs.', 303);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
93
tests/Cronjobs/CronjobsTest.php
Normal file
93
tests/Cronjobs/CronjobsTest.php
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ApiCommand
|
||||||
|
* @covers ApiParameter
|
||||||
|
* @covers IpsAndPorts
|
||||||
|
*/
|
||||||
|
class CronjobsTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
public function testAdminCronjobsList()
|
||||||
|
{
|
||||||
|
global $admin_userdata;
|
||||||
|
$json_result = Cronjobs::getLocal($admin_userdata)->listing();
|
||||||
|
$result = json_decode($json_result, true)['data'];
|
||||||
|
$this->assertTrue(isset($result['list'][0]['module']));
|
||||||
|
$this->assertTrue(isset($result['list'][0]['cronfile']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCronjobsAdd()
|
||||||
|
{
|
||||||
|
global $admin_userdata;
|
||||||
|
$data = [];
|
||||||
|
$this->expectExceptionCode(303);
|
||||||
|
$this->expectExceptionMessage("You cannot add new cronjobs yet.");
|
||||||
|
Cronjobs::getLocal($admin_userdata, $data)->add();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCronjobsGetNotFound()
|
||||||
|
{
|
||||||
|
global $admin_userdata;
|
||||||
|
$this->expectExceptionCode(404);
|
||||||
|
$this->expectExceptionMessage("cronjob with id #999 could not be found");
|
||||||
|
Cronjobs::getLocal($admin_userdata, array('id' => 999))->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCustomerCronjobsGetNotAllowed()
|
||||||
|
{
|
||||||
|
global $admin_userdata;
|
||||||
|
// get customer
|
||||||
|
$json_result = Customers::getLocal($admin_userdata, array(
|
||||||
|
'loginname' => 'test1'
|
||||||
|
))->get();
|
||||||
|
$customer_userdata = json_decode($json_result, true)['data'];
|
||||||
|
$this->expectExceptionCode(403);
|
||||||
|
$this->expectExceptionMessage("Not allowed to execute given command.");
|
||||||
|
Cronjobs::getLocal($customer_userdata, array('id' => 1))->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCronjobsEdit()
|
||||||
|
{
|
||||||
|
global $admin_userdata;
|
||||||
|
$data = [
|
||||||
|
'id' => 1,
|
||||||
|
'isactive' => 0,
|
||||||
|
'interval_value' => 10
|
||||||
|
];
|
||||||
|
$json_result = Cronjobs::getLocal($admin_userdata, $data)->update();
|
||||||
|
$result = json_decode($json_result, true)['data'];
|
||||||
|
$this->assertEquals(0, $result['isactive']);
|
||||||
|
$this->assertEquals('10 MINUTE', $result['interval']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testResellerCronjobsEditNotAllowed()
|
||||||
|
{
|
||||||
|
global $admin_userdata;
|
||||||
|
// get reseller
|
||||||
|
$json_result = Admins::getLocal($admin_userdata, array(
|
||||||
|
'loginname' => 'reseller'
|
||||||
|
))->get();
|
||||||
|
$reseller_userdata = json_decode($json_result, true)['data'];
|
||||||
|
$reseller_userdata['adminsession'] = 1;
|
||||||
|
$data = [
|
||||||
|
'id' => 1,
|
||||||
|
'isactive' => 1
|
||||||
|
];
|
||||||
|
$this->expectExceptionCode(403);
|
||||||
|
$this->expectExceptionMessage("Not allowed to execute given command.");
|
||||||
|
Cronjobs::getLocal($reseller_userdata, $data)->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdminCronjobsDelete()
|
||||||
|
{
|
||||||
|
global $admin_userdata;
|
||||||
|
$data = [
|
||||||
|
'id' => 3
|
||||||
|
];
|
||||||
|
$this->expectExceptionCode(303);
|
||||||
|
$this->expectExceptionMessage("You cannot delete system cronjobs.");
|
||||||
|
Cronjobs::getLocal($admin_userdata, $data)->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user