PluginAssetsCommandsTest.php 9.0 KB

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