Skip to content

Commit 6f40633

Browse files
committed
added unit testing for BackendHelpers class
1 parent d92887d commit 6f40633

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="../../../tests/bootstrap.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Plugin Unit Test Suite">
15+
<directory>./tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<php>
19+
<env name="APP_ENV" value="testing"/>
20+
<env name="CACHE_DRIVER" value="array"/>
21+
<env name="SESSION_DRIVER" value="array"/>
22+
</php>
23+
</phpunit>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Martin\Forms\Tests\Classes;
4+
5+
use Backend;
6+
use BackendAuth;
7+
use PluginTestCase;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use Illuminate\Support\Facades\Auth;
10+
use System\Classes\PluginManager;
11+
use Backend\Models\User;
12+
use Martin\Forms\Classes\BackendHelpers;
13+
14+
class BackendHelpersTest extends PluginTestCase {
15+
16+
use RefreshDatabase;
17+
18+
public function setUp() {
19+
parent::setUp();
20+
PluginManager::instance()->bootAll(true);
21+
}
22+
23+
/**
24+
* @testdox Get backend URL
25+
*/
26+
public function testGetBackendUrl() {
27+
$this->_loginUser();
28+
$expect = Backend::url("martin/forms/records");
29+
$bh = new BackendHelpers();
30+
$this->assertEquals($expect, $bh->getBackendURL([
31+
'martin.forms.access_records' => 'martin/forms/records',
32+
'martin.forms.access_exports' => 'martin/forms/exports'
33+
], 'martin.forms.access_records'));
34+
}
35+
36+
/**
37+
* @testdox Convert PHP array to HTML list
38+
*/
39+
public function testArray2Ul() {
40+
$list = [
41+
'item1' => 'Item 1',
42+
'item2' => ['item21' => 'Item 2.1', 'item22' => 'Item 2.2', 'item23' => 'Item 2.3'],
43+
'item3' => 'Item 3',
44+
];
45+
$expected = '<li>Item 1</li><li>item2<ul><li>Item 2.1</li><li>Item 2.2</li><li>Item 2.3</li></ul></li><li>Item 3</li>';
46+
$bh = new BackendHelpers();
47+
$this->assertEquals($expected, $bh->array2ul($list));
48+
}
49+
50+
/**
51+
* @testdox Anonymize IPv4 address
52+
*/
53+
public function testAnonymizeIPv4() {
54+
$bh = new BackendHelpers();
55+
$this->assertEquals('8.8.8.0', $bh->anonymizeIPv4('8.8.8.8'));
56+
57+
}
58+
59+
/**
60+
* @testdox Replace string containing curly braces
61+
*/
62+
public function testReplaceTokenValid() {
63+
$bh = new BackendHelpers();
64+
$this->assertEquals('includes 50 string', $bh->replaceToken('record.id', '50', 'includes {{ record.id }} string'));
65+
}
66+
67+
/**
68+
* @testdox Replace string not containing curly braces
69+
*/
70+
public function testReplaceTokenNoBraces() {
71+
$bh = new BackendHelpers();
72+
$this->assertEquals('includes record.id string', $bh->replaceToken('record.id', '50', 'includes record.id string'));
73+
}
74+
75+
76+
/**
77+
* Login backend user
78+
*
79+
* @return void
80+
*/
81+
private static function _loginUser() {
82+
$user = User::create([
83+
'email' => 'testuser@testcompany.com',
84+
'login' => 'testuser',
85+
'password' => 'superpassword',
86+
'password_confirmation' => 'superpassword',
87+
]);
88+
BackendAuth::login($user);
89+
}
90+
91+
}

0 commit comments

Comments
 (0)