AssetsTaskTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. * testRemoveSymlink method
  173. *
  174. * @return void
  175. */
  176. public function testRemoveSymlink()
  177. {
  178. if (DS === '\\') {
  179. $this->markTestSkipped(
  180. "Can't test symlink removal on windows."
  181. );
  182. }
  183. Plugin::load('TestPlugin');
  184. Plugin::load('Company/TestPluginThree');
  185. mkdir(WWW_ROOT . 'company');
  186. $this->Task->symlink();
  187. $this->assertTrue(is_link(WWW_ROOT . 'test_plugin'));
  188. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  189. $this->assertTrue(is_link($path));
  190. $this->Task->remove();
  191. $this->assertFalse(is_link(WWW_ROOT . 'test_plugin'));
  192. $this->assertFalse(is_link($path));
  193. $this->assertDirectoryExists(WWW_ROOT . 'company', 'Ensure namespace folder isn\'t removed');
  194. rmdir(WWW_ROOT . 'company');
  195. }
  196. /**
  197. * testRemoveFolder method
  198. *
  199. * @return void
  200. */
  201. public function testRemoveFolder()
  202. {
  203. Plugin::load('TestPlugin');
  204. Plugin::load('Company/TestPluginThree');
  205. $this->Task->copy();
  206. $this->assertTrue(is_dir(WWW_ROOT . 'test_plugin'));
  207. $this->assertTrue(is_dir(WWW_ROOT . 'company' . DS . 'test_plugin_three'));
  208. $this->Task->remove();
  209. $this->assertDirectoryNotExists(WWW_ROOT . 'test_plugin');
  210. $this->assertDirectoryNotExists(WWW_ROOT . 'company' . DS . 'test_plugin_three');
  211. $this->assertDirectoryExists(WWW_ROOT . 'company', 'Ensure namespace folder isn\'t removed');
  212. rmdir(WWW_ROOT . 'company');
  213. }
  214. /**
  215. * testOverwrite
  216. *
  217. * @return void
  218. */
  219. public function testOverwrite()
  220. {
  221. Plugin::load('TestPlugin');
  222. Plugin::load('Company/TestPluginThree');
  223. $path = WWW_ROOT . 'test_plugin';
  224. mkdir($path);
  225. $filectime = filectime($path);
  226. sleep(1);
  227. $this->Task->params['overwrite'] = true;
  228. $this->Task->symlink('TestPlugin');
  229. if (DS === '\\') {
  230. $this->assertDirectoryExists($path);
  231. } else {
  232. $this->assertTrue(is_link($path));
  233. }
  234. $newfilectime = filectime($path);
  235. $this->assertTrue($newfilectime !== $filectime);
  236. if (DS === '\\') {
  237. $folder = new Folder($path);
  238. $folder->delete();
  239. } else {
  240. unlink($path);
  241. }
  242. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  243. mkdir($path, 0777, true);
  244. $filectime = filectime($path);
  245. sleep(1);
  246. $this->Task->params['overwrite'] = true;
  247. $this->Task->copy('Company/TestPluginThree');
  248. $newfilectime = filectime($path);
  249. $this->assertTrue($newfilectime > $filectime);
  250. $folder = new Folder(WWW_ROOT . 'company');
  251. $folder->delete();
  252. }
  253. }