From 2dbabd06737d2984e6af26bdf2662f09b1046d61 Mon Sep 17 00:00:00 2001 From: Benedict Massolle Date: Fri, 22 Mar 2024 08:21:06 +0100 Subject: [PATCH] Add test case for custom types --- tests/AutoFileTemplatesTest.php | 41 ++++++++++++++++++- .../site/blueprints/files/custom-type.yml | 1 + 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/site/blueprints/files/custom-type.yml diff --git a/tests/AutoFileTemplatesTest.php b/tests/AutoFileTemplatesTest.php index 57866e7..aedefac 100644 --- a/tests/AutoFileTemplatesTest.php +++ b/tests/AutoFileTemplatesTest.php @@ -37,7 +37,7 @@ protected function tearDown(): void } #[DataProvider('files')] - public function testSetsTemplateBasedOnCoreFileTypes(File $file, ?string $expected): void + public function testSetsTemplateForCoreFileTypes(File $file, ?string $expected): void { $kirby = new App([ 'roots' => [ @@ -55,7 +55,44 @@ public function testSetsTemplateBasedOnCoreFileTypes(File $file, ?string $expect $this->assertEquals($expected, $template); } - public function testFileUpdatedMethodIsCalledOnce(): void + public function testSetsTemplateForCustomFileTypes(): void + { + $kirby = new App([ + 'roots' => [ + 'index' => self::$tmpDir, + ], + 'options' => [ + 'presprog.auto-file-templates' => [ + 'autoAssign' => true, + ], + ], + ]); + + F::$types['custom-type'] = ['custom']; + + $props = ['type' => 'custom-type', 'filename' => 'image.custom', 'parent' => self::page()]; + + $file = $this->getMockBuilder(File::class) + ->disableOriginalConstructor() + ->setConstructorArgs($props) + ->getMock() + ; + + $file->expects($this->once()) + ->method('update') + ->with(['template' => 'custom-type']) + ; + + $file->method('type') + ->willReturn($props['type']) + ; + + $service = new AutoFileTemplates($kirby, PluginOptions::createFromOptions($kirby->options())); + $template = $service->autoAssign($file); + $this->assertEquals('custom-type', $template); + } + + public function testFileIsUpdated(): void { $kirby = new App([ 'roots' => [ diff --git a/tests/fixtures/site/blueprints/files/custom-type.yml b/tests/fixtures/site/blueprints/files/custom-type.yml new file mode 100644 index 0000000..ac61fb8 --- /dev/null +++ b/tests/fixtures/site/blueprints/files/custom-type.yml @@ -0,0 +1 @@ +Title: Custom Type