Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…mplates into main
  • Loading branch information
bezin committed Apr 26, 2024
2 parents f0bba5f + 2dbabd0 commit ef9729b
Show file tree
Hide file tree
Showing 18 changed files with 948 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -14,4 +14,4 @@ trim_trailing_whitespace = false
insert_final_newline = false

[composer.json]
indent_size = 4
indent_size = 2
Empty file removed .github/.gitkeep
Empty file.
47 changes: 47 additions & 0 deletions .github/workflows/tests.yml
@@ -0,0 +1,47 @@
name: Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.2', '8.3' ]

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Install dev tools
run: composer run-script tools install

- name: Run test suite
run: composer run-script tests
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -28,7 +28,10 @@
/vendor/**/php4/*
/vendor/getkirby/composer-installer

# Tests
/tests/coverage
/.phpunit.cache

# Tools
/tools/**/vendor
/.php-cs-fixer.cache
/.phpunit.cache
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ This plugin automatically assigns file templates to your uploaded files, based o
> [!IMPORTANT]
> Requires at least Kirby 4.0 and PHP 8.2
## How to use
## 🚀 How to use

Set up your configuration (see next section) first. The plugin will then run after each uploaded file (`file.create:after` hook) and assign the configured template automatically.

Expand Down
13 changes: 9 additions & 4 deletions classes/AutoFileTemplates.php
Expand Up @@ -31,7 +31,7 @@ public function autoAssign(File $file): ?string
// Virtual admin user
$this->kirby->impersonate('kirby');

if ($template = $this->getTemplateForFile($file)) {
if (($template = $this->getTemplateForFile($file)) !== null) {
$file->update(['template' => $template]);
}

Expand All @@ -41,13 +41,18 @@ public function autoAssign(File $file): ?string
private function getTemplateForFile(File $file): ?string
{
$template = null;
$type = $file->type();

if ($this->templateMap[$file->type()] ?? null) {
$option = $this->templateMap[$file->type()] ?? null;
if (is_null($type)) {
return null;
}

if (($this->templateMap[$type] ?? null) !== null) {
$option = $this->templateMap[$type];

// Use file type as template name
if ($option === true) {
$template = $file->type();
$template = $type;
}

// Use specified templates
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -30,14 +30,17 @@
},
"scripts": {
"analyze": [
"@composer-validate",
"@tests",
"@psalm",
"@phpmd"
],
"composer-validate": "composer validate --strict --no-check-version --no-check-all",
"csfix": "@php tools/phpcs/vendor/bin/php-cs-fixer fix",
"psalm": "@php tools/psalm/vendor/bin/psalm",
"phpmd": "@php tools/phpmd/vendor/bin/phpmd . ansi phpmd.xml --exclude tools/*,tests/*,vendor/",
"psalm": "@php tools/psalm/vendor/bin/psalm",
"tests": "@php tools/phpunit/vendor/bin/phpunit --bootstrap=tests/bootstrap.php",
"tests:coverage": "XDEBUG_MODE=coverage tools/phpunit/vendor/bin/phpunit --bootstrap=tests/bootstrap.php --coverage-html=tests/coverage",
"tools": "@php tools/tools.php"
}
}
7 changes: 6 additions & 1 deletion extensions/commands/auto-templates.php
Expand Up @@ -19,6 +19,11 @@
'command' => function (CLI $cli) {
$kirby = $cli->kirby();

if (is_null($kirby)) {
$cli->climate()->error('No Kirby instance – something\'s wrong here.');
return;
}

// Virtual admin user
$kirby->impersonate('kirby');

Expand All @@ -36,7 +41,7 @@
foreach ($page->files() as $file) {
$template = $autoTemplates->autoAssign($file);

if (!$template) {
if (is_null($template)) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Expand Up @@ -4,6 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config tools/psalm/vendor/vimeo/psalm/config.xsd"
autoloader="tests/bootstrap.php"
>
<projectFiles>
<directory name="classes" />
Expand Down
123 changes: 122 additions & 1 deletion tests/AutoFileTemplatesTest.php
Expand Up @@ -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' => [
Expand All @@ -55,6 +55,106 @@ public function testSetsTemplateBasedOnCoreFileTypes(File $file, ?string $expect
$this->assertEquals($expected, $template);
}

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' => [
'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 = [
Expand Down Expand Up @@ -209,6 +309,27 @@ public function testOverwriteTemplateIfOptionIsSet(): void
$this->assertEquals('image', $service->autoAssign($image));
}

public function testFileTypeCannotBeDetermined(): void
{
$options = [
'autoAssign' => true,
];

$kirby = new App([
'roots' => [
'index' => self::$tmpDir,
],
'options' => [
'presprog.auto-file-templates' => $options,
],
]);

$service = new AutoFileTemplates($kirby, PluginOptions::createFromOptions($kirby->options()));
$image = new File(['type' => 'sometype', 'filename' => 'file', 'parent' => self::page()]);

$this->assertEquals(null, $service->autoAssign($image));
}

public static function files(): \Generator
{
$page = self::page();
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/site/blueprints/files/custom-type.yml
@@ -0,0 +1 @@
Title: Custom Type
3 changes: 2 additions & 1 deletion tools/kirby/composer.json
@@ -1,6 +1,7 @@
{
"require": {
"getkirby/cms": "^4.0"
"getkirby/cms": "^4.0",
"getkirby/cli": "^1.4"
},
"config": {
"allow-plugins": {
Expand Down

0 comments on commit ef9729b

Please sign in to comment.