AssetsTaskTest.php 9.1 KB

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