AssetsTaskTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 3.0.0
  13. * @license http://www.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. $link = new \SplFileInfo($path);
  67. $this->assertTrue(file_exists($path . DS . 'root.js'));
  68. if (DS === '\\') {
  69. $this->assertTrue($link->isDir());
  70. $folder = new Folder($path);
  71. $folder->delete();
  72. } else {
  73. $this->assertTrue($link->isLink());
  74. unlink($path);
  75. }
  76. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  77. $link = new \SplFileInfo($path);
  78. // If "company" directory exists beforehand "test_plugin_three" would
  79. // be a link. But if the directory is created by the shell itself
  80. // symlinking fails and the assets folder is copied as fallback.
  81. $this->assertTrue($link->isDir());
  82. $this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
  83. $folder = new Folder(WWW_ROOT . 'company');
  84. $folder->delete();
  85. }
  86. /**
  87. * testSymlinkWhenVendorDirectoryExits
  88. *
  89. * @return void
  90. */
  91. public function testSymlinkWhenVendorDirectoryExits()
  92. {
  93. Plugin::load('Company/TestPluginThree');
  94. mkdir(WWW_ROOT . 'company');
  95. $this->Task->symlink();
  96. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  97. $link = new \SplFileInfo($path);
  98. if (DS === '\\') {
  99. $this->assertTrue($link->isDir());
  100. } else {
  101. $this->assertTrue($link->isLink());
  102. }
  103. $this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
  104. $folder = new Folder(WWW_ROOT . 'company');
  105. $folder->delete();
  106. }
  107. /**
  108. * testSymlinkWhenTargetAlreadyExits
  109. *
  110. * @return void
  111. */
  112. public function testSymlinkWhenTargetAlreadyExits()
  113. {
  114. Plugin::load('TestTheme');
  115. $shell = $this->getMockBuilder('Cake\Shell\Task\AssetsTask')
  116. ->setMethods(['in', 'out', 'err', '_stop', '_createSymlink', '_copyDirectory'])
  117. ->setConstructorArgs([$this->io])
  118. ->getMock();
  119. $this->assertTrue(is_dir(WWW_ROOT . 'test_theme'));
  120. $shell->expects($this->never())->method('_createSymlink');
  121. $shell->expects($this->never())->method('_copyDirectory');
  122. $shell->symlink();
  123. }
  124. /**
  125. * test that plugins without webroot are not processed
  126. *
  127. * @return void
  128. */
  129. public function testForPluginWithoutWebroot()
  130. {
  131. Plugin::load('TestPluginTwo');
  132. $this->Task->symlink();
  133. $this->assertFalse(file_exists(WWW_ROOT . 'test_plugin_two'));
  134. }
  135. /**
  136. * testSymlinkingSpecifiedPlugin
  137. *
  138. * @return void
  139. */
  140. public function testSymlinkingSpecifiedPlugin()
  141. {
  142. Plugin::load('TestPlugin');
  143. Plugin::load('Company/TestPluginThree');
  144. $this->Task->symlink('TestPlugin');
  145. $path = WWW_ROOT . 'test_plugin';
  146. $link = new \SplFileInfo($path);
  147. $this->assertTrue(file_exists($path . DS . 'root.js'));
  148. unlink($path);
  149. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  150. $link = new \SplFileInfo($path);
  151. $this->assertFalse($link->isDir());
  152. $this->assertFalse($link->isLink());
  153. }
  154. /**
  155. * testCopy
  156. *
  157. * @return void
  158. */
  159. public function testCopy()
  160. {
  161. Plugin::load('TestPlugin');
  162. Plugin::load('Company/TestPluginThree');
  163. $this->Task->copy();
  164. $path = WWW_ROOT . 'test_plugin';
  165. $dir = new \SplFileInfo($path);
  166. $this->assertTrue($dir->isDir());
  167. $this->assertTrue(file_exists($path . DS . 'root.js'));
  168. $folder = new Folder($path);
  169. $folder->delete();
  170. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  171. $link = new \SplFileInfo($path);
  172. $this->assertTrue($link->isDir());
  173. $this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
  174. $folder = new Folder(WWW_ROOT . 'company');
  175. $folder->delete();
  176. }
  177. }