PluginAssetsCommandsTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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\Command\Command;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\ConsoleOptionParser;
  20. use Cake\Core\Configure;
  21. use Cake\Filesystem\Filesystem;
  22. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  23. use Cake\TestSuite\Stub\ConsoleOutput;
  24. use Cake\TestSuite\TestCase;
  25. use SplFileInfo;
  26. /**
  27. * PluginAssetsCommandsTest class
  28. */
  29. class PluginAssetsCommandsTest extends TestCase
  30. {
  31. use ConsoleIntegrationTestTrait;
  32. /**
  33. * @var string
  34. */
  35. protected $wwwRoot;
  36. /**
  37. * @var \Cake\Filesystem\Filesystem
  38. */
  39. protected $fs;
  40. /**
  41. * setUp method
  42. */
  43. public function setUp(): void
  44. {
  45. parent::setUp();
  46. $this->wwwRoot = TMP . 'assets_task_webroot' . DS;
  47. Configure::write('App.wwwRoot', $this->wwwRoot);
  48. $this->fs = new Filesystem();
  49. $this->fs->deleteDir($this->wwwRoot);
  50. $this->fs->copyDir(WWW_ROOT, $this->wwwRoot);
  51. $this->useCommandRunner();
  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(Command::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(Command::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 ConsoleOutput();
  95. $io = $this->getMockBuilder(ConsoleIo::class)
  96. ->setConstructorArgs([$output, $output, null, null])
  97. ->addMethods(['in'])
  98. ->getMock();
  99. $parser = new ConsoleOptionParser('cake example');
  100. $parser->addArgument('name', ['required' => false]);
  101. $parser->addOption('overwrite', ['default' => false, 'boolean' => true]);
  102. $command = $this->getMockBuilder('Cake\Command\PluginAssetsSymlinkCommand')
  103. ->onlyMethods(['getOptionParser', '_createSymlink', '_copyDirectory'])
  104. ->getMock();
  105. $command->method('getOptionParser')->will($this->returnValue($parser));
  106. $this->assertDirectoryExists($this->wwwRoot . 'test_theme');
  107. $command->expects($this->never())->method('_createSymlink');
  108. $command->expects($this->never())->method('_copyDirectory');
  109. $command->run([], $io);
  110. }
  111. /**
  112. * test that plugins without webroot are not processed
  113. */
  114. public function testForPluginWithoutWebroot(): void
  115. {
  116. $this->loadPlugins(['TestPluginTwo']);
  117. $this->exec('plugin assets symlink');
  118. $this->assertFileDoesNotExist($this->wwwRoot . 'test_plugin_two');
  119. }
  120. /**
  121. * testSymlinkingSpecifiedPlugin
  122. */
  123. public function testSymlinkingSpecifiedPlugin(): void
  124. {
  125. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  126. $this->exec('plugin assets symlink TestPlugin');
  127. $path = $this->wwwRoot . 'test_plugin';
  128. $link = new SplFileInfo($path);
  129. $this->assertFileExists($path . DS . 'root.js');
  130. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  131. $this->assertDirectoryDoesNotExist($path);
  132. $this->assertFalse(is_link($path));
  133. }
  134. /**
  135. * testCopy
  136. */
  137. public function testCopy(): void
  138. {
  139. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  140. $this->exec('plugin assets copy');
  141. $path = $this->wwwRoot . 'test_plugin';
  142. $this->assertDirectoryExists($path);
  143. $this->assertFileExists($path . DS . 'root.js');
  144. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  145. $this->assertDirectoryExists($path);
  146. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  147. }
  148. /**
  149. * testCopyOverwrite
  150. */
  151. public function testCopyOverwrite(): void
  152. {
  153. $this->loadPlugins(['TestPlugin' => ['routes' => false]]);
  154. $this->exec('plugin assets copy');
  155. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot';
  156. $path = $this->wwwRoot . 'test_plugin';
  157. $dir = new SplFileInfo($path);
  158. $this->assertTrue($dir->isDir());
  159. $this->assertFileExists($path . DS . 'root.js');
  160. file_put_contents($path . DS . 'root.js', 'updated');
  161. $this->exec('plugin assets copy');
  162. $this->assertFileNotEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  163. $this->exec('plugin assets copy --overwrite');
  164. $this->assertFileEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  165. }
  166. /**
  167. * testRemoveSymlink method
  168. */
  169. public function testRemoveSymlink(): void
  170. {
  171. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  172. mkdir($this->wwwRoot . 'company');
  173. $this->exec('plugin assets symlink');
  174. $this->assertTrue(is_link($this->wwwRoot . 'test_plugin'));
  175. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  176. $this->assertTrue(is_link($path));
  177. $this->exec('plugin assets remove');
  178. $this->assertFalse(is_link($this->wwwRoot . 'test_plugin'));
  179. $this->assertFalse(is_link($path));
  180. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  181. }
  182. /**
  183. * testRemoveFolder method
  184. */
  185. public function testRemoveFolder(): void
  186. {
  187. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  188. $this->exec('plugin assets copy');
  189. $this->assertTrue(is_dir($this->wwwRoot . 'test_plugin'));
  190. $this->assertTrue(is_dir($this->wwwRoot . 'company' . DS . 'test_plugin_three'));
  191. $this->exec('plugin assets remove');
  192. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'test_plugin');
  193. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'company' . DS . 'test_plugin_three');
  194. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  195. }
  196. /**
  197. * testOverwrite
  198. */
  199. public function testOverwrite(): void
  200. {
  201. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  202. $path = $this->wwwRoot . 'test_plugin';
  203. mkdir($path);
  204. $filectime = filectime($path);
  205. sleep(1);
  206. $this->exec('plugin assets symlink TestPlugin --overwrite');
  207. $this->assertTrue(is_link($path));
  208. $newfilectime = filectime($path);
  209. $this->assertTrue($newfilectime !== $filectime);
  210. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  211. mkdir($path, 0777, true);
  212. $filectime = filectime($path);
  213. sleep(1);
  214. $this->exec('plugin assets copy Company/TestPluginThree --overwrite');
  215. $newfilectime = filectime($path);
  216. $this->assertTrue($newfilectime > $filectime);
  217. }
  218. }