PluginAssetsCommandsTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Command;
  17. use Cake\Console\CommandInterface;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\ConsoleOptionParser;
  20. use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
  21. use Cake\Console\TestSuite\StubConsoleOutput;
  22. use Cake\Core\Configure;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Filesystem;
  25. use Mockery;
  26. use SplFileInfo;
  27. /**
  28. * PluginAssetsCommandsTest class
  29. */
  30. class PluginAssetsCommandsTest extends TestCase
  31. {
  32. use ConsoleIntegrationTestTrait;
  33. /**
  34. * @var string
  35. */
  36. protected $wwwRoot;
  37. /**
  38. * @var \Cake\Utility\Filesystem
  39. */
  40. protected $fs;
  41. /**
  42. * setUp method
  43. */
  44. public function setUp(): void
  45. {
  46. parent::setUp();
  47. $this->wwwRoot = TMP . 'assets_task_webroot' . DS;
  48. Configure::write('App.wwwRoot', $this->wwwRoot);
  49. $this->fs = new Filesystem();
  50. $this->fs->deleteDir($this->wwwRoot);
  51. $this->fs->copyDir(WWW_ROOT, $this->wwwRoot);
  52. $this->setAppNamespace();
  53. $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', []);
  54. }
  55. /**
  56. * tearDown method
  57. */
  58. public function tearDown(): void
  59. {
  60. parent::tearDown();
  61. $this->clearPlugins();
  62. }
  63. /**
  64. * testSymlink method
  65. */
  66. public function testSymlink(): void
  67. {
  68. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  69. $this->exec('plugin assets symlink');
  70. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  71. $path = $this->wwwRoot . 'test_plugin';
  72. $this->assertFileExists($path . DS . 'root.js');
  73. $this->assertTrue(is_link($path));
  74. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  75. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  76. $this->assertTrue(is_link($path));
  77. }
  78. public function testSymlinkWhenVendorDirectoryExists(): void
  79. {
  80. $this->loadPlugins(['Company/TestPluginThree']);
  81. mkdir($this->wwwRoot . 'company');
  82. $this->exec('plugin assets symlink');
  83. $this->assertExitCode(CommandInterface::CODE_SUCCESS);
  84. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  85. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  86. $this->assertTrue(is_link($path));
  87. }
  88. /**
  89. * testSymlinkWhenTargetAlreadyExits
  90. */
  91. public function testSymlinkWhenTargetAlreadyExits(): void
  92. {
  93. $this->loadPlugins(['TestTheme']);
  94. $output = new StubConsoleOutput();
  95. $io = Mockery::mock(ConsoleIo::class, [$output, $output, null, null])->makePartial();
  96. $parser = new ConsoleOptionParser('cake example');
  97. $parser->addArgument('name', ['required' => false]);
  98. $parser->addOption('overwrite', ['default' => false, 'boolean' => true]);
  99. $command = $this->getMockBuilder('Cake\Command\PluginAssetsSymlinkCommand')
  100. ->onlyMethods(['getOptionParser', '_createSymlink', '_copyDirectory'])
  101. ->getMock();
  102. $command->method('getOptionParser')->willReturn($parser);
  103. $this->assertDirectoryExists($this->wwwRoot . 'test_theme');
  104. $command->expects($this->never())->method('_createSymlink');
  105. $command->expects($this->never())->method('_copyDirectory');
  106. $command->run([], $io);
  107. }
  108. /**
  109. * test that plugins without webroot are not processed
  110. */
  111. public function testForPluginWithoutWebroot(): void
  112. {
  113. $this->loadPlugins(['TestPluginTwo']);
  114. $this->exec('plugin assets symlink');
  115. $this->assertFileDoesNotExist($this->wwwRoot . 'test_plugin_two');
  116. }
  117. /**
  118. * testSymlinkingSpecifiedPlugin
  119. */
  120. public function testSymlinkingSpecifiedPlugin(): void
  121. {
  122. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  123. $this->exec('plugin assets symlink TestPlugin');
  124. $path = $this->wwwRoot . 'test_plugin';
  125. $this->assertFileExists($path . DS . 'root.js');
  126. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  127. $this->assertDirectoryDoesNotExist($path);
  128. $this->assertFalse(is_link($path));
  129. }
  130. /**
  131. * testCopy
  132. */
  133. public function testCopy(): void
  134. {
  135. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  136. $this->exec('plugin assets copy');
  137. $path = $this->wwwRoot . 'test_plugin';
  138. $this->assertDirectoryExists($path);
  139. $this->assertFileExists($path . DS . 'root.js');
  140. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  141. $this->assertDirectoryExists($path);
  142. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  143. }
  144. /**
  145. * testCopyOverwrite
  146. */
  147. public function testCopyOverwrite(): void
  148. {
  149. $this->loadPlugins(['TestPlugin' => ['routes' => false]]);
  150. $this->exec('plugin assets copy');
  151. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot';
  152. $path = $this->wwwRoot . 'test_plugin';
  153. $dir = new SplFileInfo($path);
  154. $this->assertTrue($dir->isDir());
  155. $this->assertFileExists($path . DS . 'root.js');
  156. file_put_contents($path . DS . 'root.js', 'updated');
  157. $this->exec('plugin assets copy');
  158. $this->assertFileNotEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  159. $this->exec('plugin assets copy --overwrite');
  160. $this->assertFileEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  161. }
  162. /**
  163. * testRemoveSymlink method
  164. */
  165. public function testRemoveSymlink(): void
  166. {
  167. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  168. mkdir($this->wwwRoot . 'company');
  169. $this->exec('plugin assets symlink');
  170. $this->assertTrue(is_link($this->wwwRoot . 'test_plugin'));
  171. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  172. $this->assertTrue(is_link($path));
  173. $this->exec('plugin assets remove');
  174. $this->assertFalse(is_link($this->wwwRoot . 'test_plugin'));
  175. $this->assertFalse(is_link($path));
  176. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  177. }
  178. /**
  179. * testRemoveFolder method
  180. */
  181. public function testRemoveFolder(): void
  182. {
  183. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  184. $this->exec('plugin assets copy');
  185. $this->assertTrue(is_dir($this->wwwRoot . 'test_plugin'));
  186. $this->assertTrue(is_dir($this->wwwRoot . 'company' . DS . 'test_plugin_three'));
  187. $this->exec('plugin assets remove');
  188. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'test_plugin');
  189. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'company' . DS . 'test_plugin_three');
  190. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  191. }
  192. /**
  193. * testOverwrite
  194. */
  195. public function testOverwrite(): void
  196. {
  197. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  198. $path = $this->wwwRoot . 'test_plugin';
  199. mkdir($path);
  200. $filectime = filectime($path);
  201. sleep(1);
  202. $this->exec('plugin assets symlink TestPlugin --overwrite');
  203. $this->assertTrue(is_link($path));
  204. $newfilectime = filectime($path);
  205. $this->assertTrue($newfilectime !== $filectime);
  206. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  207. mkdir($path, 0777, true);
  208. $filectime = filectime($path);
  209. sleep(1);
  210. $this->exec('plugin assets copy Company/TestPluginThree --overwrite');
  211. $newfilectime = filectime($path);
  212. $this->assertTrue($newfilectime > $filectime);
  213. }
  214. }