PluginAssetsCommandsTest.php 9.9 KB

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