/tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTest.php

https://github.com/joomla/joomla-cms · PHP · 220 lines · 126 code · 35 blank · 59 comment · 2 complexity · 625b6dd4f942e511810bae4af323f50e MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Extension
  5. *
  6. * @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. namespace Joomla\Tests\Unit\Plugin\Task\Checkfiles\Extension;
  10. use Joomla\CMS\Application\CMSApplicationInterface;
  11. use Joomla\CMS\Language\Language;
  12. use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
  13. use Joomla\Component\Scheduler\Administrator\Task\Status;
  14. use Joomla\Component\Scheduler\Administrator\Task\Task;
  15. use Joomla\Event\Dispatcher;
  16. use Joomla\Filesystem\Folder;
  17. use Joomla\Plugin\Task\Checkfiles\Extension\Checkfiles;
  18. use Joomla\Tests\Unit\UnitTestCase;
  19. /**
  20. * Test class for Checkfiles plugin
  21. *
  22. * @package Joomla.UnitTest
  23. * @subpackage Checkfiles
  24. *
  25. * @testdox The Checkfiles plugin
  26. *
  27. * @since 4.2.0
  28. */
  29. class CheckfilesPluginTest extends UnitTestCase
  30. {
  31. /**
  32. * Setup
  33. *
  34. * @return void
  35. *
  36. * @since 4.2.0
  37. */
  38. public function setUp(): void
  39. {
  40. if (!is_dir(__DIR__ . '/tmp')) {
  41. mkdir(__DIR__ . '/tmp');
  42. }
  43. $image = imagecreate(200, 200);
  44. imagecolorallocate($image, 255, 255, 0);
  45. imagepng($image, __DIR__ . '/tmp/test.png');
  46. imagedestroy($image);
  47. }
  48. /**
  49. * Cleanup
  50. *
  51. * @return void
  52. *
  53. * @since 4.2.0
  54. */
  55. public function tearDown(): void
  56. {
  57. if (is_dir(__DIR__ . '/tmp')) {
  58. Folder::delete(__DIR__ . '/tmp');
  59. }
  60. }
  61. /**
  62. * @testdox can resize an image
  63. *
  64. * @return void
  65. *
  66. * @since 4.2.0
  67. */
  68. public function testResize()
  69. {
  70. $language = $this->createStub(Language::class);
  71. $language->method('_')->willReturn('test');
  72. $app = $this->createStub(CMSApplicationInterface::class);
  73. $app->method('getLanguage')->willReturn($language);
  74. $plugin = new Checkfiles(new Dispatcher(), [], __DIR__);
  75. $plugin->setApplication($app);
  76. $task = $this->createStub(Task::class);
  77. $task->method('get')->willReturnMap([['id', null, 1], ['type', null, 'checkfiles.imagesize']]);
  78. $event = new ExecuteTaskEvent(
  79. 'test',
  80. [
  81. 'subject' => $task,
  82. 'params' => (object)['path' => '/tmp', 'dimension' => 'width', 'limit' => 20, 'numImages' => 1]
  83. ]
  84. );
  85. $plugin->standardRoutineHandler($event);
  86. $this->assertEquals(Status::OK, $event->getResultSnapshot()['status']);
  87. list($width, $height) = getimagesize(__DIR__ . '/tmp/test.png');
  88. $this->assertEquals(20, $width);
  89. $this->assertEquals(20, $height);
  90. }
  91. /**
  92. * @testdox can resize a subset of images
  93. *
  94. * @return void
  95. *
  96. * @since 4.2.0
  97. */
  98. public function testResizeWithLimit()
  99. {
  100. copy(__DIR__ . '/tmp/test.png', __DIR__ . '/tmp/test1.png');
  101. $language = $this->createStub(Language::class);
  102. $language->method('_')->willReturn('test');
  103. $app = $this->createStub(CMSApplicationInterface::class);
  104. $app->method('getLanguage')->willReturn($language);
  105. $plugin = new Checkfiles(new Dispatcher(), [], __DIR__);
  106. $plugin->setApplication($app);
  107. $task = $this->createStub(Task::class);
  108. $task->method('get')->willReturnMap([['id', null, 1], ['type', null, 'checkfiles.imagesize']]);
  109. $event = new ExecuteTaskEvent(
  110. 'test',
  111. [
  112. 'subject' => $task,
  113. 'params' => (object)['path' => '/tmp', 'dimension' => 'width', 'limit' => 20, 'numImages' => 1]
  114. ]
  115. );
  116. $plugin->standardRoutineHandler($event);
  117. $this->assertEquals(Status::OK, $event->getResultSnapshot()['status']);
  118. list($width, $height) = getimagesize(__DIR__ . '/tmp/test.png');
  119. $this->assertEquals(20, $width);
  120. $this->assertEquals(20, $height);
  121. list($width, $height) = getimagesize(__DIR__ . '/tmp/test1.png');
  122. $this->assertEquals(200, $width);
  123. $this->assertEquals(200, $height);
  124. }
  125. /**
  126. * @testdox can resize an image
  127. *
  128. * @return void
  129. *
  130. * @since 4.2.0
  131. */
  132. public function testIgnoreResize()
  133. {
  134. $language = $this->createStub(Language::class);
  135. $language->method('_')->willReturn('test');
  136. $app = $this->createStub(CMSApplicationInterface::class);
  137. $app->method('getLanguage')->willReturn($language);
  138. $plugin = new Checkfiles(new Dispatcher(), [], __DIR__);
  139. $plugin->setApplication($app);
  140. $task = $this->createStub(Task::class);
  141. $task->method('get')->willReturnMap([['id', null, 1], ['type', null, 'checkfiles.imagesize']]);
  142. $event = new ExecuteTaskEvent(
  143. 'test',
  144. [
  145. 'subject' => $task,
  146. 'params' => (object)['path' => '/tmp', 'dimension' => 'width', 'limit' => 2000, 'numImages' => 1]
  147. ]
  148. );
  149. $plugin->standardRoutineHandler($event);
  150. $this->assertEquals(Status::OK, $event->getResultSnapshot()['status']);
  151. list($width, $height) = getimagesize(__DIR__ . '/tmp/test.png');
  152. $this->assertEquals(200, $width);
  153. $this->assertEquals(200, $height);
  154. }
  155. /**
  156. * @testdox can not run when invalid folder
  157. *
  158. * @return void
  159. *
  160. * @since 4.2.0
  161. */
  162. public function testInvalidFolder()
  163. {
  164. $language = $this->createStub(Language::class);
  165. $language->method('_')->willReturn('test');
  166. $app = $this->createStub(CMSApplicationInterface::class);
  167. $app->method('getLanguage')->willReturn($language);
  168. $plugin = new Checkfiles(new Dispatcher(), [], __DIR__);
  169. $plugin->setApplication($app);
  170. $task = $this->createStub(Task::class);
  171. $task->method('get')->willReturnMap([['id', null, 1], ['type', null, 'checkfiles.imagesize']]);
  172. $event = new ExecuteTaskEvent(
  173. 'test',
  174. [
  175. 'subject' => $task,
  176. 'params' => (object)['path' => '/invalid', 'dimension' => 'width', 'limit' => 20, 'numImages' => 1]
  177. ]
  178. );
  179. $plugin->standardRoutineHandler($event);
  180. list($width, $height) = getimagesize(__DIR__ . '/tmp/test.png');
  181. $this->assertEquals(Status::NO_RUN, $event->getResultSnapshot()['status']);
  182. $this->assertEquals(200, $width);
  183. $this->assertEquals(200, $height);
  184. }
  185. }