From d3eec7d304f757518012cf4bc5d3430da663c91c Mon Sep 17 00:00:00 2001 From: Domingo Oropeza Date: Wed, 14 Mar 2018 17:02:49 -0400 Subject: [PATCH] feat(policy): update code for remove applications Signed-off-by: Domingo Oropeza --- inc/policydeployapplication.class.php | 15 +- inc/policyremoveapplication.class.php | 46 ++++ inc/task.class.php | 5 +- .../PluginFlyvemdmPolicyRemoveapplication.php | 233 ++++++++++++++++++ 4 files changed, 287 insertions(+), 12 deletions(-) create mode 100644 tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php diff --git a/inc/policydeployapplication.class.php b/inc/policydeployapplication.class.php index 615adb6c..4da92217 100644 --- a/inc/policydeployapplication.class.php +++ b/inc/policydeployapplication.class.php @@ -191,16 +191,11 @@ public function unapply(PluginFlyvemdmFleet $fleet, $value, $itemtype, $itemId) return true; } - $task = new PluginFlyvemdmTask(); - $packageName = $package->getField('package_name'); - $packageName = ($packageName) ? $packageName : $package->getField('name'); - if (!$task->add([ - 'plugin_flyvemdm_fleets_id' => $fleet->getID(), - 'plugin_flyvemdm_policies_id' => $policyData->getID(), - 'value' => $packageName, - '_silent' => true, - ])) { - return false; + $package = new PluginFlyvemdmPackage(); + if ($package->getFromDB($itemId)) { + $policyFactory = new PluginFlyvemdmPolicyFactory(); + $removeApp = $policyFactory->createFromPolicy($policyData); + $removeApp->apply($fleet, '', $package, $itemId); } return true; diff --git a/inc/policyremoveapplication.class.php b/inc/policyremoveapplication.class.php index 60e9d2e3..0b99de36 100644 --- a/inc/policyremoveapplication.class.php +++ b/inc/policyremoveapplication.class.php @@ -100,6 +100,52 @@ public function getMqttMessage($value, $itemtype, $itemId) { return $array; } + /** + * @param PluginFlyvemdmFleet $fleet + * @param mixed $value + * @param mixed $itemtype + * @param int $itemId + * @return bool + */ + public function apply(PluginFlyvemdmFleet $fleet, $value, $itemtype, $itemId) { + // force the itemtype for packages + if (!($itemtype instanceof PluginFlyvemdmPackage)) { + $itemtype = new PluginFlyvemdmPackage(); + } + + // the itemId wasn't send and the itemtype is new + if ($itemtype->isNewItem() && !$itemId) { + Session::addMessageAfterRedirect(__('An application ID is required', 'flyvemdm'), + false, ERROR); + return false; + } + + // the itemid is send but the itemtype is new, lest's try to load the info of the package + if ($itemtype->isNewItem() && $itemId) { + if (!$itemtype->getFromDB($itemId)) { + Session::addMessageAfterRedirect(__('The application does not exists', 'flyvemdm'), + false, ERROR); + return false; + } + } + + // the package is loaded, let's create the task + $packageName = $itemtype->getField('package_name'); + $packageName = ($packageName) ? $packageName : $itemtype->getField('name'); + $value = json_encode(['package' => $packageName, 'id' => $itemtype->getID()]); + $task = new PluginFlyvemdmTask(); + if (!$task->add([ + 'plugin_flyvemdm_fleets_id' => $fleet->getID(), + 'plugin_flyvemdm_policies_id' => $this->getPolicyData()->getID(), + 'value' => $value, + '_silent' => true, + ])) { + return false; + } + + return true; + } + public static function getEnumSpecificStatus() { return [ 'waiting' => __('Waiting', 'flyvemdm'), diff --git a/inc/task.class.php b/inc/task.class.php index 69ee8121..3da4b69f 100644 --- a/inc/task.class.php +++ b/inc/task.class.php @@ -369,6 +369,8 @@ private function deleteTaskStatuses() { * MQTT publish a policy applying to the fleet * * @param PluginFlyvemdmNotifiable $item + * @throws TaskPublishPolicyBadFleetException + * @throws TaskPublishPolicyPolicyNotFoundException */ public function publishPolicy(PluginFlyvemdmNotifiable $item) { if ($this->silent) { @@ -558,10 +560,9 @@ public function getGroupOfPolicies($group, $fleet) { } /** - * * @param array $policiesToApply - * * @return array + * @throws PolicyApplicationException */ protected function buildMqttMessage($policiesToApply) { // generate message of all policies diff --git a/tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php b/tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php new file mode 100644 index 00000000..562676e2 --- /dev/null +++ b/tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php @@ -0,0 +1,233 @@ + 'application', + 'symbol' => 'removeApp', + 'type_data' => '', + 'unicity' => 0, + ]; + + private $packageName = 'com.domain.author.application.apk'; + private $filename = 'application.apk'; + + /** + * @param null|\PluginFlyvemdmPolicy $policyData + * @return array + */ + private function createNewPolicyInstance($policyData = null) { + if(null === $policyData){ + $policyData = new \PluginFlyvemdmPolicy(); + $policyData->fields = $this->dataField; + } + $policy = $this->newTestedInstance($policyData); + return [$policy, $policyData]; + } + + /** + * @return array + */ + protected function validationProvider() { + return [ + 'Check ID exist' => [ + 'data' => ['', '', 0], + 'expected' => [false, 'An application ID is required'], + ], + 'Check itemtype is empty 1' => [ + 'data' => ['lorem', 'Bad Value', 0], + 'expected' => [false], + ], + 'Check itemtype is empty 2' => [ + 'data' => ['lorem', '', 1], + 'expected' => [false], + ], + 'Valid check 1' => [ + 'data' => ['any value', '', 0], + 'expected' => [true], + ], + ]; + } + + /** + * @dataProvider validationProvider + * @tags testIntegrityCheck + * @param array $data + * @param array $expected + */ + public function testIntegrityCheck(array $data, array $expected) { + list($policy) = $this->createNewPolicyInstance(); + $success = $policy->integrityCheck($data[0], $data[1], $data[2]); + $this->boolean($success)->isEqualTo($expected[0]); + if (!$expected[0] && isset($expected[1])) { + $this->string($_SESSION["MESSAGE_AFTER_REDIRECT"][0][0])->isEqualTo($expected[1]); + } + } + + /** + * @return array + */ + public function unicityCheckProvider() { + return [ + 'Check for invalid package' => [ + 'data' => ['package_name', 'id'], + 'expected' => [true], + ], + // TODO: For test this case a fleet must be created first + /*'Check for valid package' => [ + 'data' => ['package_name', 'id'], + 'expected' => [false], + ],*/ + ]; + } + + /** + * @dataProvider unicityCheckProvider + * @tags testUnicityCheck + * @param array $data + * @param array $expected + */ + public function testUnicityCheck(array $data, array $expected) { + list($policy) = $this->createNewPolicyInstance(); + $mockInstance = $this->newMockInstance('\PluginFlyvemdmFleet'); + $mockInstance->getMockController()->getID = 1; + if(!$expected[0]){ + $application = $this->createAppInDB(); + $data[0] = $application->getName(); + $data[1] = $application->getID(); + } + $this->boolean($policy->unicityCheck($data[0], \PluginFlyvemdmPackage::class, $data[1], + $mockInstance))->isEqualTo($expected[0]); + } + + /** + * @return array + */ + public function applyProvider() { + $package = $this->createAppInDB(); + return [ + 'Check for invalid arguments 1' => [ + 'data' => [null, null, null], + 'expected' => ['return' => false], + ], + 'Check for invalid arguments 2' => [ + 'data' => [null, null, ''], + 'expected' => ['return' => false, 'message'=>'An application ID is required'], + ], + 'Check for invalid arguments 3' => [ + 'data' => [null, null, -10], + 'expected' => ['return' => false, 'message'=>'The application does not exists'], + ], + 'Check for correct value' => [ + 'data' => [null, $package, $package->getID()], + 'expected' => ['return' => true], + ], + ]; + } + + /** + * @dataProvider applyProvider + * @tags testApply + * @param array $data + * @param array $expected + */ + public function testApply(array $data, array $expected) { + $policyData = null; + if (true === $expected['return']) { + $policyData = new \PluginFlyvemdmPolicy(); + $policyData->getFromDBBySymbol('removeApp'); + } + list($policy) = $this->createNewPolicyInstance($policyData); + $mockInstance = $this->newMockInstance('\PluginFlyvemdmFleet'); + $mockInstance->getMockController()->getID = 1; + $result = $policy->apply($mockInstance, $data[0], $data[1], $data[2]); + $this->boolean($result)->isEqualTo($expected['return']); + if (!$expected['return'] && isset($expected['message'])) { + $this->string($_SESSION["MESSAGE_AFTER_REDIRECT"][1][0])->isEqualTo($expected['message']); + unset($_SESSION["MESSAGE_AFTER_REDIRECT"]); // to clear the buffer + } + } + + /** + * @tags testGetMqttMessage + */ + public function testGetMqttMessage() { + list($policy) = $this->createNewPolicyInstance(); + $this->boolean($policy->getMqttMessage(null, null, null))->isFalse(); + $item = $this->createAppInDB(); + $packageName = $item->getField('package_name'); + $result = $policy->getMqttMessage($packageName, '', 0); + $this->array($result)->hasKeys(['id', $this->dataField['symbol']]) + ->string($result['id'])->isEqualTo($item->getID()) + ->string($result[$this->dataField['symbol']])->isEqualTo($packageName); + } + + /** + * Create an application (directly in DB) because we are not uploading any file + * @return \PluginFlyvemdmPackage + */ + private function createAppInDB() { + global $DB; + + $uniqid = uniqid(); + $table_file = \PluginFlyvemdmPackage::getTable(); + $query = "INSERT INTO `$table_file` ( + `package_name`, + `alias`, + `version`, + `filename`, + `entities_id`, + `dl_filename`, + `icon` + ) VALUES ( + '" . $uniqid . $this->packageName . "', + 'application', + '1.0.5', + '0/" . $uniqid . $this->filename . "', + '0', + '" . $this->filename . "', + '' + )"; + $result = $DB->query($query); + $this->boolean($result)->isTrue(); + + $file = new \PluginFlyvemdmPackage(); + $file->getFromDB($DB->insert_id()); + $this->boolean($file->isNewItem())->isFalse(); + + return $file; + } +} \ No newline at end of file