Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit 188510d

Browse files
committed
test(policy): add unit tests for APN policy
Signed-off-by: Domingo Oropeza <[email protected]>
1 parent c17f414 commit 188510d

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

inc/policyapn.class.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
* You should have received a copy of the GNU Affero General Public License
2222
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
2323
* ------------------------------------------------------------------------------
24-
* @author Thierry Bugier
2524
* @copyright Copyright © 2018 Teclib
26-
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
25+
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
2726
* @link https://github.com/flyve-mdm/glpi-plugin
2827
* @link https://flyve-mdm.com/
2928
* ------------------------------------------------------------------------------
@@ -95,7 +94,7 @@ public function integrityCheck($value, $itemtype, $itemId) {
9594
* @param int $itemId
9695
* @return array|bool
9796
*/
98-
public function getMqttMessage($value, $itemtype, $itemId) {
97+
public function getBrokerMessage($value, $itemtype, $itemId) {
9998
$decodedValue = json_decode($value, JSON_OBJECT_AS_ARRAY);
10099
if (!$this->integrityCheck($decodedValue, $itemtype, $itemId)) {
101100
return false;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* LICENSE
4+
*
5+
* Copyright © 2016-2018 Teclib'
6+
* Copyright © 2010-2018 by the FusionInventory Development Team.
7+
*
8+
* This file is part of Flyve MDM Plugin for GLPI.
9+
*
10+
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
11+
* device management software.
12+
*
13+
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU Affero General Public License as published
15+
* by the Free Software Foundation, either version 3 of the License, or
16+
* (at your option) any later version.
17+
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
23+
* ------------------------------------------------------------------------------
24+
* @copyright Copyright © 2018 Teclib
25+
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
26+
* @link https://github.com/flyve-mdm/glpi-plugin
27+
* @link https://flyve-mdm.com/
28+
* ------------------------------------------------------------------------------
29+
*/
30+
31+
namespace tests\units;
32+
33+
use Flyvemdm\Tests\CommonTestCase;
34+
35+
class PluginFlyvemdmPolicyApn extends CommonTestCase {
36+
37+
private $dataField = [
38+
'group' => 'connectivity',
39+
'symbol' => 'apnConfiguration',
40+
'type' => 'apn',
41+
'type_data' => '',
42+
'unicity' => '0',
43+
];
44+
45+
protected function validationProvider() {
46+
return [
47+
'Check apn_name is not set' => [
48+
'data' => [[], null, null],
49+
'expected' => [false, 'APN name is mandatory'],
50+
],
51+
'Check apn_name is not empty' => [
52+
'data' => [['apn_name' => ''], null, null],
53+
'expected' => [false, 'APN name is mandatory'],
54+
],
55+
'Check apn_fqn is not set' => [
56+
'data' => [['apn_name' => 'lorem'], null, null],
57+
'expected' => [false, 'APN value is mandatory'],
58+
],
59+
'Check apn_fqn is not empty' => [
60+
'data' => [['apn_name' => 'lorem', 'apn_fqn' => ''], null, null],
61+
'expected' => [false, 'APN value is mandatory'],
62+
],
63+
'Valid check 1' => [
64+
'data' => [['apn_name' => 'lorem', 'apn_fqn' => 'ipsum'], null, null],
65+
'expected' => [true],
66+
],
67+
];
68+
}
69+
70+
/**
71+
* @dataProvider validationProvider
72+
* @tags testCreatePolicy
73+
* @param array $data
74+
* @param array $expected
75+
*/
76+
public function testCreatePolicy($data, $expected) {
77+
list($policy) = $this->createNewPolicyInstance();
78+
$success = $policy->integrityCheck($data[0], $data[1], $data[2]);
79+
$this->boolean($success)->isEqualTo($expected[0]);
80+
if (!$expected[0]) {
81+
$this->string($_SESSION["MESSAGE_AFTER_REDIRECT"][0][0])->isEqualTo($expected[1]);
82+
unset($_SESSION["MESSAGE_AFTER_REDIRECT"]); // to clear the buffer
83+
}
84+
}
85+
86+
private function createNewPolicyInstance() {
87+
$policyData = new \PluginFlyvemdmPolicy();
88+
$policyData->fields = $this->dataField;
89+
$policy = $this->newTestedInstance($policyData);
90+
return [$policy, $policyData];
91+
}
92+
93+
/**
94+
* @tags testGetBrokerMessage
95+
*/
96+
public function testGetBrokerMessage() {
97+
list($policy) = $this->createNewPolicyInstance();
98+
99+
$this->boolean($policy->getBrokerMessage(null, null, null))->isFalse();
100+
$value = '{"apn_name":"lorem","apn_fqn":"ipsum","apn_auth_type":"0","apn_type":"0"}';
101+
$result = $policy->getBrokerMessage($value, null, null);
102+
$this->array($result)->hasKeys([$this->dataField['symbol']])
103+
->string($result[$this->dataField['symbol']])->contains('"apn_name":"lorem","apn_fqn":"ipsum"');
104+
}
105+
106+
/**
107+
* @tags testShowValueInput
108+
*/
109+
public function testShowValueInput() {
110+
list($policy) = $this->createNewPolicyInstance();
111+
$value = $policy->showValueInput();
112+
$this->string($value)
113+
->contains('input type="text" name="value[apn_name]" value=""')
114+
->contains('input type="text" name="value[apn_fqn]" value=""');
115+
116+
$dropdowns = ['apn_auth_type', 'apn_type'];
117+
foreach ($dropdowns as $inputName) {
118+
$matches = null;
119+
preg_match('/.*<select[^>]*name=\'value\[' . $inputName . '\]\'[^>]*>.*/',
120+
$value, $matches);
121+
$this->array($matches)->hasSize(1);
122+
}
123+
}
124+
125+
/**
126+
* @tags testShowValue
127+
*/
128+
public function testShowValue() {
129+
list($policy) = $this->createNewPolicyInstance();
130+
$mockInstance = $this->newMockInstance('\PluginFlyvemdmTask');
131+
$mockInstance->getMockController()->getField = '{"apn_name":"lorem","apn_fqn":"ipsum","apn_auth_type":"0","apn_type":"0"}';
132+
$this->string($policy->showValue($mockInstance))->isEqualTo('lorem');
133+
}
134+
}

0 commit comments

Comments
 (0)