PluginAssetsCommandsTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\Command;
  17. use Cake\Command\Command;
  18. use Cake\Console\ConsoleIo;
  19. use Cake\Console\ConsoleOptionParser;
  20. use Cake\Core\Configure;
  21. use Cake\TestSuite\ConsoleIntegrationTestTrait;
  22. use Cake\TestSuite\Stub\ConsoleOutput;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Filesystem;
  25. /**
  26. * PluginAssetsCommandsTest class
  27. */
  28. class PluginAssetsCommandsTest extends TestCase
  29. {
  30. use ConsoleIntegrationTestTrait;
  31. /**
  32. * @var string
  33. */
  34. protected $wwwRoot;
  35. /**
  36. * @var \Cake\Utility\Filesystem
  37. */
  38. protected $fs;
  39. /**
  40. * setUp method
  41. */
  42. public function setUp(): void
  43. {
  44. parent::setUp();
  45. $this->wwwRoot = TMP . 'assets_task_webroot' . DS;
  46. Configure::write('App.wwwRoot', $this->wwwRoot);
  47. $this->fs = new Filesystem();
  48. $this->fs->deleteDir($this->wwwRoot);
  49. $this->fs->copyDir(WWW_ROOT, $this->wwwRoot);
  50. $this->setAppNamespace();
  51. $this->configApplication(Configure::read('App.namespace') . '\ApplicationWithDefaultRoutes', []);
  52. }
  53. /**
  54. * tearDown method
  55. */
  56. public function tearDown(): void
  57. {
  58. parent::tearDown();
  59. $this->clearPlugins();
  60. }
  61. /**
  62. * testSymlink method
  63. */
  64. public function testSymlink(): void
  65. {
  66. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  67. $this->exec('plugin assets symlink');
  68. $this->assertExitCode(Command::CODE_SUCCESS);
  69. $path = $this->wwwRoot . 'test_plugin';
  70. $this->assertFileExists($path . DS . 'root.js');
  71. $this->assertTrue(is_link($path));
  72. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  73. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  74. $this->assertTrue(is_link($path));
  75. }
  76. public function testSymlinkWhenVendorDirectoryExists(): void
  77. {
  78. $this->loadPlugins(['Company/TestPluginThree']);
  79. mkdir($this->wwwRoot . 'company');
  80. $this->exec('plugin assets symlink');
  81. $this->assertExitCode(Command::CODE_SUCCESS);
  82. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  83. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  84. $this->assertTrue(is_link($path));
  85. }
  86. /**
  87. * testSymlinkWhenTargetAlreadyExits
  88. */
  89. public function testSymlinkWhenTargetAlreadyExits(): void
  90. {
  91. $this->loadPlugins(['TestTheme']);
  92. $output = new ConsoleOutput();
  93. $io = $this->getMockBuilder(ConsoleIo::class)
  94. ->setConstructorArgs([$output, $output, null, null])
  95. ->addMethods(['in'])
  96. ->getMock();
  97. $parser = new ConsoleOptionParser('cake example');
  98. $parser->addArgument('name', ['optional' => true]);
  99. $parser->addOption('overwrite', ['default' => false, 'boolean' => true]);
  100. $command = $this->getMockBuilder('Cake\Command\PluginAssetsSymlinkCommand')
  101. ->onlyMethods(['getOptionParser', '_createSymlink', '_copyDirectory'])
  102. ->getMock();
  103. $command->method('getOptionParser')->will($this->returnValue($parser));
  104. $this->assertDirectoryExists($this->wwwRoot . 'test_theme');
  105. $command->expects($this->never())->method('_createSymlink');
  106. $command->expects($this->never())->method('_copyDirectory');
  107. $command->run([], $io);
  108. }
  109. /**
  110. * test that plugins without webroot are not processed
  111. */
  112. public function testForPluginWithoutWebroot(): void
  113. {
  114. $this->loadPlugins(['TestPluginTwo']);
  115. $this->exec('plugin assets symlink');
  116. $this->assertFileDoesNotExist($this->wwwRoot . 'test_plugin_two');
  117. }
  118. /**
  119. * testSymlinkingSpecifiedPlugin
  120. */
  121. public function testSymlinkingSpecifiedPlugin(): void
  122. {
  123. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  124. $this->exec('plugin assets symlink TestPlugin');
  125. $path = $this->wwwRoot . 'test_plugin';
  126. $link = new \SplFileInfo($path);
  127. $this->assertFileExists($path . DS . 'root.js');
  128. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  129. $this->assertDirectoryDoesNotExist($path);
  130. $this->assertFalse(is_link($path));
  131. }
  132. /**
  133. * testCopy
  134. */
  135. public function testCopy(): void
  136. {
  137. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  138. $this->exec('plugin assets copy');
  139. $path = $this->wwwRoot . 'test_plugin';
  140. $this->assertDirectoryExists($path);
  141. $this->assertFileExists($path . DS . 'root.js');
  142. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  143. $this->assertDirectoryExists($path);
  144. $this->assertFileExists($path . DS . 'css' . DS . 'company.css');
  145. }
  146. /**
  147. * testCopyOverwrite
  148. */
  149. public function testCopyOverwrite(): void
  150. {
  151. $this->loadPlugins(['TestPlugin' => ['routes' => false]]);
  152. $this->exec('plugin assets copy');
  153. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot';
  154. $path = $this->wwwRoot . 'test_plugin';
  155. $dir = new \SplFileInfo($path);
  156. $this->assertTrue($dir->isDir());
  157. $this->assertFileExists($path . DS . 'root.js');
  158. file_put_contents($path . DS . 'root.js', 'updated');
  159. $this->exec('plugin assets copy');
  160. $this->assertFileNotEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  161. $this->exec('plugin assets copy --overwrite');
  162. $this->assertFileEquals($path . DS . 'root.js', $pluginPath . DS . 'root.js');
  163. }
  164. /**
  165. * testRemoveSymlink method
  166. */
  167. public function testRemoveSymlink(): void
  168. {
  169. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  170. mkdir($this->wwwRoot . 'company');
  171. $this->exec('plugin assets symlink');
  172. $this->assertTrue(is_link($this->wwwRoot . 'test_plugin'));
  173. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  174. $this->assertTrue(is_link($path));
  175. $this->exec('plugin assets remove');
  176. $this->assertFalse(is_link($this->wwwRoot . 'test_plugin'));
  177. $this->assertFalse(is_link($path));
  178. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  179. }
  180. /**
  181. * testRemoveFolder method
  182. */
  183. public function testRemoveFolder(): void
  184. {
  185. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  186. $this->exec('plugin assets copy');
  187. $this->assertTrue(is_dir($this->wwwRoot . 'test_plugin'));
  188. $this->assertTrue(is_dir($this->wwwRoot . 'company' . DS . 'test_plugin_three'));
  189. $this->exec('plugin assets remove');
  190. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'test_plugin');
  191. $this->assertDirectoryDoesNotExist($this->wwwRoot . 'company' . DS . 'test_plugin_three');
  192. $this->assertDirectoryExists($this->wwwRoot . 'company', 'Ensure namespace folder isn\'t removed');
  193. }
  194. /**
  195. * testOverwrite
  196. */
  197. public function testOverwrite(): void
  198. {
  199. $this->loadPlugins(['TestPlugin' => ['routes' => false], 'Company/TestPluginThree']);
  200. $path = $this->wwwRoot . 'test_plugin';
  201. mkdir($path);
  202. $filectime = filectime($path);
  203. sleep(1);
  204. $this->exec('plugin assets symlink TestPlugin --overwrite');
  205. $this->assertTrue(is_link($path));
  206. $newfilectime = filectime($path);
  207. $this->assertTrue($newfilectime !== $filectime);
  208. $path = $this->wwwRoot . 'company' . DS . 'test_plugin_three';
  209. mkdir($path, 0777, true);
  210. $filectime = filectime($path);
  211. sleep(1);
  212. $this->exec('plugin assets copy Company/TestPluginThree --overwrite');
  213. $newfilectime = filectime($path);
  214. $this->assertTrue($newfilectime > $filectime);
  215. }
  216. }