From bd7f2c26546a252a49c88fe69350aa2546474e63 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Wed, 21 Mar 2018 20:22:43 +0100 Subject: [PATCH] add unit-tests for CustomerBackup-ApiCommand Signed-off-by: Michael Kaufmann (d00p) --- .../api/commands/class.CustomerBackups.php | 5 +- phpunit.xml | 1 + tests/Backup/CustomerBackupsTest.php | 112 ++++++++++++++++++ 3 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 tests/Backup/CustomerBackupsTest.php diff --git a/lib/classes/api/commands/class.CustomerBackups.php b/lib/classes/api/commands/class.CustomerBackups.php index 92cfd09a..46dd10e3 100644 --- a/lib/classes/api/commands/class.CustomerBackups.php +++ b/lib/classes/api/commands/class.CustomerBackups.php @@ -117,10 +117,10 @@ class CustomerBackups extends ApiCommand implements ResourceEntity { // get planned backups $result = $this->apiCall('CustomerBackups.listing', $this->getParamList()); - + $entry = $this->getParam('backup_job_entry'); $customer_ids = $this->getAllowedCustomerIds('extras.backup'); - + if ($result['count'] > 0 && $entry > 0) { // prepare statement $del_stmt = Database::prepare("DELETE FROM `" . TABLE_PANEL_TASKS . "` WHERE `id` = :tid"); @@ -130,6 +130,7 @@ class CustomerBackups extends ApiCommand implements ResourceEntity Database::pexecute($del_stmt, array( 'tid' => $entry )); + $this->logger()->logAction($this->isAdmin() ? ADM_ACTION : USR_ACTION, LOG_NOTICE, "[API] deleted planned customer-backup #" . $entry); return $this->response(200, "successfull", true); } } diff --git a/phpunit.xml b/phpunit.xml index 3f800929..33b73837 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -18,6 +18,7 @@ tests/Ftps tests/Emails tests/Extras + tests/Backup diff --git a/tests/Backup/CustomerBackupsTest.php b/tests/Backup/CustomerBackupsTest.php new file mode 100644 index 00000000..210b36eb --- /dev/null +++ b/tests/Backup/CustomerBackupsTest.php @@ -0,0 +1,112 @@ + 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + + $data = [ + 'path' => '/my-backup', + 'backup_dbs' => 1, + 'backup_mail' => 2, + 'backup_web' => 1 + ]; + $json_result = CustomerBackups::getLocal($customer_userdata, $data)->add(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals($customer_userdata['documentroot'] . 'my-backup/', $result['destdir']); + $this->assertEquals('1', $result['backup_dbs']); + $this->assertEquals('0', $result['backup_mail']); + $this->assertEquals('1', $result['backup_web']); + } + + public function testAdminCustomerBackupsGet() + { + global $admin_userdata; + $this->expectExceptionCode(303); + CustomerBackups::getLocal($admin_userdata)->get(); + } + + public function testAdminCustomerBackupsUpdate() + { + global $admin_userdata; + $this->expectExceptionCode(303); + CustomerBackups::getLocal($admin_userdata)->update(); + } + + /** + * + * @depends testCustomerCustomerBackupsAdd + */ + public function testAdminCustomerBackupsListing() + { + global $admin_userdata; + + $json_result = CustomerBackups::getLocal($admin_userdata)->listing(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals(1, $result['count']); + $this->assertEquals('1', $result['list'][0]['data']['backup_dbs']); + $this->assertEquals('0', $result['list'][0]['data']['backup_mail']); + $this->assertEquals('1', $result['list'][0]['data']['backup_web']); + } + + /** + * + * @depends testCustomerCustomerBackupsAdd + */ + public function testCustomerCustomerBackupsDelete() + { + global $admin_userdata; + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + + $data = [ + 'backup_job_entry' => 1 + ]; + $json_result = CustomerBackups::getLocal($customer_userdata, $data)->delete(); + $result = json_decode($json_result, true)['data']; + $this->assertTrue($result); + } + + /** + * + * @depends testAdminCustomerBackupsListing + */ + public function testCustomerCustomerBackupsDeleteNotFound() + { + global $admin_userdata; + + // get customer + $json_result = Customers::getLocal($admin_userdata, array( + 'loginname' => 'test1' + ))->get(); + $customer_userdata = json_decode($json_result, true)['data']; + + $data = [ + 'backup_job_entry' => 1337 + ]; + $this->expectExceptionCode(404); + $this->expectExceptionMessage('Backup job with id #1337 could not be found'); + CustomerBackups::getLocal($customer_userdata, $data)->delete(); + } +}