AssetsTaskTest.php 9.1 KB

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