PluginAssetsCommandsTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /**
  26. * PluginAssetsCommandsTest class
  27. */
  28. class PluginAssetsCommandsTest extends TestCase
  29. {
  30. use ConsoleIntegrationTestTrait;
  31. /**
  32. * @var string
  33. */
  34. protected $wwwRoot;
  35. /**
  36. * @var \Cake\Filesystem\Filesystem
  37. */
  38. protected $fs;
  39. /**
  40. * setUp method
  41. *
  42. * @return void
  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->useCommandRunner();
  53. $this->setAppNamespace();
  54. $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', []);
  55. }
  56. /**
  57. * tearDown method
  58. *
  59. * @return void
  60. */
  61. public function tearDown(): void
  62. {
  63. parent::tearDown();
  64. $this->clearPlugins();
  65. }
  66. /**
  67. * testSymlink method
  68. *
  69. * @return void
  70. */
  71. public function testSymlink()
  72. {
  73. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  74. $this->exec('plugin assets symlink');
  75. $this->assertExitCode(Command::CODE_SUCCESS);
  76. $path = $this->wwwRoot . 'test_plugin';
  77. $this->assertFileExists($path . DS . 'root.js');
  78. if (DS === '\\') {
  79. $this->assertDirectoryExists($path);
  80. } else {
  81. $this->assertTrue(is_link($path));
  82. }
  83. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  84. // If "company" directory exists beforehand "test_plugin_three" would
  85. // be a link. But if the directory is created by the shell itself
  86. // symlinking fails and the assets folder is copied as fallback.
  87. $this->assertDirectoryExists($path);
  88. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  89. }
  90. /**
  91. * testSymlinkWhenVendorDirectoryExits
  92. *
  93. * @return void
  94. */
  95. public function testSymlinkWhenVendorDirectoryExits()
  96. {
  97. $this->loadPlugins(['Company/TestPluginThree']);
  98. mkdir($this->wwwRoot . 'company');
  99. $this->exec('plugin assets symlink');
  100. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  101. if (DS === '\\') {
  102. $this->assertDirectoryExists($path);
  103. } else {
  104. $this->assertTrue(is_link($path));
  105. }
  106. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  107. }
  108. /**
  109. * testSymlinkWhenTargetAlreadyExits
  110. *
  111. * @return void
  112. */
  113. public function testSymlinkWhenTargetAlreadyExits()
  114. {
  115. $this->loadPlugins(['TestTheme']);
  116. $output = new ConsoleOutput();
  117. $io = $this->getMockBuilder(ConsoleIo::class)
  118. ->setConstructorArgs([$output, $output, null, null])
  119. ->addMethods(['in'])
  120. ->getMock();
  121. $parser = new ConsoleOptionParser('cake example');
  122. $parser->addArgument('name', ['optional' => true]);
  123. $parser->addOption('overwrite', ['default' => false, 'boolean' => true]);
  124. $command = $this->getMockBuilder('Cake\Command\PluginAssetsSymlinkCommand')
  125. ->onlyMethods(['getOptionParser', '_createSymlink', '_copyDirectory'])
  126. ->getMock();
  127. $command->method('getOptionParser')->will($this->returnValue($parser));
  128. $this->assertDirectoryExists($this->wwwRoot . 'test_theme');
  129. $command->expects($this->never())->method('_createSymlink');
  130. $command->expects($this->never())->method('_copyDirectory');
  131. $command->run([], $io);
  132. }
  133. /**
  134. * test that plugins without webroot are not processed
  135. *
  136. * @return void
  137. */
  138. public function testForPluginWithoutWebroot()
  139. {
  140. $this->loadPlugins(['TestPluginTwo']);
  141. $this->exec('plugin assets symlink');
  142. $this->assertFileDoesNotExist($this->wwwRoot . 'test_plugin_two');
  143. }
  144. /**
  145. * testSymlinkingSpecifiedPlugin
  146. *
  147. * @return void
  148. */
  149. public function testSymlinkingSpecifiedPlugin()
  150. {
  151. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  152. $this->exec('plugin assets symlink TestPlugin');
  153. $path = $this->wwwRoot . 'test_plugin';
  154. $link = new \SplFileInfo($path);
  155. $this->assertFileExists($path . DS . 'root.js');
  156. unlink($path);
  157. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  158. $this->assertDirectoryDoesNotExist($path);
  159. $this->assertFalse(is_link($path));
  160. }
  161. /**
  162. * testCopy
  163. *
  164. * @return void
  165. */
  166. public function testCopy()
  167. {
  168. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  169. $this->exec('plugin assets copy');
  170. $path = $this->wwwRoot . 'test_plugin';
  171. $this->assertDirectoryExists($path);
  172. $this->assertFileExists($path . DS . 'root.js');
  173. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  174. $this->assertDirectoryExists($path);
  175. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  176. }
  177. /**
  178. * testCopyOverwrite
  179. *
  180. * @return void
  181. */
  182. public function testCopyOverwrite()
  183. {
  184. $this->loadPlugins(['TestPlugin' => ['routes' => false]]);
  185. $this->exec('plugin assets copy');
  186. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot';
  187. $path = $this->wwwRoot . 'test_plugin';
  188. $dir = new \SplFileInfo($path);
  189. $this->assertTrue($dir->isDir());
  190. $this->assertFileExists($path . DS . 'root.js');
  191. file_put_contents($path . DS . 'root.js', 'updated');
  192. $this->exec('plugin assets copy');
  193. $this->assertFileNotEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  194. $this->exec('plugin assets copy --overwrite');
  195. $this->assertFileEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  196. }
  197. /**
  198. * testRemoveSymlink method
  199. *
  200. * @return void
  201. */
  202. public function testRemoveSymlink()
  203. {
  204. if (DS === '\\') {
  205. $this->markTestSkipped(
  206. "Can't test symlink removal on windows."
  207. );
  208. }
  209. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  210. mkdir($this->wwwRoot . 'company');
  211. $this->exec('plugin assets symlink');
  212. $this->assertTrue(is_link($this->wwwRoot . 'test_plugin'));
  213. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  214. $this->assertTrue(is_link($path));
  215. $this->exec('plugin assets remove');
  216. $this->assertFalse(is_link($this->wwwRoot . 'test_plugin'));
  217. $this->assertFalse(is_link($path));
  218. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  219. rmdir($this->wwwRoot . 'company');
  220. }
  221. /**
  222. * testRemoveFolder method
  223. *
  224. * @return void
  225. */
  226. public function testRemoveFolder()
  227. {
  228. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  229. $this->exec('plugin assets copy');
  230. $this->assertTrue(is_dir($this->wwwRoot . 'test_plugin'));
  231. $this->assertTrue(is_dir($this->wwwRoot . 'company' . DS . 'test_plugin_three'));
  232. $this->exec('plugin assets remove');
  233. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'test_plugin');
  234. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'company' . DS . 'test_plugin_three');
  235. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  236. rmdir($this->wwwRoot . 'company');
  237. }
  238. /**
  239. * testOverwrite
  240. *
  241. * @return void
  242. */
  243. public function testOverwrite()
  244. {
  245. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  246. $path = $this->wwwRoot . 'test_plugin';
  247. mkdir($path);
  248. $filectime = filectime($path);
  249. sleep(1);
  250. $this->exec('plugin assets symlink TestPlugin --overwrite');
  251. if (DS === '\\') {
  252. $this->assertDirectoryExists($path);
  253. } else {
  254. $this->assertTrue(is_link($path));
  255. }
  256. $newfilectime = filectime($path);
  257. $this->assertTrue($newfilectime !== $filectime);
  258. if (DS === '\\') {
  259. $this->fs->deleteDir($path);
  260. } else {
  261. unlink($path);
  262. }
  263. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  264. mkdir($path, 0777, true);
  265. $filectime = filectime($path);
  266. sleep(1);
  267. $this->exec('plugin assets copy Company/TestPluginThree --overwrite');
  268. $newfilectime = filectime($path);
  269. $this->assertTrue($newfilectime > $filectime);
  270. }
  271. }