Skip to content

Commit b931ca6

Browse files
juliusknorrbackportbot[bot]
authored andcommitted
Fix tests
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent a0388cd commit b931ca6

File tree

2 files changed

+78
-75
lines changed

2 files changed

+78
-75
lines changed

apps/theming/tests/Controller/ThemingControllerTest.php

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949
use OCP\Files\IAppData;
5050
use OCP\Files\NotFoundException;
5151
use OCP\Files\SimpleFS\ISimpleFile;
52-
use OCP\Files\SimpleFS\ISimpleFolder;
5352
use OCP\IConfig;
5453
use OCP\IL10N;
5554
use OCP\IRequest;
5655
use OCP\ITempManager;
5756
use OCP\IURLGenerator;
57+
use PHPUnit\Framework\MockObject\MockObject;
5858
use Test\TestCase;
5959

6060
class ThemingControllerTest extends TestCase {
@@ -98,12 +98,12 @@ protected function setUp(): void {
9898
$this->urlGenerator = $this->createMock(IURLGenerator::class);
9999
$this->imageManager = $this->createMock(ImageManager::class);
100100

101-
$this->timeFactory = $this->createMock(ITimeFactory::class);
102-
$this->timeFactory->expects($this->any())
101+
$timeFactory = $this->createMock(ITimeFactory::class);
102+
$timeFactory->expects($this->any())
103103
->method('getTime')
104104
->willReturn(123);
105105

106-
$this->overwriteService(ITimeFactory::class, $this->timeFactory);
106+
$this->overwriteService(ITimeFactory::class, $timeFactory);
107107

108108
$this->themingController = new ThemingController(
109109
'theming',
@@ -293,12 +293,9 @@ public function testUploadSVGFaviconWithoutImagemagick() {
293293
return $str;
294294
});
295295

296-
$folder = $this->createMock(ISimpleFolder::class);
297-
$this->appData
298-
->expects($this->once())
299-
->method('getFolder')
300-
->with('images')
301-
->willReturn($folder);
296+
$this->imageManager->expects($this->once())
297+
->method('updateImage')
298+
->willThrowException(new \Exception('Unsupported image type'));
302299

303300
$expected = new DataResponse(
304301
[
@@ -337,12 +334,9 @@ public function testUpdateLogoInvalidMimeType() {
337334
return $str;
338335
});
339336

340-
$folder = $this->createMock(ISimpleFolder::class);
341-
$this->appData
342-
->expects($this->once())
343-
->method('getFolder')
344-
->with('images')
345-
->willReturn($folder);
337+
$this->imageManager->expects($this->once())
338+
->method('updateImage')
339+
->willThrowException(new \Exception('Unsupported image type'));
346340

347341
$expected = new DataResponse(
348342
[
@@ -398,38 +392,17 @@ public function testUpdateLogoNormalLogoUpload($mimeType, $folderExists=true) {
398392
return $str;
399393
});
400394

401-
402-
$file = $this->createMock(ISimpleFile::class);
403-
$folder = $this->createMock(ISimpleFolder::class);
404-
if ($folderExists) {
405-
$this->appData
406-
->expects($this->once())
407-
->method('getFolder')
408-
->with('images')
409-
->willReturn($folder);
410-
} else {
411-
$this->appData
412-
->expects($this->at(0))
413-
->method('getFolder')
414-
->with('images')
415-
->willThrowException(new NotFoundException());
416-
$this->appData
417-
->expects($this->at(1))
418-
->method('newFolder')
419-
->with('images')
420-
->willReturn($folder);
421-
}
422-
$folder->expects($this->once())
423-
->method('newFile')
424-
->with('logo')
425-
->willReturn($file);
426395
$this->urlGenerator->expects($this->once())
427396
->method('linkTo')
428397
->willReturn('serverCss');
429398
$this->imageManager->expects($this->once())
430399
->method('getImageUrl')
431400
->with('logo')
432401
->willReturn('imageUrl');
402+
403+
$this->imageManager->expects($this->once())
404+
->method('updateImage');
405+
433406
$expected = new DataResponse(
434407
[
435408
'data' =>
@@ -474,30 +447,8 @@ public function testUpdateLogoLoginScreenUpload($folderExists) {
474447
return $str;
475448
});
476449

477-
$file = $this->createMock(ISimpleFile::class);
478-
$folder = $this->createMock(ISimpleFolder::class);
479-
if ($folderExists) {
480-
$this->appData
481-
->expects($this->once())
482-
->method('getFolder')
483-
->with('images')
484-
->willReturn($folder);
485-
} else {
486-
$this->appData
487-
->expects($this->at(0))
488-
->method('getFolder')
489-
->with('images')
490-
->willThrowException(new NotFoundException());
491-
$this->appData
492-
->expects($this->at(1))
493-
->method('newFolder')
494-
->with('images')
495-
->willReturn($folder);
496-
}
497-
$folder->expects($this->once())
498-
->method('newFile')
499-
->with('background')
500-
->willReturn($file);
450+
$this->imageManager->expects($this->once())
451+
->method('updateImage');
501452

502453
$this->urlGenerator->expects($this->once())
503454
->method('linkTo')
@@ -548,12 +499,9 @@ public function testUpdateLogoLoginScreenUploadWithInvalidImage() {
548499
return $str;
549500
});
550501

551-
$folder = $this->createMock(ISimpleFolder::class);
552-
$this->appData
553-
->expects($this->once())
554-
->method('getFolder')
555-
->with('images')
556-
->willReturn($folder);
502+
$this->imageManager->expects($this->once())
503+
->method('updateImage')
504+
->willThrowException(new \Exception('Unsupported image type'));
557505

558506
$expected = new DataResponse(
559507
[
@@ -723,9 +671,6 @@ public function testUndoDelete($value, $filename) {
723671
->method('linkTo')
724672
->with('', '/core/css/someHash-css-variables.scss')
725673
->willReturn('/nextcloudWebroot/core/css/someHash-css-variables.scss');
726-
$this->imageManager->expects($this->once())
727-
->method('delete')
728-
->with($filename);
729674

730675
$expected = new DataResponse(
731676
[

apps/theming/tests/ImageManagerTest.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
use OCP\ICacheFactory;
3636
use OCP\IConfig;
3737
use OCP\ILogger;
38+
use OCP\ITempManager;
3839
use OCP\IURLGenerator;
40+
use PHPUnit\Framework\MockObject\MockObject;
3941
use Test\TestCase;
4042

4143
class ImageManagerTest extends TestCase {
@@ -52,6 +54,8 @@ class ImageManagerTest extends TestCase {
5254
private $cacheFactory;
5355
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
5456
private $logger;
57+
/** @var ITempManager|MockObject */
58+
private $tempManager;
5559

5660
protected function setUp(): void {
5761
parent::setUp();
@@ -60,12 +64,14 @@ protected function setUp(): void {
6064
$this->urlGenerator = $this->createMock(IURLGenerator::class);
6165
$this->cacheFactory = $this->createMock(ICacheFactory::class);
6266
$this->logger = $this->createMock(ILogger::class);
67+
$this->tempManager = $this->createMock(ITempManager::class);
6368
$this->imageManager = new ImageManager(
6469
$this->config,
6570
$this->appData,
6671
$this->urlGenerator,
6772
$this->cacheFactory,
68-
$this->logger
73+
$this->logger,
74+
$this->tempManager
6975
);
7076
}
7177

@@ -326,4 +332,56 @@ public function testCleanup() {
326332
->willReturn($folders[2]);
327333
$this->imageManager->cleanup();
328334
}
335+
336+
337+
public function dataUpdateImage() {
338+
return [
339+
['background', __DIR__ . '/../../../tests/data/testimage.png', true, true],
340+
['background', __DIR__ . '/../../../tests/data/testimage.png', false, true],
341+
['background', __DIR__ . '/../../../tests/data/testimage.jpg', true, true],
342+
['logo', __DIR__ . '/../../../tests/data/testimagelarge.svg', true, false],
343+
];
344+
}
345+
346+
/**
347+
* @dataProvider dataUpdateImage
348+
*/
349+
public function testUpdateImage($key, $tmpFile, $folderExists, $shouldConvert) {
350+
$file = $this->createMock(ISimpleFile::class);
351+
$folder = $this->createMock(ISimpleFolder::class);
352+
$oldFile = $this->createMock(ISimpleFile::class);
353+
$folder->expects($this->any())
354+
->method('getFile')
355+
->willReturn($oldFile);
356+
if ($folderExists) {
357+
$this->appData
358+
->expects($this->any())
359+
->method('getFolder')
360+
->with('images')
361+
->willReturn($folder);
362+
} else {
363+
$this->appData
364+
->expects($this->any())
365+
->method('getFolder')
366+
->with('images')
367+
->willThrowException(new NotFoundException());
368+
$this->appData
369+
->expects($this->any())
370+
->method('newFolder')
371+
->with('images')
372+
->willReturn($folder);
373+
}
374+
$folder->expects($this->once())
375+
->method('newFile')
376+
->with($key)
377+
->willReturn($file);
378+
379+
if ($shouldConvert) {
380+
$this->tempManager->expects($this->once())
381+
->method('getTemporaryFile')
382+
->willReturn('/tmp/randomtempfile-theming');
383+
}
384+
385+
$this->imageManager->updateImage($key, $tmpFile);
386+
}
329387
}

0 commit comments

Comments
 (0)