1717use Joomla \Component \Scheduler \Administrator \Task \Status ;
1818use Joomla \Component \Scheduler \Administrator \Task \Task ;
1919use Joomla \Event \Dispatcher ;
20+ use Joomla \Filesystem \Folder ;
2021use Joomla \Http \HttpFactory ;
2122use Joomla \Http \TransportInterface ;
2223use Joomla \Plugin \Task \Requests \Extension \Requests ;
@@ -44,9 +45,9 @@ class RequestsPluginTest extends UnitTestCase
4445 */
4546 public function setUp (): void
4647 {
47- if (file_exists ( JPATH_ROOT . '/tmp/task_1_response.html ' ))
48+ if (is_dir ( __DIR__ . '/tmp ' ))
4849 {
49- unlink ( JPATH_ROOT . '/tmp/task_1_response.html ' );
50+ Folder:: delete ( __DIR__ . '/tmp ' );
5051 }
5152 }
5253
@@ -59,9 +60,9 @@ public function setUp(): void
5960 */
6061 public function tearDown (): void
6162 {
62- if (file_exists ( JPATH_ROOT . '/tmp/task_1_response.html ' ))
63+ if (is_dir ( __DIR__ . '/tmp ' ))
6364 {
64- unlink ( JPATH_ROOT . '/tmp/task_1_response.html ' );
65+ Folder:: delete ( __DIR__ . '/tmp ' );
6566 }
6667 }
6768
@@ -98,7 +99,7 @@ public static function isSupported()
9899 $ app = $ this ->createStub (CMSApplicationInterface::class);
99100 $ app ->method ('getLanguage ' )->willReturn ($ this ->createStub (Language::class));
100101
101- $ plugin = new Requests (new Dispatcher , [], $ factory );
102+ $ plugin = new Requests (new Dispatcher , [], $ factory, __DIR__ . ' /tmp ' );
102103 $ plugin ->setApplication ($ app );
103104
104105 $ task = $ this ->createStub (Task::class);
@@ -114,8 +115,9 @@ public static function isSupported()
114115 $ plugin ->standardRoutineHandler ($ event );
115116
116117 $ this ->assertEquals (Status::OK , $ event ->getResultSnapshot ()['status ' ]);
118+ $ this ->assertStringContainsString ('SAVED ' , $ event ->getResultSnapshot ()['output ' ]);
117119 $ this ->assertEquals ('http://example.com ' , $ transport ->url );
118- $ this ->assertStringEqualsFile (JPATH_ROOT . '/tmp/task_1_response.html ' , 'test ' );
120+ $ this ->assertStringEqualsFile (__DIR__ . '/tmp/task_1_response.html ' , 'test ' );
119121 }
120122
121123 /**
@@ -151,7 +153,7 @@ public static function isSupported()
151153 $ app = $ this ->createStub (CMSApplicationInterface::class);
152154 $ app ->method ('getLanguage ' )->willReturn ($ this ->createStub (Language::class));
153155
154- $ plugin = new Requests (new Dispatcher , [], $ factory );
156+ $ plugin = new Requests (new Dispatcher , [], $ factory, __DIR__ . ' /tmp ' );
155157 $ plugin ->setApplication ($ app );
156158
157159 $ task = $ this ->createStub (Task::class);
@@ -167,8 +169,9 @@ public static function isSupported()
167169 $ plugin ->standardRoutineHandler ($ event );
168170
169171 $ this ->assertEquals (Status::KNOCKOUT , $ event ->getResultSnapshot ()['status ' ]);
172+ $ this ->assertStringContainsString ('SAVED ' , $ event ->getResultSnapshot ()['output ' ]);
170173 $ this ->assertEquals ('http://example.com ' , $ transport ->url );
171- $ this ->assertStringEqualsFile (JPATH_ROOT . '/tmp/task_1_response.html ' , 'test ' );
174+ $ this ->assertStringEqualsFile (__DIR__ . '/tmp/task_1_response.html ' , 'test ' );
172175 }
173176
174177 /**
@@ -204,7 +207,7 @@ public static function isSupported()
204207 $ app = $ this ->createStub (CMSApplicationInterface::class);
205208 $ app ->method ('getLanguage ' )->willReturn ($ this ->createStub (Language::class));
206209
207- $ plugin = new Requests (new Dispatcher , [], $ factory );
210+ $ plugin = new Requests (new Dispatcher , [], $ factory, __DIR__ . ' /tmp ' );
208211 $ plugin ->setApplication ($ app );
209212
210213 $ task = $ this ->createStub (Task::class);
@@ -254,7 +257,7 @@ public static function isSupported()
254257 $ app = $ this ->createStub (CMSApplicationInterface::class);
255258 $ app ->method ('getLanguage ' )->willReturn ($ language );
256259
257- $ plugin = new Requests (new Dispatcher , [], $ factory );
260+ $ plugin = new Requests (new Dispatcher , [], $ factory, __DIR__ . ' /tmp ' );
258261 $ plugin ->setApplication ($ app );
259262
260263 $ task = $ this ->createStub (Task::class);
@@ -271,4 +274,54 @@ public static function isSupported()
271274
272275 $ this ->assertEquals (Status::TIMEOUT , $ event ->getResultSnapshot ()['status ' ]);
273276 }
277+ /**
278+ * @testdox can handle an invalid file location
279+ *
280+ * @return void
281+ *
282+ * @since __DEPLOY_VERSION__
283+ */
284+ public function testInvalidFileToWrite ()
285+ {
286+ $ transport = new class implements TransportInterface
287+ {
288+ public function request ($ method , UriInterface $ uri , $ data = null , array $ headers = [], $ timeout = null , $ userAgent = null )
289+ {
290+ return (object )['code ' => 200 , 'body ' => 'test ' ];
291+ }
292+
293+ public static function isSupported ()
294+ {
295+ return true ;
296+ }
297+ };
298+
299+ $ http = new Http ([], $ transport );
300+ $ factory = $ this ->createStub (HttpFactory::class);
301+ $ factory ->method ('getHttp ' )->willReturn ($ http );
302+
303+ $ language = $ this ->createStub (Language::class);
304+ $ language ->method ('_ ' )->willReturn ('test ' );
305+
306+ $ app = $ this ->createStub (CMSApplicationInterface::class);
307+ $ app ->method ('getLanguage ' )->willReturn ($ language );
308+
309+ $ plugin = new Requests (new Dispatcher , [], $ factory , '/invalid ' );
310+ $ plugin ->setApplication ($ app );
311+
312+ $ task = $ this ->createStub (Task::class);
313+ $ task ->method ('get ' )->willReturnMap ([['id ' , null , 1 ], ['type ' , null , 'plg_task_requests_task_get ' ]]);
314+
315+ $ event = new ExecuteTaskEvent (
316+ 'test ' ,
317+ [
318+ 'subject ' => $ task ,
319+ 'params ' => (object )['url ' => 'http://example.com ' , 'timeout ' => 0 , 'auth ' => 0 , 'authType ' => '' , 'authKey ' => '' ]
320+ ]
321+ );
322+ $ plugin ->standardRoutineHandler ($ event );
323+
324+ $ this ->assertEquals (Status::OK , $ event ->getResultSnapshot ()['status ' ]);
325+ $ this ->assertStringContainsString ('NOT_SAVED ' , $ event ->getResultSnapshot ()['output ' ]);
326+ }
274327}
0 commit comments