PluginAssetsCommandsTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\TestSuite\ConsoleIntegrationTestTrait;
  22. use Cake\TestSuite\Stub\ConsoleOutput;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Filesystem;
  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\Utility\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->setAppNamespace();
  52. $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', []);
  53. }
  54. /**
  55. * tearDown method
  56. */
  57. public function tearDown(): void
  58. {
  59. parent::tearDown();
  60. $this->clearPlugins();
  61. }
  62. /**
  63. * testSymlink method
  64. */
  65. public function testSymlink(): void
  66. {
  67. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  68. $this->exec('plugin assets symlink');
  69. $this->assertExitCode(Command::CODE_SUCCESS);
  70. $path = $this->wwwRoot . 'test_plugin';
  71. $this->assertFileExists($path . DS . 'root.js');
  72. $this->assertTrue(is_link($path));
  73. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  74. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  75. $this->assertTrue(is_link($path));
  76. }
  77. public function testSymlinkWhenVendorDirectoryExists(): void
  78. {
  79. $this->loadPlugins(['Company/TestPluginThree']);
  80. mkdir($this->wwwRoot . 'company');
  81. $this->exec('plugin assets symlink');
  82. $this->assertExitCode(Command::CODE_SUCCESS);
  83. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  84. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  85. $this->assertTrue(is_link($path));
  86. }
  87. /**
  88. * testSymlinkWhenTargetAlreadyExits
  89. */
  90. public function testSymlinkWhenTargetAlreadyExits(): void
  91. {
  92. $this->loadPlugins(['TestTheme']);
  93. $output = new ConsoleOutput();
  94. $io = $this->getMockBuilder(ConsoleIo::class)
  95. ->setConstructorArgs([$output, $output, null, null])
  96. ->addMethods(['in'])
  97. ->getMock();
  98. $parser = new ConsoleOptionParser('cake example');
  99. $parser->addArgument('name', ['required' => false]);
  100. $parser->addOption('overwrite', ['default' => false, 'boolean' => true]);
  101. $command = $this->getMockBuilder('Cake\Command\PluginAssetsSymlinkCommand')
  102. ->onlyMethods(['getOptionParser', '_createSymlink', '_copyDirectory'])
  103. ->getMock();
  104. $command->method('getOptionParser')->will($this->returnValue($parser));
  105. $this->assertDirectoryExists($this->wwwRoot . 'test_theme');
  106. $command->expects($this->never())->method('_createSymlink');
  107. $command->expects($this->never())->method('_copyDirectory');
  108. $command->run([], $io);
  109. }
  110. /**
  111. * test that plugins without webroot are not processed
  112. */
  113. public function testForPluginWithoutWebroot(): void
  114. {
  115. $this->loadPlugins(['TestPluginTwo']);
  116. $this->exec('plugin assets symlink');
  117. $this->assertFileDoesNotExist($this->wwwRoot . 'test_plugin_two');
  118. }
  119. /**
  120. * testSymlinkingSpecifiedPlugin
  121. */
  122. public function testSymlinkingSpecifiedPlugin(): void
  123. {
  124. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  125. $this->exec('plugin assets symlink TestPlugin');
  126. $path = $this->wwwRoot . 'test_plugin';
  127. $link = new SplFileInfo($path);
  128. $this->assertFileExists($path . DS . 'root.js');
  129. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  130. $this->assertDirectoryDoesNotExist($path);
  131. $this->assertFalse(is_link($path));
  132. }
  133. /**
  134. * testCopy
  135. */
  136. public function testCopy(): void
  137. {
  138. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  139. $this->exec('plugin assets copy');
  140. $path = $this->wwwRoot . 'test_plugin';
  141. $this->assertDirectoryExists($path);
  142. $this->assertFileExists($path . DS . 'root.js');
  143. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  144. $this->assertDirectoryExists($path);
  145. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  146. }
  147. /**
  148. * testCopyOverwrite
  149. */
  150. public function testCopyOverwrite(): void
  151. {
  152. $this->loadPlugins(['TestPlugin' => ['routes' => false]]);
  153. $this->exec('plugin assets copy');
  154. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot';
  155. $path = $this->wwwRoot . 'test_plugin';
  156. $dir = new SplFileInfo($path);
  157. $this->assertTrue($dir->isDir());
  158. $this->assertFileExists($path . DS . 'root.js');
  159. file_put_contents($path . DS . 'root.js', 'updated');
  160. $this->exec('plugin assets copy');
  161. $this->assertFileNotEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  162. $this->exec('plugin assets copy --overwrite');
  163. $this->assertFileEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  164. }
  165. /**
  166. * testRemoveSymlink method
  167. */
  168. public function testRemoveSymlink(): void
  169. {
  170. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  171. mkdir($this->wwwRoot . 'company');
  172. $this->exec('plugin assets symlink');
  173. $this->assertTrue(is_link($this->wwwRoot . 'test_plugin'));
  174. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  175. $this->assertTrue(is_link($path));
  176. $this->exec('plugin assets remove');
  177. $this->assertFalse(is_link($this->wwwRoot . 'test_plugin'));
  178. $this->assertFalse(is_link($path));
  179. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  180. }
  181. /**
  182. * testRemoveFolder method
  183. */
  184. public function testRemoveFolder(): void
  185. {
  186. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  187. $this->exec('plugin assets copy');
  188. $this->assertTrue(is_dir($this->wwwRoot . 'test_plugin'));
  189. $this->assertTrue(is_dir($this->wwwRoot . 'company' . DS . 'test_plugin_three'));
  190. $this->exec('plugin assets remove');
  191. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'test_plugin');
  192. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'company' . DS . 'test_plugin_three');
  193. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  194. }
  195. /**
  196. * testOverwrite
  197. */
  198. public function testOverwrite(): void
  199. {
  200. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  201. $path = $this->wwwRoot . 'test_plugin';
  202. mkdir($path);
  203. $filectime = filectime($path);
  204. sleep(1);
  205. $this->exec('plugin assets symlink TestPlugin --overwrite');
  206. $this->assertTrue(is_link($path));
  207. $newfilectime = filectime($path);
  208. $this->assertTrue($newfilectime !== $filectime);
  209. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  210. mkdir($path, 0777, true);
  211. $filectime = filectime($path);
  212. sleep(1);
  213. $this->exec('plugin assets copy Company/TestPluginThree --overwrite');
  214. $newfilectime = filectime($path);
  215. $this->assertTrue($newfilectime > $filectime);
  216. }
  217. }