AssetsTaskTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. */
  23. class AssetsTaskTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->skipIf(
  34. DS === '\\',
  35. 'Skip AssetsTask tests on windows to prevent side effects for UrlHelper tests on AppVeyor.'
  36. );
  37. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $this->Task = $this->getMockBuilder('Cake\Shell\Task\AssetsTask')
  41. ->setMethods(['in', 'out', 'err', '_stop'])
  42. ->setConstructorArgs([$this->io])
  43. ->getMock();
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. unset($this->Task);
  54. Plugin::unload();
  55. }
  56. /**
  57. * testSymlink method
  58. *
  59. * @return void
  60. */
  61. public function testSymlink()
  62. {
  63. Plugin::load('TestPlugin');
  64. Plugin::load('Company/TestPluginThree');
  65. $this->Task->symlink();
  66. $path = WWW_ROOT . 'test_plugin';
  67. $link = new \SplFileInfo($path);
  68. $this->assertTrue(file_exists($path . DS . 'root.js'));
  69. if (DS === '\\') {
  70. $this->assertTrue($link->isDir());
  71. $folder = new Folder($path);
  72. $folder->delete();
  73. } else {
  74. $this->assertTrue($link->isLink());
  75. unlink($path);
  76. }
  77. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  78. $link = new \SplFileInfo($path);
  79. // If "company" directory exists beforehand "test_plugin_three" would
  80. // be a link. But if the directory is created by the shell itself
  81. // symlinking fails and the assets folder is copied as fallback.
  82. $this->assertTrue($link->isDir());
  83. $this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
  84. $folder = new Folder(WWW_ROOT . 'company');
  85. $folder->delete();
  86. }
  87. /**
  88. * testSymlinkWhenVendorDirectoryExits
  89. *
  90. * @return void
  91. */
  92. public function testSymlinkWhenVendorDirectoryExits()
  93. {
  94. Plugin::load('Company/TestPluginThree');
  95. mkdir(WWW_ROOT . 'company');
  96. $this->Task->symlink();
  97. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  98. $link = new \SplFileInfo($path);
  99. if (DS === '\\') {
  100. $this->assertTrue($link->isDir());
  101. } else {
  102. $this->assertTrue($link->isLink());
  103. }
  104. $this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
  105. $folder = new Folder(WWW_ROOT . 'company');
  106. $folder->delete();
  107. }
  108. /**
  109. * testSymlinkWhenTargetAlreadyExits
  110. *
  111. * @return void
  112. */
  113. public function testSymlinkWhenTargetAlreadyExits()
  114. {
  115. Plugin::load('TestTheme');
  116. $shell = $this->getMockBuilder('Cake\Shell\Task\AssetsTask')
  117. ->setMethods(['in', 'out', 'err', '_stop', '_createSymlink', '_copyDirectory'])
  118. ->setConstructorArgs([$this->io])
  119. ->getMock();
  120. $this->assertTrue(is_dir(WWW_ROOT . 'test_theme'));
  121. $shell->expects($this->never())->method('_createSymlink');
  122. $shell->expects($this->never())->method('_copyDirectory');
  123. $shell->symlink();
  124. }
  125. /**
  126. * test that plugins without webroot are not processed
  127. *
  128. * @return void
  129. */
  130. public function testForPluginWithoutWebroot()
  131. {
  132. Plugin::load('TestPluginTwo');
  133. $this->Task->symlink();
  134. $this->assertFalse(file_exists(WWW_ROOT . 'test_plugin_two'));
  135. }
  136. /**
  137. * testSymlinkingSpecifiedPlugin
  138. *
  139. * @return void
  140. */
  141. public function testSymlinkingSpecifiedPlugin()
  142. {
  143. Plugin::load('TestPlugin');
  144. Plugin::load('Company/TestPluginThree');
  145. $this->Task->symlink('TestPlugin');
  146. $path = WWW_ROOT . 'test_plugin';
  147. $link = new \SplFileInfo($path);
  148. $this->assertTrue(file_exists($path . DS . 'root.js'));
  149. unlink($path);
  150. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  151. $link = new \SplFileInfo($path);
  152. $this->assertFalse($link->isDir());
  153. $this->assertFalse($link->isLink());
  154. }
  155. /**
  156. * testCopy
  157. *
  158. * @return void
  159. */
  160. public function testCopy()
  161. {
  162. Plugin::load('TestPlugin');
  163. Plugin::load('Company/TestPluginThree');
  164. $this->Task->copy();
  165. $path = WWW_ROOT . 'test_plugin';
  166. $dir = new \SplFileInfo($path);
  167. $this->assertTrue($dir->isDir());
  168. $this->assertTrue(file_exists($path . DS . 'root.js'));
  169. $folder = new Folder($path);
  170. $folder->delete();
  171. $path = WWW_ROOT . 'company' . DS . 'test_plugin_three';
  172. $link = new \SplFileInfo($path);
  173. $this->assertTrue($link->isDir());
  174. $this->assertTrue(file_exists($path . DS . 'css' . DS . 'company.css'));
  175. $folder = new Folder(WWW_ROOT . 'company');
  176. $folder->delete();
  177. }
  178. }