Skip to content

Commit 99e7962

Browse files
committed
Codeception PHPUnit Tests
1 parent 1ea6298 commit 99e7962

12 files changed

Lines changed: 520 additions & 50 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ vendor/*
22
composer.lock
33
terminaldata/*
44
firewalldata/*
5-
composer.install
5+
composer.install
6+
tests/firewalldata/*

‎codeception.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
suites:
2+
unit:
3+
path: .
4+
5+
settings:
6+
shuffle: true
7+
lint: true
8+
paths:
9+
tests: tests
10+
output: tests/_output
11+
support: tests/_support
12+
data: tests
13+
params:
14+
- testconfig.php
15+
modules:
16+
enabled:
17+
- \Helper\Phpfirewall

‎docs/wiki‎

Submodule wiki updated from d165570 to 0e33cca

‎src/Base.php‎

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ public function __construct($createRoot = false, $dataPath = null)
8989
"folder_permissions" => 0777
9090
];
9191

92-
$this->firewallConfigStore = new Store("firewall_config", $this->databaseDirectory, $this->storeConfiguration);
93-
94-
$this->firewallFiltersStore = new Store("firewall_filters", $this->databaseDirectory, $this->storeConfiguration);
95-
96-
$this->firewallFiltersDefaultStore = new Store("firewall_filters_default", $this->databaseDirectory, $this->storeConfiguration);
92+
$this->initStores();
9793

9894
$this->getConfig();
9995

@@ -128,6 +124,15 @@ public function __construct($createRoot = false, $dataPath = null)
128124
}
129125
}
130126

127+
public function initStores()
128+
{
129+
$this->firewallConfigStore = new Store("firewall_config", $this->databaseDirectory, $this->storeConfiguration);
130+
131+
$this->firewallFiltersStore = new Store("firewall_filters", $this->databaseDirectory, $this->storeConfiguration);
132+
133+
$this->firewallFiltersDefaultStore = new Store("firewall_filters_default", $this->databaseDirectory, $this->storeConfiguration);
134+
}
135+
131136
public function getFirewallConfig()
132137
{
133138
$this->getConfig();
@@ -313,62 +318,86 @@ public function setConfigIp2locationKey($key)
313318

314319
public function setIp2locationBinFileCode($fileCode)
315320
{
316-
if ($fileCode === '') {
321+
if ($fileCode === 'null') {
322+
$fileCode = null;
323+
} else {
324+
$fileCode = strtoupper($fileCode);
325+
}
326+
327+
if ($fileCode &&
328+
($fileCode === '' ||
329+
($fileCode !== 'DB3LITEBINIPV6' && $fileCode !== 'DB3BINIPV6')
330+
)
331+
) {
317332
$this->addResponse('Please provide correct file code.', 1);
318333

319334
return false;
320335
}
321336

322-
if ($fileCode === 'null') {
323-
$fileCode = null;
324-
}
325-
326-
return $this->updateConfig(['ip2location_bin_file_code' => strtoupper($fileCode)]);
337+
return $this->updateConfig(['ip2location_bin_file_code' => $fileCode]);
327338
}
328339

329340
public function setIp2locationBinAccessMode($accessMode)
330341
{
331-
if ($accessMode === '') {
332-
$this->addResponse('Please provide correct file mode.', 1);
333-
334-
return false;
335-
}
336-
337342
if ($accessMode === 'null') {
338343
$accessMode = null;
344+
} else {
345+
$accessMode = strtoupper($accessMode);
339346
}
340347

341-
return $this->updateConfig(['ip2location_bin_access_mode' => strtoupper($accessMode)]);
348+
if ($accessMode &&
349+
($accessMode === '' ||
350+
($accessMode !== 'SHARED_MEMORY' && $accessMode !== 'MEMORY_CACHE' && $accessMode !== 'FILE_IO')
351+
)
352+
) {
353+
$this->addResponse('Please provide correct access mode.', 1);
354+
355+
return false;
356+
}
357+
358+
return $this->updateConfig(['ip2location_bin_access_mode' => $accessMode]);
342359
}
343360

344361
public function setIp2locationProxyBinFileCode($fileCode)
345362
{
346-
if ($fileCode === '') {
363+
if ($fileCode === 'null') {
364+
$fileCode = null;
365+
} else {
366+
$fileCode = strtoupper($fileCode);
367+
}
368+
369+
if ($fileCode &&
370+
($fileCode === '' ||
371+
($fileCode !== 'PX3BIN' && $fileCode !== 'PX3LITEBIN')
372+
)
373+
) {
347374
$this->addResponse('Please provide correct proxy file code.', 1);
348375

349376
return false;
350377
}
351378

352-
if ($fileCode === 'null') {
353-
$fileCode = null;
354-
}
355-
356-
return $this->updateConfig(['ip2location_proxy_bin_file_code' => strtoupper($fileCode)]);
379+
return $this->updateConfig(['ip2location_proxy_bin_file_code' => $fileCode]);
357380
}
358381

359382
public function setIp2locationProxyBinAccessMode($accessMode)
360383
{
361-
if ($accessMode === '') {
362-
$this->addResponse('Please provide correct proxy file mode.', 1);
363-
364-
return false;
365-
}
366-
367384
if ($accessMode === 'null') {
368385
$accessMode = null;
386+
} else {
387+
$accessMode = strtoupper($accessMode);
388+
}
389+
390+
if ($accessMode &&
391+
($accessMode === '' ||
392+
($accessMode !== 'SHARED_MEMORY' && $accessMode !== 'MEMORY_CACHE' && $accessMode !== 'FILE_IO')
393+
)
394+
) {
395+
$this->addResponse('Please provide correct proxy access mode.', 1);
396+
397+
return false;
369398
}
370399

371-
return $this->updateConfig(['ip2location_proxy_bin_access_mode' => strtoupper($accessMode)]);
400+
return $this->updateConfig(['ip2location_proxy_bin_access_mode' => $accessMode]);
372401
}
373402

374403
public function setConfigIp2locationIoKey($key)
@@ -415,7 +444,7 @@ public function setIp2locationPrimaryLookupMethod($lookupMethod)
415444
return false;
416445
}
417446

418-
return $this->updateConfig(['ip2location_primary_lookup_method' => strtoupper($lookupMethod)]);
447+
return $this->updateConfig(['ip2location_primary_lookup_method' => $lookupMethod]);
419448
}
420449

421450
public function setConfigIp2locationBinDownloadDate()

‎src/Firewall.php‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public function __construct($createRoot = false, $dataPath = null)
2525
{
2626
parent::__construct($createRoot, $dataPath);
2727

28-
$this->geo = new Geo($this);
28+
$this->geo = new Geo($this, $dataPath);
2929

30-
$this->indexes = new Indexes($this);
30+
$this->indexes = new Indexes($this, $dataPath);
3131

32-
$this->ip2location = new Ip2location($this);
32+
$this->ip2location = new Ip2location($this, $dataPath);
3333
}
3434

3535
public function getFiltersCount($defaultStore = false)
@@ -350,6 +350,8 @@ public function getFilterByParentId($id)
350350

351351
public function addFilter(array $data, $defaultStore = false)
352352
{
353+
$data = $this->normalizeFilterData($data);
354+
353355
if (!isset($data['filter_type']) ||
354356
(isset($data['filter_type']) &&
355357
($data['filter_type'] !== 'allow' &&
@@ -500,8 +502,26 @@ public function addFilter(array $data, $defaultStore = false)
500502
return $newFilter;
501503
}
502504

505+
protected function normalizeFilterData($data)
506+
{
507+
$filterFields =
508+
[
509+
'id', 'filter_type', 'address_type', 'address', 'ip_hits', 'hit_count', 'updated_by', 'updated_at', 'ip2location_proxy'
510+
];
511+
512+
array_walk($data, function($value, $index) use (&$data, $filterFields) {
513+
if (!in_array($index, $filterFields)) {
514+
unset($data[$index]);
515+
}
516+
});
517+
518+
return $data;
519+
}
520+
503521
public function updateFilter(array $data, $defaultStore = false)
504522
{
523+
$data = $this->normalizeFilterData($data);
524+
505525
if (!isset($data['id'])) {
506526
$this->addResponse('Please provide correct filter ID', 1);
507527

‎src/Geo.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ class Geo
1919

2020
protected $firewallGeoCitiesStore;
2121

22-
public function __construct(Firewall $firewall)
22+
public function __construct(Firewall $firewall, $dataPath = null)
2323
{
2424
$this->firewall = $firewall;
2525

26-
if (str_contains(__DIR__, '/vendor/')) {
27-
$this->dataPath = $this->firewall->dataPath . 'geodata';
26+
if ($dataPath) {
27+
$this->dataPath = $dataPath . '/geodata';
2828
} else {
29-
$this->dataPath = fwbase_path($this->firewall->dataPath . 'geodata');
29+
if (str_contains(__DIR__, '/vendor/')) {
30+
$this->dataPath = $this->firewall->dataPath . 'geodata';
31+
} else {
32+
$this->dataPath = fwbase_path($this->firewall->dataPath . 'geodata');
33+
}
3034
}
3135

3236
$this->checkGeoPath();

‎src/Indexes.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ class Indexes
1212

1313
protected $firewall;
1414

15-
public function __construct(Firewall $firewall)
15+
public function __construct(Firewall $firewall, $dataPath = null)
1616
{
1717
$this->firewall = $firewall;
1818

19-
if (str_contains(__DIR__, '/vendor/')) {
20-
$this->dataPath = $this->firewall->dataPath . 'indexes';
19+
if ($dataPath) {
20+
$this->dataPath = $dataPath . '/indexes';
2121
} else {
22-
$this->dataPath = fwbase_path($this->firewall->dataPath . 'indexes');
22+
if (str_contains(__DIR__, '/vendor/')) {
23+
$this->dataPath = $this->firewall->dataPath . 'indexes';
24+
} else {
25+
$this->dataPath = fwbase_path($this->firewall->dataPath . 'indexes');
26+
}
2327
}
2428

2529
$this->checkIndexesPath();

‎src/Ip2location.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Ip2location
2626

2727
protected $firewall;
2828

29-
public function __construct(Firewall $firewall)
29+
public function __construct(Firewall $firewall, $dataPath = null)
3030
{
3131
$this->firewall = $firewall;
3232

@@ -40,10 +40,14 @@ public function __construct(Firewall $firewall)
4040
$this->ipGeoLocation = new IPGeolocation($ip2locationIoConfiguration);
4141
}
4242

43-
if (str_contains(__DIR__, '/vendor/')) {
44-
$this->dataPath = $this->firewall->dataPath . 'ip2locationdata';
43+
if ($dataPath) {
44+
$this->dataPath = $dataPath . '/ip2locationdata';
4545
} else {
46-
$this->dataPath = fwbase_path($this->firewall->dataPath . 'ip2locationdata');
46+
if (str_contains(__DIR__, '/vendor/')) {
47+
$this->dataPath = $this->firewall->dataPath . 'ip2locationdata';
48+
} else {
49+
$this->dataPath = fwbase_path($this->firewall->dataPath . 'ip2locationdata');
50+
}
4751
}
4852

4953
$this->checkIp2locationPath();
@@ -366,7 +370,7 @@ protected function checkIPIsPublic($ip)
366370
return true;
367371
}
368372

369-
protected function initStores()
373+
public function initStores()
370374
{
371375
$this->firewallFiltersIp2locationStore = new Store("firewall_filters_ip2location", $this->firewall->databaseDirectory, $this->firewall->storeConfiguration);
372376
}

0 commit comments

Comments
 (0)