AssetsTaskTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Task;
  16. use Cake\Core\Plugin;
  17. use Cake\Filesystem\Folder;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * AssetsTaskTest class
  21. */
  22. class AssetsTaskTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->skipIf(
  33. DS === '\\',
  34. 'Skip AssetsTask tests on windows to prevent side effects for UrlHelper tests on AppVeyor.'
  35. );
  36. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->Task = $this->getMockBuilder('Cake\Shell\Task\AssetsTask')
  40. ->setMethods(['in', 'out', 'err', '_stop'])
  41. ->setConstructorArgs([$this->io])
  42. ->getMock();
  43. }
  44. /**
  45. * tearDown method
  46. *
  47. * @return void
  48. */
  49. public function tearDown()
  50. {
  51. parent::tearDown();
  52. unset($this->Task);
  53. $this->clearPlugins();
  54. }
  55. /**
  56. * testSymlink method
  57. *
  58. * @return void
  59. */
  60. public function testSymlink()
  61. {
  62. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  63. $this->Task->symlink();
  64. $path = WWW_ROOT . 'test_plugin';
  65. $this->assertFileExists($path . DS . 'root.js');
  66. if (DS === '\\') {
  67. $this->assertDirectoryExists($path);
  68. $folder = new Folder($path);
  69. $folder->delete();
  70. } else {
  71. $this->assertTrue(is_link($path));
  72. unlink($path);
  73. }
  74. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  75. // If "company" directory exists beforehand "test_plugin_three" would
  76. // be a link. But if the directory is created by the shell itself
  77. // symlinking fails and the assets folder is copied as fallback.
  78. $this->assertDirectoryExists($path);
  79. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  80. $folder = new Folder(WWW_ROOT . 'company');
  81. $folder->delete();
  82. }
  83. /**
  84. * testSymlinkWhenVendorDirectoryExits
  85. *
  86. * @return void
  87. */
  88. public function testSymlinkWhenVendorDirectoryExits()
  89. {
  90. $this->loadPlugins(['Company/TestPluginThree']);
  91. mkdir(WWW_ROOT . 'company');
  92. $this->Task->symlink();
  93. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  94. if (DS === '\\') {
  95. $this->assertDirectoryExits($path);
  96. } else {
  97. $this->assertTrue(is_link($path));
  98. }
  99. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  100. $folder = new Folder(WWW_ROOT . 'company');
  101. $folder->delete();
  102. }
  103. /**
  104. * testSymlinkWhenTargetAlreadyExits
  105. *
  106. * @return void
  107. */
  108. public function testSymlinkWhenTargetAlreadyExits()
  109. {
  110. $this->loadPlugins(['TestTheme']);
  111. $shell = $this->getMockBuilder('Cake\Shell\Task\AssetsTask')
  112. ->setMethods(['in', 'out', 'err', '_stop', '_createSymlink', '_copyDirectory'])
  113. ->setConstructorArgs([$this->io])
  114. ->getMock();
  115. $this->assertDirectoryExists(WWW_ROOT . 'test_theme');
  116. $shell->expects($this->never())->method('_createSymlink');
  117. $shell->expects($this->never())->method('_copyDirectory');
  118. $shell->symlink();
  119. }
  120. /**
  121. * test that plugins without webroot are not processed
  122. *
  123. * @return void
  124. */
  125. public function testForPluginWithoutWebroot()
  126. {
  127. $this->loadPlugins(['TestPluginTwo']);
  128. $this->Task->symlink();
  129. $this->assertFileNotExists(WWW_ROOT . 'test_plugin_two');
  130. }
  131. /**
  132. * testSymlinkingSpecifiedPlugin
  133. *
  134. * @return void
  135. */
  136. public function testSymlinkingSpecifiedPlugin()
  137. {
  138. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  139. $this->Task->symlink('TestPlugin');
  140. $path = WWW_ROOT . 'test_plugin';
  141. $link = new \SplFileInfo($path);
  142. $this->assertFileExists($path . DS . 'root.js');
  143. unlink($path);
  144. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  145. $this->assertDirectoryNotExists($path);
  146. $this->assertFalse(is_link($path));
  147. }
  148. /**
  149. * testCopy
  150. *
  151. * @return void
  152. */
  153. public function testCopy()
  154. {
  155. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  156. $this->Task->copy();
  157. $path = WWW_ROOT . 'test_plugin';
  158. $this->assertDirectoryExists($path);
  159. $this->assertFileExists($path . DS . 'root.js');
  160. $folder = new Folder($path);
  161. $folder->delete();
  162. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  163. $this->assertDirectoryExists($path);
  164. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  165. $folder = new Folder(WWW_ROOT . 'company');
  166. $folder->delete();
  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 = WWW_ROOT . '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. $folder = new Folder($path);
  189. $folder->delete();
  190. }
  191. /**
  192. * testRemoveSymlink method
  193. *
  194. * @return void
  195. */
  196. public function testRemoveSymlink()
  197. {
  198. if (DS === '\\') {
  199. $this->markTestSkipped(
  200. "Can't test symlink removal on windows."
  201. );
  202. }
  203. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  204. mkdir(WWW_ROOT . 'company');
  205. $this->Task->symlink();
  206. $this->assertTrue(is_link(WWW_ROOT . 'test_plugin'));
  207. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  208. $this->assertTrue(is_link($path));
  209. $this->Task->remove();
  210. $this->assertFalse(is_link(WWW_ROOT . 'test_plugin'));
  211. $this->assertFalse(is_link($path));
  212. $this->assertDirectoryExists(WWW_ROOT . 'company', 'Ensure namespace folder isn\'t removed');
  213. rmdir(WWW_ROOT . 'company');
  214. }
  215. /**
  216. * testRemoveFolder method
  217. *
  218. * @return void
  219. */
  220. public function testRemoveFolder()
  221. {
  222. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  223. $this->Task->copy();
  224. $this->assertTrue(is_dir(WWW_ROOT . 'test_plugin'));
  225. $this->assertTrue(is_dir(WWW_ROOT . 'company' . DS . 'test_plugin_three'));
  226. $this->Task->remove();
  227. $this->assertDirectoryNotExists(WWW_ROOT . 'test_plugin');
  228. $this->assertDirectoryNotExists(WWW_ROOT . 'company' . DS . 'test_plugin_three');
  229. $this->assertDirectoryExists(WWW_ROOT . 'company', 'Ensure namespace folder isn\'t removed');
  230. rmdir(WWW_ROOT . 'company');
  231. }
  232. /**
  233. * testOverwrite
  234. *
  235. * @return void
  236. */
  237. public function testOverwrite()
  238. {
  239. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  240. $path = WWW_ROOT . 'test_plugin';
  241. mkdir($path);
  242. $filectime = filectime($path);
  243. sleep(1);
  244. $this->Task->params['overwrite'] = true;
  245. $this->Task->symlink('TestPlugin');
  246. if (DS === '\\') {
  247. $this->assertDirectoryExists($path);
  248. } else {
  249. $this->assertTrue(is_link($path));
  250. }
  251. $newfilectime = filectime($path);
  252. $this->assertTrue($newfilectime !== $filectime);
  253. if (DS === '\\') {
  254. $folder = new Folder($path);
  255. $folder->delete();
  256. } else {
  257. unlink($path);
  258. }
  259. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  260. mkdir($path, 0777, true);
  261. $filectime = filectime($path);
  262. sleep(1);
  263. $this->Task->params['overwrite'] = true;
  264. $this->Task->copy('Company/TestPluginThree');
  265. $newfilectime = filectime($path);
  266. $this->assertTrue($newfilectime > $filectime);
  267. $folder = new Folder(WWW_ROOT . 'company');
  268. $folder->delete();
  269. }
  270. }