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