ImageManagerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Haertl <jus@bitgrid.net>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Theming\Tests;
  25. use OCP\Files\SimpleFS\ISimpleFile;
  26. use OCP\IConfig;
  27. use Test\TestCase;
  28. use OCP\Files\SimpleFS\ISimpleFolder;
  29. use OCP\Files\IAppData;
  30. use OCP\Files\NotFoundException;
  31. class ImageManager extends TestCase {
  32. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $config;
  34. /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $appData;
  36. /** @var ImageManager */
  37. protected $imageManager;
  38. protected function setUp() {
  39. parent::setUp();
  40. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  41. $this->appData = $this->getMockBuilder('OCP\Files\IAppData')->getMock();
  42. $this->imageManager = new \OCA\Theming\ImageManager(
  43. $this->config,
  44. $this->appData
  45. );
  46. }
  47. public function testGetCacheFolder() {
  48. $folder = $this->createMock(ISimpleFolder::class);
  49. $this->config->expects($this->once())
  50. ->method('getAppValue')
  51. ->with('theming', 'cachebuster', '0')
  52. ->willReturn('0');
  53. $this->appData->expects($this->at(0))
  54. ->method('getFolder')
  55. ->with('0')
  56. ->willReturn($folder);
  57. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  58. }
  59. public function testGetCacheFolderCreate() {
  60. $folder = $this->createMock(ISimpleFolder::class);
  61. $this->config->expects($this->exactly(2))
  62. ->method('getAppValue')
  63. ->with('theming', 'cachebuster', '0')
  64. ->willReturn('0');
  65. $this->appData->expects($this->at(0))
  66. ->method('getFolder')
  67. ->willThrowException(new NotFoundException());
  68. $this->appData->expects($this->at(1))
  69. ->method('newFolder')
  70. ->with('0')
  71. ->willReturn($folder);
  72. $this->appData->expects($this->at(2))
  73. ->method('getFolder')
  74. ->with('0')
  75. ->willReturn($folder);
  76. $this->appData->expects($this->once())
  77. ->method('getDirectoryListing')
  78. ->willReturn([]);
  79. $this->assertEquals($folder, $this->imageManager->getCacheFolder());
  80. }
  81. public function testGetCachedImage() {
  82. $folder = $this->setupCacheFolder();
  83. $folder->expects($this->once())
  84. ->method('getFile')
  85. ->with('filename')
  86. ->willReturn('filecontent');
  87. $expected = 'filecontent';
  88. $this->assertEquals($expected, $this->imageManager->getCachedImage('filename'));
  89. }
  90. /**
  91. * @expectedException \OCP\Files\NotFoundException
  92. */
  93. public function testGetCachedImageNotFound() {
  94. $folder = $this->setupCacheFolder();
  95. $folder->expects($this->once())
  96. ->method('getFile')
  97. ->with('filename')
  98. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  99. $image = $this->imageManager->getCachedImage('filename');
  100. }
  101. public function testSetCachedImage() {
  102. $folder = $this->setupCacheFolder();
  103. $file = $this->createMock(ISimpleFile::class);
  104. $folder->expects($this->once())
  105. ->method('fileExists')
  106. ->with('filename')
  107. ->willReturn(true);
  108. $folder->expects($this->once())
  109. ->method('getFile')
  110. ->with('filename')
  111. ->willReturn($file);
  112. $file->expects($this->once())
  113. ->method('putContent')
  114. ->with('filecontent');
  115. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  116. }
  117. public function testSetCachedImageCreate() {
  118. $folder = $this->setupCacheFolder();
  119. $file = $this->createMock(ISimpleFile::class);
  120. $folder->expects($this->once())
  121. ->method('fileExists')
  122. ->with('filename')
  123. ->willReturn(false);
  124. $folder->expects($this->once())
  125. ->method('newFile')
  126. ->with('filename')
  127. ->willReturn($file);
  128. $file->expects($this->once())
  129. ->method('putContent')
  130. ->with('filecontent');
  131. $this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent'));
  132. }
  133. private function setupCacheFolder() {
  134. $folder = $this->createMock(ISimpleFolder::class);
  135. $this->config->expects($this->once())
  136. ->method('getAppValue')
  137. ->with('theming', 'cachebuster', '0')
  138. ->willReturn('0');
  139. $this->appData->expects($this->at(0))
  140. ->method('getFolder')
  141. ->with('0')
  142. ->willReturn($folder);
  143. return $folder;
  144. }
  145. public function testCleanup() {
  146. $folders = [
  147. $this->createMock(ISimpleFolder::class),
  148. $this->createMock(ISimpleFolder::class),
  149. $this->createMock(ISimpleFolder::class)
  150. ];
  151. foreach ($folders as $index=>$folder) {
  152. $folder->expects($this->any())
  153. ->method('getName')
  154. ->willReturn($index);
  155. }
  156. $folders[0]->expects($this->once())->method('delete');
  157. $folders[1]->expects($this->once())->method('delete');
  158. $folders[2]->expects($this->never())->method('delete');
  159. $this->config->expects($this->once())
  160. ->method('getAppValue')
  161. ->with('theming','cachebuster','0')
  162. ->willReturn('2');
  163. $this->appData->expects($this->once())
  164. ->method('getDirectoryListing')
  165. ->willReturn($folders);
  166. $this->appData->expects($this->once())
  167. ->method('getFolder')
  168. ->with('2')
  169. ->willReturn($folders[2]);
  170. $this->imageManager->cleanup();
  171. }
  172. }