diff --git a/cypress/e2e/templates.spec.js b/cypress/e2e/templates.spec.js index 10669c8fb9..5d237b5926 100644 --- a/cypress/e2e/templates.spec.js +++ b/cypress/e2e/templates.spec.js @@ -108,7 +108,7 @@ describe('Create templates with fields', () => { cy.login(randUser) cy.visit('/apps/files') - + // Create a templates folder cy.get('.files-list__header div[menu-title="New"] button') .should('be.visible') @@ -126,8 +126,9 @@ describe('Create templates with fields', () => { it('Create a document from a template with fields', () => { const fields = [ - { alias: 'Name', content: 'Nextcloud' }, - { alias: 'Favorite app', content: 'richdocuments' } + { type: 'rich-text', alias: 'Name', content: 'Nextcloud' }, + { type: 'rich-text', alias: 'Favorite app', content: 'richdocuments' }, + { type: 'checkbox', alias: 'Uses Nextcloud at home', checked: true }, ] cy.visit('/apps/files') diff --git a/cypress/fixtures/templates/document_template_with_fields.odt b/cypress/fixtures/templates/document_template_with_fields.odt index fb56a3adaa..5083b1b649 100644 Binary files a/cypress/fixtures/templates/document_template_with_fields.odt and b/cypress/fixtures/templates/document_template_with_fields.odt differ diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 7f8c31f41e..39e2ba55fa 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -311,9 +311,21 @@ Cypress.Commands.add('submitTemplateFields', (fields) => { cy.get('.template-field-modal__buttons').as('templateFillerButtons') for (const field of fields) { - cy.get('@templateFiller') - .find(`input[placeholder="${field.alias}"]`) - .type(field.content) + switch (field.type) { + case 'rich-text': + cy.get('@templateFiller') + .find(`input[placeholder="${field.alias}"]`) + .type(field.content) + break + case 'checkbox': + cy.get('@templateFiller') + .find('span.checkbox-radio-switch__text').contains(field.alias) + .click() + break + default: + expect.fail('Using a field type not yet supported') + break + } } // Submit the template fields @@ -332,11 +344,23 @@ Cypress.Commands.add('verifyTemplateFields', (fields, fileId) => { method: 'GET', url: Cypress.env('baseUrl') + apiEndpoint + fileId + '?format=json', headers: { - requesttoken + requesttoken, }, }).then(({ body }) => { for (const index in body.ocs.data) { - expect(body.ocs.data[index].content).to.equal(fields[index].content) + const field = body.ocs.data[index] + + switch (field.type) { + case 'rich-text': + expect(field.content).to.equal(fields[index].content) + break + case 'checkbox': + expect(field.checked).to.equal(fields[index].checked) + break + default: + expect.fail('Using a field type not yet supported') + break + } } }) }) diff --git a/lib/Service/TemplateFieldService.php b/lib/Service/TemplateFieldService.php index d9cfad8d38..f0d6a03e38 100644 --- a/lib/Service/TemplateFieldService.php +++ b/lib/Service/TemplateFieldService.php @@ -14,6 +14,7 @@ use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\Template\Field; +use OCP\Files\Template\FieldFactory; use OCP\Files\Template\FieldType; use OCP\Http\Client\IClientService; use OCP\ICacheFactory; @@ -94,16 +95,23 @@ public function extractFields(Node|int $file): array { continue; } - $fields[] = [ - new Field( - $index, - $attr['content'], - $fieldType, - $attr['alias'], - $attr['id'], - $attr['tag'] - ) - ]; + $field = FieldFactory::createField($index, $fieldType); + $field->id = $attr['id']; + $field->tag = $attr['tag']; + $field->alias = $attr['alias']; + + switch ($fieldType) { + case FieldType::RichText: + $field->setValue($attr['content']); + break; + case FieldType::CheckBox: + $field->setValue($attr['Checked'] === 'true'); + break; + default: + break; + } + + $fields[] = [$field]; } $fields = array_merge([], ...$fields);