PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / exec-with-fallback / tests / ExecWithFallbackTest.php

ExecWithFallbackTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/rosell-dk/exec-with-fallback/tests/ExecWithFallbackTest.php

182 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ExecWithFallback;
3
4 class ExecWithFallbackWithSetMethods extends ExecWithFallback
5 {
6
7 public static function setMethods($methods)
8 {
9 self::$methods = $methods;
10 }
11
12 public static function resetMethods()
13 {
14 self::$methods = ['exec', 'passthru', 'popen', 'proc_open', 'shell_exec'];
15 }
16 }
17
18 namespace ExecWithFallback\Tests;
19
20 use PHPUnit\Framework\TestCase;
21 use ExecWithFallback\ExecWithFallback;
22 use ExecWithFallback\ExecWithFallbackWithSetMethods;
23
24
25 class ExecWithFallbackTest extends BaseTest
26 {
27 public $className = 'ExecWithFallback';
28
29 public function isAvailable()
30 {
31 return
32 ExecWithFallback::functionEnabled('exec') ||
33 ExecWithFallback::functionEnabled('proc_open') ||
34 ExecWithFallback::functionEnabled('passthru') ||
35 ExecWithFallback::functionEnabled('popen') ||
36 ExecWithFallback::functionEnabled('shell_exec');
37 }
38
39 public function runExec($command, &$output = null, &$result_code = null)
40 {
41 return ExecWithFallback::exec($command, $output, $result_code);
42 }
43
44 public function testFunctionEnabled()
45 {
46 $this->assertTrue(ExecWithFallback::functionEnabled('function_exists'));
47 $this->assertFalse(ExecWithFallback::functionEnabled('atoehutaoeut'));
48
49 $d = ini_get('disable_functions');
50 if (function_exists('ini_set') && ini_set('disable_functions', 'passthru')) {
51 $this->assertFalse(ExecWithFallback::functionEnabled('passthru'));
52 ini_set('disable_functions', $d);
53 }
54 $d = ini_get('suhosin.executor.func.blacklist');
55 if (function_exists('ini_set') && ini_set('suhosin.executor.func.blacklist', 'passthru')) {
56 $this->assertFalse(ExecWithFallback::functionEnabled('passthru'));
57 ini_set('suhosin.executor.func.blacklist', $d);
58 }
59 }
60
61 public function testRunExec()
62 {
63 if (ExecWithFallback::functionEnabled('exec')) {
64 $result = ExecWithFallback::runExec('exec', 'echo hi');
65 $this->assertEquals('hi', $result);
66 } elseif (ExecWithFallback::functionEnabled('passthru')) {
67 $result = ExecWithFallback::runExec('passthru', 'echo hi');
68 $this->assertEquals('hi', $result);
69 } else {
70 $this->assertTrue(true);
71 }
72
73 /*
74 if (ExecWithFallback::functionEnabled('shell_exec')) {
75 $result = ExecWithFallback::runExec('shell_exec', 'echo hi', $output, $result_code);
76 $this->assertFalse($result);
77 }*/
78 }
79
80 public function testAnyMethodAvailable()
81 {
82 $methods = ['exec', 'passthru', 'popen', 'proc_open', 'shell_exec'];
83 $anyAvailable = false;
84 $anyOtherThanShellExecAvailable = false;
85 foreach ($methods as $method) {
86 if (ExecWithFallback::functionEnabled($method)) {
87 $anyAvailable = true;
88 if ($method != 'shell_exec') {
89 $anyOtherThanShellExecAvailable = true;
90 }
91 }
92 }
93
94 $this->assertSame($anyAvailable, ExecWithFallback::anyAvailable(false));
95 $this->assertSame($anyOtherThanShellExecAvailable, ExecWithFallback::anyAvailable(true));
96 $this->assertSame($anyOtherThanShellExecAvailable, ExecWithFallback::anyAvailable());
97 }
98
99 public function testExec()
100 {
101 if (ExecWithFallback::anyAvailable()) {
102 $result = ExecWithFallback::exec('echo hi', $output);
103 $this->assertSame('hi', $result);
104 } else {
105 $exceptionThrown = true;
106 try {
107 $result = ExecWithFallback::exec('echo hi', $output);
108 } catch (\Exception $e) {
109 $exceptionThrown = true;
110 }
111 $this->assertTrue($exceptionThrown);
112 }
113
114 }
115
116 public function testExecNoMethods()
117 {
118 ExecWithFallbackWithSetMethods::setMethods([]);
119 $exceptionThrown = true;
120 try {
121 $result = ExecWithFallbackWithSetMethods::exec('echo hi', $output);
122 } catch (\Exception $e) {
123 $exceptionThrown = true;
124 }
125 ExecWithFallbackWithSetMethods::resetMethods();
126 $this->assertTrue($exceptionThrown);
127 }
128
129 public function testExecOnlyShellExec()
130 {
131 ExecWithFallbackWithSetMethods::setMethods(['shell_exec']);
132 $exceptionThrown = false;
133 try {
134 // shell_exec cannot be used with 3 arguments, so an exception is expected
135 $result = ExecWithFallbackWithSetMethods::exec('echo hi', $output, $result_code);
136 } catch (\Exception $e) {
137 $exceptionThrown = true;
138 }
139 $this->assertTrue($exceptionThrown);
140
141 if (ExecWithFallback::functionEnabled('shell_exec')) {
142 // shell_exec works with 2 arguments, so no exception is expected
143 $result = ExecWithFallbackWithSetMethods::exec('echo hm', $output);
144 $this->assertSame('hm', $result);
145 }
146 ExecWithFallbackWithSetMethods::resetMethods();
147 }
148
149 public function testExecOnlyPassthru()
150 {
151 ExecWithFallbackWithSetMethods::setMethods(['passthru']);
152 if (ExecWithFallback::functionEnabled('passthru')) {
153 // shell_exec works with 2 arguments, so no exception is expected
154 $result = ExecWithFallbackWithSetMethods::exec('echo hi', $output);
155 $this->assertSame('hi', $result);
156 }
157 ExecWithFallbackWithSetMethods::resetMethods();
158 }
159
160
161 /**
162 * This may throw FATAL! (but does not do in any of the systems in our CI)
163 */
164 public function testExecNoMercyNoMethods()
165 {
166 ExecWithFallbackWithSetMethods::setMethods([]);
167
168 $exceptionThrown = false;
169 try {
170 $result = ExecWithFallbackWithSetMethods::exec('echo hi', $output);
171 } catch (\Exception $e) {
172 $exceptionThrown = true;
173 } catch (\Error $e) {
174 $exceptionThrown = true;
175 }
176 $this->assertTrue($exceptionThrown);
177 ExecWithFallbackWithSetMethods::resetMethods();
178 }
179
180
181 }
182