From e41ec9dfe5634ba6b1d57386202d328d8363d47a Mon Sep 17 00:00:00 2001 From: Benedict Massolle Date: Tue, 19 Mar 2024 15:41:55 +0100 Subject: [PATCH] Add test case to check whether 'update' is actually called --- tests/AutoFileTemplatesTest.php | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/AutoFileTemplatesTest.php b/tests/AutoFileTemplatesTest.php index 32b73a7..57866e7 100644 --- a/tests/AutoFileTemplatesTest.php +++ b/tests/AutoFileTemplatesTest.php @@ -55,6 +55,69 @@ public function testSetsTemplateBasedOnCoreFileTypes(File $file, ?string $expect $this->assertEquals($expected, $template); } + public function testFileUpdatedMethodIsCalledOnce(): void + { + $kirby = new App([ + 'roots' => [ + 'index' => self::$tmpDir, + ], + 'options' => [ + 'presprog.auto-file-templates' => [ + 'autoAssign' => true, + ], + ], + ]); + + $service = new AutoFileTemplates($kirby, PluginOptions::createFromOptions($kirby->options())); + + // File One: An image file without a template + $fileOneProps = ['type' => 'image', 'filename' => 'image.png', 'parent' => self::page()]; + + $fileOne = $this->getMockBuilder(File::class) + ->disableOriginalConstructor() + ->setConstructorArgs($fileOneProps) + ->getMock() + ; + + $fileOne->expects($this->once()) + ->method('update') + ->with(['template' => 'image']) + ; + + $fileOne->method('type') + ->willReturn($fileOneProps['type']) + ; + + + $templateOne = $service->autoAssign($fileOne); + $this->assertEquals('image', $templateOne); + + // File Two An image file with template `photo` (will not be updated) + + $fileTwoProps = ['type' => 'image', 'filename' => 'image.png', 'parent' => self::page(), 'template' => 'photo']; + + $fileTwo = $this->getMockBuilder(File::class) + ->disableOriginalConstructor() + ->setConstructorArgs($fileTwoProps) + ->getMock() + ; + + $fileTwo->method('template') + ->willReturn($fileTwoProps['template']) + ; + + $fileTwo->expects($this->never()) + ->method('update') + ; + + $fileTwo->method('type') + ->willReturn($fileTwoProps['type']) + ; + + $templateTwo = $service->autoAssign($fileTwo); + $this->assertEquals(null, $templateTwo); + } + public function testDoesNothingIfTurnedOffByOption(): void { $options = [