Skip to content

Commit 93c484d

Browse files
committed
translate and custom dir in request
1 parent 3e42cf1 commit 93c484d

File tree

5 files changed

+170
-24
lines changed

5 files changed

+170
-24
lines changed

libraries/src/Plugin/CMSPlugin.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,31 @@ public function loadLanguage($extension = '', $basePath = JPATH_ADMINISTRATOR)
186186
|| $lang->load($extension, JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name);
187187
}
188188

189+
/**
190+
* Translates the given key with the local applications language. If arguments are available, then
191+
* injects them into the translated string.
192+
*
193+
* @param string $key The key to translate
194+
* @param mixed[] $arguments The arguments
195+
*
196+
* @return string The translated string
197+
*
198+
* @since __DEPLOY_VERSION__
199+
*
200+
* @see sprintf
201+
*/
202+
protected function translate(string $key, ...$arguments): string
203+
{
204+
$language = $this->getApplication() ? $this->getApplication()->getLanguage() : Factory::getApplication()->getLanguage();
205+
206+
if ($arguments)
207+
{
208+
return sprintf($language->_($key), ...$arguments);
209+
}
210+
211+
return $language->_($key);
212+
}
213+
189214
/**
190215
* Registers legacy Listeners to the Dispatcher, emulating how plugins worked under Joomla! 3.x and below.
191216
*

plugins/task/requests/services/provider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ function (Container $container)
3838
$plugin = new Requests(
3939
$container->get(DispatcherInterface::class),
4040
(array) PluginHelper::getPlugin('task', 'requests'),
41-
new HttpFactory
41+
new HttpFactory,
42+
JPATH_ROOT . '/tmp'
4243
);
4344
$plugin->setApplication(Factory::getApplication());
4445

plugins/task/requests/src/Extension/Requests.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,30 @@ public static function getSubscribedEvents(): array
7575
*/
7676
private $httpFactory;
7777

78+
/**
79+
* The root directory
80+
*
81+
* @var string
82+
* @since __DEPLOY_VERSION__
83+
*/
84+
private $rootDirectory;
85+
7886
/**
7987
* Constructor.
8088
*
81-
* @param DispatcherInterface $dispatcher The dispatcher
82-
* @param array $config An optional associative array of configuration settings
83-
* @param HttpFactory $httpFactory The http factory
89+
* @param DispatcherInterface $dispatcher The dispatcher
90+
* @param array $config An optional associative array of configuration settings
91+
* @param HttpFactory $httpFactory The http factory
92+
* @param string $rootDirectory The root directory to store the output file in
8493
*
8594
* @since __DEPLOY_VERSION__
8695
*/
87-
public function __construct(DispatcherInterface $dispatcher, array $config, HttpFactory $httpFactory)
96+
public function __construct(DispatcherInterface $dispatcher, array $config, HttpFactory $httpFactory, string $rootDirectory)
8897
{
8998
parent::__construct($dispatcher, $config);
9099

91-
$this->httpFactory = $httpFactory;
100+
$this->httpFactory = $httpFactory;
101+
$this->rootDirectory = $rootDirectory;
92102
}
93103

94104
/**
@@ -124,7 +134,7 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
124134
}
125135
catch (Exception $e)
126136
{
127-
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT'));
137+
$this->logTask($this->translate('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT'));
128138

129139
return TaskStatus::TIMEOUT;
130140
}
@@ -133,16 +143,17 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
133143
$responseBody = $response->body;
134144

135145
// @todo this handling must be rethought and made safe. stands as a good demo right now.
136-
$responseFilename = Path::clean(JPATH_ROOT . "/tmp/task_{$id}_response.html");
146+
$responseFilename = Path::clean($this->rootDirectory . "/task_{$id}_response.html");
137147

138-
if (File::write($responseFilename, $responseBody))
148+
try
139149
{
150+
File::write($responseFilename, $responseBody);
140151
$this->snapshot['output_file'] = $responseFilename;
141152
$responseStatus = 'SAVED';
142153
}
143-
else
154+
catch (Exception $e)
144155
{
145-
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT'), 'error');
156+
$this->logTask($this->translate('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT'), 'error');
146157
$responseStatus = 'NOT_SAVED';
147158
}
148159

@@ -153,7 +164,7 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
153164
> Response: $responseStatus
154165
EOF;
155166

156-
$this->logTask(sprintf($this->getApplication()->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE'), $responseCode));
167+
$this->logTask($this->translate('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE', $responseCode));
157168

158169
if ($response->code !== 200)
159170
{

tests/Unit/Libraries/Cms/Plugin/CMSPluginTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ public function testLoadLanguageWithExtensionAndPath()
214214
}
215215

216216
/**
217-
* @testdox does not load the language whien the path exists
217+
* @testdox does not load the language when the path exists
218218
*
219219
* @return void
220220
*
221221
* @since __DEPLOY_VERSION__
222222
*/
223-
public function testNotLoadWhenExists()
223+
public function testNotLoadLanguageWhenExists()
224224
{
225225
$dispatcher = new Dispatcher;
226226
$language = $this->createMock(Language::class);
@@ -236,6 +236,62 @@ public function testNotLoadWhenExists()
236236
$plugin->loadLanguage();
237237
}
238238

239+
/**
240+
* @testdox can translate a string without arguments
241+
*
242+
* @return void
243+
*
244+
* @since __DEPLOY_VERSION__
245+
*/
246+
public function testTranslateWithoutArguments()
247+
{
248+
$dispatcher = new Dispatcher;
249+
$language = $this->createMock(Language::class);
250+
$language->method('_')->willReturn('test');
251+
252+
$app = $this->createStub(CMSApplicationInterface::class);
253+
$app->method('getLanguage')->willReturn($language);
254+
255+
$plugin = new class($dispatcher, []) extends CMSPlugin
256+
{
257+
public function test(): string
258+
{
259+
return parent::translate('unit');
260+
}
261+
};
262+
$plugin->setApplication($app);
263+
264+
$this->assertEquals('test', $plugin->test());
265+
}
266+
267+
/**
268+
* @testdox can translate a string with arguments
269+
*
270+
* @return void
271+
*
272+
* @since __DEPLOY_VERSION__
273+
*/
274+
public function testTranslateWithArguments()
275+
{
276+
$dispatcher = new Dispatcher;
277+
$language = $this->createMock(Language::class);
278+
$language->method('_')->willReturn('test %s in %s');
279+
280+
$app = $this->createStub(CMSApplicationInterface::class);
281+
$app->method('getLanguage')->willReturn($language);
282+
283+
$plugin = new class($dispatcher, []) extends CMSPlugin
284+
{
285+
public function test(): string
286+
{
287+
return parent::translate('unit', 1, 'two');
288+
}
289+
};
290+
$plugin->setApplication($app);
291+
292+
$this->assertEquals('test 1 in two', $plugin->test());
293+
}
294+
239295
/**
240296
* @testdox can register the listeners when is SubscriberInterface
241297
*

tests/Unit/Plugin/Task/Requests/Extension/RequestsPluginTest.php

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Joomla\Component\Scheduler\Administrator\Task\Status;
1818
use Joomla\Component\Scheduler\Administrator\Task\Task;
1919
use Joomla\Event\Dispatcher;
20+
use Joomla\Filesystem\Folder;
2021
use Joomla\Http\HttpFactory;
2122
use Joomla\Http\TransportInterface;
2223
use 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

Comments
 (0)